function AjaxExampleMenuTest::testAjaxExampleLinks

Same name in other branches
  1. 4.0.x modules/ajax_example/tests/src/Functional/AjaxExampleMenuTest.php \Drupal\Tests\ajax_example\Functional\AjaxExampleMenuTest::testAjaxExampleLinks()

Tests links.

File

modules/ajax_example/tests/src/Functional/AjaxExampleMenuTest.php, line 38

Class

AjaxExampleMenuTest
Verify tool block menu items and operability of all our routes.

Namespace

Drupal\Tests\ajax_example\Functional

Code

public function testAjaxExampleLinks() {
    // Login a user that can access content.
    $this->drupalLogin($this->createUser([
        'access content',
        'access user profiles',
    ]));
    $assertion = $this->assertSession();
    // Routes with menu links, and their form buttons.
    $routes_with_menu_links = [
        'ajax_example.description' => [],
        'ajax_example.simplest' => [],
        'ajax_example.autotextfields' => [
            'Click Me',
        ],
        'ajax_example.submit_driven_ajax' => [
            'Submit',
        ],
        'ajax_example.dependent_dropdown' => [
            'Submit',
        ],
        'ajax_example.dynamic_form_sections' => [
            'Choose',
        ],
        'ajax_example.wizard' => [
            'Next step',
        ],
        'ajax_example.wizardnojs' => [
            'Next step',
        ],
        'ajax_example.ajax_link_render' => [],
        'ajax_example.autocomplete_user' => [
            'Submit',
        ],
    ];
    // Ensure the links appear in the tools menu sidebar.
    $this->drupalGet('');
    foreach (array_keys($routes_with_menu_links) as $route) {
        $assertion->linkByHrefExists(Url::fromRoute($route)->getInternalPath());
    }
    // All our routes with their form buttons.
    $routes = [
        'ajax_example.ajax_link_callback' => [],
    ];
    // 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);
        if ($route === 'ajax_example.ajax_link_callback') {
            $url = Url::fromRoute($route, [
                'nojs' => 'nojs',
            ]);
        }
        $this->drupalGet($url);
        $assertion->statusCodeEquals(200);
        foreach ($buttons as $button) {
            $this->drupalGet($url);
            $this->submitForm([], $button);
            $assertion->statusCodeEquals(200);
        }
    }
}