function ConfigureAndExecuteTest::testAssignmentRestriction

Tests the implementation of assignment restriction in context form.

File

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

Class

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

Namespace

Drupal\Tests\rules\Functional

Code

public function testAssignmentRestriction() {
    // Create a rule.
    $rule = $this->expressionManager
        ->createRule();
    // Add a condition which is restricted to selector for 'data', restricted to
    // input for 'operation' but unrestricted on 'value'.
    $condition1 = $this->expressionManager
        ->createCondition('rules_data_comparison');
    $rule->addExpressionObject($condition1);
    // Add an action which is unrestricted on 'message' and 'type' but is
    // restricted to input for 'repeat'.
    $action1 = $this->expressionManager
        ->createAction('rules_system_message');
    $rule->addExpressionObject($action1);
    // As the ContextFormTrait is action/condition agnostic it is not necessary
    // to check an action restricted by selector because the condition covers
    // this. Save the rule to config. No event needed.
    $config_entity = $this->storage
        ->create([
        'id' => 'test_rule',
        'expression' => $rule->getConfiguration(),
    ]);
    $config_entity->save();
    
    /** @var \Drupal\Tests\WebAssert $assert */
    $assert = $this->assertSession();
    // Display the rule edit page to show the actions and conditions.
    $this->drupalGet('admin/config/workflow/rules/reactions/edit/test_rule');
    $assert->statusCodeEquals(200);
    // Edit the condition and assert that the page loads correctly.
    $this->drupalGet('admin/config/workflow/rules/reactions/edit/test_rule/edit/' . $condition1->getUuid());
    $assert->statusCodeEquals(200);
    // Check that a switch button is not shown for 'data' and that the field is
    // an autocomplete selector field not plain text entry.
    $assert->buttonNotExists('edit-context-definitions-data-switch-button');
    $assert->elementExists('xpath', '//input[@id="edit-context-definitions-data-setting" and contains(@class, "rules-autocomplete")]');
    // Check that a switch button is not shown for 'operation'.
    $assert->buttonNotExists('edit-context-definitions-operation-switch-button');
    // Check that a switch button is shown for 'value' and that the default
    // field is plain text entry not an autocomplete selector field.
    $assert->buttonExists('edit-context-definitions-value-switch-button');
    $assert->elementExists('xpath', '//input[@id="edit-context-definitions-value-setting" and not(contains(@class, "rules-autocomplete"))]');
    // Edit the action and assert that page loads correctly.
    $this->drupalGet('admin/config/workflow/rules/reactions/edit/test_rule/edit/' . $action1->getUuid());
    $assert->statusCodeEquals(200);
    // Check that a switch button is shown for 'message' and that the field is a
    // plain text entry field not an autocomplete selector field.
    $assert->buttonExists('edit-context-definitions-message-switch-button');
    $assert->elementExists('xpath', '//input[@id="edit-context-definitions-message-setting" and not(contains(@class, "rules-autocomplete"))]');
    // Check that a switch button is shown for 'type'.
    $assert->buttonExists('edit-context-definitions-type-switch-button');
    // Check that a switch button is not shown for 'repeat'.
    $assert->buttonNotExists('edit-context-definitions-repeat-switch-button');
}