3 namespace Drupal\Tests\field\Kernel\Migrate\d6;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\link\LinkItemInterface;
7 use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
8 use Drupal\node\Entity\Node;
11 * Migrate field instances.
13 * @group migrate_drupal_6
15 class MigrateFieldInstanceTest extends MigrateDrupal6TestBase {
20 public static $modules = ['menu_ui'];
23 * Tests migration of file variables to file.settings.yml.
25 public function testFieldInstanceMigration() {
26 $this->migrateFields();
28 $entity = Node::create(['type' => 'story']);
30 /** @var \Drupal\field\FieldConfigInterface $field */
31 $field = FieldConfig::load('node.story.field_test');
32 $this->assertIdentical('Text Field', $field->label());
33 // field_test is a text_long field, which have no settings.
34 $this->assertIdentical([], $field->getSettings());
35 $this->assertIdentical('text for default value', $entity->field_test->value);
37 // Test a number field.
38 $field = FieldConfig::load('node.story.field_test_two');
39 $this->assertIdentical('Integer Field', $field->label());
48 $this->assertIdentical($expected, $field->getSettings());
50 $field = FieldConfig::load('node.story.field_test_four');
51 $this->assertIdentical('Float Field', $field->label());
58 $this->assertIdentical($expected, $field->getSettings());
61 $field = FieldConfig::load('node.story.field_test_email');
62 $this->assertIdentical('Email Field', $field->label());
63 $this->assertIdentical('benjy@example.com', $entity->field_test_email->value);
66 $field = FieldConfig::load('node.story.field_test_imagefield');
67 $this->assertIdentical('Image Field', $field->label());
68 $field_settings = $field->getSettings();
69 $this->assertIdentical('', $field_settings['max_resolution']);
70 $this->assertIdentical('', $field_settings['min_resolution']);
71 $this->assertIdentical('', $field_settings['file_directory']);
72 $this->assertIdentical('png gif jpg jpeg', $field_settings['file_extensions']);
73 $this->assertIdentical('public', $field_settings['uri_scheme']);
76 $field = FieldConfig::load('node.story.field_test_filefield');
77 $this->assertIdentical('File Field', $field->label());
79 'file_extensions' => 'txt pdf doc',
80 'file_directory' => 'images',
81 'description_field' => TRUE,
82 'max_filesize' => '200KB',
83 'target_type' => 'file',
84 'display_field' => FALSE,
85 'display_default' => FALSE,
86 'uri_scheme' => 'public',
87 'handler' => 'default:file',
88 'handler_settings' => [],
90 $field_settings = $field->getSettings();
92 ksort($field_settings);
93 // This is the only way to compare arrays.
94 $this->assertIdentical($expected, $field_settings);
97 $field = FieldConfig::load('node.story.field_test_link');
98 $this->assertIdentical('Link Field', $field->label());
99 $expected = ['title' => 2, 'link_type' => LinkItemInterface::LINK_GENERIC];
100 $this->assertIdentical($expected, $field->getSettings());
101 $this->assertIdentical('default link title', $entity->field_test_link->title, 'Field field_test_link default title is correct.');
102 $this->assertIdentical('https://www.drupal.org', $entity->field_test_link->url, 'Field field_test_link default title is correct.');
103 $this->assertIdentical([], $entity->field_test_link->options['attributes']);
106 $field = FieldConfig::load('node.story.field_test_date');
107 $this->assertInstanceOf(FieldConfig::class, $field);
108 $this->assertSame('Date Field', $field->label());
109 $this->assertSame('An example date field.', $field->getDescription());
110 $expected = ['datetime_type' => 'datetime'];
111 $this->assertSame($expected, $field->getSettings());
114 'default_date_type' => 'relative',
115 'default_date' => 'blank',
118 $this->assertSame($expected, $field->getDefaultValueLiteral());
119 $this->assertTrue($field->isTranslatable());
121 // Test datetime field.
122 $field = FieldConfig::load('node.story.field_test_datetime');
123 $this->assertInstanceOf(FieldConfig::class, $field);
124 $this->assertSame('Datetime Field', $field->label());
125 $this->assertSame('An example datetime field.', $field->getDescription());
126 $expected = ['datetime_type' => 'datetime'];
127 $this->assertSame($expected, $field->getSettings());
129 $this->assertSame($expected, $field->getDefaultValueLiteral());
130 $this->assertTrue($field->isTranslatable());
132 // Test datestamp field.
133 $field = FieldConfig::load('node.story.field_test_datestamp');
134 $this->assertInstanceOf(FieldConfig::class, $field);
135 $this->assertSame('Date Stamp Field', $field->label());
136 $this->assertSame('An example date stamp field.', $field->getDescription());
138 $this->assertSame($expected, $field->getSettings());
140 $this->assertSame($expected, $field->getDefaultValueLiteral());
141 $this->assertTrue($field->isTranslatable());
143 // Test a node reference field, migrated to entity reference.
144 $field = FieldConfig::load('node.employee.field_company');
145 $this->assertInstanceOf(FieldConfig::class, $field);
146 $this->assertSame('entity_reference', $field->getType());
147 $this->assertSame('Company', $field->label());
148 $this->assertSame('default:node', $field->getSetting('handler'));
149 $this->assertSame([], $field->getSetting('handler_settings'));
150 $this->assertSame('node', $field->getSetting('target_type'));
151 $this->assertSame([], $field->getDefaultValueLiteral());
152 $this->assertTrue($field->isTranslatable());
154 // Test a user reference field, migrated to entity reference.
155 $field = FieldConfig::load('node.employee.field_commander');
156 $this->assertInstanceOf(FieldConfig::class, $field);
157 $this->assertSame('entity_reference', $field->getType());
158 $this->assertSame('Commanding Officer', $field->label());
159 $this->assertSame('default:user', $field->getSetting('handler'));
160 $this->assertSame([], $field->getSetting('handler_settings'));
161 $this->assertSame('user', $field->getSetting('target_type'));
162 $this->assertSame([], $field->getDefaultValueLiteral());
163 $this->assertTrue($field->isTranslatable());
167 * Tests migrating fields into non-existent content types.
169 public function testMigrateFieldIntoUnknownNodeType() {
170 $this->sourceDatabase->delete('node_type')
171 ->condition('type', 'test_planet')
173 // The field migrations use the migration plugin to ensure that the node
174 // types exist, so this should produce no failures...
175 $this->migrateFields();
177 // ...and the field instances should not have been migrated.
178 $this->assertNull(FieldConfig::load('node.test_planet.field_multivalue'));
179 $this->assertNull(FieldConfig::load('node.test_planet.field_test_text_single_checkbox'));