<?php
// $Id: rules_test.test,v 1.1.2.8 2009/07/31 10:45:44 fago Exp $

/**
 * @file Rules Testing Module - File containing test cases.
 */

class RulesTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => t('Rules engine API tests'),
      'description' => t('Tests the rules engine API to work correctly.'),
      'group' => t('Rules engine tests'),
    );
  }

  function setUp() {
    parent::setUp();
    drupal_install_modules(array('rules', 'rules_test'));
    rules_test_keep_rules_log();
  }
  function tearDown() {
    parent::tearDown();
    rules_test_done();
  }

  //Test changing an argument when taken by reference and verify that the changes are kept
  function test_1() {
    $argument = array('property_xy' => TRUE);
    rules_invoke_rule_set('rules_test_1', $argument);
    $error = FALSE;
    $log = rules_test_show_log($error);
    $this->assertFalse($error, t('Rule was not evaluated successfully.') .' '. $log);
  }

  //Test changing an argument by action and verify that the changes are kept for later rules
  function test_2() {
    $argument = array('property_xy' => TRUE);
    rules_invoke_rule_set('rules_test_2', $argument);
    $error = FALSE;
    $log = rules_test_show_log($error);

    $this->assertFalse($error, t('Rule was not evaluated successfully.') .' '. $log);
  }

  //Test passing arguments by reference to a set
  function test_3() {
    $argument = array('property_xy' => TRUE);
    rules_invoke_rule_set('rules_test_2', array('rules_test' => &$argument));
    $error = FALSE;
    $log = rules_test_show_log($error);

    $this->assertTrue(isset($argument['altered']) && $argument['altered'], t('Argument has not been altered correctly.'));
    $this->assertFalse($error, t('Rule was not evaluated successfully.') .' '. $log);
  }

  //Tests argument mapping and argument loading by handler
  function test_4() {
    $argument = array('property_xy' => TRUE);
    rules_invoke_rule_set('rules_test_4', $argument);
    $error = FALSE;
    $log = rules_test_show_log($error);

    $this->assertFalse($error, t('Rule was not evaluated successfully.') .' '. $log);
    $this->assertTrue(strpos($log, 'Loaded variable "node"') !== FALSE, t('Variable was not loaded successfully.') .' '. $log);
  }

  //Tests adding a new variable
  function test_5() {
    rules_invoke_rule_set('rules_test_5');
    $error = FALSE;
    $log = rules_test_show_log($error);
    $this->assertFalse($error, t('Rule was not evaluated successfully.') .' '. $log);
    $this->assertTrue(strpos($log, 'Successfully added the new variable "test"') !== FALSE, t('Variable was not added successfully.') .' '. $log);
  }
}

/**
 * Argument handler for argument 2 of rule set 4
 */
function rules_test_4_load_data($arg1) {
  if (isset($arg1) && $arg1['property_xy']) {
    return  array('property_xy' => TRUE, 'altered' => TRUE);
  }
}
