Unverified Commit f85f69c2 authored by Alex Pott's avatar Alex Pott
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
parent 8ac02b5c
Loading
Loading
Loading
Loading
+8 −13
Original line number Diff line number Diff line
@@ -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;
  }

}
+10 −0
Original line number Diff line number Diff line
{
  "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"
}
+10 −0
Original line number Diff line number Diff line
@@ -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',
          ],
        ],
      ],
    ];
  }

+88 −0
Original line number Diff line number Diff line
<?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());
  }

}