Version 1
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Field / FieldItemTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Field;
4
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;
11
12 /**
13  * Test field item methods.
14  *
15  * @group Field
16  */
17 class FieldItemTest extends EntityKernelTestBase {
18
19   /**
20    * @var string
21    */
22   protected $fieldName;
23
24   /**
25    * {@inheritdoc}
26    */
27   protected function setUp() {
28     parent::setUp();
29
30     $this->container->get('state')->set('entity_test.field_test_item', TRUE);
31     $this->entityManager->clearCachedDefinitions();
32
33     $entity_type_id = 'entity_test_mulrev';
34     $this->installEntitySchema($entity_type_id);
35
36     $this->fieldName = Unicode::strtolower($this->randomMachineName());
37
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,
43       'cardinality' => 1,
44     ])->save();
45
46     FieldConfig::create([
47       'entity_type' => $entity_type_id,
48       'field_name' => $this->fieldName,
49       'bundle' => $entity_type_id,
50       'label' => 'Test field',
51     ])->save();
52
53     $this->entityManager->clearCachedDefinitions();
54     $definitions = $this->entityManager->getFieldStorageDefinitions($entity_type_id);
55     $this->assertTrue(!empty($definitions[$this->fieldName]));
56   }
57
58   /**
59    * Tests the field item save workflow.
60    */
61   public function testSaveWorkflow() {
62     $entity = EntityTestMulRev::create([
63       'name' => $this->randomString(),
64       'field_test_item' => $this->randomString(),
65       $this->fieldName => $this->randomString(),
66     ]);
67
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");
72
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');
76
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");
80   }
81
82   /**
83    * Checks that the saved field item value matches the expected one.
84    *
85    * @param \Drupal\entity_test\Entity\EntityTest $entity
86    *   The test entity.
87    * @param $expected_value
88    *   The expected field item value.
89    *
90    * @return bool
91    *   TRUE if the item value matches expectations, FALSE otherwise.
92    */
93   protected function assertSavedFieldItemValue(EntityTest $entity, $expected_value) {
94     $entity->setNewRevision(TRUE);
95     $entity->save();
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);
102     return $result;
103   }
104
105 }