diff --git a/composer.json b/composer.json index bb2fab23b..7b3197b3b 100644 --- a/composer.json +++ b/composer.json @@ -44,6 +44,6 @@ "test": "phpunit --exclude-group ''" }, "extra": { - "version": "2.18.1-dev" + "version": "2.19.0-dev" } } diff --git a/docs/testdox.txt b/docs/testdox.txt index 2c9b8fac3..436929e16 100644 --- a/docs/testdox.txt +++ b/docs/testdox.txt @@ -6761,6 +6761,7 @@ Mastodon Helper (s9e\TextFormatter\Tests\Plugins\MediaEmbed\Configurator\SiteHel [x] addHost() normalizes the host [x] addHost() adds the Mastodon media site if it's not enabled yet [x] setHosts() resets previously allowed hosts + [x] getHosts() returns a numerically-indexed array of allowed hosts Xen Foro Helper (s9e\TextFormatter\Tests\Plugins\MediaEmbed\Configurator\SiteHelpers\XenForoHelper) [x] addHost() adds the XenForo media site if it's not enabled yet diff --git a/src/Plugins/MediaEmbed/Configurator/SiteHelpers/AbstractConfigurableHostHelper.php b/src/Plugins/MediaEmbed/Configurator/SiteHelpers/AbstractConfigurableHostHelper.php index 64a9ee8a5..f64fd3a1c 100644 --- a/src/Plugins/MediaEmbed/Configurator/SiteHelpers/AbstractConfigurableHostHelper.php +++ b/src/Plugins/MediaEmbed/Configurator/SiteHelpers/AbstractConfigurableHostHelper.php @@ -7,7 +7,7 @@ */ namespace s9e\TextFormatter\Plugins\MediaEmbed\Configurator\SiteHelpers; -use function strtolower; +use function array_keys, sort, strtolower; abstract class AbstractConfigurableHostHelper extends AbstractSiteHelper { @@ -31,6 +31,18 @@ public function addHosts(array $hosts): void } } + public function getHosts(): array + { + $hosts = array_keys( + (array) ($this->configurator->registeredVars['MediaEmbed.hosts'] ?? []), + $this->getSiteId(), + true + ); + sort($hosts, SORT_STRING); + + return $hosts; + } + abstract protected function getSiteId(): string; public function setHosts(array $hosts): void @@ -42,12 +54,7 @@ public function setHosts(array $hosts): void } // Remove previously set hosts for this site - $unsetHosts = array_keys( - (array) $this->configurator->registeredVars['MediaEmbed.hosts'], - $siteId, - true - ); - foreach ($unsetHosts as $host) + foreach ($this->getHosts() as $host) { unset($this->configurator->registeredVars['MediaEmbed.hosts'][$host]); } diff --git a/tests/Plugins/MediaEmbed/Configurator/SiteHelpers/MastodonHelperTest.php b/tests/Plugins/MediaEmbed/Configurator/SiteHelpers/MastodonHelperTest.php index 1dc475d68..6fbea6ded 100644 --- a/tests/Plugins/MediaEmbed/Configurator/SiteHelpers/MastodonHelperTest.php +++ b/tests/Plugins/MediaEmbed/Configurator/SiteHelpers/MastodonHelperTest.php @@ -58,4 +58,18 @@ public function testSetHost() $this->assertXmlStringEqualsXmlString($expected, $actual); } + + /** + * @testdox getHosts() returns a numerically-indexed array of allowed hosts + */ + public function testGetHostRegexp() + { + $mastodonHelper = new MastodonHelper($this->configurator); + + $mastodonHelper->setHosts(['mastodon.social']); + $this->assertSame(['mastodon.social'], $mastodonHelper->getHosts()); + + $mastodonHelper->addHost('test.local'); + $this->assertSame(['mastodon.social', 'test.local'], $mastodonHelper->getHosts()); + } } \ No newline at end of file