EckEntityTest.php in Entity Construction Kit (ECK) 8
File
tests/src/Kernel/EckEntityTest.php
View source
<?php
namespace Drupal\Tests\eck\Kernel;
use Drupal\eck\Entity\EckEntity;
use Drupal\eck\Entity\EckEntityBundle;
use Drupal\eck\Entity\EckEntityType;
use Drupal\KernelTests\KernelTestBase;
class EckEntityTest extends KernelTestBase {
protected static $modules = [
'system',
'eck',
'field',
];
protected function setUp() {
parent::setUp();
$this
->installConfig('eck');
}
public function testEckMethods() {
$eck_type_1 = $this
->createEckEntityType('no_fields');
$entity_1 = $this
->createEckEntity($eck_type_1);
$this
->assertNull($entity_1
->getChangedTime());
$this
->assertNull($entity_1
->getCreatedTime());
$eck_type_2 = $this
->createEckEntityType('with_fields', [
'created',
'changed',
]);
$entity_2 = $this
->createEckEntity($eck_type_2);
$this
->assertNotNull($entity_2
->getChangedTime());
$this
->assertNotNull($entity_2
->getCreatedTime());
}
protected function createEckEntity(EckEntityType $entity_type, $values = []) {
$values = [
'entity_type' => $entity_type
->id(),
'type' => $entity_type
->id(),
] + $values;
$entity = EckEntity::create($values);
return $entity;
}
protected function createEckEntityType($id, $base_fields = []) {
$entity_type = EckEntityType::create([
'id' => $id,
'label' => $this
->randomString(),
]);
$entity_type
->save();
EckEntityBundle::create([
'id' => $id,
'type' => $this
->randomString(),
]);
$allowed_base_fields = [
'created',
'changed',
'uid',
'title',
'status',
];
$enabled_fields = array_intersect($base_fields, $allowed_base_fields);
$config = \Drupal::configFactory()
->getEditable('eck.eck_entity_type.' . $entity_type
->id());
foreach ($enabled_fields as $field_name) {
$config
->set($field_name, TRUE);
}
$config
->save();
\Drupal::entityTypeManager()
->clearCachedDefinitions();
return $entity_type;
}
}