View source
<?php
namespace Drupal\Tests\rules\Functional;
use Drupal\rules\Context\ContextConfig;
class ConfigureAndExecuteTest extends RulesBrowserTestBase {
protected static $modules = [
'node',
'rules',
];
protected $profile = 'minimal';
protected $account;
protected function setUp() : void {
parent::setUp();
$type = $this->container
->get('entity_type.manager')
->getStorage('node_type')
->create([
'type' => 'article',
'name' => 'Article',
]);
$type
->save();
$this->account = $this
->drupalCreateUser([
'create article content',
'edit any article content',
'administer rules',
'administer site configuration',
]);
}
public function testConfigureAndExecute() {
$this
->drupalLogin($this->account);
$this
->drupalGet('admin/config/workflow/rules');
$this
->clickLink('Add reaction rule');
$this
->fillField('Label', 'Test rule');
$this
->fillField('Machine-readable name', 'test_rule');
$this
->fillField('React on event', 'rules_entity_presave:node');
$this
->pressButton('Save');
$this
->clickLink('Add condition');
$this
->fillField('Condition', 'rules_data_comparison');
$this
->pressButton('Continue');
$this
->fillField('context_definitions[data][setting]', 'node.title.0.value');
$this
->fillField('context_definitions[value][setting]', 'Test title');
$this
->pressButton('Save');
$this
->clickLink('Add action');
$this
->fillField('Action', 'rules_system_message');
$this
->pressButton('Continue');
$this
->fillField('context_definitions[message][setting]', 'Title matched "Test title"!');
$this
->fillField('context_definitions[type][setting]', 'status');
$this
->pressButton('Save');
$this
->pressButton('Save');
$assert = $this
->assertSession();
$this
->drupalGet('node/add/article');
$this
->fillField('Title', 'Test title');
$this
->pressButton('Save');
$assert
->pageTextContains('Title matched "Test title"!');
$this
->drupalGet('node/add/article');
$this
->fillField('Title', 'Test title');
$this
->pressButton('Save');
$assert
->pageTextContains('Title matched "Test title"!');
$this
->drupalGet('admin/config/workflow/rules');
$this
->clickLink('Disable');
$this
->drupalGet('node/add/article');
$this
->fillField('Title', 'Test title');
$this
->pressButton('Save');
$assert
->pageTextNotContains('Title matched "Test title"!');
$this
->drupalGet('admin/config/workflow/rules');
$this
->clickLink('Enable');
$this
->drupalGet('node/add/article');
$this
->fillField('Title', 'Test title');
$this
->pressButton('Save');
$assert
->pageTextContains('Title matched "Test title"!');
$this
->drupalGet('admin/config/workflow/rules/reactions/edit/test_rule');
$this
->clickLink('Edit', 0);
$this
->getSession()
->getPage()
->checkField('negate');
$this
->pressButton('Save');
$this
->pressButton('Save');
$this
->drupalGet('node/add/article');
$this
->fillField('Title', 'Test title');
$this
->pressButton('Save');
$assert
->pageTextNotContains('Title matched "Test title"!');
}
public function testTwoRulesSameEvent() {
$expressionManager = $this->container
->get('plugin.manager.rules_expression');
$storage = $this->container
->get('entity_type.manager')
->getStorage('rules_reaction_rule');
$assert = $this
->assertSession();
$this
->drupalLogin($this->account);
$rule1 = $expressionManager
->createRule();
$rule1
->addCondition('rules_data_comparison', ContextConfig::create()
->map('data', 'node.title.value')
->setValue('operation', 'contains')
->setValue('value', 'Two Rules Same Event'));
$message1 = 'RULE ONE is triggered';
$rule1
->addAction('rules_system_message', ContextConfig::create()
->setValue('message', $message1)
->setValue('type', 'status'));
$config_entity = $storage
->create([
'id' => 'rule1',
'label' => 'Rule One',
'events' => [
[
'event_name' => 'rules_entity_presave:node',
],
],
'expression' => $rule1
->getConfiguration(),
]);
$config_entity
->save();
$this
->drupalGet('node/add/article');
$this
->submitForm([
'title[0][value]' => 'Two Rules Same Event',
], 'Save');
$node = $this
->drupalGetNodeByTitle('Two Rules Same Event');
$assert
->pageTextContains($message1);
$rule2 = $expressionManager
->createRule();
$rule2
->addCondition('rules_data_comparison', ContextConfig::create()
->map('data', 'node.title.value')
->setValue('operation', 'contains')
->setValue('value', 'Two Rules Same Event'));
$message2 = 'RULE TWO is triggered';
$rule2
->addAction('rules_system_message', ContextConfig::create()
->setValue('message', $message2)
->setValue('type', 'status'));
$config_entity = $storage
->create([
'id' => 'rule2',
'label' => 'Rule Two',
'events' => [
[
'event_name' => 'rules_entity_presave:node',
],
],
'expression' => $rule2
->getConfiguration(),
]);
$config_entity
->save();
$this
->drupalGet('node/' . $node
->id() . '/edit/');
$this
->submitForm([], 'Save');
$assert
->pageTextContains($message1);
$assert
->pageTextContains($message2);
$this
->drupalGet('admin/config/workflow/rules');
$this
->clickLinkByHref('disable/rule2');
$this
->drupalGet('node/' . $node
->id() . '/edit/');
$this
->submitForm([], 'Save');
$assert
->pageTextContains($message1);
$assert
->pageTextNotContains($message2);
$this
->drupalGet('admin/config/workflow/rules');
$this
->clickLinkByHref('enable/rule2');
$this
->drupalGet('node/' . $node
->id() . '/edit/');
$this
->submitForm([], 'Save');
$assert
->pageTextContains($message1);
$assert
->pageTextContains($message2);
$message1updated = 'RULE ONE has a new message.';
$this
->drupalGet('admin/config/workflow/rules/reactions/edit/rule1');
$this
->clickLink('Edit', 1);
$this
->fillField('context_definitions[message][setting]', $message1updated);
$this
->pressButton('Save');
$this
->pressButton('Save');
$this
->drupalGet('node/' . $node
->id() . '/edit/');
$this
->submitForm([], 'Save');
$assert
->pageTextNotContains($message1);
$assert
->pageTextContains($message1updated);
$assert
->pageTextContains($message2);
$this
->drupalGet('admin/config/workflow/rules');
$this
->clickLinkByHref('delete/rule1');
$this
->pressButton('Delete');
$this
->drupalGet('node/' . $node
->id() . '/edit/');
$this
->submitForm([], 'Save');
$assert
->pageTextNotContains($message1);
$assert
->pageTextNotContains($message1updated);
$assert
->pageTextContains($message2);
$this
->drupalGet('admin/config/workflow/rules');
$this
->clickLinkByHref('disable/rule2');
$this
->drupalGet('node/' . $node
->id() . '/edit/');
$this
->submitForm([], 'Save');
$assert
->pageTextNotContains($message1);
$assert
->pageTextNotContains($message1updated);
$assert
->pageTextNotContains($message2);
}
public function testMultipleInputContext() {
$this
->drupalLogin($this->account);
$this
->drupalGet('admin/config/workflow/rules');
$this
->clickLink('Add reaction rule');
$this
->fillField('Label', 'Test rule');
$this
->fillField('Machine-readable name', 'test_rule');
$this
->fillField('React on event', 'rules_entity_insert:node');
$this
->pressButton('Save');
$this
->clickLink('Add condition');
$this
->fillField('Condition', 'rules_node_is_of_type');
$this
->pressButton('Continue');
$this
->fillField('context_definitions[node][setting]', 'node');
$suboptimal_user_input = [
" \r\nwhitespace at beginning of input\r\n",
"text\r\n",
"trailing space \r\n",
"\rleading terminator\r\n",
" leading space\r\n",
"multiple words, followed by primitive values\r\n",
"0\r\n",
"0.0\r\n",
"128\r\n",
" false\r\n",
"true \r\n",
"null\r\n",
"terminator r\r",
"two empty lines\n\r\n\r",
"terminator n\n",
"terminator nr\n\r",
"whitespace at end of input\r\n \r\n",
];
$this
->fillField('context_definitions[types][setting]', implode($suboptimal_user_input));
$this
->pressButton('Save');
$this
->pressButton('Save');
$expected_config_value = [
"whitespace at beginning of input",
"text",
"trailing space",
"leading terminator",
"leading space",
"multiple words, followed by primitive values",
"0",
"0.0",
"128",
"false",
"true",
"null",
"terminator r",
"two empty lines",
"terminator n",
"terminator nr",
"whitespace at end of input",
];
$config_factory = $this->container
->get('config.factory');
$rule = $config_factory
->get('rules.reaction.test_rule');
$this
->assertEquals($expected_config_value, $rule
->get('expression.conditions.conditions.0.context_values.types'));
}
public function testAssignmentRestriction() {
$this
->drupalLogin($this->account);
$expression_manager = $this->container
->get('plugin.manager.rules_expression');
$storage = $this->container
->get('entity_type.manager')
->getStorage('rules_reaction_rule');
$rule = $expression_manager
->createRule();
$condition1 = $expression_manager
->createCondition('rules_data_comparison');
$rule
->addExpressionObject($condition1);
$condition2 = $expression_manager
->createCondition('rules_node_is_of_type');
$rule
->addExpressionObject($condition2);
$action1 = $expression_manager
->createAction('rules_system_message');
$rule
->addExpressionObject($action1);
$action2 = $expression_manager
->createAction('rules_variable_add');
$rule
->addExpressionObject($action2);
$config_entity = $storage
->create([
'id' => 'test_rule',
'expression' => $rule
->getConfiguration(),
]);
$config_entity
->save();
$assert = $this
->assertSession();
$this
->drupalGet('admin/config/workflow/rules/reactions/edit/test_rule');
$this
->drupalGet('admin/config/workflow/rules/reactions/edit/test_rule/edit/' . $condition1
->getUuid());
$assert
->buttonExists('edit-context-definitions-value-switch-button');
$assert
->elementExists('xpath', '//input[@id="edit-context-definitions-value-setting" and not(contains(@class, "rules-autocomplete"))]');
$this
->drupalGet('admin/config/workflow/rules/reactions/edit/test_rule/edit/' . $condition2
->getUuid());
$assert
->buttonNotExists('edit-context-definitions-node-switch-button');
$assert
->elementExists('xpath', '//input[@id="edit-context-definitions-node-setting" and contains(@class, "rules-autocomplete")]');
$this
->drupalGet('admin/config/workflow/rules/reactions/edit/test_rule/edit/' . $action1
->getUuid());
$assert
->buttonExists('edit-context-definitions-message-switch-button');
$assert
->elementExists('xpath', '//input[@id="edit-context-definitions-message-setting" and not(contains(@class, "rules-autocomplete"))]');
$this
->drupalGet('admin/config/workflow/rules/reactions/edit/test_rule/edit/' . $action2
->getUuid());
$assert
->buttonNotExists('edit-context-definitions-type-switch-button');
$assert
->elementExists('xpath', '//input[@id="edit-context-definitions-type-setting" and not(contains(@class, "rules-autocomplete"))]');
}
}