Skip to content

Commit

Permalink
✨ feat: make announcement selectable via extension on SiteConfig
Browse files Browse the repository at this point in the history
- Add silverstripe/standards
- Update test suite
  • Loading branch information
Cambis committed Jul 15, 2024
1 parent af29a60 commit 54196ca
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 28 deletions.
6 changes: 6 additions & 0 deletions _config/browser-update.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
Name: browser-update
---
SilverStripe\SiteConfig\SiteConfig:
extensions:
- DNADesign\BrowserUpdate\Extension\SiteConfigExtension
1 change: 1 addition & 0 deletions build/composer-php-81.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"php": "^8.1",
"silverstripe/admin": "^1.0 || ^2.0",
"silverstripe/framework": "^4.0 || ^5.0",
"silverstripe/siteconfig": "^4.0 || ^5.0",
"symbiote/silverstripe-gridfieldextensions": "^3.0 || ^4.0"
},
"minimum-stability": "dev",
Expand Down
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"php": "^8.3",
"silverstripe/admin": "^1.0 || ^2.0",
"silverstripe/framework": "^4.0 || ^5.0",
"silverstripe/siteconfig": "^4.0 || ^5.0",
"symbiote/silverstripe-gridfieldextensions": "^3.0 || ^4.0"
},
"require-dev": {
Expand All @@ -22,6 +23,7 @@
"phpstan/phpstan-strict-rules": "^1.5",
"phpunit/phpunit": "^9.5",
"rector/rector": "^1.0",
"silverstripe/standards": "^1.0",
"slevomat/coding-standard": "^8.14",
"symplify/easy-coding-standard": "^12.0"
},
Expand Down
5 changes: 5 additions & 0 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,8 @@ parameters:
message: "#^Return type \\(bool\\|int\\) of method DNADesign\\\\BrowserUpdate\\\\Model\\\\Announcement\\:\\:canView\\(\\) should be covariant with return type \\(bool\\) of method SilverStripe\\\\ORM\\\\DataObject\\:\\:canView\\(\\)$#"
count: 1
path: src/Model/Announcement.php

-
message: "#^Access to an undefined property DNADesign\\\\BrowserUpdate\\\\Extension\\\\SiteConfigExtension\\:\\:\\$BrowserAnnouncementID\\.$#"
count: 1
path: tests/php/View/TemplateProviderTest.php
38 changes: 38 additions & 0 deletions src/Extension/SiteConfigExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace DNADesign\BrowserUpdate\Extension;

use DNADesign\BrowserUpdate\Model\Announcement;
use SilverStripe\Core\Extension;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FieldList;
use SilverStripe\ORM\DataObject;
use SilverStripe\SiteConfig\SiteConfig;

/**
* @extends Extension<SiteConfig>
*
* @method Announcement BrowserAnnouncement()
* @property int $BrowserAnnouncementID
*
* @see \DNADesign\BrowserUpdate\Tests\Extension\SiteConfigExtensionTest
*/
class SiteConfigExtension extends Extension
{
/**
* @var array<class-string<DataObject>>
*/
private static array $has_one = [
'BrowserAnnouncement' => Announcement::class,
];

protected function updateCMSFields(FieldList $fields): void
{
$fields->addFieldsToTab('Root.BrowserUpdate', [
DropdownField::create('BrowserAnnouncementID', 'Active browser announcement')
->setDescription('This announcement will display on all pages unless dismissed by the user.')
->setSource(Announcement::get()->map())
->setEmptyString('None'),
]);
}
}
9 changes: 6 additions & 3 deletions src/View/TemplateProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace DNADesign\BrowserUpdate\View;

use DNADesign\BrowserUpdate\Model\Announcement;
use DNADesign\BrowserUpdate\Extension\SiteConfigExtension;
use JsonException;
use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\View\TemplateGlobalProvider;
use function json_encode;
use function sprintf;
Expand All @@ -29,9 +30,11 @@ public static function get_template_global_variables(): array

