ShapeItemTest.php in Drupal 9
File
core/modules/field/tests/src/Kernel/ShapeItemTest.php
View source
<?php
namespace Drupal\Tests\field\Kernel;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
class ShapeItemTest extends FieldKernelTestBase {
protected static $modules = [
'field_test',
];
protected $fieldName = 'field_shape';
protected function setUp() : void {
parent::setUp();
FieldStorageConfig::create([
'field_name' => $this->fieldName,
'entity_type' => 'entity_test',
'type' => 'shape',
])
->save();
FieldConfig::create([
'entity_type' => 'entity_test',
'field_name' => $this->fieldName,
'bundle' => 'entity_test',
])
->save();
}
public function testShapeItem() {
$entity = EntityTest::create();
$shape = 'cube';
$color = 'blue';
$entity->{$this->fieldName}->shape = $shape;
$entity->{$this->fieldName}->color = $color;
$entity->name->value = $this
->randomMachineName();
$entity
->save();
$id = $entity
->id();
$entity = EntityTest::load($id);
$this
->assertInstanceOf(FieldItemListInterface::class, $entity->{$this->fieldName});
$this
->assertInstanceOf(FieldItemInterface::class, $entity->{$this->fieldName}[0]);
$this
->assertEquals($shape, $entity->{$this->fieldName}->shape);
$this
->assertEquals($color, $entity->{$this->fieldName}->color);
$this
->assertEquals($shape, $entity->{$this->fieldName}[0]->shape);
$this
->assertEquals($color, $entity->{$this->fieldName}[0]->color);
$new_shape = 'circle';
$new_color = 'red';
$entity->{$this->fieldName}->shape = $new_shape;
$entity->{$this->fieldName}->color = $new_color;
$this
->assertEquals($new_shape, $entity->{$this->fieldName}->shape);
$this
->assertEquals($new_color, $entity->{$this->fieldName}->color);
$entity
->save();
$entity = EntityTest::load($id);
$this
->assertEquals($new_shape, $entity->{$this->fieldName}->shape);
$this
->assertEquals($new_color, $entity->{$this->fieldName}->color);
}
}