Skip to content

Commit

Permalink
BUG: List types can now be disabled via configuration (existing confi…
Browse files Browse the repository at this point in the history
…g, missing feature).
  • Loading branch information
mfendeksilverstripe committed May 26, 2023
1 parent 677ae80 commit 9744bd8
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Type/Registry.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public function list(): array
$typeDefinitions = self::config()->get('types');

foreach ($typeDefinitions as $key => $def) {
// This link type is disabled, so we can skip it
if (!array_key_exists('enabled', $def) || !$def['enabled']) {
continue;
}

$types[$key] = $this->definitionToType($def);
}

Expand Down
122 changes: 122 additions & 0 deletions tests/php/Models/LinkTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@

use ReflectionException;
use SilverStripe\CMS\Model\SiteTree;
use SilverStripe\Config\Collections\MutableConfigCollectionInterface;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\LinkField\Models\EmailLink;
use SilverStripe\LinkField\Models\ExternalLink;
use SilverStripe\LinkField\Models\FileLink;
use SilverStripe\LinkField\Models\Link;
use SilverStripe\LinkField\Models\PhoneLink;
use SilverStripe\LinkField\Models\SiteTreeLink;
use SilverStripe\LinkField\Type\Registry;
use SilverStripe\LinkField\Type\Type;
use SilverStripe\ORM\DataObject;
use SilverStripe\ORM\ValidationException;

Expand Down Expand Up @@ -82,4 +86,122 @@ public function linkTypeProvider(): array
[Link::class, true],
];
}

/**
* @param array $types
* @param array $expected
* @return void
* @dataProvider linkTypeEnabledProvider
*/
public function testLinkTypeEnabled(array $types, array $expected): void
{
Config::withConfig(function (MutableConfigCollectionInterface $config) use ($types, $expected): void {
$config->set(Registry::class, 'types', $types);

$enabledTypes = Registry::singleton()->list();
$enabledTypes = array_map(static function (Type $type): string {
return $type->LinkTypeTile();
}, $enabledTypes);
$enabledTypes = array_values($enabledTypes);
sort($enabledTypes, SORT_STRING);

$this->assertSame($expected, $enabledTypes, 'We expect specific enabled link types');
});
}

public function linkTypeEnabledProvider(): array
{
return [
'all types enabled' => [
[
'cms' => [
'classname' => SiteTreeLink::class,
'enabled' => true,
],
'external' => [
'classname' => ExternalLink::class,
'enabled' => true,
],
'file' => [
'classname' => FileLink::class,
'enabled' => true,
],
'email' => [
'classname' => EmailLink::class,
'enabled' => true,
],
'phone' => [
'classname' => PhoneLink::class,
'enabled' => true,
],
],
[
'Email Link',
'External Link',
'File Link',
'Phone Link',
'Site Tree Link',
],
],
'file type disabled' => [
[
'cms' => [
'classname' => SiteTreeLink::class,
'enabled' => true,
],
'external' => [
'classname' => ExternalLink::class,
'enabled' => true,
],
'file' => [
'classname' => FileLink::class,
'enabled' => false,
],
'email' => [
'classname' => EmailLink::class,
'enabled' => true,
],
'phone' => [
'classname' => PhoneLink::class,
'enabled' => true,
],
],
[
'Email Link',
'External Link',
'Phone Link',
'Site Tree Link',
],
],
'phone and email types disabled' => [
[
'cms' => [
'classname' => SiteTreeLink::class,
'enabled' => true,
],
'external' => [
'classname' => ExternalLink::class,
'enabled' => true,
],
'file' => [
'classname' => FileLink::class,
'enabled' => true,
],
'email' => [
'classname' => EmailLink::class,
'enabled' => false,
],
'phone' => [
'classname' => PhoneLink::class,
'enabled' => false,
],
],
[
'External Link',
'File Link',
'Site Tree Link',
],
],
];
}
}

0 comments on commit 9744bd8

Please sign in to comment.