From a1d64574f66be988fc19c4165528b1a55e44b6f4 Mon Sep 17 00:00:00 2001 From: Dylan Wagstaff Date: Mon, 11 Jun 2018 10:05:30 +1200 Subject: [PATCH] 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. --- .travis.yml | 5 +- ...nsion.php => PackageSecurityExtension.php} | 0 templates/SecurityAlertSummary.ss | 4 +- tests/PackageSecurityExtensionTest.php | 30 +++++++++ tests/PackagesWithAlerts.yml | 25 +++++++ tests/SecurityAlertExtensionTest.php | 22 +++++++ tests/SiteSummaryExtensionTest.php | 66 +++++++++++++++++++ tests/Stubs/SiteSummaryAlertStub.php | 14 ++++ 8 files changed, 162 insertions(+), 4 deletions(-) rename src/Extensions/{PackageSecruityExtension.php => PackageSecurityExtension.php} (100%) create mode 100644 tests/PackageSecurityExtensionTest.php create mode 100644 tests/PackagesWithAlerts.yml create mode 100644 tests/SecurityAlertExtensionTest.php create mode 100644 tests/SiteSummaryExtensionTest.php create mode 100644 tests/Stubs/SiteSummaryAlertStub.php diff --git a/.travis.yml b/.travis.yml index f0ca414..5e60164 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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 diff --git a/src/Extensions/PackageSecruityExtension.php b/src/Extensions/PackageSecurityExtension.php similarity index 100% rename from src/Extensions/PackageSecruityExtension.php rename to src/Extensions/PackageSecurityExtension.php diff --git a/templates/SecurityAlertSummary.ss b/templates/SecurityAlertSummary.ss index 66a317d..2880447 100644 --- a/templates/SecurityAlertSummary.ss +++ b/templates/SecurityAlertSummary.ss @@ -1,6 +1,6 @@ -

+

<%t SecurityAlertSummary.TITLE "Security alert" %>
- <% if Count > 1 %> + <% if $Count > 1 %> <%t SecurityAlertSummary.NOTICE_MANY "Notices have been issued for {count} of your modules. Review and updating is recommended." count=$Count %> <% else %> <%t SecurityAlertSummary.NOTICE_ONE "A notice has been issued for {count} of your modules. Review and updating is recommended." count=$Count %> diff --git a/tests/PackageSecurityExtensionTest.php b/tests/PackageSecurityExtensionTest.php new file mode 100644 index 0000000..ccabd1f --- /dev/null +++ b/tests/PackageSecurityExtensionTest.php @@ -0,0 +1,30 @@ +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); + } +} diff --git a/tests/PackagesWithAlerts.yml b/tests/PackagesWithAlerts.yml new file mode 100644 index 0000000..7470e9d --- /dev/null +++ b/tests/PackagesWithAlerts.yml @@ -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' \ No newline at end of file diff --git a/tests/SecurityAlertExtensionTest.php b/tests/SecurityAlertExtensionTest.php new file mode 100644 index 0000000..ea9ef36 --- /dev/null +++ b/tests/SecurityAlertExtensionTest.php @@ -0,0 +1,22 @@ +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()); + } +} diff --git a/tests/SiteSummaryExtensionTest.php b/tests/SiteSummaryExtensionTest.php new file mode 100644 index 0000000..b1833ab --- /dev/null +++ b/tests/SiteSummaryExtensionTest.php @@ -0,0 +1,66 @@ + [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( + '2', + $content, + 'ensure we are counting modules with alerts, not the total number of alerts' + ); + } +} diff --git a/tests/Stubs/SiteSummaryAlertStub.php b/tests/Stubs/SiteSummaryAlertStub.php new file mode 100644 index 0000000..a47d4c3 --- /dev/null +++ b/tests/Stubs/SiteSummaryAlertStub.php @@ -0,0 +1,14 @@ +Alert! Alert!
Sound the alarm!

'; + } +}