mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 14:05:37 +02:00
BUGFIX #4188 simon_w: Let require tags in templates be conditional
git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@101867 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
parent
4864553aca
commit
6892f1d196
@ -544,9 +544,9 @@ class SSViewer {
|
|||||||
|
|
||||||
$content = ereg_replace('<' . '% +current_page +%' . '>', '<?= $_SERVER[SCRIPT_URL] ?>', $content);
|
$content = ereg_replace('<' . '% +current_page +%' . '>', '<?= $_SERVER[SCRIPT_URL] ?>', $content);
|
||||||
|
|
||||||
// add all requirements to the $requirements array
|
// change < % require x() % > calls to corresponding Requirement::x() ones, including 0, 1 or 2 options
|
||||||
preg_match_all('/<% require ([a-zA-Z]+)\(([^\)]+)\) %>/', $content, $requirements);
|
$content = preg_replace('/<% +require +([a-zA-Z]+)(?:\(([^),]+)\))? +%>/', '<? Requirements::\\1("\\2"); ?>', $content);
|
||||||
$content = preg_replace('/<% require .* %>/', null, $content);
|
$content = preg_replace('/<% +require +([a-zA-Z]+)\(([^),]+), *([^),]+)\) +%>/', '<? Requirements::\\1("\\2", "\\3"); ?>', $content);
|
||||||
|
|
||||||
// legacy
|
// legacy
|
||||||
$content = ereg_replace('<!-- +if +([A-Za-z0-9_]+) +-->', '<? if($item->cachedCall("\\1")) { ?>', $content);
|
$content = ereg_replace('<!-- +if +([A-Za-z0-9_]+) +-->', '<? if($item->cachedCall("\\1")) { ?>', $content);
|
||||||
@ -565,9 +565,6 @@ class SSViewer {
|
|||||||
$content = str_replace('?>',";\n \$val .= <<<SSVIEWER\n", $content);
|
$content = str_replace('?>',";\n \$val .= <<<SSVIEWER\n", $content);
|
||||||
|
|
||||||
$output = "<?php\n";
|
$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";
|
$output .= '$val .= <<<SSVIEWER' . "\n" . $content . "\nSSVIEWER;\n";
|
||||||
|
|
||||||
// Protect xml header @sam why is this run twice ?
|
// Protect xml header @sam why is this run twice ?
|
||||||
|
@ -10,6 +10,8 @@ class RequirementsTest extends SapphireTest {
|
|||||||
|
|
||||||
static $html_template = '<html><head></head><body></body></html>';
|
static $html_template = '<html><head></head><body></body></html>';
|
||||||
|
|
||||||
|
static $old_requirements = null;
|
||||||
|
|
||||||
function testExternalUrls() {
|
function testExternalUrls() {
|
||||||
$backend = new Requirements_Backend;
|
$backend = new Requirements_Backend;
|
||||||
$backend->set_combined_files_enabled(true);
|
$backend->set_combined_files_enabled(true);
|
||||||
@ -182,4 +184,103 @@ class RequirementsTest extends SapphireTest {
|
|||||||
$this->assertTrue(count($backend->get_css()) == 0, "There should be nothing in required css after file has been blocked.");
|
$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)) . "'"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user