Projet

Général

Profil

Paste
Télécharger (3,93 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / rules / rules_scheduler / rules_scheduler.test @ 76e2e7c3

1
<?php
2

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

    
8
class RulesSchedulerTestCase extends DrupalWebTestCase {
9

    
10
  static function getInfo() {
11
    return array(
12
      'name' => 'Rules Scheduler tests',
13
      'description' => 'Test scheduling components.',
14
      'group' => 'Rules',
15
    );
16
  }
17

    
18
  function setUp() {
19
    parent::setUp('rules_scheduler', 'rules_scheduler_test');
20
    RulesLog::logger()->clear();
21
    variable_set('rules_debug_log', 1);
22
  }
23

    
24
  /**
25
   * Tests scheduling components from the action.
26
   *
27
   * Note that this also makes sure Rules properly handles timezones, else this
28
   * test could fail due to a wrong 'now' timestamp.
29
   */
30
  function testComponentSchedule() {
31
    $set = rules_rule_set(array(
32
      'node1' => array('type' => 'node', 'label' => 'node'),
33
    ));
34
    $set->rule(rule()->condition('node_is_published', array('node:select' => 'node1'))
35
                     ->action('node_unpublish', array('node:select' => 'node1'))
36
               );
37
    $set->integrityCheck()->save('rules_test_set_2');
38

    
39
    // Use different names for the variables to ensure they are properly mapped.
40
    $rule = rule(array(
41
      'node2' => array('type' => 'node', 'label' => 'node'),
42
    ));
43
    $rule->action('schedule', array(
44
      'component' => 'rules_test_set_2',
45
      'identifier' => 'node_[node2:nid]',
46
      'date' => 'now',
47
      'param_node1:select' => 'node2',
48
    ));
49

    
50
    $node = $this->drupalCreateNode(array('title' => 'The title.', 'status' => 1));
51
    $rule->execute($node);
52

    
53
    // Run cron to let the rules scheduler do its work.
54
    drupal_cron_run();
55

    
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
  /**
62
   * Make sure recurion prevention is working fine for scheduled rule sets.
63
   */
64
  function testRecursionPrevention() {
65
    $set = rules_rule_set(array(
66
      'node1' => array('type' => 'node', 'label' => 'node'),
67
    ));
68
    $set->rule(rule()->condition('node_is_published', array('node:select' => 'node1'))
69
                     ->action('node_unpublish', array('node:select' => 'node1'))
70
               );
71
    $set->integrityCheck()->save('rules_test_set_2');
72

    
73
    // Add an reaction rule that is triggered upon a node save. The scheduled
74
    // component changes the node, thus it would be scheduled again and run in
75
    // an endless loop.
76
    $rule = rules_reaction_rule();
77
    $rule->event('node_insert');
78
    $rule->event('node_update');
79
    $rule->action('schedule', array(
80
      'component' => 'rules_test_set_2',
81
      'identifier' => '',
82
      'date' => 'now',
83
      'param_node1:select' => 'node',
84
    ));
85
    $rule->save();
86

    
87
    // Create a node, what triggers the rule.
88
    $node = $this->drupalCreateNode(array('title' => 'The title.', 'status' => 1));
89
    // Run cron to let the rules scheduler do its work.
90
    drupal_cron_run();
91

    
92
    $node = node_load($node->nid, NULL, TRUE);
93
    $this->assertFalse($node->status, 'The component has been properly scheduled.');
94
    $text1 = RulesLog::logger()->render();
95
    $text2 = RulesTestCase::t('Not evaluating reaction rule %unlabeled to prevent recursion.', array('unlabeled' => $rule->name));
96
    $this->assertTrue((strpos($text1, $text2) !== FALSE), "Scheduled recursion prevented.");
97
    RulesLog::logger()->checkLog();
98
  }
99

    
100
  /**
101
   * Tests that custom task handlers are properly invoked.
102
   */
103
  public function testCustomTaskHandler() {
104
    // Set up a scheduled task that will simply write a variable when executed.
105
    $variable = 'rules_schedule_task_handler_variable';
106
    rules_scheduler_schedule_task(array(
107
      'date' => REQUEST_TIME,
108
      'identifier' => '',
109
      'config' => '',
110
      'data' => array('variable' => $variable),
111
      'handler' => 'RulesTestTaskHandler',
112
    ));
113

    
114
    // Run cron to let the rules scheduler do its work.
115
    drupal_cron_run();
116

    
117
    // The task handler should have set the variable to TRUE now.
118
    $this->assertTrue(variable_get($variable));
119
  }
120
}
121