function RenderArrayNonHtmlSubscriberTest::testResponses

Same name in other branches
  1. 9 core/modules/system/tests/src/Functional/Render/RenderArrayNonHtmlSubscriberTest.php \Drupal\Tests\system\Functional\Render\RenderArrayNonHtmlSubscriberTest::testResponses()
  2. 8.9.x core/modules/system/tests/src/Functional/Render/RenderArrayNonHtmlSubscriberTest.php \Drupal\Tests\system\Functional\Render\RenderArrayNonHtmlSubscriberTest::testResponses()
  3. 10 core/modules/system/tests/src/Functional/Render/RenderArrayNonHtmlSubscriberTest.php \Drupal\Tests\system\Functional\Render\RenderArrayNonHtmlSubscriberTest::testResponses()

Tests handling of responses by events subscriber.

File

core/modules/system/tests/src/Functional/Render/RenderArrayNonHtmlSubscriberTest.php, line 30

Class

RenderArrayNonHtmlSubscriberTest
Functional test verifying that render array throws 406 for non-HTML requests.

Namespace

Drupal\Tests\system\Functional\Render

Code

public function testResponses() : void {
    // Test that event subscriber does not interfere with normal requests.
    $url = Url::fromRoute('render_array_non_html_subscriber_test.render_array');
    $this->drupalGet($url);
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->pageTextContains("Controller response successfully rendered.");
    // Test that correct response code is returned for any non-HTML format.
    foreach ([
        'json',
        'xml',
        'foo',
    ] as $format) {
        $url = Url::fromRoute('render_array_non_html_subscriber_test.render_array', [
            '_format' => $format,
        ]);
        $this->drupalGet($url);
        $this->assertSession()
            ->statusCodeEquals(406);
        $this->assertSession()
            ->pageTextNotContains("Controller response successfully rendered.");
    }
    // Test that event subscriber does not interfere with raw string responses.
    $url = Url::fromRoute('render_array_non_html_subscriber_test.raw_string', [
        '_format' => 'foo',
    ]);
    $this->drupalGet($url);
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertSession()
        ->responseContains("Raw controller response.");
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.