rules_test.rules.inc 9.8 KB

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