Version 1
[yaffs-website] / web / core / modules / field / tests / src / Kernel / Migrate / d6 / MigrateFieldFormatterSettingsTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Kernel\Migrate\d6;
4
5 use Drupal\Core\Entity\Entity\EntityViewDisplay;
6 use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
7
8 /**
9  * Upgrade field formatter settings to entity.display.*.*.yml.
10  *
11  * @group migrate_drupal_6
12  */
13 class MigrateFieldFormatterSettingsTest extends MigrateDrupal6TestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   public static $modules = ['menu_ui'];
19
20   /**
21    * {@inheritdoc}
22    */
23   protected function setUp() {
24     parent::setUp();
25     $this->migrateFields();
26   }
27
28   /**
29    * Asserts that a particular component is NOT included in a display.
30    *
31    * @param string $display_id
32    *   The display ID.
33    * @param string $component_id
34    *   The component ID.
35    */
36   protected function assertComponentNotExists($display_id, $component_id) {
37     $component = EntityViewDisplay::load($display_id)->getComponent($component_id);
38     $this->assertNull($component);
39   }
40
41   /**
42    * Test that migrated entity display settings can be loaded using D8 API's.
43    */
44   public function testEntityDisplaySettings() {
45     // Run tests.
46     $field_name = "field_test";
47     $expected = [
48       'label' => 'above',
49       'weight' => 1,
50       'type' => 'text_trimmed',
51       'settings' => ['trim_length' => 600],
52       'third_party_settings' => [],
53       'region' => 'content',
54     ];
55
56     // Can we load any entity display.
57     $display = EntityViewDisplay::load('node.story.teaser');
58     $this->assertIdentical($expected, $display->getComponent($field_name));
59
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));
64
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));
69
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));
75
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.");
80
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.");
83
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,
90     ];
91     $component = $display->getComponent('field_test_two');
92     $this->assertIdentical($expected, $component);
93     $expected['weight'] = 2;
94     $expected['type'] = 'number_decimal';
95     $expected['settings'] = [
96        'scale' => 2,
97        'decimal_separator' => '.',
98        'thousand_separator' => ',',
99        'prefix_suffix' => TRUE,
100     ];
101     $component = $display->getComponent('field_test_three');
102     $this->assertIdentical($expected, $component);
103
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);
110
111     // Test the link field formatter settings.
112     $expected['weight'] = 7;
113     $expected['type'] = 'link';
114     $expected['settings'] = [
115       'trim_length' => 80,
116       'url_only' => TRUE,
117       'url_plain' => TRUE,
118       'rel' => '0',
119       'target' => '0',
120     ];
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);
128
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);
139
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);
150
151     // Test phone field.
152     $expected['weight'] = 13;
153     $expected['type'] = 'basic_string';
154     $expected['settings'] = [];
155     $component = $display->getComponent('field_test_phone');
156     $this->assertIdentical($expected, $component);
157
158     // Test date field.
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);
169
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);
179
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);
189
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']));
198
199     // Test hidden field.
200     $this->assertComponentNotExists('node.test_planet.teaser', 'field_test_text_single_checkbox');
201
202     // Test a node reference field, which should be migrated to an entity
203     // reference field.
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
209     // as a link.
210     $this->assertTrue($component['settings']['link']);
211
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,
217     // unlinked.
218     $this->assertFalse($component['settings']['link']);
219
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']);
225   }
226
227 }