3 namespace Drupal\Tests\field\Kernel;
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;
13 * Parent class for Field API unit tests.
15 abstract class FieldKernelTestBase extends KernelTestBase {
22 public static $modules = ['user', 'system', 'field', 'text', 'entity_test', 'field_test'];
25 * Bag of created field storages and fields.
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]
34 * @see \Drupal\field\Tests\FieldUnitTestBase::createFieldWithStorage()
38 protected $fieldTestData;
41 * Set the default field storage backend for fields created during tests.
43 protected function setUp() {
46 $this->fieldTestData = new \ArrayObject([], \ArrayObject::ARRAY_AS_PROPS);
48 $this->installEntitySchema('entity_test');
49 $this->installEntitySchema('user');
50 $this->installSchema('system', ['sequences', 'key_value']);
52 // Set default storage backend and configure the theme system.
53 $this->installConfig(['field', 'system']);
56 $storage = \Drupal::entityManager()->getStorage('user');
60 'name' => 'entity-test',
61 'mail' => 'entity@localhost',
68 * Create a field and an associated field storage.
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.
80 protected function createFieldWithStorage($suffix = '', $entity_type = 'entity_test', $bundle = NULL) {
82 $bundle = $entity_type;
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;
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',
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,
102 'label' => $this->randomMachineName() . '_label',
103 'description' => $this->randomMachineName() . '_description',
105 'test_field_setting' => $this->randomMachineName(),
108 $this->fieldTestData->$field = FieldConfig::create($this->fieldTestData->$field_definition);
109 $this->fieldTestData->$field->save();
111 entity_get_form_display($entity_type, $bundle, 'default')
112 ->setComponent($this->fieldTestData->$field_name, [
113 'type' => 'test_field_widget',
115 'test_widget_setting' => $this->randomMachineName(),
122 * Saves and reloads an entity.
124 * @param \Drupal\Core\Entity\EntityInterface $entity
125 * The entity to save.
127 * @return \Drupal\Core\Entity\EntityInterface
128 * The entity, freshly reloaded from storage.
130 protected function entitySaveReload(EntityInterface $entity) {
132 $controller = $this->container->get('entity.manager')->getStorage($entity->getEntityTypeId());
133 $controller->resetCache();
134 return $controller->load($entity->id());
138 * Validate and save entity. Fail if violations are found.
140 * @param \Drupal\Core\Entity\EntityInterface $entity
141 * The entity to save.
143 protected function entityValidateAndSave(EntityInterface $entity) {
144 $violations = $entity->validate();
145 if ($violations->count()) {
146 $this->fail($violations);
154 * Generate random values for a field_test field.
156 * @param $cardinality
157 * Number of values to generate.
159 * An array of random values, in the format expected for field values.
161 protected function _generateTestFieldValues($cardinality) {
163 for ($i = 0; $i < $cardinality; $i++) {
164 // field_test fields treat 0 as 'empty value'.
165 $values[$i]['value'] = mt_rand(1, 127);
171 * Assert that a field has the expected values in an entity.
173 * This function only checks a single column in the field values.
175 * @param EntityInterface $entity
176 * The entity to test.
178 * The name of the field to test
179 * @param $expected_values
180 * The array of expected values.
182 * (Optional) The language code for the values. Defaults to
183 * \Drupal\Core\Language\LanguageInterface::LANGCODE_NOT_SPECIFIED.
185 * (Optional) The name of the column to check. Defaults to 'value'.
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);
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]));