class ShapeItemTest
Same name and namespace in other branches
- 11.x core/modules/field/tests/src/Kernel/ShapeItemTest.php \Drupal\Tests\field\Kernel\ShapeItemTest
- 10 core/modules/field/tests/src/Kernel/ShapeItemTest.php \Drupal\Tests\field\Kernel\ShapeItemTest
- 8.9.x core/modules/field/tests/src/Kernel/ShapeItemTest.php \Drupal\Tests\field\Kernel\ShapeItemTest
Tests the new entity API for the shape field type.
@group field
Hierarchy
- class \Drupal\KernelTests\KernelTestBase implements \Drupal\Core\DependencyInjection\ServiceProviderInterface uses \Drupal\KernelTests\AssertLegacyTrait, \Drupal\KernelTests\AssertContentTrait, \Drupal\Tests\RandomGeneratorTrait, \Drupal\Tests\ConfigTestTrait, \Drupal\Tests\ExtensionListTestTrait, \Drupal\Tests\TestRequirementsTrait, \Drupal\Tests\Traits\PhpUnitWarnings, \Drupal\Tests\PhpUnitCompatibilityTrait, \Symfony\Bridge\PhpUnit\ExpectDeprecationTrait extends \PHPUnit\Framework\TestCase
- class \Drupal\Tests\field\Kernel\FieldKernelTestBase extends \Drupal\KernelTests\KernelTestBase
- class \Drupal\Tests\field\Kernel\ShapeItemTest extends \Drupal\Tests\field\Kernel\FieldKernelTestBase
- class \Drupal\Tests\field\Kernel\FieldKernelTestBase extends \Drupal\KernelTests\KernelTestBase
Expanded class hierarchy of ShapeItemTest
File
-
core/
modules/ field/ tests/ src/ Kernel/ ShapeItemTest.php, line 16
Namespace
Drupal\Tests\field\KernelView source
class ShapeItemTest extends FieldKernelTestBase {
/**
* Modules to enable.
*
* @var array
*/
protected static $modules = [
'field_test',
];
/**
* The name of the field to use in this test.
*
* @var string
*/
protected $fieldName = 'field_shape';
/**
* {@inheritdoc}
*/
protected function setUp() : void {
parent::setUp();
// Create a 'shape' field and storage for validation.
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();
}
/**
* Tests using entity fields of the shape field type.
*/
public function testShapeItem() {
// Verify entity creation.
$entity = EntityTest::create();
$shape = 'cube';
$color = 'blue';
$entity->{$this->fieldName}->shape = $shape;
$entity->{$this->fieldName}->color = $color;
$entity->name->value = $this->randomMachineName();
$entity->save();
// Verify entity has been created properly.
$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);
// Verify changing the field value.
$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);
// Read changed entity and assert changed values.
$entity->save();
$entity = EntityTest::load($id);
$this->assertEquals($new_shape, $entity->{$this->fieldName}->shape);
$this->assertEquals($new_color, $entity->{$this->fieldName}->color);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.