You are here

public function ConfigureAndExecuteTest::testAssignmentRestriction in Rules 8.3

Tests the implementation of assignment restriction in context form.

File

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

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() {
  $this
    ->drupalLogin($this->account);
  $expression_manager = $this->container
    ->get('plugin.manager.rules_expression');
  $storage = $this->container
    ->get('entity_type.manager')
    ->getStorage('rules_reaction_rule');

  // Create a rule.
  $rule = $expression_manager
    ->createRule();

  // Add a condition which is unrestricted.
  $condition1 = $expression_manager
    ->createCondition('rules_data_comparison');
  $rule
    ->addExpressionObject($condition1);

  // Add a condition which is restricted to 'selector' for 'node'.
  $condition2 = $expression_manager
    ->createCondition('rules_node_is_of_type');
  $rule
    ->addExpressionObject($condition2);

  // Add an action which is unrestricted.
  $action1 = $expression_manager
    ->createAction('rules_system_message');
  $rule
    ->addExpressionObject($action1);

  // Add an action which is restricted to 'input' for 'type'.
  $action2 = $expression_manager
    ->createAction('rules_variable_add');
  $rule
    ->addExpressionObject($action2);

  // As the ContextFormTrait is action/condition agnostic it is not necessary
  // to check a condition restricted to input, because the check on action2
  // covers this. Likewise we do not need an action restricted by selector
  // because condition2 covers this. Save the rule to config. No event needed.
  $config_entity = $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');

  // Edit condition 1, assert that the switch button is shown for value and
  // that the default entry field is regular text entry not a selector.
  $this
    ->drupalGet('admin/config/workflow/rules/reactions/edit/test_rule/edit/' . $condition1
    ->getUuid());
  $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 condition 2, assert that the switch button is NOT shown for node
  // and that the entry field is a selector with class rules-autocomplete.
  $this
    ->drupalGet('admin/config/workflow/rules/reactions/edit/test_rule/edit/' . $condition2
    ->getUuid());
  $assert
    ->buttonNotExists('edit-context-definitions-node-switch-button');
  $assert
    ->elementExists('xpath', '//input[@id="edit-context-definitions-node-setting" and contains(@class, "rules-autocomplete")]');

  // Edit action 1, assert that the switch button is shown for message and
  // that the default entry field is a regular text entry not a selector.
  $this
    ->drupalGet('admin/config/workflow/rules/reactions/edit/test_rule/edit/' . $action1
    ->getUuid());
  $assert
    ->buttonExists('edit-context-definitions-message-switch-button');
  $assert
    ->elementExists('xpath', '//input[@id="edit-context-definitions-message-setting" and not(contains(@class, "rules-autocomplete"))]');

  // Edit action 2, assert that the switch button is NOT shown for type and
  // that the entry field is a regular text entry not a selector.
  $this
    ->drupalGet('admin/config/workflow/rules/reactions/edit/test_rule/edit/' . $action2
    ->getUuid());
  $assert
    ->buttonNotExists('edit-context-definitions-type-switch-button');
  $assert
    ->elementExists('xpath', '//input[@id="edit-context-definitions-type-setting" and not(contains(@class, "rules-autocomplete"))]');
}