function JsonApiFunctionalMultilingualTest::testPatchTranslation
Same name in other branches
- 8.9.x core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalMultilingualTest.php \Drupal\Tests\jsonapi\Functional\JsonApiFunctionalMultilingualTest::testPatchTranslation()
- 10 core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalMultilingualTest.php \Drupal\Tests\jsonapi\Functional\JsonApiFunctionalMultilingualTest::testPatchTranslation()
- 11.x core/modules/jsonapi/tests/src/Functional/JsonApiFunctionalMultilingualTest.php \Drupal\Tests\jsonapi\Functional\JsonApiFunctionalMultilingualTest::testPatchTranslation()
Tests updating a translation.
File
-
core/
modules/ jsonapi/ tests/ src/ Functional/ JsonApiFunctionalMultilingualTest.php, line 100
Class
- JsonApiFunctionalMultilingualTest
- Tests JSON:API multilingual support.
Namespace
Drupal\Tests\jsonapi\FunctionalCode
public function testPatchTranslation() {
$this->config('jsonapi.settings')
->set('read_only', FALSE)
->save(TRUE);
$node = $this->nodes[0];
$uuid = $node->uuid();
// Assert the precondition: the 'ca' translation has a different title.
$document = Json::decode($this->drupalGet('/jsonapi/node/article/' . $uuid));
$document_ca = Json::decode($this->drupalGet('/ca/jsonapi/node/article/' . $uuid));
$this->assertSame('en', $document['data']['attributes']['langcode']);
$this->assertSame('ca', $document_ca['data']['attributes']['langcode']);
$this->assertSame($node->getTitle(), $document['data']['attributes']['title']);
$this->assertSame($node->getTitle() . ' (ca)', $document_ca['data']['attributes']['title']);
// PATCH the 'ca' 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['data']['attributes']['title'] . ' UPDATED',
],
],
]);
$response = $this->request('PATCH', Url::fromUri('base:/ca/jsonapi/node/article/' . $this->nodes[0]
->uuid()), $request_options);
$this->assertSame(200, $response->getStatusCode());
// Assert the postcondition: only the 'ca' translation has an updated title.
$document_updated = Json::decode($this->drupalGet('/jsonapi/node/article/' . $uuid));
$document_ca_updated = Json::decode($this->drupalGet('/ca/jsonapi/node/article/' . $uuid));
$this->assertSame('en', $document_updated['data']['attributes']['langcode']);
$this->assertSame('ca', $document_ca_updated['data']['attributes']['langcode']);
$this->assertSame($node->getTitle(), $document_updated['data']['attributes']['title']);
$this->assertSame($node->getTitle() . ' (ca) UPDATED', $document_ca_updated['data']['attributes']['title']);
// Specifying a langcode is not allowed by default.
$request_options[RequestOptions::BODY] = Json::encode([
'data' => [
'type' => 'node--article',
'id' => $uuid,
'attributes' => [
'langcode' => 'ca-fr',
],
],
]);
$response = $this->request('PATCH', Url::fromUri('base:/ca/jsonapi/node/article/' . $this->nodes[0]
->uuid()), $request_options);
$this->assertSame(403, $response->getStatusCode());
// Specifying a langcode is allowed once configured to be alterable. But
// modifying the language of a non-default translation is still not allowed.
ContentLanguageSettings::loadByEntityTypeBundle('node', 'article')->setLanguageAlterable(TRUE)
->save();
$response = $this->request('PATCH', Url::fromUri('base:/ca/jsonapi/node/article/' . $this->nodes[0]
->uuid()), $request_options);
$this->assertSame(500, $response->getStatusCode());
$document = Json::decode((string) $response->getBody());
$this->assertSame('The translation language cannot be changed (ca).', $document['errors'][0]['detail']);
// Changing the langcode of the default ('en') translation is possible:
// first verify that it currently is 'en', then change it to 'ca-fr', and
// verify that the title is unchanged, but the langcode is updated.
$response = $this->request('GET', Url::fromUri('base:/jsonapi/node/article/' . $this->nodes[0]
->uuid()), $request_options);
$this->assertSame(200, $response->getStatusCode());
$document = Json::decode((string) $response->getBody());
$this->assertSame($node->getTitle(), $document['data']['attributes']['title']);
$this->assertSame('en', $document['data']['attributes']['langcode']);
$response = $this->request('PATCH', Url::fromUri('base:/jsonapi/node/article/' . $this->nodes[0]
->uuid()), $request_options);
$this->assertSame(200, $response->getStatusCode());
$document = Json::decode((string) $response->getBody());
$this->assertSame($node->getTitle(), $document['data']['attributes']['title']);
$this->assertSame('ca-fr', $document['data']['attributes']['langcode']);
// Finally: assert the postcondition of all installed languages.
// - When GETting the 'en' translation, we get 'ca-fr', since the 'en'
// translation doesn't exist anymore.
$response = $this->request('GET', Url::fromUri('base:/jsonapi/node/article/' . $this->nodes[0]
->uuid()), $request_options);
$document = Json::decode((string) $response->getBody());
$this->assertSame('ca-fr', $document['data']['attributes']['langcode']);
$this->assertSame($node->getTitle(), $document['data']['attributes']['title']);
// - When GETting the 'ca' translation, we still get the 'ca' one.
$response = $this->request('GET', Url::fromUri('base:/ca/jsonapi/node/article/' . $this->nodes[0]
->uuid()), $request_options);
$document = Json::decode((string) $response->getBody());
$this->assertSame('ca', $document['data']['attributes']['langcode']);
$this->assertSame($node->getTitle() . ' (ca) UPDATED', $document['data']['attributes']['title']);
// - When GETting the 'ca-fr' translation, we now get the default
// translation.
$response = $this->request('GET', Url::fromUri('base:/ca-fr/jsonapi/node/article/' . $this->nodes[0]
->uuid()), $request_options);
$document = Json::decode((string) $response->getBody());
$this->assertSame('ca-fr', $document['data']['attributes']['langcode']);
$this->assertSame($node->getTitle(), $document['data']['attributes']['title']);
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.