You are here

class AssetMetadataHelperTest in Media: Acquia DAM 8

Tests integration of the AssetMetadataHelper service.

@group media_acquiadam

Hierarchy

Expanded class hierarchy of AssetMetadataHelperTest

File

tests/src/Unit/AssetMetadataHelperTest.php, line 17

Namespace

Drupal\Tests\media_acquiadam\Unit
View source
class AssetMetadataHelperTest extends UnitTestCase {
  use AcquiadamAssetDataTrait;

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

  /**
   * Media: Acquia DAM asset metadata helper service.
   *
   * @var \Drupal\media_acquiadam\Service\AssetMetadataHelper
   */
  protected $assetMetadataHelper;

  /**
   * Validate that we can set the available XMP metadata fields.
   */
  public function testSetMetadataXmpFields() {
    $attributes = $this->assetMetadataHelper
      ->getMetadataAttributeLabels();
    $this
      ->assertArrayNotHasKey('xmp_example_field', $attributes);
    $this->assetMetadataHelper
      ->setMetadataXmpFields([
      'xmp_caption' => [
        'name' => 'Caption/Abstract',
        'label' => 'Caption/Description',
        'type' => 'textarea',
      ],
      'xmp_byline' => [
        'name' => 'By-line',
        'label' => 'Photographer',
        'type' => 'text',
      ],
    ]);
    $attributes = $this->assetMetadataHelper
      ->getMetadataAttributeLabels();
    $this
      ->assertArrayHasKey('xmp_byline', $attributes);
    $this
      ->assertArrayHasKey('xmp_caption', $attributes);
  }

  /**
   * Test that all basic attributes are set and XMP metadata gets set.
   */
  public function testGetMetadataAttributeLabels() {
    $attributes = $this->assetMetadataHelper
      ->getMetadataAttributeLabels();
    $this
      ->assertArrayHasKey('colorspace', $attributes);
    $this
      ->assertArrayHasKey('datecaptured', $attributes);
    $this
      ->assertArrayHasKey('datecreated', $attributes);
    $this
      ->assertArrayHasKey('datemodified', $attributes);
    $this
      ->assertArrayHasKey('description', $attributes);
    $this
      ->assertArrayHasKey('file', $attributes);
    $this
      ->assertArrayHasKey('filename', $attributes);
    $this
      ->assertArrayHasKey('filesize', $attributes);
    $this
      ->assertArrayHasKey('filetype', $attributes);
    $this
      ->assertArrayHasKey('folderID', $attributes);
    $this
      ->assertArrayHasKey('height', $attributes);
    $this
      ->assertArrayHasKey('status', $attributes);
    $this
      ->assertArrayHasKey('type', $attributes);
    $this
      ->assertArrayHasKey('id', $attributes);
    $this
      ->assertArrayHasKey('version', $attributes);
    $this
      ->assertArrayHasKey('width', $attributes);
    $this
      ->assertArrayNotHasKey('missing_attribute', $attributes);
    $this
      ->assertArrayNotHasKey('xmp_missing_xmp_1', $attributes);
  }

