diff --git a/core/Requirements.php b/core/Requirements.php index 6facd9845..781e11109 100644 --- a/core/Requirements.php +++ b/core/Requirements.php @@ -840,6 +840,45 @@ class Requirements_Backend { } } + foreach($files as $index=>$file) { + if(is_array($file)) { + // Either associative array path=>path type=>type or numeric 0=>path 1=>type + // Otherwise, assume path is the first item + if (isset($file['type']) && ($file['type'] == 'css' || $file['type'] == 'javascript' || $file['type'] == 'js')) { + switch ($file['type']) { + case 'css': + $this->css($file['path']); + break; + default: + $this->javascript($file['path']); + break; + } + $files[$index] = $file['path']; + } elseif (isset($file[1]) && ($file[1] == 'css' || $file[1] == 'javascript' || $file[1] == 'js')) { + switch ($file[1]) { + case 'css': + $this->css($file[0]); + break; + default: + $this->javascript($file[0]); + break; + } + $files[$index] = $file[0]; + } else { + $file = array_shift($file); + } + } + if (!is_array($file)) { + if(substr($file, -2) == 'js') { + $this->javascript($file); + } elseif(substr($file, -3) == 'css') { + $this->css($file); + } else { + user_error("Requirements_Backend::combine_files(): Couldn't guess file type for file '$file', please specify by passing using an array instead.", E_USER_NOTICE); + } + } + } + $this->combine_files[$combinedFileName] = $files; } diff --git a/tests/forms/RequirementsTest.php b/tests/forms/RequirementsTest.php index 2d64de1ec..3c04b2918 100644 --- a/tests/forms/RequirementsTest.php +++ b/tests/forms/RequirementsTest.php @@ -63,6 +63,24 @@ class RequirementsTest extends SapphireTest { ) ); } + + protected function setupCombinedNonrequiredRequirements($backend) { + $backend->clear(); + $backend->setCombinedFilesFolder('assets'); + + // clearing all previously generated requirements (just in case) + $backend->clear_combined_files(); + $backend->delete_combined_files('RequirementsTest_bc.js'); + + // require 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' + ) + ); + } function testCombinedJavascript() { $backend = new Requirements_Backend; @@ -93,6 +111,33 @@ class RequirementsTest extends SapphireTest { $this->assertTrue((bool)preg_match('/src=".*\/RequirementsTest_a\.js/', $html), 'normal requirements are still included'); $backend->delete_combined_files('RequirementsTest_bc.js'); + + // Then do it again, this time not requiring the files beforehand + $backend = new Requirements_Backend; + $backend->set_combined_files_enabled(true); + $backend->setCombinedFilesFolder('assets'); + + $this->setupCombinedNonrequiredRequirements($backend); + + $combinedFilePath = Director::baseFolder() . '/assets/' . 'RequirementsTest_bc.js'; + + $html = $backend->includeInHTML(false, self::$html_template); + + /* COMBINED JAVASCRIPT FILE IS INCLUDED IN HTML HEADER */ + $this->assertTrue((bool)preg_match('/src=".*\/RequirementsTest_bc\.js/', $html), 'combined javascript file is included in html header'); + + /* COMBINED JAVASCRIPT FILE EXISTS */ + $this->assertTrue(file_exists($combinedFilePath), 'combined javascript file exists'); + + /* 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'); + + /* COMBINED FILES ARE NOT INCLUDED TWICE */ + $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'); + + $backend->delete_combined_files('RequirementsTest_bc.js'); } function testBlockedCombinedJavascript() {