rules_test.rules.inc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. <?php
  2. /**
  3. * @file Includes any rules integration provided by the module.
  4. */
  5. /**
  6. * Implements hook_rules_event_info().
  7. */
  8. function rules_test_rules_event_info() {
  9. return array('rules_test_event' => array(
  10. 'label' => t('Test event'),
  11. 'class' => 'RulesTestEventHandler',
  12. ));
  13. }
  14. /**
  15. * Implements hook_rules_file_info().
  16. */
  17. function rules_test_rules_file_info() {
  18. return array('rules_test.test');
  19. }
  20. /**
  21. * Implements hook_rules_condition_info().
  22. */
  23. function rules_test_rules_condition_info() {
  24. $items = array();
  25. $defaults = array(
  26. 'parameter' => array(
  27. 'node' => array('type' => 'node', 'label' => t('Content')),
  28. ),
  29. 'group' => t('Node'),
  30. );
  31. $items['rules_condition_content_is_type'] = array(
  32. 'label' => t('Content has type'),
  33. 'parameter' => array(
  34. 'node' => array('type' => 'node', 'label' => t('Content')),
  35. 'type' => array('type' => 'list<text>', 'label' => t('Content types')),
  36. ),
  37. 'help' => t('Evaluates to TRUE, if the given content has one of the selected content types.'),
  38. ) + $defaults;
  39. $items['rules_condition_content_is_published'] = $defaults + array(
  40. 'label' => t('Content is published'),
  41. );
  42. $items['rules_test_condition_true'] = array(
  43. 'label' => t('Test condition returning true'),
  44. 'group' => t('Rules test'),
  45. );
  46. $items['rules_test_condition_false'] = array(
  47. 'label' => t('Test condition returning false'),
  48. 'group' => t('Rules test'),
  49. );
  50. // A condition for testing passing entities wrapped.
  51. $items['rules_test_condition_node_wrapped'] = array(
  52. 'label' => t('Content is published'),
  53. 'parameter' => array(
  54. 'node' => array(
  55. 'type' => 'node',
  56. 'label' => t('Content'),
  57. 'wrapped' => TRUE,
  58. ),
  59. ),
  60. 'group' => t('Node'),
  61. );
  62. return $items;
  63. }
  64. /**
  65. * Condition implementation returning true.
  66. */
  67. function rules_test_condition_true($settings, $state, $element) {
  68. if (!$element instanceof RulesCondition) {
  69. throw new Exception('Rules element has not been passed to condition.');
  70. }
  71. rules_log('condition true called');
  72. return TRUE;
  73. }
  74. /**
  75. * Condition implementation returning false.
  76. */
  77. function rules_test_condition_false() {
  78. rules_log('condition false called');
  79. return FALSE;
  80. }
  81. /**
  82. * Condition implementation receiving the node wrapped.
  83. */
  84. function rules_test_condition_node_wrapped($wrapper) {
  85. return $wrapper instanceof EntityMetadataWrapper;
  86. }
  87. /**
  88. * Implements hook_rules_action_info().
  89. */
  90. function rules_test_rules_action_info() {
  91. $items['rules_test_action'] = array(
  92. 'label' => t('Test action'),
  93. 'group' => t('Rules test'),
  94. );
  95. return $items + array(
  96. 'rules_node_publish_action' => array(
  97. 'label' => t('Publish content, but do not save'),
  98. 'parameter' => array(
  99. 'node' => array('type' => 'node', 'label' => t('Content')),
  100. ),
  101. 'callbacks' => array(
  102. 'help' => 'rules_test_custom_help',
  103. ),
  104. 'base' => 'node_publish_action',
  105. ),
  106. 'rules_node_publish_action_save' => array(
  107. 'label' => t('Publish content'),
  108. 'parameter' => array(
  109. 'node' => array(
  110. 'type' => 'node',
  111. 'label' => t('Content'),
  112. 'save' => TRUE,
  113. ),
  114. ),
  115. 'base' => 'node_publish_action',
  116. ),
  117. 'rules_node_make_sticky_action' => array(
  118. 'label' => t('Make content sticky'),
  119. 'parameter' => array(
  120. 'node' => array(
  121. 'type' => 'node',
  122. 'label' => t('Content'),
  123. 'save' => TRUE,
  124. ),
  125. ),
  126. 'base' => 'node_make_sticky_action',
  127. ),
  128. // The same action again requiring a 'page' node.
  129. 'rules_node_page_make_sticky_action' => array(
  130. 'label' => t('Mage page content sticky'),
  131. 'parameter' => array(
  132. 'node' => array(
  133. 'type' => 'node',
  134. 'label' => t('Content'),
  135. 'save' => TRUE,
  136. 'bundles' => array('page'),
  137. ),
  138. ),
  139. 'base' => 'node_make_sticky_action',
  140. ),
  141. 'rules_action_test_reference' => array(
  142. 'label' => t('Change argument passed by reference'),
  143. 'parameter' => array(
  144. // For references working right, we need a data type with a wrapper.
  145. 'arg' => array('type' => 'test'),
  146. ),
  147. ),
  148. 'rules_action_load_node' => array(
  149. 'label' => t('Fetch content by id'),
  150. 'parameter' => array(
  151. 'nid' => array('type' => 'integer', 'label' => t('Content ID')),
  152. 'vid' => array(
  153. 'type' => 'integer',
  154. 'label' => t('Content Revision ID'),
  155. '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."),
  156. 'optional' => TRUE,
  157. ),
  158. ),
  159. 'provides' => array(
  160. 'node_loaded' => array(
  161. 'type' => 'node',
  162. 'label' => t('Loaded content'),
  163. 'label callback' => 'rules_action_load_node_variable_label',
  164. ),
  165. ),
  166. 'group' => t('Node'),
  167. 'access callback' => 'rules_node_integration_access',
  168. ),
  169. 'rules_action_delete_node' => array(
  170. 'label' => t('Delete content'),
  171. 'parameter' => array(
  172. 'node' => array('type' => 'node', 'label' => t('Content')),
  173. ),
  174. 'group' => t('Node'),
  175. 'access callback' => 'rules_node_integration_access',
  176. ),
  177. // An action for testing named parameters.
  178. 'rules_action_node_set_title' => array(
  179. 'label' => t('Content'),
  180. 'parameter' => array(
  181. 'node' => array('type' => 'node', 'label' => t('Content')),
  182. 'title' => array('type' => 'text', 'label' => t('Text')),
  183. ),
  184. 'named parameter' => TRUE,
  185. 'group' => t('Node'),
  186. 'access callback' => 'rules_node_integration_access',
  187. ),
  188. // Tests automatic saving with a non-entity data type.
  189. 'test_type_save' => array(
  190. 'base' => 'rules_test_type_save',
  191. 'label' => t('Save test type'),
  192. 'parameter' => array(
  193. 'node' => array('type' => 'rules_test_type', 'label' => t('Test content'), 'save' => TRUE),
  194. ),
  195. 'group' => t('Node'),
  196. ),
  197. );
  198. }
  199. /**
  200. * Test action doing nothing exception logging it has been called.
  201. */
  202. function rules_test_action() {
  203. rules_log('action called');
  204. }
  205. /**
  206. * Action for testing writing class-based actions.
  207. */
  208. class RulesTestClassAction extends RulesActionHandlerBase {
  209. /**
  210. * Defines the action.
  211. */
  212. public static function getInfo() {
  213. return array(
  214. 'name' => 'rules_test_class_action',
  215. 'label' => t('Test class based action'),
  216. 'group' => t('Node'),
  217. 'parameter' => array(
  218. 'node' => array(
  219. 'type' => 'node',
  220. 'label' => t('Node'),
  221. ),
  222. ),
  223. );
  224. }
  225. /**
  226. * Executes the action.
  227. */
  228. public function execute($node) {
  229. rules_log('Action called with node ' . $node->nid);
  230. }
  231. }
  232. /**
  233. * Implements hook_rules_data_info().
  234. */
  235. function rules_test_rules_data_info() {
  236. return array(
  237. 'rules_test_type' => array(
  238. 'label' => t('test type'),
  239. 'wrap' => TRUE,
  240. 'wrapper class' => 'RulesTestTypeWrapper',
  241. ),
  242. );
  243. }
  244. /**
  245. * Implements hook_rules_data_info_alter().
  246. */
  247. function rules_test_rules_data_info_alter(&$data_info) {
  248. $data_info['log_entry']['creation callback'] = 'rules_action_data_create_array';
  249. }
  250. /**
  251. * The custom wrapper class for the rules test type.
  252. *
  253. * For testing we internally just make use of nodes.
  254. */
  255. class RulesTestTypeWrapper extends RulesIdentifiableDataWrapper implements RulesDataWrapperSavableInterface {
  256. protected function extractIdentifier($data) {
  257. return $data->nid;
  258. }
  259. protected function load($id) {
  260. return node_load($id);
  261. }
  262. public function save() {
  263. node_save($this->value());
  264. }
  265. }
  266. /**
  267. * Implements hook_rules_plugin_info().
  268. */
  269. function rules_test_rules_plugin_info() {
  270. return array(
  271. 'rules test container' => array(
  272. 'label' => t('Test container'),
  273. 'class' => 'RulesTestContainer',
  274. 'embeddable' => 'RulesActionContainer',
  275. ),
  276. );
  277. }
  278. /**
  279. * Test container plugin.
  280. */
  281. class RulesTestContainer extends RulesContainerPlugin {
  282. protected $itemName = 'rules test container';
  283. /**
  284. * Evaluate the element on a given rules evaluation state.
  285. */
  286. public function evaluate(RulesState $state) {
  287. // Do nothing.
  288. }
  289. }
  290. /**
  291. * Test event handler class.
  292. */
  293. class RulesTestEventHandler extends RulesEventDefaultHandler implements RulesEventDispatcherInterface {
  294. /**
  295. * Name of the variable in which to store the state of the event handler.
  296. *
  297. * @var string
  298. */
  299. protected $variableName = 'rules_test_event_handler_watch';
  300. /**
  301. * Implements RulesEventDispatcherInterface::startWatching().
  302. */
  303. public function startWatching() {
  304. variable_set($this->variableName, TRUE);
  305. }
  306. /**
  307. * Implements RulesEventDispatcherInterface::stopWatching().
  308. */
  309. public function stopWatching() {
  310. variable_set($this->variableName, FALSE);
  311. }
  312. /**
  313. * Implements RulesEventDispatcherInterface::isWatching().
  314. */
  315. public function isWatching() {
  316. return (bool) variable_get($this->variableName);
  317. }
  318. }