function RulesTestCase::testMetadataAssertion
Same name in other branches
- 7.x-2.x tests/rules.test \RulesTestCase::testMetadataAssertion()
Test asserting metadata, customizing action info and make sure integrity is checked.
File
-
d7-tests/
rules_test_case.test, line 578
Class
Code
function testMetadataAssertion() {
$action = rules_action('rules_node_make_sticky_action');
// Test failing integrity check.
try {
$rule = rule(array(
'node' => array(
'type' => 'entity',
),
));
$rule->action($action);
// Fails due to the 'node' variable not matching the node type.
$rule->integrityCheck();
$this->fail('Integrity check has not thrown an exception.');
} catch (RulesIntegrityException $e) {
$this->pass('Integrity check has thrown exception: ' . $e->getMessage());
}
// Test asserting additional metadata.
$rule = rule(array(
'node' => array(
'type' => 'node',
),
));
// Customize action info using the settings.
$rule->condition('data_is', array(
'data:select' => 'node:type',
'value' => 'page',
))
->condition(rules_condition('data_is', array(
'data:select' => 'node:body:value',
'value' => 'foo',
))->negate())
->action($action);
// Make sure the integrity check doesn't throw an exception.
$rule->integrityCheck();
// Test the rule.
$node = $this->drupalCreateNode(array(
'type' => 'page',
'sticky' => 0,
));
$rule->execute($node);
$this->assertTrue($node->sticky, 'Rule with asserted metadata executed.');
// Test asserting metadata on a derived property, i.e. not a variable.
$rule = rule(array(
'node' => array(
'type' => 'node',
),
));
$rule->condition('entity_is_of_type', array(
'entity:select' => 'node:reference',
'type' => 'node',
))
->condition('data_is', array(
'data:select' => 'node:reference:type',
'value' => 'page',
))
->action('rules_node_page_make_sticky_action', array(
'node:select' => 'node:reference',
));
$rule->integrityCheck();
$rule->execute($node);
// Test asserting an entity field.
$rule = rule(array(
'node' => array(
'type' => 'node',
),
));
$rule->condition('entity_has_field', array(
'entity:select' => 'node:reference',
'field' => 'field_tags',
))
->action('data_set', array(
'data:select' => 'node:reference:field-tags',
'value' => array(),
));
$rule->integrityCheck();
$rule->execute($node);
// Make sure an asserted bundle can be used as argument.
$rule = rule(array(
'node' => array(
'type' => 'node',
),
));
$rule->condition('entity_is_of_type', array(
'entity:select' => 'node:reference',
'type' => 'node',
))
->condition('node_is_of_type', array(
'node:select' => 'node:reference',
'type' => array(
'page',
),
))
->action('rules_node_page_make_sticky_action', array(
'node:select' => 'node:reference',
));
$rule->integrityCheck();
$rule->execute($node);
// Test asserting metadata on a derived property being a list item.
$rule = rule(array(
'node' => array(
'type' => 'node',
),
));
$rule->condition('node_is_of_type', array(
'node:select' => 'node:ref-nodes:0',
'type' => array(
'article',
),
))
->action('data_set', array(
'data:select' => 'node:ref-nodes:0:field-tags',
'value' => array(),
));
$rule->integrityCheck();
$rule->execute($node);
// Give green lights if there were no exceptions and check rules-log errors.
$this->pass('Rules asserting metadata on a derived property pass integrity checks.');
RulesLog::logger()->checkLog();
// Make sure assertions of a one list item are not valid for another item.
$rule = rule(array(
'node' => array(
'type' => 'node',
),
));
$rule->condition('node_is_of_type', array(
'node:select' => 'node:ref-nodes:0',
'type' => array(
'article',
),
))
->action('data_set', array(
'data:select' => 'node:ref-nodes:1:field-tags',
'value' => array(),
));
try {
$rule->integrityCheck();
$this->fail('Assertion of a list item is not valid for another item.');
} catch (RulesException $e) {
$this->pass('Assertion of a list item is not valid for another item.');
}
}