You are here

public function EntityAPITestCase::testExportableHooks in Entity API 7

Make sure insert() and update() hooks for exportables are invoked.

File

./entity.test, line 329
Entity CRUD API tests.

Class

EntityAPITestCase
Test basic API.

Code

public function testExportableHooks() {
  $_SESSION['entity_hook_test'] = array();

  // Enabling the module should invoke the enabled hook for the other
  // entities provided in code.
  module_enable(array(
    'entity_feature',
  ));
  $insert = array(
    'main',
    'test',
    'test2',
  );
  $this
    ->assertTrue($_SESSION['entity_hook_test']['entity_insert'] == $insert, 'Hook entity_insert has been invoked.');
  $this
    ->assertTrue($_SESSION['entity_hook_test']['entity_test_type_insert'] == $insert, 'Hook entity_test_type_insert has been invoked.');

  // Load a default entity and make sure the rebuilt logic only ran once.
  entity_load_single('entity_test_type', 'test');
  $this
    ->assertTrue(!isset($_SESSION['entity_hook_test']['entity_test_type_update']), '"Entity-test-type" defaults have been rebuilt only once.');

  // Add a new test entity in DB and make sure the hook is invoked too.
  $test3 = entity_create('entity_test_type', array(
    'name' => 'test3',
    'label' => 'label',
    'weight' => 0,
  ));
  $test3
    ->save();
  $insert[] = 'test3';
  $this
    ->assertTrue($_SESSION['entity_hook_test']['entity_insert'] == $insert, 'Hook entity_insert has been invoked.');
  $this
    ->assertTrue($_SESSION['entity_hook_test']['entity_test_type_insert'] == $insert, 'Hook entity_test_type_insert has been invoked.');

  // Now override the 'test' entity and make sure it invokes the update hook.
  $result = entity_load_multiple_by_name('entity_test_type', array(
    'test',
  ));
  $result['test']->label = 'modified';
  $result['test']
    ->save();
  $this
    ->assertTrue($_SESSION['entity_hook_test']['entity_update'] == array(
    'test',
  ), 'Hook entity_update has been invoked.');
  $this
    ->assertTrue($_SESSION['entity_hook_test']['entity_test_type_update'] == array(
    'test',
  ), 'Hook entity_test_type_update has been invoked.');

  // 'test' has to remain enabled, as it has been overridden.
  $delete = array(
    'main',
    'test2',
  );
  module_disable(array(
    'entity_feature',
  ));
  $this
    ->assertTrue($_SESSION['entity_hook_test']['entity_delete'] == $delete, 'Hook entity_deleted has been invoked.');
  $this
    ->assertTrue($_SESSION['entity_hook_test']['entity_test_type_delete'] == $delete, 'Hook entity_test_type_deleted has been invoked.');

  // Now make sure 'test' is not overridden any more, but custom.
  $result = entity_load_multiple_by_name('entity_test_type', array(
    'test',
  ));
  $this
    ->assertTrue(!$result['test']
    ->hasStatus(ENTITY_OVERRIDDEN), 'Entity is not marked as overridden any more.');
  $this
    ->assertTrue(entity_has_status('entity_test_type', $result['test'], ENTITY_CUSTOM), 'Entity is marked as custom.');

  // Test deleting the remaining entities from DB.
  entity_delete_multiple('entity_test_type', array(
    'test',
    'test3',
  ));
  $delete[] = 'test';
  $delete[] = 'test3';
  $this
    ->assertTrue($_SESSION['entity_hook_test']['entity_delete'] == $delete, 'Hook entity_deleted has been invoked.');
  $this
    ->assertTrue($_SESSION['entity_hook_test']['entity_test_type_delete'] == $delete, 'Hook entity_test_type_deleted has been invoked.');
}