comparison core/modules/field/tests/src/Kernel/TestItemTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\field\Kernel;
4
5 use Drupal\Core\Field\BaseFieldDefinition;
6 use Drupal\Core\Field\FieldItemInterface;
7 use Drupal\Core\Field\FieldItemListInterface;
8 use Drupal\entity_test\Entity\EntityTest;
9 use Drupal\field\Entity\FieldConfig;
10 use Drupal\field\Entity\FieldStorageConfig;
11
12 /**
13 * Tests the new entity API for the test field type.
14 *
15 * @group field
16 */
17 class TestItemTest extends FieldKernelTestBase {
18
19 /**
20 * Modules to enable.
21 *
22 * @var array
23 */
24 public static $modules = ['field_test'];
25
26 /**
27 * The name of the field to use in this test.
28 *
29 * @var string
30 */
31 protected $fieldName = 'field_test';
32
33 protected function setUp() {
34 parent::setUp();
35
36 // Create a 'test_field' field and storage for validation.
37 FieldStorageConfig::create([
38 'field_name' => $this->fieldName,
39 'entity_type' => 'entity_test',
40 'type' => 'test_field',
41 ])->save();
42 FieldConfig::create([
43 'entity_type' => 'entity_test',
44 'field_name' => $this->fieldName,
45 'bundle' => 'entity_test',
46 ])->save();
47 }
48
49 /**
50 * Tests using entity fields of the field field type.
51 */
52 public function testTestItem() {
53 // Verify entity creation.
54 $entity = EntityTest::create();
55 $value = rand(1, 10);
56 $entity->field_test = $value;
57 $entity->name->value = $this->randomMachineName();
58 $entity->save();
59
60 // Verify entity has been created properly.
61 $id = $entity->id();
62 $entity = EntityTest::load($id);
63 $this->assertTrue($entity->{$this->fieldName} instanceof FieldItemListInterface, 'Field implements interface.');
64 $this->assertTrue($entity->{$this->fieldName}[0] instanceof FieldItemInterface, 'Field item implements interface.');
65 $this->assertEqual($entity->{$this->fieldName}->value, $value);
66 $this->assertEqual($entity->{$this->fieldName}[0]->value, $value);
67
68 // Verify changing the field value.
69 $new_value = rand(1, 10);
70 $entity->field_test->value = $new_value;
71 $this->assertEqual($entity->{$this->fieldName}->value, $new_value);
72
73 // Read changed entity and assert changed values.
74 $entity->save();
75 $entity = EntityTest::load($id);
76 $this->assertEqual($entity->{$this->fieldName}->value, $new_value);
77
78 // Test the schema for this field type.
79 $expected_schema = [
80 'columns' => [
81 'value' => [
82 'type' => 'int',
83 'size' => 'medium',
84 ],
85 ],
86 'unique keys' => [],
87 'indexes' => [
88 'value' => ['value'],
89 ],
90 'foreign keys' => [],
91 ];
92 $field_schema = BaseFieldDefinition::create('test_field')->getSchema();
93 $this->assertEqual($field_schema, $expected_schema);
94 }
95
96 }