View source
<?php
namespace Drupal\system\Tests\KeyValueStore;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\simpletest\KernelTestBase;
class KeyValueContentEntityStorageTest extends KernelTestBase {
public static $modules = array(
'user',
'entity_test',
'keyvalue_test',
);
protected function setUp() {
parent::setUp();
$this
->installEntitySchema('user');
}
function testCRUD() {
$default_langcode = \Drupal::languageManager()
->getDefaultLanguage()
->getId();
$empty = entity_create('entity_test_label');
$this
->assertIdentical($empty->id->value, NULL);
$this
->assertIdentical($empty->name->value, NULL);
$this
->assertTrue($empty->uuid->value);
$this
->assertIdentical($empty->langcode->value, $default_langcode);
$this
->assertIdentical($empty
->isNew(), TRUE);
$this
->assertIdentical($empty
->bundle(), 'entity_test_label');
$this
->assertIdentical($empty
->id(), NULL);
$this
->assertTrue($empty
->uuid());
$this
->assertIdentical($empty
->label(), NULL);
$this
->assertIdentical($empty
->getEntityTypeId(), 'entity_test_label');
try {
$empty
->urlInfo();
$this
->fail('EntityMalformedException was thrown.');
} catch (EntityMalformedException $e) {
$this
->pass('EntityMalformedException was thrown.');
}
try {
$empty
->save();
$this
->fail('EntityMalformedException was thrown.');
} catch (EntityMalformedException $e) {
$this
->pass('EntityMalformedException was thrown.');
}
$empty_id = entity_create('entity_test_label', array(
'id' => '',
));
$this
->assertIdentical($empty_id
->isNew(), TRUE);
try {
$empty_id
->save();
$this
->fail('EntityMalformedException was thrown.');
} catch (EntityMalformedException $e) {
$this
->pass('EntityMalformedException was thrown.');
}
$entity_test = entity_create('entity_test_label', $expected = array(
'id' => $this
->randomMachineName(),
'name' => $this
->randomString(),
));
$this
->assertIdentical($entity_test->id->value, $expected['id']);
$this
->assertTrue($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);
$this
->assertIdentical($entity_test
->isNew(), TRUE);
$this
->assertIdentical($entity_test
->id(), $expected['id']);
$this
->assertTrue($entity_test
->uuid());
$expected['uuid'] = $entity_test
->uuid();
$this
->assertIdentical($entity_test
->label(), $expected['name']);
try {
$status = $entity_test
->save();
$this
->pass('EntityMalformedException was not thrown.');
} catch (EntityMalformedException $e) {
$this
->fail('EntityMalformedException was not thrown.');
}
$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);
$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);
$same_id = entity_create('entity_test_label', array(
'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) {
$this
->pass('Not possible to overwrite an entity entity.');
}
$ids = array(
$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
->assertIdentical($entity_test
->id(), $old_id);
$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);
$this
->assertIdentical($entity_test
->id(), $new_id);
}
}
}