'; protected function setUp() { parent::setUp(); TestAssetStore::activate('RequirementsTest'); // Set backend root to /RequirementsTest } protected function tearDown() { 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->css('http://www.mydomain.com/test.css'); $backend->css('https://www.mysecuredomain.com/test.css'); $backend->css('//scheme-relative.example.com/test.css'); $html = $backend->includeInHTML(self::$html_template); $this->assertTrue( (strpos($html, 'http://www.mydomain.com/test.js') !== false), 'Load external javascript URL' ); $this->assertTrue( (strpos($html, 'https://www.mysecuredomain.com/test.js') !== false), 'Load external secure javascript URL' ); $this->assertTrue( (strpos($html, '//scheme-relative.example.com/test.js') !== false), 'Load external scheme-relative javascript URL' ); $this->assertTrue( (strpos($html, 'http://www.mydomain.com/test.css') !== false), 'Load external CSS URL' ); $this->assertTrue( (strpos($html, 'https://www.mysecuredomain.com/test.css') !== false), 'Load external secure CSS URL' ); $this->assertTrue( (strpos($html, '//scheme-relative.example.com/test.css') !== false), 'Load scheme-relative CSS URL' ); } /** * 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) { $basePath = $this->getThemeRoot(); $this->setupRequirements($backend); // require files normally (e.g. called from a FormField instance) $backend->javascript($basePath . '/javascript/RequirementsTest_a.js'); $backend->javascript($basePath . '/javascript/RequirementsTest_b.js'); $backend->javascript($basePath . '/javascript/RequirementsTest_c.js'); // require two of those files as combined includes $backend->combineFiles( 'RequirementsTest_bc.js', array( $basePath . '/javascript/RequirementsTest_b.js', $basePath . '/javascript/RequirementsTest_c.js' ) ); } /** * Setup combined files with the backend * * @param Requirements_Backend $backend */ protected function setupCombinedNonrequiredRequirements($backend) { $basePath = $this->getThemeRoot(); $this->setupRequirements($backend); // require files as combined includes $backend->combineFiles( 'RequirementsTest_bc.js', array( $basePath . '/javascript/RequirementsTest_b.js', $basePath . '/javascript/RequirementsTest_c.js' ) ); } /** * @param Requirements_Backend $backend * @param bool $async * @param bool $defer */ protected function setupCombinedRequirementsJavascriptAsyncDefer($backend, $async, $defer) { $basePath = $this->getThemeRoot(); $this->setupRequirements($backend); // require files normally (e.g. called from a FormField instance) $backend->javascript($basePath . '/javascript/RequirementsTest_a.js'); $backend->javascript($basePath . '/javascript/RequirementsTest_b.js'); $backend->javascript($basePath . '/javascript/RequirementsTest_c.js'); // require two of those files as combined includes $backend->combineFiles( 'RequirementsTest_bc.js', array( $basePath . '/javascript/RequirementsTest_b.js', $basePath . '/javascript/RequirementsTest_c.js' ), array( 'async' => $async, 'defer' => $defer, ) ); } public function testCustomType() { /** @var Requirements_Backend $backend */ $backend = Injector::inst()->create(Requirements_Backend::class); $basePath = $this->getThemeRoot(); $this->setupRequirements($backend); // require files normally (e.g. called from a FormField instance) $backend->javascript( $basePath . '/javascript/RequirementsTest_a.js', [ 'type' => 'application/json' ] ); $backend->javascript($basePath . '/javascript/RequirementsTest_b.js'); $result = $backend->includeInHTML(self::$html_template); $this->assertRegExp( '#\n", $html); } public function testIncludedJsIsNotCommentedOut() { $template = ''; /** @var Requirements_Backend $backend */ $backend = Injector::inst()->create(Requirements_Backend::class); $this->setupRequirements($backend); $backend->javascript($this->getThemeRoot() . '/javascript/RequirementsTest_a.js'); $html = $backend->includeInHTML($template); //wiping out commented-out html $html = preg_replace('//Uis', '', $html); $this->assertContains("RequirementsTest_a.js", $html); } public function testCommentedOutScriptTagIsIgnored() { $template = '' . '

more content

'; /** * @var Requirements_Backend $backend */ $backend = Injector::inst()->create(Requirements_Backend::class); $this->setupRequirements($backend); $backend->setSuffixRequirements(false); $src = $this->getThemeRoot() . '/javascript/RequirementsTest_a.js'; $urlSrc = Controller::join_links(Director::baseURL(), $src); $backend->javascript($src); $html = $backend->includeInHTML($template); $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