From 9744bd8dc23ebe70129555b5b7573ac5295232b4 Mon Sep 17 00:00:00 2001 From: Mojmir Fendek Date: Fri, 26 May 2023 11:40:00 +1200 Subject: [PATCH] BUG: List types can now be disabled via configuration (existing config, missing feature). --- src/Type/Registry.php | 5 ++ tests/php/Models/LinkTest.php | 122 ++++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+) diff --git a/src/Type/Registry.php b/src/Type/Registry.php index 7486b5bd..18a12b44 100644 --- a/src/Type/Registry.php +++ b/src/Type/Registry.php @@ -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); } diff --git a/tests/php/Models/LinkTest.php b/tests/php/Models/LinkTest.php index c5a2d7d0..6a1a00dd 100644 --- a/tests/php/Models/LinkTest.php +++ b/tests/php/Models/LinkTest.php @@ -4,6 +4,8 @@ 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; @@ -11,6 +13,8 @@ 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; @@ -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', + ], + ], + ]; + } }