1
0

TemplateFunctionsTest.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. // ---------------------------------------------------------------------------
  76. // Test relative_modified_date with dates only
  77. // ---------------------------------------------------------------------------
  78. public function testRelativeDateToday() {
  79. $currentTime = 1380703592;
  80. $elementTime = $currentTime;
  81. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  82. $this->assertEquals('today', $result);
  83. // 2 hours ago is still today
  84. $elementTime = $currentTime - 2 * 3600;
  85. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  86. $this->assertEquals('today', $result);
  87. }
  88. public function testRelativeDateYesterday() {
  89. $currentTime = 1380703592;
  90. $elementTime = $currentTime - 24 * 3600;
  91. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  92. $this->assertEquals('yesterday', $result);
  93. // yesterday - 2 hours is still yesterday
  94. $elementTime = $currentTime - 26 * 3600;
  95. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  96. $this->assertEquals('yesterday', $result);
  97. }
  98. public function testRelativeDate2DaysAgo() {
  99. $currentTime = 1380703592;
  100. $elementTime = $currentTime - 48 * 3600;
  101. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  102. $this->assertEquals('2 days ago', $result);
  103. // 2 days ago minus 4 hours is still 2 days ago
  104. $elementTime = $currentTime - 52 * 3600;
  105. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  106. $this->assertEquals('2 days ago', $result);
  107. }
  108. public function testRelativeDateLastMonth() {
  109. $currentTime = 1380703592;
  110. $elementTime = $currentTime - 86400 * 31;
  111. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  112. $this->assertEquals('last month', $result);
  113. $elementTime = $currentTime - 86400 * 35;
  114. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  115. $this->assertEquals('last month', $result);
  116. }
  117. public function testRelativeDateMonthsAgo() {
  118. $currentTime = 1380703592;
  119. $elementTime = $currentTime - 86400 * 65;
  120. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  121. $this->assertEquals('2 months ago', $result);
  122. $elementTime = $currentTime - 86400 * 130;
  123. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  124. $this->assertEquals('4 months ago', $result);
  125. }
  126. public function testRelativeDateLastYear() {
  127. $currentTime = 1380703592;
  128. $elementTime = $currentTime - 86400 * 365;
  129. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  130. $this->assertEquals('last year', $result);
  131. $elementTime = $currentTime - 86400 * 450;
  132. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  133. $this->assertEquals('last year', $result);
  134. }
  135. public function testRelativeDateYearsAgo() {
  136. $currentTime = 1380703592;
  137. $elementTime = $currentTime - 86400 * 365.25 * 2;
  138. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  139. $this->assertEquals('2 years ago', $result);
  140. $elementTime = $currentTime - 86400 * 365.25 * 3;
  141. $result = (string)relative_modified_date($elementTime, $currentTime, true);
  142. $this->assertEquals('3 years ago', $result);
  143. }
  144. // ---------------------------------------------------------------------------
  145. // Test relative_modified_date with timestamps only (date + time value)
  146. // ---------------------------------------------------------------------------
  147. public function testRelativeTimeSecondsAgo() {
  148. $currentTime = 1380703592;
  149. $elementTime = $currentTime - 5;
  150. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  151. $this->assertEquals('seconds ago', $result);
  152. }
  153. public function testRelativeTimeMinutesAgo() {
  154. $currentTime = 1380703592;
  155. $elementTime = $currentTime - 190;
  156. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  157. $this->assertEquals('3 minutes ago', $result);
  158. }
  159. public function testRelativeTimeHoursAgo() {
  160. $currentTime = 1380703592;
  161. $elementTime = $currentTime - 7500;
  162. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  163. $this->assertEquals('2 hours ago', $result);
  164. }
  165. public function testRelativeTime2DaysAgo() {
  166. $currentTime = 1380703592;
  167. $elementTime = $currentTime - 48 * 3600;
  168. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  169. $this->assertEquals('2 days ago', $result);
  170. // 2 days ago minus 4 hours is still 2 days ago
  171. $elementTime = $currentTime - 52 * 3600;
  172. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  173. $this->assertEquals('2 days ago', $result);
  174. }
  175. public function testRelativeTimeLastMonth() {
  176. $currentTime = 1380703592;
  177. $elementTime = $currentTime - 86400 * 31;
  178. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  179. $this->assertEquals('last month', $result);
  180. $elementTime = $currentTime - 86400 * 35;
  181. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  182. $this->assertEquals('last month', $result);
  183. }
  184. public function testRelativeTimeMonthsAgo() {
  185. $currentTime = 1380703592;
  186. $elementTime = $currentTime - 86400 * 65;
  187. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  188. $this->assertEquals('2 months ago', $result);
  189. $elementTime = $currentTime - 86400 * 130;
  190. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  191. $this->assertEquals('4 months ago', $result);
  192. }
  193. public function testRelativeTimeLastYear() {
  194. $currentTime = 1380703592;
  195. $elementTime = $currentTime - 86400 * 365;
  196. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  197. $this->assertEquals('last year', $result);
  198. $elementTime = $currentTime - 86400 * 450;
  199. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  200. $this->assertEquals('last year', $result);
  201. }
  202. public function testRelativeTimeYearsAgo() {
  203. $currentTime = 1380703592;
  204. $elementTime = $currentTime - 86400 * 365.25 * 2;
  205. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  206. $this->assertEquals('2 years ago', $result);
  207. $elementTime = $currentTime - 86400 * 365.25 * 3;
  208. $result = (string)relative_modified_date($elementTime, $currentTime, false);
  209. $this->assertEquals('3 years ago', $result);
  210. }
  211. }