123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- <?php
- namespace Test;
- class TemplateFunctionsTest extends \Test\TestCase {
- protected function setUp(): void {
- parent::setUp();
- require_once \OC::$SERVERROOT . '/lib/private/legacy/OC_Template.php';
- }
- public function testPJavaScript() {
- $this->expectOutputString('<img onload="alert(1)" />');
- p('<img onload="alert(1)" />');
- }
- public function testPJavaScriptWithScriptTags() {
- $this->expectOutputString('<script>alert('Hacked!');</script>');
- p("<script>alert('Hacked!');</script>");
- }
- public function testPNormalString() {
- $string = 'This is a good string without HTML.';
- $this->expectOutputString($string);
- p($string);
- }
- public function testPrintUnescaped() {
- $htmlString = "<script>alert('xss');</script>";
- $this->expectOutputString($htmlString);
- print_unescaped($htmlString);
- }
- public function testPrintUnescapedNormalString() {
- $string = 'This is a good string!';
- $this->expectOutputString($string);
- print_unescaped($string);
- }
- public function testEmitScriptTagWithContent() {
- $this->expectOutputRegex('/<script nonce="[^"]+">\nalert\(\)\n<\/script>\n?/');
- emit_script_tag('', 'alert()');
- }
- public function testEmitScriptTagWithSource() {
- $this->expectOutputRegex('/<script nonce=".*" defer src="some.js"><\/script>/');
- emit_script_tag('some.js');
- }
- public function testEmitScriptTagWithModuleSource() {
- $this->expectOutputRegex('/<script nonce=".*" defer src="some.mjs" type="module"><\/script>/');
- emit_script_tag('some.mjs', '', 'module');
- }
- public function testEmitScriptLoadingTags() {
-
- $pattern = '/src="some\.mjs"[^>]+type="module"[^>]*>.+\n';
- $pattern .= '<script[^>]+src="other\.js"[^>]*>.+\n';
- $pattern .= '<script[^>]*>\n?.*inline.*\n?<\/script>';
- $pattern .= '/';
- $this->expectOutputRegex($pattern);
- emit_script_loading_tags([
- 'jsfiles' => ['some.mjs', 'other.js'],
- 'inline_ocjs' => '// inline'
- ]);
- }
- public function testEmitScriptLoadingTagsWithVersion() {
-
- $pattern = '/src="some\.mjs\?v=ab123cd"[^>]+type="module"[^>]*>.+\n';
- $pattern .= '<script[^>]+src="other\.js\?v=12abc34"[^>]*>.+\n';
- $pattern .= '/';
- $this->expectOutputRegex($pattern);
- emit_script_loading_tags([
- 'jsfiles' => ['some.mjs?v=ab123cd', 'other.js?v=12abc34'],
- ]);
- }
-
-
-
- public function testRelativeDateToday() {
- $currentTime = 1380703592;
- $elementTime = $currentTime;
- $result = (string)relative_modified_date($elementTime, $currentTime, true);
- $this->assertEquals('today', $result);
-
- $elementTime = $currentTime - 2 * 3600;
- $result = (string)relative_modified_date($elementTime, $currentTime, true);
- $this->assertEquals('today', $result);
- }
- public function testRelativeDateYesterday() {
- $currentTime = 1380703592;
- $elementTime = $currentTime - 24 * 3600;
- $result = (string)relative_modified_date($elementTime, $currentTime, true);
- $this->assertEquals('yesterday', $result);
-
- $elementTime = $currentTime - 26 * 3600;
- $result = (string)relative_modified_date($elementTime, $currentTime, true);
- $this->assertEquals('yesterday', $result);
- }
- public function testRelativeDate2DaysAgo() {
- $currentTime = 1380703592;
- $elementTime = $currentTime - 48 * 3600;
- $result = (string)relative_modified_date($elementTime, $currentTime, true);
- $this->assertEquals('2 days ago', $result);
-
- $elementTime = $currentTime - 52 * 3600;
- $result = (string)relative_modified_date($elementTime, $currentTime, true);
- $this->assertEquals('2 days ago', $result);
- }
- public function testRelativeDateLastMonth() {
- $currentTime = 1380703592;
- $elementTime = $currentTime - 86400 * 31;
- $result = (string)relative_modified_date($elementTime, $currentTime, true);
- $this->assertEquals('last month', $result);
- $elementTime = $currentTime - 86400 * 35;
- $result = (string)relative_modified_date($elementTime, $currentTime, true);
- $this->assertEquals('last month', $result);
- }
- public function testRelativeDateMonthsAgo() {
- $currentTime = 1380703592;
- $elementTime = $currentTime - 86400 * 65;
- $result = (string)relative_modified_date($elementTime, $currentTime, true);
- $this->assertEquals('2 months ago', $result);
- $elementTime = $currentTime - 86400 * 130;
- $result = (string)relative_modified_date($elementTime, $currentTime, true);
- $this->assertEquals('4 months ago', $result);
- }
- public function testRelativeDateLastYear() {
- $currentTime = 1380703592;
- $elementTime = $currentTime - 86400 * 365;
- $result = (string)relative_modified_date($elementTime, $currentTime, true);
- $this->assertEquals('last year', $result);
- $elementTime = $currentTime - 86400 * 450;
- $result = (string)relative_modified_date($elementTime, $currentTime, true);
- $this->assertEquals('last year', $result);
- }
- public function testRelativeDateYearsAgo() {
- $currentTime = 1380703592;
- $elementTime = $currentTime - 86400 * 365.25 * 2;
- $result = (string)relative_modified_date($elementTime, $currentTime, true);
- $this->assertEquals('2 years ago', $result);
- $elementTime = $currentTime - 86400 * 365.25 * 3;
- $result = (string)relative_modified_date($elementTime, $currentTime, true);
- $this->assertEquals('3 years ago', $result);
- }
-
-
-
- public function testRelativeTimeSecondsAgo() {
- $currentTime = 1380703592;
- $elementTime = $currentTime - 5;
- $result = (string)relative_modified_date($elementTime, $currentTime, false);
- $this->assertEquals('seconds ago', $result);
- }
- public function testRelativeTimeMinutesAgo() {
- $currentTime = 1380703592;
- $elementTime = $currentTime - 190;
- $result = (string)relative_modified_date($elementTime, $currentTime, false);
- $this->assertEquals('3 minutes ago', $result);
- }
- public function testRelativeTimeHoursAgo() {
- $currentTime = 1380703592;
- $elementTime = $currentTime - 7500;
- $result = (string)relative_modified_date($elementTime, $currentTime, false);
- $this->assertEquals('2 hours ago', $result);
- }
- public function testRelativeTime2DaysAgo() {
- $currentTime = 1380703592;
- $elementTime = $currentTime - 48 * 3600;
- $result = (string)relative_modified_date($elementTime, $currentTime, false);
- $this->assertEquals('2 days ago', $result);
-
- $elementTime = $currentTime - 52 * 3600;
- $result = (string)relative_modified_date($elementTime, $currentTime, false);
- $this->assertEquals('2 days ago', $result);
- }
- public function testRelativeTimeLastMonth() {
- $currentTime = 1380703592;
- $elementTime = $currentTime - 86400 * 31;
- $result = (string)relative_modified_date($elementTime, $currentTime, false);
- $this->assertEquals('last month', $result);
- $elementTime = $currentTime - 86400 * 35;
- $result = (string)relative_modified_date($elementTime, $currentTime, false);
- $this->assertEquals('last month', $result);
- }
- public function testRelativeTimeMonthsAgo() {
- $currentTime = 1380703592;
- $elementTime = $currentTime - 86400 * 65;
- $result = (string)relative_modified_date($elementTime, $currentTime, false);
- $this->assertEquals('2 months ago', $result);
- $elementTime = $currentTime - 86400 * 130;
- $result = (string)relative_modified_date($elementTime, $currentTime, false);
- $this->assertEquals('4 months ago', $result);
- }
- public function testRelativeTimeLastYear() {
- $currentTime = 1380703592;
- $elementTime = $currentTime - 86400 * 365;
- $result = (string)relative_modified_date($elementTime, $currentTime, false);
- $this->assertEquals('last year', $result);
- $elementTime = $currentTime - 86400 * 450;
- $result = (string)relative_modified_date($elementTime, $currentTime, false);
- $this->assertEquals('last year', $result);
- }
- public function testRelativeTimeYearsAgo() {
- $currentTime = 1380703592;
- $elementTime = $currentTime - 86400 * 365.25 * 2;
- $result = (string)relative_modified_date($elementTime, $currentTime, false);
- $this->assertEquals('2 years ago', $result);
- $elementTime = $currentTime - 86400 * 365.25 * 3;
- $result = (string)relative_modified_date($elementTime, $currentTime, false);
- $this->assertEquals('3 years ago', $result);
- }
- }
|