diff --git a/core/modules/media/src/OEmbed/Resource.php b/core/modules/media/src/OEmbed/Resource.php index 190a013eb08cb852dd650379cd43e656cb685e08..ac3e7510cd5af3401e04ad6f907d58cc2d7d617c 100644 --- a/core/modules/media/src/OEmbed/Resource.php +++ b/core/modules/media/src/OEmbed/Resource.php @@ -441,7 +441,7 @@ public function getThumbnailHeight() { * * @return int|null * The width of the resource in pixels, or NULL if the resource has no - * dimensions + * width. */ public function getWidth() { return $this->width; @@ -452,7 +452,7 @@ public function getWidth() { * * @return int|null * The height of the resource in pixels, or NULL if the resource has no - * dimensions. + * height. */ public function getHeight() { return $this->height; @@ -510,25 +510,20 @@ protected function setThumbnailDimensions($width, $height) { /** * Sets the dimensions. * - * @param int $width + * @param int|null $width * The width of the resource. - * @param int $height + * @param int|null $height * The height of the resource. * * @throws \InvalidArgumentException * If either $width or $height are not numbers greater than zero. */ protected function setDimensions($width, $height) { - $width = (int) $width; - $height = (int) $height; - - if ($width > 0 && $height > 0) { - $this->width = $width; - $this->height = $height; - } - else { - throw new \InvalidArgumentException('The dimensions must be numbers greater than zero.'); + if ((isset($width) && $width <= 0) || (isset($height) && $height <= 0)) { + throw new \InvalidArgumentException('The dimensions must be NULL or numbers greater than zero.'); } + $this->width = isset($width) ? (int) $width : NULL; + $this->height = isset($height) ? (int) $height : NULL; } } diff --git a/core/modules/media/tests/fixtures/oembed/photo_flickr_no_dimensions.json b/core/modules/media/tests/fixtures/oembed/photo_flickr_no_dimensions.json new file mode 100644 index 0000000000000000000000000000000000000000..e7f6b41b206b75a6c50bb70792861dfba94e54c1 --- /dev/null +++ b/core/modules/media/tests/fixtures/oembed/photo_flickr_no_dimensions.json @@ -0,0 +1,10 @@ +{ + "type": "photo", + "title": "Druplicon FTW!", + "url": "internal:\/core\/misc\/druplicon.png", + "thumbnail_url": "internal:\/core\/misc\/druplicon.png", + "thumbnail_width": 88, + "thumbnail_height": 100, + "provider_name": "Flickr", + "version": "1.0" +} diff --git a/core/modules/media/tests/src/Functional/FieldFormatter/OEmbedFormatterTest.php b/core/modules/media/tests/src/Functional/FieldFormatter/OEmbedFormatterTest.php index 4fdbd51cd3ebb8e95c43ae482eb04e210155ee16..2fa7ea303b456636bf84dcedee18d9769801b7f6 100644 --- a/core/modules/media/tests/src/Functional/FieldFormatter/OEmbedFormatterTest.php +++ b/core/modules/media/tests/src/Functional/FieldFormatter/OEmbedFormatterTest.php @@ -119,6 +119,16 @@ public function providerRender() { ], ], ], + 'Flickr photo (no dimensions)' => [ + 'https://www.flickr.com/photos/amazeelabs/26497866357', + 'photo_flickr_no_dimensions.json', + [], + [ + 'img' => [ + 'src' => '/core/misc/druplicon.png', + ], + ], + ], ]; } diff --git a/core/modules/media/tests/src/Unit/ResourceTest.php b/core/modules/media/tests/src/Unit/ResourceTest.php new file mode 100644 index 0000000000000000000000000000000000000000..5043565cd3e01dbad4ac914ec3b030d0fdc53589 --- /dev/null +++ b/core/modules/media/tests/src/Unit/ResourceTest.php @@ -0,0 +1,88 @@ +<?php + +namespace Drupal\Tests\media\Unit; + +use Drupal\media\OEmbed\Resource; +use Drupal\Tests\UnitTestCase; + +/** + * @coversDefaultClass \Drupal\media\OEmbed\Resource + * @group media + */ +class ResourceTest extends UnitTestCase { + + /** + * Test cases for ::testSetDimensions. + */ + public function setDimensionsTestCases() { + return [ + 'Standard rich dimensions' => [ + 'rich', + 5, + 10, + ], + 'Negative width and height' => [ + 'rich', + -5, + -10, + 'The dimensions must be NULL or numbers greater than zero.', + ], + 'Zero width' => [ + 'rich', + 0, + 5, + 'The dimensions must be NULL or numbers greater than zero.', + ], + 'NULL width' => [ + 'rich', + NULL, + 10, + ], + 'NULL height' => [ + 'rich', + NULL, + 10, + ], + 'NULL width and height' => [ + 'rich', + NULL, + NULL, + ], + 'Cast numeric dimensions' => [ + 'rich', + "1", + "45", + NULL, + 1, + 45, + ], + 'Cast invalid zero value' => [ + 'rich', + "0", + 10, + 'The dimensions must be NULL or numbers greater than zero.', + ], + 'Cast negative value' => [ + 'rich', + "-10", + 10, + 'The dimensions must be NULL or numbers greater than zero.', + ], + ]; + } + + /** + * @covers ::setDimensions + * @dataProvider setDimensionsTestCases + */ + public function testSetDimensions($factory, $width, $height, $exception = NULL, $expected_width = NULL, $expected_height = NULL) { + if ($exception) { + $this->expectException(\InvalidArgumentException::class); + $this->expectExceptionMessage($exception); + } + $resource = Resource::$factory('foo', $width, $height); + $this->assertSame($expected_width ?: $width, $resource->getWidth()); + $this->assertSame($expected_height ?: $height, $resource->getHeight()); + } + +}