2012-10-29 16:22:25 -04:00
|
|
|
<?php
|
2025-06-30 09:04:05 -04:00
|
|
|
|
2015-08-18 09:35:02 -04:00
|
|
|
/**
|
2024-05-10 09:09:14 -04:00
|
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
|
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
|
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
2015-08-18 09:35:02 -04:00
|
|
|
*/
|
2016-05-19 03:38:52 -04:00
|
|
|
|
|
|
|
|
namespace Test;
|
|
|
|
|
|
|
|
|
|
class TemplateFunctionsTest extends \Test\TestCase {
|
2019-11-21 10:40:38 -05:00
|
|
|
protected function setUp(): void {
|
2014-11-10 16:59:50 -05:00
|
|
|
parent::setUp();
|
2012-10-29 16:22:25 -04:00
|
|
|
|
2025-03-11 06:05:31 -04:00
|
|
|
require_once \OC::$SERVERROOT . '/lib/private/Template/functions.php';
|
2013-05-11 16:44:45 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testPJavaScript(): void {
|
2014-04-24 09:45:07 -04:00
|
|
|
$this->expectOutputString('<img onload="alert(1)" />');
|
|
|
|
|
p('<img onload="alert(1)" />');
|
2014-04-24 09:36:02 -04:00
|
|
|
}
|
2014-04-22 14:09:55 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testPJavaScriptWithScriptTags(): void {
|
2014-04-24 09:45:07 -04:00
|
|
|
$this->expectOutputString('<script>alert('Hacked!');</script>');
|
|
|
|
|
p("<script>alert('Hacked!');</script>");
|
2014-04-24 09:34:09 -04:00
|
|
|
}
|
2012-10-29 16:22:25 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testPNormalString(): void {
|
2014-04-24 09:45:07 -04:00
|
|
|
$string = 'This is a good string without HTML.';
|
|
|
|
|
$this->expectOutputString($string);
|
|
|
|
|
p($string);
|
2012-10-30 12:30:39 -04:00
|
|
|
}
|
2012-10-29 16:22:25 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testPrintUnescaped(): void {
|
2012-10-29 16:22:25 -04:00
|
|
|
$htmlString = "<script>alert('xss');</script>";
|
2014-04-24 09:45:07 -04:00
|
|
|
$this->expectOutputString($htmlString);
|
2012-10-29 16:22:25 -04:00
|
|
|
print_unescaped($htmlString);
|
2012-10-30 12:30:39 -04:00
|
|
|
}
|
2012-10-29 16:22:25 -04:00
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testPrintUnescapedNormalString(): void {
|
2014-04-24 09:45:07 -04:00
|
|
|
$string = 'This is a good string!';
|
|
|
|
|
$this->expectOutputString($string);
|
|
|
|
|
print_unescaped($string);
|
2012-10-29 16:22:25 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testEmitScriptTagWithContent(): void {
|
2023-01-09 19:40:42 -05:00
|
|
|
$this->expectOutputRegex('/<script nonce="[^"]+">\nalert\(\)\n<\/script>\n?/');
|
|
|
|
|
emit_script_tag('', 'alert()');
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testEmitScriptTagWithSource(): void {
|
2023-01-09 19:40:42 -05:00
|
|
|
$this->expectOutputRegex('/<script nonce=".*" defer src="some.js"><\/script>/');
|
|
|
|
|
emit_script_tag('some.js');
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testEmitScriptTagWithModuleSource(): void {
|
2025-12-09 17:54:50 -05:00
|
|
|
$this->expectOutputRegex('/<script nonce=".*" type="module" src="some.mjs"><\/script>/');
|
2023-01-09 19:40:42 -05:00
|
|
|
emit_script_tag('some.mjs', '', 'module');
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-09 17:54:50 -05:00
|
|
|
public function testEmitImportMap(): void {
|
|
|
|
|
$this->expectOutputRegex('/^<script[^>]+type="importmap">\n{"imports":{"\/some\/path\/file\.mjs":"\/some\/path\/file\.mjs\?v=123"}}\n<\/script>$/m');
|
|
|
|
|
emit_import_map(['jsfiles' => ['/some/path/file.mjs?v=123']]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// only create import map for modules with versioning
|
|
|
|
|
public function testEmitImportMapMixedScripts(): void {
|
|
|
|
|
$this->expectOutputRegex('/^<script[^>]+type="importmap">\n{"imports":{"\/some\/path\/module\.mjs":"\/some\/path\/module\.mjs\?v=123"}}\n<\/script>$/m');
|
|
|
|
|
emit_import_map(['jsfiles' => ['/some/path/module.mjs?v=123', '/some/path/classic.js?v=123']]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testEmitImportMapNoOutputWithoutVersion(): void {
|
|
|
|
|
$this->expectOutputString('');
|
|
|
|
|
emit_import_map(['jsfiles' => ['some.mjs']]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testEmitImportMapNoOutputWithClassicScript(): void {
|
|
|
|
|
$this->expectOutputString('');
|
|
|
|
|
emit_import_map(['jsfiles' => ['some.js?v=123']]);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testEmitScriptLoadingTags(): void {
|
2023-01-09 19:40:42 -05:00
|
|
|
// Test mjs js and inline content
|
2025-12-09 17:54:50 -05:00
|
|
|
$pattern = '/type="module"[^>]+src="some\.mjs"[^>]*>.+\n'; // some.mjs with type = module
|
2023-01-09 19:40:42 -05:00
|
|
|
$pattern .= '<script[^>]+src="other\.js"[^>]*>.+\n'; // other.js as plain javascript
|
|
|
|
|
$pattern .= '<script[^>]*>\n?.*inline.*\n?<\/script>'; // inline content
|
|
|
|
|
$pattern .= '/'; // no flags
|
|
|
|
|
|
|
|
|
|
$this->expectOutputRegex($pattern);
|
|
|
|
|
emit_script_loading_tags([
|
|
|
|
|
'jsfiles' => ['some.mjs', 'other.js'],
|
|
|
|
|
'inline_ocjs' => '// inline'
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testEmitScriptLoadingTagsWithVersion(): void {
|
2023-07-18 07:09:30 -04:00
|
|
|
// Test mjs js and inline content
|
2025-12-09 17:54:50 -05:00
|
|
|
$pattern = '/type="module"[^>]+src="some\.mjs\?v=ab123cd"[^>]*>.+\n'; // some.mjs with type = module
|
2023-07-18 07:09:30 -04:00
|
|
|
$pattern .= '<script[^>]+src="other\.js\?v=12abc34"[^>]*>.+\n'; // other.js as plain javascript
|
|
|
|
|
$pattern .= '/'; // no flags
|
|
|
|
|
|
|
|
|
|
$this->expectOutputRegex($pattern);
|
|
|
|
|
emit_script_loading_tags([
|
|
|
|
|
'jsfiles' => ['some.mjs?v=ab123cd', 'other.js?v=12abc34'],
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2013-10-02 14:16:35 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Test relative_modified_date with dates only
|
|
|
|
|
// ---------------------------------------------------------------------------
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRelativeDateToday(): void {
|
2013-10-02 14:16:35 -04:00
|
|
|
$currentTime = 1380703592;
|
|
|
|
|
$elementTime = $currentTime;
|
|
|
|
|
$result = (string)relative_modified_date($elementTime, $currentTime, true);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('today', $result);
|
|
|
|
|
|
|
|
|
|
// 2 hours ago is still today
|
|
|
|
|
$elementTime = $currentTime - 2 * 3600;
|
|
|
|
|
$result = (string)relative_modified_date($elementTime, $currentTime, true);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('today', $result);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRelativeDateYesterday(): void {
|
2013-10-02 14:16:35 -04:00
|
|
|
$currentTime = 1380703592;
|
|
|
|
|
$elementTime = $currentTime - 24 * 3600;
|
|
|
|
|
$result = (string)relative_modified_date($elementTime, $currentTime, true);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('yesterday', $result);
|
|
|
|
|
|
|
|
|
|
// yesterday - 2 hours is still yesterday
|
|
|
|
|
$elementTime = $currentTime - 26 * 3600;
|
|
|
|
|
$result = (string)relative_modified_date($elementTime, $currentTime, true);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('yesterday', $result);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRelativeDate2DaysAgo(): void {
|
2013-10-02 14:16:35 -04:00
|
|
|
$currentTime = 1380703592;
|
|
|
|
|
$elementTime = $currentTime - 48 * 3600;
|
|
|
|
|
$result = (string)relative_modified_date($elementTime, $currentTime, true);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('2 days ago', $result);
|
|
|
|
|
|
|
|
|
|
// 2 days ago minus 4 hours is still 2 days ago
|
|
|
|
|
$elementTime = $currentTime - 52 * 3600;
|
|
|
|
|
$result = (string)relative_modified_date($elementTime, $currentTime, true);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('2 days ago', $result);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRelativeDateLastMonth(): void {
|
2013-10-02 14:16:35 -04:00
|
|
|
$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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRelativeDateMonthsAgo(): void {
|
2013-10-02 14:16:35 -04:00
|
|
|
$currentTime = 1380703592;
|
2014-11-24 10:37:04 -05:00
|
|
|
$elementTime = $currentTime - 86400 * 65;
|
2013-10-02 14:16:35 -04:00
|
|
|
$result = (string)relative_modified_date($elementTime, $currentTime, true);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('2 months ago', $result);
|
|
|
|
|
|
2014-11-24 10:37:04 -05:00
|
|
|
$elementTime = $currentTime - 86400 * 130;
|
2013-10-02 14:16:35 -04:00
|
|
|
$result = (string)relative_modified_date($elementTime, $currentTime, true);
|
|
|
|
|
|
2014-11-24 10:37:04 -05:00
|
|
|
$this->assertEquals('4 months ago', $result);
|
2013-10-02 14:16:35 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRelativeDateLastYear(): void {
|
2013-10-02 14:16:35 -04:00
|
|
|
$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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRelativeDateYearsAgo(): void {
|
2013-10-02 14:16:35 -04:00
|
|
|
$currentTime = 1380703592;
|
|
|
|
|
$elementTime = $currentTime - 86400 * 365.25 * 2;
|
|
|
|
|
$result = (string)relative_modified_date($elementTime, $currentTime, true);
|
|
|
|
|
|
2014-11-24 10:37:04 -05:00
|
|
|
$this->assertEquals('2 years ago', $result);
|
2013-10-02 14:16:35 -04:00
|
|
|
|
|
|
|
|
$elementTime = $currentTime - 86400 * 365.25 * 3;
|
|
|
|
|
$result = (string)relative_modified_date($elementTime, $currentTime, true);
|
|
|
|
|
|
2014-11-24 10:37:04 -05:00
|
|
|
$this->assertEquals('3 years ago', $result);
|
2013-10-02 14:16:35 -04:00
|
|
|
}
|
2012-10-29 16:22:25 -04:00
|
|
|
|
2013-10-02 14:16:35 -04:00
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
// Test relative_modified_date with timestamps only (date + time value)
|
|
|
|
|
// ---------------------------------------------------------------------------
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRelativeTimeSecondsAgo(): void {
|
2013-10-02 14:16:35 -04:00
|
|
|
$currentTime = 1380703592;
|
|
|
|
|
$elementTime = $currentTime - 5;
|
|
|
|
|
$result = (string)relative_modified_date($elementTime, $currentTime, false);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('seconds ago', $result);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRelativeTimeMinutesAgo(): void {
|
2013-10-02 14:16:35 -04:00
|
|
|
$currentTime = 1380703592;
|
|
|
|
|
$elementTime = $currentTime - 190;
|
|
|
|
|
$result = (string)relative_modified_date($elementTime, $currentTime, false);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('3 minutes ago', $result);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRelativeTimeHoursAgo(): void {
|
2013-10-02 14:16:35 -04:00
|
|
|
$currentTime = 1380703592;
|
|
|
|
|
$elementTime = $currentTime - 7500;
|
|
|
|
|
$result = (string)relative_modified_date($elementTime, $currentTime, false);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('2 hours ago', $result);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRelativeTime2DaysAgo(): void {
|
2013-10-02 14:16:35 -04:00
|
|
|
$currentTime = 1380703592;
|
|
|
|
|
$elementTime = $currentTime - 48 * 3600;
|
|
|
|
|
$result = (string)relative_modified_date($elementTime, $currentTime, false);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('2 days ago', $result);
|
|
|
|
|
|
|
|
|
|
// 2 days ago minus 4 hours is still 2 days ago
|
|
|
|
|
$elementTime = $currentTime - 52 * 3600;
|
|
|
|
|
$result = (string)relative_modified_date($elementTime, $currentTime, false);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('2 days ago', $result);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRelativeTimeLastMonth(): void {
|
2013-10-02 14:16:35 -04:00
|
|
|
$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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRelativeTimeMonthsAgo(): void {
|
2013-10-02 14:16:35 -04:00
|
|
|
$currentTime = 1380703592;
|
2014-11-24 10:37:04 -05:00
|
|
|
$elementTime = $currentTime - 86400 * 65;
|
2013-10-02 14:16:35 -04:00
|
|
|
$result = (string)relative_modified_date($elementTime, $currentTime, false);
|
|
|
|
|
|
|
|
|
|
$this->assertEquals('2 months ago', $result);
|
|
|
|
|
|
2014-11-24 10:37:04 -05:00
|
|
|
$elementTime = $currentTime - 86400 * 130;
|
2013-10-02 14:16:35 -04:00
|
|
|
$result = (string)relative_modified_date($elementTime, $currentTime, false);
|
|
|
|
|
|
2014-11-24 10:37:04 -05:00
|
|
|
$this->assertEquals('4 months ago', $result);
|
2013-10-02 14:16:35 -04:00
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRelativeTimeLastYear(): void {
|
2013-10-02 14:16:35 -04:00
|
|
|
$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);
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-15 16:32:31 -04:00
|
|
|
public function testRelativeTimeYearsAgo(): void {
|
2013-10-02 14:16:35 -04:00
|
|
|
$currentTime = 1380703592;
|
|
|
|
|
$elementTime = $currentTime - 86400 * 365.25 * 2;
|
|
|
|
|
$result = (string)relative_modified_date($elementTime, $currentTime, false);
|
|
|
|
|
|
2014-11-24 10:37:04 -05:00
|
|
|
$this->assertEquals('2 years ago', $result);
|
2013-10-02 14:16:35 -04:00
|
|
|
|
|
|
|
|
$elementTime = $currentTime - 86400 * 365.25 * 3;
|
|
|
|
|
$result = (string)relative_modified_date($elementTime, $currentTime, false);
|
|
|
|
|
|
2014-11-24 10:37:04 -05:00
|
|
|
$this->assertEquals('3 years ago', $result);
|
2013-10-02 14:16:35 -04:00
|
|
|
}
|
2012-10-30 16:20:21 -04:00
|
|
|
}
|