function JsonApiPatchRegressionTest::testPatchToIncludeUrlDoesNotReturnIncludeFromIssue3026030

Same name and namespace in other branches
  1. 11.x core/modules/jsonapi/tests/src/Functional/JsonApiPatchRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiPatchRegressionTest::testPatchToIncludeUrlDoesNotReturnIncludeFromIssue3026030()

Ensure includes are respected even when PATCHing.

See also

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

File

core/modules/jsonapi/tests/src/Functional/JsonApiPatchRegressionTest.php, line 292

Class

JsonApiPatchRegressionTest
JSON:API regression tests.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testPatchToIncludeUrlDoesNotReturnIncludeFromIssue3026030() : void {
  $this->config('jsonapi.settings')
    ->set('read_only', FALSE)
    ->save(TRUE);
  // Set up data model.
  $this->drupalCreateContentType([
    'type' => 'page',
  ]);
  $this->rebuildAll();
  // Create data.
  $user = $this->drupalCreateUser([
    'bypass node access',
  ]);
  $page = Node::create([
    'title' => 'original',
    'type' => 'page',
    'uid' => $user->id(),
  ]);
  $page->save();
  // Test.
  $url = Url::fromUri(sprintf('internal:/jsonapi/node/page/%s/?include=uid', $page->uuid()));
  $request_options = [
    RequestOptions::HEADERS => [
      'Content-Type' => 'application/vnd.api+json',
      'Accept' => 'application/vnd.api+json',
    ],
    RequestOptions::AUTH => [
      $user->getAccountName(),
      $user->pass_raw,
    ],
    RequestOptions::JSON => [
      'data' => [
        'type' => 'node--page',
        'id' => $page->uuid(),
        'attributes' => [
          'title' => 'modified',
        ],
      ],
    ],
  ];
  $response = $this->request('PATCH', $url, $request_options);
  $document = $this->getDocumentFromResponse($response);
  $this->assertSame(200, $response->getStatusCode());
  $this->assertArrayHasKey('included', $document);
  $this->assertSame($user->label(), $document['included'][0]['attributes']['name']);
}

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