MigrateFieldFormatterSettingsTest.php 7.56 KB
Newer Older
1
2
<?php

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
10
 * Upgrade field formatter settings to entity.display.*.*.yml.
 *
11
 * @group migrate_drupal_6
12
 */
13
class MigrateFieldFormatterSettingsTest extends MigrateDrupal6TestBase {
14
15
16
17

  /**
   * {@inheritdoc}
   */
18
  protected function setUp() {
19
    parent::setUp();
20
    $this->migrateFields();
21
22
  }

23
24
25
26
27
28
29
30
31
32
33
34
35
  /**
   * Asserts that a particular component is NOT included in a display.
   *
   * @param string $display_id
   *   The display ID.
   * @param string $component_id
   *   The component ID.
   */
  protected function assertComponentNotExists($display_id, $component_id) {
    $component = EntityViewDisplay::load($display_id)->getComponent($component_id);
    $this->assertNull($component);
  }

36
37
38
39
40
41
  /**
   * Test that migrated entity display settings can be loaded using D8 API's.
   */
  public function testEntityDisplaySettings() {
    // Run tests.
    $field_name = "field_test";
42
    $expected = [
43
      'label' => 'above',
44
      'weight' => 1,
45
      'type' => 'text_trimmed',
46
47
      'settings' => ['trim_length' => 600],
      'third_party_settings' => [],
48
      'region' => 'content',
49
    ];
50
51

    // Can we load any entity display.
52
    $display = EntityViewDisplay::load('node.story.teaser');
53
    $this->assertIdentical($expected, $display->getComponent($field_name));
54
55

    // Test migrate worked with multiple bundles.
56
    $display = EntityViewDisplay::load('node.test_page.teaser');
57
    $expected['weight'] = 35;
58
    $this->assertIdentical($expected, $display->getComponent($field_name));
59
60

    // Test RSS because that has been converted from 4 to rss.
61
    $display = EntityViewDisplay::load('node.story.rss');
62
    $expected['weight'] = 1;
63
    $this->assertIdentical($expected, $display->getComponent($field_name));
64

65
    // Test the default format with text_default which comes from a static map.
66
    $expected['type'] = 'text_default';
67
    $expected['settings'] = [];
68
    $display = EntityViewDisplay::load('node.story.default');
69
    $this->assertIdentical($expected, $display->getComponent($field_name));
70
71
72
73
74
75

    // Check that we can migrate multiple fields.
    $content = $display->get('content');
    $this->assertTrue(isset($content['field_test']), 'Settings for field_test exist.');
    $this->assertTrue(isset($content['field_test_two']), "Settings for field_test_two exist.");

76
77
78
    // Check that we can migrate a field where exclude is not set.
    $this->assertTrue(isset($content['field_test_exclude_unset']), "Settings for field_test_exclude_unset exist.");

79
    // Test the number field formatter settings are correct.
80
    $expected['weight'] = 1;
81
    $expected['type'] = 'number_integer';
82
    $expected['settings'] = [
83
84
      'thousand_separator' => ',',
      'prefix_suffix' => TRUE,
85
    ];
86
    $component = $display->getComponent('field_test_two');
87
    $this->assertIdentical($expected, $component);
88
    $expected['weight'] = 2;
89
    $expected['type'] = 'number_decimal';
90
    $expected['settings'] = [
91
92
93
94
       'scale' => 2,
       'decimal_separator' => '.',
       'thousand_separator' => ',',
       'prefix_suffix' => TRUE,
95
    ];
96
    $component = $display->getComponent('field_test_three');
97
    $this->assertIdentical($expected, $component);
98
99

    // Test the email field formatter settings are correct.
100
    $expected['weight'] = 6;
101
    $expected['type'] = 'email_mailto';
102
    $expected['settings'] = [];
103
    $component = $display->getComponent('field_test_email');
104
    $this->assertIdentical($expected, $component);
105
106

    // Test the link field formatter settings.
107
    $expected['weight'] = 7;
108
    $expected['type'] = 'link';
109
    $expected['settings'] = [
110
      'trim_length' => 80,
111
112
113
114
      'url_only' => TRUE,
      'url_plain' => TRUE,
      'rel' => '0',
      'target' => '0',
115
    ];
116
    $component = $display->getComponent('field_test_link');
117
    $this->assertIdentical($expected, $component);
118
119
    $expected['settings']['url_only'] = FALSE;
    $expected['settings']['url_plain'] = FALSE;
120
    $display = EntityViewDisplay::load('node.story.teaser');
121
    $component = $display->getComponent('field_test_link');
122
    $this->assertIdentical($expected, $component);
123
124

    // Test the file field formatter settings.
125
    $expected['weight'] = 8;
126
    $expected['type'] = 'file_default';
127
    $expected['settings'] = [];
128
    $component = $display->getComponent('field_test_filefield');
129
    $this->assertIdentical($expected, $component);
130
    $display = EntityViewDisplay::load('node.story.default');
131
132
    $expected['type'] = 'file_url_plain';
    $component = $display->getComponent('field_test_filefield');
133
    $this->assertIdentical($expected, $component);
134
135

    // Test the image field formatter settings.
136
    $expected['weight'] = 9;
137
    $expected['type'] = 'image';
138
    $expected['settings'] = ['image_style' => '', 'image_link' => ''];
139
    $component = $display->getComponent('field_test_imagefield');
140
    $this->assertIdentical($expected, $component);
141
    $display = EntityViewDisplay::load('node.story.teaser');
142
143
    $expected['settings']['image_link'] = 'file';
    $component = $display->getComponent('field_test_imagefield');
144
    $this->assertIdentical($expected, $component);
145
146

    // Test phone field.
147
    $expected['weight'] = 13;
148
    $expected['type'] = 'basic_string';
149
    $expected['settings'] = [];
150
    $component = $display->getComponent('field_test_phone');
151
    $this->assertIdentical($expected, $component);
152
153

    // Test date field.
154
    $defaults = ['format_type' => 'fallback', 'timezone_override' => ''];
155
156
    $expected['weight'] = 10;
    $expected['type'] = 'datetime_default';
157
    $expected['settings'] = ['format_type' => 'fallback'] + $defaults;
158
    $component = $display->getComponent('field_test_date');
159
    $this->assertIdentical($expected, $component);
160
    $display = EntityViewDisplay::load('node.story.default');
161
162
    $expected['settings']['format_type'] = 'long';
    $component = $display->getComponent('field_test_date');
163
    $this->assertIdentical($expected, $component);
164
165
166
167
168

    // Test date stamp field.
    $expected['weight'] = 11;
    $expected['settings']['format_type'] = 'fallback';
    $component = $display->getComponent('field_test_datestamp');
169
    $this->assertIdentical($expected, $component);
170
    $display = EntityViewDisplay::load('node.story.teaser');
171
    $expected['settings'] = ['format_type' => 'medium'] + $defaults;
172
    $component = $display->getComponent('field_test_datestamp');
173
    $this->assertIdentical($expected, $component);
174
175
176

    // Test datetime field.
    $expected['weight'] = 12;
177
    $expected['settings'] = ['format_type' => 'short'] + $defaults;
178
    $component = $display->getComponent('field_test_datetime');
179
    $this->assertIdentical($expected, $component);
180
    $display = EntityViewDisplay::load('node.story.default');
181
182
    $expected['settings']['format_type'] = 'fallback';
    $component = $display->getComponent('field_test_datetime');
183
    $this->assertIdentical($expected, $component);
184
185
186

    // Test a date field with a random format which should be mapped
    // to datetime_default.
187
    $display = EntityViewDisplay::load('node.story.rss');
188
189
    $expected['settings']['format_type'] = 'fallback';
    $component = $display->getComponent('field_test_datetime');
190
    $this->assertIdentical($expected, $component);
191
    // Test that our Id map has the correct data.
192
    $this->assertIdentical(['node', 'story', 'teaser', 'field_test'], $this->getMigration('d6_field_formatter_settings')->getIdMap()->lookupDestinationID(['story', 'teaser', 'node', 'field_test']));
193
194
195

    // Test hidden field.
    $this->assertComponentNotExists('node.test_planet.teaser', 'field_test_text_single_checkbox');
196
197
198
  }

}