-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration tests for DataExtensions applied
By this module to classes provided by bringyourownideas/silverstripe-mainenance Utilising hook points to alter the report generated by that module, along with adding relationships to link Package info to Security Alerts. These now have some rudimentary tests which at least ensure the extensions are applied automatically iff the requisite module exists.
- Loading branch information
Dylan Wagstaff
committed
Jun 11, 2018
1 parent
38ebb03
commit 731d3c2
Showing
8 changed files
with
162 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
class PackageSecurityExtensionTest extends SapphireTest | ||
{ | ||
protected static $fixture_file = 'PackagesWithAlerts.yml'; | ||
|
||
public function setUp() | ||
{ | ||
if (!class_exists(Package::class)) { | ||
$this->markTestSkipped( | ||
'Module bringyourownideas/silverstripe-maintenance is required for this test, but is not present.' | ||
); | ||
} | ||
parent::setUp(); | ||
} | ||
|
||
public function testListSecurityAlertIdentifiers() | ||
{ | ||
$package = $this->objFromFixture(Package::class, 'otheralerts'); | ||
$this->assertEquals('ABC-001, SPY-007', $package->listSecurityAlertIdentifiers()); | ||
} | ||
|
||
public function testGetBadgesHook() | ||
{ | ||
$package = $this->objFromFixture(Package::class, 'otheralerts'); | ||
$badges = $package->getBadges(); | ||
$this->assertCount(1, $badges); | ||
$this->assertEquals('warning security-alerts__toggler', $badges->first()->Type); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
Package: | ||
noalerts: | ||
Name: venderr/noalert | ||
Type: silverstripe-module | ||
analert: | ||
Name: venderr/analert | ||
Type: silverstripe-vendormodule | ||
otheralerts: | ||
Name: venderr/otheralerts | ||
Type: silverstripe-module | ||
SecurityAlert: | ||
one: | ||
ID: 1 | ||
Title: 'an alert' | ||
PackageRecord: '=>Package.analert' | ||
two: | ||
ID: 2 | ||
Title: 'another alert' | ||
Identifier: ABC-001 | ||
PackageRecord: '=>Package.otheralerts' | ||
three: | ||
ID: 4 | ||
Title: 'other alert' | ||
Identifier: SPY-007 | ||
PackageRecord: '=>Package.otheralerts' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
class SecurityAlertExtensionTest extends SapphireTest | ||
{ | ||
protected static $fixture_file = 'PackagesWithAlerts.yml'; | ||
|
||
public function setUp() | ||
{ | ||
if (!class_exists(Package::class)) { | ||
$this->markTestSkipped( | ||
'Module bringyourownideas/silverstripe-maintenance is required for this test, but is not present.' | ||
); | ||
} | ||
parent::setUp(); | ||
} | ||
|
||
public function testExtensionAppliesWhenMaintenanceModuleIsPresent() | ||
{ | ||
$alert = $this->objFromFixture(SecurityAlert::class, 'two'); | ||
$this->assertTrue($alert->PackageRecord()->exists()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
<?php | ||
|
||
use BringYourOwnIdeas\SecurityChecker\Tests\Stubs\SiteSummaryAlertStub; | ||
|
||
class SiteSummaryExtensionTest extends SapphireTest | ||
{ | ||
protected static $fixture_file = 'PackagesWithAlerts.yml'; | ||
|
||
protected $requiredExtensions = [ | ||
SiteSummary::class => [SiteSummaryAlertStub::class] | ||
]; | ||
|
||
public function setUpOnce() | ||
{ | ||
if (!class_exists(Package::class)) { | ||
$this->requiredExtensions = []; | ||
} | ||
parent::setUpOnce(); | ||
} | ||
|
||
public function setUp() | ||
{ | ||
if (!class_exists(Package::class)) { | ||
$this->markTestSkipped( | ||
'Module bringyourownideas/silverstripe-maintenance is required for this test, but is not present.' | ||
); | ||
} | ||
// The themes should not affect test results. | ||
// Ensure we use the default templates supplied with this module. | ||
Config::inst()->update(SSViewer::class, 'theme_enabled', false); | ||
parent::setUp(); | ||
} | ||
|
||
public function testDisplayColumnsDoesNotIncludePrintingColumns() | ||
{ | ||
$report = Injector::inst()->create(SiteSummary::class); | ||
// screen output should not include this summary field added by the extension | ||
$this->assertArrayNotHasKey('listSecurityAlertIdentifiers', $report->columns()); | ||
// default summary fields are still used for print output (e.g. CSV export) | ||
$this->assertArrayHasKey('listSecurityAlertIdentifiers', Package::create()->summaryFields()); | ||
} | ||
|
||
public function testUpdateAlerts() | ||
{ | ||
$report = Injector::inst()->create(SiteSummary::class); | ||
$fields = $report->getCMSFields(); | ||
$alertSummary = $fields->fieldByName('AlertSummary'); | ||
$this->assertInstanceOf(LiteralField::class, $alertSummary); | ||
$content = $alertSummary->getContent(); | ||
$this->assertContains( | ||
'Sound the alarm!', | ||
$content, | ||
'ensure our extension doesn\'t override all others' | ||
); | ||
$this->assertContains( | ||
'site-summary__security-alerts', | ||
$content, | ||
'ensure content from our extension is present' | ||
); | ||
$this->assertContains( | ||
'<strong>2</strong>', | ||
$content, | ||
'ensure we are counting modules with alerts, not the total number of alerts' | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
namespace BringYourOwnIdeas\SecurityChecker\Tests\Stubs; | ||
|
||
use Extension; | ||
use TestOnly; | ||
|
||
class SiteSummaryAlertStub extends Extension implements TestOnly | ||
{ | ||
public function updateAlerts(&$alerts) | ||
{ | ||
$alerts[] = '<p><strong>Alert! Alert!</strong> <br />Sound the alarm!</p>'; | ||
} | ||
} |