7bf3ad541830caae7b1ed865126e7828dc928fec
[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       'use_description_as_link_text' => TRUE
134     ];
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);
142
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);
153
154     // Test phone field.
155     $expected['weight'] = 13;
156     $expected['type'] = 'basic_string';
157     $expected['settings'] = [];
158     $component = $display->getComponent('field_test_phone');
159     $this->assertIdentical($expected, $component);
160
161     // Test date field.
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);
172
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);
182
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);
192
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']));
201
202     // Test hidden field.
203     $this->assertComponentNotExists('node.test_planet.teaser', 'field_test_text_single_checkbox');
204
205     // Test a node reference field, which should be migrated to an entity
206     // reference field.
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
212     // as a link.
213     $this->assertTrue($component['settings']['link']);
214
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,
220     // unlinked.
221     $this->assertFalse($component['settings']['link']);
222
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']);
228   }
229
230 }