function RulesTestCase::testMetadataAssertion
Same name in other branches
- 8.x-3.x d7-tests/rules_test_case.test \RulesTestCase::testMetadataAssertion()
Tests asserting metadata.
Customizes action info and makes sure integrity is checked.
File
-
tests/
rules.test, line 620
Class
- RulesTestCase
- Rules test cases.
Code
public 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.');
}
}