function ResourceTest::testFormats

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

Tests that a resource without formats cannot be enabled.

File

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

Class

ResourceTest
Tests the structure of a REST resource.

Namespace

Drupal\Tests\rest\Functional

Code

public function testFormats() {
    RestResourceConfig::create([
        'id' => 'entity.entity_test',
        'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
        'configuration' => [
            'GET' => [
                'supported_auth' => [
                    'basic_auth',
                ],
            ],
        ],
    ])->save();
    // Verify that accessing the resource returns 406.
    $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/hal+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.