'; protected function setUp(): void { parent::setUp(); Director::config()->set('alternate_base_folder', __DIR__ . '/SSViewerTest'); Director::config()->set('alternate_base_url', 'http://www.mysite.com/basedir/'); Director::config()->set('alternate_public_dir', 'public'); // Enforce public dir // Add public as a theme in itself SSViewer::set_themes([SSViewer::PUBLIC_THEME, SSViewer::DEFAULT_THEME]); TestAssetStore::activate('RequirementsTest'); // Set backend root to /RequirementsTest $this->oldThemeResourceLoader = ThemeResourceLoader::inst(); } protected function tearDown(): void { ThemeResourceLoader::set_instance($this->oldThemeResourceLoader); TestAssetStore::reset(); parent::tearDown(); } public function testExternalUrls() { /** @var Requirements_Backend $backend */ $backend = Injector::inst()->create(Requirements_Backend::class); $backend->setCombinedFilesEnabled(true); $backend->javascript('http://www.mydomain.com/test.js'); $backend->javascript('https://www.mysecuredomain.com/test.js'); $backend->javascript('//scheme-relative.example.com/test.js'); $backend->javascript('http://www.mydomain.com:3000/test.js'); $backend->css('http://www.mydomain.com/test.css'); $backend->css('https://www.mysecuredomain.com/test.css'); $backend->css('//scheme-relative.example.com/test.css'); $backend->css('http://www.mydomain.com:3000/test.css'); $html = $backend->includeInHTML(self::$html_template); $this->assertStringContainsString('http://www.mydomain.com/test.js', $html, 'Load external javascript URL'); $this->assertStringContainsString('https://www.mysecuredomain.com/test.js', $html, 'Load external secure javascript URL'); $this->assertStringContainsString('//scheme-relative.example.com/test.js', $html, 'Load external scheme-relative JS'); $this->assertStringContainsString('http://www.mydomain.com:3000/test.js', $html, 'Load external with port'); $this->assertStringContainsString('http://www.mydomain.com/test.css', $html, 'Load external CSS URL'); $this->assertStringContainsString('https://www.mysecuredomain.com/test.css', $html, 'Load external secure CSS URL'); $this->assertStringContainsString('//scheme-relative.example.com/test.css', $html, 'Load scheme-relative CSS URL'); $this->assertStringContainsString('http://www.mydomain.com:3000/test.css', $html, 'Load external with port'); } /** * Setup new backend * * @param Requirements_Backend $backend */ protected function setupRequirements($backend) { // Flush requirements $backend->clear(); $backend->clearCombinedFiles(); $backend->setCombinedFilesFolder('_combinedfiles'); $backend->setMinifyCombinedFiles(false); $backend->setCombinedFilesEnabled(true); Requirements::flush(); } /** * Setup combined and non-combined js with the backend * * @param Requirements_Backend $backend */ protected function setupCombinedRequirements($backend) { $this->setupRequirements($backend); // require files normally (e.g. called from a FormField instance) $backend->javascript('javascript/RequirementsTest_a.js'); $backend->javascript('javascript/RequirementsTest_b.js'); $backend->javascript('javascript/RequirementsTest_c.js'); // Public resources may or may not be specified with `public/` prefix $backend->javascript('javascript/RequirementsTest_d.js'); $backend->javascript('public/javascript/RequirementsTest_e.js'); // require two of those files as combined includes $backend->combineFiles( 'RequirementsTest_bc.js', [ 'javascript/RequirementsTest_b.js', 'javascript/RequirementsTest_c.js' ] ); } /** * Setup combined files with the backend * * @param Requirements_Backend $backend */ protected function setupCombinedNonrequiredRequirements($backend) { $this->setupRequirements($backend); // require files as combined includes $backend->combineFiles( 'RequirementsTest_bc.js', [ 'javascript/RequirementsTest_b.js', 'javascript/RequirementsTest_c.js' ] ); } /** * @param Requirements_Backend $backend * @param bool $async * @param bool $defer */ protected function setupCombinedRequirementsJavascriptAsyncDefer($backend, $async, $defer) { $this->setupRequirements($backend); // require files normally (e.g. called from a FormField instance) $backend->javascript('javascript/RequirementsTest_a.js'); $backend->javascript('javascript/RequirementsTest_b.js'); $backend->javascript('javascript/RequirementsTest_c.js'); // require two of those files as combined includes $backend->combineFiles( 'RequirementsTest_bc.js', [ 'javascript/RequirementsTest_b.js', 'javascript/RequirementsTest_c.js' ], [ 'async' => $async, 'defer' => $defer, ] ); } public function testCustomType() { /** @var Requirements_Backend $backend */ $backend = Injector::inst()->create(Requirements_Backend::class); $this->setupRequirements($backend); // require files normally (e.g. called from a FormField instance) $backend->javascript( 'javascript/RequirementsTest_a.js', [ 'type' => 'application/json' ] ); $backend->javascript('javascript/RequirementsTest_b.js'); $result = $backend->includeInHTML(self::$html_template); $this->assertMatchesRegularExpression( '#\n", $html); } public function testIncludedJsIsNotCommentedOut() { $template = ''; /** @var Requirements_Backend $backend */ $backend = Injector::inst()->create(Requirements_Backend::class); $this->setupRequirements($backend); $backend->javascript('javascript/RequirementsTest_a.js'); $html = $backend->includeInHTML($template); //wiping out commented-out html $html = preg_replace('//Uis', '', $html ?? ''); $this->assertStringContainsString("RequirementsTest_a.js", $html); } public function testCommentedOutScriptTagIsIgnored() { /// Disable nonce $urlGenerator = new SimpleResourceURLGenerator(); Injector::inst()->registerService($urlGenerator, ResourceURLGenerator::class); $template = '' . '

more content

'; /** @var Requirements_Backend $backend */ $backend = Injector::inst()->create(Requirements_Backend::class); $this->setupRequirements($backend); $src = 'javascript/RequirementsTest_a.js'; $backend->javascript($src); $html = $backend->includeInHTML($template); $urlSrc = $urlGenerator->urlForResource($src); $this->assertEquals( '' . '

more content

\n", $html ); } public function testForceJsToBottom() { /** @var Requirements_Backend $backend */ $backend = Injector::inst()->create(Requirements_Backend::class); $this->setupRequirements($backend); $backend->javascript('http://www.mydomain.com/test.js'); $backend->customScript( <<<'EOS' var globalvar = { pattern: '\\$custom\\1' }; EOS ); // Test matching with HTML5
tags as well $template = '
My header

Body

'; // The expected outputs $expectedScripts = "\n" . "\n"; $JsInHead = "$expectedScripts
My header

Body

"; $JsInBody = "
My header

Body$expectedScripts

"; $JsAtEnd = "
My header

Body

$expectedScripts"; // Test if the script is before the head tag, not before the body. // Expected: $JsInHead $backend->setWriteJavascriptToBody(false); $backend->setForceJSToBottom(false); $html = $backend->includeInHTML($template); $this->assertNotEquals($JsInBody, $html); $this->assertNotEquals($JsAtEnd, $html); $this->assertEquals($JsInHead, $html); // Test if the script is before the first