Same name and namespace in other branches
- 8.x-2.x tests/src/Kernel/ColorFieldTypeTest.php \Drupal\Tests\color_field\Kernel\ColorFieldTypeTest::testTestItem() 1 comment
Tests using entity fields of the telephone field type.
File
-
tests/
src/ Kernel/ ColorFieldTypeTest.php, line 31
Class
- ColorFieldTypeTest
- Tests the new entity API for the color field type.
Namespace
Drupal\Tests\color_field\KernelCode
public function testTestItem() : void {
// Verify entity creation.
$entity = EntityTest::create();
$color = '#5BCEFA';
$lcolor = '5bcefa';
$opacity = 0.5;
$entity->field_test->color = $color;
$entity->field_test->opacity = $opacity;
$entity->field_hex->opacity = 5;
$entity->field_hex->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->field_test, 'Field implements interface.');
$this->assertInstanceOf(FieldItemInterface::class, $entity->field_test[0], 'Field item implements interface.');
$this->assertEquals($color, $entity->field_test->color);
$this->assertEquals($opacity, $entity->field_test->opacity);
$this->assertEquals($color, $entity->field_test[0]->color);
// Verify field setting is respected.
$this->assertEquals($lcolor, $entity->field_hex->color);
$this->assertEquals('', $entity->field_hex->opacity);
// Verify changing the field value.
$new_value = '#FFFFFF';
$entity->field_test->color = $new_value;
$this->assertEquals($new_value, $entity->field_test->color);
// Read changed entity and assert changed values.
$entity->save();
$entity = EntityTest::load($id);
$this->assertEquals($new_value, $entity->field_test->color);
// Verify setting the color with one format will save as the desired format.
$new_value = 'F5A9B8';
$entity->field_test->color = $new_value;
$this->assertEquals($new_value, $entity->field_test->color);
// Test sample item generation.
$entity = EntityTest::create();
$entity->field_test
->generateSampleItems();
$this->entityValidateAndSave($entity);
}