Version 1
[yaffs-website] / web / core / modules / field / tests / src / Kernel / Migrate / d7 / MigrateFieldInstanceTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Kernel\Migrate\d7;
4
5 use Drupal\comment\Entity\CommentType;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\FieldConfigInterface;
8 use Drupal\taxonomy\Entity\Vocabulary;
9 use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
10 use Drupal\node\Entity\NodeType;
11
12 /**
13  * Migrates Drupal 7 field instances.
14  *
15  * @group field
16  */
17 class MigrateFieldInstanceTest extends MigrateDrupal7TestBase {
18
19   /**
20    * The modules to be enabled during the test.
21    *
22    * @var array
23    */
24   public static $modules = [
25     'comment',
26     'datetime',
27     'file',
28     'image',
29     'link',
30     'node',
31     'system',
32     'taxonomy',
33     'telephone',
34     'text',
35   ];
36
37   /**
38    * {@inheritdoc}
39    */
40   protected function setUp() {
41     parent::setUp();
42     $this->installConfig(static::$modules);
43     $this->createType('page');
44     $this->createType('article');
45     $this->createType('blog');
46     $this->createType('book');
47     $this->createType('forum');
48     $this->createType('test_content_type');
49     Vocabulary::create(['vid' => 'test_vocabulary'])->save();
50     $this->executeMigrations(['d7_field', 'd7_field_instance']);
51   }
52
53   /**
54    * Creates a node type with a corresponding comment type.
55    *
56    * @param string $id
57    *   The node type ID.
58    */
59   protected function createType($id) {
60     NodeType::create([
61       'type' => $id,
62       'label' => $this->randomString(),
63     ])->save();
64
65     CommentType::create([
66       'id' => 'comment_node_' . $id,
67       'label' => $this->randomString(),
68       'target_entity_type_id' => 'node',
69     ])->save();
70   }
71
72   /**
73    * Asserts various aspects of a field config entity.
74    *
75    * @param string $id
76    *   The entity ID in the form ENTITY_TYPE.BUNDLE.FIELD_NAME.
77    * @param string $expected_label
78    *   The expected field label.
79    * @param string $expected_field_type
80    *   The expected field type.
81    * @param bool $is_required
82    *   Whether or not the field is required.
83    * @param bool $expected_translatable
84    *   Whether or not the field is expected to be translatable.
85    */
86   protected function assertEntity($id, $expected_label, $expected_field_type, $is_required, $expected_translatable) {
87     list ($expected_entity_type, $expected_bundle, $expected_name) = explode('.', $id);
88
89     /** @var \Drupal\field\FieldConfigInterface $field */
90     $field = FieldConfig::load($id);
91     $this->assertTrue($field instanceof FieldConfigInterface);
92     $this->assertIdentical($expected_label, $field->label());
93     $this->assertIdentical($expected_field_type, $field->getType());
94     $this->assertIdentical($expected_entity_type, $field->getTargetEntityTypeId());
95     $this->assertIdentical($expected_bundle, $field->getTargetBundle());
96     $this->assertIdentical($expected_name, $field->getName());
97     $this->assertEqual($is_required, $field->isRequired());
98     $this->assertIdentical($expected_entity_type . '.' . $expected_name, $field->getFieldStorageDefinition()->id());
99     $this->assertSame($expected_translatable, $field->isTranslatable());
100   }
101
102   /**
103    * Asserts the settings of a link field config entity.
104    *
105    * @param $id
106    *   The entity ID in the form ENTITY_TYPE.BUNDLE.FIELD_NAME.
107    * @param $title_setting
108    *   The expected title setting.
109    */
110   protected function assertLinkFields($id, $title_setting) {
111     $field = FieldConfig::load($id);
112     $this->assertSame($title_setting, $field->getSetting('title'));
113   }
114
115   /**
116    * Tests migrating D7 field instances to field_config entities.
117    */
118   public function testFieldInstances() {
119     $this->assertEntity('comment.comment_node_page.comment_body', 'Comment', 'text_long', TRUE, FALSE);
120     $this->assertEntity('node.page.body', 'Body', 'text_with_summary', FALSE, FALSE);
121     $this->assertEntity('comment.comment_node_article.comment_body', 'Comment', 'text_long', TRUE, FALSE);
122     $this->assertEntity('node.article.body', 'Body', 'text_with_summary', FALSE, TRUE);
123     $this->assertEntity('node.article.field_tags', 'Tags', 'entity_reference', FALSE, TRUE);
124     $this->assertEntity('node.article.field_image', 'Image', 'image', FALSE, TRUE);
125     $this->assertEntity('comment.comment_node_blog.comment_body', 'Comment', 'text_long', TRUE, FALSE);
126     $this->assertEntity('node.blog.body', 'Body', 'text_with_summary', FALSE, TRUE);
127     $this->assertEntity('comment.comment_node_book.comment_body', 'Comment', 'text_long', TRUE, FALSE);
128     $this->assertEntity('node.book.body', 'Body', 'text_with_summary', FALSE, FALSE);
129     $this->assertEntity('node.forum.taxonomy_forums', 'Forums', 'entity_reference', TRUE, FALSE);
130     $this->assertEntity('comment.comment_node_forum.comment_body', 'Comment', 'text_long', TRUE, FALSE);
131     $this->assertEntity('node.forum.body', 'Body', 'text_with_summary', FALSE, FALSE);
132     $this->assertEntity('comment.comment_node_test_content_type.comment_body', 'Comment', 'text_long', TRUE, FALSE);
133     $this->assertEntity('node.test_content_type.field_boolean', 'Boolean', 'boolean', FALSE, FALSE);
134     $this->assertEntity('node.test_content_type.field_email', 'Email', 'email', FALSE, FALSE);
135     $this->assertEntity('node.test_content_type.field_phone', 'Phone', 'telephone', TRUE, FALSE);
136     $this->assertEntity('node.test_content_type.field_date', 'Date', 'datetime', FALSE, FALSE);
137     $this->assertEntity('node.test_content_type.field_date_with_end_time', 'Date With End Time', 'datetime', FALSE, FALSE);
138     $this->assertEntity('node.test_content_type.field_file', 'File', 'file', FALSE, FALSE);
139     $this->assertEntity('node.test_content_type.field_float', 'Float', 'float', FALSE, FALSE);
140     $this->assertEntity('node.test_content_type.field_images', 'Images', 'image', TRUE, FALSE);
141     $this->assertEntity('node.test_content_type.field_integer', 'Integer', 'integer', TRUE, FALSE);
142     $this->assertEntity('node.test_content_type.field_link', 'Link', 'link', FALSE, FALSE);
143     $this->assertEntity('node.test_content_type.field_text_list', 'Text List', 'list_string', FALSE, FALSE);
144     $this->assertEntity('node.test_content_type.field_integer_list', 'Integer List', 'list_integer', FALSE, FALSE);
145     $this->assertEntity('node.test_content_type.field_long_text', 'Long text', 'text_with_summary', FALSE, FALSE);
146     $this->assertEntity('node.test_content_type.field_term_reference', 'Term Reference', 'entity_reference', FALSE, FALSE);
147     $this->assertEntity('node.test_content_type.field_text', 'Text', 'text', FALSE, FALSE);
148     $this->assertEntity('comment.comment_node_test_content_type.field_integer', 'Integer', 'integer', FALSE, FALSE);
149     $this->assertEntity('user.user.field_file', 'File', 'file', FALSE, FALSE);
150
151
152     $this->assertLinkFields('node.test_content_type.field_link', DRUPAL_OPTIONAL);
153     $this->assertLinkFields('node.article.field_link', DRUPAL_DISABLED);
154     $this->assertLinkFields('node.blog.field_link', DRUPAL_REQUIRED);
155   }
156
157 }