3 namespace Drupal\Tests\field\Kernel\Migrate\d7;
5 use Drupal\comment\Entity\CommentType;
6 use Drupal\Core\Database\Database;
7 use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
8 use Drupal\Core\Entity\Entity\EntityViewDisplay;
9 use Drupal\taxonomy\Entity\Vocabulary;
10 use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
11 use Drupal\node\Entity\NodeType;
14 * Tests migration of D7 field formatter settings.
18 class MigrateFieldFormatterSettingsTest extends MigrateDrupal7TestBase {
20 public static $modules = [
35 protected function setUp() {
38 $this->installEntitySchema('node');
39 $this->installEntitySchema('comment');
40 $this->installEntitySchema('taxonomy_term');
43 'id' => 'comment_node_page',
44 'label' => $this->randomMachineName(),
47 'id' => 'comment_node_article',
48 'label' => $this->randomMachineName(),
51 'id' => 'comment_node_blog',
52 'label' => $this->randomMachineName(),
55 'id' => 'comment_node_book',
56 'label' => $this->randomMachineName(),
59 'id' => 'comment_node_forum',
60 'label' => $this->randomMachineName(),
63 'id' => 'comment_node_test_content_type',
64 'label' => $this->randomMachineName(),
69 'label' => $this->randomMachineName(),
73 'label' => $this->randomMachineName(),
77 'label' => $this->randomMachineName(),
81 'label' => $this->randomMachineName(),
85 'label' => $this->randomMachineName(),
88 'type' => 'test_content_type',
89 'label' => $this->randomMachineName(),
92 Vocabulary::create(['vid' => 'test_vocabulary'])->save();
94 // Give one unfortunate field instance invalid display settings to ensure
95 // that the migration provides an empty array as a default (thus avoiding
96 // an "unsupported operand types" fatal).
97 Database::getConnection('default', 'migrate')
98 ->update('field_config_instance')
100 'data' => serialize( [
104 'type' => 'text_textarea_with_summary',
115 'display_summary' => TRUE,
116 'text_processing' => 1,
117 'user_register_form' => FALSE,
124 'type' => 'text_default',
134 'type' => 'text_summary_or_trimmed',
135 // settings is always expected to be an array. Making it NULL
146 ->condition('entity_type', 'node')
147 ->condition('bundle', 'article')
148 ->condition('field_name', 'body')
151 $this->executeMigrations([
155 'd7_field_formatter_settings',
160 * Asserts various aspects of a view display.
163 * The view display ID.
165 protected function assertEntity($id) {
166 $display = EntityViewDisplay::load($id);
167 $this->assertTrue($display instanceof EntityViewDisplayInterface);
171 * Asserts various aspects of a particular component of a view display.
173 * @param string $display_id
174 * The view display ID.
175 * @param string $component_id
177 * @param string $type
178 * The expected component type (formatter plugin ID).
179 * @param string $label
180 * The expected label of the component.
182 * The expected weight of the component.
184 protected function assertComponent($display_id, $component_id, $type, $label, $weight) {
185 $component = EntityViewDisplay::load($display_id)->getComponent($component_id);
186 $this->assertTrue(is_array($component));
187 $this->assertIdentical($type, $component['type']);
188 $this->assertIdentical($label, $component['label']);
189 $this->assertIdentical($weight, $component['weight']);
193 * Asserts that a particular component is NOT included in a display.
195 * @param string $display_id
197 * @param string $component_id
200 protected function assertComponentNotExists($display_id, $component_id) {
201 $component = EntityViewDisplay::load($display_id)->getComponent($component_id);
202 $this->assertTrue(is_null($component));
206 * Tests migration of D7 field formatter settings.
208 public function testMigration() {
209 $this->assertEntity('comment.comment_node_article.default');
210 $this->assertComponent('comment.comment_node_article.default', 'comment_body', 'text_default', 'hidden', 0);
212 $this->assertEntity('comment.comment_node_blog.default');
213 $this->assertComponent('comment.comment_node_blog.default', 'comment_body', 'text_default', 'hidden', 0);
215 $this->assertEntity('comment.comment_node_book.default');
216 $this->assertComponent('comment.comment_node_book.default', 'comment_body', 'text_default', 'hidden', 0);
218 $this->assertEntity('comment.comment_node_forum.default');
219 $this->assertComponent('comment.comment_node_forum.default', 'comment_body', 'text_default', 'hidden', 0);
221 $this->assertEntity('comment.comment_node_page.default');
222 $this->assertComponent('comment.comment_node_page.default', 'comment_body', 'text_default', 'hidden', 0);
224 $this->assertEntity('comment.comment_node_test_content_type.default');
225 $this->assertComponent('comment.comment_node_test_content_type.default', 'comment_body', 'text_default', 'hidden', 0);
226 $this->assertComponent('comment.comment_node_test_content_type.default', 'field_integer', 'number_integer', 'above', 1);
228 $this->assertEntity('node.article.default');
229 $this->assertComponent('node.article.default', 'body', 'text_default', 'hidden', 0);
230 $this->assertComponent('node.article.default', 'field_tags', 'entity_reference_label', 'above', 10);
231 $this->assertComponent('node.article.default', 'field_image', 'image', 'hidden', -1);
233 $this->assertEntity('node.article.teaser');
234 $this->assertComponent('node.article.teaser', 'body', 'text_summary_or_trimmed', 'hidden', 0);
235 $this->assertComponent('node.article.teaser', 'field_tags', 'entity_reference_label', 'above', 10);
236 $this->assertComponent('node.article.teaser', 'field_image', 'image', 'hidden', -1);
238 $this->assertEntity('node.blog.default');
239 $this->assertComponent('node.blog.default', 'body', 'text_default', 'hidden', 0);
241 $this->assertEntity('node.blog.teaser');
242 $this->assertComponent('node.blog.teaser', 'body', 'text_summary_or_trimmed', 'hidden', 0);
244 $this->assertEntity('node.book.default');
245 $this->assertComponent('node.book.default', 'body', 'text_default', 'hidden', 0);
247 $this->assertEntity('node.book.teaser');
248 $this->assertComponent('node.book.teaser', 'body', 'text_summary_or_trimmed', 'hidden', 0);
250 $this->assertEntity('node.forum.default');
251 $this->assertComponent('node.forum.default', 'body', 'text_default', 'hidden', 11);
252 $this->assertComponent('node.forum.default', 'taxonomy_forums', 'entity_reference_label', 'above', 10);
254 $this->assertEntity('node.forum.teaser');
255 $this->assertComponent('node.forum.teaser', 'body', 'text_summary_or_trimmed', 'hidden', 11);
256 $this->assertComponent('node.forum.teaser', 'taxonomy_forums', 'entity_reference_label', 'above', 10);
258 $this->assertEntity('node.page.default');
259 $this->assertComponent('node.page.default', 'body', 'text_default', 'hidden', 0);
261 $this->assertEntity('node.page.teaser');
262 $this->assertComponent('node.page.teaser', 'body', 'text_summary_or_trimmed', 'hidden', 0);
264 $this->assertEntity('node.test_content_type.default');
265 $this->assertComponent('node.test_content_type.default', 'field_boolean', 'list_default', 'above', 0);
266 $this->assertComponent('node.test_content_type.default', 'field_email', 'email_mailto', 'above', 1);
267 // Phone formatters are not mapped and should default to basic_string.
268 $this->assertComponent('node.test_content_type.default', 'field_phone', 'basic_string', 'above', 2);
269 $this->assertComponent('node.test_content_type.default', 'field_date', 'datetime_default', 'above', 3);
270 $this->assertComponent('node.test_content_type.default', 'field_date_with_end_time', 'datetime_default', 'above', 4);
271 $this->assertComponent('node.test_content_type.default', 'field_file', 'file_default', 'above', 5);
272 $this->assertComponent('node.test_content_type.default', 'field_float', 'number_decimal', 'above', 6);
273 $this->assertComponent('node.test_content_type.default', 'field_images', 'image', 'above', 7);
274 $this->assertComponent('node.test_content_type.default', 'field_integer', 'number_integer', 'above', 8);
275 $this->assertComponent('node.test_content_type.default', 'field_link', 'link', 'above', 9);
276 $this->assertComponent('node.test_content_type.default', 'field_text_list', 'list_default', 'above', 10);
277 $this->assertComponent('node.test_content_type.default', 'field_integer_list', 'list_default', 'above', 11);
278 $this->assertComponent('node.test_content_type.default', 'field_long_text', 'text_default', 'above', 12);
279 $this->assertComponent('node.test_content_type.default', 'field_node_entityreference', 'entity_reference_label', 'above', 15);
280 $this->assertComponent('node.test_content_type.default', 'field_user_entityreference', 'entity_reference_label', 'above', 16);
281 $this->assertComponent('node.test_content_type.default', 'field_term_entityreference', 'entity_reference_label', 'above', 17);
282 $this->assertComponentNotExists('node.test_content_type.default', 'field_term_reference');
283 $this->assertComponentNotExists('node.test_content_type.default', 'field_text');
285 $this->assertEntity('user.user.default');
286 $this->assertComponent('user.user.default', 'field_file', 'file_default', 'above', 0);