3 namespace Drupal\KernelTests\Core\Field;
5 use Drupal\entity_test\Entity\EntityTest;
6 use Drupal\entity_test\Entity\EntityTestMulRev;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
12 * Test field item methods.
16 class FieldItemTest extends EntityKernelTestBase {
26 protected function setUp() {
29 $this->container->get('state')->set('entity_test.field_test_item', TRUE);
30 $this->entityManager->clearCachedDefinitions();
32 $entity_type_id = 'entity_test_mulrev';
33 $this->installEntitySchema($entity_type_id);
35 $this->fieldName = mb_strtolower($this->randomMachineName());
37 /** @var \Drupal\field\Entity\FieldStorageConfig $field_storage */
38 FieldStorageConfig::create([
39 'field_name' => $this->fieldName,
40 'type' => 'field_test',
41 'entity_type' => $entity_type_id,
46 'entity_type' => $entity_type_id,
47 'field_name' => $this->fieldName,
48 'bundle' => $entity_type_id,
49 'label' => 'Test field',
52 $this->entityManager->clearCachedDefinitions();
53 $definitions = $this->entityManager->getFieldStorageDefinitions($entity_type_id);
54 $this->assertTrue(!empty($definitions[$this->fieldName]));
58 * Tests the field item save workflow.
60 public function testSaveWorkflow() {
61 $entity = EntityTestMulRev::create([
62 'name' => $this->randomString(),
63 'field_test_item' => $this->randomString(),
64 $this->fieldName => $this->randomString(),
67 // Save a new entity and verify that the initial field value is overwritten
68 // with a value containing the entity id, which implies a resave. Check that
69 // the entity data structure and the stored values match.
70 $this->assertSavedFieldItemValue($entity, "field_test:{$this->fieldName}:1:1");
72 // Update the entity and verify that the field value is overwritten on
73 // presave if it is not resaved.
74 $this->assertSavedFieldItemValue($entity, 'overwritten');
76 // Flag the field value as needing to be resaved and verify it actually is.
77 $entity->field_test_item->value = $entity->{$this->fieldName}->value = 'resave';
78 $this->assertSavedFieldItemValue($entity, "field_test:{$this->fieldName}:1:3");
82 * Checks that the saved field item value matches the expected one.
84 * @param \Drupal\entity_test\Entity\EntityTest $entity
86 * @param $expected_value
87 * The expected field item value.
90 * TRUE if the item value matches expectations, FALSE otherwise.
92 protected function assertSavedFieldItemValue(EntityTest $entity, $expected_value) {
93 $entity->setNewRevision(TRUE);
95 $base_field_expected_value = str_replace($this->fieldName, 'field_test_item', $expected_value);
96 $result = $this->assertEqual($entity->field_test_item->value, $base_field_expected_value);
97 $result = $result && $this->assertEqual($entity->{$this->fieldName}->value, $expected_value);
98 $entity = $this->reloadEntity($entity);
99 $result = $result && $this->assertEqual($entity->field_test_item->value, $base_field_expected_value);
100 $result = $result && $this->assertEqual($entity->{$this->fieldName}->value, $expected_value);