ba47d91307ce0b6be3a67cc2455e814fcbcfb844
[yaffs-website] / web / core / modules / field / tests / src / Kernel / Number / NumberItemTest.php
1 <?php
2
3 namespace Drupal\Tests\field\Kernel\Number;
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\Tests\field\Kernel\FieldKernelTestBase;
10 use Drupal\field\Entity\FieldStorageConfig;
11
12 /**
13  * Tests the new entity API for the number field type.
14  *
15  * @group field
16  */
17 class NumberItemTest extends FieldKernelTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = [];
25
26   protected function setUp() {
27     parent::setUp();
28
29     // Create number field storages and fields for validation.
30     foreach (['integer', 'float', 'decimal'] as $type) {
31       FieldStorageConfig::create([
32         'entity_type' => 'entity_test',
33         'field_name' => 'field_' . $type,
34         'type' => $type,
35       ])->save();
36       FieldConfig::create([
37         'entity_type' => 'entity_test',
38         'field_name' => 'field_' . $type,
39         'bundle' => 'entity_test',
40       ])->save();
41     }
42   }
43
44   /**
45    * Tests using entity fields of the number field type.
46    */
47   public function testNumberItem() {
48     // Verify entity creation.
49     $entity = EntityTest::create();
50     $integer = rand(0, 10);
51     $entity->field_integer = $integer;
52     $float = 3.14;
53     $entity->field_float = $float;
54     $entity->field_decimal = '20-40';
55     $violations = $entity->validate();
56     $this->assertIdentical(1, count($violations), 'Wrong decimal value causes validation error');
57     $decimal = '31.3';
58     $entity->field_decimal = $decimal;
59     $entity->name->value = $this->randomMachineName();
60     $entity->save();
61
62     // Verify entity has been created properly.
63     $id = $entity->id();
64     $entity = EntityTest::load($id);
65     $this->assertTrue($entity->field_integer instanceof FieldItemListInterface, 'Field implements interface.');
66     $this->assertTrue($entity->field_integer[0] instanceof FieldItemInterface, 'Field item implements interface.');
67     $this->assertEqual($entity->field_integer->value, $integer);
68     $this->assertEqual($entity->field_integer[0]->value, $integer);
69     $this->assertTrue($entity->field_float instanceof FieldItemListInterface, 'Field implements interface.');
70     $this->assertTrue($entity->field_float[0] instanceof FieldItemInterface, 'Field item implements interface.');
71     $this->assertEqual($entity->field_float->value, $float);
72     $this->assertEqual($entity->field_float[0]->value, $float);
73     $this->assertTrue($entity->field_decimal instanceof FieldItemListInterface, 'Field implements interface.');
74     $this->assertTrue($entity->field_decimal[0] instanceof FieldItemInterface, 'Field item implements interface.');
75     $this->assertEqual($entity->field_decimal->value, $decimal);
76     $this->assertEqual($entity->field_decimal[0]->value, $decimal);
77
78     // Verify changing the number value.
79     $new_integer = rand(11, 20);
80     $new_float = rand(1001, 2000) / 100;
81     $new_decimal = '18.2';
82     $entity->field_integer->value = $new_integer;
83     $this->assertEqual($entity->field_integer->value, $new_integer);
84     $entity->field_float->value = $new_float;
85     $this->assertEqual($entity->field_float->value, $new_float);
86     $entity->field_decimal->value = $new_decimal;
87     $this->assertEqual($entity->field_decimal->value, $new_decimal);
88
89     // Read changed entity and assert changed values.
90     $entity->save();
91     $entity = EntityTest::load($id);
92     $this->assertEqual($entity->field_integer->value, $new_integer);
93     $this->assertEqual($entity->field_float->value, $new_float);
94     $this->assertEqual($entity->field_decimal->value, $new_decimal);
95
96     // Test sample item generation.
97     $entity = EntityTest::create();
98     $entity->field_integer->generateSampleItems();
99     $entity->field_float->generateSampleItems();
100     $entity->field_decimal->generateSampleItems();
101     $this->entityValidateAndSave($entity);
102   }
103
104 }