function JsonApiFunctionalMultilingualTest::testPatchTranslationFallback

Same name in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalMultilingualTest.php \Drupal\Tests\jsonapi\Functional\JsonApiFunctionalMultilingualTest::testPatchTranslationFallback()
  2. 8.9.x core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalMultilingualTest.php \Drupal\Tests\jsonapi\Functional\JsonApiFunctionalMultilingualTest::testPatchTranslationFallback()
  3. 11.x 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 201

Class

JsonApiFunctionalMultilingualTest
Tests JSON:API multilingual support.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testPatchTranslationFallback() : void {
    $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_ca_fr = 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_ca_fr['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_ca_fr['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_ca_fr['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 = $this->getDocumentFromResponse($response, FALSE);
    $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']);
}

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