You are here

public function ConfigureAndExecuteTest::testConfigureAndExecute in Rules 8.3

Tests creation of a rule and then triggering its execution.

File

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

Class

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

Namespace

Drupal\Tests\rules\Functional

Code

public function testConfigureAndExecute() {
  $this
    ->drupalLogin($this->account);
  $this
    ->drupalGet('admin/config/workflow/rules');

  // Set up a rule that will show a system message if the title of a node
  // matches "Test title".
  $this
    ->clickLink('Add reaction rule');
  $this
    ->fillField('Label', 'Test rule');
  $this
    ->fillField('Machine-readable name', 'test_rule');
  $this
    ->fillField('React on event', 'rules_entity_presave:node');
  $this
    ->pressButton('Save');
  $this
    ->clickLink('Add condition');
  $this
    ->fillField('Condition', 'rules_data_comparison');
  $this
    ->pressButton('Continue');
  $this
    ->fillField('context_definitions[data][setting]', 'node.title.0.value');
  $this
    ->fillField('context_definitions[value][setting]', 'Test title');
  $this
    ->pressButton('Save');
  $this
    ->clickLink('Add action');
  $this
    ->fillField('Action', 'rules_system_message');
  $this
    ->pressButton('Continue');
  $this
    ->fillField('context_definitions[message][setting]', 'Title matched "Test title"!');
  $this
    ->fillField('context_definitions[type][setting]', 'status');
  $this
    ->pressButton('Save');

  // One more save to permanently store the rule.
  $this
    ->pressButton('Save');

  /** @var \Drupal\Tests\WebAssert $assert */
  $assert = $this
    ->assertSession();

  // Add a node now and check if our rule triggers.
  $this
    ->drupalGet('node/add/article');
  $this
    ->fillField('Title', 'Test title');
  $this
    ->pressButton('Save');
  $assert
    ->pageTextContains('Title matched "Test title"!');

  // Add a second node with the same title and check the rule triggers again.
  // This tests that the cache update (or non-update) works OK.
  // @see https://www.drupal.org/project/rules/issues/3108494
  $this
    ->drupalGet('node/add/article');
  $this
    ->fillField('Title', 'Test title');
  $this
    ->pressButton('Save');
  $assert
    ->pageTextContains('Title matched "Test title"!');

  // Disable rule and make sure it doesn't get triggered.
  $this
    ->drupalGet('admin/config/workflow/rules');
  $this
    ->clickLink('Disable');
  $this
    ->drupalGet('node/add/article');
  $this
    ->fillField('Title', 'Test title');
  $this
    ->pressButton('Save');
  $assert
    ->pageTextNotContains('Title matched "Test title"!');

  // Re-enable the rule and make sure it gets triggered again.
  $this
    ->drupalGet('admin/config/workflow/rules');
  $this
    ->clickLink('Enable');
  $this
    ->drupalGet('node/add/article');
  $this
    ->fillField('Title', 'Test title');
  $this
    ->pressButton('Save');
  $assert
    ->pageTextContains('Title matched "Test title"!');

  // Edit the rule and negate the condition.
  $this
    ->drupalGet('admin/config/workflow/rules/reactions/edit/test_rule');
  $this
    ->clickLink('Edit', 0);
  $this
    ->getSession()
    ->getPage()
    ->checkField('negate');
  $this
    ->pressButton('Save');

  // One more save to permanently store the rule.
  $this
    ->pressButton('Save');

  // Create node with same title and check that the message is not shown.
  $this
    ->drupalGet('node/add/article');
  $this
    ->fillField('Title', 'Test title');
  $this
    ->pressButton('Save');
  $assert
    ->pageTextNotContains('Title matched "Test title"!');
}