Projet

Général

Profil

Paste
Télécharger (4,9 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / rules_scheduler / tests / rules_scheduler.test @ 950416da

1
<?php
2

    
3
/**
4
 * @file
5
 * Rules Scheduler tests.
6
 */
7

    
8
/**
9
 * Test cases for the Rules Scheduler module.
10
 */
11
class RulesSchedulerTestCase extends DrupalWebTestCase {
12

    
13
  /**
14
   * Declares test metadata.
15
   */
16
  public static function getInfo() {
17
    return array(
18
      'name' => 'Rules Scheduler tests',
19
      'description' => 'Test scheduling components.',
20
      'group' => 'Rules',
21
    );
22
  }
23

    
24
  /**
25
   * Overrides DrupalWebTestCase::setUp().
26
   */
27
  protected function setUp() {
28
    parent::setUp('rules_scheduler', 'rules_scheduler_test');
29
    RulesLog::logger()->clear();
30
    variable_set('rules_debug_log', TRUE);
31
  }
32

    
33
  /**
34
   * Tests scheduling components from the action.
35
   *
36
   * Note that this also makes sure Rules properly handles timezones, else this
37
   * test could fail due to a wrong 'now' timestamp.
38
   */
39
  public function testComponentSchedule() {
40
    $set = rules_rule_set(array(
41
      'node1' => array('type' => 'node', 'label' => 'node'),
42
    ));
43
    $set->rule(rule()->condition('node_is_published', array('node:select' => 'node1'))
44
                     ->action('node_unpublish', array('node:select' => 'node1'))
45
               );
46
    $set->integrityCheck()->save('rules_test_set_2');
47

    
48
    // Use different names for the variables to ensure they are properly mapped.
49
    $rule = rule(array(
50
      'node2' => array('type' => 'node', 'label' => 'node'),
51
    ));
52
    $rule->action('schedule', array(
53
      'component' => 'rules_test_set_2',
54
      'identifier' => 'node_[node2:nid]',
55
      'date' => 'now',
56
      'param_node1:select' => 'node2',
57
    ));
58

    
59
    $node = $this->drupalCreateNode(array('title' => 'The title.', 'status' => 1));
60
    $rule->execute($node);
61

    
62
    // Run cron to let the rules scheduler do its work.
63
    $this->cronRun();
64

    
65
    $node = node_load($node->nid, NULL, TRUE);
66
    $this->assertFalse($node->status, 'The component has been properly scheduled.');
67
    RulesLog::logger()->checkLog();
68
  }
69

    
70
  /**
71
   * Makes sure recursion prevention is working fine for scheduled rule sets.
72
   */
73
  public function testRecursionPrevention() {
74
    $set = rules_rule_set(array(
75
      'node1' => array('type' => 'node', 'label' => 'node'),
76
    ));
77
    $set->rule(rule()->condition('node_is_published', array('node:select' => 'node1'))
78
                     ->action('node_unpublish', array('node:select' => 'node1'))
79
               );
80
    $set->integrityCheck()->save('rules_test_set_2');
81

    
82
    // Add an reaction rule that is triggered upon a node save. The scheduled
83
    // component changes the node, thus it would be scheduled again and run in
84
    // an endless loop.
85
    $rule = rules_reaction_rule();
86
    $rule->event('node_insert');
87
    $rule->event('node_update');
88
    $rule->action('schedule', array(
89
      'component' => 'rules_test_set_2',
90
      'identifier' => 'test_recursion_prevention',
91
      'date' => 'now',
92
      'param_node1:select' => 'node',
93
    ));
94
    $rule->save();
95

    
96
    // Create a node, what triggers the rule.
97
    $node = $this->drupalCreateNode(array('title' => 'The title.', 'status' => 1));
98
    // Run cron to let the rules scheduler do its work.
99
    $this->cronRun();
100

    
101
    $node = node_load($node->nid, NULL, TRUE);
102
    $this->assertFalse($node->status, 'The component has been properly scheduled.');
103

    
104
    // Create a simple user account with permission to see the dblog.
105
    $user = $this->drupalCreateUser(array('access site reports'));
106
    $this->drupalLogin($user);
107

    
108
    // View the database log.
109
    $this->drupalGet('admin/reports/dblog');
110

    
111
    // Can't use
112
    // $this->clickLink('Rules debug information: " Scheduled evaluation...')
113
    // because xpath doesn't allow : or " in the string.
114
    // So instead, use our own xpath to figure out the href of the second link
115
    // on the page (the first link is the most recent log entry, which is the
116
    // log entry for the user login, above.)
117

    
118
    // All links.
119
    $links = $this->xpath('//a[contains(@href, :href)]', array(':href' => 'admin/reports/event/'));
120
    // Strip off /?q= from href.
121
    $href = explode('=', $links[1]['href']);
122
    // Click the link for the RulesLog entry.
123
    $this->drupalGet($href[1]);
124
    $this->assertRaw(RulesTestCase::t('Not evaluating reaction rule %unlabeled to prevent recursion.', array('unlabeled' => $rule->name)), "Scheduled recursion prevented.");
125
    RulesLog::logger()->checkLog();
126
  }
127

    
128
  /**
129
   * Tests that custom task handlers are properly invoked.
130
   */
131
  public function testCustomTaskHandler() {
132
    // Set up a scheduled task that will simply write a variable when executed.
133
    $variable = 'rules_schedule_task_handler_variable';
134
    rules_scheduler_schedule_task(array(
135
      'date' => REQUEST_TIME,
136
      'identifier' => '',
137
      'config' => '',
138
      'data' => array('variable' => $variable),
139
      'handler' => 'RulesTestTaskHandler',
140
    ));
141

    
142
    // Run cron to let the rules scheduler do its work.
143
    $this->cronRun();
144

    
145
    // The task handler should have set the variable to TRUE now.
146
    $this->assertTrue(variable_get($variable));
147
  }
148

    
149
}