Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / field / tests / src / Kernel / Migrate / d6 / MigrateFieldInstanceTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Kernel\Migrate\d6;
4
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;
9
10 /**
11  * Migrate field instances.
12  *
13  * @group migrate_drupal_6
14  */
15 class MigrateFieldInstanceTest extends MigrateDrupal6TestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['menu_ui'];
21
22   /**
23    * Tests migration of file variables to file.settings.yml.
24    */
25   public function testFieldInstanceMigration() {
26     $this->migrateFields();
27
28     $entity = Node::create(['type' => 'story']);
29     // Test a text field.
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);
36
37     // Test a number field.
38     $field = FieldConfig::load('node.story.field_test_two');
39     $this->assertIdentical('Integer Field', $field->label());
40     $expected = [
41       'min' => 10,
42       'max' => 100,
43       'prefix' => 'pref',
44       'suffix' => 'suf',
45       'unsigned' => FALSE,
46       'size' => 'normal',
47     ];
48     $this->assertIdentical($expected, $field->getSettings());
49
50     $field = FieldConfig::load('node.story.field_test_four');
51     $this->assertIdentical('Float Field', $field->label());
52     $expected = [
53       'min' => 100.0,
54       'max' => 200.0,
55       'prefix' => 'id-',
56       'suffix' => '',
57     ];
58     $this->assertIdentical($expected, $field->getSettings());
59
60     // Test email field.
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);
64
65     // Test image field.
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']);
74
75     // Test a filefield.
76     $field = FieldConfig::load('node.story.field_test_filefield');
77     $this->assertIdentical('File Field', $field->label());
78     $expected = [
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' => [],
89     ];
90     $field_settings = $field->getSettings();
91     ksort($expected);
92     ksort($field_settings);
93     // This is the only way to compare arrays.
94     $this->assertIdentical($expected, $field_settings);
95
96     // Test a link field.
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']);
104
105     // Test date field.
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());
112     $expected = [
113       [
114         'default_date_type' => 'relative',
115         'default_date' => 'blank',
116       ],
117     ];
118     $this->assertSame($expected, $field->getDefaultValueLiteral());
119     $this->assertTrue($field->isTranslatable());
120
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());
128     $expected = [];
129     $this->assertSame($expected, $field->getDefaultValueLiteral());
130     $this->assertTrue($field->isTranslatable());
131
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());
137     $expected = [];
138     $this->assertSame($expected, $field->getSettings());
139     $expected = [];
140     $this->assertSame($expected, $field->getDefaultValueLiteral());
141     $this->assertTrue($field->isTranslatable());
142
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());
153
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());
164   }
165
166   /**
167    * Tests migrating fields into non-existent content types.
168    */
169   public function testMigrateFieldIntoUnknownNodeType() {
170     $this->sourceDatabase->delete('node_type')
171       ->condition('type', 'test_planet')
172       ->execute();
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();
176
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'));
180   }
181
182 }