function ConditionsFormTest::testConditionsFormWidgets

Test each condition provided by Rules.

Check that every condition 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 dataConditionsFormWidgets

File

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

Class

ConditionsFormTest
Tests that each Rules Condition can be edited.

Namespace

Drupal\Tests\rules\Functional

Code

public function testConditionsFormWidgets($id, $required = [], $defaulted = [], $widgets = [], $selectors = []) {
    $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 condition to the rule.
    $condition = $expressionManager->createCondition($id);
    $rule->addExpressionObject($condition);
    // Save the configuration.
    $expr_id = 'condition_' . 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 condition and check that the page is generated without error.
    $this->drupalGet('admin/config/workflow/rules/reactions/edit/' . $expr_id . '/edit/' . $condition->getUuid());
    $assert->statusCodeEquals(200);
    $assert->pageTextContains('Edit ' . $condition->getLabel());
    // If any field values have been specified then fill in the form and save.
    if (!empty($required) || !empty($defaulted)) {
        // Switch to data selector where required.
        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 condition 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=' . $condition->getUuid() . '|)$#');
        // Check that re-edit and re-save works OK.
        $this->clickLink('Edit');
        if (!empty($defaulted)) {
            // Fill each previously defaulted field with the value provided.
            foreach ($defaulted as $name => $value) {
                $this->fillField('edit-context-definitions-' . $name . '-setting', $value);
            }
        }
        $this->pressButton('Save');
        $assert->pageTextNotContains('Error message');
        $assert->addressMatches('#admin/config/workflow/rules/reactions/edit/' . $expr_id . '(\\?uuid=' . $condition->getUuid() . '|)$#');
        // Save the rule.
        $this->pressButton('Save');
        $assert->pageTextContains("Reaction rule {$expr_id} has been updated");
    }
}