View source
<?php
namespace Drupal\Tests\media_acquiadam\Kernel;
use Drupal\Core\File\FileSystem;
use Drupal\Core\File\FileSystemInterface;
use Drupal\media_acquiadam\Acquiadam;
use Drupal\media_acquiadam\Plugin\media\Source\AcquiadamAsset;
class AcquiadamMediaTest extends AcquiadamKernelTestBase {
protected $asset;
protected $media;
protected $sourceReflectionClass;
protected $acquiadamReflectionClass;
protected function setUp() {
parent::setUp();
$this->sourceReflectionClass = new \ReflectionClass(AcquiadamAsset::class);
$this->acquiadamReflectionClass = new \ReflectionClass(Acquiadam::class);
$this->asset = $this
->getAssetData();
$dir_path = 'public://acquiadam/';
$contents = 'test';
$this->container
->get('file_system')
->prepareDirectory($dir_path, FileSystemInterface::CREATE_DIRECTORY);
file_save_data($contents, $dir_path . $this->asset->filename);
$this->testClient
->addAsset($this->asset);
$this->media = $this
->createMedia($this->asset->id);
}
public function testFieldMappings() {
$media_description = $this->media
->get('field_acquiadam_asset_descrip')
->getString();
$media_file_uri = $this
->getAssetFileEntity($this->media)
->getFileUri();
$expected_asset_uri = $this
->getAssetUri($this->asset, $this->media);
$this
->assertEqual($this->media
->label(), $this->asset->filename, 'Media name mapped to asset filename as expected.');
$this
->assertEqual($media_description, $this->asset->description, 'Media description mapped to asset description as expected.');
$this
->assertEqual($media_file_uri, $expected_asset_uri, 'Media file URI mapped as expected.');
}
public function testNewVersionUpdate() {
$asset_data = $this->container
->get('media_acquiadam.asset_data');
$this
->saveNewVersion();
$file = $this
->getAssetFileEntity($this->media);
$file_uri = $file
->getFileUri();
$expected_asset_uri = $this
->getAssetUri($this->asset, $this->media);
$new_version = $asset_data
->get($this->asset->id, 'version');
$this
->assertEqual($this->media
->label(), $this->asset->filename, 'Media name updated as expected.');
$this
->assertEqual($file_uri, $expected_asset_uri, 'Media asset file updated as expected.');
$this
->assertEqual($file
->label(), $this->asset->filename, 'File entity label updated as expected.');
$this
->assertEqual($this->asset->version, $new_version, 'Asset version updated as expected.');
}
public function testFailedFileSave() {
$asset_data = $this->container
->get('media_acquiadam.asset_data');
$asset_file_helper = $this->container
->get('media_acquiadam.asset_file.helper');
$file_system = $this->container
->get('file_system');
$old_version = $asset_data
->get($this->asset->id, 'version');
$directory = $asset_file_helper
->getDestinationFromEntity($this->media, 'field_acquiadam_asset_file');
$file_system
->chmod($directory, 00);
$this
->saveNewVersion();
$new_version = $asset_data
->get($this->asset->id, 'version');
$this
->assertEqual($old_version, $new_version, 'Asset version unchanged as expected.');
$file_system
->chmod($directory, FileSystem::CHMOD_DIRECTORY);
$this
->reSaveMedia();
$new_version = $asset_data
->get($this->asset->id, 'version');
$this
->assertNotEqual($old_version, $new_version, 'New version different from old version.');
$this
->assertEqual($this->asset->version, $new_version, 'Asset version updated as expected.');
}
public function testAssetFileIsCorrect() {
$expected_fid = $this
->getAssetFileEntity($this->media)
->id();
$this
->createNewMediaRevision();
$other_asset = $this
->getAssetData([
'id' => 3455970,
'filename' => 'other_file.jpg',
]);
$this->testClient
->addAsset($other_asset);
$other_media = $this
->createMedia($other_asset->id);
$other_file = $this
->getAssetFileEntity($other_media);
$this
->saveNewVersion();
$actual_fid = $this
->getAssetFileEntity($this->media)
->id();
$this
->assertEqual($actual_fid, $expected_fid, 'First media entity still has reference to the expected file.');
$this
->assertEqual($other_file
->getFileUri(), $this
->getAssetUri($other_asset, $other_media), 'Second media entity still has the expected URI.');
}
protected function createNewMediaRevision() {
$this->media
->setName('test');
$this->media
->setNewRevision(TRUE);
$this->media
->save();
}
protected function saveNewVersion() {
$this->asset = $this
->generateNewVersion($this->asset);
$this->testClient
->addAsset($this->asset);
$this
->reSaveMedia();
}
protected function reSaveMedia() {
$this
->clearCurrentAssetFromSource($this->media
->getSource());
$this->media
->setNewRevision(TRUE);
$this->media
->save();
}
protected function clearCurrentAssetFromSource(AcquiadamAsset $source) {
$current_asset_property = $this->sourceReflectionClass
->getProperty('currentAsset');
$current_asset_property
->setAccessible(TRUE);
$current_asset_property
->setValue($source, NULL);
$cached_assets_property = $this->acquiadamReflectionClass
->getProperty('cachedAssets');
$cached_assets_property
->setAccessible(TRUE);
$cached_assets_property
->setValue([]);
}
}