Projet

Général

Profil

Révision 950416da

Ajouté par Assos Assos il y a plus de 5 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/rules/tests/rules.test
5 5
 * Rules tests.
6 6
 */
7 7

  
8
/**
9
 * Rules test cases.
10
 */
8 11
class RulesTestCase extends DrupalWebTestCase {
9 12

  
10
  static function getInfo() {
13
  /**
14
   * Declares test metadata.
15
   */
16
  public static function getInfo() {
11 17
    return array(
12 18
      'name' => 'Rules Engine tests',
13 19
      'description' => 'Test using the rules API to create and evaluate rules.',
......
15 21
    );
16 22
  }
17 23

  
18
  function setUp() {
24
  /**
25
   * Overrides DrupalWebTestCase::setUp().
26
   */
27
  protected function setUp() {
19 28
    parent::setUp('rules', 'rules_test');
20 29
    RulesLog::logger()->clear();
21
    variable_set('rules_debug_log', 1);
30
    variable_set('rules_debug_log', TRUE);
22 31
  }
23 32

  
24 33
  /**
25 34
   * Calculates the output of t() given an array of placeholders to replace.
26 35
   */
27
  static function t($text, $strings) {
36
  public static function t($text, $strings) {
28 37
    $placeholders = array();
29 38
    foreach ($strings as $key => $string) {
30 39
      $key = !is_numeric($key) ? $key : $string;
......
33 42
    return strtr($text, $placeholders);
34 43
  }
35 44

  
45
  /**
46
   * Helper function to create a test Rule.
47
   */
36 48
  protected function createTestRule() {
37 49
    $rule = rule();
38 50
    $rule->condition('rules_test_condition_true')
......
53 65
  /**
54 66
   * Tests creating a rule and iterating over the rule elements.
55 67
   */
56
  function testRuleCreation() {
68
  public function testRuleCreation() {
57 69
    $rule = $this->createTestRule();
58 70
    $rule->integrityCheck();
59 71
    $rule->execute();
......
78 90
  }
79 91

  
80 92
  /**
81
   * Test handling dependencies.
93
   * Tests handling dependencies.
82 94
   */
83
  function testdependencies() {
95
  public function testDependencies() {
84 96
    $action = rules_action('rules_node_publish_action');
85 97
    $this->assertEqual($action->dependencies(), array('rules_test'), 'Providing module is returned as dependency.');
86 98

  
......
147 159
  }
148 160

  
149 161
  /**
150
   * Test setting up an action with some action_info and serializing and
151
   * executing it.
162
   * Tests setting up an action, serializing, and executing it.
152 163
   */
153
  function testActionSetup() {
164
  public function testActionSetup() {
154 165
    $action = rules_action('rules_node_publish_action');
155 166

  
156 167
    $s = serialize($action);
......
168 179
    $action2->executeByArgs(array('node' => $node));
169 180
    $this->assertEqual($node->status, 1, 'Action executed correctly');
170 181

  
171
    // Test calling an extended + overriden method.
182
    // Test calling an extended + overridden method.
172 183
    $this->assertEqual($action2->help(), 'custom', 'Using custom help callback.');
173 184

  
174 185
    // Inspect the cache
......
177 188
  }
178 189

  
179 190
  /**
180
   * Test executing with wrong arguments.
191
   * Tests executing with wrong arguments.
181 192
   */
182
  function testActionExecutionFails() {
193
  public function testActionExecutionFails() {
183 194
    $action = rules_action('rules_node_publish_action');
184 195
    try {
185 196
      $action->execute();
186 197
      $this->fail("Execution hasn't created an exception.");
187 198
    }
188 199
    catch (RulesEvaluationException $e) {
189
      $this->pass("RulesEvaluationException was thrown: ". $e);
200
      $this->pass("RulesEvaluationException was thrown: " . $e);
190 201
    }
191 202
  }
192 203

  
193 204
  /**
194
   * Test setting up a rule and mapping variables.
205
   * Tests setting up a rule and mapping variables.
195 206
   */
196
  function testVariableMapping() {
207
  public function testVariableMapping() {
197 208
    $rule = rule(array(
198 209
      'node' => array('type' => 'node'),
199 210
      'node_unchanged' => array('type' => 'node'),
......
215 226
  /**
216 227
   * Tests making use of class based actions.
217 228
   */
218
  function testClassBasedActions() {
229
  public function testClassBasedActions() {
219 230
    $cache = rules_get_cache();
220 231
    $this->assertTrue(!empty($cache['action_info']['rules_test_class_action']), 'Action has been discovered.');
221 232
    $action = rules_action('rules_test_class_action');
......
235 246
  /**
236 247
   * Tests CRUD functionality.
237 248
   */
238
  function testRulesCRUD() {
249
  public function testRulesCRUD() {
239 250
    $rule = $this->createTestRule();
240 251
    $rule->integrityCheck()->save('test');
241 252

  
......
296 307
  }
297 308

  
298 309
  /**
299
   * Test automatic saving of variables.
310
   * Tests automatic saving of variables.
300 311
   */
301
  function testActionSaving() {
312
  public function testActionSaving() {
302 313
    // Test saving a parameter.
303 314
    $action = rules_action('rules_node_publish_action_save');
304 315
    $node = $this->drupalCreateNode(array('status' => 0, 'type' => 'page'));
305 316
    $action->executeByArgs(array('node' => $node));
306 317

  
307 318
    $this->assertEqual($node->status, 1, 'Action executed correctly on node.');
308
    // Sync node_load cache with node_save
319
    // Sync node_load cache with node_save.
309 320
    entity_get_controller('node')->resetCache();
310 321

  
311 322
    $node = node_load($node->nid);
......
335 346
  }
336 347

  
337 348
  /**
338
   * Test adding a variable and optional parameters.
349
   * Tests adding a variable and optional parameters.
339 350
   */
340
  function testVariableAdding() {
351
  public function testVariableAdding() {
341 352
    $node = $this->drupalCreateNode();
342 353
    $rule = rule(array('nid' => array('type' => 'integer')));
343 354
    $rule->condition('rules_test_condition_true')
......
351 362
    $vars = $rule->conditions()->offsetGet(0)->availableVariables();
352 363
    $this->assertEqual(!isset($vars['node_loaded']), 'Loaded variable is not available to conditions.');
353 364

  
354

  
355 365
    // Test adding a variable with a custom variable name.
356 366
    $node = $this->drupalCreateNode();
357 367
    $rule = rule(array('nid' => array('type' => 'integer')));
......
364 374
  }
365 375

  
366 376
  /**
367
   * Test custom access for using component actions/conditions.
377
   * Tests custom access for using component actions/conditions.
368 378
   */
369
  function testRuleComponentAccess() {
379
  public function testRuleComponentAccess() {
370 380
    // Create a normal user.
371 381
    $normal_user = $this->drupalCreateUser();
372 382
    // Create a role for granting access to the rule component.
......
388 398
    user_role_change_permissions($this->normal_role, array('use Rules component rules_test_roles' => TRUE));
389 399
    $this->assertTrue(rules_action('component_rules_test_roles')->access(), 'Authenticated user with the correct role can use the rule component.');
390 400

  
391
    // Reset global user to anonyous.
401
    // Reset global user to anonymous.
392 402
    $user = user_load(0);
393 403
    $this->assertFalse(rules_action('component_rules_test_roles')->access(), 'Anonymous user can\'t use the rule component.');
394 404
  }
395 405

  
396 406
  /**
397
   * Test passing arguments by reference to an action.
407
   * Tests passing arguments by reference to an action.
398 408
   */
399
  function testPassingByReference() {
409
  public function testPassingByReference() {
400 410
    // Keeping references of variables is unsupported, though the
401 411
    // EntityMetadataArrayObject may be used to achieve that.
402 412
    $array = array('foo' => 'bar');
......
406 416
  }
407 417

  
408 418
  /**
409
   * Test sorting rule elements.
419
   * Tests sorting rule elements.
410 420
   */
411
  function testSorting() {
421
  public function testSorting() {
412 422
    $rule = $this->createTestRule();
413 423
    $conditions = $rule->conditions();
414 424
    $conditions[0]->weight = 10;
......
427 437
  /**
428 438
   * Tests using data selectors.
429 439
   */
430
  function testDataSelectors() {
440
  public function testDataSelectors() {
431 441
    $body[LANGUAGE_NONE][0] = array('value' => '<b>The body & nothing.</b>');
432 442
    $node = $this->drupalCreateNode(array('body' => $body, 'type' => 'page', 'summary' => ''));
433 443

  
......
468 478
      $this->fail("Validation hasn't created an exception.");
469 479
    }
470 480
    catch (RulesIntegrityException $e) {
471
      $this->pass("Validation error correctly detected: ". $e);
481
      $this->pass("Validation error correctly detected: " . $e);
472 482
    }
473 483

  
474 484
    // Test auto creation of nested data structures, like the node body field.
......
508 518
  /**
509 519
   * Tests making use of rule sets.
510 520
   */
511
  function testRuleSets() {
521
  public function testRuleSets() {
512 522
    $set = rules_rule_set(array(
513 523
      'node' => array('type' => 'node', 'label' => 'node'),
514 524
    ));
......
543 553
  /**
544 554
   * Tests invoking components from the action.
545 555
   */
546
  function testComponentInvocations() {
556
  public function testComponentInvocations() {
547 557
    $set = rules_rule_set(array(
548 558
      'node1' => array('type' => 'node', 'label' => 'node'),
549 559
    ));
......
591 601
    $this->assertTrue($node->status == 1, 'Component provided in code has been executed.');
592 602
  }
593 603

  
594

  
595 604
  /**
596
   * Test asserting metadata, customizing action info and make sure integrity
597
   * is checked.
605
   * Tests asserting metadata.
606
   *
607
   * Customizes action info and makes sure integrity is checked.
598 608
   */
599
  function testMetadataAssertion() {
609
  public function testMetadataAssertion() {
600 610
    $action = rules_action('rules_node_make_sticky_action');
601 611

  
602 612
    // Test failing integrity check.
......
614 624
    // Test asserting additional metadata.
615 625
    $rule = rule(array('node' => array('type' => 'node')));
616 626
    // Customize action info using the settings.
617
    $rule->condition('data_is', array('data:select'   => 'node:type', 'value' => 'page'))
627
    $rule->condition('data_is', array('data:select' => 'node:type', 'value' => 'page'))
618 628
         // Configure an condition using the body. As the body is a field,
619
         // tis requires the bundle to be correctly asserted.
629
         // this requires the bundle to be correctly asserted.
620 630
         ->condition(rules_condition('data_is', array('data:select' => 'node:body:value', 'value' => 'foo'))->negate())
621 631
         // The action also requires the page bundle in order to work.
622 632
         ->action($action);
......
627 637
    $rule->execute($node);
628 638
    $this->assertTrue($node->sticky, 'Rule with asserted metadata executed.');
629 639

  
630

  
631 640
    // Test asserting metadata on a derived property, i.e. not a variable.
632 641
    $rule = rule(array('node' => array('type' => 'node')));
633 642
    $rule->condition('entity_is_of_type', array('entity:select' => 'node:reference', 'type' => 'node'))
......
676 685
  }
677 686

  
678 687
  /**
679
   * Test using loops.
688
   * Tests using loops.
680 689
   */
681
  function testLoops() {
690
  public function testLoops() {
682 691
    // Test passing the list parameter as argument to ensure that is working
683 692
    // generally for plugin container too.
684 693
    drupal_get_messages(NULL, TRUE);
......
701 710
      'list' => array(
702 711
        'type' => 'list<node>',
703 712
        'label' => 'A list of nodes',
704
      )
713
      ),
705 714
    ));
706 715
    $loop = rules_loop(array('list:select' => 'list', 'item:var' => 'node'));
707 716
    $loop->action('data_set', array('data:select' => 'node:sticky', 'value' => TRUE));
......
717 726
  }
718 727

  
719 728
  /**
720
   * Test access checks.
729
   * Tests access checks.
721 730
   */
722
  function testAccessCheck() {
731
  public function testAccessCheck() {
723 732
    $rule = rule();
724 733
    // Try to set a property which is provided by the test module and is not
725 734
    // accessible, so the access check has to return FALSE.
......
728 737
  }
729 738

  
730 739
  /**
731
   * Test returning provided variables.
740
   * Tests returning provided variables.
732 741
   */
733
  function testReturningVariables() {
742
  public function testReturningVariables() {
734 743
    $node = $this->drupalCreateNode();
735 744
    $action = rules_action('entity_fetch', array('type' => 'node', 'id' => $node->nid));
736 745
    list($node2) = $action->execute();
......
773 782
  /**
774 783
   * Tests using input evaluators.
775 784
   */
776
  function testInputEvaluators() {
785
  public function testInputEvaluators() {
777 786
    $node = $this->drupalCreateNode(array('title' => '<b>The body & nothing.</b>', 'type' => 'page'));
778 787

  
779 788
    $rule = rule(array('nid' => array('type' => 'integer')));
......
796 805
  }
797 806

  
798 807
  /**
799
   * Test importing and exporting a rule.
808
   * Tests importing and exporting a rule.
800 809
   */
801
  function testRuleImportExport() {
810
  public function testRuleImportExport() {
802 811
    $rule = rule(array('nid' => array('type' => 'integer')));
803 812
    $rule->name = "rules_export_test";
804 813
    $rule->action('rules_action_load_node')
......
899 908
  }
900 909

  
901 910
  /**
902
   * Test the named parameter mode.
911
   * Tests the named parameter mode.
903 912
   */
904
  function testNamedParameters() {
913
  public function testNamedParameters() {
905 914
    $rule = rule(array('node' => array('type' => 'node')));
906 915
    $rule->action('rules_action_node_set_title', array('title' => 'foo'));
907 916
    $rule->integrityCheck();
......
914 923
  }
915 924

  
916 925
  /**
917
   * Make sure Rules aborts when NULL values are used.
926
   * Makes sure Rules aborts when NULL values are used.
918 927
   */
919
  function testAbortOnNULLValues() {
928
  public function testAbortOnNULLValues() {
920 929
    $rule = rule(array('node' => array('type' => 'node')));
921 930
    $rule->action('drupal_message', array('message:select' => 'node:log'));
922 931
    $rule->integrityCheck();
......
930 939
    $msg = RulesTestCase::t('The variable or parameter %message is empty.', array('message'));
931 940
    $this->assertTrue(strpos($text, $msg) !== FALSE, 'Evaluation aborted due to an empty argument value.');
932 941
  }
942

  
933 943
}
934 944

  
935 945
/**
......
937 947
 */
938 948
class RulesTestDataCase extends DrupalWebTestCase {
939 949

  
940
  static function getInfo() {
950
  /**
951
   * Declares test metadata.
952
   */
953
  public static function getInfo() {
941 954
    return array(
942 955
      'name' => 'Rules Data tests',
943 956
      'description' => 'Tests rules data saving and type matching.',
......
945 958
    );
946 959
  }
947 960

  
948
  function setUp() {
961
  /**
962
   * Overrides DrupalWebTestCase::setUp().
963
   */
964
  protected function setUp() {
949 965
    parent::setUp('rules', 'rules_test');
950
    variable_set('rules_debug_log', 1);
966
    variable_set('rules_debug_log', TRUE);
951 967
    // Make sure we don't ran over issues with the node_load static cache.
952 968
    entity_get_controller('node')->resetCache();
953 969
  }
......
955 971
  /**
956 972
   * Tests intelligently saving data.
957 973
   */
958
  function testDataSaving() {
974
  public function testDataSaving() {
959 975
    $node = $this->drupalCreateNode();
960 976
    $state = new RulesState(rule());
961 977
    $state->addVariable('node', $node, array('type' => 'node'));
......
988 1004
  }
989 1005

  
990 1006
  /**
991
   * Test type matching
1007
   * Tests type matching.
992 1008
   */
993
  function testTypeMatching() {
1009
  public function testTypeMatching() {
994 1010
    $entity = array('type' => 'entity');
995 1011
    $node = array('type' => 'node');
996 1012
    $this->assertTrue(RulesData::typesMatch($node, $entity), 'Types match.');
......
1010 1026
  /**
1011 1027
   * Tests making use of custom wrapper classes.
1012 1028
   */
1013
  function testCustomWrapperClasses() {
1029
  public function testCustomWrapperClasses() {
1014 1030
    // Test loading a vocabulary by name, which is done by a custom wrapper.
1015 1031
    $set = rules_action_set(array('vocab' => array('type' => 'taxonomy_vocabulary')), array('vocab'));
1016 1032
    $set->action('drupal_message', array('message:select' => 'vocab:name'));
......
1040 1056
  /**
1041 1057
   * Makes sure the RulesIdentifiableDataWrapper is working correctly.
1042 1058
   */
1043
  function testRulesIdentifiableDataWrapper() {
1059
  public function testRulesIdentifiableDataWrapper() {
1044 1060
    $node = $this->drupalCreateNode();
1045 1061
    $wrapper = new RulesTestTypeWrapper('rules_test_type', $node);
1046 1062
    $this->assertTrue($wrapper->value() == $node, 'Data correctly wrapped.');
......
1062 1078
      $this->fail("Loading hasn't created an exception.");
1063 1079
    }
1064 1080
    catch (EntityMetadataWrapperException $e) {
1065
      $this->pass("Exception was thrown: ". $e->getMessage());
1081
      $this->pass("Exception was thrown: " . $e->getMessage());
1066 1082
    }
1067 1083

  
1068 1084
    // Test saving a savable custom, identifiable wrapper.
......
1075 1091
    $node = node_load($node->nid, NULL, TRUE);
1076 1092
    $this->assertEqual($node->status, 1, 'Savable non-entity has been saved.');
1077 1093
  }
1094

  
1078 1095
}
1079 1096

  
1080 1097
/**
......
1082 1099
 */
1083 1100
class RulesTriggerTestCase extends DrupalWebTestCase {
1084 1101

  
1085
  static function getInfo() {
1102
  /**
1103
   * Declares test metadata.
1104
   */
1105
  public static function getInfo() {
1086 1106
    return array(
1087 1107
      'name' => 'Reaction Rules',
1088 1108
      'description' => 'Tests triggering reactive rules.',
......
1090 1110
    );
1091 1111
  }
1092 1112

  
1093
  function setUp() {
1113
  /**
1114
   * Overrides DrupalWebTestCase::setUp().
1115
   */
1116
  protected function setUp() {
1094 1117
    parent::setUp('rules', 'rules_test');
1095 1118
    RulesLog::logger()->clear();
1096
    variable_set('rules_debug_log', 1);
1119
    variable_set('rules_debug_log', TRUE);
1097 1120
  }
1098 1121

  
1122
  /**
1123
   * Helper function to create a test Rule.
1124
   */
1099 1125
  protected function createTestRule($action = TRUE, $event = 'node_presave') {
1100 1126
    $rule = rules_reaction_rule();
1101 1127
    $rule->event($event)
......
1110 1136
  /**
1111 1137
   * Tests CRUD for reaction rules - making sure the events are stored properly.
1112 1138
   */
1113
  function testReactiveRuleCreation() {
1139
  public function testReactiveRuleCreation() {
1114 1140
    $rule = $this->createTestRule();
1115 1141
    $rule->save();
1116 1142
    $result = db_query("SELECT event FROM {rules_trigger} WHERE id = :id", array(':id' => $rule->id));
......
1132 1158
  /**
1133 1159
   * Tests creating and triggering a basic reaction rule.
1134 1160
   */
1135
  function testBasicReactionRule() {
1161
  public function testBasicReactionRule() {
1136 1162
    $node = $this->drupalCreateNode(array('type' => 'page'));
1137 1163
    $rule = $this->createTestRule();
1138 1164
    $rule->integrityCheck()->save();
......
1147 1173

  
1148 1174
    RulesLog::logger()->checkLog();
1149 1175
    $this->assertFalse(node_load($nid), 'Rule successfully triggered and executed');
1150
    //debug(RulesLog::logger()->render());
1176
    // debug(RulesLog::logger()->render());
1151 1177
  }
1152 1178

  
1153 1179
  /**
1154
   * Test a rule using a handler to load a variable.
1180
   * Tests a rule using a handler to load a variable.
1155 1181
   */
1156
  function testVariableHandler() {
1182
  public function testVariableHandler() {
1157 1183
    $node = $this->drupalCreateNode(array('type' => 'page', 'sticky' => 0, 'status' => 0));
1158 1184
    $rule = $this->createTestRule(FALSE, 'node_update');
1159 1185
    $rule->action('rules_node_publish_action_save', array('node:select' => 'node_unchanged'));
......
1174 1200
    $this->assertFalse($node->sticky, 'Parameter has been loaded and saved.');
1175 1201
    $this->assertTrue($node->status, 'Action has been executed.');
1176 1202

  
1177
    // Ensure the rule was evaluated a second time
1203
    // Ensure the rule was evaluated a second time.
1178 1204
    $text = RulesLog::logger()->render();
1179 1205
    $msg = RulesTestCase::t('Evaluating conditions of rule %rule 1', array('rule 1'));
1180 1206
    $pos = strpos($text, $msg);
1181 1207
    $pos = ($pos !== FALSE) ? strpos($text, $msg, $pos) : FALSE;
1182 1208
    $this->assertTrue($pos !== FALSE, "Recursion prevented.");
1183
    //debug(RulesLog::logger()->render());
1209
    // debug(RulesLog::logger()->render());
1184 1210
  }
1185 1211

  
1186 1212
  /**
1187
   * Test aborting silently when handlers are not able to load.
1213
   * Tests aborting silently when handlers are not able to load.
1188 1214
   */
1189
  function testVariableHandlerFailing() {
1215
  public function testVariableHandlerFailing() {
1190 1216
    $rule = $this->createTestRule(FALSE, 'node_presave');
1191 1217
    $rule->action('rules_node_publish_action_save', array('node:select' => 'node_unchanged'));
1192 1218
    $rule->integrityCheck()->save();
......
1194 1220
    // On insert it's not possible to get the unchanged node during presave.
1195 1221
    $node = $this->drupalCreateNode(array('type' => 'page', 'sticky' => 0, 'status' => 0));
1196 1222

  
1197
    //debug(RulesLog::logger()->render());
1223
    // debug(RulesLog::logger()->render());
1198 1224
    $text = RulesTestCase::t('Unable to load variable %node_unchanged, aborting.', array('node_unchanged'));
1199 1225
    $this->assertTrue(strpos(RulesLog::logger()->render(), $text) !== FALSE, "Aborted evaluation.");
1200 1226
  }
1201 1227

  
1202 1228
  /**
1203
   * Tests preventing recursive rule invocations by creating a rule that reacts
1204
   * on node-update and generates a node update that would trigger it itself.
1229
   * Tests preventing recursive rule invocations.
1230
   *
1231
   * Creates a rule that reacts on node-update then generates a node update
1232
   * that would trigger it itself.
1205 1233
   */
1206
  function testRecursionPrevention() {
1234
  public function testRecursionPrevention() {
1207 1235
    $rule = $this->createTestRule(FALSE, 'node_update');
1208 1236
    $rule->action('rules_node_make_sticky_action');
1209 1237
    $rule->integrityCheck()->save();
......
1213 1241
    node_save($node);
1214 1242

  
1215 1243
    $text = RulesTestCase::t('Not evaluating reaction rule %label to prevent recursion.', array('label' => $rule->name));
1216
    //debug(RulesLog::logger()->render());
1244
    // debug(RulesLog::logger()->render());
1217 1245
    $this->assertTrue((strpos(RulesLog::logger()->render(), $text) !== FALSE), "Recursion prevented.");
1218
    //debug(RulesLog::logger()->render());
1246
    // debug(RulesLog::logger()->render());
1219 1247
  }
1220 1248

  
1221 1249
  /**
1222
   * Ensure the recursion prevention still allows to let the rule trigger again
1250
   * Tests recursion prevention with altered arguments.
1251
   *
1252
   * Ensure the recursion prevention still allows the rule to trigger again
1223 1253
   * during evaluation of the same event set, if the event isn't caused by the
1224
   * rule itself - thus we won't run in an infinte loop.
1254
   * rule itself - thus we won't run in an infinite loop.
1225 1255
   */
1226
  function testRecursionOnDifferentArguments() {
1256
  public function testRecursionOnDifferentArguments() {
1227 1257
    // Create rule1 - which might recurse.
1228 1258
    $rule = $this->createTestRule(FALSE, 'node_update');
1229 1259
    $rule->action('rules_node_make_sticky_action');
......
1242 1272
    $node = $this->drupalCreateNode(array('type' => 'page', 'sticky' => 0, 'status' => 0));
1243 1273
    node_save($node);
1244 1274

  
1245
    //debug(RulesLog::logger()->render());
1275
    // debug(RulesLog::logger()->render());
1246 1276
    $text = RulesLog::logger()->render();
1247 1277
    $pos = strpos($text, RulesTestCase::t('Evaluating conditions of rule %rule 1', array('rule 1')));
1248 1278
    $pos = ($pos !== FALSE) ? strpos($text, RulesTestCase::t('Evaluating conditions of rule %rule 2', array('rule 2')), $pos) : FALSE;
......
1255 1285
  /**
1256 1286
   * Tests the provided default rule 'rules_test_default_1'.
1257 1287
   */
1258
  function testDefaultRule() {
1288
  public function testDefaultRule() {
1259 1289
    $rule = rules_config_load('rules_test_default_1');
1260 1290
    $this->assertTrue($rule->status & ENTITY_IN_CODE && !($rule->status & ENTITY_IN_DB), 'Default rule can be loaded and has the right status.');
1291
    $this->assertTrue($rule->tags == array('Admin', 'Tag2'), 'Default rule has correct tags.');
1261 1292
    // Enable.
1262 1293
    $rule->active = TRUE;
1263 1294
    $rule->save();
......
1276 1307
  /**
1277 1308
   * Tests creating and triggering a reaction rule with event settings.
1278 1309
   */
1279
  function testEventSettings() {
1310
  public function testEventSettings() {
1280 1311
    $rule = rules_reaction_rule();
1281 1312
    $rule->event('node_presave', array('bundle' => 'article'))
1282 1313
      ->condition('data_is_empty', array('data:select' => 'node:field-tags'))
......
1299 1330
      $this->pass('Integrity check failed: ' . $e);
1300 1331
    }
1301 1332
  }
1333

  
1302 1334
}
1303 1335

  
1304 1336
/**
......
1306 1338
 */
1307 1339
class RulesIntegrationTestCase extends DrupalWebTestCase {
1308 1340

  
1309
  static function getInfo() {
1341
  /**
1342
   * Declares test metadata.
1343
   */
1344
  public static function getInfo() {
1310 1345
    return array(
1311 1346
      'name' => 'Rules Core Integration',
1312 1347
      'description' => 'Tests provided integration for drupal core.',
......
1314 1349
    );
1315 1350
  }
1316 1351

  
1317
  function setUp() {
1352
  /**
1353
   * Overrides DrupalWebTestCase::setUp().
1354
   */
1355
  protected function setUp() {
1318 1356
    parent::setUp('rules', 'rules_test', 'php', 'path');
1319 1357
    RulesLog::logger()->clear();
1320
    variable_set('rules_debug_log', 1);
1358
    variable_set('rules_debug_log', TRUE);
1321 1359
  }
1322 1360

  
1323 1361
  /**
1324
   * Just make sure the access callback run without errors.
1362
   * Just makes sure the access callback run without errors.
1325 1363
   */
1326
  function testAccessCallbacks() {
1364
  public function testAccessCallbacks() {
1327 1365
    $cache = rules_get_cache();
1328 1366
    foreach (array('action', 'condition', 'event') as $type) {
1329 1367
      foreach (rules_fetch_data($type . '_info') as $name => $info) {
......
1335 1373
  }
1336 1374

  
1337 1375
  /**
1338
   * Test data integration.
1376
   * Tests data integration.
1339 1377
   */
1340
  function testDataIntegration() {
1378
  public function testDataIntegration() {
1341 1379
    // Test data_create action.
1342 1380
    $action = rules_action('data_create', array(
1343 1381
      'type' => 'log_entry',
......
1353 1391
    $pos = strpos($text, RulesTestCase::t('Added the provided variable %data_created of type %log_entry', array('data_created', 'log_entry')));
1354 1392
    $this->assertTrue($pos !== FALSE, 'Data of type log entry has been created.');
1355 1393

  
1356

  
1357 1394
    // Test variable_add action.
1358 1395
    $action = rules_action('variable_add', array(
1359 1396
      'type' => 'text_formatted',
1360 1397
      'value' => array(
1361 1398
        'value' => 'test text',
1362 1399
        'format' => 1,
1363
      )
1400
      ),
1364 1401
    ));
1365 1402
    $action->access();
1366 1403
    $action->execute();
......
1368 1405
    $pos = strpos($text, RulesTestCase::t('Added the provided variable %variable_added of type %text_formatted', array('variable_added', 'text_formatted')));
1369 1406
    $this->assertTrue($pos !== FALSE, 'Data of type text formatted has been created.');
1370 1407

  
1371

  
1372 1408
    // Test using the list actions.
1373 1409
    $rule = rule(array(
1374 1410
      'list' => array(
1375 1411
        'type' => 'list<text>',
1376 1412
        'label' => 'A list of text',
1377
      )
1413
      ),
1378 1414
    ));
1379 1415
    $rule->action('list_add', array('list:select' => 'list', 'item' => 'bar2'));
1380 1416
    $rule->action('list_add', array('list:select' => 'list', 'item' => 'bar', 'pos' => 'start'));
......
1386 1422
    $this->assertEqual($list->value(), array('bar', 'foo', 'foo2'), 'List items removed and added.');
1387 1423
    $this->assertFalse(rules_condition('list_contains')->execute($list, 'foo-bar'), 'Condition "List item contains" evaluates to FALSE');
1388 1424
    $this->assertTrue(rules_condition('list_contains')->execute($list, 'foo'), 'Condition "List item contains" evaluates to TRUE');
1389
    //debug(RulesLog::logger()->render());
1425
    // debug(RulesLog::logger()->render());
1390 1426

  
1391 1427
    // Test data_is condition with IN operation.
1392 1428
    $rule = rule(array('node' => array('type' => 'node')));
......
1396 1432

  
1397 1433
    $node = $this->drupalCreateNode(array('title' => 'foo'));
1398 1434
    $rule->execute($node);
1399
    $this->assertEqual($node->title, 'bar', "Data comparision using IN operation evaluates to TRUE.");
1400

  
1435
    $this->assertEqual($node->title, 'bar', "Data comparison using IN operation evaluates to TRUE.");
1401 1436

  
1402 1437
    // Test Condition: Data is empty.
1403 1438
    $rule = rule(array('node' => array('type' => 'node')));
......
1468 1503
    $set->action('data_convert', array('type' => 'integer', 'value:select' => 'source', 'rounding_behavior' => 'down'));
1469 1504
    $set->action('data_set', array('data:select' => 'result', 'value:select' => 'conversion_result'));
1470 1505
    list($result) = $set->execute('9.6');
1471
    $this->assertEqual($result, 9, 'Converted decimal to integer using roundin behavio down.');
1506
    $this->assertEqual($result, 9, 'Converted decimal to integer using rounding behavior down.');
1472 1507

  
1473 1508
    $set = rules_action_set(array(
1474 1509
      'result' => array('type' => 'integer', 'parameter' => FALSE),
......
1500 1535
  /**
1501 1536
   * Tests entity related integration.
1502 1537
   */
1503
  function testEntityIntegration() {
1538
  public function testEntityIntegration() {
1504 1539
    global $user;
1505 1540

  
1506 1541
    $page = $this->drupalCreateNode(array('type' => 'page'));
......
1510 1545
      ->execute(entity_metadata_wrapper('node', $article), 'field_tags');
1511 1546
    $this->assertTrue($result);
1512 1547

  
1513
    // Test entiy_is_of_bundle condition.
1548
    // Test entity_is_of_bundle condition.
1514 1549
    $result = rules_condition('entity_is_of_bundle', array(
1515 1550
      'type' => 'node',
1516 1551
      'bundle' => array('article'),
......
1582 1617
    $pos = ($pos !== FALSE) ? strpos($text, RulesTestCase::t('Saved %node of type %node.', array('node')), $pos) : FALSE;
1583 1618
    $pos = ($pos !== FALSE) ? strpos($text, RulesTestCase::t('Evaluating the action %entity_delete.', array('entity_delete')), $pos) : FALSE;
1584 1619
    $this->assertTrue($pos !== FALSE, 'Data has been fetched, saved and deleted.');
1585
    //debug(RulesLog::logger()->render());
1586

  
1587

  
1620
    // debug(RulesLog::logger()->render());
1588 1621

  
1589 1622
    $node = entity_property_values_create_entity('node', array(
1590 1623
      'type' => 'article',
......
1621 1654

  
1622 1655
    $this->assertEqual(entity_metadata_wrapper('node', $node->nid)->title->value(), 'bar', 'Entity is of type condition correctly asserts the entity type.');
1623 1656

  
1624

  
1625 1657
    // Test the entity_query action.
1626 1658
    $node = $this->drupalCreateNode(array('type' => 'page', 'title' => 'foo2'));
1627 1659
    $rule = rule();
......
1637 1669
  }
1638 1670

  
1639 1671
  /**
1640
   * Test integration for the taxonomy module.
1672
   * Tests integration for the taxonomy module.
1641 1673
   */
1642
  function testTaxonomyIntegration() {
1674
  public function testTaxonomyIntegration() {
1643 1675
    $term = entity_property_values_create_entity('taxonomy_term', array(
1644 1676
      'name' => $this->randomName(),
1645 1677
      'vocabulary' => 1,
......
1733 1765
  }
1734 1766

  
1735 1767
  /**
1736
   * Test integration for the node module.
1768
   * Tests integration for the node module.
1737 1769
   */
1738
  function testNodeIntegration() {
1770
  public function testNodeIntegration() {
1739 1771
    $tests = array(
1740 1772
      array('node_unpublish', 'node_is_published', 'node_publish', 'status'),
1741 1773
      array('node_make_unsticky', 'node_is_sticky', 'node_make_sticky', 'sticky'),
......
1748 1780
      rules_action($action1)->execute($node);
1749 1781

  
1750 1782
      $node = node_load($node->nid, NULL, TRUE);
1751
      $this->assertFalse($node->$property, 'Action has permanently disabled node '. $property);
1783
      $this->assertFalse($node->$property, 'Action has permanently disabled node ' . $property);
1752 1784
      $return = rules_condition($condition)->execute($node);
1753
      $this->assertFalse($return, 'Condition determines node '. $property . ' is disabled.');
1785
      $this->assertFalse($return, 'Condition determines node ' . $property . ' is disabled.');
1754 1786

  
1755 1787
      rules_action($action2)->execute($node);
1756 1788
      $node = node_load($node->nid, NULL, TRUE);
1757
      $this->assertTrue($node->$property, 'Action has permanently enabled node '. $property);
1789
      $this->assertTrue($node->$property, 'Action has permanently enabled node ' . $property);
1758 1790
      $return = rules_condition($condition)->execute($node);
1759
      $this->assertTrue($return, 'Condition determines node '. $property . ' is enabled.');
1791
      $this->assertTrue($return, 'Condition determines node ' . $property . ' is enabled.');
1760 1792
    }
1761 1793

  
1762 1794
    $return = rules_condition('node_is_of_type', array('type' => array('page', 'article')))->execute($node);
......
1764 1796
    $return = rules_condition('node_is_of_type', array('type' => array('article')))->execute($node);
1765 1797
    $this->assertFalse($return, 'Condition determines node is not of type article.');
1766 1798

  
1767

  
1768 1799
    // Test auto saving of a new node after it has been inserted into the DB.
1769 1800
    $rule = rules_reaction_rule();
1770 1801
    $rand = $this->randomName();
......
1778 1809
  }
1779 1810

  
1780 1811
  /**
1781
   * Test integration for the user module.
1812
   * Tests integration for the user module.
1782 1813
   */
1783
  function testUserIntegration() {
1814
  public function testUserIntegration() {
1784 1815
    $rid = $this->drupalCreateRole(array('administer nodes'), 'foo');
1785 1816
    $user = $this->drupalCreateUser();
1786 1817

  
......
1827 1858
  }
1828 1859

  
1829 1860
  /**
1830
   * Test integration for the php module.
1861
   * Tests integration for the php module.
1831 1862
   */
1832
  function testPHPIntegration() {
1863
  public function testPHPIntegration() {
1833 1864
    $node = $this->drupalCreateNode(array('title' => 'foo'));
1834 1865
    $rule = rule(array('var_name' => array('type' => 'node')));
1835 1866
    $rule->condition('php_eval', array('code' => 'return TRUE;'))
......
1843 1874
    $this->assertEqual(array_pop($msg['status']), "Title: foo Token: foo", 'PHP input evaluation has been applied.');
1844 1875
    $this->assertEqual(array_pop($msg['status']), "Executed-foo", 'PHP code condition and action have been evaluated.');
1845 1876

  
1846
    // Test PHP data processor
1877
    // Test PHP data processor.
1847 1878
    $rule = rule(array('var_name' => array('type' => 'node')));
1848 1879
    $rule->action('drupal_message', array(
1849 1880
      'message:select' => 'var_name:title',
1850 1881
      'message:process' => array(
1851
        'php' => array('code' => 'return "Title: $value";')
1882
        'php' => array('code' => 'return "Title: $value";'),
1852 1883
      ),
1853 1884
    ));
1854 1885
    $rule->execute($node);
......
1859 1890
  }
1860 1891

  
1861 1892
  /**
1862
   * Test the "rules_core" integration.
1893
   * Tests the "rules_core" integration.
1863 1894
   */
1864
  function testRulesCoreIntegration() {
1895
  public function testRulesCoreIntegration() {
1865 1896
    // Make sure the date input evaluator evaluates properly using strtotime().
1866 1897
    $node = $this->drupalCreateNode(array('title' => 'foo'));
1867 1898
    $rule = rule(array('node' => array('type' => 'node')));
......
1930 1961
  }
1931 1962

  
1932 1963
  /**
1933
   * Test site/system integration.
1964
   * Tests site/system integration.
1934 1965
   */
1935
  function testSystemIntegration() {
1966
  public function testSystemIntegration() {
1936 1967
    // Test using the 'site' variable.
1937 1968
    $condition = rules_condition('data_is', array('data:select' => 'site:current-user:name', 'value' => $GLOBALS['user']->name));
1938 1969
    $this->assertTrue($condition->execute(), 'Retrieved the current user\'s name.');
......
1969 2000
    $this->drupalGet('node/' . $node->nid);
1970 2001
    $this->assertEqual($this->getUrl(), url('user', array('absolute' => TRUE, 'fragment' => 'fragment')), 'Redirect has been issued.');
1971 2002

  
1972

  
1973 2003
    // Test sending mail.
1974 2004
    $settings = array('to' => 'mail@example.com', 'subject' => 'subject', 'message' => 'hello.');
1975 2005
    rules_action('mail', $settings)->execute();
......
1979 2009
    rules_action('mail', $settings + array('from' => 'sender@example.com'))->execute();
1980 2010
    $this->assertMail('from', 'sender@example.com', 'Specified from address has been used');
1981 2011

  
1982
    // Test sending mail to all users of a role. First make sure there is a
1983
    // custom role and a user for it.
1984
    $user = $this->drupalCreateUser(array('administer nodes'));
1985
    $roles = $user->roles;
1986
    // Remove the authenticate role so we only use the new role created by
2012
    // Test sending mail to all users of a role. First clear the mail
2013
    // collector to remove the mail sent in the previous line of code.
2014
    variable_set('drupal_test_email_collector', array());
2015

  
2016
    // Now make sure there is a custom role and two users with that role.
2017
    $user1 = $this->drupalCreateUser(array('administer nodes'));
2018
    $roles = $user1->roles;
2019
    // Remove the authenticated role so we only use the new role created by
1987 2020
    // drupalCreateUser().
1988 2021
    unset($roles[DRUPAL_AUTHENTICATED_RID]);
2022

  
2023
    // Now create a second user with the same role.
2024
    $user2 = $this->drupalCreateUser();
2025
    user_save($user2, array('roles' => $roles));
2026

  
2027
    // Now create a third user without the same role - this user should NOT
2028
    // receive the role email.
2029
    $user3 = $this->drupalCreateUser(array('administer blocks'));
2030
    $additional_roles = $user3->roles;
2031
    unset($additional_roles[DRUPAL_AUTHENTICATED_RID]);
2032

  
2033
    // Execute action and check that only two mails were sent.
1989 2034
    rules_action('mail_to_users_of_role', $settings + array('roles' => array_keys($roles)))->execute();
1990
    $this->assertMail('to', $user->mail, 'Mail to users of a role has been sent.');
2035
    $mails = $this->drupalGetMails();
2036
    $this->assertEqual(count($mails), 2, '2 e-mails were sent to users of a role.');
2037

  
2038
    // Check each mail to ensure that only $user1 and $user2 got the mail.
2039
    $mail = array_pop($mails);
2040
    $this->assertTrue($mail['to'] == $user2->mail, 'Mail to user of a role has been sent.');
2041
    $mail = array_pop($mails);
2042
    $this->assertTrue($mail['to'] == $user1->mail, 'Mail to user of a role has been sent.');
2043

  
2044
    // Execute action again, this time to send mail to both roles.
2045
    // This time check that three mails were sent - one for each user..
2046
    variable_set('drupal_test_email_collector', array());
2047
    rules_action('mail_to_users_of_role', $settings + array('roles' => array_keys($roles + $additional_roles)))->execute();
2048
    $mails = $this->drupalGetMails();
2049
    $this->assertEqual(count($mails), 3, '3 e-mails were sent to users of multiple roles.');
1991 2050

  
1992 2051
    // Test reacting on new log entries and make sure the log entry is usable.
1993 2052
    $rule = rules_reaction_rule();
......
2003 2062
  /**
2004 2063
   * Tests the path module integration.
2005 2064
   */
2006
  function testPathIntegration() {
2065
  public function testPathIntegration() {
2007 2066
    rules_action('path_alias')->execute('foo', 'bar');
2008 2067
    $path = path_load('foo');
2009 2068
    $this->assertTrue($path['alias'] == 'bar', 'URL alias has been created.');
......
2031 2090

  
2032 2091
    RulesLog::logger()->checkLog();
2033 2092
  }
2093

  
2034 2094
}
2035 2095

  
2036 2096
/**
2037
 * Test event dispatcher functionality.
2097
 * Tests event dispatcher functionality.
2038 2098
 */
2039 2099
class RulesEventDispatcherTestCase extends DrupalWebTestCase {
2040 2100

  
2041
  static function getInfo() {
2101
  /**
2102
   * Declares test metadata.
2103
   */
2104
  public static function getInfo() {
2042 2105
    return array(
2043 2106
      'name' => 'Rules event dispatchers',
2044 2107
      'description' => 'Tests event dispatcher functionality.',
......
2046 2109
    );
2047 2110
  }
2048 2111

  
2049
  function setUp() {
2112
  /**
2113
   * Overrides DrupalWebTestCase::setUp().
2114
   */
2115
  protected function setUp() {
2050 2116
    parent::setUp('rules', 'rules_test');
2051 2117
  }
2052 2118

  
......
2098 2164
      $this->assertEqual($key !== 5, $handler->isWatching());
2099 2165
    }
2100 2166
  }
2167

  
2101 2168
}
2102 2169

  
2103 2170
/**
......
2106 2173
class RulesInvocationEnabledTestCase extends DrupalWebTestCase {
2107 2174

  
2108 2175
  /**
2109
   * {@inheritdoc}
2176
   * Declares test metadata.
2110 2177
   */
2111 2178
  public static function getInfo() {
2112 2179
    return array(
......
2117 2184
  }
2118 2185

  
2119 2186
  /**
2120
   * {@inheritdoc}
2187
   * Overrides DrupalWebTestCase::setUp().
2121 2188
   */
2122
  public function setUp() {
2189
  protected function setUp() {
2123 2190
    parent::setUp('dblog', 'rules', 'rules_test', 'rules_test_invocation');
2124 2191
  }
2125 2192

  

Formats disponibles : Unified diff