public function MachineNameItemTest::testTestItem in Machine name 8
Tests using entity fields of the machine_name field type.
File
- tests/
src/ Kernel/ MachineNameItemTest.php, line 48
Class
- MachineNameItemTest
- Tests the new entity API for the machine_name field type.
Namespace
Drupal\Tests\machine_name\KernelCode
public function testTestItem() {
// Verify entity creation.
$entity = EntityTest::create();
$value = 'machine_name_test';
$entity->field_test = $value;
$entity->name->value = $this
->randomMachineName();
$entity
->save();
// Verify entity has been created properly.
$id = $entity
->id();
$entity = EntityTest::load($id);
$this
->assertTrue($entity->field_test instanceof FieldItemListInterface, 'Field implements interface.');
$this
->assertTrue($entity->field_test[0] instanceof FieldItemInterface, 'Field item implements interface.');
$this
->assertEqual($entity->field_test->value, $value);
$this
->assertEqual($entity->field_test[0]->value, $value);
// Verify changing the field value.
$new_value = 'changed_machine_name';
$entity->field_test->value = $new_value;
$this
->assertEqual($entity->field_test->value, $new_value);
// Read changed entity and assert changed values.
$entity
->save();
$entity = EntityTest::load($id);
$this
->assertEqual($entity->field_test->value, $new_value);
// Test sample item generation.
$entity = EntityTest::create();
$entity->field_test
->generateSampleItems();
$this
->entityValidateAndSave($entity);
}