Newer
Older
Lee Rowlands
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<?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\migrate_drupal\Traits\CreateTestContentEntitiesTrait;
use Drupal\workflows\Entity\Workflow;
/**
* Tests the migration auditor for ID conflicts.
*
* @group migrate_drupal
*/
class MigrateDrupal7AuditIdsTest extends MigrateDrupal7TestBase {
use FileSystemModuleDiscoveryDataProviderTrait;
use CreateTestContentEntitiesTrait;
/**
* {@inheritdoc}
*/
protected function setUp() {
// Enable all modules.
self::$modules = array_keys($this->coreModuleListDataProvider());
parent::setUp();
// Install required entity schemas.
$this->installEntitySchemas();
// Install required schemas.
$this->installSchema('book', ['book']);
$this->installSchema('dblog', ['watchdog']);
$this->installSchema('forum', ['forum_index']);
$this->installSchema('node', ['node_access']);
$this->installSchema('search', ['search_dataset']);
Gabor Hojtsy
committed
$this->installSchema('system', ['sequences']);
Lee Rowlands
committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
$this->installSchema('tracker', ['tracker_node', 'tracker_user']);
// Enable content moderation for nodes of type page.
$this->installEntitySchema('content_moderation_state');
$this->installConfig('content_moderation');
NodeType::create(['type' => 'page'])->save();
$workflow = Workflow::load('editorial');
$workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'page');
$workflow->save();
}
/**
* Tests multiple migrations to the same destination with no ID conflicts.
*/
public function testMultipleMigrationWithoutIdConflicts() {
// Create a node of type page.
$node = Node::create(['type' => 'page', 'title' => 'foo']);
$node->moderation_state->value = 'published';
$node->save();
// Insert data in the d7_node:page migration mappping table to simulate a
// previously migrated node.
$table_name = $this->getMigration('d7_node:page')->getIdMap()->mapTableName();
$this->container->get('database')->insert($table_name)
->fields([
'source_ids_hash' => 1,
'sourceid1' => 1,
'destid1' => 1,
])
->execute();
// Audit the IDs of the d7_node migrations for the page & article node type.
// There should be no conflicts since the highest destination ID should be
// equal to the highest migrated ID, as found in the aggregated mapping
// tables of the two node migrations.
$migrations = [
$this->getMigration('d7_node:page'),
$this->getMigration('d7_node:article'),
];
$results = (new IdAuditor())->auditMultiple($migrations);
/** @var \Drupal\migrate\Audit\AuditResult $result */
foreach ($results as $result) {
$this->assertInstanceOf(AuditResult::class, $result);
$this->assertTrue($result->passed());
}
}
/**
* Tests all migrations with no ID conflicts.
*/
public function testAllMigrationsWithNoIdConflicts() {
$migrations = $this->container
->get('plugin.manager.migration')
->createInstancesByTag('Drupal 7');
// Audit the IDs of all Drupal 7 migrations. There should be no conflicts
// since no content has been created.
$results = (new IdAuditor())->auditMultiple($migrations);
/** @var \Drupal\migrate\Audit\AuditResult $result */
foreach ($results as $result) {
$this->assertInstanceOf(AuditResult::class, $result);
$this->assertTrue($result->passed());
}
}
/**
* Tests all migrations with ID conflicts.
*/
public function testAllMigrationsWithIdConflicts() {
$migrations = $this->container
->get('plugin.manager.migration')
->createInstancesByTag('Drupal 7');
// Create content.
$this->createContent();
// Audit the IDs of all Drupal 7 migrations. There should be conflicts since
// content has been created.
$conflicts = array_map(
function (AuditResult $result) {
return $result->passed() ? NULL : $result->getMigration()->getBaseId();
},
(new IdAuditor())->auditMultiple($migrations)
);
$expected = [
'd7_aggregator_feed',
'd7_aggregator_item',
'd7_comment',
'd7_custom_block',
'd7_file',
'd7_file_private',
'd7_menu_links',
'd7_node',
'd7_node_revision',
'd7_taxonomy_term',
'd7_user',
];
$this->assertEmpty(array_diff(array_filter($conflicts), $expected));
}
/**
* Tests draft revisions ID conflicts.
*/
public function testDraftRevisionIdConflicts() {
// Create a published node of type page.
$node = Node::create(['type' => 'page', 'title' => 'foo']);
$node->moderation_state->value = 'published';
$node->save();
// Create a draft revision.
$node->moderation_state->value = 'draft';
$node->setNewRevision(TRUE);
$node->save();
// Insert data in the d7_node_revision:page migration mappping table to
// simulate a previously migrated node revison.
$table_name = $this->getMigration('d7_node_revision:page')->getIdMap()->mapTableName();
$this->container->get('database')->insert($table_name)
->fields([
'source_ids_hash' => 1,
'sourceid1' => 1,
'destid1' => 1,
])
->execute();
// Audit the IDs of the d7_node_revision migration. There should be
// conflicts since a draft revision has been created.
/** @var \Drupal\migrate\Audit\AuditResult $result */
$result = (new IdAuditor())->audit($this->getMigration('d7_node_revision:page'));
$this->assertInstanceOf(AuditResult::class, $result);
$this->assertFalse($result->passed());
}
/**
* Tests ID conflicts for inaccessible nodes.
*/
public function testNodeGrantsIdConflicts() {
// Enable the node_test module to restrict access to page nodes.
$this->enableModules(['node_test']);
// Create a published node of type page.
$node = Node::create(['type' => 'page', 'title' => 'foo']);
$node->moderation_state->value = 'published';
$node->save();
// Audit the IDs of the d7_node migration. There should be conflicts
// even though the new node is not accessible.
/** @var \Drupal\migrate\Audit\AuditResult $result */
$result = (new IdAuditor())->audit($this->getMigration('d7_node:page'));
$this->assertInstanceOf(AuditResult::class, $result);
$this->assertFalse($result->passed());
}
}