Issue #3071682 by Sam152, Roensby, phenaproxima, Wim Leers: The oembed Resource value object should be more permissive for NULL dimensions
parent
8ac02b5cbd
commit
f85f69c255
|
@ -441,7 +441,7 @@ class Resource implements CacheableDependencyInterface {
|
|||
*
|
||||
* @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 @@ class Resource implements CacheableDependencyInterface {
|
|||
*
|
||||
* @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 @@ class Resource implements CacheableDependencyInterface {
|
|||
/**
|
||||
* 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
}
|
|
@ -119,6 +119,16 @@ class OEmbedFormatterTest extends MediaFunctionalTestBase {
|
|||
],
|
||||
],
|
||||
],
|
||||
'Flickr photo (no dimensions)' => [
|
||||
'https://www.flickr.com/photos/amazeelabs/26497866357',
|
||||
'photo_flickr_no_dimensions.json',
|
||||
[],
|
||||
[
|
||||
'img' => [
|
||||
'src' => '/core/misc/druplicon.png',
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue