public function KeyValueContentEntityStorageTest::testCRUD in Drupal 8
Same name and namespace in other branches
- 9 core/tests/Drupal/KernelTests/Core/KeyValueStore/KeyValueContentEntityStorageTest.php \Drupal\KernelTests\Core\KeyValueStore\KeyValueContentEntityStorageTest::testCRUD()
- 10 core/tests/Drupal/KernelTests/Core/KeyValueStore/KeyValueContentEntityStorageTest.php \Drupal\KernelTests\Core\KeyValueStore\KeyValueContentEntityStorageTest::testCRUD()
Tests CRUD operations.
@covers \Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage::hasData
File
- core/
tests/ Drupal/ KernelTests/ Core/ KeyValueStore/ KeyValueContentEntityStorageTest.php, line 37
Class
- KeyValueContentEntityStorageTest
- Tests KeyValueEntityStorage for content entities.
Namespace
Drupal\KernelTests\Core\KeyValueStoreCode
public function testCRUD() {
$default_langcode = \Drupal::languageManager()
->getDefaultLanguage()
->getId();
$storage = \Drupal::entityTypeManager()
->getStorage('entity_test_label');
$this
->assertFalse($storage
->hasData());
// Verify default properties on a newly created empty entity.
$empty = EntityTestLabel::create();
$this
->assertIdentical($empty->id->value, NULL);
$this
->assertIdentical($empty->name->value, NULL);
$this
->assertNotEmpty($empty->uuid->value);
$this
->assertIdentical($empty->langcode->value, $default_langcode);
// Verify ConfigEntity properties/methods on the newly created empty entity.
$this
->assertIdentical($empty
->isNew(), TRUE);
$this
->assertIdentical($empty
->bundle(), 'entity_test_label');
$this
->assertIdentical($empty
->id(), NULL);
$this
->assertNotEmpty($empty
->uuid());
$this
->assertIdentical($empty
->label(), NULL);
// Verify Entity properties/methods on the newly created empty entity.
$this
->assertIdentical($empty
->getEntityTypeId(), 'entity_test_label');
// The URI can only be checked after saving.
try {
$empty
->toUrl();
$this
->fail('EntityMalformedException was thrown.');
} catch (EntityMalformedException $e) {
// Expected exception; just continue testing.
}
// Verify that an empty entity cannot be saved.
try {
$empty
->save();
$this
->fail('EntityMalformedException was thrown.');
} catch (EntityMalformedException $e) {
// Expected exception; just continue testing.
}
// Verify that an entity with an empty ID string is considered empty, too.
$empty_id = EntityTestLabel::create([
'id' => '',
]);
$this
->assertIdentical($empty_id
->isNew(), TRUE);
try {
$empty_id
->save();
$this
->fail('EntityMalformedException was thrown.');
} catch (EntityMalformedException $e) {
// Expected exception; just continue testing.
}
// Verify properties on a newly created entity.
$entity_test = EntityTestLabel::create($expected = [
'id' => $this
->randomMachineName(),
'name' => $this
->randomString(),
]);
$this
->assertIdentical($entity_test->id->value, $expected['id']);
$this
->assertNotEmpty($entity_test->uuid->value);
$this
->assertNotEqual($entity_test->uuid->value, $empty->uuid->value);
$this
->assertIdentical($entity_test->name->value, $expected['name']);
$this
->assertIdentical($entity_test->langcode->value, $default_langcode);
// Verify methods on the newly created entity.
$this
->assertIdentical($entity_test
->isNew(), TRUE);
$this
->assertIdentical($entity_test
->id(), $expected['id']);
$this
->assertNotEmpty($entity_test
->uuid());
$expected['uuid'] = $entity_test
->uuid();
$this
->assertIdentical($entity_test
->label(), $expected['name']);
// Verify that the entity can be saved.
try {
$status = $entity_test
->save();
} catch (EntityMalformedException $e) {
$this
->fail('EntityMalformedException was not thrown.');
}
// Verify that hasData() returns the expected result.
$this
->assertTrue($storage
->hasData());
// Verify that the correct status is returned and properties did not change.
$this
->assertIdentical($status, SAVED_NEW);
$this
->assertIdentical($entity_test
->id(), $expected['id']);
$this
->assertIdentical($entity_test
->uuid(), $expected['uuid']);
$this
->assertIdentical($entity_test
->label(), $expected['name']);
$this
->assertIdentical($entity_test
->isNew(), FALSE);
// Save again, and verify correct status and properties again.
$status = $entity_test
->save();
$this
->assertIdentical($status, SAVED_UPDATED);
$this
->assertIdentical($entity_test
->id(), $expected['id']);
$this
->assertIdentical($entity_test
->uuid(), $expected['uuid']);
$this
->assertIdentical($entity_test
->label(), $expected['name']);
$this
->assertIdentical($entity_test
->isNew(), FALSE);
// Ensure that creating an entity with the same id as an existing one is not
// possible.
$same_id = EntityTestLabel::create([
'id' => $entity_test
->id(),
]);
$this
->assertIdentical($same_id
->isNew(), TRUE);
try {
$same_id
->save();
$this
->fail('Not possible to overwrite an entity entity.');
} catch (EntityStorageException $e) {
// Expected exception; just continue testing.
}
// Verify that renaming the ID returns correct status and properties.
$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];
// Before renaming, everything should point to the current ID.
$this
->assertIdentical($entity_test
->id(), $old_id);
// Rename.
$entity_test->id = $new_id;
$this
->assertIdentical($entity_test
->id(), $new_id);
$status = $entity_test
->save();
$this
->assertIdentical($status, SAVED_UPDATED);
$this
->assertIdentical($entity_test
->isNew(), FALSE);
// Verify that originalID points to new ID directly after renaming.
$this
->assertIdentical($entity_test
->id(), $new_id);
}
}