Skip to content

Commit

Permalink
Merge pull request #885 from mobilisedonline/2.3
Browse files Browse the repository at this point in the history
Reindex  array to remove gaps after applying allowed_domains list
  • Loading branch information
colinodell authored Jun 7, 2022
2 parents d4301f3 + effe03b commit e42dbdc
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 29 deletions.
4 changes: 2 additions & 2 deletions src/Extension/Embed/DomainFilteringAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public function __construct(EmbedAdapterInterface $decorated, array $allowedDoma
*/
public function updateEmbeds(array $embeds): void
{
$this->decorated->updateEmbeds(\array_filter($embeds, function (Embed $embed): bool {
$this->decorated->updateEmbeds(\array_values(\array_filter($embeds, function (Embed $embed): bool {
return \preg_match($this->regex, $embed->getUrl()) === 1;
}));
})));
}

/**
Expand Down
44 changes: 17 additions & 27 deletions tests/unit/Extension/Embed/DomainFilteringAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,28 @@ final class DomainFilteringAdapterTest extends TestCase
{
public function testUpdateEmbeds(): void
{
$inner = new class implements EmbedAdapterInterface {
/**
* {@inheritDoc}
*/
public function updateEmbeds(array $embeds): void
{
foreach ($embeds as $embed) {
$embed->setEmbedCode('some html');
}
}
};

$adapter = new DomainFilteringAdapter($inner, ['example.com', 'foo.bar.com']);

$embeds = [
new Embed('example.com'),
new Embed('foo.example.com'),
new Embed('http://foo.bar.com'),
new Embed('https://foo.bar.com/baz'),
new Embed('https://bar.com'),
new Embed('google.com'),
$embed1 = new Embed('example.com'),
$embed2 = new Embed('foo.example.com'),
new Embed('www.bar.com'),
new Embed('badexample.com'),
$embed3 = new Embed('http://foo.bar.com'),
$embed4 = new Embed('https://foo.bar.com/baz'),
new Embed('https://bar.com'),
];

$adapter->updateEmbeds($embeds);
$inner = $this->createMock(EmbedAdapterInterface::class);
$inner->expects($this->once())->method('updateEmbeds')->with([
// It is critical that the filtered values have their keys re-indexed
// See https://github.com/thephpleague/commonmark/issues/884
0 => $embed1,
1 => $embed2,
2 => $embed3,
3 => $embed4,
]);

$this->assertSame('some html', $embeds[0]->getEmbedCode());
$this->assertSame('some html', $embeds[1]->getEmbedCode());
$this->assertSame('some html', $embeds[2]->getEmbedCode());
$this->assertSame('some html', $embeds[3]->getEmbedCode());
$this->assertNull($embeds[4]->getEmbedCode());
$this->assertNull($embeds[5]->getEmbedCode());
$this->assertNull($embeds[6]->getEmbedCode());
$adapter = new DomainFilteringAdapter($inner, ['example.com', 'foo.bar.com']);
$adapter->updateEmbeds($embeds);
}
}

0 comments on commit e42dbdc

Please sign in to comment.