You are here

public function JsonApiFunctionalMultilingualTest::testPatchTranslationFallback in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalMultilingualTest.php \Drupal\Tests\jsonapi\Functional\JsonApiFunctionalMultilingualTest::testPatchTranslationFallback()

Tests updating a translation fallback.

File

core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalMultilingualTest.php, line 199

Class

JsonApiFunctionalMultilingualTest
Tests JSON:API multilingual support.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testPatchTranslationFallback() {
  $this
    ->config('jsonapi.settings')
    ->set('read_only', FALSE)
    ->save(TRUE);
  $node = $this->nodes[0];
  $uuid = $node
    ->uuid();

  // Assert the precondition: 'ca-fr' falls back to the 'ca' translation which
  // has a different title.
  $document = Json::decode($this
    ->drupalGet('/jsonapi/node/article/' . $uuid));
  $document_ca = Json::decode($this
    ->drupalGet('/ca/jsonapi/node/article/' . $uuid));
  $document_cafr = Json::decode($this
    ->drupalGet('/ca-fr/jsonapi/node/article/' . $uuid));
  $this
    ->assertSame('en', $document['data']['attributes']['langcode']);
  $this
    ->assertSame('ca', $document_ca['data']['attributes']['langcode']);
  $this
    ->assertSame('ca', $document_cafr['data']['attributes']['langcode']);
  $this
    ->assertSame($node
    ->getTitle(), $document['data']['attributes']['title']);
  $this
    ->assertSame($node
    ->getTitle() . ' (ca)', $document_ca['data']['attributes']['title']);
  $this
    ->assertSame($node
    ->getTitle() . ' (ca)', $document_cafr['data']['attributes']['title']);

  // PATCH the 'ca-fr' translation.
  $this
    ->grantPermissions(Role::load(RoleInterface::ANONYMOUS_ID), [
    'bypass node access',
  ]);
  $request_options = [];
  $request_options[RequestOptions::HEADERS]['Content-Type'] = 'application/vnd.api+json';
  $request_options[RequestOptions::BODY] = Json::encode([
    'data' => [
      'type' => 'node--article',
      'id' => $uuid,
      'attributes' => [
        'title' => $document_cafr['data']['attributes']['title'] . ' UPDATED',
      ],
    ],
  ]);
  $response = $this
    ->request('PATCH', Url::fromUri('base:/ca-fr/jsonapi/node/article/' . $this->nodes[0]
    ->uuid()), $request_options);
  $this
    ->assertSame(405, $response
    ->getStatusCode());
  $document = Json::decode((string) $response
    ->getBody());
  $this
    ->assertSame('The requested translation of the resource object does not exist, instead modify one of the translations that do exist: ca, en.', $document['errors'][0]['detail']);
}