You are here

public function CspSubscriberTest::testDirectives in Googalytics - Google Analytics 8

Test that enabled required directives are modified.

@covers ::onCspPolicyAlter

File

tests/src/Unit/EventSubscriber/CspSubscriberTest.php, line 74

Class

CspSubscriberTest
Test for Content Security Policy event integration.

Namespace

Drupal\Tests\ga\Unit\EventSubscriber

Code

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'));
}