function ResourceTest::testAuthentication

Same name in other branches
  1. 9 core/modules/rest/tests/src/Functional/ResourceTest.php \Drupal\Tests\rest\Functional\ResourceTest::testAuthentication()
  2. 8.9.x core/modules/rest/tests/src/Functional/ResourceTest.php \Drupal\Tests\rest\Functional\ResourceTest::testAuthentication()
  3. 10 core/modules/rest/tests/src/Functional/ResourceTest.php \Drupal\Tests\rest\Functional\ResourceTest::testAuthentication()

Tests that a resource without authentication cannot be enabled.

File

core/modules/rest/tests/src/Functional/ResourceTest.php, line 92

Class

ResourceTest
Tests the structure of a REST resource.

Namespace

Drupal\Tests\rest\Functional

Code

public function testAuthentication() : void {
    RestResourceConfig::create([
        'id' => 'entity.entity_test',
        'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
        'configuration' => [
            'GET' => [
                'supported_formats' => [
                    'json',
                ],
            ],
        ],
    ])->save();
    // Verify that accessing the resource returns 401.
    $this->drupalGet($this->entity
        ->toUrl()
        ->setRouteParameter('_format', 'json'));
    // \Drupal\Core\Routing\RequestFormatRouteFilter considers the canonical,
    // non-REST route a match, but a lower quality one: no format restrictions
    // means there's always a match and hence when there is no matching REST
    // route, the non-REST route is used, but can't render into
    // application/json, so it returns a 406.
    $this->assertSession()
        ->statusCodeEquals(406);
}

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