function EventsExampleTest::testEventsExample

Same name in other branches
  1. 4.0.x modules/events_example/tests/src/Functional/EventsExampleTest.php \Drupal\Tests\events_example\Functional\EventsExampleTest::testEventsExample()

Test the output of the example page.

File

modules/events_example/tests/src/Functional/EventsExampleTest.php, line 39

Class

EventsExampleTest
Test the functionality of the Events Example module.

Namespace

Drupal\Tests\events_example\Functional

Code

public function testEventsExample() {
    // Test that the main page for the example is accessible.
    $events_example_form = Url::fromRoute('events_example.description');
    $this->drupalGet($events_example_form);
    $this->assertSession()
        ->statusCodeEquals(200);
    // Verify the page contains the required form fields.
    $this->assertSession()
        ->fieldExists('incident_type');
    $this->assertSession()
        ->fieldExists('incident');
    // Submit the form with an incident type of 'stolen_princess'. This does a
    // couple of things. Fist of all, it ensures that our code in
    // EventsExampleForm::submitForm() that dispatches events works. If it did
    // not work, no event would be dispatched, and the message below would never
    // get displayed. Secondly, it tests that our
    // EventsExampleSubscriber::notifyMario() event subscriber is triggered for
    // incidents of the type 'stolen_princess'.
    $values = [
        'incident_type' => 'stolen_princess',
        'incident' => $this->randomString(),
    ];
    $this->drupalGet($events_example_form);
    $this->submitForm($values, 'Submit');
    $this->assertSession()
        ->pageTextContains('Mario has been alerted. Thank you.');
    // Fill out the form again, this time testing that the
    // EventsExampleSubscriber::notifyBatman() subscriber is working.
    $values = [
        'incident_type' => 'joker',
        'incident' => $this->randomString(),
    ];
    $this->drupalGet($events_example_form);
    $this->submitForm($values, 'Submit');
    $this->assertSession()
        ->pageTextContains('Batman has been alerted. Thank you.');
    // Fill out the form again, this time testing that our default handler
    // catches all the remaining values.
    $values = [
        'incident_type' => 'cat',
        'incident' => $this->randomString(),
    ];
    $this->drupalGet($events_example_form);
    $this->submitForm($values, 'Submit');
    $this->assertSession()
        ->pageTextContains('notifyDefault()');
}