You are here

public function ConfigureAndExecuteTest::testMultipleInputContext in Rules 8.3

Tests user input in context form for 'multiple' valued context variables.

File

tests/src/Functional/ConfigureAndExecuteTest.php, line 283

Class

ConfigureAndExecuteTest
Tests that a rule can be configured and triggered when a node is edited.

Namespace

Drupal\Tests\rules\Functional

Code

public function testMultipleInputContext() {
  $this
    ->drupalLogin($this->account);
  $this
    ->drupalGet('admin/config/workflow/rules');

  // Set up a rule that will check the node type of a newly-created node.
  // The node type is the 'multiple' valued textarea we will test.
  $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');

  // Use node_is_of_type because the types field has 'multiple = TRUE'.
  $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');

  // One more save to permanently store the rule.
  $this
    ->pressButton('Save');

  // Now examine the config to ensure the user input was parsed properly
  // and that blank lines, leading and trailing whitespace, and wrong line
  // terminators were removed.
  $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'));
}