TemplateFunctionsTest.php 8.0 KB

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