MachineNameItemTest.php in Machine name 8
File
tests/src/Kernel/MachineNameItemTest.php
View source
<?php
namespace Drupal\Tests\machine_name\Kernel;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\field\Kernel\FieldKernelTestBase;
use Drupal\field\Entity\FieldStorageConfig;
class MachineNameItemTest extends FieldKernelTestBase {
public static $modules = [
'machine_name',
];
protected function setUp() {
parent::setUp();
FieldStorageConfig::create(array(
'field_name' => 'field_test',
'entity_type' => 'entity_test',
'type' => 'machine_name',
))
->save();
FieldConfig::create([
'entity_type' => 'entity_test',
'field_name' => 'field_test',
'bundle' => 'entity_test',
])
->save();
}
public function testTestItem() {
$entity = EntityTest::create();
$value = 'machine_name_test';
$entity->field_test = $value;
$entity->name->value = $this
->randomMachineName();
$entity
->save();
$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);
$new_value = 'changed_machine_name';
$entity->field_test->value = $new_value;
$this
->assertEqual($entity->field_test->value, $new_value);
$entity
->save();
$entity = EntityTest::load($id);
$this
->assertEqual($entity->field_test->value, $new_value);
$entity = EntityTest::create();
$entity->field_test
->generateSampleItems();
$this
->entityValidateAndSave($entity);
}
}