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 // Test that user reference field values were migrated.
99 $this->assertCount(1, $node->field_commander);
100 $this->assertSame('joe.roe', $node->field_commander[0]->entity->getUsername());
102 $node = Node::load(2);
103 $this->assertIdentical('Test title rev 3', $node->getTitle());
104 $this->assertIdentical('test rev 3', $node->body->value);
105 $this->assertIdentical('filtered_html', $node->body->format);
107 // Test that a link field with an external link is migrated.
108 $this->assertIdentical('http://groups.drupal.org/', $node->field_test_link->uri);
109 $this->assertIdentical('Drupal Groups', $node->field_test_link->title);
110 $this->assertIdentical([], $node->field_test_link->options['attributes']);
112 $node = Node::load(3);
113 // Test multivalue field.
114 $value_1 = $node->field_multivalue->value;
115 $value_2 = $node->field_multivalue[1]->value;
117 // SQLite does not support scales for float data types so we need to convert
118 // the value manually.
119 if ($this->container->get('database')->driver() == 'sqlite') {
120 $value_1 = sprintf('%01.2f', $value_1);
121 $value_2 = sprintf('%01.2f', $value_2);
123 $this->assertSame('33.00', $value_1);
124 $this->assertSame('44.00', $value_2);
126 // Test that a link field with an internal link is migrated.
127 $node = Node::load(9);
128 $this->assertSame('internal:/node/10', $node->field_test_link->uri);
129 $this->assertSame('Buy it now', $node->field_test_link->title);
130 $this->assertSame(['attributes' => ['target' => '_blank']], $node->field_test_link->options);
132 // Test that translations are working.
133 $node = Node::load(10);
134 $this->assertIdentical('en', $node->langcode->value);
135 $this->assertIdentical('The Real McCoy', $node->title->value);
136 $this->assertTrue($node->hasTranslation('fr'), "Node 10 has french translation");
138 // Test that content_translation_source is set.
139 $manager = $this->container->get('content_translation.manager');
140 $this->assertIdentical('en', $manager->getTranslationMetadata($node->getTranslation('fr'))->getSource());
142 // Test that content_translation_source for a source other than English.
143 $node = Node::load(12);
144 $this->assertIdentical('zu', $manager->getTranslationMetadata($node->getTranslation('en'))->getSource());
146 // Node 11 is a translation of node 10, and should not be imported separately.
147 $this->assertNull(Node::load(11), "Node 11 doesn't exist in D8, it was a translation");
149 // Rerun migration with two source database changes.
150 // 1. Add an invalid link attributes and a different URL and
151 // title. If only the attributes are changed the error does not occur.
152 Database::getConnection('default', 'migrate')
153 ->update('content_type_story')
155 'field_test_link_url' => 'https://www.drupal.org/node/2127611',
156 'field_test_link_title' => 'Migrate API in Drupal 8',
157 'field_test_link_attributes' => '',
159 ->condition('nid', '2')
160 ->condition('vid', '3')
163 // 2. Add a leading slash to an internal link.
164 Database::getConnection('default', 'migrate')
165 ->update('content_type_story')
167 'field_test_link_url' => '/node/10',
169 ->condition('nid', '9')
170 ->condition('vid', '12')
173 $this->rerunMigration();
174 $node = Node::load(2);
175 $this->assertIdentical('https://www.drupal.org/node/2127611', $node->field_test_link->uri);
176 $this->assertIdentical('Migrate API in Drupal 8', $node->field_test_link->title);
177 $this->assertIdentical([], $node->field_test_link->options['attributes']);
179 $node = Node::load(9);
180 $this->assertSame('internal:/node/10', $node->field_test_link->uri);
181 $this->assertSame('Buy it now', $node->field_test_link->title);
182 $this->assertSame(['attributes' => ['target' => '_blank']], $node->field_test_link->options);
184 // Test that we can re-import using the EntityContentBase destination.
185 $title = $this->rerunMigration();
186 $node = Node::load(2);
187 $this->assertIdentical($title, $node->getTitle());
188 // Test multi-column fields are correctly upgraded.
189 $this->assertIdentical('test rev 3', $node->body->value);
190 $this->assertIdentical('full_html', $node->body->format);
192 // Now insert a row indicating a failure and set to update later.
193 $title = $this->rerunMigration([
196 'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
198 $node = Node::load(2);
199 $this->assertIdentical($title, $node->getTitle());
203 * Execute the migration a second time.
205 * @param array $new_row
206 * An optional row to be inserted into the id map.
209 * The new title in the source for vid 3.
211 protected function rerunMigration($new_row = []) {
212 $title = $this->randomString();
213 $source_connection = Database::getConnection('default', 'migrate');
214 $source_connection->update('node_revisions')
219 ->condition('vid', 3)
221 $migration = $this->getMigration('d6_node:story');
222 $table_name = $migration->getIdMap()->mapTableName();
223 $default_connection = \Drupal::database();
224 $default_connection->truncate($table_name)->execute();
226 $hash = $migration->getIdMap()->getSourceIDsHash(['nid' => $new_row['sourceid1']]);
227 $new_row['source_ids_hash'] = $hash;
228 $default_connection->insert($table_name)
232 $this->executeMigration($migration);