class RulesConditionalFrameworkTestCase in Conditional Rules 8
Same name and namespace in other branches
- 7 tests/rules_conditional.test \RulesConditionalFrameworkTestCase
Framework tests.
Hierarchy
- class \RulesConditionalBaseTestCase extends \DrupalWebTestCase
Expanded class hierarchy of RulesConditionalFrameworkTestCase
File
- tests/
rules_conditional.test, line 44 - SimpleTest testing suites.
View source
class RulesConditionalFrameworkTestCase extends RulesConditionalBaseTestCase {
/**
* Returns test info.
*/
public static function getInfo() {
return array(
'name' => 'Framework',
'description' => 'Test the core conditional plugin framework.',
'group' => 'Conditional Rules',
);
}
/**
* Sets up test case.
*/
protected function setUp() {
parent::setUp('rules_conditional_test');
}
/**
* Tests plugin label.
*/
public function testPluginLabel() {
$container = new RulesConditionalTestStubContainer();
$label = $container
->label();
$this
->assertEqual('Stub conditional', $label, 'Default conditional container label uses the plugin label.');
$branch = new RulesConditionalTestStubElement();
$label = $branch
->label();
$this
->assertEqual('Stub conditional element', $label, 'Default conditional element label uses the plugin label.');
}
/**
* Tests intercepting a method.
*/
public function testInterceptMethod() {
$message = 'Test magic method is intercepted.';
try {
$container = new RulesConditionalTestStubContainer();
$container
->test();
$this
->fail($message);
} catch (Exception $ex) {
$this
->assertEqual('intercept', $ex
->getMessage(), $message);
}
}
/**
* Tests fluent interface.
*/
public function testFluentInterface() {
$container = new RulesConditionalTestStubContainer();
$actions1 = new RulesActionSet();
$container
->fluent($actions1)
->action('drupal_message', array(
'message' => '[site:name]',
));
$this
->assertEqual(1, count($actions1
->elements()), 'Fluent interface adds action to the active element.');
$actions2 = new RulesActionSet();
$container
->fluent($actions2)
->action('drupal_message', array(
'message' => '[site:name]',
));
$this
->assertEqual(1, count($actions1
->elements()), 'Fluent interface does not add action to a previously active element.');
}
/**
* Tests branch sibling methods.
*/
public function testBranchSibling() {
// Set up stub objects.
$container = new RulesConditionalTestStubContainer();
$branch1 = new RulesConditionalTestStubElement();
$branch1
->setParent($container);
$branch2 = new RulesConditionalTestStubElement();
$branch2
->setParent($container);
$orphanBranch = new RulesConditionalTestStubElement();
// Test obtaining siblings.
$this
->assertIdentical($branch2, $branch1
->getNextSibling(), 'Next sibling branch can be obtained.');
$this
->assertIdentical($branch1, $branch2
->getPreviousSibling(), 'Previous sibling branch can be obtained.');
$this
->assertNull($branch1
->getPreviousSibling(), 'First branch has no previous sibling.');
$this
->assertNull($branch2
->getNextSibling(), 'Last branch has no next sibling.');
// Test obtaining siblings from an orphan element.
$this
->assertNull($orphanBranch
->getNextSibling(), 'Orphan branch has no next sibling.');
$this
->assertNull($orphanBranch
->getPreviousSibling(), 'Orphan branch has no previous sibling.');
}
/**
* Tests integrity check.
*/
public function testIntegrityCheck() {
$container = new RulesConditionalTestStubContainer();
$message = 'Dependent element does not validate without a required preceding element.';
$dependent = new RulesConditionalTestStubDependentElement();
$dependent
->setParent($container);
try {
$dependent
->integrityCheck();
$this
->fail($message);
} catch (RulesIntegrityException $ex) {
$element = new RulesConditionalTestStubElement();
$element->weight = -1;
$element
->setParent($container);
$container
->sortChildren();
$dependent
->integrityCheck();
$this
->pass($message);
}
$message = 'A single element does not validate if it occurs more than once in a conditional container.';
$single = new RulesConditionalTestStubSingleElement();
$single
->setParent($container);
try {
$single
->integrityCheck();
$single2 = new RulesConditionalTestStubSingleElement();
$single2
->setParent($container);
try {
$single
->integrityCheck();
$this
->fail($message);
} catch (RulesIntegrityException $ex) {
try {
$single2
->integrityCheck();
$this
->fail($message);
} catch (RulesIntegrityException $ex) {
$single2
->delete();
try {
$single
->integrityCheck();
$this
->pass($message);
} catch (RulesIntegrityException $ex) {
$this
->fail($message);
}
}
}
} catch (RulesIntegrityException $ex) {
$this
->fail($message);
}
$message = 'A default element does not validate if it precedes any element.';
$default = new RulesConditionalTestStubDefaultElement();
try {
$default
->setParent($container);
$default
->integrityCheck();
try {
$element = new RulesConditionalTestStubElement();
$element
->setParent($container);
$default
->integrityCheck();
$this
->fail($message);
} catch (RulesIntegrityException $ex) {
$element
->delete();
try {
$default
->integrityCheck();
$this
->pass($message);
} catch (RulesIntegrityException $ex) {
$this
->fail($message);
}
}
} catch (RulesIntegrityException $ex) {
$this
->fail($message);
}
}
/**
* Tests basic evaluation.
*/
public function testEvaluate() {
// Set up stub objects.
$container = new RulesConditionalTestStubContainer();
$branch = new RulesConditionalTestStubElement();
$branch
->action('rules_conditional_test_throw', array(
'message' => 'evaluate',
))
->setParent($container);
$defaultBranch = new RulesConditionalTestStubDefaultElement();
$defaultBranch
->action('rules_conditional_test_throw', array(
'message' => 'evaluate default',
))
->setParent($container);
// Evaluate an element.
$this
->assertExecution('evaluate', $container, array(), 'Evaluating container evaluates elements.');
$branch
->setPass(FALSE);
$this
->assertExecution('evaluate default', $container, array(), 'Evaluating container evaluates default elements.');
}
/**
* Tests container provided variables.
*/
public function testProvidesVariables() {
rules_action_set(array())
->action($container = new RulesConditionalTestStubContainer());
$textVariable = array(
'variable_added:var' => 'text',
'type' => 'text',
);
$mixedVariable1 = array(
'variable_added:var' => 'mixed',
'type' => 'text',
);
$mixedVariable2 = array(
'variable_added:var' => 'mixed',
'type' => 'token',
);
$branch1 = new RulesConditionalTestStubElement();
$branch1
->action('variable_add', $textVariable)
->setParent($container);
$this
->assertIdentical(array(), $container
->providesVariables(), 'Container does not provide variables without a default branch.');
$defaultBranch = new RulesConditionalTestStubDefaultElement();
$defaultBranch
->action('variable_add', $textVariable)
->setParent($container);
$this
->assertIdentical(array(
'text',
), array_keys($container
->providesVariables()), 'Container provides common variables in complete branches.');
$branch1
->action('variable_add', $mixedVariable1);
$defaultBranch
->action('variable_add', $mixedVariable2);
$this
->assertIdentical(array(
'text',
), array_keys($container
->providesVariables()), 'Container does not provide variables with mixed types.');
$branch2 = new RulesConditionalTestStubElement();
$branch2
->setParent($container);
$this
->assertIdentical(array(), $container
->providesVariables(), 'Container provides no variables if one branch provides none.');
}
/**
* Tests the base predicate element.
*/
public function testPredicateElement() {
$predicateElement = new RulesConditionalTestStubPredicateElement();
// Test integrity check.
$message = 'Predicate element does not validate without predicate.';
try {
$predicateElement
->integrityCheck();
$this
->fail($message);
} catch (RulesIntegrityException $ex) {
$expectedExceptionMessage = t('The conditional "%plugin" does not have a valid predicate.', array(
'%plugin' => $predicateElement
->plugin(),
));
$this
->assertEqual($expectedExceptionMessage, $ex
->getMessage(), $message);
}
// Test variable assertion using a field in a content type.
field_create_field(array(
'field_name' => 'field_test',
'type' => 'text',
));
field_create_instance(array(
'field_name' => 'field_test',
'entity_type' => 'node',
'bundle' => 'page',
));
$settings = array(
'type' => 'page',
'field_test' => array(
LANGUAGE_NONE => array(
array(
'value' => 'test value',
),
),
),
);
$node = $this
->drupalCreateNode($settings);
$predicateElement = new RulesConditionalTestStubPredicateElement('data_is', array(
'data:select' => 'node:type',
'op' => '==',
'value' => 'page',
));
$actionSet = rules_action_set(array(
'node' => array(
'type' => 'node',
'label' => 'Node',
),
))
->action($predicateElement
->action('rules_conditional_test_throw', array(
'message:select' => 'node:field_test',
)));
$this
->assertExecution('test value', $actionSet
->integrityCheck(), array(
$node,
), 'Predicate element correctly adds predicate assertions to state.');
}
}