MigrateNodeTest.php 8.92 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\node\Kernel\Migrate\d6;
4 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;
9 10

/**
11 12
 * Node content migration.
 *
13
 * @group migrate_drupal_6
14 15 16
 */
class MigrateNodeTest extends MigrateNodeTestBase {

17 18
  use FileMigrationTestTrait;

19 20 21
  /**
   * {@inheritdoc}
   */
22
  public static $modules = ['language', 'content_translation', 'menu_ui'];
23

24 25 26
  /**
   * {@inheritdoc}
   */
27
  protected function setUp() {
28
    parent::setUp();
29 30
    $this->setUpMigratedFiles();
    $this->installSchema('file', ['file_usage']);
31 32 33 34 35 36
    $this->executeMigrations([
      'language',
      'd6_language_content_settings',
      'd6_node',
      'd6_node_translation',
    ]);
37 38 39 40 41 42
  }

  /**
   * Test node migration from Drupal 6 to 8.
   */
  public function testNode() {
43
    $node = Node::load(1);
44 45 46 47 48 49 50 51 52 53 54
    $this->assertIdentical('1', $node->id(), 'Node 1 loaded.');
    $this->assertIdentical('und', $node->langcode->value);
    $this->assertIdentical('test', $node->body->value);
    $this->assertIdentical('test', $node->body->summary);
    $this->assertIdentical('filtered_html', $node->body->format);
    $this->assertIdentical('story', $node->getType(), 'Node has the correct bundle.');
    $this->assertIdentical('Test title', $node->getTitle(), 'Node has the correct title.');
    $this->assertIdentical('1388271197', $node->getCreatedTime(), 'Node has the correct created time.');
    $this->assertIdentical(FALSE, $node->isSticky());
    $this->assertIdentical('1', $node->getOwnerId());
    $this->assertIdentical('1420861423', $node->getRevisionCreationTime());
55

56 57
    /** @var \Drupal\node\NodeInterface $node_revision */
    $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 61
    $this->assertSame('1', $node_revision->id(), 'Node 1 loaded.');
    $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 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
    $this->assertIdentical('This is a shared text field', $node->field_test->value);
    $this->assertIdentical('filtered_html', $node->field_test->format);
    $this->assertIdentical('10', $node->field_test_two->value);
    $this->assertIdentical('20', $node->field_test_two[1]->value);

    $this->assertIdentical('42.42', $node->field_test_three->value, 'Single field second value is correct.');
    $this->assertIdentical('3412', $node->field_test_integer_selectlist[0]->value);
    $this->assertIdentical('1', $node->field_test_identical1->value, 'Integer value is correct');
    $this->assertIdentical('1', $node->field_test_identical2->value, 'Integer value is correct');
    $this->assertIdentical('This is a field with exclude unset.', $node->field_test_exclude_unset->value, 'Field with exclude unset is correct.');

    // Test that link fields are migrated.
    $this->assertIdentical('https://www.drupal.org/project/drupal', $node->field_test_link->uri);
    $this->assertIdentical('Drupal project page', $node->field_test_link->title);
    $this->assertIdentical(['target' => '_blank'], $node->field_test_link->options['attributes']);

    // Test the file field meta.
    $this->assertIdentical('desc', $node->field_test_filefield->description);
82
    $this->assertIdentical('4', $node->field_test_filefield->target_id);
83

84 85 86 87 88
    $node = Node::load(2);
    $this->assertIdentical('Test title rev 3', $node->getTitle());
    $this->assertIdentical('test rev 3', $node->body->value);
    $this->assertIdentical('filtered_html', $node->body->format);

89
    // Test that a link field with an external link is migrated.
90 91 92 93
    $this->assertIdentical('http://groups.drupal.org/', $node->field_test_link->uri);
    $this->assertIdentical('Drupal Groups', $node->field_test_link->title);
    $this->assertIdentical([], $node->field_test_link->options['attributes']);

94 95 96 97 98 99 100 101 102 103 104 105 106 107
    $node = Node::load(3);
    // Test multivalue field.
    $value_1 = $node->field_multivalue->value;
    $value_2 = $node->field_multivalue[1]->value;

    // SQLite does not support scales for float data types so we need to convert
    // the value manually.
    if ($this->container->get('database')->driver() == 'sqlite') {
      $value_1 = sprintf('%01.2f', $value_1);
      $value_2 = sprintf('%01.2f', $value_2);
    }
    $this->assertSame('33.00', $value_1);
    $this->assertSame('44.00', $value_2);

108 109 110 111 112 113
    // Test that a link field with an internal link is migrated.
    $node = Node::load(9);
    $this->assertSame('internal:/node/10', $node->field_test_link->uri);
    $this->assertSame('Buy it now', $node->field_test_link->title);
    $this->assertSame(['attributes' => ['target' => '_blank']], $node->field_test_link->options);

114 115 116 117 118 119
    // Test that translations are working.
    $node = Node::load(10);
    $this->assertIdentical('en', $node->langcode->value);
    $this->assertIdentical('The Real McCoy', $node->title->value);
    $this->assertTrue($node->hasTranslation('fr'), "Node 10 has french translation");

120 121 122 123 124 125 126 127
    // Test that content_translation_source is set.
    $manager = $this->container->get('content_translation.manager');
    $this->assertIdentical('en', $manager->getTranslationMetadata($node->getTranslation('fr'))->getSource());

    // Test that content_translation_source for a source other than English.
    $node = Node::load(12);
    $this->assertIdentical('zu', $manager->getTranslationMetadata($node->getTranslation('en'))->getSource());

128 129 130
    // Node 11 is a translation of node 10, and should not be imported separately.
    $this->assertNull(Node::load(11), "Node 11 doesn't exist in D8, it was a translation");

131 132
    // Rerun migration with two source database changes.
    // 1. Add an invalid link attributes and a different URL and
133 134 135 136 137 138 139 140 141 142 143 144
    // title. If only the attributes are changed the error does not occur.
    Database::getConnection('default', 'migrate')
      ->update('content_type_story')
      ->fields([
        'field_test_link_url' => 'https://www.drupal.org/node/2127611',
        'field_test_link_title' => 'Migrate API in Drupal 8',
        'field_test_link_attributes' => '',
      ])
      ->condition('nid', '2')
      ->condition('vid', '3')
      ->execute();

145 146 147 148 149 150 151 152 153 154
    // 2. Add a leading slash to an internal link.
    Database::getConnection('default', 'migrate')
      ->update('content_type_story')
      ->fields([
        'field_test_link_url' => '/node/10',
      ])
      ->condition('nid', '9')
      ->condition('vid', '12')
      ->execute();

155 156 157 158 159 160
    $this->rerunMigration();
    $node = Node::load(2);
    $this->assertIdentical('https://www.drupal.org/node/2127611', $node->field_test_link->uri);
    $this->assertIdentical('Migrate API in Drupal 8', $node->field_test_link->title);
    $this->assertIdentical([], $node->field_test_link->options['attributes']);

161 162 163 164 165
    $node = Node::load(9);
    $this->assertSame('internal:/node/10', $node->field_test_link->uri);
    $this->assertSame('Buy it now', $node->field_test_link->title);
    $this->assertSame(['attributes' => ['target' => '_blank']], $node->field_test_link->options);

166
    // Test that we can re-import using the EntityContentBase destination.
167 168 169 170 171 172 173 174
    $title = $this->rerunMigration();
    $node = Node::load(2);
    $this->assertIdentical($title, $node->getTitle());
    // Test multi-column fields are correctly upgraded.
    $this->assertIdentical('test rev 3', $node->body->value);
    $this->assertIdentical('full_html', $node->body->format);

    // Now insert a row indicating a failure and set to update later.
175
    $title = $this->rerunMigration([
176 177 178
      'sourceid1' => 2,
      'destid1' => NULL,
      'source_row_status' => MigrateIdMapInterface::STATUS_NEEDS_UPDATE,
179
    ]);
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
    $node = Node::load(2);
    $this->assertIdentical($title, $node->getTitle());
  }

  /**
   * Execute the migration a second time.
   *
   * @param array $new_row
   *   An optional row to be inserted into the id map.
   *
   * @return string
   *   The new title in the source for vid 3.
   */
  protected function rerunMigration($new_row = []) {
    $title = $this->randomString();
    $source_connection = Database::getConnection('default', 'migrate');
    $source_connection->update('node_revisions')
197
      ->fields([
198
        'title' => $title,
199
        'format' => 2,
200
      ])
201
      ->condition('vid', 3)
202
      ->execute();
203
    $migration = $this->getMigration('d6_node:story');
204 205 206 207
    $table_name = $migration->getIdMap()->mapTableName();
    $default_connection = \Drupal::database();
    $default_connection->truncate($table_name)->execute();
    if ($new_row) {
208 209
      $hash = $migration->getIdMap()->getSourceIDsHash(['nid' => $new_row['sourceid1']]);
      $new_row['source_ids_hash'] = $hash;
210 211 212 213
      $default_connection->insert($table_name)
        ->fields($new_row)
        ->execute();
    }
214
    $this->executeMigration($migration);
215
    return $title;
216 217 218
  }

}