You are here

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

Test that enabled style directives are modified.

@covers ::onCspPolicyAlter

File

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

Class

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

Namespace

Drupal\Tests\csp\Unit\EventSubscriber

Code

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