You are here

public function TriggerExampleTestCase::testTriggersomethingEvent in Examples for Developers 7

Test assigning a configurable action to the triggersomething event.

File

trigger_example/trigger_example.test, line 36
test file for trigger_example module.

Class

TriggerExampleTestCase
Default test case for the trigger_example module.

Code

public function testTriggersomethingEvent() {

  // Create an administrative user.
  $test_user = $this
    ->drupalCreateUser(array(
    'administer actions',
  ));
  $this
    ->drupalLogin($test_user);

  // Create a configurable action for display a message to the user.
  $hash = drupal_hash_base64('system_message_action');
  $action_label = $this
    ->randomName();
  $edit = array(
    'actions_label' => $action_label,
    'message' => $action_label,
  );
  $this
    ->drupalPost('admin/config/system/actions/configure/' . $hash, $edit, t('Save'));
  $aid = db_query('SELECT aid FROM {actions} WHERE callback = :callback', array(
    ':callback' => 'system_message_action',
  ))
    ->fetchField();

  // $aid is likely 3 but if we add more uses for the sequences table in
  // core it might break, so it is easier to get the value from the database.
  $edit = array(
    'aid' => drupal_hash_base64($aid),
  );

  // Note that this only works because there's just one item on the page.
  $this
    ->drupalPost('admin/structure/trigger/trigger_example', $edit, t('Assign'));

  // Request triggersomething form and submit.
  $this
    ->drupalPost('examples/trigger_example', array(), t('Run triggersomething event'));

  // Verify the message is shown to the user.
  $this
    ->assertText($action_label, 'The triggersomething event executed the action.');
}