Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / field / tests / src / Kernel / ShapeItemTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Kernel;
4
5 use Drupal\Core\Field\FieldItemInterface;
6 use Drupal\Core\Field\FieldItemListInterface;
7 use Drupal\entity_test\Entity\EntityTest;
8 use Drupal\field\Entity\FieldConfig;
9 use Drupal\field\Entity\FieldStorageConfig;
10
11 /**
12  * Tests the new entity API for the shape field type.
13  *
14  * @group field
15  */
16 class ShapeItemTest extends FieldKernelTestBase {
17
18   /**
19    * Modules to enable.
20    *
21    * @var array
22    */
23   public static $modules = ['field_test'];
24
25   /**
26    * The name of the field to use in this test.
27    *
28    * @var string
29    */
30   protected $fieldName = 'field_shape';
31
32   protected function setUp() {
33     parent::setUp();
34
35     // Create a 'shape' field and storage for validation.
36     FieldStorageConfig::create([
37       'field_name' => $this->fieldName,
38       'entity_type' => 'entity_test',
39       'type' => 'shape',
40     ])->save();
41     FieldConfig::create([
42       'entity_type' => 'entity_test',
43       'field_name' => $this->fieldName,
44       'bundle' => 'entity_test',
45     ])->save();
46   }
47
48   /**
49    * Tests using entity fields of the field field type.
50    */
51   public function testShapeItem() {
52     // Verify entity creation.
53     $entity = EntityTest::create();
54     $shape = 'cube';
55     $color = 'blue';
56     $entity->{$this->fieldName}->shape = $shape;
57     $entity->{$this->fieldName}->color = $color;
58     $entity->name->value = $this->randomMachineName();
59     $entity->save();
60
61     // Verify entity has been created properly.
62     $id = $entity->id();
63     $entity = EntityTest::load($id);
64     $this->assertTrue($entity->{$this->fieldName} instanceof FieldItemListInterface, 'Field implements interface.');
65     $this->assertTrue($entity->{$this->fieldName}[0] instanceof FieldItemInterface, 'Field item implements interface.');
66     $this->assertEqual($entity->{$this->fieldName}->shape, $shape);
67     $this->assertEqual($entity->{$this->fieldName}->color, $color);
68     $this->assertEqual($entity->{$this->fieldName}[0]->shape, $shape);
69     $this->assertEqual($entity->{$this->fieldName}[0]->color, $color);
70
71     // Verify changing the field value.
72     $new_shape = 'circle';
73     $new_color = 'red';
74     $entity->{$this->fieldName}->shape = $new_shape;
75     $entity->{$this->fieldName}->color = $new_color;
76     $this->assertEqual($entity->{$this->fieldName}->shape, $new_shape);
77     $this->assertEqual($entity->{$this->fieldName}->color, $new_color);
78
79     // Read changed entity and assert changed values.
80     $entity->save();
81     $entity = EntityTest::load($id);
82     $this->assertEqual($entity->{$this->fieldName}->shape, $new_shape);
83     $this->assertEqual($entity->{$this->fieldName}->color, $new_color);
84   }
85
86 }