Updated Drupal to 8.6. This goes with the following updates because it's possible...
[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\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 MigrateDrupal7AuditIdsTest extends MigrateDrupal7TestBase {
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 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)
67       ->fields([
68         'source_ids_hash' => 1,
69         'sourceid1' => 1,
70         'destid1' => 1,
71       ])
72       ->execute();
73
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.
78     $migrations = [
79       $this->getMigration('d7_node:page'),
80       $this->getMigration('d7_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 7');
98
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());
106     }
107   }
108
109   /**
110    * Tests all migrations with ID conflicts.
111    */
112   public function testAllMigrationsWithIdConflicts() {
113     $migrations = $this->container
114       ->get('plugin.manager.migration')
115       ->createInstancesByTag('Drupal 7');
116
117     // Create content.
118     $this->createContent();
119
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();
125       },
126       (new IdAuditor())->auditMultiple($migrations)
127     );
128
129     $expected = [
130       'd7_aggregator_feed',
131       'd7_aggregator_item',
132       'd7_comment',
133       'd7_custom_block',
134       'd7_file',
135       'd7_file_private',
136       'd7_menu_links',
137       'd7_node',
138       'd7_node_revision',
139       'd7_taxonomy_term',
140       'd7_user',
141       'node_translation_menu_links',
142     ];
143     $this->assertEmpty(array_diff(array_filter($conflicts), $expected));
144   }
145
146   /**
147    * Tests draft revisions ID conflicts.
148    */
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';
153     $node->save();
154
155     // Create a draft revision.
156     $node->moderation_state->value = 'draft';
157     $node->setNewRevision(TRUE);
158     $node->save();
159
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)
165       ->fields([
166         'source_ids_hash' => 1,
167         'sourceid1' => 1,
168         'destid1' => 1,
169       ])
170       ->execute();
171
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());
178   }
179
180   /**
181    * Tests ID conflicts for inaccessible nodes.
182    */
183   public function testNodeGrantsIdConflicts() {
184     // Enable the node_test module to restrict access to page nodes.
185     $this->enableModules(['node_test']);
186
187     // Create a published node of type page.
188     $node = Node::create(['type' => 'page', 'title' => 'foo']);
189     $node->moderation_state->value = 'published';
190     $node->save();
191
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());
198   }
199
200 }