3 namespace Drupal\Tests\migrate_drupal\Kernel\d6;
5 use Drupal\KernelTests\FileSystemModuleDiscoveryDataProviderTrait;
6 use Drupal\migrate\Audit\AuditResult;
7 use Drupal\migrate\Audit\IdAuditor;
8 use Drupal\node\Entity\Node;
9 use Drupal\node\Entity\NodeType;
10 use Drupal\Tests\migrate_drupal\Traits\CreateTestContentEntitiesTrait;
11 use Drupal\workflows\Entity\Workflow;
14 * Tests the migration auditor for ID conflicts.
16 * @group migrate_drupal
18 class MigrateDrupal6AuditIdsTest extends MigrateDrupal6TestBase {
20 use FileSystemModuleDiscoveryDataProviderTrait;
21 use CreateTestContentEntitiesTrait;
26 protected function setUp() {
27 // Enable all modules.
28 self::$modules = array_keys($this->coreModuleListDataProvider());
31 // Install required entity schemas.
32 $this->installEntitySchemas();
34 // Install required schemas.
35 $this->installSchema('book', ['book']);
36 $this->installSchema('dblog', ['watchdog']);
37 $this->installSchema('forum', ['forum_index']);
38 $this->installSchema('node', ['node_access']);
39 $this->installSchema('search', ['search_dataset']);
40 $this->installSchema('system', ['sequences']);
41 $this->installSchema('tracker', ['tracker_node', 'tracker_user']);
43 // Enable content moderation for nodes of type page.
44 $this->installEntitySchema('content_moderation_state');
45 $this->installConfig('content_moderation');
46 NodeType::create(['type' => 'page'])->save();
47 $workflow = Workflow::load('editorial');
48 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'page');
53 * Tests multiple migrations to the same destination with no ID conflicts.
55 public function testMultipleMigrationWithoutIdConflicts() {
56 // Create a node of type page.
57 $node = Node::create(['type' => 'page', 'title' => 'foo']);
58 $node->moderation_state->value = 'published';
61 // Insert data in the d6_node:page migration mappping table to simulate a
62 // previously migrated node.
63 $table_name = $this->getMigration('d6_node:page')->getIdMap()->mapTableName();
64 $this->container->get('database')->insert($table_name)
66 'source_ids_hash' => 1,
72 // Audit the IDs of the d6_node migrations for the page & article node type.
73 // There should be no conflicts since the highest destination ID should be
74 // equal to the highest migrated ID, as found in the aggregated mapping
75 // tables of the two node migrations.
77 $this->getMigration('d6_node:page'),
78 $this->getMigration('d6_node:article'),
81 $results = (new IdAuditor())->auditMultiple($migrations);
82 /** @var \Drupal\migrate\Audit\AuditResult $result */
83 foreach ($results as $result) {
84 $this->assertInstanceOf(AuditResult::class, $result);
85 $this->assertTrue($result->passed());
90 * Tests all migrations with no ID conflicts.
92 public function testAllMigrationsWithNoIdConflicts() {
93 $migrations = $this->container
94 ->get('plugin.manager.migration')
95 ->createInstancesByTag('Drupal 6');
97 // Audit all Drupal 6 migrations that support it. There should be no
98 // conflicts since no content has been created.
99 $results = (new IdAuditor())->auditMultiple($migrations);
100 /** @var \Drupal\migrate\Audit\AuditResult $result */
101 foreach ($results as $result) {
102 $this->assertInstanceOf(AuditResult::class, $result);
103 $this->assertTrue($result->passed());
108 * Tests all migrations with ID conflicts.
110 public function testAllMigrationsWithIdConflicts() {
111 // Get all Drupal 6 migrations.
112 $migrations = $this->container
113 ->get('plugin.manager.migration')
114 ->createInstancesByTag('Drupal 6');
117 $this->createContent();
119 // Audit the IDs of all migrations. There should be conflicts since content
121 $conflicts = array_map(
122 function (AuditResult $result) {
123 return $result->passed() ? NULL : $result->getMigration()->getBaseId();
125 (new IdAuditor())->auditMultiple($migrations)
129 'd6_aggregator_feed',
130 'd6_aggregator_item',
138 'd6_term_node_revision',
141 $this->assertEmpty(array_diff(array_filter($conflicts), $expected));
145 * Tests draft revisions ID conflicts.
147 public function testDraftRevisionIdConflicts() {
148 // Create a published node of type page.
149 $node = Node::create(['type' => 'page', 'title' => 'foo']);
150 $node->moderation_state->value = 'published';
153 // Create a draft revision.
154 $node->moderation_state->value = 'draft';
155 $node->setNewRevision(TRUE);
158 // Insert data in the d6_node_revision:page migration mappping table to
159 // simulate a previously migrated node revison.
160 $table_name = $this->getMigration('d6_node_revision:page')->getIdMap()->mapTableName();
161 $this->container->get('database')->insert($table_name)
163 'source_ids_hash' => 1,
169 // Audit the IDs of the d6_node_revision migration. There should be
170 // conflicts since a draft revision has been created.
171 /** @var \Drupal\migrate\Audit\AuditResult $result */
172 $result = (new IdAuditor())->audit($this->getMigration('d6_node_revision:page'));
173 $this->assertInstanceOf(AuditResult::class, $result);
174 $this->assertFalse($result->passed());
178 * Tests ID conflicts for inaccessible nodes.
180 public function testNodeGrantsIdConflicts() {
181 // Enable the node_test module to restrict access to page nodes.
182 $this->enableModules(['node_test']);
184 // Create a published node of type page.
185 $node = Node::create(['type' => 'page', 'title' => 'foo']);
186 $node->moderation_state->value = 'published';
189 // Audit the IDs of the d6_node migration. There should be conflicts
190 // even though the new node is not accessible.
191 /** @var \Drupal\migrate\Audit\AuditResult $result */
192 $result = (new IdAuditor())->audit($this->getMigration('d6_node:page'));
193 $this->assertInstanceOf(AuditResult::class, $result);
194 $this->assertFalse($result->passed());