Skip to content

Commit

Permalink
Add onAfterInit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Firesphere committed Jul 27, 2019
1 parent ae673fe commit 095b9ea
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Extensions/ControllerCSPExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ protected function addInlineCSSPolicy($policy, $config): void
*/
public function isAddPolicyHeaders(): bool
{
return $this->addPolicyHeaders;
return $this->addPolicyHeaders ?? false;
}

/**
Expand Down
9 changes: 8 additions & 1 deletion tests/unit/ControllerExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use SilverStripe\Control\Cookie;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\NullHTTPRequest;
use SilverStripe\Dev\Debug;
use SilverStripe\Dev\SapphireTest;

class ControllerExtensionTest extends SapphireTest
Expand All @@ -23,6 +22,13 @@ public function testInit()
$extension = new ControllerCSPExtension();
$extension->setOwner($controller);

$this->assertFalse($extension->isAddPolicyHeaders());

$extension->onBeforeInit();
$this->assertNull($extension->getNonce());
$extension->onAfterInit();
$this->assertArrayNotHasKey('content-security-policy-report-only', $controller->getResponse()->getHeaders());

$request = new HTTPRequest('GET', '/', ['build-headers' => 'true']);
$controller->setRequest($request);
$extension->setOwner($controller);
Expand All @@ -41,5 +47,6 @@ public function testInit()
$extension->onAfterInit();

$this->assertArrayHasKey('content-security-policy-report-only', $controller->getResponse()->getHeaders());
$this->assertContains('self', $controller->getResponse()->getHeader('content-security-policy-report-only'));
}
}

0 comments on commit 095b9ea

Please sign in to comment.