Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\field\Tests;
|
Chris@0
|
4
|
Chris@18
|
5 @trigger_error(__NAMESPACE__ . '\FieldTestBase is deprecated for removal before Drupal 9.0.0. Use Drupal\Tests\field\Functional\FieldTestBase instead. See https://www.drupal.org/node/2999939', E_USER_DEPRECATED);
|
Chris@18
|
6
|
Chris@0
|
7 use Drupal\Core\Entity\EntityInterface;
|
Chris@0
|
8 use Drupal\Core\Language\LanguageInterface;
|
Chris@0
|
9 use Drupal\simpletest\WebTestBase;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Parent class for Field API tests.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @deprecated Scheduled for removal in Drupal 9.0.0.
|
Chris@0
|
15 * Use \Drupal\Tests\field\Functional\FieldTestBase instead.
|
Chris@18
|
16 *
|
Chris@18
|
17 * @see https://www.drupal.org/node/2999939
|
Chris@0
|
18 */
|
Chris@0
|
19 abstract class FieldTestBase extends WebTestBase {
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * Generate random values for a field_test field.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @param $cardinality
|
Chris@0
|
25 * Number of values to generate.
|
Chris@0
|
26 * @return
|
Chris@0
|
27 * An array of random values, in the format expected for field values.
|
Chris@0
|
28 */
|
Chris@0
|
29 public function _generateTestFieldValues($cardinality) {
|
Chris@0
|
30 $values = [];
|
Chris@0
|
31 for ($i = 0; $i < $cardinality; $i++) {
|
Chris@0
|
32 // field_test fields treat 0 as 'empty value'.
|
Chris@0
|
33 $values[$i]['value'] = mt_rand(1, 127);
|
Chris@0
|
34 }
|
Chris@0
|
35 return $values;
|
Chris@0
|
36 }
|
Chris@0
|
37
|
Chris@0
|
38 /**
|
Chris@0
|
39 * Assert that a field has the expected values in an entity.
|
Chris@0
|
40 *
|
Chris@0
|
41 * This function only checks a single column in the field values.
|
Chris@0
|
42 *
|
Chris@12
|
43 * @param \Drupal\Core\Entity\EntityInterface $entity
|
Chris@0
|
44 * The entity to test.
|
Chris@0
|
45 * @param $field_name
|
Chris@0
|
46 * The name of the field to test
|
Chris@0
|
47 * @param $expected_values
|
Chris@0
|
48 * The array of expected values.
|
Chris@0
|
49 * @param $langcode
|
Chris@0
|
50 * (Optional) The language code for the values. Defaults to
|
Chris@0
|
51 * \Drupal\Core\Language\LanguageInterface::LANGCODE_DEFAULT.
|
Chris@0
|
52 * @param $column
|
Chris@0
|
53 * (Optional) The name of the column to check. Defaults to 'value'.
|
Chris@0
|
54 */
|
Chris@0
|
55 public function assertFieldValues(EntityInterface $entity, $field_name, $expected_values, $langcode = LanguageInterface::LANGCODE_DEFAULT, $column = 'value') {
|
Chris@0
|
56 // Re-load the entity to make sure we have the latest changes.
|
Chris@0
|
57 $storage = $this->container->get('entity_type.manager')
|
Chris@0
|
58 ->getStorage($entity->getEntityTypeId());
|
Chris@0
|
59 $storage->resetCache([$entity->id()]);
|
Chris@0
|
60 $e = $storage->load($entity->id());
|
Chris@0
|
61
|
Chris@0
|
62 $field = $values = $e->getTranslation($langcode)->$field_name;
|
Chris@0
|
63 // Filter out empty values so that they don't mess with the assertions.
|
Chris@0
|
64 $field->filterEmptyItems();
|
Chris@0
|
65 $values = $field->getValue();
|
Chris@0
|
66 $this->assertEqual(count($values), count($expected_values), 'Expected number of values were saved.');
|
Chris@0
|
67 foreach ($expected_values as $key => $value) {
|
Chris@0
|
68 $this->assertEqual($values[$key][$column], $value, format_string('Value @value was saved correctly.', ['@value' => $value]));
|
Chris@0
|
69 }
|
Chris@0
|
70 }
|
Chris@0
|
71
|
Chris@0
|
72 }
|