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

3
namespace Drupal\Tests\field\Kernel\Migrate\d7;
4
5
6

use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
7
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
8
9
10
11
12
13
14
15

/**
 * Tests migration of D7 field formatter settings.
 *
 * @group field
 */
class MigrateFieldFormatterSettingsTest extends MigrateDrupal7TestBase {

16
  protected static $modules = [
17
18
19
20
    'comment',
    'datetime',
    'image',
    'link',
21
    'menu_ui',
22
    'node',
23
    'taxonomy',
24
25
26
27
28
29
30
    'telephone',
    'text',
  ];

  /**
   * {@inheritdoc}
   */
31
  protected function setUp(): void {
32
    parent::setUp();
33
    $this->migrateFields();
34
35
36
37
    $this->executeMigrations([
      'd7_view_modes',
      'd7_field_formatter_settings',
    ]);
38
39
40
41
42
43
44
45
46
47
  }

  /**
   * Asserts various aspects of a view display.
   *
   * @param string $id
   *   The view display ID.
   */
  protected function assertEntity($id) {
    $display = EntityViewDisplay::load($id);
48
    $this->assertInstanceOf(EntityViewDisplayInterface::class, $display);
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  }

  /**
   * Asserts various aspects of a particular component of a view display.
   *
   * @param string $display_id
   *   The view display ID.
   * @param string $component_id
   *   The component ID.
   * @param string $type
   *   The expected component type (formatter plugin ID).
   * @param string $label
   *   The expected label of the component.
   * @param int $weight
   *   The expected weight of the component.
   */
  protected function assertComponent($display_id, $component_id, $type, $label, $weight) {
    $component = EntityViewDisplay::load($display_id)->getComponent($component_id);
    $this->assertTrue(is_array($component));
    $this->assertIdentical($type, $component['type']);
    $this->assertIdentical($label, $component['label']);
    $this->assertIdentical($weight, $component['weight']);
  }

