View source
<?php
namespace Drupal\Tests\media_library\Functional;
use Drupal\Core\Entity\Entity\EntityFormDisplay;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
use Drupal\image\Entity\ImageStyle;
use Drupal\media\Plugin\media\Source\File;
use Drupal\media\Plugin\media\Source\Image;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
use Drupal\media\Entity\MediaType;
class MediaLibraryDisplayModeTest extends BrowserTestBase {
use MediaTypeCreationTrait;
protected static $modules = [
'field_ui',
'media',
'system',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$this
->drupalLogin($this
->drupalCreateUser([
'access media overview',
'administer media',
'administer media fields',
'administer media form display',
'administer media display',
'administer media types',
'view media',
]));
}
public function testDisplayModes() {
$this
->createMediaType('file', [
'id' => 'type_one',
]);
$this
->createMediaType('file', [
'id' => 'type_two',
'field_map' => [
'name' => File::METADATA_ATTRIBUTE_NAME,
],
]);
$this
->createMediaType('image', [
'id' => 'type_three',
]);
$this
->createMediaType('image', [
'id' => 'type_four',
'field_map' => [
'name' => Image::METADATA_ATTRIBUTE_NAME,
],
]);
$this
->assertNull(EntityFormDisplay::load('media.type_one.media_library'));
$this
->assertNull(EntityViewDisplay::load('media.type_one.media_library'));
$this
->assertNull(EntityFormDisplay::load('media.type_two.media_library'));
$this
->assertNull(EntityViewDisplay::load('media.type_two.media_library'));
$this
->assertNull(EntityFormDisplay::load('media.type_three.media_library'));
$this
->assertNull(EntityViewDisplay::load('media.type_three.media_library'));
$this
->assertNull(EntityFormDisplay::load('media.type_four.media_library'));
$this
->assertNull(EntityViewDisplay::load('media.type_four.media_library'));
$this->container
->get('module_installer')
->install([
'media_library',
]);
$this
->assertFormDisplay('type_one', TRUE, FALSE);
$this
->assertViewDisplay('type_one', 'medium');
$this
->assertFormDisplay('type_two', FALSE, FALSE);
$this
->assertViewDisplay('type_two', 'medium');
$this
->assertFormDisplay('type_three', TRUE, TRUE);
$this
->assertViewDisplay('type_three', 'medium');
$this
->assertFormDisplay('type_four', FALSE, TRUE);
$this
->assertViewDisplay('type_four', 'medium');
$type_five_id = 'type_five';
$edit = [
'label' => $type_five_id,
'id' => $type_five_id,
'source' => 'file',
];
$this
->drupalPostForm('admin/structure/media/add', $edit, 'Save');
$this
->drupalPostForm(NULL, [], t('Save'));
$this
->assertSession()
->pageTextContains("Media Library form and view displays have been created for the {$type_five_id} media type.");
$this
->assertFormDisplay($type_five_id, TRUE, FALSE);
$this
->assertViewDisplay($type_five_id, 'medium');
$type_six_id = 'type_six';
$edit = [
'label' => $type_six_id,
'id' => $type_six_id,
'source' => 'file',
];
$this
->drupalPostForm('admin/structure/media/add', $edit, 'Save');
$edit = [
'field_map[name]' => File::METADATA_ATTRIBUTE_NAME,
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertSession()
->pageTextContains("Media Library form and view displays have been created for the {$type_six_id} media type.");
$this
->assertFormDisplay($type_six_id, FALSE, FALSE);
$this
->assertViewDisplay($type_six_id, 'medium');
$type_seven_id = 'type_seven';
$edit = [
'label' => $type_seven_id,
'id' => $type_seven_id,
'source' => 'image',
];
$this
->drupalPostForm('admin/structure/media/add', $edit, 'Save');
$this
->drupalPostForm(NULL, [], t('Save'));
$this
->assertSession()
->pageTextContains("Media Library form and view displays have been created for the {$type_seven_id} media type.");
$this
->assertFormDisplay($type_seven_id, TRUE, TRUE);
$this
->assertViewDisplay($type_seven_id, 'medium');
$type_eight_id = 'type_eight';
$edit = [
'label' => $type_eight_id,
'id' => $type_eight_id,
'source' => 'image',
];
$this
->drupalPostForm('admin/structure/media/add', $edit, 'Save');
$edit = [
'field_map[name]' => Image::METADATA_ATTRIBUTE_NAME,
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertSession()
->pageTextContains("Media Library form and view displays have been created for the {$type_eight_id} media type.");
$this
->assertFormDisplay($type_eight_id, FALSE, TRUE);
$this
->assertViewDisplay($type_eight_id, 'medium');
$type_id = 'pinto_bean';
$edit = [
'label' => $type_id,
'id' => $type_id,
'source' => 'oembed:video',
];
$this
->drupalPostForm('admin/structure/media/add', $edit, 'Save');
$edit = [
'field_map[title]' => 'name',
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertSession()
->pageTextContains("Media Library form and view displays have been created for the {$type_id} media type.");
$this
->assertFormDisplay($type_id, FALSE, FALSE);
$this
->assertViewDisplay($type_id, 'medium');
EntityFormDisplay::load('media.type_one.media_library')
->delete();
EntityViewDisplay::load('media.type_one.media_library')
->delete();
$this
->drupalPostForm('admin/structure/media/manage/type_one', [], 'Save');
$this
->assertNull(EntityFormDisplay::load('media.type_one.media_library'));
$this
->assertNull(EntityViewDisplay::load('media.type_one.media_library'));
ImageStyle::load('medium')
->delete();
$type_nine_id = 'type_nine';
$edit = [
'label' => $type_nine_id,
'id' => $type_nine_id,
'source' => 'image',
];
$this
->drupalPostForm('admin/structure/media/add', $edit, 'Save');
$this
->drupalPostForm(NULL, [], t('Save'));
$this
->assertSession()
->pageTextContains("Media Library form and view displays have been created for the {$type_nine_id} media type.");
$this
->assertFormDisplay($type_nine_id, TRUE, TRUE);
$this
->assertViewDisplay($type_nine_id, 'media_library');
}
protected function assertFormDisplay($type_id, $has_name, $has_source_field) {
$components = [
'revision_log_message',
'langcode',
];
if ($has_name) {
$components[] = 'name';
}
if ($has_source_field) {
$type = MediaType::load($type_id);
$components[] = $type
->getSource()
->getSourceFieldDefinition($type)
->getName();
}
$form_display = EntityFormDisplay::load('media.' . $type_id . '.media_library');
$this
->assertInstanceOf(EntityFormDisplay::class, $form_display);
$actual_components = array_keys($form_display
->getComponents());
sort($components);
sort($actual_components);
$this
->assertSame($components, $actual_components);
}
protected function assertViewDisplay($type_id, $image_style) {
$view_display = EntityViewDisplay::load('media.' . $type_id . '.media_library');
$this
->assertInstanceOf(EntityViewDisplay::class, $view_display);
$this
->assertSame([
'thumbnail',
], array_keys($view_display
->getComponents()));
$thumbnail = $view_display
->getComponent('thumbnail');
$this
->assertIsArray($thumbnail);
$this
->assertSame($image_style, $thumbnail['settings']['image_style']);
}
}