public function ResourceTest::testFormats in Drupal 9
Same name and namespace in other branches
- 8 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\FunctionalCode
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', 'hal_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);
}