diff --git a/.travis.yml b/.travis.yml index 3f9d86a..6c29ac4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -6,20 +6,18 @@ services: env: global: - - TRAVIS_NODE_VERSION="10" + - COMPOSER_ROOT_VERSION=4.3.x-dev matrix: include: - - php: 5.6 - env: DB=MYSQL RECIPE_VERSION=4.3.x-dev PHPUNIT_TEST=1 - - php: 7.0 - env: DB=PGSQL RECIPE_VERSION=4.3.x-dev PHPUNIT_TEST=1 - php: 7.1 - env: DB=MYSQL RECIPE_VERSION=4.3.x-dev PHPUNIT_TEST=1 PHPCS_TEST=1 + env: DB=MYSQL RECIPE_VERSION=~4.3.0 PHPUNIT_TEST=1 PHPCS_TEST=1 - php: 7.2 - env: DB=PGSQL RECIPE_VERSION=4.4.x-dev PHPUNIT_TEST=1 + env: DB=PGSQL RECIPE_VERSION=~4.4.0 PHPUNIT_TEST=1 - php: 7.3 - env: DB=MYSQL RECIPE_VERSION=4.x-dev PHPUNIT_COVERAGE_TEST=1 + env: DB=MYSQL RECIPE_VERSION=^4 + - php: 7.4 + env: DB=MYSQL RECIPE_VERSION=^4 PHPUNIT_COVERAGE_TEST=1 before_install: - mysql -e 'CREATE DATABASE IF NOT EXISTS test;' @@ -30,11 +28,11 @@ before_script: - composer validate - composer require --no-update silverstripe/recipe-cms:"$RECIPE_VERSION" - - if [[ $DB == PGSQL ]]; then composer require --no-update silverstripe/postgresql:2.1.x-dev; fi - - composer install --prefer-source --no-interaction --no-progress --no-suggest --optimize-autoloader --verbose --profile + - if [[ $DB == PGSQL ]]; then composer require --no-update silverstripe/postgresql:^2.1.0; fi + - composer install --prefer-source --no-interaction --no-progress --optimize-autoloader --verbose --profile script: - - if [[ $PHPUNIT_TEST ]]; then vendor/bin/phpunit tests/EnvBarExtensionTest.php; fi + - if [[ $PHPUNIT_TEST ]]; then vendor/bin/phpunit tests/; fi - if [[ $PHPUNIT_COVERAGE_TEST ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=coverage.xml; fi - if [[ $PHPCS_TEST ]]; then vendor/bin/phpcs src/ tests/ *.php; fi diff --git a/CHANGELOG.md b/CHANGELOG.md index 466c55f..fb7729a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,11 @@ # Changelog -1.0.0 (2020-02-03) -- Initial release \ No newline at end of file +1.1.0 (2020-06-18) +- Add override to settings to disable automatic frontend display +- Add yml config and template variable to enable developer repositioning + +1.0.0 (2020-05-26) +- Initial release + +(2020-02-03) +- Initial development \ No newline at end of file diff --git a/README.md b/README.md index b8e3c56..df62d5f 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ [![Build Status](https://travis-ci.org/signify-nz/silverstripe-environmentindicator.svg?branch=master)](https://travis-ci.org/signify-nz/silverstripe-environmentindicator) -[![codecov](https://codecov.io/gh/signify-nz/silverstripe-environmentindicator/branch/master/graph/badge.svg)](https://codecov.io/gh/signify-nz/silverstripe-environmentindicator) +[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/signify-nz/silverstripe-environmentindicator/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/signify-nz/silverstripe-environmentindicator/?branch=master) # Environment Indicator @@ -23,9 +23,20 @@ __Composer:__ ## Documentation -No further configuration is required. +No further configuration is required, however customisation is optional. -Customisation can be achieved by editing the EnvBar.ss template or the envbar- .js and .css files. Functional tests are available in the tests directory. These will need to be updated if you modify the template. +### Developer + +To disable the automatic placement of the bar as the first element of the body (if, for example, you have a fixed notification bar in your theme design), please add the following to a yml file in your site _config directory: + +```yml +Signify\EnvBar\Extensions\EnvBarExtension: + disable_auto_insert: true +``` + +You can then use the template variable `$EnvBar` in a more suitable position (recommended placement is in the Page.ss file or an include that is visible sitewide). + +Customisation of the content and/or appearance can be achieved by editing the EnvBar.ss template or the envbar- .js and .css files. Functional tests are available in the tests directory. These will need to be updated if you modify the template. * [Changelog](CHANGELOG.md) * [Contributing](CONTRIBUTING.md) @@ -34,7 +45,10 @@ Customisation can be achieved by editing the EnvBar.ss template or the envbar- . ## Usage -This module performs an informative function only. No special usage instructions are available. +This module performs an informative function only. + +There is a setting in the CMS to disable the bar from being visible on the frontend. +![Override Setting](docs/en/img/Override_Setting.png) ![Live Published Anonymous](docs/en/img/Live_Pub_Anon.png) ![Live Published Editor](docs/en/img/Live_Pub_Edit.png) diff --git a/_config/environmentindicatorconfig.yml b/_config/environmentindicatorconfig.yml index 4ddf89f..60130d8 100644 --- a/_config/environmentindicatorconfig.yml +++ b/_config/environmentindicatorconfig.yml @@ -9,4 +9,7 @@ After: "framework/*, cms/*" --- SilverStripe\CMS\Controllers\ContentController: extensions: - - Signify\EnvBar\Extensions\EnvBarExtension \ No newline at end of file + - Signify\EnvBar\Extensions\EnvBarExtension +SilverStripe\SiteConfig\SiteConfig: + extensions: + - Signify\EnvBar\Extensions\EnvBarSiteConfigExtension \ No newline at end of file diff --git a/composer.json b/composer.json index 6df944d..a8bdf62 100644 --- a/composer.json +++ b/composer.json @@ -48,7 +48,11 @@ } }, "config": { - "process-timeout": 600 + "process-timeout": 600, + "allow-plugins": { + "composer/installers": true, + "silverstripe/vendor-plugin": true + } }, "prefer-stable": true, "minimum-stability": "dev" diff --git a/docs/en/img/Override_Setting.png b/docs/en/img/Override_Setting.png new file mode 100644 index 0000000..11b77a1 Binary files /dev/null and b/docs/en/img/Override_Setting.png differ diff --git a/src/Extensions/EnvBarExtension.php b/src/Extensions/EnvBarExtension.php index b956fc9..cbce7e5 100644 --- a/src/Extensions/EnvBarExtension.php +++ b/src/Extensions/EnvBarExtension.php @@ -3,10 +3,12 @@ namespace Signify\EnvBar\Extensions; use SilverStripe\Control\Director; +use SilverStripe\Core\Config\Config; use SilverStripe\ORM\DataExtension; use SilverStripe\ORM\FieldType\DBHTMLText; use SilverStripe\Security\Permission; use SilverStripe\Security\Security; +use SilverStripe\SiteConfig\SiteConfig; use SilverStripe\Versioned\Versioned; use SilverStripe\View\Requirements; @@ -20,24 +22,25 @@ * and user access (member permissions) and returns values to populate the * EnvBar. * - * It also contains methods to modify the HTTPResponse before and after the - * @see SilverStripe\Control\RequestHandler inserting the CSS and HTML required - * to produce the EnvBar. + * It also contains methods to modify the @see SilverStripe\Control\HTTPResponse + * before and after the @see SilverStripe\Control\RequestHandler + * inserting the CSS and HTML required to produce the EnvBar. * * @package Signify\EnvBar\Extensions - * @author Lani Field - * @version 1.0.0 + * @author Lani Field + * @version 1.0.1 * - * @param HTTPRequest $request - * @return HTTPResponse $result with the EnvBar CSS and HTML inserted. + * @param \SilverStripe\Control\HTTPRequest $request + * @return \SilverStripe\Control\HTTPResponse $result + * with the EnvBar CSS and HTML inserted. */ class EnvBarExtension extends DataExtension { /** * Load the CSS requirement. * - * @param HTTPRequest $request - * @param string $action + * @param \SilverStripe\Control\HTTPRequest $request + * @param string $action * @return void * @see SilverStripe\Control\RequestHandler::handleAction */ @@ -47,19 +50,28 @@ public function beforeCallActionHandler($request, $action) } /** - * Rewrite the HTML of the viewed page to insert the EnvBar. + * Rewrite the HTML of the viewed page to insert the EnvBar if the + * two conditions (display and auto-insert enabled) have been met. * - * @param HTTPRequest $request - * @param string $action - * @param DBHTMLText $result from the original RequestHandler - * @return HTTPResponse $result with the EnvBar CSS and HTML inserted + * @param \SilverStripe\Control\HTTPRequest $request + * @param string $action + * @param \SilverStripe\ORM\FieldType\DBHTMLText $result + * from the original RequestHandler + * @return \SilverStripe\ORM\FieldType\DBHTMLText $result + * with the EnvBar HTML inserted * @see SilverStripe\Control\RequestHandler::handleAction */ public function afterCallActionHandler($request, $action, $result) { - if (!($result instanceof DBHTMLText)) { + // Check if display is turned on in the CMS + if (!SiteConfig::current_site_config()->EnvBarDisplay) { return $result; } + // Check if automatic placement has been turned off in a _config yml + if (Config::inst()->get(__CLASS__, 'disable_auto_insert')) { + return $result; + } + $html = $result->getValue(); $envBar = $this->generateEnvBar()->getValue(); $html = preg_replace( @@ -68,14 +80,17 @@ public function afterCallActionHandler($request, $action, $result) $html ); $result->setValue($html); + return $result; } /** * Check the environment type. * - * @return string "dev" if the site is in dev mode, "test" if the site is - * in test mode (e.g. QA or UAT), "live" otherwise (e.g. Production) + * @return string + * "dev" if the site is in dev mode, + * "test" if the site is in test mode (e.g. QA or UAT), + * "live" otherwise (e.g. Production) */ public function getEnvironment() { @@ -91,9 +106,10 @@ public function getEnvironment() /** * Check the version of the page being viewed. * - * @return string "published" if it is the current live version in this - * environment, "draft" if it is a modified or unpublished version, - * "not staged" otherwise + * @return string + * "published" if it is the current live version in this environment, + * "draft" if it is a modified or unpublished version, + * "not staged" otherwise */ public function getPageStatus() { @@ -109,7 +125,9 @@ public function getPageStatus() /** * Check whether CurrentUser has access to edit pages. * - * @return boolean "true" if the user can edit pages, "false" otherwise + * @return boolean + * "true" if the user can edit pages, + * "false" otherwise */ public function getCanAccess() { @@ -122,10 +140,25 @@ public function getCanAccess() } /** - * Generate the HTML to inject using the EnvBar.ss template. + * Get EnvBar HTML as a template variable. * * @return DBHTMLText */ + public function getEnvBar() + { + if ( + Config::inst()->get(__CLASS__, 'disable_auto_insert') + && SiteConfig::current_site_config()->EnvBarDisplay + ) { + return $this->generateEnvBar(); + } + } + + /** + * Generate the HTML to inject using the EnvBar.ss template. + * + * @return \SilverStripe\ORM\FieldType\DBHTMLText + */ private function generateEnvBar() { return $this->owner->renderWith('EnvBar'); diff --git a/src/Extensions/EnvBarSiteConfigExtension.php b/src/Extensions/EnvBarSiteConfigExtension.php new file mode 100644 index 0000000..c8416a6 --- /dev/null +++ b/src/Extensions/EnvBarSiteConfigExtension.php @@ -0,0 +1,49 @@ + + * @version 1.1.0 + * @package Signify\EnvBar\Extensions + */ +class EnvBarSiteConfigExtension extends DataExtension +{ + /** + * Add override column to SiteConfig db record. + * + * @var string[] + */ + private static $db = [ + 'EnvBarDisplay' => 'Boolean', + ]; + + /** + * Add Checkbox field for override value to new Environment Indicator tab + * in CMS Settings. + * + * @param FieldList $fields + * @return void + * @throws BadMethodCallException + */ + public function updateCMSFields(FieldList $fields) + { + $fields->addFieldsToTab('Root.EnvironmentIndicator', [ + CheckboxField::create( + 'EnvBarDisplay', + 'Display the environment indicator bar?' + )->setDescription('Check to show the environment indicator bar on' + . ' all pages being viewed in this environment.'), + ]); + } +} diff --git a/tests/EnvBarExtensionTest.yml b/tests/EnvBarExtensionTest.yml index b00e17d..37ab931 100644 --- a/tests/EnvBarExtensionTest.yml +++ b/tests/EnvBarExtensionTest.yml @@ -3,6 +3,7 @@ SilverStripe\SiteConfig\SiteConfig: Title: Test site CanViewType: Anyone CanEditType: LoggedInUsers + EnvBarDisplay: true SilverStripe\Security\Group: admins: diff --git a/tests/EnvBarSiteConfigExtensionTest.php b/tests/EnvBarSiteConfigExtensionTest.php new file mode 100644 index 0000000..9df5239 --- /dev/null +++ b/tests/EnvBarSiteConfigExtensionTest.php @@ -0,0 +1,98 @@ +objFromFixture( + 'SilverStripe\CMS\Model\SiteTree', + 'default' + ); + $url = $page->URLSegment . '?stage=Stage'; + + $user = $this->objFromFixture( + 'SilverStripe\Security\Member', + 'admin' + ); + + $this->EnvBarVisible( + $url, + $user + ); + } + + /** + * Test EnvBar NOT present when display is disabled in CMS + */ + public function testDisplayDisabled() + { + $page = $this->objFromFixture( + 'SilverStripe\CMS\Model\SiteTree', + 'default' + ); + $url = $page->URLSegment . '?stage=Stage'; + + $user = $this->objFromFixture( + 'SilverStripe\Security\Member', + 'admin' + ); + + $this->EnvBarNotVisible( + $url, + $user + ); + } + + + /** + * Test EnvBar is present somewhere on page + */ + public function EnvBarVisible($url, $user) + { + if ($user) { + $this->logInAs($user); + } + SiteConfig::current_site_config()->setField('EnvBarDisplay', true); + $testPage = $this->get($url); + $this->assertContains( + 'page__envbar', + $testPage->getBody() + ); + if ($user) { + $this->logOut(); + } + } + + /** + * Test EnvBar is not injected into the page + */ + public function EnvBarNotVisible($url, $user) + { + if ($user) { + $this->logInAs($user); + } + SiteConfig::current_site_config()->setField('EnvBarDisplay', false); + $testPage = $this->get($url); + $this->assertNotContains( + 'page__envbar', + $testPage->getBody() + ); + if ($user) { + $this->logOut(); + } + } +}