You are here

public function ActionsFormTest::testActionsFormWidgets in Rules 8.3

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 67

Class

ActionsFormTest
Tests that each Rules Action can be editted.

Namespace

Drupal\Tests\rules\Functional

Code

public function testActionsFormWidgets($id, $values = [], $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 action to the rule.
  $action = $expressionManager
    ->createAction($id);
  $rule
    ->addExpressionObject($action);

  // Save the configuration.
  $expr_id = 'test_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($values)) {

    // 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")]');
      }
    }

    // Fill each given field with the value provided.
    foreach ($values 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');
    $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");
  }
}