function ResourceTest::testSerializationClassIsOptional

Same name in other branches
  1. 9 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()
  3. 11.x 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());
}

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