2008-07-18 01:32:31 +02:00
|
|
|
<?php
|
2015-10-12 06:34:34 +02:00
|
|
|
|
2008-07-18 01:32:31 +02:00
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2008-07-18 01:32:31 +02:00
|
|
|
* @subpackage tests
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2008-07-18 01:32:31 +02:00
|
|
|
* @todo Test that order of combine_files() is correct
|
2008-07-18 05:47:17 +02:00
|
|
|
* @todo Figure out how to clear the modified state of Requirements class - might affect other tests.
|
2008-07-18 01:32:31 +02:00
|
|
|
*/
|
|
|
|
class RequirementsTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-07-18 01:32:31 +02:00
|
|
|
static $html_template = '<html><head></head><body></body></html>';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
AssetStoreTest_SpyStore::activate('RequirementsTest'); // Set backend root to /RequirementsTest
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
AssetStoreTest_SpyStore::reset();
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
2012-11-23 14:55:19 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testExternalUrls() {
|
2016-01-14 05:23:21 +01:00
|
|
|
/** @var Requirements_Backend $backend */
|
2015-11-30 03:03:46 +01:00
|
|
|
$backend = Injector::inst()->create('Requirements_Backend');
|
2015-10-12 06:34:34 +02:00
|
|
|
$backend->setCombinedFilesEnabled(true);
|
2010-05-25 06:13:45 +02:00
|
|
|
|
|
|
|
$backend->javascript('http://www.mydomain.com/test.js');
|
|
|
|
$backend->javascript('https://www.mysecuredomain.com/test.js');
|
2012-08-23 01:13:29 +02:00
|
|
|
$backend->javascript('//scheme-relative.example.com/test.js');
|
2010-05-25 06:13:45 +02:00
|
|
|
$backend->css('http://www.mydomain.com/test.css');
|
|
|
|
$backend->css('https://www.mysecuredomain.com/test.css');
|
2012-08-23 01:13:29 +02:00
|
|
|
$backend->css('//scheme-relative.example.com/test.css');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-04-19 07:20:30 +02:00
|
|
|
$html = $backend->includeInHTML(self::$html_template);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-08-11 09:22:50 +02:00
|
|
|
$this->assertTrue(
|
|
|
|
(strpos($html, 'http://www.mydomain.com/test.js') !== false),
|
|
|
|
'Load external javascript URL'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
2014-08-15 08:53:05 +02:00
|
|
|
(strpos($html, 'https://www.mysecuredomain.com/test.js') !== false),
|
2008-08-11 09:22:50 +02:00
|
|
|
'Load external secure javascript URL'
|
|
|
|
);
|
2012-08-23 01:13:29 +02:00
|
|
|
$this->assertTrue(
|
2014-08-15 08:53:05 +02:00
|
|
|
(strpos($html, '//scheme-relative.example.com/test.js') !== false),
|
2012-08-23 01:13:29 +02:00
|
|
|
'Load external scheme-relative javascript URL'
|
|
|
|
);
|
2008-08-11 09:22:50 +02:00
|
|
|
$this->assertTrue(
|
2014-08-15 08:53:05 +02:00
|
|
|
(strpos($html, 'http://www.mydomain.com/test.css') !== false),
|
2008-08-11 09:22:50 +02:00
|
|
|
'Load external CSS URL'
|
|
|
|
);
|
|
|
|
$this->assertTrue(
|
2014-08-15 08:53:05 +02:00
|
|
|
(strpos($html, 'https://www.mysecuredomain.com/test.css') !== false),
|
2008-08-11 09:22:50 +02:00
|
|
|
'Load external secure CSS URL'
|
|
|
|
);
|
2012-08-23 01:13:29 +02:00
|
|
|
$this->assertTrue(
|
2014-08-15 08:53:05 +02:00
|
|
|
(strpos($html, '//scheme-relative.example.com/test.css') !== false),
|
2012-08-23 01:13:29 +02:00
|
|
|
'Load scheme-relative CSS URL'
|
|
|
|
);
|
2008-08-11 09:22:50 +02:00
|
|
|
}
|
2010-05-25 06:13:45 +02:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
/**
|
|
|
|
* Setup new backend
|
|
|
|
*
|
|
|
|
* @param Requirements_Backend $backend
|
|
|
|
*/
|
|
|
|
protected function setupRequirements($backend) {
|
|
|
|
// Flush requirements
|
2010-05-25 06:13:45 +02:00
|
|
|
$backend->clear();
|
2015-10-12 06:34:34 +02:00
|
|
|
$backend->clearCombinedFiles();
|
|
|
|
$backend->setCombinedFilesFolder('_combinedfiles');
|
2015-10-23 02:51:26 +02:00
|
|
|
$backend->setMinifyCombinedJSFiles(false);
|
2015-11-26 03:19:43 +01:00
|
|
|
Requirements::flush();
|
2015-10-12 06:34:34 +02:00
|
|
|
}
|
2010-05-25 06:13:45 +02:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
/**
|
|
|
|
* Setup combined and non-combined js with the backend
|
|
|
|
*
|
|
|
|
* @param Requirements_Backend $backend
|
|
|
|
*/
|
|
|
|
protected function setupCombinedRequirements($backend) {
|
|
|
|
$basePath = $this->getCurrentRelativePath();
|
|
|
|
$this->setupRequirements($backend);
|
2016-03-08 21:50:18 +01:00
|
|
|
|
2010-05-25 06:13:45 +02:00
|
|
|
// require files normally (e.g. called from a FormField instance)
|
2011-03-30 08:49:11 +02:00
|
|
|
$backend->javascript($basePath . '/RequirementsTest_a.js');
|
|
|
|
$backend->javascript($basePath . '/RequirementsTest_b.js');
|
|
|
|
$backend->javascript($basePath . '/RequirementsTest_c.js');
|
2010-05-25 06:13:45 +02:00
|
|
|
|
|
|
|
// require two of those files as combined includes
|
2015-10-12 06:34:34 +02:00
|
|
|
$backend->combineFiles(
|
2010-05-25 06:13:45 +02:00
|
|
|
'RequirementsTest_bc.js',
|
|
|
|
array(
|
2011-03-30 08:49:11 +02:00
|
|
|
$basePath . '/RequirementsTest_b.js',
|
|
|
|
$basePath . '/RequirementsTest_c.js'
|
2010-05-25 06:13:45 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
/**
|
|
|
|
* Setup combined files with the backend
|
|
|
|
*
|
|
|
|
* @param Requirements_Backend $backend
|
|
|
|
*/
|
2010-10-15 05:09:15 +02:00
|
|
|
protected function setupCombinedNonrequiredRequirements($backend) {
|
2015-10-12 06:34:34 +02:00
|
|
|
$basePath = $this->getCurrentRelativePath();
|
|
|
|
$this->setupRequirements($backend);
|
2010-05-25 06:13:45 +02:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
// require files as combined includes
|
|
|
|
$backend->combineFiles(
|
|
|
|
'RequirementsTest_bc.js',
|
|
|
|
array(
|
|
|
|
$basePath . '/RequirementsTest_b.js',
|
|
|
|
$basePath . '/RequirementsTest_c.js'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
protected function setupCombinedRequirementsJavascriptAsyncDefer($backend, $async, $defer) {
|
|
|
|
$basePath = $this->getCurrentRelativePath();
|
|
|
|
$this->setupRequirements($backend);
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
// 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');
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
// 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,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2010-05-25 06:13:45 +02:00
|
|
|
|
2016-08-02 08:48:56 +02:00
|
|
|
public function testCustomType() {
|
|
|
|
/** @var Requirements_Backend $backend */
|
|
|
|
$backend = Injector::inst()->create('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->assertContains(
|
|
|
|
'<script type="application/json" src="/framework/tests/forms/RequirementsTest_a.js',
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'<script type="application/javascript" src="/framework/tests/forms/RequirementsTest_b.js',
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
public function testCombinedJavascript() {
|
2016-01-14 05:23:21 +01:00
|
|
|
/** @var Requirements_Backend $backend */
|
2015-11-30 03:03:46 +01:00
|
|
|
$backend = Injector::inst()->create('Requirements_Backend');
|
2010-05-25 06:13:45 +02:00
|
|
|
$this->setupCombinedRequirements($backend);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-01-20 01:16:27 +01:00
|
|
|
$combinedFileName = '/_combinedfiles/RequirementsTest_bc-2a55d56.js';
|
2015-10-12 06:34:34 +02:00
|
|
|
$combinedFilePath = AssetStoreTest_SpyStore::base_path() . $combinedFileName;
|
2010-05-25 06:15:43 +02:00
|
|
|
|
2016-04-19 07:20:30 +02:00
|
|
|
$html = $backend->includeInHTML(self::$html_template);
|
2008-08-11 08:28:29 +02:00
|
|
|
|
2008-07-18 01:32:31 +02:00
|
|
|
/* COMBINED JAVASCRIPT FILE IS INCLUDED IN HTML HEADER */
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->assertRegExp(
|
|
|
|
'/src=".*' . preg_quote($combinedFileName, '/') . '/',
|
|
|
|
$html,
|
|
|
|
'combined javascript file is included in html header'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-07-18 01:32:31 +02:00
|
|
|
/* COMBINED JAVASCRIPT FILE EXISTS */
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->assertTrue(
|
|
|
|
file_exists($combinedFilePath),
|
|
|
|
'combined javascript file exists'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-07-18 01:32:31 +02:00
|
|
|
/* COMBINED JAVASCRIPT HAS CORRECT CONTENT */
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertTrue((strpos(file_get_contents($combinedFilePath), "alert('b')") !== false),
|
|
|
|
'combined javascript has correct content');
|
|
|
|
$this->assertTrue((strpos(file_get_contents($combinedFilePath), "alert('c')") !== false),
|
|
|
|
'combined javascript has correct content');
|
2011-03-30 08:49:11 +02:00
|
|
|
|
2008-07-18 01:32:31 +02:00
|
|
|
/* COMBINED FILES ARE NOT INCLUDED TWICE */
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->assertNotRegExp(
|
|
|
|
'/src=".*\/RequirementsTest_b\.js/',
|
|
|
|
$html,
|
|
|
|
'combined files are not included twice'
|
|
|
|
);
|
|
|
|
$this->assertNotRegExp(
|
|
|
|
'/src=".*\/RequirementsTest_c\.js/',
|
|
|
|
$html,
|
|
|
|
'combined files are not included twice'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-07-18 01:32:31 +02:00
|
|
|
/* NORMAL REQUIREMENTS ARE STILL INCLUDED */
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->assertRegExp(
|
|
|
|
'/src=".*\/RequirementsTest_a\.js/',
|
|
|
|
$html,
|
|
|
|
'normal requirements are still included'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:09:15 +02:00
|
|
|
// Then do it again, this time not requiring the files beforehand
|
2015-10-12 06:34:34 +02:00
|
|
|
unlink($combinedFilePath);
|
2016-01-14 05:23:21 +01:00
|
|
|
/** @var Requirements_Backend $backend */
|
2015-11-30 03:03:46 +01:00
|
|
|
$backend = Injector::inst()->create('Requirements_Backend');
|
2010-10-15 05:09:15 +02:00
|
|
|
$this->setupCombinedNonrequiredRequirements($backend);
|
2016-04-19 07:20:30 +02:00
|
|
|
$html = $backend->includeInHTML(self::$html_template);
|
2010-10-15 05:09:15 +02:00
|
|
|
|
|
|
|
/* COMBINED JAVASCRIPT FILE IS INCLUDED IN HTML HEADER */
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->assertRegExp(
|
|
|
|
'/src=".*' . preg_quote($combinedFileName, '/') . '/',
|
|
|
|
$html,
|
|
|
|
'combined javascript file is included in html header'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:09:15 +02:00
|
|
|
/* COMBINED JAVASCRIPT FILE EXISTS */
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->assertTrue(
|
|
|
|
file_exists($combinedFilePath),
|
|
|
|
'combined javascript file exists'
|
|
|
|
);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:09:15 +02:00
|
|
|
/* COMBINED JAVASCRIPT HAS CORRECT CONTENT */
|
2012-09-26 23:34:00 +02:00
|
|
|
$this->assertTrue((strpos(file_get_contents($combinedFilePath), "alert('b')") !== false),
|
|
|
|
'combined javascript has correct content');
|
|
|
|
$this->assertTrue((strpos(file_get_contents($combinedFilePath), "alert('c')") !== false),
|
|
|
|
'combined javascript has correct content');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-10-15 05:09:15 +02:00
|
|
|
/* COMBINED FILES ARE NOT INCLUDED TWICE */
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->assertNotRegExp(
|
|
|
|
'/src=".*\/RequirementsTest_b\.js/',
|
|
|
|
$html,
|
|
|
|
'combined files are not included twice'
|
|
|
|
);
|
|
|
|
$this->assertNotRegExp(
|
|
|
|
'/src=".*\/RequirementsTest_c\.js/',
|
|
|
|
$html,
|
|
|
|
'combined files are not included twice'
|
|
|
|
);
|
2008-07-18 05:47:17 +02:00
|
|
|
}
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
public function testCombinedJavascriptAsyncDefer() {
|
|
|
|
/** @var Requirements_Backend $backend */
|
|
|
|
$backend = Injector::inst()->create('Requirements_Backend');
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
$this->setupCombinedRequirementsJavascriptAsyncDefer($backend, true, false);
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
$combinedFileName = '/_combinedfiles/RequirementsTest_bc-2a55d56.js';
|
|
|
|
$combinedFilePath = AssetStoreTest_SpyStore::base_path() . $combinedFileName;
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
$html = $backend->includeInHTML(false, self::$html_template);
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
/* ASYNC IS INCLUDED IN SCRIPT TAG */
|
|
|
|
$this->assertRegExp(
|
|
|
|
'/src=".*' . preg_quote($combinedFileName, '/') . '" async/',
|
|
|
|
$html,
|
|
|
|
'async is included in script tag'
|
|
|
|
);
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
/* DEFER IS NOT INCLUDED IN SCRIPT TAG */
|
|
|
|
$this->assertNotContains('defer', $html, 'defer is not included');
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
/* COMBINED JAVASCRIPT FILE EXISTS */
|
|
|
|
clearstatcache(); // needed to get accurate file_exists() results
|
|
|
|
$this->assertFileExists($combinedFilePath,
|
|
|
|
'combined javascript file exists');
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
/* COMBINED JAVASCRIPT HAS CORRECT CONTENT */
|
|
|
|
$this->assertTrue((strpos(file_get_contents($combinedFilePath), "alert('b')") !== false),
|
|
|
|
'combined javascript has correct content');
|
|
|
|
$this->assertTrue((strpos(file_get_contents($combinedFilePath), "alert('c')") !== false),
|
|
|
|
'combined javascript has correct content');
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
/* COMBINED FILES ARE NOT INCLUDED TWICE */
|
|
|
|
$this->assertNotRegExp('/src=".*\/RequirementsTest_b\.js/', $html,
|
|
|
|
'combined files are not included twice');
|
|
|
|
$this->assertNotRegExp('/src=".*\/RequirementsTest_c\.js/', $html,
|
|
|
|
'combined files are not included twice');
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
/* NORMAL REQUIREMENTS ARE STILL INCLUDED */
|
|
|
|
$this->assertRegExp('/src=".*\/RequirementsTest_a\.js/', $html,
|
|
|
|
'normal requirements are still included');
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
/* NORMAL REQUIREMENTS DON'T HAVE ASYNC/DEFER */
|
|
|
|
$this->assertNotRegExp('/src=".*\/RequirementsTest_a\.js\?m=\d+" async/', $html,
|
|
|
|
'normal requirements don\'t have async');
|
|
|
|
$this->assertNotRegExp('/src=".*\/RequirementsTest_a\.js\?m=\d+" defer/', $html,
|
|
|
|
'normal requirements don\'t have defer');
|
|
|
|
$this->assertNotRegExp('/src=".*\/RequirementsTest_a\.js\?m=\d+" async defer/', $html,
|
|
|
|
'normal requirements don\'t have async/defer');
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
// setup again for testing defer
|
|
|
|
unlink($combinedFilePath);
|
|
|
|
/** @var Requirements_Backend $backend */
|
|
|
|
$backend = Injector::inst()->create('Requirements_Backend');
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
$this->setupCombinedRequirementsJavascriptAsyncDefer($backend, false, true);
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
$html = $backend->includeInHTML(self::$html_template);
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
/* DEFER IS INCLUDED IN SCRIPT TAG */
|
|
|
|
$this->assertRegExp(
|
|
|
|
'/src=".*' . preg_quote($combinedFileName, '/') . '" defer/',
|
|
|
|
$html,
|
|
|
|
'defer is included in script tag'
|
|
|
|
);
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
/* ASYNC IS NOT INCLUDED IN SCRIPT TAG */
|
|
|
|
$this->assertNotContains('async', $html, 'async is not included');
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
/* COMBINED JAVASCRIPT FILE EXISTS */
|
|
|
|
clearstatcache(); // needed to get accurate file_exists() results
|
|
|
|
$this->assertFileExists($combinedFilePath,
|
|
|
|
'combined javascript file exists');
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
/* COMBINED JAVASCRIPT HAS CORRECT CONTENT */
|
|
|
|
$this->assertTrue((strpos(file_get_contents($combinedFilePath), "alert('b')") !== false),
|
|
|
|
'combined javascript has correct content');
|
|
|
|
$this->assertTrue((strpos(file_get_contents($combinedFilePath), "alert('c')") !== false),
|
|
|
|
'combined javascript has correct content');
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
/* COMBINED FILES ARE NOT INCLUDED TWICE */
|
|
|
|
$this->assertNotRegExp('/src=".*\/RequirementsTest_b\.js/', $html,
|
|
|
|
'combined files are not included twice');
|
|
|
|
$this->assertNotRegExp('/src=".*\/RequirementsTest_c\.js/', $html,
|
|
|
|
'combined files are not included twice');
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
/* NORMAL REQUIREMENTS ARE STILL INCLUDED */
|
|
|
|
$this->assertRegExp('/src=".*\/RequirementsTest_a\.js/', $html,
|
|
|
|
'normal requirements are still included');
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
/* NORMAL REQUIREMENTS DON'T HAVE ASYNC/DEFER */
|
|
|
|
$this->assertNotRegExp('/src=".*\/RequirementsTest_a\.js\?m=\d+" async/', $html,
|
|
|
|
'normal requirements don\'t have async');
|
|
|
|
$this->assertNotRegExp('/src=".*\/RequirementsTest_a\.js\?m=\d+" defer/', $html,
|
|
|
|
'normal requirements don\'t have defer');
|
|
|
|
$this->assertNotRegExp('/src=".*\/RequirementsTest_a\.js\?m=\d+" async defer/', $html,
|
|
|
|
'normal requirements don\'t have async/defer');
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
// setup again for testing async and defer
|
|
|
|
unlink($combinedFilePath);
|
|
|
|
/** @var Requirements_Backend $backend */
|
|
|
|
$backend = Injector::inst()->create('Requirements_Backend');
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
$this->setupCombinedRequirementsJavascriptAsyncDefer($backend, true, true);
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
$html = $backend->includeInHTML(self::$html_template);
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
/* ASYNC/DEFER IS INCLUDED IN SCRIPT TAG */
|
|
|
|
$this->assertRegExp(
|
|
|
|
'/src=".*' . preg_quote($combinedFileName, '/') . '" async defer/',
|
|
|
|
$html,
|
|
|
|
'async and defer are included in script tag'
|
|
|
|
);
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
/* COMBINED JAVASCRIPT FILE EXISTS */
|
|
|
|
clearstatcache(); // needed to get accurate file_exists() results
|
|
|
|
$this->assertFileExists($combinedFilePath,
|
|
|
|
'combined javascript file exists');
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
/* COMBINED JAVASCRIPT HAS CORRECT CONTENT */
|
|
|
|
$this->assertTrue((strpos(file_get_contents($combinedFilePath), "alert('b')") !== false),
|
|
|
|
'combined javascript has correct content');
|
|
|
|
$this->assertTrue((strpos(file_get_contents($combinedFilePath), "alert('c')") !== false),
|
|
|
|
'combined javascript has correct content');
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
/* COMBINED FILES ARE NOT INCLUDED TWICE */
|
|
|
|
$this->assertNotRegExp('/src=".*\/RequirementsTest_b\.js/', $html,
|
|
|
|
'combined files are not included twice');
|
|
|
|
$this->assertNotRegExp('/src=".*\/RequirementsTest_c\.js/', $html,
|
|
|
|
'combined files are not included twice');
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
/* NORMAL REQUIREMENTS ARE STILL INCLUDED */
|
|
|
|
$this->assertRegExp('/src=".*\/RequirementsTest_a\.js/', $html,
|
|
|
|
'normal requirements are still included');
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
/* NORMAL REQUIREMENTS DON'T HAVE ASYNC/DEFER */
|
|
|
|
$this->assertNotRegExp('/src=".*\/RequirementsTest_a\.js\?m=\d+" async/', $html,
|
|
|
|
'normal requirements don\'t have async');
|
|
|
|
$this->assertNotRegExp('/src=".*\/RequirementsTest_a\.js\?m=\d+" defer/', $html,
|
|
|
|
'normal requirements don\'t have defer');
|
|
|
|
$this->assertNotRegExp('/src=".*\/RequirementsTest_a\.js\?m=\d+" async defer/', $html,
|
|
|
|
'normal requirements don\'t have async/defer');
|
2016-07-19 04:09:15 +02:00
|
|
|
|
2016-07-04 08:55:35 +02:00
|
|
|
unlink($combinedFilePath);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-02-12 22:54:53 +01:00
|
|
|
public function testCombinedCss() {
|
|
|
|
$basePath = $this->getCurrentRelativePath();
|
2016-01-14 05:23:21 +01:00
|
|
|
/** @var Requirements_Backend $backend */
|
2015-11-30 03:03:46 +01:00
|
|
|
$backend = Injector::inst()->create('Requirements_Backend');
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->setupRequirements($backend);
|
2013-02-12 22:54:53 +01:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
$backend->combineFiles(
|
2013-02-12 22:54:53 +01:00
|
|
|
'print.css',
|
|
|
|
array(
|
|
|
|
$basePath . '/RequirementsTest_print_a.css',
|
|
|
|
$basePath . '/RequirementsTest_print_b.css'
|
|
|
|
),
|
2016-07-04 08:55:35 +02:00
|
|
|
array(
|
|
|
|
'media' => 'print'
|
|
|
|
)
|
2013-02-12 22:54:53 +01:00
|
|
|
);
|
|
|
|
|
2016-04-19 07:20:30 +02:00
|
|
|
$html = $backend->includeInHTML(self::$html_template);
|
2013-02-12 22:54:53 +01:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->assertRegExp(
|
2015-11-26 03:19:43 +01:00
|
|
|
'/href=".*\/print\-94e723d\.css/',
|
2015-10-12 06:34:34 +02:00
|
|
|
$html,
|
|
|
|
'Print stylesheets have been combined.'
|
|
|
|
);
|
|
|
|
$this->assertRegExp(
|
|
|
|
'/media="print/',
|
|
|
|
$html,
|
2013-02-12 22:54:53 +01:00
|
|
|
'Combined print stylesheet retains the media parameter'
|
|
|
|
);
|
2015-10-12 06:34:34 +02:00
|
|
|
|
|
|
|
// Test that combining a file multiple times doesn't trigger an error
|
2016-01-14 05:23:21 +01:00
|
|
|
/** @var Requirements_Backend $backend */
|
2015-11-30 03:03:46 +01:00
|
|
|
$backend = Injector::inst()->create('Requirements_Backend');
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->setupRequirements($backend);
|
|
|
|
$backend->combineFiles(
|
|
|
|
'style.css',
|
|
|
|
array(
|
|
|
|
$basePath . '/RequirementsTest_b.css',
|
|
|
|
$basePath . '/RequirementsTest_c.css'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$backend->combineFiles(
|
|
|
|
'style.css',
|
|
|
|
array(
|
|
|
|
$basePath . '/RequirementsTest_b.css',
|
|
|
|
$basePath . '/RequirementsTest_c.css'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2016-04-19 07:20:30 +02:00
|
|
|
$html = $backend->includeInHTML(self::$html_template);
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->assertRegExp(
|
2016-01-20 01:16:27 +01:00
|
|
|
'/href=".*\/style\-bcd90f5\.css/',
|
2015-10-12 06:34:34 +02:00
|
|
|
$html,
|
|
|
|
'Stylesheets have been combined.'
|
|
|
|
);
|
2013-02-12 22:54:53 +01:00
|
|
|
}
|
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testBlockedCombinedJavascript() {
|
2011-03-30 08:49:11 +02:00
|
|
|
$basePath = $this->getCurrentRelativePath();
|
2016-01-14 05:23:21 +01:00
|
|
|
/** @var Requirements_Backend $backend */
|
2015-11-30 03:03:46 +01:00
|
|
|
$backend = Injector::inst()->create('Requirements_Backend');
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->setupCombinedRequirements($backend);
|
2016-01-20 01:16:27 +01:00
|
|
|
$combinedFileName = '/_combinedfiles/RequirementsTest_bc-2a55d56.js';
|
2015-10-12 06:34:34 +02:00
|
|
|
$combinedFilePath = AssetStoreTest_SpyStore::base_path() . $combinedFileName;
|
2008-08-11 09:22:50 +02:00
|
|
|
|
2008-07-18 05:47:17 +02:00
|
|
|
/* BLOCKED COMBINED FILES ARE NOT INCLUDED */
|
2010-05-25 06:13:45 +02:00
|
|
|
$backend->block('RequirementsTest_bc.js');
|
2008-08-11 09:22:50 +02:00
|
|
|
|
2008-07-18 05:47:17 +02:00
|
|
|
clearstatcache(); // needed to get accurate file_exists() results
|
2016-04-19 07:20:30 +02:00
|
|
|
$html = $backend->includeInHTML(self::$html_template);
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->assertFileNotExists($combinedFilePath);
|
|
|
|
$this->assertNotRegExp(
|
|
|
|
'/src=".*\/RequirementsTest_bc\.js/',
|
|
|
|
$html,
|
|
|
|
'blocked combined files are not included'
|
|
|
|
);
|
2010-05-25 06:13:45 +02:00
|
|
|
$backend->unblock('RequirementsTest_bc.js');
|
2008-07-18 05:47:17 +02:00
|
|
|
|
|
|
|
/* BLOCKED UNCOMBINED FILES ARE NOT INCLUDED */
|
2010-05-25 06:13:45 +02:00
|
|
|
$this->setupCombinedRequirements($backend);
|
2011-03-30 08:49:11 +02:00
|
|
|
$backend->block($basePath .'/RequirementsTest_b.js');
|
2016-07-04 08:55:35 +02:00
|
|
|
$combinedFileName2 = '/_combinedfiles/RequirementsTest_bc-3748f67.js'; // SHA1 without file b included
|
2015-10-12 06:34:34 +02:00
|
|
|
$combinedFilePath2 = AssetStoreTest_SpyStore::base_path() . $combinedFileName2;
|
2008-07-18 05:47:17 +02:00
|
|
|
clearstatcache(); // needed to get accurate file_exists() results
|
2016-04-19 07:20:30 +02:00
|
|
|
$html = $backend->includeInHTML(self::$html_template);
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->assertFileExists($combinedFilePath2);
|
|
|
|
$this->assertTrue(
|
|
|
|
strpos(file_get_contents($combinedFilePath2), "alert('b')") === false,
|
|
|
|
'blocked uncombined files are not included'
|
|
|
|
);
|
|
|
|
$backend->unblock($basePath . '/RequirementsTest_b.js');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2008-07-18 05:47:17 +02:00
|
|
|
/* A SINGLE FILE CAN'T BE INCLUDED IN TWO COMBINED FILES */
|
2010-05-25 06:13:45 +02:00
|
|
|
$this->setupCombinedRequirements($backend);
|
2008-07-18 05:47:17 +02:00
|
|
|
clearstatcache(); // needed to get accurate file_exists() results
|
2008-08-12 01:18:56 +02:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
// Exception generated from including invalid file
|
|
|
|
$this->setExpectedException(
|
|
|
|
'InvalidArgumentException',
|
|
|
|
sprintf(
|
|
|
|
"Requirements_Backend::combine_files(): Already included file(s) %s in combined file '%s'",
|
|
|
|
$basePath . '/RequirementsTest_c.js',
|
|
|
|
'RequirementsTest_bc.js'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
$backend->combineFiles(
|
2008-11-01 15:04:31 +01:00
|
|
|
'RequirementsTest_ac.js',
|
2008-07-18 05:47:17 +02:00
|
|
|
array(
|
2011-03-30 08:49:11 +02:00
|
|
|
$basePath . '/RequirementsTest_a.js',
|
|
|
|
$basePath . '/RequirementsTest_c.js'
|
2008-07-18 05:47:17 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testArgsInUrls() {
|
2011-03-30 08:49:11 +02:00
|
|
|
$basePath = $this->getCurrentRelativePath();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-01-14 05:23:21 +01:00
|
|
|
/** @var Requirements_Backend $backend */
|
2015-11-30 03:03:46 +01:00
|
|
|
$backend = Injector::inst()->create('Requirements_Backend');
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->setupRequirements($backend);
|
2009-04-29 02:07:39 +02:00
|
|
|
|
2011-03-30 08:49:11 +02:00
|
|
|
$backend->javascript($basePath . '/RequirementsTest_a.js?test=1&test=2&test=3');
|
|
|
|
$backend->css($basePath . '/RequirementsTest_a.css?test=1&test=2&test=3');
|
2016-04-19 07:20:30 +02:00
|
|
|
$html = $backend->includeInHTML(self::$html_template);
|
2009-04-29 02:07:39 +02:00
|
|
|
|
|
|
|
/* Javascript has correct path */
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->assertRegExp(
|
|
|
|
'/src=".*\/RequirementsTest_a\.js\?m=\d\d+&test=1&test=2&test=3/',
|
|
|
|
$html,
|
|
|
|
'javascript has correct path'
|
|
|
|
);
|
2009-04-29 02:07:39 +02:00
|
|
|
|
|
|
|
/* CSS has correct path */
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->assertRegExp(
|
|
|
|
'/href=".*\/RequirementsTest_a\.css\?m=\d\d+&test=1&test=2&test=3/',
|
|
|
|
$html,
|
|
|
|
'css has correct path'
|
|
|
|
);
|
2009-04-29 02:07:39 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testRequirementsBackend() {
|
2011-03-30 08:49:11 +02:00
|
|
|
$basePath = $this->getCurrentRelativePath();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-01-14 05:23:21 +01:00
|
|
|
/** @var Requirements_Backend $backend */
|
2015-11-30 03:03:46 +01:00
|
|
|
$backend = Injector::inst()->create('Requirements_Backend');
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->setupRequirements($backend);
|
2011-03-30 08:49:11 +02:00
|
|
|
$backend->javascript($basePath . '/a.js');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->assertTrue(count($backend->getJavascript()) == 1,
|
2012-09-26 23:34:00 +02:00
|
|
|
"There should be only 1 file included in required javascript.");
|
2016-07-04 08:55:35 +02:00
|
|
|
$this->assertArrayHasKey($basePath . '/a.js', $backend->getJavascript(),
|
2012-09-26 23:34:00 +02:00
|
|
|
"a.js should be included in required javascript.");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-30 08:49:11 +02:00
|
|
|
$backend->javascript($basePath . '/b.js');
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->assertTrue(count($backend->getJavascript()) == 2,
|
2012-09-26 23:34:00 +02:00
|
|
|
"There should be 2 files included in required javascript.");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-30 08:49:11 +02:00
|
|
|
$backend->block($basePath . '/a.js');
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->assertTrue(count($backend->getJavascript()) == 1,
|
2012-09-26 23:34:00 +02:00
|
|
|
"There should be only 1 file included in required javascript.");
|
2016-07-04 08:55:35 +02:00
|
|
|
$this->assertArrayNotHasKey($basePath . '/a.js', $backend->getJavascript(),
|
2012-09-26 23:34:00 +02:00
|
|
|
"a.js should not be included in required javascript after it has been blocked.");
|
2016-07-04 08:55:35 +02:00
|
|
|
$this->assertArrayHasKey($basePath . '/b.js', $backend->getJavascript(),
|
2012-09-26 23:34:00 +02:00
|
|
|
"b.js should be included in required javascript.");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-30 08:49:11 +02:00
|
|
|
$backend->css($basePath . '/a.css');
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->assertTrue(count($backend->getCSS()) == 1,
|
2012-09-26 23:34:00 +02:00
|
|
|
"There should be only 1 file included in required css.");
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->assertArrayHasKey($basePath . '/a.css', $backend->getCSS(),
|
2012-09-26 23:34:00 +02:00
|
|
|
"a.css should be in required css.");
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2011-03-30 08:49:11 +02:00
|
|
|
$backend->block($basePath . '/a.css');
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->assertTrue(count($backend->getCSS()) == 0,
|
2012-09-26 23:34:00 +02:00
|
|
|
"There should be nothing in required css after file has been blocked.");
|
2008-11-07 03:06:28 +01:00
|
|
|
}
|
2010-10-13 03:15:47 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testConditionalTemplateRequire() {
|
2011-03-30 08:49:11 +02:00
|
|
|
$basePath = $this->getCurrentRelativePath();
|
2012-09-26 23:34:00 +02:00
|
|
|
// we're asserting "framework", so set the relative path accordingly in case FRAMEWORK_DIR was changed
|
|
|
|
// to something else
|
2012-04-14 08:48:28 +02:00
|
|
|
$basePath = 'framework' . substr($basePath, strlen(FRAMEWORK_DIR));
|
|
|
|
|
2016-01-14 05:23:21 +01:00
|
|
|
/** @var Requirements_Backend $backend */
|
2015-11-30 03:03:46 +01:00
|
|
|
$backend = Injector::inst()->create('Requirements_Backend');
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->setupRequirements($backend);
|
2010-10-13 03:15:47 +02:00
|
|
|
$holder = Requirements::backend();
|
|
|
|
Requirements::set_backend($backend);
|
|
|
|
$data = new ArrayData(array(
|
|
|
|
'FailTest' => true,
|
|
|
|
));
|
|
|
|
$data->renderWith('RequirementsTest_Conditionals');
|
2012-11-23 14:55:19 +01:00
|
|
|
$this->assertFileIncluded($backend, 'css', $basePath .'/RequirementsTest_a.css');
|
|
|
|
$this->assertFileIncluded($backend, 'js',
|
2012-09-26 23:34:00 +02:00
|
|
|
array($basePath .'/RequirementsTest_b.js', $basePath .'/RequirementsTest_c.js'));
|
2012-11-23 14:55:19 +01:00
|
|
|
$this->assertFileNotIncluded($backend, 'js', $basePath .'/RequirementsTest_a.js');
|
|
|
|
$this->assertFileNotIncluded($backend, 'css',
|
2012-09-26 23:34:00 +02:00
|
|
|
array($basePath .'/RequirementsTest_b.css', $basePath .'/RequirementsTest_c.css'));
|
2010-10-13 03:15:47 +02:00
|
|
|
$backend->clear();
|
|
|
|
$data = new ArrayData(array(
|
|
|
|
'FailTest' => false,
|
|
|
|
));
|
|
|
|
$data->renderWith('RequirementsTest_Conditionals');
|
2012-11-23 14:55:19 +01:00
|
|
|
$this->assertFileNotIncluded($backend, 'css', $basePath .'/RequirementsTest_a.css');
|
|
|
|
$this->assertFileNotIncluded($backend, 'js',
|
2012-09-26 23:34:00 +02:00
|
|
|
array($basePath .'/RequirementsTest_b.js', $basePath .'/RequirementsTest_c.js'));
|
2012-11-23 14:55:19 +01:00
|
|
|
$this->assertFileIncluded($backend, 'js', $basePath .'/RequirementsTest_a.js');
|
|
|
|
$this->assertFileIncluded($backend, 'css',
|
2012-09-26 23:34:00 +02:00
|
|
|
array($basePath .'/RequirementsTest_b.css', $basePath .'/RequirementsTest_c.css'));
|
2010-10-13 03:15:47 +02:00
|
|
|
Requirements::set_backend($holder);
|
|
|
|
}
|
2011-12-23 16:33:45 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testJsWriteToBody() {
|
2016-01-14 05:23:21 +01:00
|
|
|
/** @var Requirements_Backend $backend */
|
2015-11-30 03:03:46 +01:00
|
|
|
$backend = Injector::inst()->create('Requirements_Backend');
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->setupRequirements($backend);
|
2011-12-23 16:33:45 +01:00
|
|
|
$backend->javascript('http://www.mydomain.com/test.js');
|
|
|
|
|
|
|
|
// Test matching with HTML5 <header> tags as well
|
|
|
|
$template = '<html><head></head><body><header>My header</header><p>Body</p></body></html>';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
$backend->setWriteJavascriptToBody(false);
|
2016-04-19 07:20:30 +02:00
|
|
|
$html = $backend->includeInHTML($template);
|
2011-12-23 16:33:45 +01:00
|
|
|
$this->assertContains('<head><script', $html);
|
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
$backend->setWriteJavascriptToBody(true);
|
2016-04-19 07:20:30 +02:00
|
|
|
$html = $backend->includeInHTML($template);
|
2011-12-23 16:33:45 +01:00
|
|
|
$this->assertNotContains('<head><script', $html);
|
|
|
|
$this->assertContains('</script></body>', $html);
|
|
|
|
}
|
2010-10-13 03:15:47 +02:00
|
|
|
|
2014-08-30 20:32:28 +02:00
|
|
|
public function testIncludedJsIsNotCommentedOut() {
|
|
|
|
$template = '<html><head></head><body><!--<script>alert("commented out");</script>--></body></html>';
|
2016-01-14 05:23:21 +01:00
|
|
|
/** @var Requirements_Backend $backend */
|
2015-11-30 03:03:46 +01:00
|
|
|
$backend = Injector::inst()->create('Requirements_Backend');
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->setupRequirements($backend);
|
2014-08-30 20:32:28 +02:00
|
|
|
$backend->javascript($this->getCurrentRelativePath() . '/RequirementsTest_a.js');
|
2016-04-19 07:20:30 +02:00
|
|
|
$html = $backend->includeInHTML($template);
|
2014-08-30 20:32:28 +02:00
|
|
|
//wiping out commented-out html
|
|
|
|
$html = preg_replace('/<!--(.*)-->/Uis', '', $html);
|
|
|
|
$this->assertContains("RequirementsTest_a.js", $html);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCommentedOutScriptTagIsIgnored() {
|
|
|
|
$template = '<html><head></head><body><!--<script>alert("commented out");</script>-->'
|
|
|
|
. '<h1>more content</h1></body></html>';
|
2016-01-14 05:23:21 +01:00
|
|
|
/** @var Requirements_Backend $backend */
|
2015-11-30 03:03:46 +01:00
|
|
|
$backend = Injector::inst()->create('Requirements_Backend');
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->setupRequirements($backend);
|
|
|
|
$backend->setSuffixRequirements(false);
|
2014-08-30 20:32:28 +02:00
|
|
|
$src = $this->getCurrentRelativePath() . '/RequirementsTest_a.js';
|
2015-10-12 06:34:34 +02:00
|
|
|
$urlSrc = ControllerTest_ContainerController::join_links(Director::baseURL(), $src);
|
2014-08-30 20:32:28 +02:00
|
|
|
$backend->javascript($src);
|
2016-04-19 07:20:30 +02:00
|
|
|
$html = $backend->includeInHTML($template);
|
2014-08-30 20:32:28 +02:00
|
|
|
$this->assertEquals('<html><head></head><body><!--<script>alert("commented out");</script>-->'
|
2016-01-24 06:12:45 +01:00
|
|
|
. '<h1>more content</h1><script type="application/javascript" src="' . $urlSrc . '"></script></body></html>', $html);
|
2014-08-30 20:32:28 +02:00
|
|
|
}
|
|
|
|
|
2013-12-07 15:38:51 +01:00
|
|
|
public function testForceJsToBottom() {
|
2016-01-14 05:23:21 +01:00
|
|
|
/** @var Requirements_Backend $backend */
|
2015-11-30 03:03:46 +01:00
|
|
|
$backend = Injector::inst()->create('Requirements_Backend');
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->setupRequirements($backend);
|
2013-12-07 15:38:51 +01:00
|
|
|
$backend->javascript('http://www.mydomain.com/test.js');
|
2016-04-19 07:20:30 +02:00
|
|
|
$backend->customScript(
|
|
|
|
<<<'EOS'
|
|
|
|
var globalvar = {
|
|
|
|
pattern: '\\$custom\\1'
|
|
|
|
};
|
|
|
|
EOS
|
|
|
|
);
|
2013-12-07 15:38:51 +01:00
|
|
|
|
|
|
|
// Test matching with HTML5 <header> tags as well
|
|
|
|
$template = '<html><head></head><body><header>My header</header><p>Body<script></script></p></body></html>';
|
|
|
|
|
2013-11-25 20:49:35 +01:00
|
|
|
// The expected outputs
|
2016-04-19 07:20:30 +02:00
|
|
|
$expectedScripts = "<script type=\"application/javascript\" src=\"http://www.mydomain.com/test.js\">"
|
|
|
|
. "</script><script type=\"application/javascript\">//<![CDATA[\n"
|
|
|
|
. "var globalvar = {\n\tpattern: '\\\\\$custom\\\\1'\n};\n"
|
|
|
|
. "//]]></script>";
|
|
|
|
$JsInHead = "<html><head>$expectedScripts</head><body><header>My header</header><p>Body<script></script></p></body></html>";
|
|
|
|
$JsInBody = "<html><head></head><body><header>My header</header><p>Body$expectedScripts<script></script></p></body></html>";
|
|
|
|
$JsAtEnd = "<html><head></head><body><header>My header</header><p>Body<script></script></p>$expectedScripts</body></html>";
|
2013-11-25 20:49:35 +01:00
|
|
|
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-11-25 20:49:35 +01:00
|
|
|
// Test if the script is before the head tag, not before the body.
|
|
|
|
// Expected: $JsInHead
|
2015-10-12 06:34:34 +02:00
|
|
|
$backend->setWriteJavascriptToBody(false);
|
|
|
|
$backend->setForceJSToBottom(false);
|
2016-04-19 07:20:30 +02:00
|
|
|
$html = $backend->includeInHTML($template);
|
2013-11-25 20:49:35 +01:00
|
|
|
$this->assertNotEquals($JsInBody, $html);
|
|
|
|
$this->assertNotEquals($JsAtEnd, $html);
|
|
|
|
$this->assertEquals($JsInHead, $html);
|
2013-12-07 15:38:51 +01:00
|
|
|
|
|
|
|
// Test if the script is before the first <script> tag, not before the body.
|
2013-11-25 20:49:35 +01:00
|
|
|
// Expected: $JsInBody
|
2015-10-12 06:34:34 +02:00
|
|
|
$backend->setWriteJavascriptToBody(true);
|
|
|
|
$backend->setForceJSToBottom(false);
|
2016-04-19 07:20:30 +02:00
|
|
|
$html = $backend->includeInHTML($template);
|
2013-11-25 20:49:35 +01:00
|
|
|
$this->assertNotEquals($JsAtEnd, $html);
|
|
|
|
$this->assertEquals($JsInBody, $html);
|
2013-12-07 15:38:51 +01:00
|
|
|
|
|
|
|
// Test if the script is placed just before the closing bodytag, with write-to-body false.
|
2013-11-25 20:49:35 +01:00
|
|
|
// Expected: $JsAtEnd
|
2015-10-12 06:34:34 +02:00
|
|
|
$backend->setWriteJavascriptToBody(false);
|
|
|
|
$backend->setForceJSToBottom(true);
|
2016-04-19 07:20:30 +02:00
|
|
|
$html = $backend->includeInHTML($template);
|
2013-11-25 20:49:35 +01:00
|
|
|
$this->assertNotEquals($JsInHead, $html);
|
|
|
|
$this->assertNotEquals($JsInBody, $html);
|
|
|
|
$this->assertEquals($JsAtEnd, $html);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-12-07 15:38:51 +01:00
|
|
|
// Test if the script is placed just before the closing bodytag, with write-to-body true.
|
2013-11-25 20:49:35 +01:00
|
|
|
// Expected: $JsAtEnd
|
2015-10-12 06:34:34 +02:00
|
|
|
$backend->setWriteJavascriptToBody(true);
|
|
|
|
$backend->setForceJSToBottom(true);
|
2016-04-19 07:20:30 +02:00
|
|
|
$html = $backend->includeInHTML($template);
|
2013-11-25 20:49:35 +01:00
|
|
|
$this->assertNotEquals($JsInHead, $html);
|
|
|
|
$this->assertNotEquals($JsInBody, $html);
|
|
|
|
$this->assertEquals($JsAtEnd, $html);
|
2013-12-07 15:38:51 +01:00
|
|
|
}
|
|
|
|
|
2012-12-18 00:58:09 +01:00
|
|
|
public function testSuffix() {
|
|
|
|
$template = '<html><head></head><body><header>My header</header><p>Body</p></body></html>';
|
|
|
|
$basePath = $this->getCurrentRelativePath();
|
|
|
|
|
2016-01-14 05:23:21 +01:00
|
|
|
/** @var Requirements_Backend $backend */
|
2015-11-30 03:03:46 +01:00
|
|
|
$backend = Injector::inst()->create('Requirements_Backend');
|
2015-10-12 06:34:34 +02:00
|
|
|
$this->setupRequirements($backend);
|
2012-12-18 00:58:09 +01:00
|
|
|
|
|
|
|
$backend->javascript($basePath .'/RequirementsTest_a.js');
|
|
|
|
$backend->javascript($basePath .'/RequirementsTest_b.js?foo=bar&bla=blubb');
|
|
|
|
$backend->css($basePath .'/RequirementsTest_a.css');
|
|
|
|
$backend->css($basePath .'/RequirementsTest_b.css?foo=bar&bla=blubb');
|
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
$backend->setSuffixRequirements(true);
|
2016-04-19 07:20:30 +02:00
|
|
|
$html = $backend->includeInHTML($template);
|
2013-04-30 00:33:21 +02:00
|
|
|
$this->assertRegexp('/RequirementsTest_a\.js\?m=[\d]*"/', $html);
|
|
|
|
$this->assertRegexp('/RequirementsTest_b\.js\?m=[\d]*&foo=bar&bla=blubb"/', $html);
|
|
|
|
$this->assertRegexp('/RequirementsTest_a\.css\?m=[\d]*"/', $html);
|
|
|
|
$this->assertRegexp('/RequirementsTest_b\.css\?m=[\d]*&foo=bar&bla=blubb"/', $html);
|
2012-12-18 00:58:09 +01:00
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
$backend->setSuffixRequirements(false);
|
2016-04-19 07:20:30 +02:00
|
|
|
$html = $backend->includeInHTML($template);
|
2012-12-18 00:58:09 +01:00
|
|
|
$this->assertNotContains('RequirementsTest_a.js=', $html);
|
2013-04-30 00:33:21 +02:00
|
|
|
$this->assertNotRegexp('/RequirementsTest_a\.js\?m=[\d]*"/', $html);
|
|
|
|
$this->assertNotRegexp('/RequirementsTest_b\.js\?m=[\d]*&foo=bar&bla=blubb"/', $html);
|
|
|
|
$this->assertNotRegexp('/RequirementsTest_a\.css\?m=[\d]*"/', $html);
|
|
|
|
$this->assertNotRegexp('/RequirementsTest_b\.css\?m=[\d]*&foo=bar&bla=blubb"/', $html);
|
2012-12-18 00:58:09 +01:00
|
|
|
}
|
|
|
|
|
2016-01-14 05:23:21 +01:00
|
|
|
/**
|
|
|
|
* Tests that provided files work
|
|
|
|
*/
|
|
|
|
public function testProvidedFiles() {
|
|
|
|
/** @var Requirements_Backend $backend */
|
|
|
|
$template = '<html><head></head><body><header>My header</header><p>Body</p></body></html>';
|
|
|
|
$basePath = $this->getCurrentRelativePath();
|
|
|
|
|
|
|
|
// Test that provided files block subsequent files
|
|
|
|
$backend = Injector::inst()->create('Requirements_Backend');
|
|
|
|
$this->setupRequirements($backend);
|
|
|
|
$backend->javascript($basePath . '/RequirementsTest_a.js');
|
|
|
|
$backend->javascript($basePath . '/RequirementsTest_b.js', [
|
|
|
|
'provides' => [
|
|
|
|
$basePath . '/RequirementsTest_a.js',
|
|
|
|
$basePath . '/RequirementsTest_c.js'
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
$backend->javascript($basePath . '/RequirementsTest_c.js');
|
|
|
|
// Note that _a.js isn't considered provided because it was included
|
|
|
|
// before it was marked as provided
|
|
|
|
$this->assertEquals([
|
|
|
|
$basePath . '/RequirementsTest_c.js' => $basePath . '/RequirementsTest_c.js'
|
|
|
|
], $backend->getProvidedScripts());
|
2016-04-19 07:20:30 +02:00
|
|
|
$html = $backend->includeInHTML($template);
|
2016-01-14 05:23:21 +01:00
|
|
|
$this->assertRegExp('/src=".*\/RequirementsTest_a\.js/', $html);
|
|
|
|
$this->assertRegExp('/src=".*\/RequirementsTest_b\.js/', $html);
|
|
|
|
$this->assertNotRegExp('/src=".*\/RequirementsTest_c\.js/', $html);
|
|
|
|
|
|
|
|
// Test that provided files block subsequent combined files
|
|
|
|
$backend = Injector::inst()->create('Requirements_Backend');
|
|
|
|
$this->setupRequirements($backend);
|
|
|
|
$backend->combineFiles('combined_a.js', [$basePath . '/RequirementsTest_a.js']);
|
|
|
|
$backend->javascript($basePath . '/RequirementsTest_b.js', [
|
|
|
|
'provides' => [
|
|
|
|
$basePath . '/RequirementsTest_a.js',
|
|
|
|
$basePath . '/RequirementsTest_c.js'
|
|
|
|
]
|
|
|
|
]);
|
|
|
|
$backend->combineFiles('combined_c.js', [$basePath . '/RequirementsTest_c.js']);
|
|
|
|
$this->assertEquals([
|
|
|
|
$basePath . '/RequirementsTest_c.js' => $basePath . '/RequirementsTest_c.js'
|
|
|
|
], $backend->getProvidedScripts());
|
2016-04-19 07:20:30 +02:00
|
|
|
$html = $backend->includeInHTML($template);
|
2016-01-14 05:23:21 +01:00
|
|
|
$this->assertRegExp('/src=".*\/combined_a/', $html);
|
|
|
|
$this->assertRegExp('/src=".*\/RequirementsTest_b\.js/', $html);
|
|
|
|
$this->assertNotRegExp('/src=".*\/combined_c/', $html);
|
|
|
|
$this->assertNotRegExp('/src=".*\/RequirementsTest_c\.js/', $html);
|
|
|
|
}
|
|
|
|
|
2015-10-12 06:34:34 +02:00
|
|
|
/**
|
|
|
|
* Verify that the given backend includes the given files
|
|
|
|
*
|
|
|
|
* @param Requirements_Backend $backend
|
|
|
|
* @param string $type js or css
|
|
|
|
* @param array|string $files Files or list of files to check
|
|
|
|
*/
|
2012-11-23 14:55:19 +01:00
|
|
|
public function assertFileIncluded($backend, $type, $files) {
|
2015-10-12 06:34:34 +02:00
|
|
|
$includedFiles = $this->getBackendFiles($backend, $type);
|
2012-11-23 14:55:19 +01:00
|
|
|
|
2010-10-13 03:15:47 +02:00
|
|
|
if(is_array($files)) {
|
|
|
|
$failedMatches = array();
|
|
|
|
foreach ($files as $file) {
|
2012-11-23 14:55:19 +01:00
|
|
|
if(!array_key_exists($file, $includedFiles)) {
|
2010-10-13 03:15:47 +02:00
|
|
|
$failedMatches[] = $file;
|
|
|
|
}
|
|
|
|
}
|
2012-11-23 14:55:19 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
(count($failedMatches) == 0),
|
2010-10-13 03:15:47 +02:00
|
|
|
"Failed asserting the $type files '"
|
|
|
|
. implode("', '", $failedMatches)
|
|
|
|
. "' have exact matches in the required elements:\n'"
|
2012-11-23 14:55:19 +01:00
|
|
|
. implode("'\n'", array_keys($includedFiles)) . "'"
|
2010-10-13 03:15:47 +02:00
|
|
|
);
|
|
|
|
} else {
|
2012-11-23 14:55:19 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
(array_key_exists($files, $includedFiles)),
|
|
|
|
"Failed asserting the $type file '$files' has an exact match in the required elements:\n'"
|
|
|
|
. implode("'\n'", array_keys($includedFiles)) . "'"
|
|
|
|
);
|
2010-10-13 03:15:47 +02:00
|
|
|
}
|
|
|
|
}
|
2012-12-08 12:20:20 +01:00
|
|
|
|
2012-11-23 14:55:19 +01:00
|
|
|
public function assertFileNotIncluded($backend, $type, $files) {
|
2015-10-12 06:34:34 +02:00
|
|
|
$includedFiles = $this->getBackendFiles($backend, $type);
|
2010-10-13 03:15:47 +02:00
|
|
|
if(is_array($files)) {
|
|
|
|
$failedMatches = array();
|
|
|
|
foreach ($files as $file) {
|
2012-11-23 14:55:19 +01:00
|
|
|
if(array_key_exists($file, $includedFiles)) {
|
2010-10-13 03:15:47 +02:00
|
|
|
$failedMatches[] = $file;
|
|
|
|
}
|
|
|
|
}
|
2012-11-23 14:55:19 +01:00
|
|
|
$this->assertTrue(
|
|
|
|
(count($failedMatches) == 0),
|
2010-10-13 03:15:47 +02:00
|
|
|
"Failed asserting the $type files '"
|
|
|
|
. implode("', '", $failedMatches)
|
|
|
|
. "' do not have exact matches in the required elements:\n'"
|
2012-11-23 14:55:19 +01:00
|
|
|
. implode("'\n'", array_keys($includedFiles)) . "'"
|
2010-10-13 03:15:47 +02:00
|
|
|
);
|
|
|
|
} else {
|
2012-11-23 14:55:19 +01:00
|
|
|
$this->assertFalse(
|
|
|
|
(array_key_exists($files, $includedFiles)),
|
|
|
|
"Failed asserting the $type file '$files' does not have an exact match in the required elements:"
|
|
|
|
. "\n'" . implode("'\n'", array_keys($includedFiles)) . "'"
|
|
|
|
);
|
2010-10-13 03:15:47 +02:00
|
|
|
}
|
|
|
|
}
|
2015-10-12 06:34:34 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get files of the given type from the backend
|
|
|
|
*
|
|
|
|
* @param Requirements_Backend $backend
|
|
|
|
* @param string $type js or css
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getBackendFiles($backend, $type) {
|
|
|
|
$type = strtolower($type);
|
|
|
|
switch (strtolower($type)) {
|
|
|
|
case 'css':
|
|
|
|
return $backend->getCSS();
|
|
|
|
case 'js':
|
|
|
|
case 'javascript':
|
|
|
|
case 'script':
|
2016-07-04 08:55:35 +02:00
|
|
|
return $backend->getJavascript();
|
2015-10-12 06:34:34 +02:00
|
|
|
}
|
|
|
|
return array();
|
|
|
|
}
|
2012-12-08 12:20:20 +01:00
|
|
|
}
|