View source
<?php
namespace Drupal\Tests\migrate_drupal\Kernel\d7;
use Drupal\KernelTests\FileSystemModuleDiscoveryDataProviderTrait;
use Drupal\migrate\Audit\AuditResult;
use Drupal\migrate\Audit\IdAuditor;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
use Drupal\Tests\migrate_drupal\Traits\CreateTestContentEntitiesTrait;
class MigrateDrupal7AuditIdsTest extends MigrateDrupal7TestBase {
use FileSystemModuleDiscoveryDataProviderTrait;
use CreateTestContentEntitiesTrait;
use ContentModerationTestTrait;
protected function setUp() : void {
self::$modules = array_keys($this
->coreModuleListDataProvider());
parent::setUp();
$this
->installEntitySchemas();
$this
->installSchema('book', [
'book',
]);
$this
->installSchema('dblog', [
'watchdog',
]);
$this
->installSchema('forum', [
'forum_index',
]);
$this
->installSchema('node', [
'node_access',
]);
$this
->installSchema('search', [
'search_dataset',
]);
$this
->installSchema('system', [
'sequences',
]);
$this
->installSchema('tracker', [
'tracker_node',
'tracker_user',
]);
$this
->installEntitySchema('content_moderation_state');
$this
->installConfig('content_moderation');
NodeType::create([
'type' => 'page',
])
->save();
$workflow = $this
->createEditorialWorkflow();
$workflow
->getTypePlugin()
->addEntityTypeAndBundle('node', 'page');
$workflow
->save();
}
public function testMultipleMigrationWithoutIdConflicts() {
$node = Node::create([
'type' => 'page',
'title' => 'foo',
]);
$node->moderation_state->value = 'published';
$node
->save();
$id_map = $this
->getMigration('d7_node:page')
->getIdMap();
$table_name = $id_map
->mapTableName();
$id_map
->getDatabase()
->insert($table_name)
->fields([
'source_ids_hash' => 1,
'sourceid1' => 1,
'destid1' => 1,
])
->execute();
$migrations = [
$this
->getMigration('d7_node:page'),
$this
->getMigration('d7_node:article'),
];
$results = (new IdAuditor())
->auditMultiple($migrations);
foreach ($results as $result) {
$this
->assertInstanceOf(AuditResult::class, $result);
$this
->assertTrue($result
->passed());
}
}
public function testAllMigrationsWithNoIdConflicts() {
$migrations = $this->container
->get('plugin.manager.migration')
->createInstancesByTag('Drupal 7');
$results = (new IdAuditor())
->auditMultiple($migrations);
foreach ($results as $result) {
$this
->assertInstanceOf(AuditResult::class, $result);
$this
->assertTrue($result
->passed());
}
}
public function testAllMigrationsWithIdConflicts() {
$migrations = $this->container
->get('plugin.manager.migration')
->createInstancesByTag('Drupal 7');
$this
->createContent();
$conflicts = array_map(function (AuditResult $result) {
return $result
->passed() ? NULL : $result
->getMigration()
->getBaseId();
}, (new IdAuditor())
->auditMultiple($migrations));
$expected = [
'd7_comment',
'd7_custom_block',
'd7_file',
'd7_file_private',
'd7_menu_links',
'd7_node',
'd7_node_complete',
'd7_node_revision',
'd7_taxonomy_term',
'd7_user',
'node_translation_menu_links',
];
$this
->assertEmpty(array_diff(array_filter($conflicts), $expected));
}
public function testDraftRevisionIdConflicts() {
$node = Node::create([
'type' => 'page',
'title' => 'foo',
]);
$node->moderation_state->value = 'published';
$node
->save();
$node->moderation_state->value = 'draft';
$node
->setNewRevision(TRUE);
$node
->save();
$id_map = $this
->getMigration('d7_node_revision:page')
->getIdMap();
$table_name = $id_map
->mapTableName();
$id_map
->getDatabase()
->insert($table_name)
->fields([
'source_ids_hash' => 1,
'sourceid1' => 1,
'destid1' => 1,
])
->execute();
$result = (new IdAuditor())
->audit($this
->getMigration('d7_node_revision:page'));
$this
->assertInstanceOf(AuditResult::class, $result);
$this
->assertFalse($result
->passed());
}
public function testNodeGrantsIdConflicts() {
$this
->enableModules([
'node_test',
]);
$node = Node::create([
'type' => 'page',
'title' => 'foo',
]);
$node->moderation_state->value = 'published';
$node
->save();
$result = (new IdAuditor())
->audit($this
->getMigration('d7_node:page'));
$this
->assertInstanceOf(AuditResult::class, $result);
$this
->assertFalse($result
->passed());
}
}