View source
<?php
namespace Drupal\Tests\media_migration\Kernel\Migrate;
use Drupal\Core\Extension\ModuleInstallerInterface;
use Drupal\migrate\Plugin\Migration;
use Drupal\migrate\Plugin\MigrationPluginManagerInterface;
use Drupal\Tests\media_migration\Traits\MediaMigrationAssertionsForMediaSourceTrait;
use PHPUnit\Framework\AssertionFailedError;
class MediaMigrationTest 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 testMediaMigration(string $destination_token, string $reference_method, bool $classic_node_migration, array $expected_node1_embed_attributes, bool $preexisting_media_types) {
if ($preexisting_media_types) {
$this
->createStandardMediaTypes();
}
$this
->setEmbedTokenDestinationFilterPlugin($destination_token);
$this
->setEmbedMediaReferenceMethod($reference_method);
$this
->setClassicNodeMigration($classic_node_migration);
$this
->assertArticleBodyFieldMigrationProcesses($classic_node_migration ? 'd7_node:article' : 'd7_node_complete:article', [
[
'plugin' => 'get',
'source' => 'body',
],
[
'plugin' => 'media_wysiwyg_filter',
],
[
'plugin' => 'img_tag_to_embed',
],
]);
$manager = $this->container
->get('plugin.manager.migration');
assert($manager instanceof MigrationPluginManagerInterface);
$node_migration = $manager
->createInstance($classic_node_migration ? 'd7_node:article' : 'd7_node_complete:article');
assert($node_migration instanceof Migration);
$dependencies = array_map(function (array $dependencies) {
natsort($dependencies);
return array_values(array_unique($dependencies));
}, $node_migration
->getMigrationDependencies());
$this
->assertEquals([
'required' => [
'd7_file_entity:image:public',
'd7_file_entity:video:public',
'd7_file_entity:video:vimeo',
'd7_file_entity:video:youtube',
'd7_node_type',
'd7_user',
],
'optional' => [
'd7_comment_field_instance',
'd7_field_instance',
],
], $dependencies);
$this
->executeMediaMigrations($classic_node_migration);
$this
->assertArticleImageFieldsAllowedTypes();
$this
->assertArticleMediaFieldsAllowedTypes();
$this
->assertMedia1FieldValues();
$this
->assertMedia2FieldValues();
$this
->assertMedia3FieldValues();
$this
->assertMedia4FieldValues();
$this
->assertMedia5FieldValues();
$this
->assertMedia6FieldValues();
$this
->assertMedia7FieldValues();
$this
->assertMedia8FieldValues();
$this
->assertMedia9FieldValues();
$this
->assertMedia10FieldValues();
$this
->assertMedia11FieldValues();
$this
->assertMedia12FieldValues();
$this
->assertNode1FieldValues($expected_node1_embed_attributes);
$this
->assertFilterFormats();
}
public function providerTestMediaMigration() {
$default_attributes = [
'data-entity-type' => 'media',
'alt' => 'Different alternative text about blue.png in the test article',
'title' => 'Different title copy for blue.png in the test article',
'data-align' => 'center',
];
$test_cases = [
'Entity embed destination, ID reference method, classic node migration, preexisting media types' => [
'Destination filter' => 'entity_embed',
'Reference method' => 'id',
'Classic node migration' => TRUE,
'expected_node1_embed_html_attributes' => [
0 => [
'data-entity-id' => '1',
'data-embed-button' => 'media',
'data-entity-embed-display' => 'view_mode:media.wysiwyg',
] + $default_attributes,
1 => [
'data-entity-type' => 'media',
'data-entity-id' => '7',
'data-embed-button' => 'media',
'data-entity-embed-display' => 'view_mode:media.full',
'alt' => 'A yellow image',
'title' => 'This is a yellow image',
],
],
'Preexisting media types' => TRUE,
],
'Media embed destination, ID reference method, classic node migration, preexisting media types' => [
'Destination filter' => 'media_embed',
'Reference method' => 'id',
'Classic node migration' => TRUE,
'expected_node1_embed_html_attributes' => [
0 => [
'data-entity-uuid' => TRUE,
'data-view-mode' => 'wysiwyg',
] + $default_attributes,
1 => [
'data-entity-type' => 'media',
'data-entity-uuid' => TRUE,
'data-view-mode' => 'default',
'alt' => 'A yellow image',
'title' => 'This is a yellow image',
],
],
'Preexisting media types' => TRUE,
],
'Entity embed destination, ID reference method, complete node migration, preexisting media types' => [
'Destination filter' => 'entity_embed',
'Reference method' => 'id',
'Classic node migration' => FALSE,
'expected_node1_embed_html_attributes' => [
0 => [
'data-entity-id' => '1',
'data-embed-button' => 'media',
'data-entity-embed-display' => 'view_mode:media.wysiwyg',
] + $default_attributes,
1 => [
'data-entity-id' => '7',
'data-embed-button' => 'media',
'data-entity-embed-display' => 'view_mode:media.full',
'alt' => 'A yellow image',
'title' => 'This is a yellow image',
],
],
'Preexisting media types' => TRUE,
],
'Media embed destination, ID reference method, complete node migration, preexisting media types' => [
'Destination filter' => 'media_embed',
'Reference method' => 'id',
'Classic node migration' => FALSE,
'expected_node1_embed_html_attributes' => [
0 => [
'data-entity-uuid' => TRUE,
'data-view-mode' => 'wysiwyg',
] + $default_attributes,
1 => [
'data-entity-type' => 'media',
'data-entity-uuid' => TRUE,
'data-view-mode' => 'default',
'alt' => 'A yellow image',
'title' => 'This is a yellow image',
],
],
'Preexisting media types' => TRUE,
],
'Entity embed destination, UUID reference method, classic node migration, preexisting media types' => [
'Destination filter' => 'entity_embed',
'Reference method' => 'uuid',
'Classic node migration' => TRUE,
'expected_node1_embed_html_attributes' => [
0 => [
'data-entity-uuid' => TRUE,
'data-embed-button' => 'media',
'data-entity-embed-display' => 'view_mode:media.wysiwyg',
] + $default_attributes,
1 => [
'data-entity-type' => 'media',
'data-entity-uuid' => TRUE,
'data-entity-embed-display' => 'view_mode:media.full',
'data-embed-button' => 'media',
'alt' => 'A yellow image',
'title' => 'This is a yellow image',
],
],
'Preexisting media types' => TRUE,
],
'Media embed destination, UUID reference method, classic node migration, preexisting media types' => [
'Destination filter' => 'media_embed',
'Reference method' => 'uuid',
'Classic node migration' => TRUE,
'expected_node1_embed_html_attributes' => [
0 => [
'data-entity-uuid' => TRUE,
'data-view-mode' => 'wysiwyg',
] + $default_attributes,
1 => [
'data-entity-type' => 'media',
'data-entity-uuid' => TRUE,
'data-view-mode' => 'default',
'alt' => 'A yellow image',
'title' => 'This is a yellow image',
],
],
'Preexisting media types' => TRUE,
],
'Entity embed destination, UUID reference method, complete node migration, preexisting media types' => [
'Destination filter' => 'entity_embed',
'Reference method' => 'uuid',
'Classic node migration' => FALSE,
'expected_node1_embed_html_attributes' => [
0 => [
'data-entity-uuid' => TRUE,
'data-embed-button' => 'media',
'data-entity-embed-display' => 'view_mode:media.wysiwyg',
] + $default_attributes,
1 => [
'data-entity-type' => 'media',
'data-entity-uuid' => TRUE,
'data-entity-embed-display' => 'view_mode:media.full',
'data-embed-button' => 'media',
'alt' => 'A yellow image',
'title' => 'This is a yellow image',
],
],
'Preexisting media types' => TRUE,
],
'Media embed destination, UUID reference method, complete node migration, preexisting media types' => [
'Destination filter' => 'media_embed',
'Reference method' => 'uuid',
'Classic node migration' => FALSE,
'expected_node1_embed_html_attributes' => [
0 => [
'data-entity-uuid' => TRUE,
'data-view-mode' => 'wysiwyg',
] + $default_attributes,
1 => [
'data-entity-type' => 'media',
'data-entity-uuid' => TRUE,
'data-view-mode' => 'default',
'alt' => 'A yellow image',
'title' => 'This is a yellow image',
],
],
'Preexisting media types' => TRUE,
],
];
$test_cases_without_media_types = [];
foreach ($test_cases as $test_case_label => $test_case) {
$without_media_label = preg_replace('/preexisting media types$/', 'no media types', $test_case_label);
$test_case['Preexisting media types'] = FALSE;
$test_cases_without_media_types[$without_media_label] = $test_case;
}
$test_cases += $test_cases_without_media_types;
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;
}
public function testChangeTracking() {
$module_installer = \Drupal::service('module_installer');
assert($module_installer instanceof ModuleInstallerInterface);
$module_installer
->install([
'media_migration_test_change_tracking',
]);
$this
->testMediaMigration('media_embed', 'uuid', FALSE, [
[
'data-entity-type' => 'media',
'data-entity-uuid' => TRUE,
'data-view-mode' => 'wysiwyg',
'alt' => 'Different alternative text about blue.png in the test article',
'title' => 'Different title copy for blue.png in the test article',
'data-align' => 'center',
],
[
'data-entity-type' => 'media',
'data-entity-uuid' => TRUE,
'data-view-mode' => 'default',
'alt' => 'A yellow image',
'title' => 'This is a yellow image',
],
], FALSE);
$this->sourceDatabase
->update('file_managed')
->condition('fid', 1)
->fields([
'filename' => 'Blue PNG changed',
])
->execute();
try {
$this
->executeMediaMigrations();
} catch (\Exception $exception) {
throw new AssertionFailedError($exception
->getMessage() . "\n" . $exception
->getLine() . "\n" . $exception
->getTraceAsString());
}
$this
->assertMedia1FieldValues('Blue PNG changed');
}
}