function ConfigureAndExecuteTest::testDeleteEventAndExecute

Tests deleting an event and then triggering its execution.

File

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

Class

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

Namespace

Drupal\Tests\rules\Functional

Code

public function testDeleteEventAndExecute() {
    // Create a rule with two events and an action.
    $message = 'Rule is triggered';
    $rule = $this->expressionManager
        ->createRule();
    $rule->addAction('rules_system_message', ContextConfig::create()->setValue('message', $message)
        ->setValue('type', 'status'));
    $config_entity = $this->storage
        ->create([
        'id' => 'test_rule',
        'label' => 'Test rule',
        'events' => [
            [
                'event_name' => 'rules_entity_insert:node',
            ],
            [
                'event_name' => 'rules_entity_update:node',
            ],
        ],
        'expression' => $rule->getConfiguration(),
    ]);
    $config_entity->save();
    $this->drupalLogin($this->account);
    
    /** @var \Drupal\Tests\WebAssert $assert */
    $assert = $this->assertSession();
    // Create a node to ensure that the rule is triggered and the message is
    // displayed when creating a node (the first of the two events).
    $this->drupalGet('node/add/article');
    $this->submitForm([
        'title[0][value]' => 'Foo',
    ], 'Save');
    $assert->pageTextContains($message);
    // Delete an event using the UI.
    $this->drupalGet('admin/config/workflow/rules/reactions/edit/test_rule');
    // Click delete button for the first event.
    $this->clickLinkByHref('event-delete/rules_entity_insert');
    // Assert we are on the delete page.
    $assert->pageTextContains('Are you sure you want to delete the event After saving a new content item entity from Test rule?');
    // And confirm the delete.
    $this->pressButton('Delete');
    $assert->pageTextContains('Deleted event After saving a new content item entity from Test rule.');
    // Create a node and assert that the event is not triggered.
    $this->drupalGet('node/add/article');
    $this->submitForm([
        'title[0][value]' => 'Bar',
    ], 'Save');
    $node = $this->drupalGetNodeByTitle('Bar');
    $assert->pageTextNotContains($message);
    // Update it and assert that the message now does get displayed.
    $this->drupalGet('node/' . $node->id() . '/edit/');
    $this->submitForm([], 'Save');
    $assert->pageTextContains($message);
}