class Ie9CspSubscriberTest in Content-Security-Policy 8
@coversDefaultClass \Drupal\csp\EventSubscriber\Ie9CspSubscriber @group csp
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\csp\Unit\EventSubscriber\Ie9CspSubscriberTest
Expanded class hierarchy of Ie9CspSubscriberTest
File
- tests/
src/ Unit/ EventSubscriber/ Ie9CspSubscriberTest.php, line 18
Namespace
Drupal\Tests\csp\Unit\EventSubscriberView source
class Ie9CspSubscriberTest extends UnitTestCase {
/**
* The Module Handler service.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit\Framework\MockObject\MockObject
*/
private $moduleHandler;
/**
* The response object.
*
* @var \Drupal\Core\Render\HtmlResponse|\PHPUnit\Framework\MockObject\MockObject
*/
private $response;
/**
* {@inheritdoc}
*/
public function setUp() : void {
parent::setUp();
$this->moduleHandler = $this
->getMockBuilder(ModuleHandlerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->response = $this
->getMockBuilder(HtmlResponse::class)
->disableOriginalConstructor()
->getMock();
}
/**
* Check that the subscriber listens to the Policy Alter event.
*
* @covers ::getSubscribedEvents
*/
public function testSubscribedEvents() {
$this
->assertArrayHasKey(CspEvents::POLICY_ALTER, CoreCspSubscriber::getSubscribedEvents());
}
/**
* Shouldn't alter the policy if no directives are enabled.
*
* @covers ::onCspPolicyAlter
*/
public function testNoDirectives() {
$this->moduleHandler
->method('moduleExists')
->willReturn($this
->callback(function ($parameter) {
return $parameter === 'ie9';
}));
/** @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject $configFactory */
$configFactory = $this
->getConfigFactoryStub([
'system.performance' => [
'css.preprocess' => FALSE,
],
]);
$policy = new Csp();
$alterEvent = new PolicyAlterEvent($policy, $this->response);
$subscriber = new Ie9CspSubscriber($configFactory, $this->moduleHandler);
$subscriber
->onCspPolicyAlter($alterEvent);
$this
->assertFalse($alterEvent
->getPolicy()
->hasDirective('script-src'));
$this
->assertFalse($alterEvent
->getPolicy()
->hasDirective('script-src-attr'));
$this
->assertFalse($alterEvent
->getPolicy()
->hasDirective('script-src-elem'));
}
/**
* Test that enabled style directives are modified.
*
* @covers ::onCspPolicyAlter
*/
public function testStyle() {
$this->moduleHandler
->method('moduleExists')
->willReturn($this
->callback(function ($parameter) {
return $parameter === 'ie9';
}));
/** @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject $configFactory */
$configFactory = $this
->getConfigFactoryStub([
'system.performance' => [
'css.preprocess' => FALSE,
],
]);
$policy = new Csp();
$policy
->setDirective('default-src', [
Csp::POLICY_ANY,
]);
$policy
->setDirective('style-src', [
Csp::POLICY_SELF,
]);
$policy
->setDirective('style-src-attr', [
Csp::POLICY_SELF,
]);
$policy
->setDirective('style-src-elem', [
Csp::POLICY_SELF,
]);
$alterEvent = new PolicyAlterEvent($policy, $this->response);
$subscriber = new Ie9CspSubscriber($configFactory, $this->moduleHandler);
$subscriber
->onCspPolicyAlter($alterEvent);
$this
->assertEquals([
Csp::POLICY_SELF,
Csp::POLICY_UNSAFE_INLINE,
], $alterEvent
->getPolicy()
->getDirective('style-src'));
$this
->assertEquals([
Csp::POLICY_SELF,
], $alterEvent
->getPolicy()
->getDirective('style-src-attr'));
$this
->assertEquals([
Csp::POLICY_SELF,
Csp::POLICY_UNSAFE_INLINE,
], $alterEvent
->getPolicy()
->getDirective('style-src-elem'));
}
/**
* Test that style directive are not modified if CSS preprocessing is enabled.
*
* @covers ::onCspPolicyAlter
*/
public function testPreprocessEnabled() {
$this->moduleHandler
->method('moduleExists')
->willReturn($this
->callback(function ($parameter) {
return $parameter === 'ie9';
}));
/** @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject $configFactory */
$configFactory = $this
->getConfigFactoryStub([
'system.performance' => [
'css.preprocess' => TRUE,
],
]);
$policy = new Csp();
$policy
->setDirective('default-src', [
Csp::POLICY_ANY,
]);
$policy
->setDirective('style-src', [
Csp::POLICY_SELF,
]);
$policy
->setDirective('style-src-attr', [
Csp::POLICY_SELF,
]);
$policy
->setDirective('style-src-elem', [
Csp::POLICY_SELF,
]);
$alterEvent = new PolicyAlterEvent($policy, $this->response);
$subscriber = new Ie9CspSubscriber($configFactory, $this->moduleHandler);
$subscriber
->onCspPolicyAlter($alterEvent);
$this
->assertEquals([
Csp::POLICY_SELF,
], $alterEvent
->getPolicy()
->getDirective('style-src'));
$this
->assertEquals([
Csp::POLICY_SELF,
], $alterEvent
->getPolicy()
->getDirective('style-src-attr'));
$this
->assertEquals([
Csp::POLICY_SELF,
], $alterEvent
->getPolicy()
->getDirective('style-src-elem'));
}
/**
* Test style-src-elem fallback if style-src enabled.
*
* @covers ::onCspPolicyAlter
*/
public function testStyleElemFallback() {
$this->moduleHandler
->method('moduleExists')
->with($this
->equalTo('ie9'))
->willReturn(TRUE);
/** @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject $configFactory */
$configFactory = $this
->getConfigFactoryStub([
'system.performance' => [
'css.preprocess' => FALSE,
],
]);
$policy = new Csp();
$policy
->setDirective('default-src', [
Csp::POLICY_ANY,
]);
$policy
->setDirective('style-src', [
Csp::POLICY_SELF,
]);
$alterEvent = new PolicyAlterEvent($policy, $this->response);
$subscriber = new Ie9CspSubscriber($configFactory, $this->moduleHandler);
$subscriber
->onCspPolicyAlter($alterEvent);
$this
->assertEquals([
Csp::POLICY_SELF,
Csp::POLICY_UNSAFE_INLINE,
], $alterEvent
->getPolicy()
->getDirective('style-src'));
$this
->assertEquals([
Csp::POLICY_SELF,
], $alterEvent
->getPolicy()
->getDirective('style-src-attr'));
$this
->assertEquals([
Csp::POLICY_SELF,
Csp::POLICY_UNSAFE_INLINE,
], array_unique($alterEvent
->getPolicy()
->getDirective('style-src-elem')));
}
/**
* Test style-src-elem fallback if default-src enabled.
*
* @covers ::onCspPolicyAlter
*/
public function testStyleDefaultFallback() {
$this->moduleHandler
->method('moduleExists')
->willReturn($this
->callback(function ($parameter) {
return $parameter === 'ie9';
}));
/** @var \Drupal\Core\Config\ConfigFactoryInterface|\PHPUnit_Framework_MockObject_MockObject $configFactory */
$configFactory = $this
->getConfigFactoryStub([
'system.performance' => [
'css.preprocess' => FALSE,
],
]);
$policy = new Csp();
$policy
->setDirective('default-src', [
Csp::POLICY_SELF,
]);
$alterEvent = new PolicyAlterEvent($policy, $this->response);
$subscriber = new Ie9CspSubscriber($configFactory, $this->moduleHandler);
$subscriber
->onCspPolicyAlter($alterEvent);
$this
->assertEquals([
Csp::POLICY_SELF,
Csp::POLICY_UNSAFE_INLINE,
], $alterEvent
->getPolicy()
->getDirective('style-src'));
$this
->assertEquals([
Csp::POLICY_SELF,
], $alterEvent
->getPolicy()
->getDirective('style-src-attr'));
$this
->assertEquals([
Csp::POLICY_SELF,
Csp::POLICY_UNSAFE_INLINE,
], array_unique($alterEvent
->getPolicy()
->getDirective('style-src-elem')));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
Ie9CspSubscriberTest:: |
private | property | The Module Handler service. | |
Ie9CspSubscriberTest:: |
private | property | The response object. | |
Ie9CspSubscriberTest:: |
public | function |
Overrides UnitTestCase:: |
|
Ie9CspSubscriberTest:: |
public | function | Shouldn't alter the policy if no directives are enabled. | |
Ie9CspSubscriberTest:: |
public | function | Test that style directive are not modified if CSS preprocessing is enabled. | |
Ie9CspSubscriberTest:: |
public | function | Test that enabled style directives are modified. | |
Ie9CspSubscriberTest:: |
public | function | Test style-src-elem fallback if default-src enabled. | |
Ie9CspSubscriberTest:: |
public | function | Test style-src-elem fallback if style-src enabled. | |
Ie9CspSubscriberTest:: |
public | function | Check that the subscriber listens to the Policy Alter event. | |
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. |