comparison core/modules/field/tests/src/Kernel/FieldKernelTestBase.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 7a779792577d
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\field\Kernel;
4
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\Core\Language\LanguageInterface;
8 use Drupal\field\Entity\FieldConfig;
9 use Drupal\field\Entity\FieldStorageConfig;
10 use Drupal\KernelTests\KernelTestBase;
11
12 /**
13 * Parent class for Field API unit tests.
14 */
15 abstract class FieldKernelTestBase extends KernelTestBase {
16
17 /**
18 * Modules to enable.
19 *
20 * @var array
21 */
22 public static $modules = ['user', 'system', 'field', 'text', 'entity_test', 'field_test'];
23
24 /**
25 * Bag of created field storages and fields.
26 *
27 * Allows easy access to test field storage/field names/IDs/objects via:
28 * - $this->fieldTestData->field_name[suffix]
29 * - $this->fieldTestData->field_storage[suffix]
30 * - $this->fieldTestData->field_storage_uuid[suffix]
31 * - $this->fieldTestData->field[suffix]
32 * - $this->fieldTestData->field_definition[suffix]
33 *
34 * @see \Drupal\field\Tests\FieldUnitTestBase::createFieldWithStorage()
35 *
36 * @var \ArrayObject
37 */
38 protected $fieldTestData;
39
40 /**
41 * Set the default field storage backend for fields created during tests.
42 */
43 protected function setUp() {
44 parent::setUp();
45
46 $this->fieldTestData = new \ArrayObject([], \ArrayObject::ARRAY_AS_PROPS);
47
48 $this->installEntitySchema('entity_test');
49 $this->installEntitySchema('user');
50 $this->installSchema('system', ['sequences', 'key_value']);
51
52 // Set default storage backend and configure the theme system.
53 $this->installConfig(['field', 'system']);
54
55 // Create user 1.
56 $storage = \Drupal::entityManager()->getStorage('user');
57 $storage
58 ->create([
59 'uid' => 1,
60 'name' => 'entity-test',
61 'mail' => 'entity@localhost',
62 'status' => TRUE,
63 ])
64 ->save();
65 }
66
67 /**
68 * Create a field and an associated field storage.
69 *
70 * @param string $suffix
71 * (optional) A string that should only contain characters that are valid in
72 * PHP variable names as well.
73 * @param string $entity_type
74 * (optional) The entity type on which the field should be created.
75 * Defaults to "entity_test".
76 * @param string $bundle
77 * (optional) The entity type on which the field should be created.
78 * Defaults to the default bundle of the entity type.
79 */
80 protected function createFieldWithStorage($suffix = '', $entity_type = 'entity_test', $bundle = NULL) {
81 if (empty($bundle)) {
82 $bundle = $entity_type;
83 }
84 $field_name = 'field_name' . $suffix;
85 $field_storage = 'field_storage' . $suffix;
86 $field_storage_uuid = 'field_storage_uuid' . $suffix;
87 $field = 'field' . $suffix;
88 $field_definition = 'field_definition' . $suffix;
89
90 $this->fieldTestData->$field_name = Unicode::strtolower($this->randomMachineName() . '_field_name' . $suffix);
91 $this->fieldTestData->$field_storage = FieldStorageConfig::create([
92 'field_name' => $this->fieldTestData->$field_name,
93 'entity_type' => $entity_type,
94 'type' => 'test_field',
95 'cardinality' => 4,
96 ]);
97 $this->fieldTestData->$field_storage->save();
98 $this->fieldTestData->$field_storage_uuid = $this->fieldTestData->$field_storage->uuid();
99 $this->fieldTestData->$field_definition = [
100 'field_storage' => $this->fieldTestData->$field_storage,
101 'bundle' => $bundle,
102 'label' => $this->randomMachineName() . '_label',
103 'description' => $this->randomMachineName() . '_description',
104 'settings' => [
105 'test_field_setting' => $this->randomMachineName(),
106 ],
107 ];
108 $this->fieldTestData->$field = FieldConfig::create($this->fieldTestData->$field_definition);
109 $this->fieldTestData->$field->save();
110
111 entity_get_form_display($entity_type, $bundle, 'default')
112 ->setComponent($this->fieldTestData->$field_name, [
113 'type' => 'test_field_widget',
114 'settings' => [
115 'test_widget_setting' => $this->randomMachineName(),
116 ]
117 ])
118 ->save();
119 }
120
121 /**
122 * Saves and reloads an entity.
123 *
124 * @param \Drupal\Core\Entity\EntityInterface $entity
125 * The entity to save.
126 *
127 * @return \Drupal\Core\Entity\EntityInterface
128 * The entity, freshly reloaded from storage.
129 */
130 protected function entitySaveReload(EntityInterface $entity) {
131 $entity->save();
132 $controller = $this->container->get('entity.manager')->getStorage($entity->getEntityTypeId());
133 $controller->resetCache();
134 return $controller->load($entity->id());
135 }
136
137 /**
138 * Validate and save entity. Fail if violations are found.
139 *
140 * @param \Drupal\Core\Entity\EntityInterface $entity
141 * The entity to save.
142 */
143 protected function entityValidateAndSave(EntityInterface $entity) {
144 $violations = $entity->validate();
145 if ($violations->count()) {
146 $this->fail($violations);
147 }
148 else {
149 $entity->save();
150 }
151 }
152
153 /**
154 * Generate random values for a field_test field.
155 *
156 * @param $cardinality
157 * Number of values to generate.
158 * @return
159 * An array of random values, in the format expected for field values.
160 */
161 protected function _generateTestFieldValues($cardinality) {
162 $values = [];
163 for ($i = 0; $i < $cardinality; $i++) {
164 // field_test fields treat 0 as 'empty value'.
165 $values[$i]['value'] = mt_rand(1, 127);
166 }
167 return $values;
168 }
169
170 /**
171 * Assert that a field has the expected values in an entity.
172 *
173 * This function only checks a single column in the field values.
174 *
175 * @param EntityInterface $entity
176 * The entity to test.
177 * @param $field_name
178 * The name of the field to test
179 * @param $expected_values
180 * The array of expected values.
181 * @param $langcode
182 * (Optional) The language code for the values. Defaults to
183 * \Drupal\Core\Language\LanguageInterface::LANGCODE_NOT_SPECIFIED.
184 * @param $column
185 * (Optional) The name of the column to check. Defaults to 'value'.
186 */
187 protected function assertFieldValues(EntityInterface $entity, $field_name, $expected_values, $langcode = LanguageInterface::LANGCODE_NOT_SPECIFIED, $column = 'value') {
188 // Re-load the entity to make sure we have the latest changes.
189 $storage = $this->container->get('entity_type.manager')
190 ->getStorage($entity->getEntityTypeId());
191 $storage->resetCache([$entity->id()]);
192 $e = $storage->load($this->entityId);
193
194 $field = $values = $e->getTranslation($langcode)->$field_name;
195 // Filter out empty values so that they don't mess with the assertions.
196 $field->filterEmptyItems();
197 $values = $field->getValue();
198 $this->assertEqual(count($values), count($expected_values), 'Expected number of values were saved.');
199 foreach ($expected_values as $key => $value) {
200 $this->assertEqual($values[$key][$column], $value, format_string('Value @value was saved correctly.', ['@value' => $value]));
201 }
202 }
203
204 }