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 = ['language', 'content_translation', 'menu_ui'];
27 protected function setUp() {
29 $this->setUpMigratedFiles();
30 $this->installSchema('file', ['file_usage']);
31 $this->executeMigrations([
33 'd6_language_content_settings',
35 'd6_node_translation',
40 * Test node migration from Drupal 6 to 8.
42 public function testNode() {
43 $node = Node::load(1);
44 $this->assertIdentical('1', $node->id(), 'Node 1 loaded.');
45 $this->assertIdentical('und', $node->langcode->value);
46 $this->assertIdentical('test', $node->body->value);
47 $this->assertIdentical('test', $node->body->summary);
48 $this->assertIdentical('filtered_html', $node->body->format);
49 $this->assertIdentical('story', $node->getType(), 'Node has the correct bundle.');
50 $this->assertIdentical('Test title', $node->getTitle(), 'Node has the correct title.');
51 $this->assertIdentical('1388271197', $node->getCreatedTime(), 'Node has the correct created time.');
52 $this->assertIdentical(FALSE, $node->isSticky());
53 $this->assertIdentical('1', $node->getOwnerId());
54 $this->assertIdentical('1420861423', $node->getRevisionCreationTime());
56 /** @var \Drupal\node\NodeInterface $node_revision */
57 $node_revision = \Drupal::entityManager()->getStorage('node')->loadRevision(1);
58 $this->assertIdentical('Test title', $node_revision->getTitle());
59 $this->assertIdentical('1', $node_revision->getRevisionUser()->id(), 'Node revision has the correct user');
60 $this->assertSame('1', $node_revision->id(), 'Node 1 loaded.');
61 $this->assertSame('1', $node_revision->getRevisionId(), 'Node 1 revision 1 loaded.');
62 // This is empty on the first revision.
63 $this->assertIdentical(NULL, $node_revision->revision_log->value);
64 $this->assertIdentical('This is a shared text field', $node->field_test->value);
65 $this->assertIdentical('filtered_html', $node->field_test->format);
66 $this->assertIdentical('10', $node->field_test_two->value);
67 $this->assertIdentical('20', $node->field_test_two[1]->value);
69 $this->assertIdentical('42.42', $node->field_test_three->value, 'Single field second value is correct.');
70 $this->assertIdentical('3412', $node->field_test_integer_selectlist[0]->value);
71 $this->assertIdentical('1', $node->field_test_identical1->value, 'Integer value is correct');
72 $this->assertIdentical('1', $node->field_test_identical2->value, 'Integer value is correct');
73 $this->assertIdentical('This is a field with exclude unset.', $node->field_test_exclude_unset->value, 'Field with exclude unset is correct.');
75 // Test that date fields are migrated.
76 $this->assertSame('2013-01-02T04:05:00', $node->field_test_date->value, 'Date field is correct');
77 $this->assertSame('1391357160', $node->field_test_datestamp->value, 'Datestamp field is correct');
78 $this->assertSame('2015-03-04T06:07:00', $node->field_test_datetime->value, 'Datetime field is correct');
80 // Test that link fields are migrated.
81 $this->assertIdentical('https://www.drupal.org/project/drupal', $node->field_test_link->uri);
82 $this->assertIdentical('Drupal project page', $node->field_test_link->title);
83 $this->assertIdentical(['target' => '_blank'], $node->field_test_link->options['attributes']);
85 // Test the file field meta.
86 $this->assertIdentical('desc', $node->field_test_filefield->description);
87 $this->assertIdentical('4', $node->field_test_filefield->target_id);
89 // Test that an email field is migrated.
90 $this->assertSame('PrincessRuwenne@example.com', $node->field_test_email->value);
92 // Test that node reference field values were migrated.
93 $node = Node::load(18);
94 $this->assertCount(2, $node->field_company);
95 $this->assertSame('Klingon Empire', $node->field_company[0]->entity->label());
96 $this->assertSame('Romulan Empire', $node->field_company[1]->entity->label());
98 $node = Node::load(2);
99 $this->assertIdentical('Test title rev 3', $node->getTitle());
100 $this->assertIdentical('test rev 3', $node->body->value);
101 $this->assertIdentical('filtered_html', $node->body->format);
103 // Test that a link field with an external link is migrated.
104 $this->assertIdentical('http://groups.drupal.org/', $node->field_test_link->uri);
105 $this->assertIdentical('Drupal Groups', $node->field_test_link->title);
106 $this->assertIdentical([], $node->field_test_link->options['attributes']);
108 $node = Node::load(3);
109 // Test multivalue field.
110 $value_1 = $node->field_multivalue->value;
111 $value_2 = $node->field_multivalue[1]->value;
113 // SQLite does not support scales for float data types so we need to convert
114 // the value manually.
115 if ($this->container->get('database')->driver() == 'sqlite') {
116 $value_1 = sprintf('%01.2f', $value_1);
117 $value_2 = sprintf('%01.2f', $value_2);
119 $this->assertSame('33.00', $value_1);
120 $this->assertSame('44.00', $value_2);
122 // Test that a link field with an internal link is migrated.
123 $node = Node::load(9);
124 $this->assertSame('internal:/node/10', $node->field_test_link->uri);
125 $this->assertSame('Buy it now', $node->field_test_link->title);
126 $this->assertSame(['attributes' => ['target' => '_blank']], $node->field_test_link->options);
128 // Test that translations are working.
129 $node = Node::load(10);
130 $this->assertIdentical('en', $node->langcode->value);
131 $this->assertIdentical('The Real McCoy', $node->title->value);
132 $this->assertTrue($node->hasTranslation('fr'), "Node 10 has french translation");
134 // Test that content_translation_source is set.
135 $manager = $this->container->get('content_translation.manager');
136 $this->assertIdentical('en', $manager->getTranslationMetadata($node->getTranslation('fr'))->getSource());
138 // Test that content_translation_source for a source other than English.
139 $node = Node::load(12);
140 $this->assertIdentical('zu', $manager->getTranslationMetadata($node->getTranslation('en'))->getSource());
142 // Node 11 is a translation of node 10, and should not be imported separately.
143 $this->assertNull(Node::load(11), "Node 11 doesn't exist in D8, it was a translation");
145 // Rerun migration with two source database changes.
146 // 1. Add an invalid link attributes and a different URL and
147 // title. If only the attributes are changed the error does not occur.
148 Database::getConnection('default', 'migrate')
149 ->update('content_type_story')
151 'field_test_link_url' => 'https://www.drupal.org/node/2127611',
152 'field_test_link_title' => 'Migrate API in Drupal 8',
153 'field_test_link_attributes' => '',
155 ->condition('nid', '2')
156 ->condition('vid', '3')
159 // 2. Add a leading slash to an internal link.
160 Database::getConnection('default', 'migrate')
161 ->update('content_type_story')
163 'field_test_link_url' => '/node/10',
165 ->condition('nid', '9')
166 ->condition('vid', '12')
169 $this->rerunMigration();
170 $node = Node::load(2);
171 $this->assertIdentical('https://www.drupal.org/node/2127611', $node->field_test_link->uri);
172 $this->assertIdentical('Migrate API in Drupal 8', $node->field_test_link->title);
173 $this->assertIdentical([], $node->field_test_link->options['attributes']);
175 $node = Node::load(9);
176 $this->assertSame('internal:/node/10', $node->field_test_link->uri);
177 $this->assertSame('Buy it now', $node->field_test_link->title);
178 $this->assertSame(['attributes' => ['target' => '_blank']], $node->field_test_link->options);
180 // Test that we can re-import using the EntityContentBase destination.
181 $title = $this->rerunMigration();
182 $node = Node::load(2);
183 $this->assertIdentical($title, $node->getTitle());
184 // Test multi-column fields are correctly upgraded.
185 $this->assertIdentical('test rev 3', $node->body->value);
186 $this->assertIdentical('full_html', $node->body->format);
188 // Now insert a row indicating a failure and set to update later.
189 $title = $this->rerunMigration([
192 'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
194 $node = Node::load(2);
195 $this->assertIdentical($title, $node->getTitle());
199 * Execute the migration a second time.
201 * @param array $new_row
202 * An optional row to be inserted into the id map.
205 * The new title in the source for vid 3.
207 protected function rerunMigration($new_row = []) {
208 $title = $this->randomString();
209 $source_connection = Database::getConnection('default', 'migrate');
210 $source_connection->update('node_revisions')
215 ->condition('vid', 3)
217 $migration = $this->getMigration('d6_node:story');
218 $table_name = $migration->getIdMap()->mapTableName();
219 $default_connection = \Drupal::database();
220 $default_connection->truncate($table_name)->execute();
222 $hash = $migration->getIdMap()->getSourceIDsHash(['nid' => $new_row['sourceid1']]);
223 $new_row['source_ids_hash'] = $hash;
224 $default_connection->insert($table_name)
228 $this->executeMigration($migration);