3 namespace Drupal\Tests\node\Kernel\Migrate\d6;
5 use Drupal\Core\Database\Database;
6 use Drupal\migrate\Plugin\MigrateIdMapInterface;
7 use Drupal\node\Entity\Node;
8 use Drupal\Tests\file\Kernel\Migrate\d6\FileMigrationTestTrait;
11 * Node content migration.
13 * @group migrate_drupal_6
15 class MigrateNodeTest extends MigrateNodeTestBase {
17 use FileMigrationTestTrait;
22 public static $modules = [
24 'content_translation',
26 // Required for translation migrations.
27 'migrate_drupal_multilingual',
33 protected function setUp() {
35 $this->setUpMigratedFiles();
36 $this->installSchema('file', ['file_usage']);
37 $this->executeMigrations([
39 'd6_language_content_settings',
41 'd6_node_translation',
46 * Test node migration from Drupal 6 to 8.
48 public function testNode() {
49 $node = Node::load(1);
50 $this->assertIdentical('1', $node->id(), 'Node 1 loaded.');
51 $this->assertIdentical('und', $node->langcode->value);
52 $this->assertIdentical('test', $node->body->value);
53 $this->assertIdentical('test', $node->body->summary);
54 $this->assertIdentical('filtered_html', $node->body->format);
55 $this->assertIdentical('story', $node->getType(), 'Node has the correct bundle.');
56 $this->assertIdentical('Test title', $node->getTitle(), 'Node has the correct title.');
57 $this->assertIdentical('1388271197', $node->getCreatedTime(), 'Node has the correct created time.');
58 $this->assertIdentical(FALSE, $node->isSticky());
59 $this->assertIdentical('1', $node->getOwnerId());
60 $this->assertIdentical('1420861423', $node->getRevisionCreationTime());
62 /** @var \Drupal\node\NodeInterface $node_revision */
63 $node_revision = \Drupal::entityManager()->getStorage('node')->loadRevision(1);
64 $this->assertIdentical('Test title', $node_revision->getTitle());
65 $this->assertIdentical('1', $node_revision->getRevisionUser()->id(), 'Node revision has the correct user');
66 $this->assertSame('1', $node_revision->id(), 'Node 1 loaded.');
67 $this->assertSame('1', $node_revision->getRevisionId(), 'Node 1 revision 1 loaded.');
68 // This is empty on the first revision.
69 $this->assertIdentical(NULL, $node_revision->revision_log->value);
70 $this->assertIdentical('This is a shared text field', $node->field_test->value);
71 $this->assertIdentical('filtered_html', $node->field_test->format);
72 $this->assertIdentical('10', $node->field_test_two->value);
73 $this->assertIdentical('20', $node->field_test_two[1]->value);
75 $this->assertIdentical('42.42', $node->field_test_three->value, 'Single field second value is correct.');
76 $this->assertIdentical('3412', $node->field_test_integer_selectlist[0]->value);
77 $this->assertIdentical('1', $node->field_test_identical1->value, 'Integer value is correct');
78 $this->assertIdentical('1', $node->field_test_identical2->value, 'Integer value is correct');
79 $this->assertIdentical('This is a field with exclude unset.', $node->field_test_exclude_unset->value, 'Field with exclude unset is correct.');
81 // Test that date fields are migrated.
82 $this->assertSame('2013-01-02T04:05:00', $node->field_test_date->value, 'Date field is correct');
83 $this->assertSame('1391357160', $node->field_test_datestamp->value, 'Datestamp field is correct');
84 $this->assertSame('2015-03-04T06:07:00', $node->field_test_datetime->value, 'Datetime field is correct');
86 // Test that link fields are migrated.
87 $this->assertIdentical('https://www.drupal.org/project/drupal', $node->field_test_link->uri);
88 $this->assertIdentical('Drupal project page', $node->field_test_link->title);
89 $this->assertIdentical(['target' => '_blank'], $node->field_test_link->options['attributes']);
91 // Test the file field meta.
92 $this->assertIdentical('desc', $node->field_test_filefield->description);
93 $this->assertIdentical('4', $node->field_test_filefield->target_id);
95 // Test that an email field is migrated.
96 $this->assertSame('PrincessRuwenne@example.com', $node->field_test_email->value);
98 // Test that node reference field values were migrated.
99 $node = Node::load(18);
100 $this->assertCount(2, $node->field_company);
101 $this->assertSame('Klingon Empire', $node->field_company[0]->entity->label());
102 $this->assertSame('Romulan Empire', $node->field_company[1]->entity->label());
103 $this->assertCount(1, $node->field_company_2);
104 $this->assertSame('Klingon Empire', $node->field_company_2[0]->entity->label());
105 $this->assertCount(1, $node->field_company_3);
106 $this->assertSame('Romulan Empire', $node->field_company_3[0]->entity->label());
108 // Test that user reference field values were migrated.
109 $this->assertCount(1, $node->field_commander);
110 $this->assertSame('joe.roe', $node->field_commander[0]->entity->getUsername());
112 $node = Node::load(2);
113 $this->assertIdentical('Test title rev 3', $node->getTitle());
114 $this->assertIdentical('test rev 3', $node->body->value);
115 $this->assertIdentical('filtered_html', $node->body->format);
117 // Test that a link field with an external link is migrated.
118 $this->assertIdentical('http://groups.drupal.org/', $node->field_test_link->uri);
119 $this->assertIdentical('Drupal Groups', $node->field_test_link->title);
120 $this->assertIdentical([], $node->field_test_link->options['attributes']);
122 $node = Node::load(3);
123 // Test multivalue field.
124 $value_1 = $node->field_multivalue->value;
125 $value_2 = $node->field_multivalue[1]->value;
127 // SQLite does not support scales for float data types so we need to convert
128 // the value manually.
129 if ($this->container->get('database')->driver() == 'sqlite') {
130 $value_1 = sprintf('%01.2f', $value_1);
131 $value_2 = sprintf('%01.2f', $value_2);
133 $this->assertSame('33.00', $value_1);
134 $this->assertSame('44.00', $value_2);
136 // Test that a link field with an internal link is migrated.
137 $node = Node::load(9);
138 $this->assertSame('internal:/node/10', $node->field_test_link->uri);
139 $this->assertSame('Buy it now', $node->field_test_link->title);
140 $this->assertSame(['attributes' => ['target' => '_blank']], $node->field_test_link->options);
142 // Test that translations are working.
143 $node = Node::load(10);
144 $this->assertIdentical('en', $node->langcode->value);
145 $this->assertIdentical('The Real McCoy', $node->title->value);
146 $this->assertTrue($node->hasTranslation('fr'), "Node 10 has french translation");
148 // Test that content_translation_source is set.
149 $manager = $this->container->get('content_translation.manager');
150 $this->assertIdentical('en', $manager->getTranslationMetadata($node->getTranslation('fr'))->getSource());
152 // Test that content_translation_source for a source other than English.
153 $node = Node::load(12);
154 $this->assertIdentical('zu', $manager->getTranslationMetadata($node->getTranslation('en'))->getSource());
156 // Node 11 is a translation of node 10, and should not be imported separately.
157 $this->assertNull(Node::load(11), "Node 11 doesn't exist in D8, it was a translation");
159 // Rerun migration with two source database changes.
160 // 1. Add an invalid link attributes and a different URL and
161 // title. If only the attributes are changed the error does not occur.
162 Database::getConnection('default', 'migrate')
163 ->update('content_type_story')
165 'field_test_link_url' => 'https://www.drupal.org/node/2127611',
166 'field_test_link_title' => 'Migrate API in Drupal 8',
167 'field_test_link_attributes' => '',
169 ->condition('nid', '2')
170 ->condition('vid', '3')
173 // 2. Add a leading slash to an internal link.
174 Database::getConnection('default', 'migrate')
175 ->update('content_type_story')
177 'field_test_link_url' => '/node/10',
179 ->condition('nid', '9')
180 ->condition('vid', '12')
183 $this->rerunMigration();
184 $node = Node::load(2);
185 $this->assertIdentical('https://www.drupal.org/node/2127611', $node->field_test_link->uri);
186 $this->assertIdentical('Migrate API in Drupal 8', $node->field_test_link->title);
187 $this->assertIdentical([], $node->field_test_link->options['attributes']);
189 $node = Node::load(9);
190 $this->assertSame('internal:/node/10', $node->field_test_link->uri);
191 $this->assertSame('Buy it now', $node->field_test_link->title);
192 $this->assertSame(['attributes' => ['target' => '_blank']], $node->field_test_link->options);
194 // Test that we can re-import using the EntityContentBase destination.
195 $title = $this->rerunMigration();
196 $node = Node::load(2);
197 $this->assertIdentical($title, $node->getTitle());
198 // Test multi-column fields are correctly upgraded.
199 $this->assertIdentical('test rev 3', $node->body->value);
200 $this->assertIdentical('full_html', $node->body->format);
202 // Now insert a row indicating a failure and set to update later.
203 $title = $this->rerunMigration([
206 'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
208 $node = Node::load(2);
209 $this->assertIdentical($title, $node->getTitle());
213 * Execute the migration a second time.
215 * @param array $new_row
216 * An optional row to be inserted into the id map.
219 * The new title in the source for vid 3.
221 protected function rerunMigration($new_row = []) {
222 $title = $this->randomString();
223 $source_connection = Database::getConnection('default', 'migrate');
224 $source_connection->update('node_revisions')
229 ->condition('vid', 3)
231 $migration = $this->getMigration('d6_node:story');
232 $table_name = $migration->getIdMap()->mapTableName();
233 $default_connection = \Drupal::database();
234 $default_connection->truncate($table_name)->execute();
236 $hash = $migration->getIdMap()->getSourceIdsHash(['nid' => $new_row['sourceid1']]);
237 $new_row['source_ids_hash'] = $hash;
238 $default_connection->insert($table_name)
242 $this->executeMigration($migration);