You are here

public function ResourceTest::testSerializationClassIsOptional in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/rest/tests/src/Functional/ResourceTest.php \Drupal\Tests\rest\Functional\ResourceTest::testSerializationClassIsOptional()
  2. 10 core/modules/rest/tests/src/Functional/ResourceTest.php \Drupal\Tests\rest\Functional\ResourceTest::testSerializationClassIsOptional()

Tests that serialization_class is optional.

File

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

Class

ResourceTest
Tests the structure of a REST resource.

Namespace

Drupal\Tests\rest\Functional

Code

public function testSerializationClassIsOptional() {
  RestResourceConfig::create([
    'id' => 'serialization_test',
    'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY,
    'configuration' => [
      'POST' => [
        'supported_formats' => [
          'json',
        ],
        'supported_auth' => [
          'cookie',
        ],
      ],
    ],
  ])
    ->save();
  \Drupal::service('router.builder')
    ->rebuildIfNeeded();
  Role::load(RoleInterface::ANONYMOUS_ID)
    ->grantPermission('restful post serialization_test')
    ->save();
  $serialized = $this->container
    ->get('serializer')
    ->serialize([
    'foo',
    'bar',
  ], 'json');
  $request_options = [
    RequestOptions::HEADERS => [
      'Content-Type' => 'application/json',
    ],
    RequestOptions::BODY => $serialized,
  ];

  /** @var \GuzzleHttp\ClientInterface $client */
  $client = $this
    ->getSession()
    ->getDriver()
    ->getClient()
    ->getClient();
  $response = $client
    ->request('POST', $this
    ->buildUrl('serialization_test', [
    'query' => [
      '_format' => 'json',
    ],
  ]), $request_options);
  $this
    ->assertSame(200, $response
    ->getStatusCode());
  $this
    ->assertSame('["foo","bar"]', (string) $response
    ->getBody());
}