3 namespace Drupal\Tests\migrate_drupal\Kernel\d7;
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 MigrateDrupal7AuditIdsTest extends MigrateDrupal7TestBase {
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 d7_node:page migration mappping table to simulate a
62 // previously migrated node.
63 $table_name = $this->getMigration('d7_node:page')->getIdMap()->mapTableName();
64 $this->container->get('database')->insert($table_name)
66 'source_ids_hash' => 1,
72 // Audit the IDs of the d7_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('d7_node:page'),
78 $this->getMigration('d7_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 7');
97 // Audit the IDs of all Drupal 7 migrations. There should be no conflicts
98 // 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 $migrations = $this->container
112 ->get('plugin.manager.migration')
113 ->createInstancesByTag('Drupal 7');
116 $this->createContent();
118 // Audit the IDs of all Drupal 7 migrations. There should be conflicts since
119 // content has been created.
120 $conflicts = array_map(
121 function (AuditResult $result) {
122 return $result->passed() ? NULL : $result->getMigration()->getBaseId();
124 (new IdAuditor())->auditMultiple($migrations)
128 'd7_aggregator_feed',
129 'd7_aggregator_item',
140 $this->assertEmpty(array_diff(array_filter($conflicts), $expected));
144 * Tests draft revisions ID conflicts.
146 public function testDraftRevisionIdConflicts() {
147 // Create a published node of type page.
148 $node = Node::create(['type' => 'page', 'title' => 'foo']);
149 $node->moderation_state->value = 'published';
152 // Create a draft revision.
153 $node->moderation_state->value = 'draft';
154 $node->setNewRevision(TRUE);
157 // Insert data in the d7_node_revision:page migration mappping table to
158 // simulate a previously migrated node revison.
159 $table_name = $this->getMigration('d7_node_revision:page')->getIdMap()->mapTableName();
160 $this->container->get('database')->insert($table_name)
162 'source_ids_hash' => 1,
168 // Audit the IDs of the d7_node_revision migration. There should be
169 // conflicts since a draft revision has been created.
170 /** @var \Drupal\migrate\Audit\AuditResult $result */
171 $result = (new IdAuditor())->audit($this->getMigration('d7_node_revision:page'));
172 $this->assertInstanceOf(AuditResult::class, $result);
173 $this->assertFalse($result->passed());
177 * Tests ID conflicts for inaccessible nodes.
179 public function testNodeGrantsIdConflicts() {
180 // Enable the node_test module to restrict access to page nodes.
181 $this->enableModules(['node_test']);
183 // Create a published node of type page.
184 $node = Node::create(['type' => 'page', 'title' => 'foo']);
185 $node->moderation_state->value = 'published';
188 // Audit the IDs of the d7_node migration. There should be conflicts
189 // even though the new node is not accessible.
190 /** @var \Drupal\migrate\Audit\AuditResult $result */
191 $result = (new IdAuditor())->audit($this->getMigration('d7_node:page'));
192 $this->assertInstanceOf(AuditResult::class, $result);
193 $this->assertFalse($result->passed());