From 9e4a19df1c6eaa25afe496a370caefb321896af2 Mon Sep 17 00:00:00 2001 From: webchick Date: Fri, 11 Jan 2013 00:04:34 -0800 Subject: [PATCH] Issue #1885542 by larowlan, tim.plunkett: Fixed DatabaseControllerNG does not rollback failed ::save() operations. --- .../Core/Entity/DatabaseStorageController.php | 5 ++-- .../Entity/DatabaseStorageControllerNG.php | 2 +- .../Tests/Entity/EntityCrudHookTest.php | 28 ++++++++++++++++++- .../modules/entity_test/entity_test.module | 9 ++++++ 4 files changed, 39 insertions(+), 5 deletions(-) diff --git a/core/lib/Drupal/Core/Entity/DatabaseStorageController.php b/core/lib/Drupal/Core/Entity/DatabaseStorageController.php index f8138defd1d1..e6d8fe6fbea6 100644 --- a/core/lib/Drupal/Core/Entity/DatabaseStorageController.php +++ b/core/lib/Drupal/Core/Entity/DatabaseStorageController.php @@ -9,7 +9,6 @@ namespace Drupal\Core\Entity; use PDO; use Drupal\Core\Entity\Query\QueryInterface; -use Exception; use Drupal\Component\Uuid\Uuid; use Drupal\Component\Utility\NestedArray; @@ -492,7 +491,7 @@ class DatabaseStorageController implements EntityStorageControllerInterface { // 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); @@ -548,7 +547,7 @@ class DatabaseStorageController implements EntityStorageControllerInterface { return $return; } - catch (Exception $e) { + catch (\Exception $e) { $transaction->rollback(); watchdog_exception($this->entityType, $e); throw new EntityStorageException($e->getMessage(), $e->getCode(), $e); diff --git a/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php b/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php index 875b7a2d91e2..fd8248e1c7c9 100644 --- a/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php +++ b/core/lib/Drupal/Core/Entity/DatabaseStorageControllerNG.php @@ -217,7 +217,7 @@ class DatabaseStorageControllerNG extends DatabaseStorageController { return $return; } - catch (Exception $e) { + catch (\Exception $e) { $transaction->rollback(); watchdog_exception($this->entityType, $e); throw new EntityStorageException($e->getMessage(), $e->getCode(), $e); diff --git a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityCrudHookTest.php b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityCrudHookTest.php index 91c292cb8205..db722bb28b48 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Entity/EntityCrudHookTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Entity/EntityCrudHookTest.php @@ -8,6 +8,7 @@ namespace Drupal\system\Tests\Entity; use Drupal\simpletest\WebTestBase; +use Drupal\Core\Database\Database; /** * Tests invocation of hooks when performing an action. @@ -28,7 +29,7 @@ class EntityCrudHookTest extends WebTestBase { * * @var array */ - public static $modules = array('entity_crud_hook_test', 'taxonomy', 'comment', 'file'); + public static $modules = array('entity_crud_hook_test', 'taxonomy', 'comment', 'file', 'entity_test'); protected $ids = array(); @@ -409,4 +410,29 @@ class EntityCrudHookTest extends WebTestBase { 'entity_crud_hook_test_entity_delete called for type user', )); } + + /** + * Tests rollback from failed insert in EntityNG. + */ + function testEntityNGRollback() { + // Create a block. + try { + $entity = entity_create('entity_test', array('name' => 'fail_insert'))->save(); + $this->fail('Expected exception has not been thrown.'); + } + catch (\Exception $e) { + $this->pass('Expected exception has been thrown.'); + } + + if (Database::getConnection()->supportsTransactions()) { + // Check that the block does not exist in the database. + $ids = entity_query('entity_test')->condition('name', 'fail_insert')->execute(); + $this->assertTrue(empty($ids), 'Transactions supported, and entity not found in database.'); + } + else { + // Check that the block exists in the database. + $ids = entity_query('entity_test')->condition('name', 'fail_insert')->execute(); + $this->assertFalse(empty($ids), 'Transactions not supported, and entity found in database.'); + } + } } diff --git a/core/modules/system/tests/modules/entity_test/entity_test.module b/core/modules/system/tests/modules/entity_test/entity_test.module index f2b5b380e8c6..90636149b79e 100644 --- a/core/modules/system/tests/modules/entity_test/entity_test.module +++ b/core/modules/system/tests/modules/entity_test/entity_test.module @@ -169,3 +169,12 @@ function entity_test_form_node_form_alter(&$form, &$form_state, $form_id) { $langcode = $form_state['controller']->getFormLangcode($form_state); variable_set('entity_form_langcode', $langcode); } + +/** + * Implements hook_ENTITY_TYPE_insert(). + */ +function entity_test_entity_test_insert($entity) { + if ($entity->name->value == 'fail_insert') { + throw new Exception("Test exception rollback."); + } +}