PageRenderTime 27ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/web/core/modules/node/tests/src/Kernel/Migrate/d6/MigrateNodeTest.php

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