View source
<?php
namespace Drupal\KernelTests\Core\KeyValueStore;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\KernelTests\KernelTestBase;
use Drupal\entity_test\Entity\EntityTestLabel;
class KeyValueContentEntityStorageTest extends KernelTestBase {
protected static $modules = [
'user',
'entity_test',
'keyvalue_test',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('user');
}
public function testCRUD() {
$default_langcode = \Drupal::languageManager()
->getDefaultLanguage()
->getId();
$storage = \Drupal::entityTypeManager()
->getStorage('entity_test_label');
$this
->assertFalse($storage
->hasData());
$empty = EntityTestLabel::create();
$this
->assertNull($empty->id->value);
$this
->assertNull($empty->name->value);
$this
->assertNotEmpty($empty->uuid->value);
$this
->assertSame($default_langcode, $empty->langcode->value);
$this
->assertTrue($empty
->isNew());
$this
->assertSame('entity_test_label', $empty
->bundle());
$this
->assertNull($empty
->id());
$this
->assertNotEmpty($empty
->uuid());
$this
->assertNull($empty
->label());
$this
->assertSame('entity_test_label', $empty
->getEntityTypeId());
try {
$empty
->toUrl();
$this
->fail('EntityMalformedException was thrown.');
} catch (EntityMalformedException $e) {
}
try {
$empty
->save();
$this
->fail('EntityMalformedException was thrown.');
} catch (EntityMalformedException $e) {
}
$empty_id = EntityTestLabel::create([
'id' => '',
]);
$this
->assertTrue($empty_id
->isNew());
try {
$empty_id
->save();
$this
->fail('EntityMalformedException was thrown.');
} catch (EntityMalformedException $e) {
}
$entity_test = EntityTestLabel::create($expected = [
'id' => $this
->randomMachineName(),
'name' => $this
->randomString(),
]);
$this
->assertSame($expected['id'], $entity_test->id->value);
$this
->assertNotEmpty($entity_test->uuid->value);
$this
->assertNotEquals($empty->uuid->value, $entity_test->uuid->value);
$this
->assertSame($expected['name'], $entity_test->name->value);
$this
->assertSame($default_langcode, $entity_test->langcode->value);
$this
->assertTrue($entity_test
->isNew());
$this
->assertSame($expected['id'], $entity_test
->id());
$this
->assertNotEmpty($entity_test
->uuid());
$expected['uuid'] = $entity_test
->uuid();
$this
->assertSame($expected['name'], $entity_test
->label());
try {
$status = $entity_test
->save();
} catch (EntityMalformedException $e) {
$this
->fail('EntityMalformedException was not thrown.');
}
$this
->assertTrue($storage
->hasData());
$this
->assertSame(SAVED_NEW, $status);
$this
->assertSame($expected['id'], $entity_test
->id());
$this
->assertSame($expected['uuid'], $entity_test
->uuid());
$this
->assertSame($expected['name'], $entity_test
->label());
$this
->assertFalse($entity_test
->isNew());
$status = $entity_test
->save();
$this
->assertSame(SAVED_UPDATED, $status);
$this
->assertSame($expected['id'], $entity_test
->id());
$this
->assertSame($expected['uuid'], $entity_test
->uuid());
$this
->assertSame($expected['name'], $entity_test
->label());
$this
->assertFalse($entity_test
->isNew());
$same_id = EntityTestLabel::create([
'id' => $entity_test
->id(),
]);
$this
->assertTrue($same_id
->isNew());
try {
$same_id
->save();
$this
->fail('Not possible to overwrite an entity entity.');
} catch (EntityStorageException $e) {
}
$ids = [
$expected['id'],
'second_' . $this
->randomMachineName(4),
'third_' . $this
->randomMachineName(4),
];
for ($i = 1; $i < 3; $i++) {
$old_id = $ids[$i - 1];
$new_id = $ids[$i];
$this
->assertSame($old_id, $entity_test
->id());
$entity_test->id = $new_id;
$this
->assertSame($new_id, $entity_test
->id());
$status = $entity_test
->save();
$this
->assertSame(SAVED_UPDATED, $status);
$this
->assertFalse($entity_test
->isNew());
$this
->assertSame($new_id, $entity_test
->id());
}
}
public function testUninstall() {
$uninstall_validator_reasons = \Drupal::service('content_uninstall_validator')
->validate('keyvalue_test');
$this
->assertEmpty($uninstall_validator_reasons);
}
}