Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / field / tests / src / Kernel / Migrate / d7 / MigrateFieldInstanceTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Kernel\Migrate\d7;
4
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\FieldConfigInterface;
7 use Drupal\taxonomy\Entity\Vocabulary;
8 use Drupal\Tests\migrate\Kernel\NodeCommentCombinationTrait;
9 use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
10
11 /**
12  * Migrates Drupal 7 field instances.
13  *
14  * @group field
15  */
16 class MigrateFieldInstanceTest extends MigrateDrupal7TestBase {
17
18   use NodeCommentCombinationTrait;
19
20   /**
21    * {@inheritdoc}
22    */
23   public static $modules = [
24     'comment',
25     'datetime',
26     'file',
27     'image',
28     'link',
29     'node',
30     'system',
31     'taxonomy',
32     'telephone',
33     'text',
34   ];
35
36   /**
37    * {@inheritdoc}
38    */
39   protected function setUp() {
40     parent::setUp();
41     $this->installConfig(static::$modules);
42     $this->createNodeCommentCombination('page');
43     $this->createNodeCommentCombination('article');
44     $this->createNodeCommentCombination('blog');
45     $this->createNodeCommentCombination('book');
46     $this->createNodeCommentCombination('forum', 'comment_forum');
47     $this->createNodeCommentCombination('test_content_type');
48     Vocabulary::create(['vid' => 'test_vocabulary'])->save();
49     $this->executeMigrations(['d7_field', 'd7_field_instance']);
50   }
51
52   /**
53    * Asserts various aspects of a field config entity.
54    *
55    * @param string $id
56    *   The entity ID in the form ENTITY_TYPE.BUNDLE.FIELD_NAME.
57    * @param string $expected_label
58    *   The expected field label.
59    * @param string $expected_field_type
60    *   The expected field type.
61    * @param bool $is_required
62    *   Whether or not the field is required.
63    * @param bool $expected_translatable
64    *   Whether or not the field is expected to be translatable.
65    */
66   protected function assertEntity($id, $expected_label, $expected_field_type, $is_required, $expected_translatable) {
67     list ($expected_entity_type, $expected_bundle, $expected_name) = explode('.', $id);
68
69     /** @var \Drupal\field\FieldConfigInterface $field */
70     $field = FieldConfig::load($id);
71     $this->assertInstanceOf(FieldConfigInterface::class, $field);
72     $this->assertEquals($expected_label, $field->label());
73     $this->assertEquals($expected_field_type, $field->getType());
74     $this->assertEquals($expected_entity_type, $field->getTargetEntityTypeId());
75     $this->assertEquals($expected_bundle, $field->getTargetBundle());
76     $this->assertEquals($expected_name, $field->getName());
77     $this->assertEquals($is_required, $field->isRequired());
78     $this->assertEquals($expected_entity_type . '.' . $expected_name, $field->getFieldStorageDefinition()->id());
79     $this->assertEquals($expected_translatable, $field->isTranslatable());
80   }
81
82   /**
83    * Asserts the settings of a link field config entity.
84    *
85    * @param $id
86    *   The entity ID in the form ENTITY_TYPE.BUNDLE.FIELD_NAME.
87    * @param $title_setting
88    *   The expected title setting.
89    */
90   protected function assertLinkFields($id, $title_setting) {
91     $field = FieldConfig::load($id);
92     $this->assertSame($title_setting, $field->getSetting('title'));
93   }
94
95   /**
96    * Tests migrating D7 field instances to field_config entities.
97    */
98   public function testFieldInstances() {
99     $this->assertEntity('comment.comment_node_page.comment_body', 'Comment', 'text_long', TRUE, FALSE);
100     $this->assertEntity('node.page.body', 'Body', 'text_with_summary', FALSE, FALSE);
101     $this->assertEntity('comment.comment_node_article.comment_body', 'Comment', 'text_long', TRUE, FALSE);
102     $this->assertEntity('node.article.body', 'Body', 'text_with_summary', FALSE, TRUE);
103     $this->assertEntity('node.article.field_tags', 'Tags', 'entity_reference', FALSE, TRUE);
104     $this->assertEntity('node.article.field_image', 'Image', 'image', FALSE, TRUE);
105     $this->assertEntity('comment.comment_node_blog.comment_body', 'Comment', 'text_long', TRUE, FALSE);
106     $this->assertEntity('node.blog.body', 'Body', 'text_with_summary', FALSE, TRUE);
107     $this->assertEntity('comment.comment_node_book.comment_body', 'Comment', 'text_long', TRUE, FALSE);
108     $this->assertEntity('node.book.body', 'Body', 'text_with_summary', FALSE, FALSE);
109     $this->assertEntity('node.forum.taxonomy_forums', 'Forums', 'entity_reference', TRUE, FALSE);
110     $this->assertEntity('comment.comment_forum.comment_body', 'Comment', 'text_long', TRUE, FALSE);
111     $this->assertEntity('node.forum.body', 'Body', 'text_with_summary', FALSE, FALSE);
112     $this->assertEntity('comment.comment_node_test_content_type.comment_body', 'Comment', 'text_long', TRUE, FALSE);
113     $this->assertEntity('node.test_content_type.field_boolean', 'Boolean', 'boolean', FALSE, FALSE);
114     $this->assertEntity('node.test_content_type.field_email', 'Email', 'email', FALSE, FALSE);
115     $this->assertEntity('node.test_content_type.field_phone', 'Phone', 'telephone', TRUE, FALSE);
116     $this->assertEntity('node.test_content_type.field_date', 'Date', 'datetime', FALSE, FALSE);
117     $this->assertEntity('node.test_content_type.field_date_with_end_time', 'Date With End Time', 'timestamp', FALSE, FALSE);
118     $this->assertEntity('node.test_content_type.field_file', 'File', 'file', FALSE, FALSE);
119     $this->assertEntity('node.test_content_type.field_float', 'Float', 'float', FALSE, FALSE);
120     $this->assertEntity('node.test_content_type.field_images', 'Images', 'image', TRUE, FALSE);
121     $this->assertEntity('node.test_content_type.field_integer', 'Integer', 'integer', TRUE, TRUE);
122     $this->assertEntity('node.test_content_type.field_link', 'Link', 'link', FALSE, FALSE);
123     $this->assertEntity('node.test_content_type.field_text_list', 'Text List', 'list_string', FALSE, FALSE);
124     $this->assertEntity('node.test_content_type.field_integer_list', 'Integer List', 'list_integer', FALSE, FALSE);
125     $this->assertEntity('node.test_content_type.field_float_list', 'Float List', 'list_float', FALSE, FALSE);
126     $this->assertEntity('node.test_content_type.field_long_text', 'Long text', 'text_with_summary', FALSE, FALSE);
127     $this->assertEntity('node.test_content_type.field_term_reference', 'Term Reference', 'entity_reference', FALSE, FALSE);
128     $this->assertEntity('node.test_content_type.field_text', 'Text', 'string', FALSE, FALSE);
129     $this->assertEntity('comment.comment_node_test_content_type.field_integer', 'Integer', 'integer', FALSE, TRUE);
130     $this->assertEntity('user.user.field_file', 'File', 'file', FALSE, FALSE);
131
132     $this->assertLinkFields('node.test_content_type.field_link', DRUPAL_OPTIONAL);
133     $this->assertLinkFields('node.article.field_link', DRUPAL_DISABLED);
134     $this->assertLinkFields('node.blog.field_link', DRUPAL_REQUIRED);
135
136     // Tests that fields created by the Title module are not migrated.
137     $title_field = FieldConfig::load('node.test_content_type.title_field');
138     $this->assertNull($title_field);
139     $subject_field = FieldConfig::load('comment.comment_node_article.subject_field');
140     $this->assertNull($subject_field);
141     $name_field = FieldConfig::load('taxonomy_term.test_vocabulary.name_field');
142     $this->assertNull($name_field);
143     $description_field = FieldConfig::load('taxonomy_term.test_vocabulary.description_field');
144     $this->assertNull($description_field);
145   }
146
147   /**
148    * Tests the migration of text field instances with different text processing.
149    */
150   public function testTextFieldInstances() {
151     // All text and text_long field instances using a field base that has only
152     // plain text instances should be migrated to string and string_long fields.
153     // All text_with_summary field instances using a field base that has only
154     // plain text instances should not have been migrated since there's no such
155     // thing as a string_with_summary field.
156     $this->assertEntity('node.page.field_text_plain', 'Text plain', 'string', FALSE, FALSE);
157     $this->assertEntity('node.article.field_text_plain', 'Text plain', 'string', FALSE, TRUE);
158     $this->assertEntity('node.page.field_text_long_plain', 'Text long plain', 'string_long', FALSE, FALSE);
159     $this->assertEntity('node.article.field_text_long_plain', 'Text long plain', 'string_long', FALSE, TRUE);
160     $this->assertNull(FieldConfig::load('node.page.field_text_sum_plain'));
161     $this->assertNull(FieldConfig::load('node.article.field_text_sum_plain'));
162
163     // All text, text_long and text_with_summary field instances using a field
164     // base that has only filtered text instances should be migrated to text,
165     // text_long and text_with_summary fields.
166     $this->assertEntity('node.page.field_text_filtered', 'Text filtered', 'text', FALSE, FALSE);
167     $this->assertEntity('node.article.field_text_filtered', 'Text filtered', 'text', FALSE, TRUE);
168     $this->assertEntity('node.page.field_text_long_filtered', 'Text long filtered', 'text_long', FALSE, FALSE);
169     $this->assertEntity('node.article.field_text_long_filtered', 'Text long filtered', 'text_long', FALSE, TRUE);
170     $this->assertEntity('node.page.field_text_sum_filtered', 'Text summary filtered', 'text_with_summary', FALSE, FALSE);
171     $this->assertEntity('node.article.field_text_sum_filtered', 'Text summary filtered', 'text_with_summary', FALSE, TRUE);
172
173     // All text, text_long and text_with_summary field instances using a field
174     // base that has both plain text and filtered text instances should not have
175     // been migrated.
176     $this->assertNull(FieldConfig::load('node.page.field_text_plain_filtered'));
177     $this->assertNull(FieldConfig::load('node.article.field_text_plain_filtered'));
178     $this->assertNull(FieldConfig::load('node.page.field_text_long_plain_filtered'));
179     $this->assertNull(FieldConfig::load('node.article.field_text_long_plain_filtered'));
180     $this->assertNull(FieldConfig::load('node.page.field_text_sum_plain_filtered'));
181     $this->assertNull(FieldConfig::load('node.article.field_text_sum_plain_filtered'));
182
183     // For each text field instances that were skipped, there should be a log
184     // message with the required steps to fix this.
185     $migration = $this->getMigration('d7_field_instance');
186     $messages = $migration->getIdMap()->getMessageIterator()->fetchAll();
187     $errors = array_map(function ($message) {
188       return $message->message;
189     }, $messages);
190     $this->assertCount(8, $errors);
191     sort($errors);
192     $message = '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';
193     $this->assertEquals($errors[0], $message);
194     $this->assertEquals($errors[1], $message);
195     $message = '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';
196     $this->assertEquals($errors[2], $message);
197     $this->assertEquals($errors[3], $message);
198     $message = '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';
199     $this->assertEquals($errors[4], $message);
200     $this->assertEquals($errors[5], $message);
201     $message = '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';
202     $this->assertEquals($errors[6], $message);
203     $this->assertEquals($errors[7], $message);
204   }
205
206 }