Commit 9e4a19df authored by webchick's avatar webchick

Issue #1885542 by larowlan, tim.plunkett: Fixed DatabaseControllerNG does not...

Issue #1885542 by larowlan, tim.plunkett: Fixed DatabaseControllerNG does not rollback failed ::save() operations.
parent dd8e4f2d
......@@ -9,7 +9,6 @@
use PDO;
use Drupal\Core\Entity\Query\QueryInterface;
use Exception;
use Drupal\Component\Uuid\Uuid;
use Drupal\Component\Utility\NestedArray;
......@@ -492,7 +491,7 @@ 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);
......@@ -548,7 +547,7 @@ public function save(EntityInterface $entity) {
return $return;
}
catch (Exception $e) {
catch (\Exception $e) {
$transaction->rollback();
watchdog_exception($this->entityType, $e);
throw new EntityStorageException($e->getMessage(), $e->getCode(), $e);
......
......@@ -217,7 +217,7 @@ public function save(EntityInterface $entity) {
return $return;
}
catch (Exception $e) {
catch (\Exception $e) {
$transaction->rollback();
watchdog_exception($this->entityType, $e);
throw new EntityStorageException($e->getMessage(), $e->getCode(), $e);
......
......@@ -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 @@ public function testUserHooks() {
'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.');
}
}
}
......@@ -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.");
}
}
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