Project

General

Profile

Paste
Download (5.35 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / flag / tests / flag_hook_test / flag_hook_test.module @ 4cfd8be6

1
<?php
2

    
3
/**
4
 * @file flag_hook_test.module
5
 * Test module for the hooks that Flag invokes.
6
 */
7

    
8
/**
9
 * Store the hook name and parameters into a variable for retrieval by the test.
10
 *
11
 * Hook implementations should call this with their hook name and parameters.
12
 *
13
 * @param $hook_name
14
 *  The name of the hook invoked.
15
 * @param $function_parameters
16
 *  The array of parameters the hook received.
17
 * @param $flagging
18
 *  (optional) The flagging entity that the hook received. If this is given,
19
 *  then various flag API functions have their data set into the tracking
20
 *  variable for verification by the test case.
21
 */
22
function _flag_hook_test_record_invocation($hook_name, $function_parameters, $flagging = NULL) {
23
  $variable = variable_get('flag_hook_test_hook_tracking', array());
24

    
25
  $variable[$hook_name] = array();
26
  $variable[$hook_name]['parameters'] = $function_parameters;
27

    
28
  // If a Flagging entity was passed in, call API functions and store their data
29
  // for the test case to check.
30
  if (isset($flagging)) {
31
    $flag = flag_get_flag(NULL, $flagging->fid);
32

    
33
    $variable[$hook_name]['api_calls'] = array();
34

    
35
    $variable[$hook_name]['api_calls']['flag_get_entity_flags'] = flag_get_entity_flags('node', $flagging->entity_id, $flag->name);
36

    
37
    $variable[$hook_name]['api_calls']['flag_get_user_flags'] = flag_get_user_flags('node', $flagging->entity_id, $flagging->uid);
38

    
39
    $variable[$hook_name]['api_calls']['flag_get_counts'] = flag_get_counts('node', $flagging->entity_id);
40

    
41
    $variable[$hook_name]['api_calls']['flag_get_flag_counts'] = flag_get_flag_counts($flag->name);
42

    
43
    $variable[$hook_name]['api_calls']['flag_get_entity_flag_counts'] = flag_get_entity_flag_counts($flag, 'node');
44

    
45
    $account = user_load($flagging->uid);
46
    $variable[$hook_name]['api_calls']['flag_get_user_flag_counts'] = flag_get_user_flag_counts($flag, $account);
47
  }
48

    
49
  variable_set('flag_hook_test_hook_tracking', $variable);
50
}
51

    
52
/**
53
 * Implements hook_flag_flag().
54
 */
55
function flag_hook_test_flag_flag($flag, $entity_id, $account, $flagging) {
56
  _flag_hook_test_record_invocation('hook_flag_flag', func_get_args(), $flagging);
57
}
58

    
59
/**
60
 * Implements hook_flag_unflag().
61
 */
62
function flag_hook_test_flag_unflag($flag, $entity_id, $account, $flagging) {
63
  _flag_hook_test_record_invocation('hook_flag_unflag', func_get_args(), $flagging);
64
}
65

    
66
/**
67
 * Implements hook_entity_presave().
68
 */
69
function flag_hook_test_entity_presave($entity, $type) {
70
  if ($type == 'flagging') {
71
    _flag_hook_test_record_invocation('hook_entity_presave', func_get_args(), $entity);
72
  }
73
}
74

    
75
/**
76
 * Implements hook_entity_insert().
77
 */
78
function flag_hook_test_entity_insert($entity, $type) {
79
  if ($type == 'flagging') {
80
    _flag_hook_test_record_invocation('hook_entity_insert', func_get_args(), $entity);
81
  }
82
}
83

    
84
/**
85
 * Implements hook_entity_update().
86
 */
87
function flag_hook_test_entity_update($entity, $type) {
88
  if ($type == 'flagging') {
89
    _flag_hook_test_record_invocation('hook_entity_update', func_get_args(), $entity);
90
  }
91
}
92

    
93
/**
94
 * Implements hook_entity_delete().
95
 */
96
function flag_hook_test_entity_delete($entity, $type) {
97
  if ($type == 'flagging') {
98
    _flag_hook_test_record_invocation('hook_entity_delete', func_get_args(), $entity);
99
  }
100
}
101

    
102
// ========================================================= Configuration
103

    
104
/**
105
 * Implements hook_flag_default_flags().
106
 */
107
function flag_hook_test_flag_default_flags() {
108
  $flags = array();
109
  $flags['flag_hook_test_flag'] = array(
110
    'entity_type' => 'node',
111
    'title' => 'Test Flag',
112
    'global' => FALSE,
113
    'types' => array(
114
      0 => 'article',
115
    ),
116
    'flag_short' => 'Flag this',
117
    'flag_long' => 'Flag this post',
118
    'flag_message' => 'This post has been flagged',
119
    'unflag_short' => 'Unflag this',
120
    'unflag_long' => 'Remove this post from your flagged items',
121
    'unflag_message' => 'This post has been unflagged',
122
    'unflag_denied_text' => 'You may not unflag this item',
123
    'link_type' => 'normal',
124
    'weight' => 0,
125
    'show_in_links' => array(
126
      'full' => TRUE,
127
      'teaser' => TRUE,
128
    ),
129
    'show_as_field' => FALSE,
130
    'show_on_form' => FALSE,
131
    'access_author' => '',
132
    'show_contextual_link' => TRUE,
133
    'show_on_profile' => FALSE,
134
    'access_uid' => '',
135
    'api_version' => 3,
136
  );
137
  return $flags;
138
}
139

    
140
/**
141
 * Implements hook_rules_action_info().
142
 */
143
function flag_hook_test_rules_action_info() {
144
  return array(
145
    'flag_test_action' => array(
146
      'label' => t('Flag test action'),
147
      'group' => t('Flag test'),
148
    ),
149
  );
150
}
151

    
152
/**
153
 * Test action for flagging.
154
 */
155
function flag_test_action() {
156
  _flag_hook_test_record_invocation('rules_event', func_get_args());
157
}
158

    
159
/**
160
 * Implements hook_default_rules_configuration().
161
 */
162
function flag_hook_test_default_rules_configuration() {
163
  $configs['flag_test_rule_flag'] = rules_import('{ "flag_test_rule" : {
164
      "LABEL" : "Flag test rule",
165
      "PLUGIN" : "reaction rule",
166
      "OWNER" : "rules",
167
      "REQUIRES" : [ "flag_hook_test", "flag" ],
168
      "ON" : { "flag_flagged_flag_hook_test_flag" : [] },
169
      "DO" : [ { "flag_test_action" : [] } ]
170
    }
171
  }');
172

    
173
  $configs['flag_test_rule_unflag'] = rules_import('{ "flag_test_rule" : {
174
      "LABEL" : "Flag test rule",
175
      "PLUGIN" : "reaction rule",
176
      "OWNER" : "rules",
177
      "REQUIRES" : [ "flag_hook_test", "flag" ],
178
      "ON" : { "flag_unflagged_flag_hook_test_flag" : [] },
179
      "DO" : [ { "flag_test_action" : [] } ]
180
    }
181
  }');
182

    
183
  return $configs;
184
}