Merge pull request #7713 from creative-commoners/pulls/4.0/block-via-reference

FIX Allow Requirements::block to handle module resource paths
This commit is contained in:
Damian Mooyman 2017-12-18 14:22:37 +13:00 committed by GitHub
commit 11b15f6c1a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 97 additions and 75 deletions

View File

@ -733,10 +733,14 @@ class Requirements_Backend
* Note that blocking should be used sparingly because it's hard to trace where an file is * Note that blocking should be used sparingly because it's hard to trace where an file is
* being blocked from. * being blocked from.
* *
* @param string|int $fileOrID * @param string|int $fileOrID Relative path from webroot, module resource reference or
* requirement API ID
*/ */
public function block($fileOrID) public function block($fileOrID)
{ {
if (is_string($fileOrID)) {
$fileOrID = ModuleResourceLoader::singleton()->resolvePath($fileOrID);
}
$this->blocked[$fileOrID] = $fileOrID; $this->blocked[$fileOrID] = $fileOrID;
} }

View File

@ -3,10 +3,8 @@
namespace SilverStripe\View\Tests; namespace SilverStripe\View\Tests;
use InvalidArgumentException; use InvalidArgumentException;
use SilverStripe\Control\Controller;
use SilverStripe\Core\Injector\Injector; use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest; use SilverStripe\Dev\SapphireTest;
use SilverStripe\Control\Director;
use SilverStripe\View\Requirements; use SilverStripe\View\Requirements;
use SilverStripe\View\ArrayData; use SilverStripe\View\ArrayData;
use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore; use SilverStripe\Assets\Tests\Storage\AssetStoreTest\TestAssetStore;
@ -53,38 +51,14 @@ class RequirementsTest extends SapphireTest
$html = $backend->includeInHTML(self::$html_template); $html = $backend->includeInHTML(self::$html_template);
$this->assertTrue( $this->assertContains('http://www.mydomain.com/test.js', $html, 'Load external javascript URL');
(strpos($html, 'http://www.mydomain.com/test.js') !== false), $this->assertContains('https://www.mysecuredomain.com/test.js', $html, 'Load external secure javascript URL');
'Load external javascript URL' $this->assertContains('//scheme-relative.example.com/test.js', $html, 'Load external scheme-relative JS');
); $this->assertContains('http://www.mydomain.com:3000/test.js', $html, 'Load external with port');
$this->assertTrue( $this->assertContains('http://www.mydomain.com/test.css', $html, 'Load external CSS URL');
(strpos($html, 'https://www.mysecuredomain.com/test.js') !== false), $this->assertContains('https://www.mysecuredomain.com/test.css', $html, 'Load external secure CSS URL');
'Load external secure javascript URL' $this->assertContains('//scheme-relative.example.com/test.css', $html, 'Load scheme-relative CSS URL');
); $this->assertContains('http://www.mydomain.com:3000/test.css', $html, 'Load external with port');
$this->assertTrue(
(strpos($html, '//scheme-relative.example.com/test.js') !== false),
'Load external scheme-relative javascript URL'
);
$this->assertTrue(
(strpos($html, 'http://www.mydomain.com:3000/test.js') !== false),
'Load external with port'
);
$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'
);
$this->assertTrue(
(strpos($html, '//scheme-relative.example.com/test.css') !== false),
'Load scheme-relative CSS URL'
);
$this->assertTrue(
(strpos($html, 'http://www.mydomain.com:3000/test.css') !== false),
'Load external with port'
);
} }
/** /**
@ -229,12 +203,14 @@ class RequirementsTest extends SapphireTest
); );
/* COMBINED JAVASCRIPT HAS CORRECT CONTENT */ /* COMBINED JAVASCRIPT HAS CORRECT CONTENT */
$this->assertTrue( $this->assertContains(
(strpos(file_get_contents($combinedFilePath), "alert('b')") !== false), "alert('b')",
file_get_contents($combinedFilePath),
'combined javascript has correct content' 'combined javascript has correct content'
); );
$this->assertTrue( $this->assertContains(
(strpos(file_get_contents($combinedFilePath), "alert('c')") !== false), "alert('c')",
file_get_contents($combinedFilePath),
'combined javascript has correct content' 'combined javascript has correct content'
); );
@ -278,12 +254,14 @@ class RequirementsTest extends SapphireTest
); );
/* COMBINED JAVASCRIPT HAS CORRECT CONTENT */ /* COMBINED JAVASCRIPT HAS CORRECT CONTENT */
$this->assertTrue( $this->assertContains(
(strpos(file_get_contents($combinedFilePath), "alert('b')") !== false), "alert('b')",
file_get_contents($combinedFilePath),
'combined javascript has correct content' 'combined javascript has correct content'
); );
$this->assertTrue( $this->assertContains(
(strpos(file_get_contents($combinedFilePath), "alert('c')") !== false), "alert('c')",
file_get_contents($combinedFilePath),
'combined javascript has correct content' 'combined javascript has correct content'
); );
@ -330,12 +308,14 @@ class RequirementsTest extends SapphireTest
); );
/* COMBINED JAVASCRIPT HAS CORRECT CONTENT */ /* COMBINED JAVASCRIPT HAS CORRECT CONTENT */
$this->assertTrue( $this->assertContains(
(strpos(file_get_contents($combinedFilePath), "alert('b')") !== false), "alert('b')",
file_get_contents($combinedFilePath),
'combined javascript has correct content' 'combined javascript has correct content'
); );
$this->assertTrue( $this->assertContains(
(strpos(file_get_contents($combinedFilePath), "alert('c')") !== false), "alert('c')",
file_get_contents($combinedFilePath),
'combined javascript has correct content' 'combined javascript has correct content'
); );
@ -402,12 +382,14 @@ class RequirementsTest extends SapphireTest
); );
/* COMBINED JAVASCRIPT HAS CORRECT CONTENT */ /* COMBINED JAVASCRIPT HAS CORRECT CONTENT */
$this->assertTrue( $this->assertContains(
(strpos(file_get_contents($combinedFilePath), "alert('b')") !== false), "alert('b')",
file_get_contents($combinedFilePath),
'combined javascript has correct content' 'combined javascript has correct content'
); );
$this->assertTrue( $this->assertContains(
(strpos(file_get_contents($combinedFilePath), "alert('c')") !== false), "alert('c')",
file_get_contents($combinedFilePath),
'combined javascript has correct content' 'combined javascript has correct content'
); );
@ -471,12 +453,14 @@ class RequirementsTest extends SapphireTest
); );
/* COMBINED JAVASCRIPT HAS CORRECT CONTENT */ /* COMBINED JAVASCRIPT HAS CORRECT CONTENT */
$this->assertTrue( $this->assertContains(
(strpos(file_get_contents($combinedFilePath), "alert('b')") !== false), "alert('b')",
file_get_contents($combinedFilePath),
'combined javascript has correct content' 'combined javascript has correct content'
); );
$this->assertTrue( $this->assertContains(
(strpos(file_get_contents($combinedFilePath), "alert('c')") !== false), "alert('c')",
file_get_contents($combinedFilePath),
'combined javascript has correct content' 'combined javascript has correct content'
); );
@ -607,8 +591,9 @@ class RequirementsTest extends SapphireTest
clearstatcache(); // needed to get accurate file_exists() results clearstatcache(); // needed to get accurate file_exists() results
$backend->includeInHTML(self::$html_template); $backend->includeInHTML(self::$html_template);
$this->assertFileExists($combinedFilePath2); $this->assertFileExists($combinedFilePath2);
$this->assertTrue( $this->assertNotContains(
strpos(file_get_contents($combinedFilePath2), "alert('b')") === false, "alert('b')",
file_get_contents($combinedFilePath2),
'blocked uncombined files are not included' 'blocked uncombined files are not included'
); );
$backend->unblock($basePath . '/javascript/RequirementsTest_b.js'); $backend->unblock($basePath . '/javascript/RequirementsTest_b.js');
@ -669,8 +654,9 @@ class RequirementsTest extends SapphireTest
$this->setupRequirements($backend); $this->setupRequirements($backend);
$backend->javascript($basePath . '/a.js'); $backend->javascript($basePath . '/a.js');
$this->assertTrue( $this->assertCount(
count($backend->getJavascript()) == 1, 1,
$backend->getJavascript(),
"There should be only 1 file included in required javascript." "There should be only 1 file included in required javascript."
); );
$this->assertArrayHasKey( $this->assertArrayHasKey(
@ -680,14 +666,16 @@ class RequirementsTest extends SapphireTest
); );
$backend->javascript($basePath . '/b.js'); $backend->javascript($basePath . '/b.js');
$this->assertTrue( $this->assertCount(
count($backend->getJavascript()) == 2, 2,
$backend->getJavascript(),
"There should be 2 files included in required javascript." "There should be 2 files included in required javascript."
); );
$backend->block($basePath . '/a.js'); $backend->block($basePath . '/a.js');
$this->assertTrue( $this->assertCount(
count($backend->getJavascript()) == 1, 1,
$backend->getJavascript(),
"There should be only 1 file included in required javascript." "There should be only 1 file included in required javascript."
); );
$this->assertArrayNotHasKey( $this->assertArrayNotHasKey(
@ -702,8 +690,9 @@ class RequirementsTest extends SapphireTest
); );
$backend->css($basePath . '/a.css'); $backend->css($basePath . '/a.css');
$this->assertTrue( $this->assertCount(
count($backend->getCSS()) == 1, 1,
$backend->getCSS(),
"There should be only 1 file included in required css." "There should be only 1 file included in required css."
); );
$this->assertArrayHasKey( $this->assertArrayHasKey(
@ -713,12 +702,37 @@ class RequirementsTest extends SapphireTest
); );
$backend->block($basePath . '/a.css'); $backend->block($basePath . '/a.css');
$this->assertTrue( $this->assertCount(
count($backend->getCSS()) == 0, 0,
$backend->getCSS(),
"There should be nothing in required css after file has been blocked." "There should be nothing in required css after file has been blocked."
); );
} }
public function testAppendAndBlockWithModuleResourceLoader()
{
$basePath = $this->getThemeRoot();
/** @var Requirements_Backend $backend */
$backend = Injector::inst()->create(Requirements_Backend::class);
$this->setupRequirements($backend);
// Note: assumes that client/styles/debug.css is "exposed"
$backend->css('silverstripe/framework:client/styles/debug.css');
$this->assertCount(
1,
$backend->getCSS(),
'Module resource can be loaded via resources reference'
);
$backend->block('silverstripe/framework:client/styles/debug.css');
$this->assertCount(
0,
$backend->getCSS(),
'Module resource can be blocked via resources reference'
);
}
public function testConditionalTemplateRequire() public function testConditionalTemplateRequire()
{ {
$testPath = $this->getThemeRoot(); $testPath = $this->getThemeRoot();
@ -1017,16 +1031,18 @@ EOS
$failedMatches[] = $file; $failedMatches[] = $file;
} }
} }
$this->assertTrue( $this->assertCount(
(count($failedMatches) == 0), 0,
$failedMatches,
"Failed asserting the $type files '" "Failed asserting the $type files '"
. implode("', '", $failedMatches) . implode("', '", $failedMatches)
. "' have exact matches in the required elements:\n'" . "' have exact matches in the required elements:\n'"
. implode("'\n'", array_keys($includedFiles)) . "'" . implode("'\n'", array_keys($includedFiles)) . "'"
); );
} else { } else {
$this->assertTrue( $this->assertArrayHasKey(
(array_key_exists($files, $includedFiles)), $files,
$includedFiles,
"Failed asserting the $type file '$files' has an exact match in the required elements:\n'" "Failed asserting the $type file '$files' has an exact match in the required elements:\n'"
. implode("'\n'", array_keys($includedFiles)) . "'" . implode("'\n'", array_keys($includedFiles)) . "'"
); );
@ -1043,16 +1059,18 @@ EOS
$failedMatches[] = $file; $failedMatches[] = $file;
} }
} }
$this->assertTrue( $this->assertCount(
(count($failedMatches) == 0), 0,
$failedMatches,
"Failed asserting the $type files '" "Failed asserting the $type files '"
. implode("', '", $failedMatches) . implode("', '", $failedMatches)
. "' do not have exact matches in the required elements:\n'" . "' do not have exact matches in the required elements:\n'"
. implode("'\n'", array_keys($includedFiles)) . "'" . implode("'\n'", array_keys($includedFiles)) . "'"
); );
} else { } else {
$this->assertFalse( $this->assertArrayNotHasKey(
(array_key_exists($files, $includedFiles)), $files,
$includedFiles,
"Failed asserting the $type file '$files' does not have an exact match in the required elements:" "Failed asserting the $type file '$files' does not have an exact match in the required elements:"
. "\n'" . implode("'\n'", array_keys($includedFiles)) . "'" . "\n'" . implode("'\n'", array_keys($includedFiles)) . "'"
); );