Chris@0: installEntitySchema('entity_test'); Chris@0: $this->installEntitySchema('configurable_language'); Chris@0: \Drupal::service('router.builder')->rebuild(); 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: ConfigurableLanguage::create(['id' => 'es'])->save(); Chris@0: ConfigurableLanguage::create(['id' => 'fr'])->save(); Chris@0: Chris@0: $config = $this->config('language.negotiation'); Chris@0: $config->set('url.prefixes', ['en' => 'en', 'es' => 'es', 'fr' => 'fr']) Chris@0: ->save(); Chris@0: Chris@0: \Drupal::service('kernel')->rebuildContainer(); Chris@0: Chris@0: $this->createTranslatableEntity(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensures that entity URLs in a language have the right language prefix. Chris@0: */ Chris@0: public function testEntityUrlLanguage() { Chris@18: $this->assertTrue(strpos($this->entity->toUrl()->toString(), '/en/entity_test/' . $this->entity->id()) !== FALSE); Chris@18: $this->assertTrue(strpos($this->entity->getTranslation('es')->toUrl()->toString(), '/es/entity_test/' . $this->entity->id()) !== FALSE); Chris@18: $this->assertTrue(strpos($this->entity->getTranslation('fr')->toUrl()->toString(), '/fr/entity_test/' . $this->entity->id()) !== FALSE); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Ensures correct entity URLs with the method language-content-entity enabled. Chris@0: * Chris@0: * Test case with the method language-content-entity enabled and configured Chris@0: * with higher and also with lower priority than the method language-url. Chris@0: */ Chris@0: public function testEntityUrlLanguageWithLanguageContentEnabled() { Chris@0: // Define the method language-content-entity with a higher priority than Chris@0: // language-url. 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: LanguageNegotiationContentEntity::METHOD_ID => 0, Chris@17: LanguageNegotiationUrl::METHOD_ID => 1, Chris@0: ]); Chris@0: $config->save(); Chris@0: Chris@0: // Without being on an content entity route the default entity URL tests Chris@0: // should still pass. Chris@0: $this->testEntityUrlLanguage(); 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: // 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@18: $this->assertTrue(strpos($this->entity->toUrl('canonical')->toString(), '/en/entity_test/' . $this->entity->id() . '?' . LanguageNegotiationContentEntity::QUERY_PARAMETER . '=en') !== FALSE); Chris@18: $this->assertTrue(strpos($this->entity->getTranslation('es')->toUrl('canonical')->toString(), '/en/entity_test/' . $this->entity->id() . '?' . LanguageNegotiationContentEntity::QUERY_PARAMETER . '=es') !== FALSE); Chris@18: $this->assertTrue(strpos($this->entity->getTranslation('fr')->toUrl('canonical')->toString(), '/en/entity_test/' . $this->entity->id() . '?' . LanguageNegotiationContentEntity::QUERY_PARAMETER . '=fr') !== FALSE); Chris@0: 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->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: // The default entity URL tests should pass again with the current Chris@0: // configuration. Chris@0: $this->testEntityUrlLanguage(); 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: }