View source
<?php
namespace Drupal\Tests\media_acquiadam\Unit;
use cweagans\webdam\Entity\Asset;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\file\FileInterface;
use Drupal\media\MediaInterface;
use Drupal\media_acquiadam\Acquiadam;
use Drupal\media_acquiadam\AssetData;
use Drupal\media_acquiadam\MediaEntityHelper;
use Drupal\media_acquiadam\Service\AssetFileEntityHelper;
use Drupal\Tests\media_acquiadam\Traits\AcquiadamAssetDataTrait;
use Drupal\Tests\media_acquiadam\Traits\AcquiadamMockedMediaEntityTrait;
use Drupal\Tests\UnitTestCase;
class MediaEntityHelperTest extends UnitTestCase {
use AcquiadamAssetDataTrait, AcquiadamMockedMediaEntityTrait;
protected $container;
public function testGetFile() {
$helper = $this
->getNewMediaEntityHelper();
$this
->assertInstanceOf(FileInterface::class, $helper
->getFile());
$media = $this
->getMockedMediaEntity($this
->getAssetData()->id, 'phpunit_test_fail');
$this
->assertFalse($this
->getNewMediaEntityHelper($media)
->getFile());
}
public function testGetExistingFile() {
$this
->assertInstanceOf(FileInterface::class, $this
->getNewMediaEntityHelper()
->getExistingFile());
$media = $this
->getMockBuilder(MediaInterface::class)
->disableOriginalConstructor()
->getMock();
$this
->assertFalse($this
->getNewMediaEntityHelper($media)
->getExistingFile());
}
public function testGetExistingFileId() {
$this
->assertEquals($this
->getMockedFileEntity()
->id(), $this
->getNewMediaEntityHelper()
->getExistingFileId());
$media = $this
->getMockBuilder(MediaInterface::class)
->disableOriginalConstructor()
->getMock();
$this
->assertFalse($this
->getNewMediaEntityHelper($media)
->getExistingFileId());
}
public function testGetAssetFileField() {
$this
->assertEquals('phpunit_file_field', $this
->getNewMediaEntityHelper()
->getAssetFileField());
$media = $this
->getMockBuilder(MediaInterface::class)
->disableOriginalConstructor()
->getMock();
$this
->assertFalse($this
->getNewMediaEntityHelper($media)
->getAssetFileField());
}
public function testGetAsset() {
$this
->assertInstanceOf(Asset::class, $this
->getNewMediaEntityHelper()
->getAsset());
$media = $this
->getMockedMediaEntity($this
->getAssetData()->id, 'phpunit_test_fail');
$this
->assertFalse($this
->getNewMediaEntityHelper($media)
->getAsset());
}
public function testGetAssetId() {
$this
->assertEquals($this
->getAssetData()->id, $this
->getNewMediaEntityHelper()
->getAssetId());
$media = $this
->getMockedMediaEntity($this
->getAssetData()->id, 'phpunit_test_fail');
$this
->assertFalse($this
->getNewMediaEntityHelper($media)
->getAssetId());
}
protected function setUp() {
parent::setUp();
$this->container = new ContainerBuilder();
$this
->setMockedDrupalServices($this->container);
$this
->setMockedAcquiaDamServices($this->container);
\Drupal::setContainer($this->container);
}
protected function getNewMediaEntityHelper(MediaInterface $media = NULL) {
if (is_null($media)) {
$media = $this
->getMockedMediaEntity($this
->getAssetData()->id);
}
return new MediaEntityHelper($media, $this->container
->get('entity_type.manager'), $this->container
->get('media_acquiadam.asset_data'), $this->container
->get('media_acquiadam.acquiadam'), $this->container
->get('media_acquiadam.asset_file.helper'));
}
protected function setMockedDrupalServices(ContainerBuilder $container) {
$media_bundle = $this
->getMockBuilder(\stdClass::class)
->setMethods([
'getFieldMap',
])
->getMock();
$media_bundle
->method('getFieldMap')
->willReturn([
'file' => 'phpunit_file_field',
]);
$entity_storage = $this
->getMockBuilder(EntityStorageInterface::class)
->disableOriginalConstructor()
->getMock();
$entity_storage
->method('load')
->willReturnMap([
[
$this
->getMockedFileEntity()
->id(),
$this
->getMockedFileEntity(),
],
[
'acquiadam',
$media_bundle,
],
]);
$entity_storage
->method('loadByProperties')
->willReturnMap([
[
[
'uri' => 'private://assets/replaced/' . $this
->getAssetData()->filename,
],
[
$this
->getMockedFileEntity(),
],
],
[
[
'uri' => 'private://assets/replaced/Micro turbine 60.jpg',
],
[
$this
->getMockedFileEntity(),
],
],
]);
$entity_type_manager = $this
->getMockBuilder(EntityTypeManagerInterface::class)
->disableOriginalConstructor()
->getMock();
$entity_type_manager
->method('getStorage')
->willReturnMap([
[
'file',
$entity_storage,
],
[
'media_type',
$entity_storage,
],
]);
$container
->set('entity_type.manager', $entity_type_manager);
}
protected function setMockedAcquiaDamServices(ContainerBuilder $container) {
$asset_data = $this
->getMockBuilder(AssetData::class)
->disableOriginalConstructor()
->getMock();
$asset_data
->method('isUpdatedAsset')
->willReturnOnConsecutiveCalls(FALSE, TRUE);
$acquiadam = $this
->getMockBuilder(Acquiadam::class)
->disableOriginalConstructor()
->getMock();
$acquiadam
->method('getAsset')
->willReturnMap([
[
$this
->getAssetData()->id,
TRUE,
$this
->getAssetData(),
],
]);
$asset_file_helper = $this
->getMockBuilder(AssetFileEntityHelper::class)
->disableOriginalConstructor()
->getMock();
$asset_file_helper
->method('getDestinationFromEntity')
->willReturn('private://assets/replaced');
$asset_file_helper
->method('createNewFile')
->with($this
->anything(), 'private://assets/replaced')
->willReturn($this
->getMockedFileEntity());
$container
->set('media_acquiadam.asset_data', $asset_data);
$container
->set('media_acquiadam.acquiadam', $acquiadam);
$container
->set('media_acquiadam.asset_file.helper', $asset_file_helper);
}
}