You are here

public function CspOptimizationTest::testStyleSrcFallback in Content-Security-Policy 8

Test optimizing policy based on the style-src fallback list.

@covers ::getHeaderValue @covers ::getDirectiveFallbackList @covers ::reduceSourceList

File

tests/src/Unit/CspOptimizationTest.php, line 170

Class

CspOptimizationTest
Test optimization of CSP directives.

Namespace

Drupal\Tests\csp\Unit

Code

public function testStyleSrcFallback() {
  $policy = new Csp();
  $policy
    ->setDirective('default-src', Csp::POLICY_SELF);
  $policy
    ->setDirective('style-src', [
    Csp::POLICY_SELF,
    Csp::POLICY_UNSAFE_INLINE,
  ]);

  // style-src-elem should not fall back to default-src.
  $policy
    ->setDirective('style-src-elem', Csp::POLICY_SELF);
  $policy
    ->setDirective('style-src-attr', Csp::POLICY_UNSAFE_INLINE);
  $this
    ->assertEquals("default-src 'self'; style-src 'self' 'unsafe-inline'; style-src-attr 'unsafe-inline'; style-src-elem 'self'", $policy
    ->getHeaderValue());
  $policy
    ->setDirective('style-src-attr', [
    Csp::POLICY_SELF,
    Csp::POLICY_UNSAFE_INLINE,
  ]);
  $this
    ->assertEquals("default-src 'self'; style-src 'self' 'unsafe-inline'; style-src-elem 'self'", $policy
    ->getHeaderValue());
}