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 $component = $display->getComponent('field_test_filefield');
134 $this->assertIdentical($expected, $component);
135 $display = EntityViewDisplay::load('node.story.default');
136 $expected['type'] = 'file_url_plain';
137 $component = $display->getComponent('field_test_filefield');
138 $this->assertIdentical($expected, $component);
140 // Test the image field formatter settings.
141 $expected['weight'] = 9;
142 $expected['type'] = 'image';
143 $expected['settings'] = ['image_style' => '', 'image_link' => ''];
144 $component = $display->getComponent('field_test_imagefield');
145 $this->assertIdentical($expected, $component);
146 $display = EntityViewDisplay::load('node.story.teaser');
147 $expected['settings']['image_link'] = 'file';
148 $component = $display->getComponent('field_test_imagefield');
149 $this->assertIdentical($expected, $component);
152 $expected['weight'] = 13;
153 $expected['type'] = 'basic_string';
154 $expected['settings'] = [];
155 $component = $display->getComponent('field_test_phone');
156 $this->assertIdentical($expected, $component);
159 $defaults = ['format_type' => 'fallback', 'timezone_override' => ''];
160 $expected['weight'] = 10;
161 $expected['type'] = 'datetime_default';
162 $expected['settings'] = ['format_type' => 'fallback'] + $defaults;
163 $component = $display->getComponent('field_test_date');
164 $this->assertIdentical($expected, $component);
165 $display = EntityViewDisplay::load('node.story.default');
166 $expected['settings']['format_type'] = 'long';
167 $component = $display->getComponent('field_test_date');
168 $this->assertIdentical($expected, $component);
170 // Test date stamp field.
171 $expected['weight'] = 11;
172 $expected['settings']['format_type'] = 'fallback';
173 $component = $display->getComponent('field_test_datestamp');
174 $this->assertIdentical($expected, $component);
175 $display = EntityViewDisplay::load('node.story.teaser');
176 $expected['settings'] = ['format_type' => 'medium'] + $defaults;
177 $component = $display->getComponent('field_test_datestamp');
178 $this->assertIdentical($expected, $component);
180 // Test datetime field.
181 $expected['weight'] = 12;
182 $expected['settings'] = ['format_type' => 'short'] + $defaults;
183 $component = $display->getComponent('field_test_datetime');
184 $this->assertIdentical($expected, $component);
185 $display = EntityViewDisplay::load('node.story.default');
186 $expected['settings']['format_type'] = 'fallback';
187 $component = $display->getComponent('field_test_datetime');
188 $this->assertIdentical($expected, $component);
190 // Test a date field with a random format which should be mapped
191 // to datetime_default.
192 $display = EntityViewDisplay::load('node.story.rss');
193 $expected['settings']['format_type'] = 'fallback';
194 $component = $display->getComponent('field_test_datetime');
195 $this->assertIdentical($expected, $component);
196 // Test that our Id map has the correct data.
197 $this->assertIdentical(['node', 'story', 'teaser', 'field_test'], $this->getMigration('d6_field_formatter_settings')->getIdMap()->lookupDestinationID(['story', 'teaser', 'node', 'field_test']));
199 // Test hidden field.
200 $this->assertComponentNotExists('node.test_planet.teaser', 'field_test_text_single_checkbox');
202 // Test a node reference field, which should be migrated to an entity
204 $display = EntityViewDisplay::load('node.employee.default');
205 $component = $display->getComponent('field_company');
206 $this->assertInternalType('array', $component);
207 $this->assertSame('entity_reference_label', $component['type']);
208 // The default node reference formatter shows the referenced node's title
210 $this->assertTrue($component['settings']['link']);
212 $display = EntityViewDisplay::load('node.employee.teaser');
213 $component = $display->getComponent('field_company');
214 $this->assertInternalType('array', $component);
215 $this->assertSame('entity_reference_label', $component['type']);
216 // The plain node reference formatter shows the referenced node's title,
218 $this->assertFalse($component['settings']['link']);
220 $component = $display->getComponent('field_commander');
221 $this->assertInternalType('array', $component);
222 $this->assertSame('entity_reference_label', $component['type']);
223 // The default user reference formatter links to the referenced user.
224 $this->assertTrue($component['settings']['link']);