public static function getBrowserUpdate(): string
{
$announcement = Announcement::get()->sort('Sort')->first();
/** @var SiteConfigExtension $siteConfig */
$siteConfig = SiteConfig::current_site_config();
$announcement = $siteConfig->BrowserAnnouncement();

if (!$announcement instanceof Announcement) {
if (!$announcement->exists()) {
return '';
}

Expand Down
33 changes: 33 additions & 0 deletions tests/php/Extension/SiteConfigExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace DNADesign\BrowserUpdate\Tests\Extension;

use SilverStripe\Dev\SapphireTest;
use SilverStripe\Forms\DropdownField;
use SilverStripe\Forms\FormField;
use SilverStripe\SiteConfig\SiteConfig;

final class SiteConfigExtensionTest extends SapphireTest
{
/**
* @return array<array{string, class-string<FormField>}>
*/
public function updateCMSFieldsProvider(): array
{
return [
['BrowserAnnouncementID', DropdownField::class],
];
}

/**
* @dataProvider updateCMSFieldsProvider
* @param class-string<FormField> $fieldClass
*/
public function testUpdateCMSFields(string $fieldName, string $fieldClass): void
{
$fields = SiteConfig::current_site_config()->getCMSFields();
$field = $fields->dataFieldByName($fieldName);

$this->assertInstanceOf($fieldClass, $field);
}
}
35 changes: 10 additions & 25 deletions tests/php/View/TemplateProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@

namespace DNADesign\BrowserUpdate\Tests\View;

use DNADesign\BrowserUpdate\Extension\SiteConfigExtension;
use DNADesign\BrowserUpdate\Model\Announcement;
use DNADesign\BrowserUpdate\View\TemplateProvider;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\SiteConfig\SiteConfig;
use SilverStripe\View\SSViewer;

final class TemplateProviderTest extends SapphireTest
{
protected function setUp(): void
{
parent::setUp();

Announcement::get()->removeAll();
}

/**
* @return array<array<string>>
*/
Expand Down Expand Up @@ -52,28 +47,18 @@ public function testBrowserUpdate(): void
$announcement = Announcement::create();
$announcement->write();

/** @var SiteConfigExtension $siteConfig */
$siteConfig = SiteConfig::current_site_config();
$siteConfig->BrowserAnnouncementID = $announcement->ID;

$template = SSViewer::execute_string(
'{$BrowserUpdate}',
''
);

$this->assertStringContainsString(<<<'HTML'
<script>
var $buoop = JSON.parse('{"reminder":"24","reminderClosed":"150","test":"0","newwindow":"1","url":null,"noclose":"0","no_permanent_hide":"0","api":"2024.07","text":{"msg":"Your web browser ({brow_name}) is out of date.","msgmore":"Update your browser for more security, speed and the best experience on this site.","bupdate":"Update browser","bignore":"Ignore","remind":"You will be reminded in {days} days.","bnever":"Never show again"}}');
function $buo_f() {
var e = document.createElement("script");
e.src = "//browser-update.org/update.min.js";
document.body.appendChild(e);
};
try {
document.addEventListener("DOMContentLoaded", $buo_f, false);
}
catch(e) {
window.attachEvent("onload", $buo_f);
}
</script>
HTML, $template);
$this->assertStringContainsString(
'{"reminder":"24","reminderClosed":"150","test":"0","newwindow":"1","url":null,"noclose":"0","no_permanent_hide":"0","api":"2024.07","text":{"msg":"Your web browser ({brow_name}) is out of date.","msgmore":"Update your browser for more security, speed and the best experience on this site.","bupdate":"Update browser","bignore":"Ignore","remind":"You will be reminded in {days} days.","bnever":"Never show again"}}',
$template,
);
}
}

0 comments on commit 54196ca

Please sign in to comment.