Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add integration tests for DataExtensions applied #41

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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>';
}
}