You are here

public function EntityAPIRulesIntegrationTestCase::testEvents in Entity API 7

Test the events.

File

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

Class

EntityAPIRulesIntegrationTestCase
Test the generated Rules integration.

Code

public function testEvents() {
  $rule = rules_reaction_rule();
  $rule
    ->event('entity_test_presave');
  $rule
    ->event('entity_test_insert');
  $rule
    ->event('entity_test_update');
  $rule
    ->event('entity_test_delete');
  $rule
    ->action('drupal_message', array(
    'message' => 'hello!',
  ));
  $rule
    ->save();
  rules_clear_cache(TRUE);

  // Let the events occur.
  $user1 = $this
    ->drupalCreateUser();
  RulesLog::logger()
    ->clear();
  $entity = entity_create('entity_test', array(
    'name' => 'test',
    'uid' => $user1->uid,
  ));
  $entity
    ->save();
  $entity->name = 'update';
  $entity
    ->save();
  $entity
    ->delete();

  // Now there should have been 5 events, 2 times presave and once insert,
  // update and delete.
  $count = substr_count(RulesLog::logger()
    ->render(), '0 ms Reacting on event');
  $this
    ->assertTrue($count == 5, 'Events have been properly invoked.');
  RulesLog::logger()
    ->checkLog();
}