3 namespace Drupal\KernelTests\Core\Field;
5 use Drupal\Component\Utility\Unicode;
6 use Drupal\entity_test\Entity\EntityTest;
7 use Drupal\entity_test\Entity\EntityTestMulRev;
8 use Drupal\field\Entity\FieldConfig;
9 use Drupal\field\Entity\FieldStorageConfig;
10 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
13 * Test field item methods.
17 class FieldItemTest extends EntityKernelTestBase {
27 protected function setUp() {
30 $this->container->get('state')->set('entity_test.field_test_item', TRUE);
31 $this->entityManager->clearCachedDefinitions();
33 $entity_type_id = 'entity_test_mulrev';
34 $this->installEntitySchema($entity_type_id);
36 $this->fieldName = Unicode::strtolower($this->randomMachineName());
38 /** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
39 FieldStorageConfig::create([
40 'field_name' => $this->fieldName,
41 'type' => 'field_test',
42 'entity_type' => $entity_type_id,
47 'entity_type' => $entity_type_id,
48 'field_name' => $this->fieldName,
49 'bundle' => $entity_type_id,
50 'label' => 'Test field',
53 $this->entityManager->clearCachedDefinitions();
54 $definitions = $this->entityManager->getFieldStorageDefinitions($entity_type_id);
55 $this->assertTrue(!empty($definitions[$this->fieldName]));
59 * Tests the field item save workflow.
61 public function testSaveWorkflow() {
62 $entity = EntityTestMulRev::create([
63 'name' => $this->randomString(),
64 'field_test_item' => $this->randomString(),
65 $this->fieldName => $this->randomString(),
68 // Save a new entity and verify that the initial field value is overwritten
69 // with a value containing the entity id, which implies a resave. Check that
70 // the entity data structure and the stored values match.
71 $this->assertSavedFieldItemValue($entity, "field_test:{$this->fieldName}:1:1");
73 // Update the entity and verify that the field value is overwritten on
74 // presave if it is not resaved.
75 $this->assertSavedFieldItemValue($entity, 'overwritten');
77 // Flag the field value as needing to be resaved and verify it actually is.
78 $entity->field_test_item->value = $entity->{$this->fieldName}->value = 'resave';
79 $this->assertSavedFieldItemValue($entity, "field_test:{$this->fieldName}:1:3");
83 * Checks that the saved field item value matches the expected one.
85 * @param \Drupal\entity_test\Entity\EntityTest $entity
87 * @param $expected_value
88 * The expected field item value.
91 * TRUE if the item value matches expectations, FALSE otherwise.
93 protected function assertSavedFieldItemValue(EntityTest $entity, $expected_value) {
94 $entity->setNewRevision(TRUE);
96 $base_field_expected_value = str_replace($this->fieldName, 'field_test_item', $expected_value);
97 $result = $this->assertEqual($entity->field_test_item->value, $base_field_expected_value);
98 $result = $result && $this->assertEqual($entity->{$this->fieldName}->value, $expected_value);
99 $entity = $this->reloadEntity($entity);
100 $result = $result && $this->assertEqual($entity->field_test_item->value, $base_field_expected_value);
101 $result = $result && $this->assertEqual($entity->{$this->fieldName}->value, $expected_value);