class LibraryPolicyBuilderTest in Content-Security-Policy 8
@coversDefaultClass \Drupal\csp\LibraryPolicyBuilder @group csp
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\csp\Unit\LibraryPolicyBuilderTest
Expanded class hierarchy of LibraryPolicyBuilderTest
File
- tests/
src/ Unit/ LibraryPolicyBuilderTest.php, line 16
Namespace
Drupal\Tests\csp\UnitView source
class LibraryPolicyBuilderTest extends UnitTestCase {
/**
* Memory Cache backend.
*
* @var \Drupal\Core\Cache\CacheBackendInterface
*/
protected $cache;
/**
* Mock Module Handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $moduleHandler;
/**
* Mock Active Theme.
*
* @var \Drupal\Core\Theme\ActiveTheme|\PHPUnit_Framework_MockObject_MockObject
*/
protected $activeTheme;
/**
* Mock Theme Handler.
*
* @var \Drupal\Core\Extension\ThemeHandlerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $themeHandler;
/**
* Mock Library Discovery.
*
* @var \Drupal\Core\Asset\LibraryDiscoveryInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $libraryDiscovery;
/**
* {@inheritdoc}
*/
public function setUp() : void {
parent::setUp();
$this->cache = new MemoryBackend();
$this->moduleHandler = $this
->getMockBuilder(ModuleHandler::class)
->disableOriginalConstructor()
->getMock();
$this->themeHandler = $this
->getMockBuilder(ThemeHandler::class)
->disableOriginalConstructor()
->getMock();
$this->libraryDiscovery = $this
->getMockBuilder(LibraryDiscovery::class)
->disableOriginalConstructor()
->getMock();
}
/**
* Test an empty extension set.
*
* @covers ::getSources
* @covers ::getExtensionSources
* @covers ::getLibrarySources
*/
public function testEmptyPolicy() {
$this->themeHandler
->expects($this
->atLeastOnce())
->method('listInfo')
->willReturn([]);
$this->moduleHandler
->expects($this
->atLeastOnce())
->method('getModuleList')
->willReturn([]);
$this->libraryDiscovery
->expects($this
->atLeastOnce())
->method('getLibrariesByExtension')
->with('core')
->willReturn([]);
$libraryPolicy = new LibraryPolicyBuilder($this->cache, $this->moduleHandler, $this->themeHandler, $this->libraryDiscovery);
$this
->assertEquals([], $libraryPolicy
->getSources());
}
/**
* Test that a library's external sources are discovered.
*
* @covers ::getSources
* @covers ::getExtensionSources
* @covers ::getLibrarySources
* @covers ::getHostFromUri
*/
public function testLibraryWithSources() {
$this->moduleHandler
->expects($this
->any())
->method('getModuleList')
->willReturn([]);
$this->themeHandler
->expects($this
->any())
->method('listInfo')
->willReturn([
'stark' => (object) [
'name' => 'stark',
],
]);
$extensionMap = [
[
'core',
[],
],
[
'stark',
[
'test' => [],
],
],
];
$this->libraryDiscovery
->expects($this
->any())
->method('getLibrariesByExtension')
->willReturnMap($extensionMap);
// Test a few behaviours:
// - local files are ignored.
// - script domains are sorted.
// - duplicate style domains are filtered.
$libraryInfo = [
'js' => [
[
'type' => 'file',
'data' => 'js/script.js',
],
[
'type' => 'external',
'data' => 'http://js.example.org/js/script.js',
],
[
'type' => 'external',
'data' => 'http://js.example.com/js/script.js',
],
],
'css' => [
[
'type' => 'external',
'data' => 'http://css.example.com/css/style1.css',
],
[
'type' => 'external',
'data' => 'http://css.example.com/css/style2.css',
],
],
];
$this->libraryDiscovery
->expects($this
->atLeastOnce())
->method('getLibraryByName')
->with('stark', 'test')
->willReturn($libraryInfo);
$libraryPolicy = new LibraryPolicyBuilder($this->cache, $this->moduleHandler, $this->themeHandler, $this->libraryDiscovery);
$this
->assertEquals([
'script-src' => [
'js.example.com',
'js.example.org',
],
'script-src-elem' => [
'js.example.com',
'js.example.org',
],
'style-src' => [
'css.example.com',
],
'style-src-elem' => [
'css.example.com',
],
], $libraryPolicy
->getSources());
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
LibraryPolicyBuilderTest:: |
protected | property | Mock Active Theme. | |
LibraryPolicyBuilderTest:: |
protected | property | Memory Cache backend. | |
LibraryPolicyBuilderTest:: |
protected | property | Mock Library Discovery. | |
LibraryPolicyBuilderTest:: |
protected | property | Mock Module Handler. | |
LibraryPolicyBuilderTest:: |
protected | property | Mock Theme Handler. | |
LibraryPolicyBuilderTest:: |
public | function |
Overrides UnitTestCase:: |
|
LibraryPolicyBuilderTest:: |
public | function | Test an empty extension set. | |
LibraryPolicyBuilderTest:: |
public | function | Test that a library's external sources are discovered. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |