View source
<?php
namespace Drupal\Tests\media_migration\Kernel\Migrate;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleInstallerInterface;
use Drupal\Tests\media_migration\Traits\MediaMigrationAssertionsForMediaSourceTrait;
class MediaMigrationFormatterWidgetTest extends MediaMigrationTestBase {
use MediaMigrationAssertionsForMediaSourceTrait;
public static $modules = [
'comment',
'datetime',
'datetime_range',
'editor',
'embed',
'entity_embed',
'field',
'file',
'filter',
'image',
'link',
'media',
'media_migration',
'media_migration_test_oembed',
'menu_ui',
'migrate',
'migrate_drupal',
'migrate_plus',
'node',
'options',
'smart_sql_idmap',
'system',
'taxonomy',
'telephone',
'text',
'user',
];
public function testWidgetsAndFormattersWithoutMediaLibrary(bool $classic_node_migration) {
$this
->createStandardMediaTypes();
$this
->setClassicNodeMigration($classic_node_migration);
$this
->executeMediaMigrations($classic_node_migration);
$media_fields = [
'field_image' => [
'form' => 'image_image',
'view' => 'entity_reference_entity_view',
],
'field_media' => [
'form' => 'file_generic',
'view' => 'entity_reference_entity_view',
],
];
$entity_type_manager = $this->container
->get('entity_type.manager');
assert($entity_type_manager instanceof EntityTypeManagerInterface);
foreach ($media_fields as $field_name => $expected_config) {
$entity_form_display = $entity_type_manager
->getStorage('entity_form_display')
->load(implode('.', [
'node',
'article',
'default',
]));
$entity_form_display_array = $entity_form_display
->toArray();
$content = $entity_form_display_array['content'];
$this
->assertTrue(array_key_exists($field_name, $content));
$this
->assertEquals($expected_config['form'], $content[$field_name]['type']);
}
foreach ($media_fields as $field_name => $expected_config) {
$entity_form_display = $entity_type_manager
->getStorage('entity_view_display')
->load(implode('.', [
'node',
'article',
'default',
]));
$entity_form_display_array = $entity_form_display
->toArray();
$content = $entity_form_display_array['content'];
$this
->assertTrue(array_key_exists($field_name, $content));
$this
->assertEquals($expected_config['view'], $content[$field_name]['type']);
}
$this
->assertMediaTypeDisplayModes();
}
public function testWidgetsAndFormattersWithMediaLibrary(bool $classic_node_migration) {
$this
->createStandardMediaTypes();
$this
->setClassicNodeMigration($classic_node_migration);
$installer = $this->container
->get('module_installer');
assert($installer instanceof ModuleInstallerInterface);
$installer
->install([
'media_library',
]);
$this
->executeMediaMigrations($classic_node_migration);
$media_fields = [
'field_image' => [
'form' => 'media_library_widget',
'view' => 'entity_reference_entity_view',
],
'field_media' => [
'form' => 'media_library_widget',
'view' => 'entity_reference_entity_view',
],
];
$entity_type_manager = $this->container
->get('entity_type.manager');
assert($entity_type_manager instanceof EntityTypeManagerInterface);
foreach ($media_fields as $field_name => $expected_config) {
$entity_form_display = $entity_type_manager
->getStorage('entity_form_display')
->load(implode('.', [
'node',
'article',
'default',
]));
$entity_form_display_array = $entity_form_display
->toArray();
$content = $entity_form_display_array['content'];
$this
->assertTrue(array_key_exists($field_name, $content));
$this
->assertEquals($expected_config['form'], $content[$field_name]['type']);
}
foreach ($media_fields as $field_name => $expected_config) {
$entity_form_display = $entity_type_manager
->getStorage('entity_view_display')
->load(implode('.', [
'node',
'article',
'default',
]));
$entity_form_display_array = $entity_form_display
->toArray();
$content = $entity_form_display_array['content'];
$this
->assertTrue(array_key_exists($field_name, $content));
$this
->assertEquals($expected_config['view'], $content[$field_name]['type']);
}
$this
->assertMediaTypeDisplayModes();
}
public function assertMediaTypeDisplayModes() {
$this
->assertMediaAudioDisplayModes();
$this
->assertMediaDocumentDisplayModes();
$this
->assertMediaImageDisplayModes(TRUE);
$this
->assertMediaRemoteVideoDisplayModes();
$this
->assertMediaVideoDisplayModes();
}
public function providerTestCases() {
$test_cases = [
'Classic node migration' => [
'Classic node migration' => TRUE,
],
'Complete node migration' => [
'Classic node migration' => FALSE,
],
];
if (version_compare(\Drupal::VERSION, '8.9', '<')) {
$test_cases = array_filter($test_cases, function ($test_case) {
return $test_case['Classic node migration'];
});
}
return $test_cases;
}
}