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\content_moderation\Traits\ContentModerationTestTrait;
11 use Drupal\Tests\migrate_drupal\Traits\CreateTestContentEntitiesTrait;
14 * Tests the migration auditor for ID conflicts.
16 * @group migrate_drupal
18 class MigrateDrupal7AuditIdsTest extends MigrateDrupal7TestBase {
20 use FileSystemModuleDiscoveryDataProviderTrait;
21 use CreateTestContentEntitiesTrait;
22 use ContentModerationTestTrait;
27 protected function setUp() {
28 // Enable all modules.
29 self::$modules = array_keys($this->coreModuleListDataProvider());
32 // Install required entity schemas.
33 $this->installEntitySchemas();
35 // Install required schemas.
36 $this->installSchema('book', ['book']);
37 $this->installSchema('dblog', ['watchdog']);
38 $this->installSchema('forum', ['forum_index']);
39 $this->installSchema('node', ['node_access']);
40 $this->installSchema('search', ['search_dataset']);
41 $this->installSchema('system', ['sequences']);
42 $this->installSchema('tracker', ['tracker_node', 'tracker_user']);
44 // Enable content moderation for nodes of type page.
45 $this->installEntitySchema('content_moderation_state');
46 $this->installConfig('content_moderation');
47 NodeType::create(['type' => 'page'])->save();
48 $workflow = $this->createEditorialWorkflow();
49 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'page');
54 * Tests multiple migrations to the same destination with no ID conflicts.
56 public function testMultipleMigrationWithoutIdConflicts() {
57 // Create a node of type page.
58 $node = Node::create(['type' => 'page', 'title' => 'foo']);
59 $node->moderation_state->value = 'published';
62 // Insert data in the d7_node:page migration mapping table to simulate a
63 // previously migrated node.
64 $id_map = $this->getMigration('d7_node:page')->getIdMap();
65 $table_name = $id_map->mapTableName();
66 $id_map->getDatabase()->insert($table_name)
68 'source_ids_hash' => 1,
74 // Audit the IDs of the d7_node migrations for the page & article node type.
75 // There should be no conflicts since the highest destination ID should be
76 // equal to the highest migrated ID, as found in the aggregated mapping
77 // tables of the two node migrations.
79 $this->getMigration('d7_node:page'),
80 $this->getMigration('d7_node:article'),
83 $results = (new IdAuditor())->auditMultiple($migrations);
84 /** @var \Drupal\migrate\Audit\AuditResult $result */
85 foreach ($results as $result) {
86 $this->assertInstanceOf(AuditResult::class, $result);
87 $this->assertTrue($result->passed());
92 * Tests all migrations with no ID conflicts.
94 public function testAllMigrationsWithNoIdConflicts() {
95 $migrations = $this->container
96 ->get('plugin.manager.migration')
97 ->createInstancesByTag('Drupal 7');
99 // Audit the IDs of all Drupal 7 migrations. There should be no conflicts
100 // since no content has been created.
101 $results = (new IdAuditor())->auditMultiple($migrations);
102 /** @var \Drupal\migrate\Audit\AuditResult $result */
103 foreach ($results as $result) {
104 $this->assertInstanceOf(AuditResult::class, $result);
105 $this->assertTrue($result->passed());
110 * Tests all migrations with ID conflicts.
112 public function testAllMigrationsWithIdConflicts() {
113 $migrations = $this->container
114 ->get('plugin.manager.migration')
115 ->createInstancesByTag('Drupal 7');
118 $this->createContent();
120 // Audit the IDs of all Drupal 7 migrations. There should be conflicts since
121 // content has been created.
122 $conflicts = array_map(
123 function (AuditResult $result) {
124 return $result->passed() ? NULL : $result->getMigration()->getBaseId();
126 (new IdAuditor())->auditMultiple($migrations)
130 'd7_aggregator_feed',
131 'd7_aggregator_item',
141 'node_translation_menu_links',
143 $this->assertEmpty(array_diff(array_filter($conflicts), $expected));
147 * Tests draft revisions ID conflicts.
149 public function testDraftRevisionIdConflicts() {
150 // Create a published node of type page.
151 $node = Node::create(['type' => 'page', 'title' => 'foo']);
152 $node->moderation_state->value = 'published';
155 // Create a draft revision.
156 $node->moderation_state->value = 'draft';
157 $node->setNewRevision(TRUE);
160 // Insert data in the d7_node_revision:page migration mapping table to
161 // simulate a previously migrated node revision.
162 $id_map = $this->getMigration('d7_node_revision:page')->getIdMap();
163 $table_name = $id_map->mapTableName();
164 $id_map->getDatabase()->insert($table_name)
166 'source_ids_hash' => 1,
172 // Audit the IDs of the d7_node_revision migration. There should be
173 // conflicts since a draft revision has been created.
174 /** @var \Drupal\migrate\Audit\AuditResult $result */
175 $result = (new IdAuditor())->audit($this->getMigration('d7_node_revision:page'));
176 $this->assertInstanceOf(AuditResult::class, $result);
177 $this->assertFalse($result->passed());
181 * Tests ID conflicts for inaccessible nodes.
183 public function testNodeGrantsIdConflicts() {
184 // Enable the node_test module to restrict access to page nodes.
185 $this->enableModules(['node_test']);
187 // Create a published node of type page.
188 $node = Node::create(['type' => 'page', 'title' => 'foo']);
189 $node->moderation_state->value = 'published';
192 // Audit the IDs of the d7_node migration. There should be conflicts
193 // even though the new node is not accessible.
194 /** @var \Drupal\migrate\Audit\AuditResult $result */
195 $result = (new IdAuditor())->audit($this->getMigration('d7_node:page'));
196 $this->assertInstanceOf(AuditResult::class, $result);
197 $this->assertFalse($result->passed());