class AssetMetadataHelperTest in Media: Acquia DAM 8
Tests integration of the AssetMetadataHelper service.
@group media_acquiadam
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\media_acquiadam\Unit\AssetMetadataHelperTest uses AcquiadamAssetDataTrait
Expanded class hierarchy of AssetMetadataHelperTest
File
- tests/
src/ Unit/ AssetMetadataHelperTest.php, line 17
Namespace
Drupal\Tests\media_acquiadam\UnitView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
AcquiadamAssetDataTrait:: |
protected | function | Create a new version of a given asset. | |
AcquiadamAssetDataTrait:: |
protected | function | Returns an Asset object for testing against. | |
AssetMetadataHelperTest:: |
protected | property | Media: Acquia DAM asset metadata helper service. | |
AssetMetadataHelperTest:: |
protected | property | Container builder helper. | |
AssetMetadataHelperTest:: |
protected | function |
Overrides UnitTestCase:: |
|
AssetMetadataHelperTest:: |
public | function | Test that all basic attributes are set and XMP metadata gets set. | |
AssetMetadataHelperTest:: |
public | function | Validate that we can retrieve complicated metadata from assets. | |
AssetMetadataHelperTest:: |
public | function | Validate that we can set the available XMP metadata fields. | |
PhpunitCompatibilityTrait:: |
public | function | Returns a mock object for the specified class using the available method. | |
PhpunitCompatibilityTrait:: |
public | function | Compatibility layer for PHPUnit 6 to support PHPUnit 4 code. | |
UnitTestCase:: |
protected | property | The random generator. | |
UnitTestCase:: |
protected | property | The app root. | 1 |
UnitTestCase:: |
protected | function | Asserts if two arrays are equal by sorting them first. | |
UnitTestCase:: |
protected | function | Mocks a block with a block plugin. | 1 |
UnitTestCase:: |
protected | function | Returns a stub class resolver. | |
UnitTestCase:: |
public | function | Returns a stub config factory that behaves according to the passed array. | |
UnitTestCase:: |
public | function | Returns a stub config storage that returns the supplied configuration. | |
UnitTestCase:: |
protected | function | Sets up a container with a cache tags invalidator. | |
UnitTestCase:: |
protected | function | Gets the random generator for the utility methods. | |
UnitTestCase:: |
public | function | Returns a stub translation manager that just returns the passed string. | |
UnitTestCase:: |
public | function | Generates a unique random string containing letters and numbers. |