TemplateFunctionsTest.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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. public function testEmitScriptTagWithContent() {
  52. $this->expectOutputRegex('/<script nonce="[^"]+">\nalert\(\)\n<\/script>\n?/');
  53. emit_script_tag('', 'alert()');
  54. }
  55. public function testEmitScriptTagWithSource() {
  56. $this->expectOutputRegex('/<script nonce=".*" defer src="some.js"><\/script>/');
  57. emit_script_tag('some.js');
  58. }
  59. public function testEmitScriptTagWithModuleSource() {
  60. $this->expectOutputRegex('/<script nonce=".*" defer src="some.mjs" type="module"><\/script>/');
  61. emit_script_tag('some.mjs', '', 'module');
  62. }
  63. public function testEmitScriptLoadingTags() {
  64. // Test mjs js and inline content
  65. $pattern = '/src="some\.mjs"[^>]+type="module"[^>]*>.+\n'; // some.mjs with type = module
  66. $pattern .= '<script[^>]+src="other\.js"[^>]*>.+\n'; // other.js as plain javascript
  67. $pattern .= '<script[^>]*>\n?.*inline.*\n?<\/script>'; // inline content
  68. $pattern .= '/'; // no flags
  69. $this->expectOutputRegex($pattern);
  70. emit_script_loading_tags([
  71. 'jsfiles' => ['some.mjs', 'other.js'],
  72. 'inline_ocjs' => '// inline'
  73. ]);
  74. }
  75. public function testEmitScriptLoadingTagsWithVersion() {
  76. // Test mjs js and inline content
  77. $pattern = '/src="some\.mjs\?v=ab123cd"[^>]+type="module"[^>]*>.+\n'; // some.mjs with type = module
  78. $pattern .= '<script[^>]+src="other\.js\?v=12abc34"[^>]*>.+\n'; // other.js as plain javascript
  79. $pattern .= '/'; // no flags
  80. $this->expectOutputRegex($pattern);
  81. emit_script_loading_tags([
  82. 'jsfiles' => ['some.mjs?v=ab123cd', 'other.js?v=12abc34'],
  83. ]);
  84. }
  85. // ---------------------------------------------------------------------------
  86. // Test relative_modified_date with dates only
  87. // ---------------------------------------------------------------------------
  88. public function testRelativeDateToday() {
  89. $currentTime = 1380703592;
  90. $elementTime = $currentTime;
  91. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  92. $this->assertEquals('today', $result);
  93. // 2 hours ago is still today
  94. $elementTime = $currentTime - 2 * 3600;
  95. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  96. $this->assertEquals('today', $result);
  97. }
  98. public function testRelativeDateYesterday() {
  99. $currentTime = 1380703592;
  100. $elementTime = $currentTime - 24 * 3600;
  101. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  102. $this->assertEquals('yesterday', $result);
  103. // yesterday - 2 hours is still yesterday
  104. $elementTime = $currentTime - 26 * 3600;
  105. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  106. $this->assertEquals('yesterday', $result);
  107. }
  108. public function testRelativeDate2DaysAgo() {
  109. $currentTime = 1380703592;
  110. $elementTime = $currentTime - 48 * 3600;
  111. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  112. $this->assertEquals('2 days ago', $result);
  113. // 2 days ago minus 4 hours is still 2 days ago
  114. $elementTime = $currentTime - 52 * 3600;
  115. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  116. $this->assertEquals('2 days ago', $result);
  117. }
  118. public function testRelativeDateLastMonth() {
  119. $currentTime = 1380703592;
  120. $elementTime = $currentTime - 86400 * 31;
  121. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  122. $this->assertEquals('last month', $result);
  123. $elementTime = $currentTime - 86400 * 35;
  124. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  125. $this->assertEquals('last month', $result);
  126. }
  127. public function testRelativeDateMonthsAgo() {
  128. $currentTime = 1380703592;
  129. $elementTime = $currentTime - 86400 * 65;
  130. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  131. $this->assertEquals('2 months ago', $result);
  132. $elementTime = $currentTime - 86400 * 130;
  133. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  134. $this->assertEquals('4 months ago', $result);
  135. }
  136. public function testRelativeDateLastYear() {
  137. $currentTime = 1380703592;
  138. $elementTime = $currentTime - 86400 * 365;
  139. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  140. $this->assertEquals('last year', $result);
  141. $elementTime = $currentTime - 86400 * 450;
  142. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  143. $this->assertEquals('last year', $result);
  144. }
  145. public function testRelativeDateYearsAgo() {
  146. $currentTime = 1380703592;
  147. $elementTime = $currentTime - 86400 * 365.25 * 2;
  148. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  149. $this->assertEquals('2 years ago', $result);
  150. $elementTime = $currentTime - 86400 * 365.25 * 3;
  151. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  152. $this->assertEquals('3 years ago', $result);
  153. }
  154. // ---------------------------------------------------------------------------
  155. // Test relative_modified_date with timestamps only (date + time value)
  156. // ---------------------------------------------------------------------------
  157. public function testRelativeTimeSecondsAgo() {
  158. $currentTime = 1380703592;
  159. $elementTime = $currentTime - 5;
  160. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  161. $this->assertEquals('seconds ago', $result);
  162. }
  163. public function testRelativeTimeMinutesAgo() {
  164. $currentTime = 1380703592;
  165. $elementTime = $currentTime - 190;
  166. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  167. $this->assertEquals('3 minutes ago', $result);
  168. }
  169. public function testRelativeTimeHoursAgo() {
  170. $currentTime = 1380703592;
  171. $elementTime = $currentTime - 7500;
  172. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  173. $this->assertEquals('2 hours ago', $result);
  174. }
  175. public function testRelativeTime2DaysAgo() {
  176. $currentTime = 1380703592;
  177. $elementTime = $currentTime - 48 * 3600;
  178. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  179. $this->assertEquals('2 days ago', $result);
  180. // 2 days ago minus 4 hours is still 2 days ago
  181. $elementTime = $currentTime - 52 * 3600;
  182. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  183. $this->assertEquals('2 days ago', $result);
  184. }
  185. public function testRelativeTimeLastMonth() {
  186. $currentTime = 1380703592;
  187. $elementTime = $currentTime - 86400 * 31;
  188. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  189. $this->assertEquals('last month', $result);
  190. $elementTime = $currentTime - 86400 * 35;
  191. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  192. $this->assertEquals('last month', $result);
  193. }
  194. public function testRelativeTimeMonthsAgo() {
  195. $currentTime = 1380703592;
  196. $elementTime = $currentTime - 86400 * 65;
  197. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  198. $this->assertEquals('2 months ago', $result);
  199. $elementTime = $currentTime - 86400 * 130;
  200. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  201. $this->assertEquals('4 months ago', $result);
  202. }
  203. public function testRelativeTimeLastYear() {
  204. $currentTime = 1380703592;
  205. $elementTime = $currentTime - 86400 * 365;
  206. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  207. $this->assertEquals('last year', $result);
  208. $elementTime = $currentTime - 86400 * 450;
  209. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  210. $this->assertEquals('last year', $result);
  211. }
  212. public function testRelativeTimeYearsAgo() {
  213. $currentTime = 1380703592;
  214. $elementTime = $currentTime - 86400 * 365.25 * 2;
  215. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  216. $this->assertEquals('2 years ago', $result);
  217. $elementTime = $currentTime - 86400 * 365.25 * 3;
  218. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  219. $this->assertEquals('3 years ago', $result);
  220. }
  221. }