Commit ce7ff5cd authored by alexpott's avatar alexpott

Issue #1634442 by chia, Berdir, amateescu, fago: Fixed...

Issue #1634442 by chia, Berdir, amateescu, fago: Fixed DatabaseStorageController can't catch exceptions.
parent d556b325
......@@ -532,7 +532,7 @@ public function delete(array $entities) {
catch (\Exception $e) {
$transaction->rollback();
watchdog_exception($this->entityType, $e);
throw new EntityStorageException($e->getMessage, $e->getCode, $e);
throw new EntityStorageException($e->getMessage(), $e->getCode(), $e);
}
}
......
......@@ -633,10 +633,10 @@ public function delete(array $entities) {
// Ignore slave server temporarily.
db_ignore_slave();
}
catch (Exception $e) {
catch (\Exception $e) {
$transaction->rollback();
watchdog_exception($this->entityType, $e);
throw new EntityStorageException($e->getMessage, $e->getCode, $e);
throw new EntityStorageException($e->getMessage(), $e->getCode(), $e);
}
}
}
......@@ -7,6 +7,7 @@
namespace Drupal\system\Tests\Entity;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\user\Plugin\Core\Entity\User;
/**
......@@ -90,4 +91,52 @@ protected function assertCRUD($entity_type, User $user1) {
$all = entity_load_multiple($entity_type);
$this->assertTrue(empty($all), format_string('%entity_type: Deleted all entities.', array('%entity_type' => $entity_type)));
}
/**
* Tests that exceptions are properly thrown when saving or deleting an
* entity.
*/
public function testEntityStorageExceptionHandling() {
$entity = entity_create('entity_test', array('name' => 'test'));
try {
$GLOBALS['entity_test_throw_exception'] = TRUE;
$entity->save();
$this->fail('Entity presave EntityStorageException thrown but not caught.');
}
catch (EntityStorageException $e) {
$this->assertEqual($e->getcode(), 1, 'Entity presave EntityStorageException caught.');
}
$entity = entity_create('entity_test', array('name' => 'test2'));
try {
unset($GLOBALS['entity_test_throw_exception']);
$entity->save();
$this->pass('Exception presave not thrown and not caught.');
}
catch (EntityStorageException $e) {
$this->assertNotEqual($e->getCode(), 1, 'Entity presave EntityStorageException caught.');
}
$entity = entity_create('entity_test', array('name' => 'test3'));
$entity->save();
try {
$GLOBALS['entity_test_throw_exception'] = TRUE;
$entity->delete();
$this->fail('Entity predelete EntityStorageException not thrown.');
}
catch (EntityStorageException $e) {
$this->assertEqual($e->getCode(), 2, 'Entity predelete EntityStorageException caught.');
}
unset($GLOBALS['entity_test_throw_exception']);
$entity = entity_create('entity_test', array('name' => 'test4'));
$entity->save();
try {
$entity->delete();
$this->pass('Entity predelete EntityStorageException not thrown and not caught.');
}
catch (EntityStorageException $e) {
$this->assertNotEqual($e->getCode(), 2, 'Entity predelete EntityStorageException thrown.');
}
}
}
......@@ -379,3 +379,21 @@ function entity_test_entity_form_display_alter(EntityFormDisplay $form_display,
}
}
}
/**
* Implements hook_entity_presave()
*/
function entity_test_entity_presave(EntityInterface $entity) {
if (isset($GLOBALS['entity_test_throw_exception'])) {
throw new Exception('Entity presave exception', 1);
}
}
/**
* Implements hook_entity_predelete()
*/
function entity_test_entity_predelete(EntityInterface $entity) {
if (isset($GLOBALS['entity_test_throw_exception'])) {
throw new Exception('Entity predelete exception', 2);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment