diff --git a/core/modules/hal/tests/src/Functional/EntityResource/Node/NodeHalJsonCookieTranslationsTest.php b/core/modules/hal/tests/src/Functional/EntityResource/Node/NodeHalJsonCookieTranslationsTest.php new file mode 100644 index 0000000..1a107ce --- /dev/null +++ b/core/modules/hal/tests/src/Functional/EntityResource/Node/NodeHalJsonCookieTranslationsTest.php @@ -0,0 +1,155 @@ + 'languages:language_content', + 1 => 'languages:language_interface', + 2 => 'url.site', + 3 => 'user.permissions', + ]; + } + + /** + * {@inheritdoc} + */ + protected function setUpAuthorization($method) { + parent::setUpAuthorization($method); + $this->grantPermissionsToTestedRole(['translate any entity']); + } + + /** + * {@inheritdoc} + */ + protected function createEntity() { + if (!ConfigurableLanguage::load('de')) { + ConfigurableLanguage::createFromLangcode('de')->save(); + } + + /** @var \Drupal\node\NodeInterface $node */ + $node = parent::createEntity(); + $translation = $node->addTranslation('de', $node->toArray()); + $translation->get('title')->value = 'Lama'; + $node->save(); + return $node; + } + + /** + * {@inheritdoc} + */ + protected function getExpectedNormalizedEntity() { + $normalization = parent::getExpectedNormalizedEntity(); + + foreach ($this->entity->getFieldDefinitions() as $field_name => $field_definition) { + // @todo It seems that currently two items exist if the *storage* is + // translatable, should be when the field is? + if ($field_definition->getFieldStorageDefinition()->isTranslatable() && isset($normalization[$field_name])) { + $normalization[$field_name][1] = [ + 'lang' => 'de', + ] + $normalization[$field_name][0]; + } + } + + $normalization['title'][1]['value'] = 'Lama'; + $normalization['langcode'][1]['value'] = 'de'; + $normalization['default_langcode'][1]['value'] = '0'; + + $author = User::load($this->entity->getOwnerId()); + $normalization['_links'][$this->baseUrl . '/rest/relation/node/camelids/uid'][1] = [ + 'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json', + 'lang' => 'de', + ]; + $normalization['_embedded'][$this->baseUrl . '/rest/relation/node/camelids/uid'][1] = [ + '_links' => [ + 'self' => [ + 'href' => $this->baseUrl . '/user/' . $author->id() . '?_format=hal_json', + ], + 'type' => [ + 'href' => $this->baseUrl . '/rest/type/user/user', + ], + ], + 'uuid' => [ + ['value' => $author->uuid()] + ], + 'lang' => 'de', + ]; + + return $normalization; + } + + /** + * {@inheritdoc} + */ + public function testPost() { + parent::testPost(); + + // Create an entity with a translation. + $entity_body_with_a_translations = $this->serializer->encode($this->getExpectedNormalizedEntity(), static::$format); + + $request_options = []; + $request_options[RequestOptions::HEADERS]['Content-Type'] = static::$mimeType; + $request_options[RequestOptions::BODY] = $entity_body_with_a_translations; + $request_options = NestedArray::mergeDeep($request_options, $this->getAuthenticationRequestOptions('POST')); + + $url = $this->getPostUrl(); + $url->setOption('query', ['_format' => static::$format]); + + // 201 for well-formed request. + $response = $this->request('POST', $url, $request_options); + $this->assertResourceResponse(201, FALSE, $response); + /** @var \Drupal\node\NodeInterface $created_node */ + $created_node = $this->serializer->denormalize((string) $response->getBody(), static::$format); + $this->assertSame([$created_node->toUrl('canonical')->setAbsolute(TRUE)->toString()], $response->getHeader('Location')); + $this->assertFalse($response->hasHeader('X-Drupal-Cache')); + + $this->assertSame('Llama', $created_node->getTitle()); + $this->assertSame('Lama', $created_node->getTranslation('de')->getTitle()); + + + } + +} diff --git a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php index 73dfc62..42f99b6 100644 --- a/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php +++ b/core/modules/rest/tests/src/Functional/EntityResource/EntityResourceTestBase.php @@ -435,12 +435,16 @@ public function testGet() { // Finally, assert that the expected 'Link' headers are present. $this->assertArrayHasKey('Link', $response->getHeaders()); $link_relation_type_manager = $this->container->get('plugin.manager.link_relation_type'); - $expected_link_relation_headers = array_map(function ($rel) use ($link_relation_type_manager) { + $expected_link_relation_headers = array_filter(array_map(function ($rel) use ($link_relation_type_manager) { + // @todo What to do about content_translation link relationships? + if (strpos($rel, 'drupal:content-translation') !== FALSE) { + return FALSE; + } $definition = $link_relation_type_manager->getDefinition($rel, FALSE); return (!empty($definition['uri'])) ? $definition['uri'] : $rel; - }, array_keys($this->entity->getEntityType()->getLinkTemplates())); + }, array_keys($this->entity->getEntityType()->getLinkTemplates()))); $parse_rel_from_link_header = function ($value) use ($link_relation_type_manager) { $matches = []; if (preg_match('/rel="([^"]+)"/', $value, $matches) === 1) {