function SessionExampleTest::testSessionExampleLinks

Same name in other branches
  1. 3.x modules/session_example/tests/src/Functional/SessionExampleTest.php \Drupal\Tests\session_example\Functional\SessionExampleTest::testSessionExampleLinks()
  2. 4.0.x modules/session_example/tests/src/Functional/SessionExampleTest.php \Drupal\Tests\session_example\Functional\SessionExampleTest::testSessionExampleLinks()

Test all the routes, and ensure that forms can be submitted.

File

session_example/tests/src/Functional/SessionExampleTest.php, line 45

Class

SessionExampleTest
Tests the basic functions of the Session Example module.

Namespace

Drupal\Tests\session_example\Functional

Code

public function testSessionExampleLinks() {
    $assert = $this->assertSession();
    // Routes with menu links, and their form buttons.
    $routes_with_menu_links = [
        'session_example.form' => [
            'Save',
            'Clear Session',
        ],
    ];
    // Ensure the links appear in the tools menu sidebar.
    $this->drupalGet('');
    foreach (array_keys($routes_with_menu_links) as $route) {
        $assert->linkByHrefExists(Url::fromRoute($route)->getInternalPath());
    }
    // All our routes with their form buttons.
    $routes = [
        'session_example.view' => [],
    ];
    // Go to all the routes and click all the buttons.
    $routes = array_merge($routes_with_menu_links, $routes);
    foreach ($routes as $route => $buttons) {
        $url = Url::fromRoute($route);
        $this->drupalGet($url);
        $assert->statusCodeEquals(200);
        foreach ($buttons as $button) {
            $this->drupalPostForm($url, [], $button);
            $assert->statusCodeEquals(200);
        }
    }
}