View source
<?php
declare (strict_types=1);
namespace Drupal\Tests\entity_share_client\Functional;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\node\NodeInterface;
use Drupal\Tests\TestFileCreationTrait;
class MediaEntityReferenceTest extends EntityShareClientFunctionalTestBase {
use TestFileCreationTrait;
protected static $entityTypeId = 'node';
protected static $entityBundleId = 'es_test';
protected static $entityLangcode = 'en';
protected static $filesData = [
'file_audio' => [
'filename' => 'sample.mp3',
'filemime' => 'audio/mpeg',
'uri' => 'public://sample.mp3',
'file_content_callback' => 'getMediaEntityReferenceTestFiles',
],
'file_document' => [
'filename' => 'sample.pdf',
'filemime' => 'application/pdf',
'uri' => 'public://sample.pdf',
'file_content_callback' => 'getMediaEntityReferenceTestFiles',
],
'file_image' => [
'filename' => 'image-test.jpg',
'filemime' => 'image/jpeg',
'uri' => 'public://image-test.jpg',
],
'file_video' => [
'filename' => 'sample.mp4',
'filemime' => 'video/mp4',
'uri' => 'public://sample.mp4',
'file_content_callback' => 'getMediaEntityReferenceTestFiles',
],
];
protected $filesSize = [];
protected function setUp() : void {
parent::setUp();
$this
->getTestFiles('image');
if (isset(static::$filesData['file_image'])) {
$this->filesSize['file_image'] = filesize(static::$filesData['file_image']['uri']);
}
$this
->postSetupFixture();
}
protected function postSetupFixture() {
$this
->prepareContent();
$this
->populateRequestService();
foreach (static::$filesData as $file_data) {
$this->fileSystem
->delete($file_data['uri']);
}
$this
->deleteContent();
}
protected function getImportConfigProcessorSettings() {
$processors = parent::getImportConfigProcessorSettings();
$processors['physical_file'] = [
'rename' => FALSE,
'weights' => [
'process_entity' => 0,
],
];
return $processors;
}
protected function getEntitiesDataArray() {
return [
'file' => [
'en' => $this
->preparePhysicalFilesAndFileEntitiesData(),
],
'media' => [
'en' => [
'es_test_audio' => $this
->getCompleteMediaInfos([
'field_es_test_audio_file' => [
'value_callback' => function () {
return [
[
'target_id' => $this
->getEntityId('file', 'file_audio'),
],
];
},
'checker_callback' => 'getFilteredStructureValues',
],
'bundle' => [
'value' => 'es_test_audio',
'checker_callback' => 'getTargetId',
],
]),
'es_test_document' => $this
->getCompleteMediaInfos([
'field_es_test_document' => [
'value_callback' => function () {
return [
[
'target_id' => $this
->getEntityId('file', 'file_document'),
'display' => 1,
],
];
},
'checker_callback' => 'getFilteredStructureValues',
],
'bundle' => [
'value' => 'es_test_document',
'checker_callback' => 'getTargetId',
],
]),
'es_test_image' => $this
->getCompleteMediaInfos([
'field_es_test_image' => [
'value_callback' => function () {
return [
[
'target_id' => $this
->getEntityId('file', 'file_image'),
'alt' => 'Alt text',
],
];
},
'checker_callback' => 'getFilteredStructureValues',
],
'bundle' => [
'value' => 'es_test_image',
'checker_callback' => 'getTargetId',
],
]),
'es_test_remote_video' => $this
->getCompleteMediaInfos([
'field_es_test_oembed_video' => [
'value' => 'https://www.youtube.com/watch?v=Apqd4ff0NRI',
'checker_callback' => 'getValue',
],
'bundle' => [
'value' => 'es_test_remote_video',
'checker_callback' => 'getTargetId',
],
]),
'es_test_video' => $this
->getCompleteMediaInfos([
'field_es_test_video_file' => [
'value_callback' => function () {
return [
[
'target_id' => $this
->getEntityId('file', 'file_video'),
],
];
},
'checker_callback' => 'getFilteredStructureValues',
],
'bundle' => [
'value' => 'es_test_video',
'checker_callback' => 'getTargetId',
],
]),
],
],
'node' => [
'en' => [
'es_test_media' => $this
->getCompleteNodeInfos([
'field_es_test_media' => [
'value_callback' => function () {
return [
[
'target_id' => $this
->getEntityId('media', 'es_test_audio'),
],
[
'target_id' => $this
->getEntityId('media', 'es_test_document'),
],
[
'target_id' => $this
->getEntityId('media', 'es_test_image'),
],
[
'target_id' => $this
->getEntityId('media', 'es_test_remote_video'),
],
[
'target_id' => $this
->getEntityId('media', 'es_test_video'),
],
];
},
'checker_callback' => 'getExpectedMediaReferenceValue',
],
'status' => [
'value' => NodeInterface::PUBLISHED,
'checker_callback' => 'getValue',
],
]),
],
],
];
}
public function testBasicPull() {
$this
->commonBasicPull();
}
protected function getExpectedMediaReferenceValue(ContentEntityInterface $entity, string $field_name) {
return [
[
'target_id' => $this
->getEntityId('media', 'es_test_audio'),
],
[
'target_id' => $this
->getEntityId('media', 'es_test_document'),
],
[
'target_id' => $this
->getEntityId('media', 'es_test_image'),
],
[
'target_id' => $this
->getEntityId('media', 'es_test_remote_video'),
],
[
'target_id' => $this
->getEntityId('media', 'es_test_video'),
],
];
}
}