You are here

public function JsonApiRegressionTest::testPostToIncludeUrlDoesNotReturnIncludeFromIssue3026030 in JSON:API 8.2

Ensure includes are respected even when POSTing.

See also

https://www.drupal.org/project/jsonapi/issues/3026030

File

tests/src/Functional/JsonApiRegressionTest.php, line 789

Class

JsonApiRegressionTest
JSON:API regression tests.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testPostToIncludeUrlDoesNotReturnIncludeFromIssue3026030() {
  $this
    ->config('jsonapi.settings')
    ->set('read_only', FALSE)
    ->save(TRUE);

  // Set up data model.
  $this
    ->drupalCreateContentType([
    'type' => 'page',
  ]);
  $this
    ->rebuildAll();

  // Test.
  $user = $this
    ->drupalCreateUser([
    'bypass node access',
  ]);
  $url = Url::fromUri('internal:/jsonapi/node/page?include=uid');
  $request_options = [
    RequestOptions::HEADERS => [
      'Content-Type' => 'application/vnd.api+json',
      'Accept' => 'application/vnd.api+json',
    ],
    RequestOptions::AUTH => [
      $user
        ->getUsername(),
      $user->pass_raw,
    ],
    RequestOptions::JSON => [
      'data' => [
        'type' => 'node--page',
        'attributes' => [
          'title' => 'test',
        ],
      ],
    ],
  ];
  $response = $this
    ->request('POST', $url, $request_options);
  $this
    ->assertSame(201, $response
    ->getStatusCode());
  $doc = Json::decode((string) $response
    ->getBody());
  $this
    ->assertArrayHasKey('included', $doc);
  $this
    ->assertSame($user
    ->label(), $doc['included'][0]['attributes']['name']);
}