rules_scheduler.test 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /**
  3. * @file
  4. * Rules Scheduler tests.
  5. */
  6. class RulesSchedulerTestCase extends DrupalWebTestCase {
  7. static function getInfo() {
  8. return array(
  9. 'name' => 'Rules Scheduler tests',
  10. 'description' => 'Test scheduling components.',
  11. 'group' => 'Rules',
  12. );
  13. }
  14. function setUp() {
  15. parent::setUp('rules_scheduler');
  16. RulesLog::logger()->clear();
  17. variable_set('rules_debug_log', 1);
  18. }
  19. /**
  20. * Tests scheduling components from the action.
  21. *
  22. * Note that this also makes sure Rules properly handles timezones, else this
  23. * test could fail due to a wrong 'now' timestamp.
  24. */
  25. function testComponentSchedule() {
  26. $set = rules_rule_set(array(
  27. 'node1' => array('type' => 'node', 'label' => 'node'),
  28. ));
  29. $set->rule(rule()->condition('node_is_published', array('node:select' => 'node1'))
  30. ->action('node_unpublish', array('node:select' => 'node1'))
  31. );
  32. $set->integrityCheck()->save('rules_test_set_2');
  33. // Use different names for the variables to ensure they are properly mapped.
  34. $rule = rule(array(
  35. 'node2' => array('type' => 'node', 'label' => 'node'),
  36. ));
  37. $rule->action('schedule', array(
  38. 'component' => 'rules_test_set_2',
  39. 'identifier' => 'node_[node2:nid]',
  40. 'date' => 'now',
  41. 'param_node1:select' => 'node2',
  42. ));
  43. $node = $this->drupalCreateNode(array('title' => 'The title.', 'status' => 1));
  44. $rule->execute($node);
  45. // Run cron to let the rules scheduler do its work.
  46. drupal_cron_run();
  47. $node = node_load($node->nid, NULL, TRUE);
  48. $this->assertFalse($node->status, 'The component has been properly scheduled.');
  49. RulesLog::logger()->checkLog();
  50. }
  51. /**
  52. * Make sure recurion prevention is working fine for scheduled rule sets.
  53. */
  54. function testRecursionPrevention() {
  55. $set = rules_rule_set(array(
  56. 'node1' => array('type' => 'node', 'label' => 'node'),
  57. ));
  58. $set->rule(rule()->condition('node_is_published', array('node:select' => 'node1'))
  59. ->action('node_unpublish', array('node:select' => 'node1'))
  60. );
  61. $set->integrityCheck()->save('rules_test_set_2');
  62. // Add an reaction rule that is triggered upon a node save. The scheduled
  63. // component changes the node, thus it would be scheduled again and run in
  64. // an endless loop.
  65. $rule = rules_reaction_rule();
  66. $rule->event('node_insert');
  67. $rule->event('node_update');
  68. $rule->action('schedule', array(
  69. 'component' => 'rules_test_set_2',
  70. 'identifier' => '',
  71. 'date' => 'now',
  72. 'param_node1:select' => 'node',
  73. ));
  74. $rule->save();
  75. // Create a node, what triggers the rule.
  76. $node = $this->drupalCreateNode(array('title' => 'The title.', 'status' => 1));
  77. // Run cron to let the rules scheduler do its work.
  78. drupal_cron_run();
  79. $node = node_load($node->nid, NULL, TRUE);
  80. $this->assertFalse($node->status, 'The component has been properly scheduled.');
  81. $text1 = RulesLog::logger()->render();
  82. $text2 = RulesTestCase::t('Not evaluating reaction rule %unlabeled to prevent recursion.', array('unlabeled' => $rule->name));
  83. $this->assertTrue((strpos($text1, $text2) !== FALSE), "Scheduled recursion prevented.");
  84. RulesLog::logger()->checkLog();
  85. }
  86. }