public function EntityApiTest::testEntityStorageExceptionHandling

Tests that exceptions are properly thrown when saving or deleting an entity.

File

drupal/core/modules/system/lib/Drupal/system/Tests/Entity/EntityApiTest.php, line 99
Definition of Drupal\system\Tests\Entity\EntityApiTest.

Class

EntityApiTest
Tests the basic Entity API.

Namespace

Drupal\system\Tests\Entity

Code

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.');
  }
}