class MediaEntityHelperTest in Media: Acquia DAM 8
Testing of the Media Entity helper class.
@group media_acquiadam
Hierarchy
- class \Drupal\Tests\UnitTestCase extends \PHPUnit\Framework\TestCase uses PhpunitCompatibilityTrait
- class \Drupal\Tests\media_acquiadam\Unit\MediaEntityHelperTest uses AcquiadamAssetDataTrait
Expanded class hierarchy of MediaEntityHelperTest
File
- tests/
src/ Unit/ MediaEntityHelperTest.php, line 24
Namespace
Drupal\Tests\media_acquiadam\UnitView source
class MediaEntityHelperTest extends UnitTestCase {
use AcquiadamAssetDataTrait, AcquiadamMockedMediaEntityTrait;
/**
* Container builder helper.
*
* @var \Drupal\Core\DependencyInjection\ContainerBuilder
*/
protected $container;
/**
* Validate we can get file from a media entity.
*/
public function testGetFile() {
$helper = $this
->getNewMediaEntityHelper();
$this
->assertInstanceOf(FileInterface::class, $helper
->getFile());
// Change the source field to a failing one to simulate a missing file.
$media = $this
->getMockedMediaEntity($this
->getAssetData()->id, 'phpunit_test_fail');
$this
->assertFalse($this
->getNewMediaEntityHelper($media)
->getFile());
}
/**
* Validate we can properly load an existing file.
*/
public function testGetExistingFile() {
$this
->assertInstanceOf(FileInterface::class, $this
->getNewMediaEntityHelper()
->getExistingFile());
$media = $this
->getMockBuilder(MediaInterface::class)
->disableOriginalConstructor()
->getMock();
/** @var \Drupal\media\MediaInterface|\PHPUnit\Framework\MockObject\MockObject $media */
$this
->assertFalse($this
->getNewMediaEntityHelper($media)
->getExistingFile());
}
/**
* Validate we can get an existing fild ID if one is present.
*/
public function testGetExistingFileId() {
$this
->assertEquals($this
->getMockedFileEntity()
->id(), $this
->getNewMediaEntityHelper()
->getExistingFileId());
/** @var \Drupal\media\MediaInterface|\PHPUnit\Framework\MockObject\MockObject $media */
$media = $this
->getMockBuilder(MediaInterface::class)
->disableOriginalConstructor()
->getMock();
$this
->assertFalse($this
->getNewMediaEntityHelper($media)
->getExistingFileId());
}
/**
* Validates we can get the file field assets data is stored in.
*/
public function testGetAssetFileField() {
$this
->assertEquals('phpunit_file_field', $this
->getNewMediaEntityHelper()
->getAssetFileField());
/** @var \Drupal\media\MediaInterface|\PHPUnit\Framework\MockObject\MockObject $media */
$media = $this
->getMockBuilder(MediaInterface::class)
->disableOriginalConstructor()
->getMock();
$this
->assertFalse($this
->getNewMediaEntityHelper($media)
->getAssetFileField());
}
/**
* Validates that we can get the asset.
*/
public function testGetAsset() {
$this
->assertInstanceOf(Asset::class, $this
->getNewMediaEntityHelper()
->getAsset());
// Change our source field to simulate a missing asset.
$media = $this
->getMockedMediaEntity($this
->getAssetData()->id, 'phpunit_test_fail');
$this
->assertFalse($this
->getNewMediaEntityHelper($media)
->getAsset());
}
/**
* Validates that we can get the asset ID.
*/
public function testGetAssetId() {
$this
->assertEquals($this
->getAssetData()->id, $this
->getNewMediaEntityHelper()
->getAssetId());
// Change our source field to simulate a missing asset.
$media = $this
->getMockedMediaEntity($this
->getAssetData()->id, 'phpunit_test_fail');
$this
->assertFalse($this
->getNewMediaEntityHelper($media)
->getAssetId());
}
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->container = new ContainerBuilder();
$this
->setMockedDrupalServices($this->container);
$this
->setMockedAcquiaDamServices($this->container);
\Drupal::setContainer($this->container);
}
/**
* Gets an instance of the MediaEntityHelper class.
*
* @param \Drupal\media\MediaInterface|null $media
* The media entity to wrap.
*
* @return \Drupal\media_acquiadam\MediaEntityHelper
* An instance of the MediaEntityHelper class.
*/
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'));
}
/**
* Sets Drupal mocked services into a container.
*
* @param \Drupal\Core\DependencyInjection\ContainerBuilder $container
* The container to set mocks into.
*/
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);
}
/**
* Sets Media: Acquia DAM mocked services into a container.
*
* @param \Drupal\Core\DependencyInjection\ContainerBuilder $container
* The container to set mocks into.
*/
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);
}
}
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. | |
MediaEntityHelperTest:: |
protected | property | Container builder helper. | |
MediaEntityHelperTest:: |
protected | function | Gets an instance of the MediaEntityHelper class. | |
MediaEntityHelperTest:: |
protected | function | Sets Media: Acquia DAM mocked services into a container. | |
MediaEntityHelperTest:: |
protected | function | Sets Drupal mocked services into a container. | |
MediaEntityHelperTest:: |
protected | function |
Overrides UnitTestCase:: |
|
MediaEntityHelperTest:: |
public | function | Validates that we can get the asset. | |
MediaEntityHelperTest:: |
public | function | Validates we can get the file field assets data is stored in. | |
MediaEntityHelperTest:: |
public | function | Validates that we can get the asset ID. | |
MediaEntityHelperTest:: |
public | function | Validate we can properly load an existing file. | |
MediaEntityHelperTest:: |
public | function | Validate we can get an existing fild ID if one is present. | |
MediaEntityHelperTest:: |
public | function | Validate we can get file from a media entity. | |
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. |