flag_hook_test.module 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * @file flag_hook_test.module
  4. * Test module for the hooks that Flag invokes.
  5. */
  6. /**
  7. * Store the hook name and parameters into a variable for retrieval by the test.
  8. *
  9. * Hook implementations should call this with their hook name and parameters.
  10. *
  11. * @param $hook_name
  12. * The name of the hook invoked.
  13. * @param $function_parameters
  14. * The array of parameters the hook received.
  15. * @param $flagging
  16. * (optional) The flagging entity that the hook received. If this is given,
  17. * then various flag API functions have their data set into the tracking
  18. * variable for verification by the test case.
  19. */
  20. function _flag_hook_test_record_invocation($hook_name, $function_parameters, $flagging = NULL) {
  21. $variable = variable_get('flag_hook_test_hook_tracking', array());
  22. $variable[$hook_name] = array();
  23. $variable[$hook_name]['parameters'] = $function_parameters;
  24. // If a Flagging entity was passed in, call API functions and store their data
  25. // for the test case to check.
  26. if (isset($flagging)) {
  27. $flag = flag_get_flag(NULL, $flagging->fid);
  28. $variable[$hook_name]['api_calls'] = array();
  29. $variable[$hook_name]['api_calls']['flag_get_entity_flags'] = flag_get_entity_flags('node', $flagging->entity_id, $flag->name);
  30. $variable[$hook_name]['api_calls']['flag_get_user_flags'] = flag_get_user_flags('node', $flagging->entity_id, $flagging->uid);
  31. $variable[$hook_name]['api_calls']['flag_get_counts'] = flag_get_counts('node', $flagging->entity_id);
  32. $variable[$hook_name]['api_calls']['flag_get_flag_counts'] = flag_get_flag_counts($flag->name);
  33. $variable[$hook_name]['api_calls']['flag_get_entity_flag_counts'] = flag_get_entity_flag_counts($flag, 'node');
  34. $account = user_load($flagging->uid);
  35. $variable[$hook_name]['api_calls']['flag_get_user_flag_counts'] = flag_get_user_flag_counts($flag, $account);
  36. }
  37. variable_set('flag_hook_test_hook_tracking', $variable);
  38. }
  39. /**
  40. * Implements hook_flag_flag().
  41. */
  42. function flag_hook_test_flag_flag($flag, $entity_id, $account, $flagging) {
  43. _flag_hook_test_record_invocation('hook_flag_flag', func_get_args(), $flagging);
  44. }
  45. /**
  46. * Implements hook_flag_unflag().
  47. */
  48. function flag_hook_test_flag_unflag($flag, $entity_id, $account, $flagging) {
  49. _flag_hook_test_record_invocation('hook_flag_unflag', func_get_args(), $flagging);
  50. }
  51. /**
  52. * Implements hook_entity_presave().
  53. */
  54. function flag_hook_test_entity_presave($entity, $type) {
  55. if ($type == 'flagging') {
  56. _flag_hook_test_record_invocation('hook_entity_presave', func_get_args(), $entity);
  57. }
  58. }
  59. /**
  60. * Implements hook_entity_insert().
  61. */
  62. function flag_hook_test_entity_insert($entity, $type) {
  63. if ($type == 'flagging') {
  64. _flag_hook_test_record_invocation('hook_entity_insert', func_get_args(), $entity);
  65. }
  66. }
  67. /**
  68. * Implements hook_entity_update().
  69. */
  70. function flag_hook_test_entity_update($entity, $type) {
  71. if ($type == 'flagging') {
  72. _flag_hook_test_record_invocation('hook_entity_update', func_get_args(), $entity);
  73. }
  74. }
  75. /**
  76. * Implements hook_entity_delete().
  77. */
  78. function flag_hook_test_entity_delete($entity, $type) {
  79. if ($type == 'flagging') {
  80. _flag_hook_test_record_invocation('hook_entity_delete', func_get_args(), $entity);
  81. }
  82. }
  83. // ========================================================= Configuration
  84. /**
  85. * Implements hook_flag_default_flags().
  86. */
  87. function flag_hook_test_flag_default_flags() {
  88. $flags = array();
  89. $flags['flag_hook_test_flag'] = array(
  90. 'entity_type' => 'node',
  91. 'title' => 'Test Flag',
  92. 'global' => FALSE,
  93. 'types' => array(
  94. 0 => 'article',
  95. ),
  96. 'flag_short' => 'Flag this',
  97. 'flag_long' => 'Flag this post',
  98. 'flag_message' => 'This post has been flagged',
  99. 'unflag_short' => 'Unflag this',
  100. 'unflag_long' => 'Remove this post from your flagged items',
  101. 'unflag_message' => 'This post has been unflagged',
  102. 'unflag_denied_text' => 'You may not unflag this item',
  103. 'link_type' => 'normal',
  104. 'weight' => 0,
  105. 'show_in_links' => array(
  106. 'full' => TRUE,
  107. 'teaser' => TRUE,
  108. ),
  109. 'show_as_field' => FALSE,
  110. 'show_on_form' => FALSE,
  111. 'access_author' => '',
  112. 'show_contextual_link' => TRUE,
  113. 'show_on_profile' => FALSE,
  114. 'access_uid' => '',
  115. 'api_version' => 3,
  116. );
  117. return $flags;
  118. }
  119. /**
  120. * Implements hook_rules_action_info().
  121. */
  122. function flag_hook_test_rules_action_info() {
  123. return array(
  124. 'flag_test_action' => array(
  125. 'label' => t('Flag test action'),
  126. 'group' => t('Flag test'),
  127. ),
  128. );
  129. }
  130. /**
  131. * Test action for flagging.
  132. */
  133. function flag_test_action() {
  134. _flag_hook_test_record_invocation('rules_event', func_get_args());
  135. }
  136. /**
  137. * Implements hook_default_rules_configuration().
  138. */
  139. function flag_hook_test_default_rules_configuration() {
  140. $configs['flag_test_rule_flag'] = rules_import('{ "flag_test_rule" : {
  141. "LABEL" : "Flag test rule",
  142. "PLUGIN" : "reaction rule",
  143. "OWNER" : "rules",
  144. "REQUIRES" : [ "flag_hook_test", "flag" ],
  145. "ON" : { "flag_flagged_flag_hook_test_flag" : [] },
  146. "DO" : [ { "flag_test_action" : [] } ]
  147. }
  148. }');
  149. $configs['flag_test_rule_unflag'] = rules_import('{ "flag_test_rule" : {
  150. "LABEL" : "Flag test rule",
  151. "PLUGIN" : "reaction rule",
  152. "OWNER" : "rules",
  153. "REQUIRES" : [ "flag_hook_test", "flag" ],
  154. "ON" : { "flag_unflagged_flag_hook_test_flag" : [] },
  155. "DO" : [ { "flag_test_action" : [] } ]
  156. }
  157. }');
  158. return $configs;
  159. }