1
0

TemplateFunctionsTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. namespace Test;
  23. class TemplateFunctionsTest extends \Test\TestCase {
  24. protected function setUp() {
  25. parent::setUp();
  26. $loader = new \OC\Autoloader([\OC::$SERVERROOT . '/lib']);
  27. $loader->load('OC_Template');
  28. }
  29. public function testPJavaScript() {
  30. $this->expectOutputString('&lt;img onload=&quot;alert(1)&quot; /&gt;');
  31. p('<img onload="alert(1)" />');
  32. }
  33. public function testPJavaScriptWithScriptTags() {
  34. $this->expectOutputString('&lt;script&gt;alert(&#039;Hacked!&#039;);&lt;/script&gt;');
  35. p("<script>alert('Hacked!');</script>");
  36. }
  37. public function testPNormalString() {
  38. $string = 'This is a good string without HTML.';
  39. $this->expectOutputString($string);
  40. p($string);
  41. }
  42. public function testPrintUnescaped() {
  43. $htmlString = "<script>alert('xss');</script>";
  44. $this->expectOutputString($htmlString);
  45. print_unescaped($htmlString);
  46. }
  47. public function testPrintUnescapedNormalString() {
  48. $string = 'This is a good string!';
  49. $this->expectOutputString($string);
  50. print_unescaped($string);
  51. }
  52. // ---------------------------------------------------------------------------
  53. // Test relative_modified_date with dates only
  54. // ---------------------------------------------------------------------------
  55. public function testRelativeDateToday() {
  56. $currentTime = 1380703592;
  57. $elementTime = $currentTime;
  58. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  59. $this->assertEquals('today', $result);
  60. // 2 hours ago is still today
  61. $elementTime = $currentTime - 2 * 3600;
  62. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  63. $this->assertEquals('today', $result);
  64. }
  65. public function testRelativeDateYesterday() {
  66. $currentTime = 1380703592;
  67. $elementTime = $currentTime - 24 * 3600;
  68. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  69. $this->assertEquals('yesterday', $result);
  70. // yesterday - 2 hours is still yesterday
  71. $elementTime = $currentTime - 26 * 3600;
  72. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  73. $this->assertEquals('yesterday', $result);
  74. }
  75. public function testRelativeDate2DaysAgo() {
  76. $currentTime = 1380703592;
  77. $elementTime = $currentTime - 48 * 3600;
  78. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  79. $this->assertEquals('2 days ago', $result);
  80. // 2 days ago minus 4 hours is still 2 days ago
  81. $elementTime = $currentTime - 52 * 3600;
  82. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  83. $this->assertEquals('2 days ago', $result);
  84. }
  85. public function testRelativeDateLastMonth() {
  86. $currentTime = 1380703592;
  87. $elementTime = $currentTime - 86400 * 31;
  88. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  89. $this->assertEquals('last month', $result);
  90. $elementTime = $currentTime - 86400 * 35;
  91. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  92. $this->assertEquals('last month', $result);
  93. }
  94. public function testRelativeDateMonthsAgo() {
  95. $currentTime = 1380703592;
  96. $elementTime = $currentTime - 86400 * 65;
  97. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  98. $this->assertEquals('2 months ago', $result);
  99. $elementTime = $currentTime - 86400 * 130;
  100. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  101. $this->assertEquals('4 months ago', $result);
  102. }
  103. public function testRelativeDateLastYear() {
  104. $currentTime = 1380703592;
  105. $elementTime = $currentTime - 86400 * 365;
  106. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  107. $this->assertEquals('last year', $result);
  108. $elementTime = $currentTime - 86400 * 450;
  109. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  110. $this->assertEquals('last year', $result);
  111. }
  112. public function testRelativeDateYearsAgo() {
  113. $currentTime = 1380703592;
  114. $elementTime = $currentTime - 86400 * 365.25 * 2;
  115. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  116. $this->assertEquals('2 years ago', $result);
  117. $elementTime = $currentTime - 86400 * 365.25 * 3;
  118. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  119. $this->assertEquals('3 years ago', $result);
  120. }
  121. // ---------------------------------------------------------------------------
  122. // Test relative_modified_date with timestamps only (date + time value)
  123. // ---------------------------------------------------------------------------
  124. public function testRelativeTimeSecondsAgo() {
  125. $currentTime = 1380703592;
  126. $elementTime = $currentTime - 5;
  127. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  128. $this->assertEquals('seconds ago', $result);
  129. }
  130. public function testRelativeTimeMinutesAgo() {
  131. $currentTime = 1380703592;
  132. $elementTime = $currentTime - 190;
  133. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  134. $this->assertEquals('3 minutes ago', $result);
  135. }
  136. public function testRelativeTimeHoursAgo() {
  137. $currentTime = 1380703592;
  138. $elementTime = $currentTime - 7500;
  139. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  140. $this->assertEquals('2 hours ago', $result);
  141. }
  142. public function testRelativeTime2DaysAgo() {
  143. $currentTime = 1380703592;
  144. $elementTime = $currentTime - 48 * 3600;
  145. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  146. $this->assertEquals('2 days ago', $result);
  147. // 2 days ago minus 4 hours is still 2 days ago
  148. $elementTime = $currentTime - 52 * 3600;
  149. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  150. $this->assertEquals('2 days ago', $result);
  151. }
  152. public function testRelativeTimeLastMonth() {
  153. $currentTime = 1380703592;
  154. $elementTime = $currentTime - 86400 * 31;
  155. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  156. $this->assertEquals('last month', $result);
  157. $elementTime = $currentTime - 86400 * 35;
  158. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  159. $this->assertEquals('last month', $result);
  160. }
  161. public function testRelativeTimeMonthsAgo() {
  162. $currentTime = 1380703592;
  163. $elementTime = $currentTime - 86400 * 65;
  164. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  165. $this->assertEquals('2 months ago', $result);
  166. $elementTime = $currentTime - 86400 * 130;
  167. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  168. $this->assertEquals('4 months ago', $result);
  169. }
  170. public function testRelativeTimeLastYear() {
  171. $currentTime = 1380703592;
  172. $elementTime = $currentTime - 86400 * 365;
  173. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  174. $this->assertEquals('last year', $result);
  175. $elementTime = $currentTime - 86400 * 450;
  176. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  177. $this->assertEquals('last year', $result);
  178. }
  179. public function testRelativeTimeYearsAgo() {
  180. $currentTime = 1380703592;
  181. $elementTime = $currentTime - 86400 * 365.25 * 2;
  182. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  183. $this->assertEquals('2 years ago', $result);
  184. $elementTime = $currentTime - 86400 * 365.25 * 3;
  185. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  186. $this->assertEquals('3 years ago', $result);
  187. }
  188. }