You are here

class RulesConditionalSwitchTestCase in Conditional Rules 8

Same name and namespace in other branches
  1. 7 tests/rules_conditional.test \RulesConditionalSwitchTestCase

Switch tests.

Hierarchy

Expanded class hierarchy of RulesConditionalSwitchTestCase

File

tests/rules_conditional.test, line 391
SimpleTest testing suites.

View source
class RulesConditionalSwitchTestCase extends RulesConditionalBaseTestCase {

  /**
   * Returns test info.
   */
  public static function getInfo() {
    return array(
      'name' => 'Switch',
      'description' => 'Test the switch plugins.',
      'group' => 'Conditional Rules',
    );
  }

  /**
   * Sets up test case.
   */
  protected function setUp() {
    parent::setUp('rules_conditional_test');
  }

  /**
   * Tests evaluation.
   */
  public function testEvaluate() {
    $comp = $this
      ->createTestComponent();
    $this
      ->assert($this
      ->doTestEvaluate($comp), 'Switch correctly evaluates.');
  }

  /**
   * Tests exporting.
   */
  public function testExport() {
    $comp = $this
      ->createTestComponent();
    $comp->name = 'switch_test';
    $export = $this
      ->getFileContents('switch_test_export.txt');
    $this
      ->assertEqual($export, $comp
      ->export(), 'Switch is correctly exported.');
  }

  /**
   * Tests importing.
   */
  public function testImport() {
    $export = $this
      ->getFileContents('switch_test_export.txt');
    $comp = entity_import('rules_config', $export);
    $this
      ->assert($this
      ->doTestEvaluate($comp), 'Imported switch correctly evaluates.');
  }

  /**
   * Creates an action set to test a conditional.
   */
  protected function createTestComponent() {
    return rules_action_set(array(
      'node' => array(
        'type' => 'node',
        'label' => 'Node',
      ),
    ))
      ->action(rules_conditional_switch('node:title')
      ->case(array(
      'value' => 'case 1',
    ))
      ->action('rules_conditional_test_throw', array(
      'message' => 'case',
    ))
      ->case(array(
      'value' => 'case 2',
    ), TRUE)
      ->action('data_set', array(
      'data:select' => 'node:title',
      'value' => 'fall through',
    ))
      ->case(array(
      'value' => 'case 3',
    ))
      ->action('rules_conditional_test_throw', array(
      'message' => 'case 3',
    ))
      ->defaultCase()
      ->action('rules_conditional_test_throw', array(
      'message' => 'default',
    )));
  }

  /**
   * Tests evaluating a conditional.
   */
  public function doTestEvaluate($comp) {
    $node = $this
      ->drupalCreateNode();
    $result = TRUE;

    // Test basic "case".
    $node->title = 'case 1';
    $result = $this
      ->assertExecution('case', $comp, array(
      $node,
    )) && $result;

    // Test fall-through "case".
    $node->title = 'case 2';
    $result = $this
      ->assertExecution('case 3', $comp, array(
      $node,
    )) && $result;
    $result = $this
      ->assertEqual('fall through', $node->title) && $result;

    // Test "default case".
    $node->title = 'anything';
    $result = $this
      ->assertExecution('default', $comp, array(
      $node,
    )) && $result;
    return $result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
RulesConditionalBaseTestCase::assertExecution protected function Asserts a test message from the execution of a rule configuration.
RulesConditionalBaseTestCase::getFileContents protected function Retrieves a file in the test directory.
RulesConditionalSwitchTestCase::createTestComponent protected function Creates an action set to test a conditional.
RulesConditionalSwitchTestCase::doTestEvaluate public function Tests evaluating a conditional.
RulesConditionalSwitchTestCase::getInfo public static function Returns test info.
RulesConditionalSwitchTestCase::setUp protected function Sets up test case.
RulesConditionalSwitchTestCase::testEvaluate public function Tests evaluation.
RulesConditionalSwitchTestCase::testExport public function Tests exporting.
RulesConditionalSwitchTestCase::testImport public function Tests importing.