annotate core/modules/field/tests/src/Kernel/ShapeItemTest.php @ 19:fa3358dc1485 tip

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