View source
<?php
namespace Drupal\Tests\paragraphs\Kernel\migrate;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\migrate\Plugin\MigrationInterface;
use Drupal\paragraphs\Entity\ParagraphsType;
use Drupal\Tests\migrate_drupal\Kernel\MigrateDrupalTestBase;
abstract class ParagraphsMigrationTestBase extends MigrateDrupalTestBase {
public static $modules = [
'entity_reference_revisions',
'migrate',
'migrate_drupal',
'paragraphs',
];
public function setUp() : void {
parent::setUp();
$this
->loadFixture(__DIR__ . '/../../../fixtures/drupal7.php');
}
protected function assertParagraphBundleExists($bundle_machine_name, $bundle_label) {
$bundle = ParagraphsType::load($bundle_machine_name);
$this
->assertInstanceOf(ParagraphsType::class, $bundle);
$this
->assertEquals($bundle_label, $bundle
->label());
}
protected function assertParagraphEntityFieldExists($field_name, $field_type) {
$field_storage = FieldStorageConfig::loadByName('paragraph', $field_name);
$this
->assertNotNull($field_storage);
$this
->assertEquals($field_type, $field_storage
->getType());
}
protected function assertParagraphFieldExists($entity_type, $field_name) {
$field_storage = FieldStorageConfig::loadByName($entity_type, $field_name);
$this
->assertNotNull($field_storage);
$this
->assertEquals('entity_reference_revisions', $field_storage
->getType());
$this
->assertEquals('paragraph', $field_storage
->getSetting('target_type'));
}
protected function assertFieldInstanceExists($entity_type, $bundle, $field_name, $field_type = 'entity_reference_revisions') {
$field = FieldConfig::loadByName($entity_type, $bundle, $field_name);
$this
->assertNotNull($field);
$this
->assertEquals($field_type, $field
->getType());
}
protected function executeMigrationWithDependencies($plugin_id) {
$manager = $this->container
->get('plugin.manager.migration');
$migrations = $manager
->createInstances($plugin_id);
foreach ($migrations as $migration) {
$this
->executeMigrationDependencies($migration);
$this
->executeMigration($migration);
}
}
protected function executeMigrationDependencies(MigrationInterface $migration) {
$dependencies = $migration
->getMigrationDependencies();
foreach ($dependencies['required'] as $dependency) {
$plugin = $this
->getMigration($dependency);
if (!$plugin
->allRowsProcessed()) {
$this
->executeMigrationDependencies($plugin);
$this
->executeMigration($plugin);
}
}
foreach ($dependencies['optional'] as $dependency) {
if ($plugin = $this
->getMigration($dependency)) {
if (!$plugin
->allRowsProcessed()) {
$this
->executeMigrationDependencies($plugin);
$this
->executeMigration($plugin);
}
}
}
}
protected function prepareMigration(MigrationInterface $migration) {
if ($migration
->id() == 'd7_node_revision:paragraphs_test') {
$migration
->set('migration_dependencies', [
'required' => [
'd7_node:paragraphs_test',
],
'optional' => [],
]);
$migration
->set('requirements', [
'd7_node:paragraphs_test' => 'd7_node:paragraphs_test',
]);
}
}
}