  /**
   * 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);
83
    $this->assertNull($component);
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
  }

  /**
   * Tests migration of D7 field formatter settings.
   */
  public function testMigration() {
    $this->assertEntity('comment.comment_node_article.default');
    $this->assertComponent('comment.comment_node_article.default', 'comment_body', 'text_default', 'hidden', 0);

    $this->assertEntity('comment.comment_node_blog.default');
    $this->assertComponent('comment.comment_node_blog.default', 'comment_body', 'text_default', 'hidden', 0);

    $this->assertEntity('comment.comment_node_book.default');
    $this->assertComponent('comment.comment_node_book.default', 'comment_body', 'text_default', 'hidden', 0);

99
100
    $this->assertEntity('comment.comment_forum.default');
    $this->assertComponent('comment.comment_forum.default', 'comment_body', 'text_default', 'hidden', 0);
101
102
103
104
105
106
107
108
109
110
111
112

    $this->assertEntity('comment.comment_node_page.default');
    $this->assertComponent('comment.comment_node_page.default', 'comment_body', 'text_default', 'hidden', 0);

    $this->assertEntity('comment.comment_node_test_content_type.default');
    $this->assertComponent('comment.comment_node_test_content_type.default', 'comment_body', 'text_default', 'hidden', 0);
    $this->assertComponent('comment.comment_node_test_content_type.default', 'field_integer', 'number_integer', 'above', 1);

    $this->assertEntity('node.article.default');
    $this->assertComponent('node.article.default', 'body', 'text_default', 'hidden', 0);
    $this->assertComponent('node.article.default', 'field_tags', 'entity_reference_label', 'above', 10);
    $this->assertComponent('node.article.default', 'field_image', 'image', 'hidden', -1);
113
114
115
116
117
    $this->assertComponent('node.article.default', 'field_text_plain', 'string', 'above', 11);
    $this->assertComponent('node.article.default', 'field_text_filtered', 'text_default', 'above', 12);
    $this->assertComponent('node.article.default', 'field_text_long_plain', 'basic_string', 'above', 14);
    $this->assertComponent('node.article.default', 'field_text_long_filtered', 'text_default', 'above', 15);
    $this->assertComponent('node.article.default', 'field_text_sum_filtered', 'text_default', 'above', 18);
118
119
    $this->assertComponent('node.article.default', 'field_reference', 'entity_reference_label', 'above', 20);
    $this->assertComponent('node.article.default', 'field_reference_2', 'entity_reference_entity_view', 'above', 21);
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147

    $this->assertEntity('node.article.teaser');
    $this->assertComponent('node.article.teaser', 'body', 'text_summary_or_trimmed', 'hidden', 0);
    $this->assertComponent('node.article.teaser', 'field_tags', 'entity_reference_label', 'above', 10);
    $this->assertComponent('node.article.teaser', 'field_image', 'image', 'hidden', -1);

    $this->assertEntity('node.blog.default');
    $this->assertComponent('node.blog.default', 'body', 'text_default', 'hidden', 0);

    $this->assertEntity('node.blog.teaser');
    $this->assertComponent('node.blog.teaser', 'body', 'text_summary_or_trimmed', 'hidden', 0);

    $this->assertEntity('node.book.default');
    $this->assertComponent('node.book.default', 'body', 'text_default', 'hidden', 0);

    $this->assertEntity('node.book.teaser');
    $this->assertComponent('node.book.teaser', 'body', 'text_summary_or_trimmed', 'hidden', 0);

    $this->assertEntity('node.forum.default');
    $this->assertComponent('node.forum.default', 'body', 'text_default', 'hidden', 11);
    $this->assertComponent('node.forum.default', 'taxonomy_forums', 'entity_reference_label', 'above', 10);

    $this->assertEntity('node.forum.teaser');
    $this->assertComponent('node.forum.teaser', 'body', 'text_summary_or_trimmed', 'hidden', 11);
    $this->assertComponent('node.forum.teaser', 'taxonomy_forums', 'entity_reference_label', 'above', 10);

    $this->assertEntity('node.page.default');
    $this->assertComponent('node.page.default', 'body', 'text_default', 'hidden', 0);
148
149
150
151
152
    $this->assertComponent('node.page.default', 'field_text_plain', 'string', 'above', 1);
    $this->assertComponent('node.page.default', 'field_text_filtered', 'text_default', 'above', 2);
    $this->assertComponent('node.page.default', 'field_text_long_plain', 'basic_string', 'above', 4);
    $this->assertComponent('node.page.default', 'field_text_long_filtered', 'text_default', 'above', 5);
    $this->assertComponent('node.page.default', 'field_text_sum_filtered', 'text_default', 'above', 8);
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170

    $this->assertEntity('node.page.teaser');
    $this->assertComponent('node.page.teaser', 'body', 'text_summary_or_trimmed', 'hidden', 0);

    $this->assertEntity('node.test_content_type.default');
    $this->assertComponent('node.test_content_type.default', 'field_boolean', 'list_default', 'above', 0);
    $this->assertComponent('node.test_content_type.default', 'field_email', 'email_mailto', 'above', 1);
    // Phone formatters are not mapped and should default to basic_string.
    $this->assertComponent('node.test_content_type.default', 'field_phone', 'basic_string', 'above', 2);
    $this->assertComponent('node.test_content_type.default', 'field_date', 'datetime_default', 'above', 3);
    $this->assertComponent('node.test_content_type.default', 'field_date_with_end_time', 'datetime_default', 'above', 4);
    $this->assertComponent('node.test_content_type.default', 'field_file', 'file_default', 'above', 5);
    $this->assertComponent('node.test_content_type.default', 'field_float', 'number_decimal', 'above', 6);
    $this->assertComponent('node.test_content_type.default', 'field_images', 'image', 'above', 7);
    $this->assertComponent('node.test_content_type.default', 'field_integer', 'number_integer', 'above', 8);
    $this->assertComponent('node.test_content_type.default', 'field_link', 'link', 'above', 9);
    $this->assertComponent('node.test_content_type.default', 'field_text_list', 'list_default', 'above', 10);
    $this->assertComponent('node.test_content_type.default', 'field_integer_list', 'list_default', 'above', 11);
171
    $this->assertComponent('node.test_content_type.default', 'field_float_list', 'list_default', 'above', 19);
172
    $this->assertComponent('node.test_content_type.default', 'field_long_text', 'text_default', 'above', 12);
173
174
175
    $this->assertComponent('node.test_content_type.default', 'field_node_entityreference', 'entity_reference_label', 'above', 15);
    $this->assertComponent('node.test_content_type.default', 'field_user_entityreference', 'entity_reference_label', 'above', 16);
    $this->assertComponent('node.test_content_type.default', 'field_term_entityreference', 'entity_reference_label', 'above', 17);
176
177
178
179
180
181
182
183
    $this->assertComponentNotExists('node.test_content_type.default', 'field_term_reference');
    $this->assertComponentNotExists('node.test_content_type.default', 'field_text');

    $this->assertEntity('user.user.default');
    $this->assertComponent('user.user.default', 'field_file', 'file_default', 'above', 0);
  }

}