  /**
   * Validate that we can retrieve complicated metadata from assets.
   */
  public function testGetMetadataFromAsset() {
    $this->assetMetadataHelper
      ->setMetadataXmpFields([
      'xmp_caption' => [
        'name' => 'Caption/Abstract',
        'label' => 'Caption/Description',
        'type' => 'textarea',
      ],
      'xmp_byline' => [
        'name' => 'By-line',
        'label' => 'Photographer',
        'type' => 'text',
      ],
    ]);

    // Check some regular properties.
    $this
      ->assertEquals(3455969, $this->assetMetadataHelper
      ->getMetadataFromAsset($this
      ->getAssetData(), 'id'));
    $this
      ->assertEquals(4, $this->assetMetadataHelper
      ->getMetadataFromAsset($this
      ->getAssetData(), 'version'));
    $this
      ->assertEquals('XAAAZZZZZ.jpg', $this->assetMetadataHelper
      ->getMetadataFromAsset($this
      ->getAssetData(), 'filename'));

    // Check special properties.
    $this
      ->assertEquals(90754, $this->assetMetadataHelper
      ->getMetadataFromAsset($this
      ->getAssetData(), 'folderID'));
    $this
      ->assertEquals('Image', $this->assetMetadataHelper
      ->getMetadataFromAsset($this
      ->getAssetData(), 'type'));
    $this
      ->assertNull($this->assetMetadataHelper
      ->getMetadataFromAsset($this
      ->getAssetData(), 'status'));

    // Check date properties.
    $this
      ->assertEquals('2017-03-22 18:34:43', $this->assetMetadataHelper
      ->getMetadataFromAsset($this
      ->getAssetData(), 'datecreated'));
    $this
      ->assertEquals('2017-03-22 18:36:33', $this->assetMetadataHelper
      ->getMetadataFromAsset($this
      ->getAssetData(), 'datemodified'));
    $this
      ->assertEquals('2013-03-19 14:16:49', $this->assetMetadataHelper
      ->getMetadataFromAsset($this
      ->getAssetData(), 'datecaptured'));
    $this
      ->assertEquals('2017-03-22T18:34:43', $this->assetMetadataHelper
      ->getMetadataFromAsset($this
      ->getAssetData(), 'datecreated_date'));
    $this
      ->assertEquals('2017-03-22T18:36:33', $this->assetMetadataHelper
      ->getMetadataFromAsset($this
      ->getAssetData(), 'datemodified_date'));
    $this
      ->assertEquals('2013-03-19T14:16:49', $this->assetMetadataHelper
      ->getMetadataFromAsset($this
      ->getAssetData(), 'datecaptured_date'));
    $this
      ->assertEquals(1490207683, $this->assetMetadataHelper
      ->getMetadataFromAsset($this
      ->getAssetData(), 'datecreated_unix'));
    $this
      ->assertEquals(1490207793, $this->assetMetadataHelper
      ->getMetadataFromAsset($this
      ->getAssetData(), 'datemodified_unix'));
    $this
      ->assertEquals(1363702609, $this->assetMetadataHelper
      ->getMetadataFromAsset($this
      ->getAssetData(), 'datecaptured_unix'));

    // Check XMP properties.
    $this
      ->assertEquals('XMP Byline', $this->assetMetadataHelper
      ->getMetadataFromAsset($this
      ->getAssetData(), 'xmp_byline'));
    $this
      ->assertEquals('XMP Caption', $this->assetMetadataHelper
      ->getMetadataFromAsset($this
      ->getAssetData(), 'xmp_caption'));
  }

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $date_formatter = $this
      ->getMockBuilder(DateFormatterInterface::class)
      ->disableOriginalConstructor()
      ->getMock();
    $date_formatter
      ->method('format')
      ->willReturnCallback(function ($timestamp, $type, $format) {
      if ('custom' == $type) {
        $dt = new \DateTime('now', new \DateTimeZone('UTC'));
        $dt
          ->setTimestamp($timestamp);
        return $dt
          ->format($format);
      }
      return FALSE;
    });
    $acquiadam_client = $this
      ->getMockBuilder(Acquiadam::class)
      ->disableOriginalConstructor()
      ->setMethods([
      'getActiveXmpFields',
    ])
      ->getMock();
    $acquiadam_client
      ->method('getActiveXmpFields')
      ->willReturn([]);
    $this->container = new ContainerBuilder();
    $this->container
      ->set('string_translation', $this
      ->getStringTranslationStub());
    $this->container
      ->set('date.formatter', $date_formatter);
    $this->container
      ->set('media_acquiadam.acquiadam', $acquiadam_client);
    \Drupal::setContainer($this->container);
    $this->assetMetadataHelper = AssetMetadataHelper::create($this->container);
  }

}

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.
AssetMetadataHelperTest::$assetMetadataHelper protected property Media: Acquia DAM asset metadata helper service.
AssetMetadataHelperTest::$container protected property Container builder helper.
AssetMetadataHelperTest::setUp protected function Overrides UnitTestCase::setUp
AssetMetadataHelperTest::testGetMetadataAttributeLabels public function Test that all basic attributes are set and XMP metadata gets set.
AssetMetadataHelperTest::testGetMetadataFromAsset public function Validate that we can retrieve complicated metadata from assets.
AssetMetadataHelperTest::testSetMetadataXmpFields public function Validate that we can set the available XMP metadata fields.
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.