function ConfigureAndExecuteTest::testAddEventAndExecute

Tests adding an event and then triggering its execution.

File

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

Class

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

Namespace

Drupal\Tests\rules\Functional

Code

public function testAddEventAndExecute() {
    // Create an article.
    $node = $this->drupalCreateNode([
        'type' => 'article',
    ]);
    // Create a rule with a single event and with 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--article',
            ],
        ],
        'expression' => $rule->getConfiguration(),
    ]);
    $config_entity->save();
    $this->drupalLogin($this->account);
    
    /** @var \Drupal\Tests\WebAssert $assert */
    $assert = $this->assertSession();
    // Now add an event using the UI.
    $this->drupalGet('admin/config/workflow/rules/reactions/edit/test_rule');
    // Go to "Add event" page.
    $this->clickLink('Add event');
    $assert->pageTextContains('Add event to Test rule');
    $assert->pageTextContains('Event selection');
    $assert->pageTextContains('React on event');
    // Select an event.
    $this->findField('events[0][event_name]')
        ->selectOption('rules_entity_update:node');
    $this->pressButton('Add');
    // Select bundle 'article'.
    $this->findField('bundle')
        ->selectOption('article');
    $this->pressButton('Add');
    // Update an article and assert that the event is triggered.
    $this->drupalGet('node/' . $node->id() . '/edit/');
    $this->submitForm([], 'Save');
    $assert->pageTextContains($message);
}