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