You are here

public function ConditionsFormTest::testConditionsFormWidgets in Rules 8.3

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 67

Class

ConditionsFormTest
Tests that each Rules Condition can be editted.

Namespace

Drupal\Tests\rules\Functional

Code

public function testConditionsFormWidgets($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 condition to the rule.
  $condition = $expressionManager
    ->createCondition($id);
  $rule
    ->addExpressionObject($condition);

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

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

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