View source
<?php
namespace Drupal\Tests\jsonapi\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Url;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\node\Entity\Node;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
use GuzzleHttp\RequestOptions;
class JsonApiFunctionalMultilingualTest extends JsonApiFunctionalTestBase {
public static $modules = [
'language',
'content_translation',
];
protected $defaultTheme = 'stark';
protected function setUp() {
parent::setUp();
$language = ConfigurableLanguage::createFromLangcode('ca');
$language
->save();
ConfigurableLanguage::createFromLangcode('ca-fr')
->save();
$this
->rebuildContainer();
\Drupal::configFactory()
->getEditable('language.negotiation')
->set('url.prefixes.ca', 'ca')
->set('url.prefixes.ca-fr', 'ca-fr')
->save();
ContentLanguageSettings::create([
'target_entity_type_id' => 'node',
'target_bundle' => 'article',
])
->setThirdPartySetting('content_translation', 'enabled', TRUE)
->save();
$this
->createDefaultContent(5, 5, TRUE, TRUE, static::IS_MULTILINGUAL, FALSE);
}
public function testReadMultilingual() {
$default_sort = [
'sort' => 'drupal_internal__nid',
];
$output = Json::decode($this
->drupalGet('/ca/jsonapi/node/article/' . $this->nodes[0]
->uuid(), [
'query' => [
'include' => 'field_tags,field_image',
] + $default_sort,
]));
$this
->assertEquals($this->nodes[0]
->getTranslation('ca')
->getTitle(), $output['data']['attributes']['title']);
$this
->assertSame('ca', $output['data']['attributes']['langcode']);
$included_tags = array_filter($output['included'], function ($entry) {
return $entry['type'] === 'taxonomy_term--tags';
});
$tag_name = $this->nodes[0]
->get('field_tags')->entity
->getTranslation('ca')
->getName();
$this
->assertEquals($tag_name, reset($included_tags)['attributes']['name']);
$alt = $this->nodes[0]
->getTranslation('ca')
->get('field_image')->alt;
$this
->assertSame($alt, $output['data']['relationships']['field_image']['data']['meta']['alt']);
$output = Json::decode($this
->drupalGet('/ca-fr/jsonapi/node/article/' . $this->nodes[0]
->uuid()));
$this
->assertEquals($this->nodes[0]
->getTranslation('ca')
->getTitle(), $output['data']['attributes']['title']);
$output = Json::decode($this
->drupalGet('/ca/jsonapi/node/article/' . $this->nodes[0]
->uuid(), [
'query' => $default_sort,
]));
$this
->assertEquals($this->nodes[0]
->getTranslation('ca')
->getTitle(), $output['data']['attributes']['title']);
$output = Json::decode($this
->drupalGet('/ca/jsonapi/node/article', [
'query' => $default_sort,
]));
$this
->assertEquals($this->nodes[0]
->getTranslation('ca')
->getTitle(), $output['data'][0]['attributes']['title']);
}
public function testPatchTranslation() {
$this
->config('jsonapi.settings')
->set('read_only', FALSE)
->save(TRUE);
$node = $this->nodes[0];
$uuid = $node
->uuid();
$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']);
$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());
$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']);
$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());
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']);
$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']);
$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']);
$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']);
$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']);
}
public function testPatchTranslationFallback() {
$this
->config('jsonapi.settings')
->set('read_only', FALSE)
->save(TRUE);
$node = $this->nodes[0];
$uuid = $node
->uuid();
$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']);
$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']);
}
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';
$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']);
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()));
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()));
$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()));
}
public function testDeleteMultilingual() {
$this
->config('jsonapi.settings')
->set('read_only', FALSE)
->save(TRUE);
$this
->grantPermissions(Role::load(RoleInterface::ANONYMOUS_ID), [
'bypass node access',
]);
$response = $this
->request('DELETE', Url::fromUri('base:/ca/jsonapi/node/article/' . $this->nodes[0]
->uuid()), []);
$this
->assertSame(405, $response
->getStatusCode());
$document = Json::decode((string) $response
->getBody());
$this
->assertSame('Deleting a resource object translation is not yet supported. See https://www.drupal.org/docs/8/modules/jsonapi/translations.', $document['errors'][0]['detail']);
$response = $this
->request('DELETE', Url::fromUri('base:/ca-fr/jsonapi/node/article/' . $this->nodes[0]
->uuid()), []);
$this
->assertSame(405, $response
->getStatusCode());
$document = Json::decode((string) $response
->getBody());
$this
->assertSame('Deleting a resource object translation is not yet supported. See https://www.drupal.org/docs/8/modules/jsonapi/translations.', $document['errors'][0]['detail']);
$response = $this
->request('DELETE', Url::fromUri('base:/jsonapi/node/article/' . $this->nodes[0]
->uuid()), []);
$this
->assertSame(204, $response
->getStatusCode());
$this
->assertNull(Node::load($this->nodes[0]
->id()));
}
}