2008-07-18 01:32:31 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @package sapphire
|
|
|
|
* @subpackage tests
|
|
|
|
*
|
|
|
|
* @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 {
|
|
|
|
|
|
|
|
static $html_template = '<html><head></head><body></body></html>';
|
|
|
|
|
2008-08-11 09:22:50 +02:00
|
|
|
function testExternalUrls() {
|
|
|
|
Requirements::javascript('http://www.mydomain.com/test.js');
|
|
|
|
Requirements::javascript('https://www.mysecuredomain.com/test.js');
|
|
|
|
Requirements::css('http://www.mydomain.com/test.css');
|
|
|
|
Requirements::css('https://www.mysecuredomain.com/test.css');
|
|
|
|
|
|
|
|
$html = Requirements::includeInHTML(false, 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, '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'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2008-07-18 01:32:31 +02:00
|
|
|
function testCombinedJavascript() {
|
2008-07-18 05:47:17 +02:00
|
|
|
$this->setupCombinedRequirements();
|
2008-07-18 01:32:31 +02:00
|
|
|
|
|
|
|
$combinedFilePath = Director::baseFolder() . '/' . 'bc.js';
|
|
|
|
|
|
|
|
$html = Requirements::includeInHTML(false, 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 */
|
2008-08-11 08:28:29 +02:00
|
|
|
$this->assertTrue((bool)preg_match('/src=".*\/bc\.js/', $html), 'combined javascript file is included in html header');
|
2008-07-18 01:32:31 +02:00
|
|
|
|
|
|
|
/* COMBINED JAVASCRIPT FILE EXISTS */
|
2008-07-18 05:47:17 +02:00
|
|
|
$this->assertTrue(file_exists($combinedFilePath), 'combined javascript file exists');
|
2008-07-18 01:32:31 +02:00
|
|
|
|
|
|
|
/* COMBINED JAVASCRIPT HAS CORRECT CONTENT */
|
2008-07-18 05:47:17 +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');
|
2008-07-18 01:32:31 +02:00
|
|
|
|
|
|
|
/* COMBINED FILES ARE NOT INCLUDED TWICE */
|
2008-08-11 08:28:29 +02:00
|
|
|
$this->assertFalse((bool)preg_match('/src=".*\/b\.js/', $html), 'combined files are not included twice');
|
|
|
|
$this->assertFalse((bool)preg_match('/src=".*\/c\.js/', $html), 'combined files are not included twice');
|
2008-07-18 01:32:31 +02:00
|
|
|
|
|
|
|
/* NORMAL REQUIREMENTS ARE STILL INCLUDED */
|
2008-08-11 08:28:29 +02:00
|
|
|
$this->assertTrue((bool)preg_match('/src=".*\/a\.js/', $html), 'normal requirements are still included');
|
2008-07-18 05:47:17 +02:00
|
|
|
|
2008-08-12 01:18:56 +02:00
|
|
|
Requirements::delete_combined_files('bc.js');
|
2008-07-18 05:47:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function testBlockedCombinedJavascript() {
|
|
|
|
$combinedFilePath = Director::baseFolder() . '/' . 'bc.js';
|
2008-08-11 09:22:50 +02:00
|
|
|
|
2008-07-18 05:47:17 +02:00
|
|
|
/* BLOCKED COMBINED FILES ARE NOT INCLUDED */
|
|
|
|
$this->setupCombinedRequirements();
|
|
|
|
Requirements::block('bc.js');
|
2008-08-12 01:18:56 +02:00
|
|
|
Requirements::delete_combined_files('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
|
|
|
|
$html = Requirements::includeInHTML(false, self::$html_template);
|
2008-08-11 09:22:50 +02:00
|
|
|
|
2008-08-11 08:28:29 +02:00
|
|
|
$this->assertFalse((bool)preg_match('/src=".*\/bc\.js/', $html), 'blocked combined files are not included ');
|
2008-07-18 05:47:17 +02:00
|
|
|
Requirements::unblock('bc.js');
|
|
|
|
|
|
|
|
/* BLOCKED UNCOMBINED FILES ARE NOT INCLUDED */
|
|
|
|
// need to re-add requirements, as Requirements::process_combined_includes() alters the
|
|
|
|
// original arrays grml...
|
|
|
|
$this->setupCombinedRequirements();
|
|
|
|
Requirements::block('sapphire/tests/forms/b.js');
|
2008-08-12 01:18:56 +02:00
|
|
|
Requirements::delete_combined_files('bc.js');
|
2008-07-18 05:47:17 +02:00
|
|
|
clearstatcache(); // needed to get accurate file_exists() results
|
|
|
|
$html = Requirements::includeInHTML(false, self::$html_template);
|
|
|
|
$this->assertFalse((strpos(file_get_contents($combinedFilePath), "alert('b')") !== false), 'blocked uncombined files are not included');
|
|
|
|
Requirements::unblock('b.js');
|
|
|
|
|
|
|
|
/* A SINGLE FILE CAN'T BE INCLUDED IN TWO COMBINED FILES */
|
|
|
|
$this->setupCombinedRequirements();
|
|
|
|
clearstatcache(); // needed to get accurate file_exists() results
|
2008-08-12 01:18:56 +02:00
|
|
|
|
|
|
|
// This throws a notice-level error, so we prefix with @
|
|
|
|
@Requirements::combine_files(
|
2008-07-18 05:47:17 +02:00
|
|
|
'ac.js',
|
|
|
|
array(
|
|
|
|
'sapphire/tests/forms/a.js',
|
|
|
|
'sapphire/tests/forms/c.js'
|
|
|
|
)
|
|
|
|
);
|
2008-08-12 01:18:56 +02:00
|
|
|
|
2008-07-18 05:47:17 +02:00
|
|
|
$combinedFiles = Requirements::get_combine_files();
|
|
|
|
$this->assertEquals(
|
|
|
|
array_keys($combinedFiles),
|
2008-08-11 09:22:50 +02:00
|
|
|
array('bc.js'),
|
|
|
|
"A single file can't be included in two combined files"
|
2008-07-18 05:47:17 +02:00
|
|
|
);
|
2008-07-18 01:32:31 +02:00
|
|
|
|
2008-08-12 01:18:56 +02:00
|
|
|
Requirements::delete_combined_files('bc.js');
|
2008-07-18 05:47:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This is a bit of a hack, as it alters the Requirements
|
|
|
|
* statics globally for all tests.
|
|
|
|
*
|
|
|
|
* @todo Refactor Requirements to work on test instance level
|
|
|
|
*/
|
|
|
|
protected function setupCombinedRequirements() {
|
|
|
|
Requirements::clear();
|
|
|
|
|
|
|
|
// clearing all previously generated requirements (just in case)
|
2008-08-12 01:18:56 +02:00
|
|
|
Requirements::clear_combined_files();
|
|
|
|
Requirements::delete_combined_files('bc.js');
|
2008-07-18 01:32:31 +02:00
|
|
|
|
2008-07-18 05:47:17 +02:00
|
|
|
// require files normally (e.g. called from a FormField instance)
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
Requirements::javascript(SAPPHIRE_DIR . '/tests/forms/a.js');
|
|
|
|
Requirements::javascript(SAPPHIRE_DIR . '/tests/forms/b.js');
|
|
|
|
Requirements::javascript(SAPPHIRE_DIR . '/tests/forms/c.js');
|
2008-07-18 05:47:17 +02:00
|
|
|
|
|
|
|
// require two of those files as combined includes
|
|
|
|
Requirements::combine_files(
|
|
|
|
'bc.js',
|
|
|
|
array(
|
ENHANCEMENT Introduced constants for system paths like /sapphire in preparation for a more flexible directory reorganisation. Instead of hardcoding your path, please use the following constants: BASE_PATH, BASE_URL, SAPPHIRE_DIR, SAPPHIRE_PATH, CMS_DIR, CMS_PATH, THIRDPARTY_DIR, THIRDPARTY_PATH, ASSETS_DIR, ASSETS_PATH, THEMES_DIR, THEMES_PATH
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@63154 467b73ca-7a2a-4603-9d3b-597d59a354a9
2008-09-27 18:02:38 +02:00
|
|
|
SAPPHIRE_DIR . '/tests/forms/b.js',
|
|
|
|
SAPPHIRE_DIR . '/tests/forms/c.js'
|
2008-07-18 05:47:17 +02:00
|
|
|
)
|
|
|
|
);
|
2008-07-18 01:32:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
?>
|