TemplateFunctionsTest.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test;
  8. class TemplateFunctionsTest extends \Test\TestCase {
  9. protected function setUp(): void {
  10. parent::setUp();
  11. require_once \OC::$SERVERROOT . '/lib/private/legacy/OC_Template.php';
  12. }
  13. public function testPJavaScript(): void {
  14. $this->expectOutputString('&lt;img onload=&quot;alert(1)&quot; /&gt;');
  15. p('<img onload="alert(1)" />');
  16. }
  17. public function testPJavaScriptWithScriptTags(): void {
  18. $this->expectOutputString('&lt;script&gt;alert(&#039;Hacked!&#039;);&lt;/script&gt;');
  19. p("<script>alert('Hacked!');</script>");
  20. }
  21. public function testPNormalString(): void {
  22. $string = 'This is a good string without HTML.';
  23. $this->expectOutputString($string);
  24. p($string);
  25. }
  26. public function testPrintUnescaped(): void {
  27. $htmlString = "<script>alert('xss');</script>";
  28. $this->expectOutputString($htmlString);
  29. print_unescaped($htmlString);
  30. }
  31. public function testPrintUnescapedNormalString(): void {
  32. $string = 'This is a good string!';
  33. $this->expectOutputString($string);
  34. print_unescaped($string);
  35. }
  36. public function testEmitScriptTagWithContent(): void {
  37. $this->expectOutputRegex('/<script nonce="[^"]+">\nalert\(\)\n<\/script>\n?/');
  38. emit_script_tag('', 'alert()');
  39. }
  40. public function testEmitScriptTagWithSource(): void {
  41. $this->expectOutputRegex('/<script nonce=".*" defer src="some.js"><\/script>/');
  42. emit_script_tag('some.js');
  43. }
  44. public function testEmitScriptTagWithModuleSource(): void {
  45. $this->expectOutputRegex('/<script nonce=".*" defer src="some.mjs" type="module"><\/script>/');
  46. emit_script_tag('some.mjs', '', 'module');
  47. }
  48. public function testEmitScriptLoadingTags(): void {
  49. // Test mjs js and inline content
  50. $pattern = '/src="some\.mjs"[^>]+type="module"[^>]*>.+\n'; // some.mjs with type = module
  51. $pattern .= '<script[^>]+src="other\.js"[^>]*>.+\n'; // other.js as plain javascript
  52. $pattern .= '<script[^>]*>\n?.*inline.*\n?<\/script>'; // inline content
  53. $pattern .= '/'; // no flags
  54. $this->expectOutputRegex($pattern);
  55. emit_script_loading_tags([
  56. 'jsfiles' => ['some.mjs', 'other.js'],
  57. 'inline_ocjs' => '// inline'
  58. ]);
  59. }
  60. public function testEmitScriptLoadingTagsWithVersion(): void {
  61. // Test mjs js and inline content
  62. $pattern = '/src="some\.mjs\?v=ab123cd"[^>]+type="module"[^>]*>.+\n'; // some.mjs with type = module
  63. $pattern .= '<script[^>]+src="other\.js\?v=12abc34"[^>]*>.+\n'; // other.js as plain javascript
  64. $pattern .= '/'; // no flags
  65. $this->expectOutputRegex($pattern);
  66. emit_script_loading_tags([
  67. 'jsfiles' => ['some.mjs?v=ab123cd', 'other.js?v=12abc34'],
  68. ]);
  69. }
  70. // ---------------------------------------------------------------------------
  71. // Test relative_modified_date with dates only
  72. // ---------------------------------------------------------------------------
  73. public function testRelativeDateToday(): void {
  74. $currentTime = 1380703592;
  75. $elementTime = $currentTime;
  76. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  77. $this->assertEquals('today', $result);
  78. // 2 hours ago is still today
  79. $elementTime = $currentTime - 2 * 3600;
  80. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  81. $this->assertEquals('today', $result);
  82. }
  83. public function testRelativeDateYesterday(): void {
  84. $currentTime = 1380703592;
  85. $elementTime = $currentTime - 24 * 3600;
  86. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  87. $this->assertEquals('yesterday', $result);
  88. // yesterday - 2 hours is still yesterday
  89. $elementTime = $currentTime - 26 * 3600;
  90. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  91. $this->assertEquals('yesterday', $result);
  92. }
  93. public function testRelativeDate2DaysAgo(): void {
  94. $currentTime = 1380703592;
  95. $elementTime = $currentTime - 48 * 3600;
  96. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  97. $this->assertEquals('2 days ago', $result);
  98. // 2 days ago minus 4 hours is still 2 days ago
  99. $elementTime = $currentTime - 52 * 3600;
  100. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  101. $this->assertEquals('2 days ago', $result);
  102. }
  103. public function testRelativeDateLastMonth(): void {
  104. $currentTime = 1380703592;
  105. $elementTime = $currentTime - 86400 * 31;
  106. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  107. $this->assertEquals('last month', $result);
  108. $elementTime = $currentTime - 86400 * 35;
  109. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  110. $this->assertEquals('last month', $result);
  111. }
  112. public function testRelativeDateMonthsAgo(): void {
  113. $currentTime = 1380703592;
  114. $elementTime = $currentTime - 86400 * 65;
  115. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  116. $this->assertEquals('2 months ago', $result);
  117. $elementTime = $currentTime - 86400 * 130;
  118. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  119. $this->assertEquals('4 months ago', $result);
  120. }
  121. public function testRelativeDateLastYear(): void {
  122. $currentTime = 1380703592;
  123. $elementTime = $currentTime - 86400 * 365;
  124. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  125. $this->assertEquals('last year', $result);
  126. $elementTime = $currentTime - 86400 * 450;
  127. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  128. $this->assertEquals('last year', $result);
  129. }
  130. public function testRelativeDateYearsAgo(): void {
  131. $currentTime = 1380703592;
  132. $elementTime = $currentTime - 86400 * 365.25 * 2;
  133. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  134. $this->assertEquals('2 years ago', $result);
  135. $elementTime = $currentTime - 86400 * 365.25 * 3;
  136. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  137. $this->assertEquals('3 years ago', $result);
  138. }
  139. // ---------------------------------------------------------------------------
  140. // Test relative_modified_date with timestamps only (date + time value)
  141. // ---------------------------------------------------------------------------
  142. public function testRelativeTimeSecondsAgo(): void {
  143. $currentTime = 1380703592;
  144. $elementTime = $currentTime - 5;
  145. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  146. $this->assertEquals('seconds ago', $result);
  147. }
  148. public function testRelativeTimeMinutesAgo(): void {
  149. $currentTime = 1380703592;
  150. $elementTime = $currentTime - 190;
  151. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  152. $this->assertEquals('3 minutes ago', $result);
  153. }
  154. public function testRelativeTimeHoursAgo(): void {
  155. $currentTime = 1380703592;
  156. $elementTime = $currentTime - 7500;
  157. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  158. $this->assertEquals('2 hours ago', $result);
  159. }
  160. public function testRelativeTime2DaysAgo(): void {
  161. $currentTime = 1380703592;
  162. $elementTime = $currentTime - 48 * 3600;
  163. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  164. $this->assertEquals('2 days ago', $result);
  165. // 2 days ago minus 4 hours is still 2 days ago
  166. $elementTime = $currentTime - 52 * 3600;
  167. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  168. $this->assertEquals('2 days ago', $result);
  169. }
  170. public function testRelativeTimeLastMonth(): void {
  171. $currentTime = 1380703592;
  172. $elementTime = $currentTime - 86400 * 31;
  173. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  174. $this->assertEquals('last month', $result);
  175. $elementTime = $currentTime - 86400 * 35;
  176. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  177. $this->assertEquals('last month', $result);
  178. }
  179. public function testRelativeTimeMonthsAgo(): void {
  180. $currentTime = 1380703592;
  181. $elementTime = $currentTime - 86400 * 65;
  182. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  183. $this->assertEquals('2 months ago', $result);
  184. $elementTime = $currentTime - 86400 * 130;
  185. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  186. $this->assertEquals('4 months ago', $result);
  187. }
  188. public function testRelativeTimeLastYear(): void {
  189. $currentTime = 1380703592;
  190. $elementTime = $currentTime - 86400 * 365;
  191. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  192. $this->assertEquals('last year', $result);
  193. $elementTime = $currentTime - 86400 * 450;
  194. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  195. $this->assertEquals('last year', $result);
  196. }
  197. public function testRelativeTimeYearsAgo(): void {
  198. $currentTime = 1380703592;
  199. $elementTime = $currentTime - 86400 * 365.25 * 2;
  200. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  201. $this->assertEquals('2 years ago', $result);
  202. $elementTime = $currentTime - 86400 * 365.25 * 3;
  203. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  204. $this->assertEquals('3 years ago', $result);
  205. }
  206. }