function ConfigureAndExecuteTest::testMultipleInputContext

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

File

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

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() {
    // Set up a rule. The event is not relevant, we just want a rule to use.
    // Calling $rule = $this->createRule('Test Multiple Input via UI',
    // 'test_rule', 'rules_entity_insert:node') works locally but fails
    // $this->assertEquals($expected_config_value, $to) on drupal.org with
    // 'null does not match expected type "array".', hence revert to the
    // long-hand way of creating the rule.
    $this->drupalGet('admin/config/workflow/rules');
    $this->clickLink('Add reaction rule');
    $this->fillField('Label', 'Test Multiple Input via UI');
    $this->fillField('Machine-readable name', 'test_rule');
    $this->fillField('React on event', 'rules_entity_insert:node');
    $this->pressButton('Save');
    // Add action rules_send_email because the 'to' field has 'multiple = TRUE'
    // rendered as a textarea that we can use for this test.
    $this->clickLink('Add action');
    $this->fillField('Action', 'rules_send_email');
    $this->pressButton('Continue');
    $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[to][setting]', implode($suboptimal_user_input));
    // Set the other required fields. These play no part in the test.
    $this->fillField('context_definitions[subject][setting]', 'Hello');
    $this->fillField('context_definitions[message][setting]', 'Dear Heart');
    $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",
    ];
    // Need to get the $rule again, as the existing $rule does not have the
    // changes added above and $rule->get('expression.actions...) is empty.
    // @todo Is there a way to refresh $rule and not have to get it again?
    $config_factory = $this->container
        ->get('config.factory');
    $config_factory->clearStaticCache();
    $rule = $config_factory->get('rules.reaction.test_rule');
    $to = $rule->get('expression.actions.actions.0.context_values.to');
    $this->assertEquals($expected_config_value, $to);
}