function JsonApiFunctionalMultilingualTest::testPostTranslation

Same name in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalMultilingualTest.php \Drupal\Tests\jsonapi\Functional\JsonApiFunctionalMultilingualTest::testPostTranslation()
  2. 8.9.x core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalMultilingualTest.php \Drupal\Tests\jsonapi\Functional\JsonApiFunctionalMultilingualTest::testPostTranslation()
  3. 11.x core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalMultilingualTest.php \Drupal\Tests\jsonapi\Functional\JsonApiFunctionalMultilingualTest::testPostTranslation()

Tests creating a translation.

File

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

Class

JsonApiFunctionalMultilingualTest
Tests JSON:API multilingual support.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testPostTranslation() : void {
    $this->config('jsonapi.settings')
        ->set('read_only', FALSE)
        ->save(TRUE);
    $this->grantPermissions(Role::load(RoleInterface::ANONYMOUS_ID), [
        'bypass node access',
    ]);
    $title = 'Llamas FTW (ca)';
    $request_document = [
        'data' => [
            'type' => 'node--article',
            'attributes' => [
                'title' => $title,
                'langcode' => 'ca',
            ],
        ],
    ];
    $request_options = [];
    $request_options[RequestOptions::HEADERS]['Content-Type'] = 'application/vnd.api+json';
    // Specifying a langcode is forbidden by language_entity_field_access().
    $request_options[RequestOptions::BODY] = Json::encode($request_document);
    $response = $this->request('POST', Url::fromUri('base:/ca/jsonapi/node/article/'), $request_options);
    $this->assertSame(403, $response->getStatusCode());
    $document = $this->getDocumentFromResponse($response, FALSE);
    $this->assertSame('The current user is not allowed to POST the selected field (langcode).', $document['errors'][0]['detail']);
    // Omitting a langcode results in an entity in 'en': the default language of
    // the site.
    unset($request_document['data']['attributes']['langcode']);
    $request_options[RequestOptions::BODY] = Json::encode($request_document);
    $response = $this->request('POST', Url::fromUri('base:/ca/jsonapi/node/article/'), $request_options);
    $document = $this->getDocumentFromResponse($response);
    $this->assertSame(201, $response->getStatusCode());
    $this->assertSame($title, $document['data']['attributes']['title']);
    $this->assertSame('en', $document['data']['attributes']['langcode']);
    $this->assertSame([
        'en',
    ], array_keys(Node::load($document['data']['attributes']['drupal_internal__nid'])->getTranslationLanguages()));
    // Specifying a langcode is allowed once configured to be alterable. Now an
    // entity can be created with the specified langcode.
    ContentLanguageSettings::loadByEntityTypeBundle('node', 'article')->setLanguageAlterable(TRUE)
        ->save();
    $request_document['data']['attributes']['langcode'] = 'ca';
    $request_options[RequestOptions::BODY] = Json::encode($request_document);
    $response = $this->request('POST', Url::fromUri('base:/ca/jsonapi/node/article/'), $request_options);
    $document = $this->getDocumentFromResponse($response);
    $this->assertSame(201, $response->getStatusCode());
    $this->assertSame($title, $document['data']['attributes']['title']);
    $this->assertSame('ca', $document['data']['attributes']['langcode']);
    $this->assertSame([
        'ca',
    ], array_keys(Node::load($document['data']['attributes']['drupal_internal__nid'])->getTranslationLanguages()));
    // Same request, but sent to the URL without the language prefix.
    $response = $this->request('POST', Url::fromUri('base:/jsonapi/node/article/'), $request_options);
    $document = $this->getDocumentFromResponse($response);
    $this->assertSame(201, $response->getStatusCode());
    $this->assertSame($title, $document['data']['attributes']['title']);
    $this->assertSame('ca', $document['data']['attributes']['langcode']);
    $this->assertSame([
        'ca',
    ], array_keys(Node::load($document['data']['attributes']['drupal_internal__nid'])->getTranslationLanguages()));
}

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