Tests CRUD operations.
function testCRUD() {
// Verify default properties on a newly created empty entity.
$empty = entity_create('config_test', array());
$this
->assertIdentical($empty->id, NULL);
$this
->assertTrue($empty->uuid);
$this
->assertIdentical($empty->label, NULL);
$this
->assertIdentical($empty->style, NULL);
$this
->assertIdentical($empty->langcode, LANGUAGE_NOT_SPECIFIED);
// Verify ConfigEntity properties/methods on the newly created empty entity.
$this
->assertIdentical($empty
->isNew(), TRUE);
$this
->assertIdentical($empty
->getOriginalID(), NULL);
$this
->assertIdentical($empty
->bundle(), 'config_test');
$this
->assertIdentical($empty
->id(), NULL);
$this
->assertTrue($empty
->uuid());
$this
->assertIdentical($empty
->label(), NULL);
$this
->assertIdentical($empty
->get('id'), NULL);
$this
->assertTrue($empty
->get('uuid'));
$this
->assertIdentical($empty
->get('label'), NULL);
$this
->assertIdentical($empty
->get('style'), NULL);
$this
->assertIdentical($empty
->get('langcode'), LANGUAGE_NOT_SPECIFIED);
// Verify Entity properties/methods on the newly created empty entity.
$this
->assertIdentical($empty
->isNewRevision(), FALSE);
$this
->assertIdentical($empty
->entityType(), 'config_test');
$uri = $empty
->uri();
$this
->assertIdentical($uri['path'], 'admin/structure/config_test/manage/');
$this
->assertIdentical($empty
->isDefaultRevision(), TRUE);
// Verify that an empty entity cannot be saved.
try {
$empty
->save();
$this
->fail('EntityMalformedException was thrown.');
} catch (EntityMalformedException $e) {
$this
->pass('EntityMalformedException was thrown.');
}
// Verify that an entity with an empty ID string is considered empty, too.
$empty_id = entity_create('config_test', 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.');
}
// Verify properties on a newly created entity.
$config_test = entity_create('config_test', $expected = array(
'id' => $this
->randomName(),
'label' => $this
->randomString(),
'style' => $this
->randomName(),
));
$this
->assertIdentical($config_test->id, $expected['id']);
$this
->assertTrue($config_test->uuid);
$this
->assertNotEqual($config_test->uuid, $empty->uuid);
$this
->assertIdentical($config_test->label, $expected['label']);
$this
->assertIdentical($config_test->style, $expected['style']);
$this
->assertIdentical($config_test->langcode, LANGUAGE_NOT_SPECIFIED);
// Verify methods on the newly created entity.
$this
->assertIdentical($config_test
->isNew(), TRUE);
$this
->assertIdentical($config_test
->getOriginalID(), $expected['id']);
$this
->assertIdentical($config_test
->id(), $expected['id']);
$this
->assertTrue($config_test
->uuid());
$expected['uuid'] = $config_test
->uuid();
$this
->assertIdentical($config_test
->label(), $expected['label']);
$this
->assertIdentical($config_test
->isNewRevision(), FALSE);
$uri = $config_test
->uri();
$this
->assertIdentical($uri['path'], 'admin/structure/config_test/manage/' . $expected['id']);
$this
->assertIdentical($config_test
->isDefaultRevision(), TRUE);
// Verify that the entity can be saved.
try {
$status = $config_test
->save();
$this
->pass('EntityMalformedException was not thrown.');
} catch (EntityMalformedException $e) {
$this
->fail('EntityMalformedException was not thrown.');
}
// Verify that the correct status is returned and properties did not change.
$this
->assertIdentical($status, SAVED_NEW);
$this
->assertIdentical($config_test
->id(), $expected['id']);
$this
->assertIdentical($config_test
->uuid(), $expected['uuid']);
$this
->assertIdentical($config_test
->label(), $expected['label']);
$this
->assertIdentical($config_test
->isNew(), FALSE);
$this
->assertIdentical($config_test
->getOriginalID(), $expected['id']);
// Save again, and verify correct status and properties again.
$status = $config_test
->save();
$this
->assertIdentical($status, SAVED_UPDATED);
$this
->assertIdentical($config_test
->id(), $expected['id']);
$this
->assertIdentical($config_test
->uuid(), $expected['uuid']);
$this
->assertIdentical($config_test
->label(), $expected['label']);
$this
->assertIdentical($config_test
->isNew(), FALSE);
$this
->assertIdentical($config_test
->getOriginalID(), $expected['id']);
// Re-create the entity with the same ID and verify updated status.
$same_id = entity_create('config_test', array(
'id' => $config_test
->id(),
));
$this
->assertIdentical($same_id
->isNew(), TRUE);
$status = $same_id
->save();
$this
->assertIdentical($status, SAVED_UPDATED);
// Verify that the entity was overwritten.
$same_id = entity_load('config_test', $config_test
->id());
$this
->assertIdentical($same_id
->id(), $config_test
->id());
// Note: Reloading loads from FileStorage, and FileStorage enforces strings.
$this
->assertIdentical($same_id
->label(), '');
$this
->assertNotEqual($same_id
->uuid(), $config_test
->uuid());
// Revert to previous state.
$config_test
->save();
// Verify that renaming the ID returns correct status and properties.
$ids = array(
$expected['id'],
'second_' . $this
->randomName(4),
'third_' . $this
->randomName(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($config_test
->id(), $old_id);
$this
->assertIdentical($config_test
->getOriginalID(), $old_id);
// Rename.
$config_test->id = $new_id;
$this
->assertIdentical($config_test
->id(), $new_id);
$status = $config_test
->save();
$this
->assertIdentical($status, SAVED_UPDATED);
$this
->assertIdentical($config_test
->isNew(), FALSE);
// Verify that originalID points to new ID directly after renaming.
$this
->assertIdentical($config_test
->id(), $new_id);
$this
->assertIdentical($config_test
->getOriginalID(), $new_id);
}
}