You are here

public function JsonApiFunctionalMultilingualTest::testPostTranslation in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalMultilingualTest.php \Drupal\Tests\jsonapi\Functional\JsonApiFunctionalMultilingualTest::testPostTranslation()
  2. 9 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 240

Class

JsonApiFunctionalMultilingualTest
Tests JSON:API multilingual support.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testPostTranslation() {
  $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 = Json::decode((string) $response
    ->getBody());
  $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);
  $this
    ->assertSame(201, $response
    ->getStatusCode());
  $document = Json::decode((string) $response
    ->getBody());
  $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);
  $this
    ->assertSame(201, $response
    ->getStatusCode());
  $document = Json::decode((string) $response
    ->getBody());
  $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);
  $this
    ->assertSame(201, $response
    ->getStatusCode());
  $document = Json::decode((string) $response
    ->getBody());
  $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()));
}