Skip to content

Commit

Permalink
Add integration tests for DataExtensions applied
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 8 changed files with 162 additions and 4 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ matrix:
- php: 5.6
env: DB=MYSQL CORE_RELEASE=3.5
- php: 5.6
env: DB=MYSQL CORE_RELEASE=3.6
env: DB=MYSQL CORE_RELEASE=3.6 MAINTENANCE=^1
- php: 7.0
env: DB=MYSQL CORE_RELEASE=3.6
- php: 7.1
env: DB=MYSQL CORE_RELEASE=3.6
env: DB=MYSQL CORE_RELEASE=3.6 MAINTENANCE=^1.0.x-dev

before_script:
- composer self-update || true
- composer clear-cache
- composer validate --strict
- if [[ $MAINTENANCE ]]; then composer require --no-update bringyourownideas/silverstripe-maintenance $MAINTENANCE; fi
- git clone git://github.com/silverstripe-labs/silverstripe-travis-support.git ~/travis-support
- php ~/travis-support/travis_setup.php --source `pwd` --target ~/builds/ss
- cd ~/builds/ss
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions templates/SecurityAlertSummary.ss
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<p>
<p class="site-summary__security-alerts">
<strong><%t SecurityAlertSummary.TITLE "Security alert" %></strong><br />
<% if Count > 1 %>
<% if $Count > 1 %>
<%t SecurityAlertSummary.NOTICE_MANY "Notices have been issued for <strong>{count}</strong> of your modules. Review and updating is recommended." count=$Count %>
<% else %>
<%t SecurityAlertSummary.NOTICE_ONE "A notice has been issued for <strong>{count}</strong> of your modules. Review and updating is recommended." count=$Count %>
Expand Down
30 changes: 30 additions & 0 deletions tests/PackageSecurityExtensionTest.php
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);
}
}
25 changes: 25 additions & 0 deletions tests/PackagesWithAlerts.yml
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'
22 changes: 22 additions & 0 deletions tests/SecurityAlertExtensionTest.php
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());
}
}
66 changes: 66 additions & 0 deletions tests/SiteSummaryExtensionTest.php
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'
);
}
}
14 changes: 14 additions & 0 deletions tests/Stubs/SiteSummaryAlertStub.php
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>';
}
}

0 comments on commit 731d3c2

Please sign in to comment.