You are here

public function Ie9CspSubscriberTest::testStyleElemFallback in Content-Security-Policy 8

Test style-src-elem fallback if style-src enabled.

@covers ::onCspPolicyAlter

File

tests/src/Unit/EventSubscriber/Ie9CspSubscriberTest.php, line 178

Class

Ie9CspSubscriberTest
@coversDefaultClass \Drupal\csp\EventSubscriber\Ie9CspSubscriber @group csp

Namespace

Drupal\Tests\csp\Unit\EventSubscriber

Code

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