Unverified Commit 9e3592a7 authored by alexpott's avatar alexpott
Browse files

Issue #3071682 by Sam152, Roensby, phenaproxima, Wim Leers: The oembed...

Issue #3071682 by Sam152, Roensby, phenaproxima, Wim Leers: The oembed Resource value object should be more permissive for NULL dimensions

(cherry picked from commit f85f69c2)
parent 14afd1cb
......@@ -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;
}
}
{
"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 @@ 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',
],
],
],
];
}
......
<?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());
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment