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