Further Drupal 8.6.4 changes. Some core files were not committed before a commit...
[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\content_moderation\Traits\ContentModerationTestTrait;
11 use Drupal\Tests\migrate_drupal\Traits\CreateTestContentEntitiesTrait;
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   use ContentModerationTestTrait;
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function setUp() {
28     // Enable all modules.
29     self::$modules = array_keys($this->coreModuleListDataProvider());
30     parent::setUp();
31
32     // Install required entity schemas.
33     $this->installEntitySchemas();
34
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']);
43
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');
50     $workflow->save();
51   }
52
53   /**
54    * Tests multiple migrations to the same destination with no ID conflicts.
55    */
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';
60     $node->save();
61
62     // Insert data in the d6_node:page migration mapping table to simulate a
63     // previously migrated node.
64     $id_map = $this->getMigration('d6_node:page')->getIdMap();
65     $table_name = $id_map->mapTableName();
66     $id_map->getDatabase()->insert($table_name)
67       ->fields([
68         'source_ids_hash' => 1,
69         'sourceid1' => 1,
70         'destid1' => 1,
71       ])
72       ->execute();
73
74     // Audit the IDs of the d6_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.
78     $migrations = [
79       $this->getMigration('d6_node:page'),
80       $this->getMigration('d6_node:article'),
81     ];
82
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());
88     }
89   }
90
91   /**
92    * Tests all migrations with no ID conflicts.
93    */
94   public function testAllMigrationsWithNoIdConflicts() {
95     $migrations = $this->container
96       ->get('plugin.manager.migration')
97       ->createInstancesByTag('Drupal 6');
98
99     // Audit all Drupal 6 migrations that support it. There should be no
100     // conflicts 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());
106     }
107   }
108
109   /**
110    * Tests all migrations with ID conflicts.
111    */
112   public function testAllMigrationsWithIdConflicts() {
113     // Get all Drupal 6 migrations.
114     $migrations = $this->container
115       ->get('plugin.manager.migration')
116       ->createInstancesByTag('Drupal 6');
117
118     // Create content.
119     $this->createContent();
120
121     // Audit the IDs of all migrations. There should be conflicts since content
122     // has been created.
123     $conflicts = array_map(
124       function (AuditResult $result) {
125         return $result->passed() ? NULL : $result->getMigration()->getBaseId();
126       },
127       (new IdAuditor())->auditMultiple($migrations)
128     );
129
130     $expected = [
131       'd6_aggregator_feed',
132       'd6_aggregator_item',
133       'd6_comment',
134       'd6_custom_block',
135       'd6_file',
136       'd6_menu_links',
137       'd6_node',
138       'd6_node_revision',
139       'd6_taxonomy_term',
140       'd6_term_node_revision',
141       'd6_user',
142       'node_translation_menu_links',
143     ];
144     $this->assertEmpty(array_diff(array_filter($conflicts), $expected));
145   }
146
147   /**
148    * Tests draft revisions ID conflicts.
149    */
150   public function testDraftRevisionIdConflicts() {
151     // Create a published node of type page.
152     $node = Node::create(['type' => 'page', 'title' => 'foo']);
153     $node->moderation_state->value = 'published';
154     $node->save();
155
156     // Create a draft revision.
157     $node->moderation_state->value = 'draft';
158     $node->setNewRevision(TRUE);
159     $node->save();
160
161     // Insert data in the d6_node_revision:page migration mapping table to
162     // simulate a previously migrated node revision.
163     $id_map = $this->getMigration('d6_node_revision:page')->getIdMap();
164     $table_name = $id_map->mapTableName();
165     $id_map->getDatabase()->insert($table_name)
166       ->fields([
167         'source_ids_hash' => 1,
168         'sourceid1' => 1,
169         'destid1' => 1,
170       ])
171       ->execute();
172
173     // Audit the IDs of the d6_node_revision migration. There should be
174     // conflicts since a draft revision has been created.
175     /** @var \Drupal\migrate\Audit\AuditResult $result */
176     $result = (new IdAuditor())->audit($this->getMigration('d6_node_revision:page'));
177     $this->assertInstanceOf(AuditResult::class, $result);
178     $this->assertFalse($result->passed());
179   }
180
181   /**
182    * Tests ID conflicts for inaccessible nodes.
183    */
184   public function testNodeGrantsIdConflicts() {
185     // Enable the node_test module to restrict access to page nodes.
186     $this->enableModules(['node_test']);
187
188     // Create a published node of type page.
189     $node = Node::create(['type' => 'page', 'title' => 'foo']);
190     $node->moderation_state->value = 'published';
191     $node->save();
192
193     // Audit the IDs of the d6_node migration. There should be conflicts
194     // even though the new node is not accessible.
195     /** @var \Drupal\migrate\Audit\AuditResult $result */
196     $result = (new IdAuditor())->audit($this->getMigration('d6_node:page'));
197     $this->assertInstanceOf(AuditResult::class, $result);
198     $this->assertFalse($result->passed());
199   }
200
201 }