rules_scheduler.test 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * @file
  4. * Rules Scheduler tests.
  5. */
  6. /**
  7. * Test cases for the Rules Scheduler module.
  8. */
  9. class RulesSchedulerTestCase extends DrupalWebTestCase {
  10. /**
  11. * Declares test metadata.
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Rules Scheduler tests',
  16. 'description' => 'Test scheduling components.',
  17. 'group' => 'Rules',
  18. );
  19. }
  20. /**
  21. * Overrides DrupalWebTestCase::setUp().
  22. */
  23. protected function setUp() {
  24. parent::setUp('rules_scheduler', 'rules_scheduler_test');
  25. RulesLog::logger()->clear();
  26. variable_set('rules_debug_log', TRUE);
  27. }
  28. /**
  29. * Tests scheduling components from the action.
  30. *
  31. * Note that this also makes sure Rules properly handles timezones, else this
  32. * test could fail due to a wrong 'now' timestamp.
  33. */
  34. public function testComponentSchedule() {
  35. $set = rules_rule_set(array(
  36. 'node1' => array('type' => 'node', 'label' => 'node'),
  37. ));
  38. $set->rule(rule()->condition('node_is_published', array('node:select' => 'node1'))
  39. ->action('node_unpublish', array('node:select' => 'node1'))
  40. );
  41. $set->integrityCheck()->save('rules_test_set_2');
  42. // Use different names for the variables to ensure they are properly mapped.
  43. $rule = rule(array(
  44. 'node2' => array('type' => 'node', 'label' => 'node'),
  45. ));
  46. $rule->action('schedule', array(
  47. 'component' => 'rules_test_set_2',
  48. 'identifier' => 'node_[node2:nid]',
  49. 'date' => 'now',
  50. 'param_node1:select' => 'node2',
  51. ));
  52. $node = $this->drupalCreateNode(array('title' => 'The title.', 'status' => 1));
  53. $rule->execute($node);
  54. // Run cron to let the rules scheduler do its work.
  55. $this->cronRun();
  56. $node = node_load($node->nid, NULL, TRUE);
  57. $this->assertFalse($node->status, 'The component has been properly scheduled.');
  58. RulesLog::logger()->checkLog();
  59. }
  60. /**
  61. * Makes sure recursion prevention is working fine for scheduled rule sets.
  62. */
  63. public function testRecursionPrevention() {
  64. $set = rules_rule_set(array(
  65. 'node1' => array('type' => 'node', 'label' => 'node'),
  66. ));
  67. $set->rule(rule()->condition('node_is_published', array('node:select' => 'node1'))
  68. ->action('node_unpublish', array('node:select' => 'node1'))
  69. );
  70. $set->integrityCheck()->save('rules_test_set_2');
  71. // Add an reaction rule that is triggered upon a node save. The scheduled
  72. // component changes the node, thus it would be scheduled again and run in
  73. // an endless loop.
  74. $rule = rules_reaction_rule();
  75. $rule->event('node_insert');
  76. $rule->event('node_update');
  77. $rule->action('schedule', array(
  78. 'component' => 'rules_test_set_2',
  79. 'identifier' => 'test_recursion_prevention',
  80. 'date' => 'now',
  81. 'param_node1:select' => 'node',
  82. ));
  83. $rule->save();
  84. // Create a node, what triggers the rule.
  85. $node = $this->drupalCreateNode(array('title' => 'The title.', 'status' => 1));
  86. // Run cron to let the rules scheduler do its work.
  87. $this->cronRun();
  88. $node = node_load($node->nid, NULL, TRUE);
  89. $this->assertFalse($node->status, 'The component has been properly scheduled.');
  90. // Create a simple user account with permission to see the dblog.
  91. $user = $this->drupalCreateUser(array('access site reports'));
  92. $this->drupalLogin($user);
  93. // View the database log.
  94. $this->drupalGet('admin/reports/dblog');
  95. // Can't use
  96. // $this->clickLink('Rules debug information: " Scheduled evaluation...')
  97. // because xpath doesn't allow : or " in the string.
  98. // So instead, use our own xpath to figure out the href of the second link
  99. // on the page (the first link is the most recent log entry, which is the
  100. // log entry for the user login, above.)
  101. // All links.
  102. $links = $this->xpath('//a[contains(@href, :href)]', array(':href' => 'admin/reports/event/'));
  103. // Strip off /?q= from href.
  104. $href = explode('=', $links[1]['href']);
  105. // Click the link for the RulesLog entry.
  106. $this->drupalGet($href[1]);
  107. $this->assertRaw(RulesTestCase::t('Not evaluating reaction rule %unlabeled to prevent recursion.', array('unlabeled' => $rule->name)), "Scheduled recursion prevented.");
  108. RulesLog::logger()->checkLog();
  109. }
  110. /**
  111. * Tests that custom task handlers are properly invoked.
  112. */
  113. public function testCustomTaskHandler() {
  114. // Set up a scheduled task that will simply write a variable when executed.
  115. $variable = 'rules_schedule_task_handler_variable';
  116. rules_scheduler_schedule_task(array(
  117. 'date' => REQUEST_TIME,
  118. 'identifier' => '',
  119. 'config' => '',
  120. 'data' => array('variable' => $variable),
  121. 'handler' => 'RulesTestTaskHandler',
  122. ));
  123. // Run cron to let the rules scheduler do its work.
  124. $this->cronRun();
  125. // The task handler should have set the variable to TRUE now.
  126. $this->assertTrue(variable_get($variable));
  127. }
  128. }