You are here

class AssetImageHelperTest in Media: Acquia DAM 8

Tests integration of the AssetImageHelper service.

@group media_acquiadam

Hierarchy

Expanded class hierarchy of AssetImageHelperTest

File

tests/src/Unit/AssetImageHelperTest.php, line 20

Namespace

Drupal\Tests\media_acquiadam\Unit
View source
class AssetImageHelperTest extends UnitTestCase {
  use AcquiadamAssetDataTrait, AcquiadamConfigTrait;

  /**
   * Container builder helper.
   *
   * @var \Drupal\Core\DependencyInjection\ContainerBuilder
   */
  protected $container;

  /**
   * A mocked version of the AssetImageHelper service.
   *
   * @var \Drupal\media_acquiadam\Service\AssetImageHelper|\PHPUnit\Framework\MockObject\MockObject
   */
  protected $assetImageHelper;

  /**
   * Validate we fetch the correct thumbnail size when given varying sizes.
   */
  public function testGetThumbnailUrlBySize() {
    $asset = $this
      ->getAssetData();

    // Ensure that we get the smallest size when given something smaller than
    // set.
    $tn_url = $this->assetImageHelper
      ->getThumbnailUrlBySize($asset, 50);
    $this
      ->assertEquals('http://subdomain.webdamdb.com/s/100th_sm_0UerYozlI3.jpg', $tn_url);

    // Ensure we can get an exact size.
    $tn_url = $this->assetImageHelper
      ->getThumbnailUrlBySize($asset, 100);
    $this
      ->assertEquals('http://subdomain.webdamdb.com/s/100th_sm_0UerYozlI3.jpg', $tn_url);

    // Ensure we get the closest smallest if available.
    $tn_url = $this->assetImageHelper
      ->getThumbnailUrlBySize($asset, 120);
    $this
      ->assertEquals('http://subdomain.webdamdb.com/s/100th_sm_0UerYozlI3.jpg', $tn_url);

    // Ensure we get the closest smallest for larger sizes.
    $tn_url = $this->assetImageHelper
      ->getThumbnailUrlBySize($asset, 350);
    $this
      ->assertEquals('http://subdomain.webdamdb.com/s/310th_sm_0UerYozlI3.jpg', $tn_url);

    // Ensure we get the  biggest if nothing was available.
    $tn_url = $this->assetImageHelper
      ->getThumbnailUrlBySize($asset, 12000);
    $this
      ->assertEquals('http://subdomain.webdamdb.com/s/md_0UerYozlI3.jpg', $tn_url);

    // Ensure we get the biggest when nothing is specified.
    $tn_url = $this->assetImageHelper
      ->getThumbnailUrlBySize($asset);
    $this
      ->assertEquals('http://subdomain.webdamdb.com/s/md_0UerYozlI3.jpg', $tn_url);
  }

  /**
   * Validate that a fallback image can be found.
   */
  public function testGetFallbackThumbnail() {

    // First FALSE will trigger the set thumbnail method.
    // Second FALSE will trigger the file copy.
    // TRUE will trigger using the default.
    $this->assetImageHelper
      ->method('phpFileExists')
      ->with('public://webdam.png')
      ->willReturnOnConsecutiveCalls(FALSE, FALSE, TRUE);
    $this
      ->assertEquals('public://webdam.png_copy', $this->assetImageHelper
      ->getFallbackThumbnail(), 'File should be copied to new location');
    $this
      ->assertEquals('public://webdam.png', $this->assetImageHelper
      ->getFallbackThumbnail(), 'Existing file should be used');
  }

  /**
   * Validate we get a generic media icon when no image can be loaded.
   */
  public function testGetGenericMediaIcon() {
    $mimetype = [
      'discrete' => 'image',
      'sub' => 'jpg',
    ];

    // @todo Any way to reset the mock method so we can change willReturn?
    // We have to get a new mock class each test because it is (seemingly) not
    // possible to change the willReturn for a mocked method after it has been
    // set.
    $helper = $this
      ->getMockedAssetImageHelper();
    $helper
      ->method('phpFileExists')
      ->willReturn(TRUE);
    $this
      ->assertEquals('public://media-icons/image-jpg.png', $helper
      ->getGenericMediaIcon($mimetype));
    $helper = $this
      ->getMockedAssetImageHelper();
    $helper
      ->method('phpFileExists')
      ->willReturnOnConsecutiveCalls(FALSE, TRUE);
    $this
      ->assertEquals('public://media-icons/jpg.png', $helper
      ->getGenericMediaIcon($mimetype));
    $helper = $this
      ->getMockedAssetImageHelper();
    $helper
      ->method('phpFileExists')
      ->willReturnOnConsecutiveCalls(FALSE, FALSE, TRUE);
    $this
      ->assertEquals('public://media-icons/generic.png', $helper
      ->getGenericMediaIcon($mimetype));
    $helper = $this
      ->getMockedAssetImageHelper();
    $helper
      ->method('phpFileExists')
      ->willReturn(FALSE);
    $this
      ->assertFalse($helper
      ->getGenericMediaIcon($mimetype));
  }

