You are here

class CspSubscriberTest in Googalytics - Google Analytics 8

Test for Content Security Policy event integration.

@group ga @coversDefaultClass \Drupal\ga\EventSubscriber\CspSubscriber

Hierarchy

Expanded class hierarchy of CspSubscriberTest

File

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

Namespace

Drupal\Tests\ga\Unit\EventSubscriber
View 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

Namesort descending Modifiers Type Description Overrides
CspSubscriberTest::$response private property The response object.
CspSubscriberTest::setUp public function Overrides UnitTestCase::setUp
CspSubscriberTest::testConnectFallback public function Test connect-src fallback if default-src enabled.
CspSubscriberTest::testDirectives public function Test that enabled required directives are modified.
CspSubscriberTest::testImgFallback public function Test img-src fallback if default-src enabled.
CspSubscriberTest::testNoDirectives public function Shouldn't alter the policy if no directives are enabled.
CspSubscriberTest::testSubscribedEvents public function Check that the subscriber listens to the Policy Alter event.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.