rules_test.rules.inc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. /**
  3. * @file Includes any rules integration provided by the module.
  4. */
  5. /**
  6. * Implements hook_rules_file_info().
  7. */
  8. function rules_test_rules_file_info() {
  9. return array('rules_test.test');
  10. }
  11. /**
  12. * Implements hook_rules_condition_info().
  13. */
  14. function rules_test_rules_condition_info() {
  15. $items = array();
  16. $defaults = array(
  17. 'parameter' => array(
  18. 'node' => array('type' => 'node', 'label' => t('Content')),
  19. ),
  20. 'group' => t('Node'),
  21. );
  22. $items['rules_condition_content_is_type'] = array(
  23. 'label' => t('Content has type'),
  24. 'parameter' => array(
  25. 'node' => array('type' => 'node', 'label' => t('Content')),
  26. 'type' => array('type' => 'list<text>', 'label' => t('Content types')),
  27. ),
  28. 'help' => t('Evaluates to TRUE, if the given content has one of the selected content types.'),
  29. ) + $defaults;
  30. $items['rules_condition_content_is_published'] = $defaults + array(
  31. 'label' => t('Content is published'),
  32. );
  33. $items['rules_test_condition_true'] = array(
  34. 'label' => t('Test condition returning true'),
  35. 'group' => t('Rules test'),
  36. );
  37. $items['rules_test_condition_false'] = array(
  38. 'label' => t('Test condition returning false'),
  39. 'group' => t('Rules test'),
  40. );
  41. // A condition for testing passing entities wrapped.
  42. $items['rules_test_condition_node_wrapped'] = array(
  43. 'label' => t('Content is published'),
  44. 'parameter' => array(
  45. 'node' => array(
  46. 'type' => 'node',
  47. 'label' => t('Content'),
  48. 'wrapped' => TRUE,
  49. ),
  50. ),
  51. 'group' => t('Node'),
  52. );
  53. return $items;
  54. }
  55. /**
  56. * Condition implementation returning true.
  57. */
  58. function rules_test_condition_true($settings, $state, $element) {
  59. if (!$element instanceof RulesCondition) {
  60. throw new Exception('Rules element has not been passed to condition.');
  61. }
  62. rules_log('condition true called');
  63. return TRUE;
  64. }
  65. /**
  66. * Condition implementation returning false.
  67. */
  68. function rules_test_condition_false() {
  69. rules_log('condition false called');
  70. return FALSE;
  71. }
  72. /**
  73. * Condition implementation receiving the node wrapped.
  74. */
  75. function rules_test_condition_node_wrapped($wrapper) {
  76. return $wrapper instanceof EntityMetadataWrapper;
  77. }
  78. /**
  79. * Implements hook_rules_action_info().
  80. */
  81. function rules_test_rules_action_info() {
  82. $items['rules_test_action'] = array(
  83. 'label' => t('Test action'),
  84. 'group' => t('Rules test'),
  85. );
  86. return $items + array(
  87. 'rules_node_publish_action' => array(
  88. 'label' => t('Publish content, but do not save'),
  89. 'parameter' => array(
  90. 'node' => array('type' => 'node', 'label' => t('Content')),
  91. ),
  92. 'callbacks' => array(
  93. 'help' => 'rules_test_custom_help',
  94. ),
  95. 'base' => 'node_publish_action',
  96. ),
  97. 'rules_node_publish_action_save' => array(
  98. 'label' => t('Publish content'),
  99. 'parameter' => array(
  100. 'node' => array(
  101. 'type' => 'node',
  102. 'label' => t('Content'),
  103. 'save' => TRUE,
  104. ),
  105. ),
  106. 'base' => 'node_publish_action',
  107. ),
  108. 'rules_node_make_sticky_action' => array(
  109. 'label' => t('Make content sticky'),
  110. 'parameter' => array(
  111. 'node' => array(
  112. 'type' => 'node',
  113. 'label' => t('Content'),
  114. 'save' => TRUE,
  115. ),
  116. ),
  117. 'base' => 'node_make_sticky_action',
  118. ),
  119. // The same action again requiring a 'page' node.
  120. 'rules_node_page_make_sticky_action' => array(
  121. 'label' => t('Mage page content sticky'),
  122. 'parameter' => array(
  123. 'node' => array(
  124. 'type' => 'node',
  125. 'label' => t('Content'),
  126. 'save' => TRUE,
  127. 'bundles' => array('page'),
  128. ),
  129. ),
  130. 'base' => 'node_make_sticky_action',
  131. ),
  132. 'rules_action_test_reference' => array(
  133. 'label' => t('Change argument passed by reference'),
  134. 'parameter' => array(
  135. // For references working right, we need a data type with a wrapper.
  136. 'arg' => array('type' => 'test'),
  137. ),
  138. ),
  139. 'rules_action_load_node' => array(
  140. 'label' => t('Fetch content by id'),
  141. 'parameter' => array(
  142. 'nid' => array('type' => 'integer', 'label' => t('Content ID')),
  143. 'vid' => array(
  144. 'type' => 'integer',
  145. 'label' => t('Content Revision ID'),
  146. 'description' => t("If you want to fetch a specific revision, specify it's revision id. Else leave it empty to fetch the currently active revision."),
  147. 'optional' => TRUE,
  148. ),
  149. ),
  150. 'provides' => array(
  151. 'node_loaded' => array(
  152. 'type' => 'node',
  153. 'label' => t('Loaded content'),
  154. 'label callback' => 'rules_action_load_node_variable_label',
  155. ),
  156. ),
  157. 'group' => t('Node'),
  158. 'access callback' => 'rules_node_integration_access',
  159. ),
  160. 'rules_action_delete_node' => array(
  161. 'label' => t('Delete content'),
  162. 'parameter' => array(
  163. 'node' => array('type' => 'node', 'label' => t('Content')),
  164. ),
  165. 'group' => t('Node'),
  166. 'access callback' => 'rules_node_integration_access',
  167. ),
  168. // An action for testing named parameters.
  169. 'rules_action_node_set_title' => array(
  170. 'label' => t('Content'),
  171. 'parameter' => array(
  172. 'node' => array('type' => 'node', 'label' => t('Content')),
  173. 'title' => array('type' => 'text', 'label' => t('Text')),
  174. ),
  175. 'named parameter' => TRUE,
  176. 'group' => t('Node'),
  177. 'access callback' => 'rules_node_integration_access',
  178. ),
  179. // Tests automatic saving with a non-entity data type.
  180. 'test_type_save' => array(
  181. 'base' => 'rules_test_type_save',
  182. 'label' => t('Save test type'),
  183. 'parameter' => array(
  184. 'node' => array('type' => 'rules_test_type', 'label' => t('Test content'), 'save' => TRUE),
  185. ),
  186. 'group' => t('Node'),
  187. ),
  188. );
  189. }
  190. /**
  191. * Test action doing nothing exception logging it has been called.
  192. */
  193. function rules_test_action() {
  194. rules_log('action called');
  195. }
  196. /**
  197. * Implements hook_rules_data_info().
  198. */
  199. function rules_test_rules_data_info() {
  200. return array(
  201. 'rules_test_type' => array(
  202. 'label' => t('test type'),
  203. 'wrap' => TRUE,
  204. 'wrapper class' => 'RulesTestTypeWrapper',
  205. ),
  206. );
  207. }
  208. /**
  209. * Implements hook_rules_data_info_alter().
  210. */
  211. function rules_test_rules_data_info_alter(&$data_info) {
  212. $data_info['log_entry']['creation callback'] = 'rules_action_data_create_array';
  213. }
  214. /**
  215. * The custom wrapper class for the rules test type.
  216. *
  217. * For testing we internally just make use of nodes.
  218. */
  219. class RulesTestTypeWrapper extends RulesIdentifiableDataWrapper implements RulesDataWrapperSavableInterface {
  220. protected function extractIdentifier($data) {
  221. return $data->nid;
  222. }
  223. protected function load($id) {
  224. return node_load($id);
  225. }
  226. public function save() {
  227. node_save($this->value());
  228. }
  229. }