  /**
   * Validate that we can get proper mime types based on a file extension.
   */
  public function testGetMimeTypeFromFileType() {
    $this
      ->assertArrayEquals([
      'discrete' => 'image',
      'sub' => 'jpg',
    ], $this->assetImageHelper
      ->getMimeTypeFromFileType('jpg'));
    $this
      ->assertArrayEquals([
      'discrete' => 'video',
      'sub' => 'quicktime',
    ], $this->assetImageHelper
      ->getMimeTypeFromFileType('mov'));
    $this
      ->assertArrayEquals([
      'discrete' => 'application',
      'sub' => 'pdf',
    ], $this->assetImageHelper
      ->getMimeTypeFromFileType('pdf'));
    $this
      ->assertFalse($this->assetImageHelper
      ->getMimeTypeFromFileType('abc123'));
  }

  /**
   * Gets a mocked version of the AssetImageHelper class.
   *
   * This is used to provide some implemented methods that would normally be
   * an issue to test.
   *
   * @return \Drupal\media_acquiadam\Service\AssetImageHelper|\PHPUnit\Framework\MockObject\MockObject
   *   A mocked AssetImageHelper object.
   */
  protected function getMockedAssetImageHelper() {
    $helper = $this
      ->getMockBuilder(AssetImageHelper::class)
      ->setConstructorArgs([
      $this->container
        ->get('config.factory'),
      $this->container
        ->get('file_system'),
      $this->container
        ->get('http_client'),
      $this->container
        ->get('file.mime_type.guesser'),
      $this->container
        ->get('image.factory'),
    ])
      ->setMethods([
      'phpFileExists',
      'getAcquiaDamModulePath',
      'saveFallbackThumbnail',
    ])
      ->getMock();
    $helper
      ->method('getAcquiaDamModulePath')
      ->willReturn('modules/contrib/media_acquiadam');
    $helper
      ->method('saveFallbackThumbnail')
      ->willReturn(NULL);
    return $helper;
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $http_client = $this
      ->getMockBuilder(GuzzleClient::class)
      ->disableOriginalConstructor()
      ->getMock();
    $file_system = $this
      ->getMockBuilder(FileSystem::class)
      ->disableOriginalConstructor()
      ->setMethods([
      'copy',
    ])
      ->getMockForAbstractClass();
    $file_system
      ->method('copy')
      ->willReturnCallback(function ($source, $target) {
      return is_string($target) ? $target . '_copy' : $target . '_blah';
    });
    $mime_type_guesser = $this
      ->getMockBuilder(MimeTypeGuesserInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $mime_type_guesser
      ->method('guess')
      ->willReturnCallback(function ($uri) {
      $map = [
        'public://nothing.jpg' => 'image/jpg',
        'public://nothing.mov' => 'video/quicktime',
        'public://nothing.pdf' => 'application/pdf',
      ];
      return $map[$uri] ?? FALSE;
    });
    $image_factory = $this
      ->getMockBuilder(ImageFactory::class)
      ->disableOriginalConstructor()
      ->getMock();
    $this->container = new ContainerBuilder();
    $this->container
      ->set('http_client', $http_client);
    $this->container
      ->set('file_system', $file_system);
    $this->container
      ->set('file.mime_type.guesser', $mime_type_guesser);
    $this->container
      ->set('image.factory', $image_factory);
    $this->container
      ->set('config.factory', $this
      ->getConfigFactoryStub());
    \Drupal::setContainer($this->container);
    $this->assetImageHelper = $this
      ->getMockedAssetImageHelper();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AcquiadamAssetDataTrait::generateNewVersion protected function Create a new version of a given asset.
AcquiadamAssetDataTrait::getAssetData protected function Returns an Asset object for testing against.
AssetImageHelperTest::$assetImageHelper protected property A mocked version of the AssetImageHelper service.
AssetImageHelperTest::$container protected property Container builder helper.
AssetImageHelperTest::getMockedAssetImageHelper protected function Gets a mocked version of the AssetImageHelper class.
AssetImageHelperTest::setUp protected function Overrides UnitTestCase::setUp
AssetImageHelperTest::testGetFallbackThumbnail public function Validate that a fallback image can be found.
AssetImageHelperTest::testGetGenericMediaIcon public function Validate we get a generic media icon when no image can be loaded.
AssetImageHelperTest::testGetMimeTypeFromFileType public function Validate that we can get proper mime types based on a file extension.
AssetImageHelperTest::testGetThumbnailUrlBySize public function Validate we fetch the correct thumbnail size when given varying sizes.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.