351ab43d7261fab6491f1992f1797f3f6501ba8a
[yaffs-website] / web / core / modules / migrate_drupal / tests / src / Kernel / d7 / MigrateDrupal7AuditIdsTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate_drupal\Kernel\d7;
4
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;
12
13 /**
14  * Tests the migration auditor for ID conflicts.
15  *
16  * @group migrate_drupal
17  */
18 class MigrateDrupal7AuditIdsTest extends MigrateDrupal7TestBase {
19
20   use FileSystemModuleDiscoveryDataProviderTrait;
21   use CreateTestContentEntitiesTrait;
22
23   /**
24    * {@inheritdoc}
25    */
26   protected function setUp() {
27     // Enable all modules.
28     self::$modules = array_keys($this->coreModuleListDataProvider());
29     parent::setUp();
30
31     // Install required entity schemas.
32     $this->installEntitySchemas();
33
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']);
42
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');
49     $workflow->save();
50   }
51
52   /**
53    * Tests multiple migrations to the same destination with no ID conflicts.
54    */
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';
59     $node->save();
60
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)
65       ->fields([
66         'source_ids_hash' => 1,
67         'sourceid1' => 1,
68         'destid1' => 1,
69       ])
70       ->execute();
71
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.
76     $migrations = [
77       $this->getMigration('d7_node:page'),
78       $this->getMigration('d7_node:article'),
79     ];
80
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());
86     }
87   }
88
89   /**
90    * Tests all migrations with no ID conflicts.
91    */
92   public function testAllMigrationsWithNoIdConflicts() {
93     $migrations = $this->container
94       ->get('plugin.manager.migration')
95       ->createInstancesByTag('Drupal 7');
96
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());
104     }
105   }
106
107   /**
108    * Tests all migrations with ID conflicts.
109    */
110   public function testAllMigrationsWithIdConflicts() {
111     $migrations = $this->container
112       ->get('plugin.manager.migration')
113       ->createInstancesByTag('Drupal 7');
114
115     // Create content.
116     $this->createContent();
117
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();
123       },
124       (new IdAuditor())->auditMultiple($migrations)
125     );
126
127     $expected = [
128       'd7_aggregator_feed',
129       'd7_aggregator_item',
130       'd7_comment',
131       'd7_custom_block',
132       'd7_file',
133       'd7_file_private',
134       'd7_menu_links',
135       'd7_node',
136       'd7_node_revision',
137       'd7_taxonomy_term',
138       'd7_user',
139     ];
140     $this->assertEmpty(array_diff(array_filter($conflicts), $expected));
141   }
142
143   /**
144    * Tests draft revisions ID conflicts.
145    */
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';
150     $node->save();
151
152     // Create a draft revision.
153     $node->moderation_state->value = 'draft';
154     $node->setNewRevision(TRUE);
155     $node->save();
156
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)
161       ->fields([
162         'source_ids_hash' => 1,
163         'sourceid1' => 1,
164         'destid1' => 1,
165       ])
166       ->execute();
167
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());
174   }
175
176   /**
177    * Tests ID conflicts for inaccessible nodes.
178    */
179   public function testNodeGrantsIdConflicts() {
180     // Enable the node_test module to restrict access to page nodes.
181     $this->enableModules(['node_test']);
182
183     // Create a published node of type page.
184     $node = Node::create(['type' => 'page', 'title' => 'foo']);
185     $node->moderation_state->value = 'published';
186     $node->save();
187
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());
194   }
195
196 }