BUGFIX #4188 simon_w: Let require tags in templates be conditional (from r101867)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@112044 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sam Minnee 2010-10-13 01:15:47 +00:00
parent 3ba0434d6d
commit 34207e069a
2 changed files with 106 additions and 8 deletions

View File

@ -538,9 +538,9 @@ class SSViewer {
$content = ereg_replace('<' . '% +current_page +%' . '>', '<?= $_SERVER[SCRIPT_URL] ?>', $content);
// add all requirements to the $requirements array
preg_match_all('/<% require ([a-zA-Z]+)\(([^\)]+)\) %>/', $content, $requirements);
$content = preg_replace('/<% require .* %>/', null, $content);
// change < % require x() % > calls to corresponding Requirement::x() ones, including 0, 1 or 2 options
$content = preg_replace('/<% +require +([a-zA-Z]+)(?:\(([^),]+)\))? +%>/', '<? Requirements::\\1("\\2"); ?>', $content);
$content = preg_replace('/<% +require +([a-zA-Z]+)\(([^),]+), *([^),]+)\) +%>/', '<? Requirements::\\1("\\2", "\\3"); ?>', $content);
// Add include filename comments on dev sites
@ -570,9 +570,6 @@ class SSViewer {
$content = str_replace('?>',";\n \$val .= <<<SSVIEWER\n", $content);
$output = "<?php\n";
for($i = 0; $i < count($requirements[0]); $i++) {
$output .= 'Requirements::' . $requirements[1][$i] . '(\'' . $requirements[2][$i] . "');\n";
}
$output .= '$val .= <<<SSVIEWER' . "\n" . $content . "\nSSVIEWER;\n";
// Protect xml header @sam why is this run twice ?

View File

@ -10,6 +10,8 @@ class RequirementsTest extends SapphireTest {
static $html_template = '<html><head></head><body></body></html>';
static $old_requirements = null;
function testExternalUrls() {
$backend = new Requirements_Backend;
$backend->set_combined_files_enabled(true);
@ -181,5 +183,104 @@ class RequirementsTest extends SapphireTest {
$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.");
}
}
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)) . "'"
);
}
}
}
}