Skip to content

Commit e6a9dee

Browse files
committed
Harden empty oEmbed provider configuration
1 parent 6a11078 commit e6a9dee

3 files changed

Lines changed: 59 additions & 4 deletions

File tree

‎src/Provider/ProviderConfig.php‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ public static function fromArray(array $data): self {
8989
if (($iframePlayer === null || $iframePlayer === '') && ($oEmbed === null || $oEmbed === '')) {
9090
throw ProviderConfigException::missingField('iframe-player or oembed', $data);
9191
}
92+
$iframePlayer = $iframePlayer === '' ? null : $iframePlayer;
93+
$oEmbed = $oEmbed === '' ? null : $oEmbed;
9294
if ($iframePlayer !== null && !is_string($iframePlayer)) {
9395
throw ProviderConfigException::invalidField('iframe-player', $data['iframe-player'], 'string', $data);
9496
}
@@ -187,10 +189,10 @@ public function toArray(): array {
187189
if ($this->slug !== null) {
188190
$array['slug'] = $this->slug;
189191
}
190-
if ($this->iframePlayer !== null) {
192+
if ($this->hasIframeSupport()) {
191193
$array['iframe-player'] = $this->iframePlayer;
192194
}
193-
if ($this->oEmbed !== null) {
195+
if ($this->hasOEmbedSupport()) {
194196
$array['oembed'] = $this->oEmbed;
195197
}
196198
if ($this->imageSrc !== null) {
@@ -234,7 +236,7 @@ public function getUrlMatchPatterns(): array {
234236
* @return bool
235237
*/
236238
public function hasIframeSupport(): bool {
237-
return $this->iframePlayer !== null;
239+
return $this->iframePlayer !== null && $this->iframePlayer !== '';
238240
}
239241

240242
/**
@@ -243,7 +245,7 @@ public function hasIframeSupport(): bool {
243245
* @return bool
244246
*/
245247
public function hasOEmbedSupport(): bool {
246-
return $this->oEmbed !== null;
248+
return $this->oEmbed !== null && $this->oEmbed !== '';
247249
}
248250

249251
/**

‎tests/MediaEmbedTest.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,16 @@ public function testOEmbedHtmlSupportsCustomOEmbedOnlyProvider(): void {
604604
$this->assertSame($html, $MediaEmbed->oEmbedHtml($Object));
605605
}
606606

607+
public function testOEmbedHtmlReturnsNullWithoutHtml(): void {
608+
$httpClient = $this->createStub(HttpClientInterface::class);
609+
$httpClient->method('get')->willReturn('{"type":"link","version":"1.0"}');
610+
$MediaEmbed = new MediaEmbed(httpClient: $httpClient);
611+
$Object = $MediaEmbed->parseUrl('https://vimeo.com/99585787');
612+
$this->assertInstanceOf(MediaObject::class, $Object);
613+
614+
$this->assertNull($MediaEmbed->oEmbedHtml($Object));
615+
}
616+
607617
public function testOEmbedReturnsNullWithoutRegisteredEndpoint(): void {
608618
$MediaEmbed = new MediaEmbed();
609619
$Object = $MediaEmbed->parseUrl('https://my.matterport.com/show/?m=Zh14WDtkjdC');

‎tests/Provider/ProviderConfigTest.php‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,35 @@ public function testFromArrayAllowsOEmbedOnlyProvider(): void {
146146
$this->assertSame('https://rich.example.com/oembed', $config->toArray()['oembed']);
147147
}
148148

149+
public function testFromArrayNormalizesUnusedEmptyRendererToNull(): void {
150+
$config = ProviderConfig::fromArray([
151+
'name' => 'IframeProvider',
152+
'website' => 'https://iframe.example.com',
153+
'url-match' => 'iframe\\.example\\.com/([0-9]+)',
154+
'embed-width' => 640,
155+
'embed-height' => 360,
156+
'iframe-player' => 'https://iframe.example.com/embed/$2',
157+
'oembed' => '',
158+
]);
159+
160+
$this->assertNull($config->oEmbed);
161+
$this->assertArrayNotHasKey('oembed', $config->toArray());
162+
}
163+
164+
public function testFromArrayRejectsNonStringOEmbedEndpoint(): void {
165+
$this->expectException(ProviderConfigException::class);
166+
$this->expectExceptionMessage('Provider configuration field "oembed" has invalid value. Expected string.');
167+
168+
ProviderConfig::fromArray([
169+
'name' => 'InvalidProvider',
170+
'website' => 'https://invalid.example.com',
171+
'url-match' => 'invalid\\.example\\.com/([0-9]+)',
172+
'embed-width' => 640,
173+
'embed-height' => 360,
174+
'oembed' => [],
175+
]);
176+
}
177+
149178
public function testFromArrayPreservesExtraProviderMetadata(): void {
150179
$data = [
151180
'name' => 'ExtraProvider',
@@ -347,6 +376,20 @@ public function testHasOEmbedSupport(): void {
347376
);
348377

349378
$this->assertTrue($config->hasOEmbedSupport());
379+
380+
$empty = new ProviderConfig(
381+
name: 'Empty',
382+
website: 'https://empty.example.com',
383+
urlMatch: 'pattern',
384+
embedWidth: 640,
385+
embedHeight: 360,
386+
iframePlayer: '',
387+
oEmbed: '',
388+
);
389+
$this->assertFalse($empty->hasIframeSupport());
390+
$this->assertFalse($empty->hasOEmbedSupport());
391+
$this->assertArrayNotHasKey('iframe-player', $empty->toArray());
392+
$this->assertArrayNotHasKey('oembed', $empty->toArray());
350393
}
351394

352395
public function testHasThumbnailSupport(): void {

0 commit comments

Comments
 (0)