3 namespace Drupal\Tests\field\Kernel\Migrate\d6;
5 use Drupal\Core\Entity\Entity\EntityViewDisplay;
6 use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
9 * Upgrade field formatter settings to entity.display.*.*.yml.
11 * @group migrate_drupal_6
13 class MigrateFieldFormatterSettingsTest extends MigrateDrupal6TestBase {
18 public static $modules = ['menu_ui'];
23 protected function setUp() {
25 $this->migrateFields();
29 * Asserts that a particular component is NOT included in a display.
31 * @param string $display_id
33 * @param string $component_id
36 protected function assertComponentNotExists($display_id, $component_id) {
37 $component = EntityViewDisplay::load($display_id)->getComponent($component_id);
38 $this->assertNull($component);
42 * Test that migrated entity display settings can be loaded using D8 API's.
44 public function testEntityDisplaySettings() {
46 $field_name = "field_test";
50 'type' => 'text_trimmed',
51 'settings' => ['trim_length' => 600],
52 'third_party_settings' => [],
53 'region' => 'content',
56 // Can we load any entity display.
57 $display = EntityViewDisplay::load('node.story.teaser');
58 $this->assertIdentical($expected, $display->getComponent($field_name));
60 // Test migrate worked with multiple bundles.
61 $display = EntityViewDisplay::load('node.test_page.teaser');
62 $expected['weight'] = 35;
63 $this->assertIdentical($expected, $display->getComponent($field_name));
65 // Test RSS because that has been converted from 4 to rss.
66 $display = EntityViewDisplay::load('node.story.rss');
67 $expected['weight'] = 1;
68 $this->assertIdentical($expected, $display->getComponent($field_name));
70 // Test the default format with text_default which comes from a static map.
71 $expected['type'] = 'text_default';
72 $expected['settings'] = [];
73 $display = EntityViewDisplay::load('node.story.default');
74 $this->assertIdentical($expected, $display->getComponent($field_name));
76 // Check that we can migrate multiple fields.
77 $content = $display->get('content');
78 $this->assertTrue(isset($content['field_test']), 'Settings for field_test exist.');
79 $this->assertTrue(isset($content['field_test_two']), "Settings for field_test_two exist.");
81 // Check that we can migrate a field where exclude is not set.
82 $this->assertTrue(isset($content['field_test_exclude_unset']), "Settings for field_test_exclude_unset exist.");
84 // Test the number field formatter settings are correct.
85 $expected['weight'] = 1;
86 $expected['type'] = 'number_integer';
87 $expected['settings'] = [
88 'thousand_separator' => ',',
89 'prefix_suffix' => TRUE,
91 $component = $display->getComponent('field_test_two');
92 $this->assertIdentical($expected, $component);
93 $expected['weight'] = 2;
94 $expected['type'] = 'number_decimal';
95 $expected['settings'] = [
97 'decimal_separator' => '.',
98 'thousand_separator' => ',',
99 'prefix_suffix' => TRUE,
101 $component = $display->getComponent('field_test_three');
102 $this->assertIdentical($expected, $component);
104 // Test the email field formatter settings are correct.
105 $expected['weight'] = 6;
106 $expected['type'] = 'email_mailto';
107 $expected['settings'] = [];
108 $component = $display->getComponent('field_test_email');
109 $this->assertIdentical($expected, $component);
111 // Test the link field formatter settings.
112 $expected['weight'] = 7;
113 $expected['type'] = 'link';
114 $expected['settings'] = [
121 $component = $display->getComponent('field_test_link');
122 $this->assertIdentical($expected, $component);
123 $expected['settings']['url_only'] = FALSE;
124 $expected['settings']['url_plain'] = FALSE;
125 $display = EntityViewDisplay::load('node.story.teaser');
126 $component = $display->getComponent('field_test_link');
127 $this->assertIdentical($expected, $component);
129 // Test the file field formatter settings.
130 $expected['weight'] = 8;
131 $expected['type'] = 'file_default';
132 $expected['settings'] = [
133 'use_description_as_link_text' => TRUE
135 $component = $display->getComponent('field_test_filefield');
136 $this->assertIdentical($expected, $component);
137 $display = EntityViewDisplay::load('node.story.default');
138 $expected['type'] = 'file_url_plain';
139 $expected['settings'] = [];
140 $component = $display->getComponent('field_test_filefield');
141 $this->assertIdentical($expected, $component);
143 // Test the image field formatter settings.
144 $expected['weight'] = 9;
145 $expected['type'] = 'image';
146 $expected['settings'] = ['image_style' => '', 'image_link' => ''];
147 $component = $display->getComponent('field_test_imagefield');
148 $this->assertIdentical($expected, $component);
149 $display = EntityViewDisplay::load('node.story.teaser');
150 $expected['settings']['image_link'] = 'file';
151 $component = $display->getComponent('field_test_imagefield');
152 $this->assertIdentical($expected, $component);
155 $expected['weight'] = 13;
156 $expected['type'] = 'basic_string';
157 $expected['settings'] = [];
158 $component = $display->getComponent('field_test_phone');
159 $this->assertIdentical($expected, $component);
162 $defaults = ['format_type' => 'fallback', 'timezone_override' => ''];
163 $expected['weight'] = 10;
164 $expected['type'] = 'datetime_default';
165 $expected['settings'] = ['format_type' => 'fallback'] + $defaults;
166 $component = $display->getComponent('field_test_date');
167 $this->assertIdentical($expected, $component);
168 $display = EntityViewDisplay::load('node.story.default');
169 $expected['settings']['format_type'] = 'long';
170 $component = $display->getComponent('field_test_date');
171 $this->assertIdentical($expected, $component);
173 // Test date stamp field.
174 $expected['weight'] = 11;
175 $expected['settings']['format_type'] = 'fallback';
176 $component = $display->getComponent('field_test_datestamp');
177 $this->assertIdentical($expected, $component);
178 $display = EntityViewDisplay::load('node.story.teaser');
179 $expected['settings'] = ['format_type' => 'medium'] + $defaults;
180 $component = $display->getComponent('field_test_datestamp');
181 $this->assertIdentical($expected, $component);
183 // Test datetime field.
184 $expected['weight'] = 12;
185 $expected['settings'] = ['format_type' => 'short'] + $defaults;
186 $component = $display->getComponent('field_test_datetime');
187 $this->assertIdentical($expected, $component);
188 $display = EntityViewDisplay::load('node.story.default');
189 $expected['settings']['format_type'] = 'fallback';
190 $component = $display->getComponent('field_test_datetime');
191 $this->assertIdentical($expected, $component);
193 // Test a date field with a random format which should be mapped
194 // to datetime_default.
195 $display = EntityViewDisplay::load('node.story.rss');
196 $expected['settings']['format_type'] = 'fallback';
197 $component = $display->getComponent('field_test_datetime');
198 $this->assertIdentical($expected, $component);
199 // Test that our Id map has the correct data.
200 $this->assertIdentical(['node', 'story', 'teaser', 'field_test'], $this->getMigration('d6_field_formatter_settings')->getIdMap()->lookupDestinationID(['story', 'teaser', 'node', 'field_test']));
202 // Test hidden field.
203 $this->assertComponentNotExists('node.test_planet.teaser', 'field_test_text_single_checkbox');
205 // Test a node reference field, which should be migrated to an entity
207 $display = EntityViewDisplay::load('node.employee.default');
208 $component = $display->getComponent('field_company');
209 $this->assertInternalType('array', $component);
210 $this->assertSame('entity_reference_label', $component['type']);
211 // The default node reference formatter shows the referenced node's title
213 $this->assertTrue($component['settings']['link']);
215 $display = EntityViewDisplay::load('node.employee.teaser');
216 $component = $display->getComponent('field_company');
217 $this->assertInternalType('array', $component);
218 $this->assertSame('entity_reference_label', $component['type']);
219 // The plain node reference formatter shows the referenced node's title,
221 $this->assertFalse($component['settings']['link']);
223 $component = $display->getComponent('field_commander');
224 $this->assertInternalType('array', $component);
225 $this->assertSame('entity_reference_label', $component['type']);
226 // The default user reference formatter links to the referenced user.
227 $this->assertTrue($component['settings']['link']);