Chris@0: 'es'])->save(); Chris@0: ConfigurableLanguage::create(['id' => 'fr'])->save(); Chris@0: Chris@0: // In order to reflect the changes for a multilingual site in the container Chris@0: // we have to rebuild it. Chris@0: $this->rebuildContainer(); Chris@0: Chris@0: $this->createTranslatableEntity(); Chris@0: Chris@0: $user = $this->drupalCreateUser(['view test entity']); Chris@0: $this->drupalLogin($user); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests default with content language remaining same as interface language. Chris@0: */ Chris@0: public function testDefaultConfiguration() { Chris@0: $translation = $this->entity; Chris@18: $this->drupalGet($translation->toUrl()); Chris@0: $last = $this->container->get('state')->get('language_test.language_negotiation_last'); Chris@0: $last_content_language = $last[LanguageInterface::TYPE_CONTENT]; Chris@0: $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE]; Chris@0: $this->assertTrue(($last_interface_language == $last_content_language) && ($last_content_language == $translation->language()->getId()), new FormattableMarkup('Interface language %interface_language and Content language %content_language are the same as the translation language %translation_language of the entity.', ['%interface_language' => $last_interface_language, '%content_language' => $last_content_language, '%translation_language' => $translation->language()->getId()])); Chris@0: Chris@0: $translation = $this->entity->getTranslation('es'); Chris@18: $this->drupalGet($translation->toUrl()); Chris@0: $last = $this->container->get('state')->get('language_test.language_negotiation_last'); Chris@0: $last_content_language = $last[LanguageInterface::TYPE_CONTENT]; Chris@0: $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE]; Chris@0: $this->assertTrue(($last_interface_language == $last_content_language) && ($last_content_language == $translation->language()->getId()), new FormattableMarkup('Interface language %interface_language and Content language %content_language are the same as the translation language %translation_language of the entity.', ['%interface_language' => $last_interface_language, '%content_language' => $last_content_language, '%translation_language' => $translation->language()->getId()])); Chris@0: Chris@0: $translation = $this->entity->getTranslation('fr'); Chris@18: $this->drupalGet($translation->toUrl()); Chris@0: $last = $this->container->get('state')->get('language_test.language_negotiation_last'); Chris@0: $last_content_language = $last[LanguageInterface::TYPE_CONTENT]; Chris@0: $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE]; Chris@0: $this->assertTrue(($last_interface_language == $last_content_language) && ($last_content_language == $translation->language()->getId()), new FormattableMarkup('Interface language %interface_language and Content language %content_language are the same as the translation language %translation_language of the entity.', ['%interface_language' => $last_interface_language, '%content_language' => $last_content_language, '%translation_language' => $translation->language()->getId()])); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests enabling the language negotiator language_content_entity. Chris@0: */ Chris@0: public function testEnabledLanguageContentNegotiator() { Chris@0: // Define the method language-url with a higher priority than Chris@0: // language-content-entity. This configuration should match the default one, Chris@0: // where the language-content-entity is turned off. Chris@0: $config = $this->config('language.types'); Chris@0: $config->set('configurable', [LanguageInterface::TYPE_INTERFACE, LanguageInterface::TYPE_CONTENT]); Chris@0: $config->set('negotiation.language_content.enabled', [ Chris@0: LanguageNegotiationUrl::METHOD_ID => 0, Chris@17: LanguageNegotiationContentEntity::METHOD_ID => 1, Chris@0: ]); Chris@0: $config->save(); Chris@0: Chris@0: // In order to reflect the changes for a multilingual site in the container Chris@0: // we have to rebuild it. Chris@0: $this->rebuildContainer(); Chris@0: Chris@0: // The tests for the default configuration should still pass. Chris@0: $this->testDefaultConfiguration(); Chris@0: Chris@0: // Define the method language-content-entity with a higher priority than Chris@0: // language-url. Chris@0: $config->set('negotiation.language_content.enabled', [ Chris@0: LanguageNegotiationContentEntity::METHOD_ID => 0, Chris@17: LanguageNegotiationUrl::METHOD_ID => 1, Chris@0: ]); Chris@0: $config->save(); Chris@0: Chris@0: // In order to reflect the changes for a multilingual site in the container Chris@0: // we have to rebuild it. Chris@0: $this->rebuildContainer(); Chris@0: Chris@0: // The method language-content-entity should run before language-url and Chris@0: // append query parameter for the content language and prevent language-url Chris@0: // from overwriting the URL. Chris@0: $default_site_langcode = $this->config('system.site')->get('default_langcode'); Chris@0: Chris@0: // Now switching to an entity route, so that the URL links are generated Chris@0: // while being on an entity route. Chris@0: $this->setCurrentRequestForRoute('/entity_test/{entity_test}', 'entity.entity_test.canonical'); Chris@0: Chris@0: $translation = $this->entity; Chris@18: $this->drupalGet($translation->toUrl()); Chris@0: $last = $this->container->get('state')->get('language_test.language_negotiation_last'); Chris@0: $last_content_language = $last[LanguageInterface::TYPE_CONTENT]; Chris@0: $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE]; Chris@0: $this->assertTrue(($last_interface_language == $default_site_langcode) && ($last_interface_language == $last_content_language) && ($last_content_language == $translation->language()->getId()), 'Interface language and Content language are the same as the default translation language of the entity.'); Chris@0: $this->assertTrue($last_interface_language == $default_site_langcode, 'Interface language did not change from the default site language.'); Chris@0: $this->assertTrue($last_content_language == $translation->language()->getId(), 'Content language matches the current entity translation language.'); Chris@0: Chris@0: $translation = $this->entity->getTranslation('es'); Chris@18: $this->drupalGet($translation->toUrl()); Chris@0: $last = $this->container->get('state')->get('language_test.language_negotiation_last'); Chris@0: $last_content_language = $last[LanguageInterface::TYPE_CONTENT]; Chris@0: $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE]; Chris@0: $this->assertTrue($last_interface_language == $default_site_langcode, 'Interface language did not change from the default site language.'); Chris@0: $this->assertTrue($last_content_language == $translation->language()->getId(), 'Content language matches the current entity translation language.'); Chris@0: Chris@0: $translation = $this->entity->getTranslation('fr'); Chris@18: $this->drupalGet($translation->toUrl()); Chris@0: $last = $this->container->get('state')->get('language_test.language_negotiation_last'); Chris@0: $last_content_language = $last[LanguageInterface::TYPE_CONTENT]; Chris@0: $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE]; Chris@0: $this->assertTrue($last_interface_language == $default_site_langcode, 'Interface language did not change from the default site language.'); Chris@0: $this->assertTrue($last_content_language == $translation->language()->getId(), 'Content language matches the current entity translation language.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a translated entity. Chris@0: */ Chris@0: protected function createTranslatableEntity() { Chris@0: $this->entity = EntityTest::create(); Chris@0: $this->entity->addTranslation('es', ['name' => 'name spanish']); Chris@0: $this->entity->addTranslation('fr', ['name' => 'name french']); Chris@0: $this->entity->save(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the current request to a specific path with the corresponding route. Chris@0: * Chris@0: * @param string $path Chris@0: * The path for which the current request should be created. Chris@0: * @param string $route_name Chris@0: * The route name for which the route object for the request should be Chris@0: * created. Chris@0: */ Chris@0: protected function setCurrentRequestForRoute($path, $route_name) { Chris@0: $request = Request::create($path); Chris@0: $request->attributes->set(RouteObjectInterface::ROUTE_NAME, $route_name); Chris@0: $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route($path)); Chris@0: $this->container->get('request_stack')->push($request); Chris@0: } Chris@0: Chris@0: }