template.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Bernhard Posselt
  6. * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. class Test_TemplateFunctions extends PHPUnit_Framework_TestCase {
  23. public function setUp() {
  24. $loader = new \OC\Autoloader();
  25. $loader->load('OC_Template');
  26. }
  27. public function testPJavaScript() {
  28. $this->expectOutputString('&lt;img onload=&quot;alert(1)&quot; /&gt;');
  29. p('<img onload="alert(1)" />');
  30. }
  31. public function testPJavaScriptWithScriptTags() {
  32. $this->expectOutputString('&lt;script&gt;alert(&#039;Hacked!&#039;);&lt;/script&gt;');
  33. p("<script>alert('Hacked!');</script>");
  34. }
  35. public function testPNormalString() {
  36. $string = 'This is a good string without HTML.';
  37. $this->expectOutputString($string);
  38. p($string);
  39. }
  40. public function testPrintUnescaped() {
  41. $htmlString = "<script>alert('xss');</script>";
  42. $this->expectOutputString($htmlString);
  43. print_unescaped($htmlString);
  44. }
  45. public function testPrintUnescapedNormalString() {
  46. $string = 'This is a good string!';
  47. $this->expectOutputString($string);
  48. print_unescaped($string);
  49. }
  50. // ---------------------------------------------------------------------------
  51. // Test relative_modified_date with dates only
  52. // ---------------------------------------------------------------------------
  53. public function testRelativeDateToday(){
  54. $currentTime = 1380703592;
  55. $elementTime = $currentTime;
  56. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  57. $this->assertEquals('today', $result);
  58. // 2 hours ago is still today
  59. $elementTime = $currentTime - 2 * 3600;
  60. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  61. $this->assertEquals('today', $result);
  62. }
  63. public function testRelativeDateYesterday(){
  64. $currentTime = 1380703592;
  65. $elementTime = $currentTime - 24 * 3600;
  66. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  67. $this->assertEquals('yesterday', $result);
  68. // yesterday - 2 hours is still yesterday
  69. $elementTime = $currentTime - 26 * 3600;
  70. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  71. $this->assertEquals('yesterday', $result);
  72. }
  73. public function testRelativeDate2DaysAgo(){
  74. $currentTime = 1380703592;
  75. $elementTime = $currentTime - 48 * 3600;
  76. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  77. $this->assertEquals('2 days ago', $result);
  78. // 2 days ago minus 4 hours is still 2 days ago
  79. $elementTime = $currentTime - 52 * 3600;
  80. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  81. $this->assertEquals('2 days ago', $result);
  82. }
  83. public function testRelativeDateLastMonth(){
  84. $currentTime = 1380703592;
  85. $elementTime = $currentTime - 86400 * 31;
  86. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  87. $this->assertEquals('last month', $result);
  88. $elementTime = $currentTime - 86400 * 35;
  89. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  90. $this->assertEquals('last month', $result);
  91. }
  92. public function testRelativeDateMonthsAgo(){
  93. $currentTime = 1380703592;
  94. $elementTime = $currentTime - 86400 * 60;
  95. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  96. $this->assertEquals('2 months ago', $result);
  97. $elementTime = $currentTime - 86400 * 65;
  98. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  99. $this->assertEquals('2 months ago', $result);
  100. }
  101. public function testRelativeDateLastYear(){
  102. $currentTime = 1380703592;
  103. $elementTime = $currentTime - 86400 * 365;
  104. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  105. $this->assertEquals('last year', $result);
  106. $elementTime = $currentTime - 86400 * 450;
  107. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  108. $this->assertEquals('last year', $result);
  109. }
  110. public function testRelativeDateYearsAgo(){
  111. $currentTime = 1380703592;
  112. $elementTime = $currentTime - 86400 * 365.25 * 2;
  113. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  114. $this->assertEquals('years ago', $result);
  115. $elementTime = $currentTime - 86400 * 365.25 * 3;
  116. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  117. $this->assertEquals('years ago', $result);
  118. }
  119. // ---------------------------------------------------------------------------
  120. // Test relative_modified_date with timestamps only (date + time value)
  121. // ---------------------------------------------------------------------------
  122. public function testRelativeTimeSecondsAgo(){
  123. $currentTime = 1380703592;
  124. $elementTime = $currentTime - 5;
  125. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  126. $this->assertEquals('seconds ago', $result);
  127. }
  128. public function testRelativeTimeMinutesAgo(){
  129. $currentTime = 1380703592;
  130. $elementTime = $currentTime - 190;
  131. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  132. $this->assertEquals('3 minutes ago', $result);
  133. }
  134. public function testRelativeTimeHoursAgo(){
  135. $currentTime = 1380703592;
  136. $elementTime = $currentTime - 7500;
  137. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  138. $this->assertEquals('2 hours ago', $result);
  139. }
  140. public function testRelativeTime2DaysAgo(){
  141. $currentTime = 1380703592;
  142. $elementTime = $currentTime - 48 * 3600;
  143. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  144. $this->assertEquals('2 days ago', $result);
  145. // 2 days ago minus 4 hours is still 2 days ago
  146. $elementTime = $currentTime - 52 * 3600;
  147. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  148. $this->assertEquals('2 days ago', $result);
  149. }
  150. public function testRelativeTimeLastMonth(){
  151. $currentTime = 1380703592;
  152. $elementTime = $currentTime - 86400 * 31;
  153. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  154. $this->assertEquals('last month', $result);
  155. $elementTime = $currentTime - 86400 * 35;
  156. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  157. $this->assertEquals('last month', $result);
  158. }
  159. public function testRelativeTimeMonthsAgo(){
  160. $currentTime = 1380703592;
  161. $elementTime = $currentTime - 86400 * 60;
  162. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  163. $this->assertEquals('2 months ago', $result);
  164. $elementTime = $currentTime - 86400 * 65;
  165. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  166. $this->assertEquals('2 months ago', $result);
  167. }
  168. public function testRelativeTimeLastYear(){
  169. $currentTime = 1380703592;
  170. $elementTime = $currentTime - 86400 * 365;
  171. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  172. $this->assertEquals('last year', $result);
  173. $elementTime = $currentTime - 86400 * 450;
  174. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  175. $this->assertEquals('last year', $result);
  176. }
  177. public function testRelativeTimeYearsAgo(){
  178. $currentTime = 1380703592;
  179. $elementTime = $currentTime - 86400 * 365.25 * 2;
  180. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  181. $this->assertEquals('years ago', $result);
  182. $elementTime = $currentTime - 86400 * 365.25 * 3;
  183. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  184. $this->assertEquals('years ago', $result);
  185. }
  186. }