14c6c31767790bafab7893c1011b6d4230156876
[yaffs-website] / web / core / modules / field / tests / src / Kernel / Migrate / d7 / MigrateFieldTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Kernel\Migrate\d7;
4
5 use Drupal\field\Entity\FieldStorageConfig;
6 use Drupal\field\FieldStorageConfigInterface;
7 use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
8
9 /**
10  * Migrates Drupal 7 fields.
11  *
12  * @group field
13  */
14 class MigrateFieldTest extends MigrateDrupal7TestBase {
15
16   /**
17    * The modules to be enabled during the test.
18    *
19    * @var array
20    */
21   public static $modules = [
22     'comment',
23     'datetime',
24     'file',
25     'image',
26     'link',
27     'node',
28     'system',
29     'taxonomy',
30     'telephone',
31     'text',
32   ];
33
34   /**
35    * {@inheritdoc}
36    */
37   protected function setUp() {
38     parent::setUp();
39     $this->installConfig(static::$modules);
40     $this->executeMigration('d7_field');
41   }
42
43   /**
44    * Asserts various aspects of a field_storage_config entity.
45    *
46    * @param string $id
47    *   The entity ID in the form ENTITY_TYPE.FIELD_NAME.
48    * @param string $expected_type
49    *   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    *   The expected cardinality of the field.
54    */
55   protected function assertEntity($id, $expected_type, $expected_translatable, $expected_cardinality) {
56     list ($expected_entity_type, $expected_name) = explode('.', $id);
57
58     /** @var \Drupal\field\FieldStorageConfigInterface $field */
59     $field = FieldStorageConfig::load($id);
60     $this->assertInstanceOf(FieldStorageConfigInterface::class, $field);
61     $this->assertEquals($expected_name, $field->getName());
62     $this->assertEquals($expected_type, $field->getType());
63     $this->assertEquals($expected_translatable, $field->isTranslatable());
64     $this->assertEquals($expected_entity_type, $field->getTargetEntityTypeId());
65
66     if ($expected_cardinality === 1) {
67       $this->assertFalse($field->isMultiple());
68     }
69     else {
70       $this->assertTrue($field->isMultiple());
71     }
72     $this->assertEquals($expected_cardinality, $field->getCardinality());
73   }
74
75   /**
76    * Tests migrating D7 fields to field_storage_config entities.
77    */
78   public function testFields() {
79     $this->assertEntity('node.body', 'text_with_summary', TRUE, 1);
80     $this->assertEntity('node.field_long_text', 'text_with_summary', TRUE, 1);
81     $this->assertEntity('comment.comment_body', 'text_long', TRUE, 1);
82     $this->assertEntity('node.field_file', 'file', TRUE, 1);
83     $this->assertEntity('user.field_file', 'file', TRUE, 1);
84     $this->assertEntity('node.field_float', 'float', TRUE, 1);
85     $this->assertEntity('node.field_image', 'image', TRUE, 1);
86     $this->assertEntity('node.field_images', 'image', TRUE, 1);
87     $this->assertEntity('node.field_integer', 'integer', TRUE, 1);
88     $this->assertEntity('comment.field_integer', 'integer', TRUE, 1);
89     $this->assertEntity('node.field_integer_list', 'list_integer', TRUE, 1);
90     $this->assertEntity('node.field_link', 'link', TRUE, 1);
91     $this->assertEntity('node.field_tags', 'entity_reference', TRUE, -1);
92     $this->assertEntity('node.field_term_reference', 'entity_reference', TRUE, 1);
93     $this->assertEntity('node.taxonomy_forums', 'entity_reference', TRUE, 1);
94     $this->assertEntity('node.field_text', 'string', TRUE, 1);
95     $this->assertEntity('node.field_text_list', 'list_string', TRUE, 3);
96     $this->assertEntity('node.field_float_list', 'list_float', TRUE, 1);
97     $this->assertEntity('node.field_boolean', 'boolean', TRUE, 1);
98     $this->assertEntity('node.field_email', 'email', TRUE, -1);
99     $this->assertEntity('node.field_phone', 'telephone', TRUE, 1);
100     $this->assertEntity('node.field_date', 'datetime', TRUE, 1);
101     $this->assertEntity('node.field_date_with_end_time', 'timestamp', TRUE, 1);
102     $this->assertEntity('node.field_node_entityreference', 'entity_reference', TRUE, -1);
103     $this->assertEntity('node.field_user_entityreference', 'entity_reference', TRUE, 1);
104     $this->assertEntity('node.field_term_entityreference', 'entity_reference', TRUE, -1);
105     $this->assertEntity('node.field_date_without_time', 'datetime', TRUE, 1);
106     $this->assertEntity('node.field_datetime_without_time', 'datetime', TRUE, 1);
107
108     // Tests that fields created by the Title module are not migrated.
109     $title_field = FieldStorageConfig::load('node.title_field');
110     $this->assertNull($title_field);
111     $subject_field = FieldStorageConfig::load('comment.subject_field');
112     $this->assertNull($subject_field);
113     $name_field = FieldStorageConfig::load('taxonomy_term.name_field');
114     $this->assertNull($name_field);
115     $description_field = FieldStorageConfig::load('taxonomy_term.description_field');
116     $this->assertNull($description_field);
117
118     // Assert that the taxonomy term reference fields are referencing the
119     // correct entity type.
120     $field = FieldStorageConfig::load('node.field_term_reference');
121     $this->assertEquals('taxonomy_term', $field->getSetting('target_type'));
122     $field = FieldStorageConfig::load('node.taxonomy_forums');
123     $this->assertEquals('taxonomy_term', $field->getSetting('target_type'));
124
125     // Assert that the entityreference fields are referencing the correct
126     // entity type.
127     $field = FieldStorageConfig::load('node.field_node_entityreference');
128     $this->assertEquals('node', $field->getSetting('target_type'));
129     $field = FieldStorageConfig::load('node.field_user_entityreference');
130     $this->assertEquals('user', $field->getSetting('target_type'));
131     $field = FieldStorageConfig::load('node.field_term_entityreference');
132     $this->assertEquals('taxonomy_term', $field->getSetting('target_type'));
133
134     // Make sure that datetime fields get the right datetime_type setting
135     $field = FieldStorageConfig::load('node.field_date');
136     $this->assertEquals('datetime', $field->getSetting('datetime_type'));
137     $field = FieldStorageConfig::load('node.field_date_without_time');
138     $this->assertEquals('date', $field->getSetting('datetime_type'));
139     $field = FieldStorageConfig::load('node.field_datetime_without_time');
140     $this->assertEquals('date', $field->getSetting('datetime_type'));
141     // Except for field_date_with_end_time which is a timestamp and so does not
142     // have a datetime_type setting.
143     $field = FieldStorageConfig::load('node.field_date_with_end_time');
144     $this->assertNull($field->getSetting('datetime_type'));
145   }
146
147   /**
148    * Tests the migration of text fields with different text processing.
149    */
150   public function testTextFields() {
151     // All text and text_long field bases that have only plain text instances
152     // should be migrated to string and string_long fields.
153     // All text_with_summary field bases that have only plain text instances
154     // should not have been migrated since there's no such thing as a
155     // string_with_summary field.
156     $this->assertEntity('node.field_text_plain', 'string', TRUE, 1);
157     $this->assertEntity('node.field_text_long_plain', 'string_long', TRUE, 1);
158     $this->assertNull(FieldStorageConfig::load('node.field_text_sum_plain'));
159
160     // All text, text_long and text_with_summary field bases that have only
161     // filtered text instances should be migrated to text, text_long and
162     // text_with_summary fields.
163     $this->assertEntity('node.field_text_filtered', 'text', TRUE, 1);
164     $this->assertEntity('node.field_text_long_filtered', 'text_long', TRUE, 1);
165     $this->assertEntity('node.field_text_sum_filtered', 'text_with_summary', TRUE, 1);
166
167     // All text, text_long and text_with_summary field bases that have both
168     // plain text and filtered text instances should not have been migrated.
169     $this->assertNull(FieldStorageConfig::load('node.field_text_plain_filtered'));
170     $this->assertNull(FieldStorageConfig::load('node.field_text_long_plain_filtered'));
171     $this->assertNull(FieldStorageConfig::load('node.field_text_sum_plain_filtered'));
172
173     // For each text field bases that were skipped, there should be a log
174     // message with the required steps to fix this.
175     $migration = $this->getMigration('d7_field');
176     $messages = $migration->getIdMap()->getMessageIterator()->fetchAll();
177     $errors = array_map(function ($message) {
178       return $message->message;
179     }, $messages);
180     sort($errors);
181     $this->assertCount(4, $errors);
182     $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');
183     $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');
184     $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');
185     $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');
186   }
187
188 }