123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- <?php
- /**
- * ownCloud
- *
- * @author Bernhard Posselt
- * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com>
- *
- * This library is free software; you can redistribute it and/or
- * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or any later version.
- *
- * This library is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
- *
- * You should have received a copy of the GNU Affero General Public
- * License along with this library. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- 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() {
- // Test mjs js and inline content
- $pattern = '/src="some\.mjs"[^>]+type="module"[^>]*>.+\n'; // some.mjs with type = module
- $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'
- ]);
- }
- public function testEmitScriptLoadingTagsWithVersion() {
- // Test mjs js and inline content
- $pattern = '/src="some\.mjs\?v=ab123cd"[^>]+type="module"[^>]*>.+\n'; // some.mjs with type = module
- $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'],
- ]);
- }
- // ---------------------------------------------------------------------------
- // Test relative_modified_date with dates only
- // ---------------------------------------------------------------------------
- public function testRelativeDateToday() {
- $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);
- }
- public function testRelativeDateYesterday() {
- $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);
- }
- public function testRelativeDate2DaysAgo() {
- $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);
- }
- 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);
- }
- // ---------------------------------------------------------------------------
- // Test relative_modified_date with timestamps only (date + time value)
- // ---------------------------------------------------------------------------
- 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);
- // 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);
- }
- 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);
- }
- }
|