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>' ;
2010-03-30 06:53:24 +02:00
static $old_requirements = null ;
2008-08-11 09:22:50 +02:00
function testExternalUrls () {
2010-03-04 22:25:18 +01:00
$backend = new Requirements_Backend ;
$backend -> set_combined_files_enabled ( true );
$backend -> javascript ( 'http://www.mydomain.com/test.js' );
$backend -> javascript ( 'https://www.mysecuredomain.com/test.js' );
$backend -> css ( 'http://www.mydomain.com/test.css' );
$backend -> css ( 'https://www.mysecuredomain.com/test.css' );
2008-08-11 09:22:50 +02:00
2010-03-04 22:25:18 +01:00
$html = $backend -> includeInHTML ( false , self :: $html_template );
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 (
( 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'
);
}
2010-03-04 22:25:18 +01:00
protected function setupCombinedRequirements ( $backend ) {
$backend -> clear ();
2010-03-05 00:05:21 +01:00
$backend -> setCombinedFilesFolder ( 'assets' );
2010-03-04 22:25:18 +01:00
// clearing all previously generated requirements (just in case)
$backend -> clear_combined_files ();
$backend -> delete_combined_files ( 'RequirementsTest_bc.js' );
// require files normally (e.g. called from a FormField instance)
$backend -> javascript ( SAPPHIRE_DIR . '/tests/forms/RequirementsTest_a.js' );
$backend -> javascript ( SAPPHIRE_DIR . '/tests/forms/RequirementsTest_b.js' );
$backend -> javascript ( SAPPHIRE_DIR . '/tests/forms/RequirementsTest_c.js' );
// require two of those files as combined includes
$backend -> combine_files (
'RequirementsTest_bc.js' ,
array (
SAPPHIRE_DIR . '/tests/forms/RequirementsTest_b.js' ,
SAPPHIRE_DIR . '/tests/forms/RequirementsTest_c.js'
)
);
}
2008-07-18 01:32:31 +02:00
function testCombinedJavascript () {
2010-03-04 22:25:18 +01:00
$backend = new Requirements_Backend ;
$backend -> set_combined_files_enabled ( true );
2010-03-05 00:05:21 +01:00
$backend -> setCombinedFilesFolder ( 'assets' );
2010-03-04 22:25:18 +01:00
$this -> setupCombinedRequirements ( $backend );
2008-07-18 01:32:31 +02:00
2010-03-05 00:05:21 +01:00
$combinedFilePath = Director :: baseFolder () . '/assets/' . 'RequirementsTest_bc.js' ;
2010-03-04 22:25:18 +01:00
$html = $backend -> 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-11-01 15:04:31 +01:00
$this -> assertTrue (( bool ) preg_match ( '/src=".*\/RequirementsTest_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-11-01 15:04:31 +01:00
$this -> assertFalse (( bool ) preg_match ( '/src=".*\/RequirementsTest_b\.js/' , $html ), 'combined files are not included twice' );
$this -> assertFalse (( bool ) preg_match ( '/src=".*\/RequirementsTest_c\.js/' , $html ), 'combined files are not included twice' );
2008-07-18 01:32:31 +02:00
/* NORMAL REQUIREMENTS ARE STILL INCLUDED */
2008-11-01 15:04:31 +01:00
$this -> assertTrue (( bool ) preg_match ( '/src=".*\/RequirementsTest_a\.js/' , $html ), 'normal requirements are still included' );
2008-07-18 05:47:17 +02:00
2010-03-04 22:25:18 +01:00
$backend -> delete_combined_files ( 'RequirementsTest_bc.js' );
2008-07-18 05:47:17 +02:00
}
function testBlockedCombinedJavascript () {
2010-03-04 22:25:18 +01:00
$backend = new Requirements_Backend ;
$backend -> set_combined_files_enabled ( true );
2010-03-05 00:05:21 +01:00
$backend -> setCombinedFilesFolder ( 'assets' );
$combinedFilePath = Director :: baseFolder () . '/assets/' . 'RequirementsTest_bc.js' ;
2008-08-11 09:22:50 +02:00
2008-07-18 05:47:17 +02:00
/* BLOCKED COMBINED FILES ARE NOT INCLUDED */
2010-03-04 22:25:18 +01:00
$this -> setupCombinedRequirements ( $backend );
$backend -> block ( 'RequirementsTest_bc.js' );
$backend -> delete_combined_files ( '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
2010-03-04 22:25:18 +01:00
$html = $backend -> includeInHTML ( false , self :: $html_template );
2008-08-11 09:22:50 +02:00
2008-11-01 15:04:31 +01:00
$this -> assertFalse (( bool ) preg_match ( '/src=".*\/RequirementsTest_bc\.js/' , $html ), 'blocked combined files are not included ' );
2010-03-04 22:25:18 +01:00
$backend -> unblock ( 'RequirementsTest_bc.js' );
2008-07-18 05:47:17 +02:00
/* BLOCKED UNCOMBINED FILES ARE NOT INCLUDED */
2010-03-04 22:25:18 +01:00
$this -> setupCombinedRequirements ( $backend );
$backend -> block ( 'sapphire/tests/forms/RequirementsTest_b.js' );
$backend -> delete_combined_files ( 'RequirementsTest_bc.js' );
2008-07-18 05:47:17 +02:00
clearstatcache (); // needed to get accurate file_exists() results
2010-03-04 22:25:18 +01:00
$html = $backend -> includeInHTML ( false , self :: $html_template );
2008-07-18 05:47:17 +02:00
$this -> assertFalse (( strpos ( file_get_contents ( $combinedFilePath ), " alert('b') " ) !== false ), 'blocked uncombined files are not included' );
2010-03-04 22:25:18 +01:00
$backend -> unblock ( 'RequirementsTest_b.js' );
2008-07-18 05:47:17 +02:00
/* A SINGLE FILE CAN'T BE INCLUDED IN TWO COMBINED FILES */
2010-03-04 22:25:18 +01: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
// This throws a notice-level error, so we prefix with @
2010-03-04 22:25:18 +01:00
@ $backend -> combine_files (
2008-11-01 15:04:31 +01:00
'RequirementsTest_ac.js' ,
2008-07-18 05:47:17 +02:00
array (
2008-11-01 15:04:31 +01:00
'sapphire/tests/forms/RequirementsTest_a.js' ,
'sapphire/tests/forms/RequirementsTest_c.js'
2008-07-18 05:47:17 +02:00
)
);
2008-08-12 01:18:56 +02:00
2010-03-04 22:25:18 +01:00
$combinedFiles = $backend -> get_combine_files ();
2008-07-18 05:47:17 +02:00
$this -> assertEquals (
array_keys ( $combinedFiles ),
2008-11-01 15:04:31 +01:00
array ( 'RequirementsTest_bc.js' ),
2008-08-11 09:22:50 +02:00
" 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
2010-03-04 22:25:18 +01:00
$backend -> delete_combined_files ( 'RequirementsTest_bc.js' );
2008-07-18 05:47:17 +02:00
}
2009-04-29 02:07:39 +02:00
function testArgsInUrls () {
2010-03-04 22:25:18 +01:00
$backend = new Requirements_Backend ;
$backend -> set_combined_files_enabled ( true );
2009-04-29 02:07:39 +02:00
2010-03-04 22:25:18 +01:00
$backend -> javascript ( SAPPHIRE_DIR . '/tests/forms/RequirementsTest_a.js?test=1&test=2&test=3' );
$backend -> css ( SAPPHIRE_DIR . '/tests/forms/RequirementsTest_a.css?test=1&test=2&test=3' );
2010-03-05 02:27:44 +01:00
$backend -> delete_combined_files ( 'RequirementsTest_bc.js' );
2009-04-29 02:07:39 +02:00
2010-03-04 22:25:18 +01:00
$html = $backend -> includeInHTML ( false , self :: $html_template );
2009-04-29 02:07:39 +02:00
/* Javascript has correct path */
$this -> assertTrue (( bool ) preg_match ( '/src=".*\/RequirementsTest_a\.js\?m=\d\d+&test=1&test=2&test=3/' , $html ), 'javascript has correct path' );
/* CSS has correct path */
$this -> assertTrue (( bool ) preg_match ( '/href=".*\/RequirementsTest_a\.css\?m=\d\d+&test=1&test=2&test=3/' , $html ), 'css has correct path' );
}
2008-11-07 03:06:28 +01:00
function testRequirementsBackend () {
2010-03-04 22:25:18 +01:00
$backend = new Requirements_Backend ();
$backend -> javascript ( SAPPHIRE_DIR . '/tests/forms/a.js' );
2008-11-07 03:06:28 +01:00
2010-03-04 22:25:18 +01:00
$this -> assertTrue ( count ( $backend -> get_javascript ()) == 1 , " There should be only 1 file included in required javascript. " );
$this -> assertTrue ( in_array ( SAPPHIRE_DIR . '/tests/forms/a.js' , $backend -> get_javascript ()), " /test/forms/a.js should be included in required javascript. " );
2008-11-07 03:06:28 +01:00
2010-03-04 22:25:18 +01:00
$backend -> javascript ( SAPPHIRE_DIR . '/tests/forms/b.js' );
$this -> assertTrue ( count ( $backend -> get_javascript ()) == 2 , " There should be 2 files included in required javascript. " );
2008-11-07 03:06:28 +01:00
2010-03-04 22:25:18 +01:00
$backend -> block ( SAPPHIRE_DIR . '/tests/forms/a.js' );
$this -> assertTrue ( count ( $backend -> get_javascript ()) == 1 , " There should be only 1 file included in required javascript. " );
$this -> assertFalse ( in_array ( SAPPHIRE_DIR . '/tests/forms/a.js' , $backend -> get_javascript ()), " /test/forms/a.js should not be included in required javascript after it has been blocked. " );
$this -> assertTrue ( in_array ( SAPPHIRE_DIR . '/tests/forms/b.js' , $backend -> get_javascript ()), " /test/forms/b.js should be included in required javascript. " );
2008-11-07 03:06:28 +01:00
2010-03-04 22:25:18 +01:00
$backend -> css ( SAPPHIRE_DIR . '/tests/forms/a.css' );
$this -> assertTrue ( count ( $backend -> get_css ()) == 1 , " There should be only 1 file included in required css. " );
$this -> assertArrayHasKey ( SAPPHIRE_DIR . '/tests/forms/a.css' , $backend -> get_css (), " /tests/forms/a.css should be in required css. " );
2008-11-07 03:06:28 +01:00
2010-03-04 22:25:18 +01:00
$backend -> block ( SAPPHIRE_DIR . '/tests/forms/a.css' );
$this -> assertTrue ( count ( $backend -> get_css ()) == 0 , " There should be nothing in required css after file has been blocked. " );
2008-11-07 03:06:28 +01:00
}
2010-03-30 06:53:24 +02:00
function testConditionalTemplateRequire () {
$backend = new RequirementsTest_Backend ();
$holder = Requirements :: backend ();
Requirements :: set_backend ( $backend );
$data = new ArrayData ( array (
'FailTest' => true ,
));
$data -> renderWith ( 'RequirementsTest_Conditionals' );
$backend -> assertFileIncluded ( 'css' , 'sapphire/tests/forms/RequirementsTest_a.css' );
$backend -> assertFileIncluded ( 'js' , array ( 'sapphire/tests/forms/RequirementsTest_b.js' , 'sapphire/tests/forms/RequirementsTest_c.js' ));
$backend -> assertFileNotIncluded ( 'js' , 'sapphire/tests/forms/RequirementsTest_a.js' );
$backend -> assertFileNotIncluded ( 'css' , array ( 'sapphire/tests/forms/RequirementsTest_b.css' , 'sapphire/tests/forms/RequirementsTest_c.css' ));
$backend -> clear ();
$data = new ArrayData ( array (
'FailTest' => false ,
));
$data -> renderWith ( 'RequirementsTest_Conditionals' );
$backend -> assertFileNotIncluded ( 'css' , 'sapphire/tests/forms/RequirementsTest_a.css' );
$backend -> assertFileNotIncluded ( 'js' , array ( 'sapphire/tests/forms/RequirementsTest_b.js' , 'sapphire/tests/forms/RequirementsTest_c.js' ));
$backend -> assertFileIncluded ( 'js' , 'sapphire/tests/forms/RequirementsTest_a.js' );
$backend -> assertFileIncluded ( 'css' , array ( 'sapphire/tests/forms/RequirementsTest_b.css' , 'sapphire/tests/forms/RequirementsTest_c.css' ));
Requirements :: set_backend ( $holder );
}
}
class RequirementsTest_Backend extends Requirements_Backend implements TestOnly {
function assertFileIncluded ( $type , $files ) {
$type = strtolower ( $type );
switch ( strtolower ( $type )) {
case 'css' :
$var = 'css' ;
$type = 'CSS' ;
break ;
case 'js' :
case 'javascript' :
case 'script' :
$var = 'javascript' ;
$type = 'JavaScript' ;
break ;
}
if ( is_array ( $files )) {
$failedMatches = array ();
foreach ( $files as $file ) {
if ( ! array_key_exists ( $file , $this -> $var )) {
$failedMatches [] = $file ;
}
}
if ( count ( $failedMatches ) > 0 ) throw new PHPUnit_Framework_AssertionFailedError (
" Failed asserting the $type files ' "
. implode ( " ', ' " , $failedMatches )
. " ' have exact matches in the required elements: \n ' "
. implode ( " ' \n ' " , array_keys ( $this -> $var )) . " ' "
);
} else {
if ( ! array_key_exists ( $files , $this -> $var )) {
throw new PHPUnit_Framework_AssertionFailedError (
" Failed asserting the $type file ' $files ' has an exact match in the required elements: \n ' "
. implode ( " ' \n ' " , array_keys ( $this -> $var )) . " ' "
);
}
}
}
function assertFileNotIncluded ( $type , $files ) {
$type = strtolower ( $type );
switch ( $type ) {
case 'css' :
$var = 'css' ;
$type = 'CSS' ;
break ;
case 'js' :
case 'javascript' :
case 'script' :
$var = 'javascript' ;
$type = 'JavaScript' ;
break ;
}
if ( is_array ( $files )) {
$failedMatches = array ();
foreach ( $files as $file ) {
if ( array_key_exists ( $file , $this -> $var )) {
$failedMatches [] = $file ;
}
}
if ( count ( $failedMatches ) > 0 ) throw new PHPUnit_Framework_AssertionFailedError (
" Failed asserting the $type files ' "
. implode ( " ', ' " , $failedMatches )
. " ' do not have exact matches in the required elements: \n ' "
. implode ( " ' \n ' " , array_keys ( $this -> $var )) . " ' "
);
} else {
if ( array_key_exists ( $files , $this -> $var )) {
throw new PHPUnit_Framework_AssertionFailedError (
" Failed asserting the $type file ' $files ' does not have an exact match in the required elements: \n ' "
. implode ( " ' \n ' " , array_keys ( $this -> $var )) . " ' "
);
}
}
}
}