Skip to content

Commit

Permalink
MediaEmbed: added AbstractConfigurableHostHelper::getHosts()
Browse files Browse the repository at this point in the history
  • Loading branch information
JoshyPHP committed Nov 3, 2024
1 parent 9a15fb8 commit 0ad2008
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 8 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,6 @@
"test": "phpunit --exclude-group ''"
},
"extra": {
"version": "2.18.1-dev"
"version": "2.19.0-dev"
}
}
1 change: 1 addition & 0 deletions docs/testdox.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
Expand All @@ -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]);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 0ad2008

Please sign in to comment.