'; public function setUp() { parent::setUp(); AssetStoreTest_SpyStore::activate('RequirementsTest'); // Set backend root to /RequirementsTest } public function tearDown() { AssetStoreTest_SpyStore::reset(); parent::tearDown(); } public function testExternalUrls() { /** @var Requirements_Backend $backend */ $backend = Injector::inst()->create('SilverStripe\\View\\Requirements_Backend'); $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->setMinifyCombinedJSFiles(false); Requirements::flush(); } /** * Setup combined and non-combined js with the backend * * @param Requirements_Backend $backend */ protected function setupCombinedRequirements($backend) { $basePath = $this->getCurrentRelativePath(); $this->setupRequirements($backend); // require files normally (e.g. called from a FormField instance) $backend->javascript($basePath . '/RequirementsTest_a.js'); $backend->javascript($basePath . '/RequirementsTest_b.js'); $backend->javascript($basePath . '/RequirementsTest_c.js'); // require two of those files as combined includes $backend->combineFiles( 'RequirementsTest_bc.js', array( $basePath . '/RequirementsTest_b.js', $basePath . '/RequirementsTest_c.js' ) ); } /** * Setup combined files with the backend * * @param Requirements_Backend $backend */ protected function setupCombinedNonrequiredRequirements($backend) { $basePath = $this->getCurrentRelativePath(); $this->setupRequirements($backend); // require files as combined includes $backend->combineFiles( 'RequirementsTest_bc.js', array( $basePath . '/RequirementsTest_b.js', $basePath . '/RequirementsTest_c.js' ) ); } protected function setupCombinedRequirementsJavascriptAsyncDefer($backend, $async, $defer) { $basePath = $this->getCurrentRelativePath(); $this->setupRequirements($backend); // require files normally (e.g. called from a FormField instance) $backend->javascript($basePath . '/RequirementsTest_a.js'); $backend->javascript($basePath . '/RequirementsTest_b.js'); $backend->javascript($basePath . '/RequirementsTest_c.js'); // require two of those files as combined includes $backend->combineFiles( 'RequirementsTest_bc.js', array( $basePath . '/RequirementsTest_b.js', $basePath . '/RequirementsTest_c.js' ), array( 'async' => $async, 'defer' => $defer, ) ); } public function testCustomType() { /** @var Requirements_Backend $backend */ $backend = Injector::inst()->create('SilverStripe\\View\\Requirements_Backend'); $basePath = $this->getCurrentRelativePath(); $this->setupRequirements($backend); // require files normally (e.g. called from a FormField instance) $backend->javascript($basePath . '/RequirementsTest_a.js', [ 'type' => 'application/json' ]); $backend->javascript($basePath . '/RequirementsTest_b.js'); $result = $backend->includeInHTML(self::$html_template); $this->assertRegexp( '#', $html); } public function testIncludedJsIsNotCommentedOut() { $template = ''; /** @var Requirements_Backend $backend */ $backend = Injector::inst()->create('SilverStripe\\View\\Requirements_Backend'); $this->setupRequirements($backend); $backend->javascript($this->getCurrentRelativePath() . '/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('SilverStripe\\View\\Requirements_Backend'); $this->setupRequirements($backend); $backend->setSuffixRequirements(false); $src = $this->getCurrentRelativePath() . '/RequirementsTest_a.js'; $urlSrc = ControllerTest_ContainerController::join_links(Director::baseURL(), $src); $backend->javascript($src); $html = $backend->includeInHTML($template); $this->assertEquals('' . '

more content

', $html); } public function testForceJsToBottom() { /** @var Requirements_Backend $backend */ $backend = Injector::inst()->create('SilverStripe\\View\\Requirements_Backend'); $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 = ""; $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