function ActionsFormTest::testActionsFormWidgets

Test each action provided by Rules.

Check that every action can be added to a rule and that the edit page can be accessed. This ensures that the datatypes used in the definitions do exist. This test does not execute the conditions or actions.

@dataProvider dataActionsFormWidgets

File

tests/src/Functional/ActionsFormTest.php, line 70

Class

ActionsFormTest
Tests that each Rules Action can be edited.

Namespace

Drupal\Tests\rules\Functional

Code

public function testActionsFormWidgets($id, $required = [], $defaulted = [], $widgets = [], $selectors = [], $provides = []) {
    $expressionManager = $this->container
        ->get('plugin.manager.rules_expression');
    $storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('rules_reaction_rule');
    
    /** @var \Drupal\Tests\WebAssert $assert */
    $assert = $this->assertSession();
    // Create a rule.
    $rule = $expressionManager->createRule();
    // Add the action to the rule.
    $action = $expressionManager->createAction($id);
    $rule->addExpressionObject($action);
    // Save the configuration.
    $expr_id = 'action_' . str_replace(':', '_', $id);
    $config_entity = $storage->create([
        'id' => $expr_id,
        'expression' => $rule->getConfiguration(),
        // Specify a node event so that the node... selector values are available.
'events' => [
            [
                'event_name' => 'rules_entity_update:node',
            ],
        ],
    ]);
    $config_entity->save();
    // Edit the action and check that the page is generated without error.
    $this->drupalGet('admin/config/workflow/rules/reactions/edit/' . $expr_id . '/edit/' . $action->getUuid());
    $assert->statusCodeEquals(200);
    $assert->pageTextContains('Edit ' . $action->getLabel());
    // If any field values have been specified then fill in the form and save.
    if (!empty($required) || !empty($defaulted)) {
        // Switch to data selector if required by the test settings.
        if (!empty($selectors)) {
            foreach ($selectors as $name) {
                $this->pressButton('edit-context-definitions-' . $name . '-switch-button');
                // Check that the switch worked.
                $assert->elementExists('xpath', '//input[@id="edit-context-definitions-' . $name . '-switch-button" and contains(@value, "Switch to the direct input mode")]');
            }
        }
        // Try to save the form before entering the required values.
        if (!empty($required)) {
            $this->pressButton('Save');
            // Check that the form has not been saved.
            $assert->pageTextContains('Error message');
            $assert->pageTextContains('field is required');
            // Fill each required field with the value provided.
            foreach ($required as $name => $value) {
                $this->fillField('edit-context-definitions-' . $name . '-setting', $value);
            }
        }
        // Check that the action can be saved.
        $this->pressButton('Save');
        $assert->pageTextNotContains('InvalidArgumentException: Cannot set a list with a non-array value');
        $assert->pageTextNotContains('Error message');
        $assert->pageTextContains('You have unsaved changes.');
        // Allow for the ?uuid query string being present or absent in the assert
        // method by using addressMatches() with regex instead of addressEquals().
        $assert->addressMatches('#admin/config/workflow/rules/reactions/edit/' . $expr_id . '(\\?uuid=' . $action->getUuid() . '|)$#');
        // Check that re-edit and re-save works OK.
        $this->clickLink('Edit');
        if (!empty($defaulted) || !empty($provides)) {
            // Fill each previously defaulted field with the value provided.
            foreach ($defaulted as $name => $value) {
                $this->fillField('edit-context-definitions-' . $name . '-setting', $value);
            }
            foreach ($provides as $name => $value) {
                $this->fillField('edit-provides-' . $name . '-name', $value);
            }
        }
        $this->pressButton('Save');
        $assert->pageTextNotContains('Error message');
        $assert->addressMatches('#admin/config/workflow/rules/reactions/edit/' . $expr_id . '(\\?uuid=' . $action->getUuid() . '|)$#');
        // Save the rule.
        $this->pressButton('Save');
        $assert->pageTextContains("Reaction rule {$expr_id} has been updated");
    }
}