Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / modules / migrate_drupal / tests / src / Kernel / d6 / MigrateDrupal6AuditIdsTest.php
1 <?php
2
3 namespace Drupal\Tests\migrate_drupal\Kernel\d6;
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 MigrateDrupal6AuditIdsTest extends MigrateDrupal6TestBase {
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 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)
65       ->fields([
66         'source_ids_hash' => 1,
67         'sourceid1' => 1,
68         'destid1' => 1,
69       ])
70       ->execute();
71
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.
76     $migrations = [
77       $this->getMigration('d6_node:page'),
78       $this->getMigration('d6_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 6');
96
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());
104     }
105   }
106
107   /**
108    * Tests all migrations with ID conflicts.
109    */
110   public function testAllMigrationsWithIdConflicts() {
111     // Get all Drupal 6 migrations.
112     $migrations = $this->container
113       ->get('plugin.manager.migration')
114       ->createInstancesByTag('Drupal 6');
115
116     // Create content.
117     $this->createContent();
118
119     // Audit the IDs of all migrations. There should be conflicts since content
120     // has been created.
121     $conflicts = array_map(
122       function (AuditResult $result) {
123         return $result->passed() ? NULL : $result->getMigration()->getBaseId();
124       },
125       (new IdAuditor())->auditMultiple($migrations)
126     );
127
128     $expected = [
129       'd6_aggregator_feed',
130       'd6_aggregator_item',
131       'd6_comment',
132       'd6_custom_block',
133       'd6_file',
134       'd6_menu_links',
135       'd6_node',
136       'd6_node_revision',
137       'd6_taxonomy_term',
138       'd6_term_node_revision',
139       'd6_user',
140     ];
141     $this->assertEmpty(array_diff(array_filter($conflicts), $expected));
142   }
143
144   /**
145    * Tests draft revisions ID conflicts.
146    */
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';
151     $node->save();
152
153     // Create a draft revision.
154     $node->moderation_state->value = 'draft';
155     $node->setNewRevision(TRUE);
156     $node->save();
157
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)
162       ->fields([
163         'source_ids_hash' => 1,
164         'sourceid1' => 1,
165         'destid1' => 1,
166       ])
167       ->execute();
168
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());
175   }
176
177   /**
178    * Tests ID conflicts for inaccessible nodes.
179    */
180   public function testNodeGrantsIdConflicts() {
181     // Enable the node_test module to restrict access to page nodes.
182     $this->enableModules(['node_test']);
183
184     // Create a published node of type page.
185     $node = Node::create(['type' => 'page', 'title' => 'foo']);
186     $node->moderation_state->value = 'published';
187     $node->save();
188
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());
195   }
196
197 }