MigrateFieldTest.php 7.67 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\field\Kernel\Migrate\d7;
4 5 6

use Drupal\field\Entity\FieldStorageConfig;
use Drupal\field\FieldStorageConfigInterface;
7
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
8 9 10 11 12 13 14 15 16 17 18 19 20

/**
 * Migrates Drupal 7 fields.
 *
 * @group field
 */
class MigrateFieldTest extends MigrateDrupal7TestBase {

  /**
   * The modules to be enabled during the test.
   *
   * @var array
   */
21
  public static $modules = [
22 23 24 25 26 27 28 29 30 31
    'comment',
    'datetime',
    'file',
    'image',
    'link',
    'node',
    'system',
    'taxonomy',
    'telephone',
    'text',
32
  ];
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->installConfig(static::$modules);
    $this->executeMigration('d7_field');
  }

  /**
   * Asserts various aspects of a field_storage_config entity.
   *
   * @param string $id
   *   The entity ID in the form ENTITY_TYPE.FIELD_NAME.
   * @param string $expected_type
   *   The expected field type.
50
   * @param bool $expected_translatable
51
   *   Whether or not the field is expected to be translatable.
52
   * @param int $expected_cardinality
53 54 55 56 57 58 59
   *   The expected cardinality of the field.
   */
  protected function assertEntity($id, $expected_type, $expected_translatable, $expected_cardinality) {
    list ($expected_entity_type, $expected_name) = explode('.', $id);

    /** @var \Drupal\field\FieldStorageConfigInterface $field */
    $field = FieldStorageConfig::load($id);
60 61 62 63 64
    $this->assertInstanceOf(FieldStorageConfigInterface::class, $field);
    $this->assertEquals($expected_name, $field->getName());
    $this->assertEquals($expected_type, $field->getType());
    $this->assertEquals($expected_translatable, $field->isTranslatable());
    $this->assertEquals($expected_entity_type, $field->getTargetEntityTypeId());
65 66 67 68 69 70 71

    if ($expected_cardinality === 1) {
      $this->assertFalse($field->isMultiple());
    }
    else {
      $this->assertTrue($field->isMultiple());
    }
72
    $this->assertEquals($expected_cardinality, $field->getCardinality());
73 74 75 76 77 78
  }

  /**
   * Tests migrating D7 fields to field_storage_config entities.
   */
  public function testFields() {
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    $this->assertEntity('node.body', 'text_with_summary', TRUE, 1);
    $this->assertEntity('node.field_long_text', 'text_with_summary', TRUE, 1);
    $this->assertEntity('comment.comment_body', 'text_long', TRUE, 1);
    $this->assertEntity('node.field_file', 'file', TRUE, 1);
    $this->assertEntity('user.field_file', 'file', TRUE, 1);
    $this->assertEntity('node.field_float', 'float', TRUE, 1);
    $this->assertEntity('node.field_image', 'image', TRUE, 1);
    $this->assertEntity('node.field_images', 'image', TRUE, 1);
    $this->assertEntity('node.field_integer', 'integer', TRUE, 1);
    $this->assertEntity('comment.field_integer', 'integer', TRUE, 1);
    $this->assertEntity('node.field_integer_list', 'list_integer', TRUE, 1);
    $this->assertEntity('node.field_link', 'link', TRUE, 1);
    $this->assertEntity('node.field_tags', 'entity_reference', TRUE, -1);
    $this->assertEntity('node.field_term_reference', 'entity_reference', TRUE, 1);
    $this->assertEntity('node.taxonomy_forums', 'entity_reference', TRUE, 1);
94
    $this->assertEntity('node.field_text', 'string', TRUE, 1);
95 96 97 98 99
    $this->assertEntity('node.field_text_list', 'list_string', TRUE, 3);
    $this->assertEntity('node.field_boolean', 'boolean', TRUE, 1);
    $this->assertEntity('node.field_email', 'email', TRUE, -1);
    $this->assertEntity('node.field_phone', 'telephone', TRUE, 1);
    $this->assertEntity('node.field_date', 'datetime', TRUE, 1);
100
    $this->assertEntity('node.field_date_with_end_time', 'timestamp', TRUE, 1);
101 102 103
    $this->assertEntity('node.field_node_entityreference', 'entity_reference', TRUE, -1);
    $this->assertEntity('node.field_user_entityreference', 'entity_reference', TRUE, 1);
    $this->assertEntity('node.field_term_entityreference', 'entity_reference', TRUE, -1);
104 105 106 107

    // Assert that the taxonomy term reference fields are referencing the
    // correct entity type.
    $field = FieldStorageConfig::load('node.field_term_reference');
108
    $this->assertEquals('taxonomy_term', $field->getSetting('target_type'));
109
    $field = FieldStorageConfig::load('node.taxonomy_forums');
110
    $this->assertEquals('taxonomy_term', $field->getSetting('target_type'));
111

112 113 114
    // Assert that the entityreference fields are referencing the correct
    // entity type.
    $field = FieldStorageConfig::load('node.field_node_entityreference');
115
    $this->assertEquals('node', $field->getSetting('target_type'));
116
    $field = FieldStorageConfig::load('node.field_user_entityreference');
117
    $this->assertEquals('user', $field->getSetting('target_type'));
118
    $field = FieldStorageConfig::load('node.field_term_entityreference');
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
    $this->assertEquals('taxonomy_term', $field->getSetting('target_type'));
  }

  /**
   * Tests the migration of text fields with different text processing.
   */
  public function testTextFields() {
    // All text and text_long field bases that have only plain text instances
    // should be migrated to string and string_long fields.
    // All text_with_summary field bases that have only plain text instances
    // should not have been migrated since there's no such thing as a
    // string_with_summary field.
    $this->assertEntity('node.field_text_plain', 'string', TRUE, 1);
    $this->assertEntity('node.field_text_long_plain', 'string_long', TRUE, 1);
    $this->assertNull(FieldStorageConfig::load('node.field_text_sum_plain'));

    // All text, text_long and text_with_summary field bases that have only
    // filtered text instances should be migrated to text, text_long and
    // text_with_summary fields.
    $this->assertEntity('node.field_text_filtered', 'text', TRUE, 1);
    $this->assertEntity('node.field_text_long_filtered', 'text_long', TRUE, 1);
    $this->assertEntity('node.field_text_sum_filtered', 'text_with_summary', TRUE, 1);

    // All text, text_long and text_with_summary field bases that have both
    // plain text and filtered text instances should not have been migrated.
    $this->assertNull(FieldStorageConfig::load('node.field_text_plain_filtered'));
    $this->assertNull(FieldStorageConfig::load('node.field_text_long_plain_filtered'));
    $this->assertNull(FieldStorageConfig::load('node.field_text_sum_plain_filtered'));
147

148 149
    // For each text field bases that were skipped, there should be a log
    // message with the required steps to fix this.
150
    $migration = $this->getMigration('d7_field');
151
    $messages = $migration->getIdMap()->getMessageIterator()->fetchAll();
152
    $errors = array_map(function ($message) {
153 154
      return $message->message;
    }, $messages);
155 156 157 158 159 160
    sort($errors);
    $this->assertCount(4, $errors);
    $this->assertEquals($errors[0], 'Can\'t migrate source field field_text_long_plain_filtered configured with both plain text and filtered text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text');
    $this->assertEquals($errors[1], 'Can\'t migrate source field field_text_plain_filtered configured with both plain text and filtered text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text');
    $this->assertEquals($errors[2], 'Can\'t migrate source field field_text_sum_plain of type text_with_summary configured with plain text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text');
    $this->assertEquals($errors[3], 'Can\'t migrate source field field_text_sum_plain_filtered of type text_with_summary configured with plain text processing. See https://www.drupal.org/docs/8/upgrade/known-issues-when-upgrading-from-drupal-6-or-7-to-drupal-8#plain-text');
161 162 163
  }

}