class CspSubscriberTest in Googalytics - Google Analytics 8
Test for Content Security Policy event integration.
@group ga @coversDefaultClass \Drupal\ga\EventSubscriber\CspSubscriber
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\ga\Unit\EventSubscriber\CspSubscriberTest
Expanded class hierarchy of CspSubscriberTest
File
- tests/
src/ Unit/ EventSubscriber/ CspSubscriberTest.php, line 19
Namespace
Drupal\Tests\ga\Unit\EventSubscriberView source
class CspSubscriberTest extends UnitTestCase {
/**
* The response object.
*
* @var \Drupal\Core\Render\HtmlResponse|\PHPUnit\Framework\MockObject\MockObject
*/
private $response;
/**
* {@inheritdoc}
*/
public function setUp() {
parent::setUp();
if (!class_exists(Csp::class)) {
$this
->markTestSkipped('Content Security Policy module is not available.');
}
$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() {
$policy = new Csp();
$alterEvent = new PolicyAlterEvent($policy, $this->response);
$subscriber = new CspSubscriber();
$subscriber
->onCspPolicyAlter($alterEvent);
$this
->assertFalse($alterEvent
->getPolicy()
->hasDirective('default-src'));
$this
->assertFalse($alterEvent
->getPolicy()
->hasDirective('img-src'));
$this
->assertFalse($alterEvent
->getPolicy()
->hasDirective('connect-src'));
}
/**
* Test that enabled required directives are modified.
*
* @covers ::onCspPolicyAlter
*/
public function testDirectives() {
$policy = new Csp();
$policy
->setDirective('default-src', [
Csp::POLICY_ANY,
]);
$policy
->setDirective('img-src', [
Csp::POLICY_SELF,
]);
$policy
->setDirective('connect-src', [
Csp::POLICY_SELF,
]);
$alterEvent = new PolicyAlterEvent($policy, $this->response);
$subscriber = new CspSubscriber();
$subscriber
->onCspPolicyAlter($alterEvent);
$this
->assertArrayEquals([
Csp::POLICY_ANY,
], $alterEvent
->getPolicy()
->getDirective('default-src'));
$this
->assertArrayEquals([
Csp::POLICY_SELF,
CspSubscriber::TRACKING_DOMAIN,
], $alterEvent
->getPolicy()
->getDirective('img-src'));
$this
->assertArrayEquals([
Csp::POLICY_SELF,
CspSubscriber::TRACKING_DOMAIN,
], $alterEvent
->getPolicy()
->getDirective('connect-src'));
}
/**
* Test img-src fallback if default-src enabled.
*
* @covers ::onCspPolicyAlter
*/
public function testImgFallback() {
$policy = new Csp();
$policy
->setDirective('default-src', [
Csp::POLICY_SELF,
]);
$alterEvent = new PolicyAlterEvent($policy, $this->response);
$subscriber = new CspSubscriber();
$subscriber
->onCspPolicyAlter($alterEvent);
$this
->assertArrayEquals([
Csp::POLICY_SELF,
], $alterEvent
->getPolicy()
->getDirective('default-src'));
$this
->assertArrayEquals([
Csp::POLICY_SELF,
CspSubscriber::TRACKING_DOMAIN,
], $alterEvent
->getPolicy()
->getDirective('img-src'));
}
/**
* Test connect-src fallback if default-src enabled.
*
* @covers ::onCspPolicyAlter
*/
public function testConnectFallback() {
$policy = new Csp();
$policy
->setDirective('default-src', [
Csp::POLICY_SELF,
]);
$alterEvent = new PolicyAlterEvent($policy, $this->response);
$subscriber = new CspSubscriber();
$subscriber
->onCspPolicyAlter($alterEvent);
$this
->assertArrayEquals([
Csp::POLICY_SELF,
], $alterEvent
->getPolicy()
->getDirective('default-src'));
$this
->assertArrayEquals([
Csp::POLICY_SELF,
CspSubscriber::TRACKING_DOMAIN,
], $alterEvent
->getPolicy()
->getDirective('connect-src'));
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CspSubscriberTest:: |
private | property | The response object. | |
CspSubscriberTest:: |
public | function |
Overrides UnitTestCase:: |
|
CspSubscriberTest:: |
public | function | Test connect-src fallback if default-src enabled. | |
CspSubscriberTest:: |
public | function | Test that enabled required directives are modified. | |
CspSubscriberTest:: |
public | function | Test img-src fallback if default-src enabled. | |
CspSubscriberTest:: |
public | function | Shouldn't alter the policy if no directives are enabled. | |
CspSubscriberTest:: |
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. |