Commit e971d388 authored by joachim's avatar joachim

Issue #2193421 by joachim: Add tests for hook invocation and Rules event...

Issue #2193421 by joachim: Add tests for hook invocation and Rules event triggering during flag/unflag.
parent fc9bcff7
......@@ -5,6 +5,7 @@ package = Flags
configure = admin/structure/flags
test_dependencies[] = token
test_dependencies[] = rules
; Files that contain classes.
; Flag classes
......
......@@ -1124,3 +1124,90 @@ class FlagEntityFieldQueryTestCase extends FlagTestCaseBase {
}
}
/**
* Verifies the invocation of hooks when performing flagging and unflagging.
*/
class FlagHookInvocationsTestCase extends FlagTestCaseBase {
/**
* Implements getInfo().
*/
public static function getInfo() {
return array(
'name' => 'Flag hook invocations',
'description' => 'Invocation of flag and entity hooks and rules during flagging and unflagging.',
'group' => 'Flag',
);
}
/**
* Implements setUp().
*/
function setUp() {
parent::setUp('flag', 'rules', 'flag_hook_test');
// Note the test module contains our test flag.
// Create test user who can flag and unflag.
$this->flag_unflag_user = $this->drupalCreateUser(array('flag flag_hook_test_flag', 'unflag flag_hook_test_flag'));
$this->drupalLogin($this->flag_unflag_user);
}
function testHookInvocation() {
// @todo: Expand this test to check the values returned by API functions and
// flag counts within each hook implementation.
// Create an article node that we try to create a flagging entity for.
$title = $this->randomName(8);
$node = array(
'title' => $title,
'body' => array(LANGUAGE_NONE => array(array('value' => $this->randomName(32)))),
'uid' => 1,
'type' => 'article',
'is_new' => TRUE,
);
$node = node_submit((object) $node);
node_save($node);
// Initialize a tracking variable. The test module will update this when
// its hooks are invoked.
variable_set('flag_hook_test_hook_tracking', array());
// Flag the node as the user.
$flag = flag_get_flag('flag_hook_test_flag');
$flag->flag('flag', $node->nid, $this->flag_unflag_user);
// Get the variable the test module sets the hook order into.
$hook_order_variable = variable_get('flag_hook_test_hook_tracking', array());
//debug($hook_order_variable);
$expected_hook_order = array(
'hook_entity_presave',
'hook_entity_insert',
'hook_flag_flag',
'rules_event',
);
$this->assertEqual($expected_hook_order, $hook_order_variable, "The hooks are invoked in the correct order when flagging a node.");
// Clear the tracking variable.
variable_set('flag_hook_test_hook_tracking', array());
// Unflag the node as the user.
$flag->flag('unflag', $node->nid, $this->flag_unflag_user);
// Get the variable the test module sets the hook order into.
$hook_order_variable = variable_get('flag_hook_test_hook_tracking', array());
//debug($hook_order_variable);
$expected_hook_order = array(
'hook_flag_unflag',
'rules_event',
'hook_entity_delete',
);
$this->assertEqual($expected_hook_order, $hook_order_variable, "The hooks are invoked in the correct order when unflagging a node.");
}
}
name = Flag Hooks Test
description = Test module for invocation of flag hooks.
dependencies[] = flag
dependencies[] = rules
core = 7.x
hidden = TRUE
<?php
/**
* @file flag_hook_test.module
* Test module for the hooks that Flag invokes.
*/
/**
*
*/
function _flag_hook_test_record_invocation($hook_name) {
$variable = variable_get('flag_hook_test_hook_tracking', array());
$variable[] = $hook_name;
variable_set('flag_hook_test_hook_tracking', $variable);
}
/**
* Implements hook_flag_flag().
*/
function flag_hook_test_flag_flag($flag, $entity_id, $account, $flagging) {
_flag_hook_test_record_invocation('hook_flag_flag');
}
/**
* Implements hook_flag_unflag().
*/
function flag_hook_test_flag_unflag($flag, $entity_id, $account, $flagging) {
_flag_hook_test_record_invocation('hook_flag_unflag');
}
/**
* Implements hook_entity_presave().
*/
function flag_hook_test_entity_presave($entity, $type) {
if ($type == 'flagging') {
_flag_hook_test_record_invocation('hook_entity_presave');
}
}
/**
* Implements hook_entity_insert().
*/
function flag_hook_test_entity_insert($entity, $type) {
if ($type == 'flagging') {
_flag_hook_test_record_invocation('hook_entity_insert');
}
}
/**
* Implements hook_entity_update().
*/
function flag_hook_test_entity_update($entity, $type) {
if ($type == 'flagging') {
_flag_hook_test_record_invocation('hook_entity_update');
}
}
/**
* Implements hook_entity_delete().
*/
function flag_hook_test_entity_delete($entity, $type) {
if ($type == 'flagging') {
_flag_hook_test_record_invocation('hook_entity_delete');
}
}
// ========================================================= Configuration
/**
* Implements hook_flag_default_flags().
*/
function flag_hook_test_flag_default_flags() {
$flags = array();
$flags['flag_hook_test_flag'] = array (
'entity_type' => 'node',
'title' => 'Test Flag',
'global' => FALSE,
'types' => array (
0 => 'article',
),
'flag_short' => 'Flag this',
'flag_long' => 'Flag this post',
'flag_message' => 'This post has been flagged',
'unflag_short' => 'Unflag this',
'unflag_long' => 'Remove this post from your flagged items',
'unflag_message' => 'This post has been unflagged',
'unflag_denied_text' => 'You may not unflag this item',
'link_type' => 'normal',
'weight' => 0,
'show_in_links' => array (
'full' => TRUE,
'teaser' => TRUE,
),
'show_as_field' => FALSE,
'show_on_form' => FALSE,
'access_author' => '',
'show_contextual_link' => TRUE,
'show_on_profile' => FALSE,
'access_uid' => '',
'api_version' => 3,
);
return $flags;
}
/**
* Implements hook_rules_action_info().
*/
function flag_hook_test_rules_action_info() {
return array(
'flag_test_action' => array(
'label' => t('Flag test action'),
'group' => t('Flag test'),
),
);
}
/**
* Test action for flagging.
*/
function flag_test_action() {
_flag_hook_test_record_invocation('rules_event');
}
/**
* Implements hook_default_rules_configuration().
*/
function flag_hook_test_default_rules_configuration() {
$configs['flag_test_rule_flag'] = rules_import('{ "flag_test_rule" : {
"LABEL" : "Flag test rule",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "flag_hook_test", "flag" ],
"ON" : { "flag_flagged_flag_hook_test_flag" : [] },
"DO" : [ { "flag_test_action" : [] } ]
}
}');
$configs['flag_test_rule_unflag'] = rules_import('{ "flag_test_rule" : {
"LABEL" : "Flag test rule",
"PLUGIN" : "reaction rule",
"OWNER" : "rules",
"REQUIRES" : [ "flag_hook_test", "flag" ],
"ON" : { "flag_unflagged_flag_hook_test_flag" : [] },
"DO" : [ { "flag_test_action" : [] } ]
}
}');
return $configs;
}
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