Skip to content
Snippets Groups Projects
Commit b27795b1 authored by Yash Rode's avatar Yash Rode Committed by Zoltan Attila Horvath
Browse files

Issue #3277312 by yash.rode, huzooka: Migrate Media: oEmbed from D7 to Media migration in D9

parent e9a59f34
No related branches found
No related tags found
No related merge requests found
Showing
with 555 additions and 16 deletions
......@@ -60,33 +60,37 @@ class FileEntityDealerManager extends DefaultPluginManager implements FileEntity
*/
protected function getDefinitionsByTypeAndScheme(string $type, string $scheme) {
$definitions = $this->getDefinitions();
$list = [];
$strict_list = array_filter($this->getDefinitions(), function ($definition) use ($type, $scheme) {
return in_array($type, $definition['types'], TRUE) && in_array($scheme, $definition['schemes'], TRUE);
});
if (!empty($strict_list)) {
return $strict_list;
}
$only_type_list = array_filter($definitions, function ($definition) use ($type) {
return in_array($type, $definition['types'], TRUE) && empty($definition['schemes']);
});
if (!empty($only_type_list)) {
return $only_type_list;
}
$list = array_merge(
$list,
$strict_list,
);
$only_scheme_list = array_filter($definitions, function ($definition) use ($scheme) {
return empty($definition['types']) && in_array($scheme, $definition['schemes'], TRUE);
});
if (!empty($only_scheme_list)) {
return $only_scheme_list;
}
$list = array_merge(
$list,
$only_scheme_list,
);
$only_type_list = array_filter($definitions, function ($definition) use ($type) {
return in_array($type, $definition['types'], TRUE) && empty($definition['schemes']);
});
$list = array_merge(
$list,
$only_type_list,
);
if (array_key_exists('fallback', $definitions)) {
if (empty($list) && array_key_exists('fallback', $definitions)) {
return ['fallback' => $definitions['fallback']];
}
return [];
return $list;
}
/**
......
<?php
namespace Drupal\media_migration\Plugin\media_migration\file_entity;
use Drupal\Core\Database\Connection;
/**
* Oembed media migration plugin for Oembed video media entities.
*
* @FileEntityDealer(
* id = "oembed_video",
* types= {},
* schemes = {"oembed"},
* destination_media_source_plugin_id = "oembed:video"
* )
*/
class Oembed extends RemoteVideoBase {
/**
* {@inheritdoc}
*/
public function alterMediaEntityMigrationDefinition(array &$migration_definition, Connection $connection): void {
$migration_definition['process'][$this->getDestinationMediaSourceFieldName() . '/value'] = [
'plugin' => 'media_oembed_field_value',
'source' => 'uri',
];
}
}
......@@ -68,7 +68,7 @@ class MediaMigrateUuid extends ProcessPluginBase implements ContainerFactoryPlug
}
// No UUID was found – lets set the destination property to empty before
// throwing a skip process exception (this is only required for 9.2.x and
// below)
// below).
if (version_compare(\Drupal::VERSION, '9.3.0', 'lt')) {
$row->setEmptyDestinationProperty($destination_property);
}
......
<?php
namespace Drupal\media_migration\Plugin\migrate\process;
use Drupal\Component\Utility\Variable;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\ProcessPluginBase;
use Drupal\migrate\Row;
/**
* Processes and returns oembed media field values.
*
* @MigrateProcessPlugin(
* id = "media_oembed_field_value"
* )
*/
class MediaOembedFieldValue extends ProcessPluginBase {
/**
* {@inheritdoc}
*/
public function transform($value, MigrateExecutableInterface $migrate_executable, Row $row, $destination_property) {
if (!is_string($value)) {
throw new \UnexpectedValueException('The value to be transformed must be a string.');
}
if (!preg_match('/^oembed:\/\/(.+)$/', $value, $match)) {
throw new \UnexpectedValueException(
sprintf(
"The actual value doesn't seem to be an oembed URI: %s",
Variable::export($value)
)
);
}
return urldecode($match[1]);
}
}
......@@ -37,6 +37,11 @@ projects:
version: "2.26"
type: module
# Media Oembed
media_oembed:
version: "2.8"
type: module
# Media Vimeo
media_vimeo:
version: "2.1"
......
<?php
// phpcs:ignoreFile
/**
* @file
* A database agnostic dump for testing purposes.
*
* This file was generated by the Drupal 9.2.6 db-tools.php script.
*/
use Drupal\Core\Database\Database;
$connection = Database::getConnection();
include 'drupal7_oembed_partial' . DIRECTORY_SEPARATOR . 'field_config_instance.php';
include 'drupal7_oembed_partial' . DIRECTORY_SEPARATOR . 'field_data_field_media.php';
include 'drupal7_oembed_partial' . DIRECTORY_SEPARATOR . 'field_revision_field_media.php';
include 'drupal7_oembed_partial' . DIRECTORY_SEPARATOR . 'file_managed.php';
include 'drupal7_oembed_partial' . DIRECTORY_SEPARATOR . 'node.php';
include 'drupal7_oembed_partial' . DIRECTORY_SEPARATOR . 'node_revision.php';
include 'drupal7_oembed_partial' . DIRECTORY_SEPARATOR . 'node_type.php';
include 'drupal7_oembed_partial' . DIRECTORY_SEPARATOR . 'system.php';
<?php
// phpcs:ignoreFile
/**
* @file
* A database agnostic dump for testing purposes.
*
* This file was generated by the Drupal 9.2.6 db-tools.php script.
*/
use Drupal\Core\Database\Database;
$connection = Database::getConnection();
$connection->insert('field_config_instance')
->fields(array(
'id',
'field_id',
'field_name',
'entity_type',
'bundle',
'data',
'deleted',
))
->values(array(
'id' => '83863',
'field_id' => '7',
'field_name' => 'field_media',
'entity_type' => 'node',
'bundle' => 'oembed_content',
'data' => 'a:6:{s:5:"label";s:5:"media";s:6:"widget";a:5:{s:6:"weight";s:2:"-3";s:4:"type";s:13:"media_generic";s:6:"module";s:5:"media";s:6:"active";i:1;s:8:"settings";a:3:{s:15:"browser_plugins";a:4:{s:14:"media_internet";s:14:"media_internet";s:6:"upload";i:0;s:30:"media_default--media_browser_1";i:0;s:37:"media_default--media_browser_my_files";i:0;}s:13:"allowed_types";a:4:{s:5:"video";s:5:"video";s:5:"image";i:0;s:5:"audio";i:0;s:8:"document";i:0;}s:15:"allowed_schemes";a:4:{s:6:"oembed";s:6:"oembed";s:6:"public";s:6:"public";s:5:"vimeo";i:0;s:7:"youtube";i:0;}}}s:8:"settings";a:5:{s:14:"file_directory";s:31:"[date:custom:Y]-[date:custom:m]";s:15:"file_extensions";s:3:"txt";s:12:"max_filesize";s:0:"";s:17:"description_field";i:0;s:18:"user_register_form";b:0;}s:7:"display";a:1:{s:7:"default";a:5:{s:5:"label";s:5:"above";s:4:"type";s:12:"file_default";s:8:"settings";a:0:{}s:6:"module";s:4:"file";s:6:"weight";i:1;}}s:8:"required";i:0;s:11:"description";s:0:"";}',
'deleted' => '0',
))
->execute();
<?php
// phpcs:ignoreFile
/**
* @file
* A database agnostic dump for testing purposes.
*
* This file was generated by the Drupal 9.2.10 db-tools.php script.
*/
use Drupal\Core\Database\Database;
$connection = Database::getConnection();
$connection->insert('field_data_field_media')
->fields(array(
'entity_type' => 'node',
'bundle' => 'oembed_content',
'deleted' => '0',
'entity_id' => '83863',
'revision_id' => '83863',
'language' => 'und',
'delta' => '0',
'field_media_fid' => '83863',
'field_media_display' => '1',
'field_media_description' => '',
))
->execute();
<?php
// phpcs:ignoreFile
/**
* @file
* A database agnostic dump for testing purposes.
*
* This file was generated by the Drupal 9.2.10 db-tools.php script.
*/
use Drupal\Core\Database\Database;
$connection = Database::getConnection();
$connection->insert('field_revision_field_media')
->fields(array(
'entity_type' => 'node',
'bundle' => 'oembed_content',
'deleted' => '0',
'entity_id' => '83863',
'revision_id' => '83863',
'language' => 'und',
'delta' => '0',
'field_media_fid' => '83863',
'field_media_display' => '1',
'field_media_description' => '',
))
->execute();
<?php
// phpcs:ignoreFile
/**
* @file
* A database agnostic dump for testing purposes.
*
* This file was generated by the Drupal 9.2.10 db-tools.php script.
*/
use Drupal\Core\Database\Database;
$connection = Database::getConnection();
$connection->insert('file_managed')
->fields([
'fid' => '83863',
'uid' => '1',
'filename' => 'ACSF',
'uri' => 'oembed://https%3A//player.vimeo.com/video/268828727',
'filemime' => 'video/oembed',
'filesize' => '0',
'status' => '1',
'timestamp' => '1648447404',
'type' => 'remote_video',
'type' => 'video',
])
->execute();
<?php
// phpcs:ignoreFile
/**
* @file
* A database agnostic dump for testing purposes.
*
* This file was generated by the Drupal 9.2.6 db-tools.php script.
*/
use Drupal\Core\Database\Database;
$connection = Database::getConnection();
$connection->insert('node')
->fields([
'nid' => '83863',
'vid' => '83863',
'type' => 'oembed_content',
'language' => 'und',
'title' => 'Oembed example #1',
'uid' => '1',
'status' => '1',
'created' => '1648447404',
'changed' => '1648449960',
'comment' => '2',
'promote' => '1',
'sticky' => '0',
'tnid' => '0',
'translate' => '0',
])
->execute();
<?php
// phpcs:ignoreFile
/**
* @file
* A database agnostic dump for testing purposes.
*
* This file was generated by the Drupal 9.2.6 db-tools.php script.
*/
use Drupal\Core\Database\Database;
$connection = Database::getConnection();
$connection->insert('node_revision')
->fields([
'nid' => '83863',
'vid' => '83863',
'uid' => '1',
'title' => 'Oembed example #1',
'log' => '',
'timestamp' => '1648449960',
'status' => '1',
'comment' => '2',
'promote' => '1',
'sticky' => '0',
])
->execute();
<?php
// phpcs:ignoreFile
/**
* @file
* A database agnostic dump for testing purposes.
*
* This file was generated by the Drupal 9.2.6 db-tools.php script.
*/
use Drupal\Core\Database\Database;
$connection = Database::getConnection();
$connection->insert('node_type')
->fields(array(
'type' => 'oembed_content',
'name' => 'OEmbed Content',
'base' => 'node_content',
'module' => 'node',
'description' => '',
'help' => '',
'has_title' => '1',
'title_label' => 'Title',
'custom' => '1',
'modified' => '1',
'locked' => '0',
'disabled' => '0',
'orig_type' => 'oembed_content',
))
->execute();
<?php
// phpcs:ignoreFile
/**
* @file
* A database agnostic dump for testing purposes.
*
* This file was generated by the Drupal 9.2.6 db-tools.php script.
*/
use Drupal\Core\Database\Database;
$connection = Database::getConnection();
$connection->insert('system')
->fields(array(
'filename' => 'sites/all/modules/media_oembed/media_oembed.module',
'name' => 'media_oembed',
'type' => 'module',
'owner' => '',
'status' => '1',
'bootstrap' => '0',
'schema_version' => '0',
'weight' => '0',
'info' => 'a:13:{s:4:"name";s:13:"Media: oEmbed";s:11:"description";s:42:"Adds oEmbed as a supported media provider.";s:7:"package";s:5:"Media";s:4:"core";s:3:"7.x";s:12:"dependencies";a:1:{i:0;s:14:"media_internet";}s:5:"files";a:2:{i:0;s:37:"includes/MediaOEmbedStreamWrapper.inc";i:1;s:39:"includes/MediaInternetOEmbedHandler.inc";}s:9:"configure";s:31:"admin/config/media/media-oembed";s:7:"version";s:7:"7.x-2.7";s:7:"project";s:12:"media_oembed";s:9:"datestamp";s:10:"1467129893";s:5:"mtime";i:1648601256;s:3:"php";s:5:"5.2.4";s:9:"bootstrap";i:0;}',
))
->execute();
<?php
namespace Drupal\Tests\media_migration\Kernel;
use Drupal\media\Entity\Media;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\node\Entity\Node;
use Drupal\Tests\media\Traits\MediaTypeCreationTrait;
use Drupal\Tests\migmag\Traits\MigMagKernelTestDxTrait;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
/**
* Tests Oembed media migration.
*
* @group media_migration
*/
class OembedMigrationTest extends MigrateDrupal7TestBase {
use MigMagKernelTestDxTrait;
use MediaTypeCreationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'comment',
'media',
'migrate_plus',
'media_migration',
'media_migration_test_oembed',
'migmag_process',
'menu_ui',
'node',
'smart_sql_idmap',
'image',
'file',
'text',
'filter',
'taxonomy',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installSchema('media_migration', ['media_migration_media_entity_uuid_prophecy']);
$this->createMediaType('oembed:video', ['id' => 'remote_video']);
$this->installEntitySchema('media');
$this->installEntitySchema('file');
$this->installEntitySchema('node');
$this->installConfig(['node']);
$this->installSchema('file', 'file_usage');
$this->loadFixture(
implode(DIRECTORY_SEPARATOR, [
dirname(__DIR__, 3),
'tests',
'fixtures',
'drupal7_oembed_partial.php',
])
);
}
/**
* {@inheritdoc}
*/
protected function getFixtureFilePath() {
return implode(DIRECTORY_SEPARATOR, [
dirname(__DIR__, 3),
'tests',
'fixtures',
'drupal7_media.php',
]);
}
/**
* Tests Oembed media migrations.
*/
public function testMigration(): void {
$this->startCollectingMessages();
$this->executeMigration('d7_file');
$this->executeMigrations([
'd7_field',
'd7_view_modes',
'd7_filter_format',
'd7_comment_type',
'd7_node_type',
'd7_file_entity_source_field',
'd7_file_entity_type',
'd7_field_instance',
'd7_file_entity_source_field_config',
'd7_user_role',
'd7_user',
'd7_file_entity:video',
'd7_file_entity:image:public',
'd7_node_complete:oembed_content',
]);
$this->assertExpectedMigrationMessages();
$media = Media::load(1);
$this->assertInstanceOf(Media::class, $media);
$this->assertEquals(
[
'mid' => [['value' => '1']],
'vid' => [['value' => '1']],
'langcode' => [['value' => 'en']],
'bundle' => [['target_id' => 'remote_video']],
'revision_user' => [],
'revision_log_message' => [],
'status' => [['value' => '1']],
'uid' => [['target_id' => '1']],
'name' => [['value' => 'ACSF']],
'created' => [['value' => '1648447404']],
'default_langcode' => [['value' => '1']],
'revision_default' => [['value' => '1']],
'revision_translation_affected' => [['value' => '1']],
'field_media_oembed_video' => [
[
'value' => 'https://player.vimeo.com/video/268828727',
],
],
],
array_diff_key(
$media->toArray(),
['uuid' => 1, 'thumbnail' => 1, 'revision_created' => 1, 'changed' => 1]
)
);
$this->assertCount(1, Node::loadMultiple());
$this->assertEquals(
[
'field_media' => [['target_id' => 1]],
],
array_intersect_key(
Node::load(83863)->toArray(),
['field_media' => 1]
)
);
}
/**
* {@inheritdoc}
*/
protected function prepareMigration(MigrationInterface $migration) {
$source = $migration->getSourceConfiguration();
if ($source['plugin'] === 'd7_file') {
$source_file_path = implode(
DIRECTORY_SEPARATOR,
[
dirname(__DIR__, 2),
'fixtures',
]
);
$source['constants']['source_base_path'] = $source_file_path;
$migration->set('source', $source);
}
}
}
<?php
namespace Drupal\Tests\media_migration\Unit\Plugin\migrate\process;
use Drupal\media_migration\Plugin\migrate\process\MediaOembedFieldValue;
use Drupal\Tests\migrate\Unit\process\MigrateProcessTestCase;
/**
* Tests the MediaOembedFieldValue migration process plugin.
*
* @coversDefaultClass \Drupal\media_migration\Plugin\migrate\process\MediaOembedFieldValue
*
* @group media_migration
*/
class MediaOembedFieldValueTest extends MigrateProcessTestCase {
/**
* Tests the process plugin.
*
* @dataProvider providerTestTransform
*/
public function testTransform(string $value, string $expected_value): void {
$plugin = new MediaOembedFieldValue([], 'media_oembed_field_value', []);
$this->assertEquals(
$expected_value,
$plugin->transform($value, $this->migrateExecutable, $this->row, 'd')
);
}
/**
* Data provider for ::testTransform.
*
* @return string[][]
* The test cases.
*/
public function providerTestTransform(): array {
return [
'A Vimeo oembed url' => [
'value' => 'oembed://https%3A//player.vimeo.com/video/268828727',
'expected' => 'https://player.vimeo.com/video/268828727',
],
'A YouTube oembed url' => [
'value' => 'oembed://https%3A//youtu.be/RosijHlrgBI',
'expected' => 'https://youtu.be/RosijHlrgBI',
],
];
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment