3 namespace Drupal\Tests\language\Kernel;
5 use Drupal\Core\Language\LanguageInterface;
6 use Drupal\entity_test\Entity\EntityTest;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationContentEntity;
9 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
10 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
11 use Symfony\Component\HttpFoundation\Request;
12 use Symfony\Component\Routing\Route;
15 * Tests the language of entity URLs.
18 class EntityUrlLanguageTest extends LanguageTestBase {
25 public static $modules = ['entity_test', 'user'];
28 * The entity being used for testing.
30 * @var \Drupal\Core\Entity\ContentEntityInterface
34 protected function setUp() {
37 $this->installEntitySchema('entity_test');
38 $this->installEntitySchema('configurable_language');
39 \Drupal::service('router.builder')->rebuild();
41 // In order to reflect the changes for a multilingual site in the container
42 // we have to rebuild it.
43 ConfigurableLanguage::create(['id' => 'es'])->save();
44 ConfigurableLanguage::create(['id' => 'fr'])->save();
46 $config = $this->config('language.negotiation');
47 $config->set('url.prefixes', ['en' => 'en', 'es' => 'es', 'fr' => 'fr'])
50 \Drupal::service('kernel')->rebuildContainer();
52 $this->createTranslatableEntity();
56 * Ensures that entity URLs in a language have the right language prefix.
58 public function testEntityUrlLanguage() {
59 $this->assertTrue(strpos($this->entity->urlInfo()->toString(), '/en/entity_test/' . $this->entity->id()) !== FALSE);
60 $this->assertTrue(strpos($this->entity->getTranslation('es')->urlInfo()->toString(), '/es/entity_test/' . $this->entity->id()) !== FALSE);
61 $this->assertTrue(strpos($this->entity->getTranslation('fr')->urlInfo()->toString(), '/fr/entity_test/' . $this->entity->id()) !== FALSE);
65 * Ensures correct entity URLs with the method language-content-entity enabled.
67 * Test case with the method language-content-entity enabled and configured
68 * with higher and also with lower priority than the method language-url.
70 public function testEntityUrlLanguageWithLanguageContentEnabled() {
71 // Define the method language-content-entity with a higher priority than
73 $config = $this->config('language.types');
74 $config->set('configurable', [LanguageInterface::TYPE_INTERFACE, LanguageInterface::TYPE_CONTENT]);
75 $config->set('negotiation.language_content.enabled', [
76 LanguageNegotiationContentEntity::METHOD_ID => 0,
77 LanguageNegotiationUrl::METHOD_ID => 1,
81 // Without being on an content entity route the default entity URL tests
83 $this->testEntityUrlLanguage();
85 // Now switching to an entity route, so that the URL links are generated
86 // while being on an entity route.
87 $this->setCurrentRequestForRoute('/entity_test/{entity_test}', 'entity.entity_test.canonical');
89 // The method language-content-entity should run before language-url and
90 // append query parameter for the content language and prevent language-url
91 // from overwriting the url.
92 $this->assertTrue(strpos($this->entity->urlInfo('canonical')->toString(), '/en/entity_test/' . $this->entity->id() . '?' . LanguageNegotiationContentEntity::QUERY_PARAMETER . '=en') !== FALSE);
93 $this->assertTrue(strpos($this->entity->getTranslation('es')->urlInfo('canonical')->toString(), '/en/entity_test/' . $this->entity->id() . '?' . LanguageNegotiationContentEntity::QUERY_PARAMETER . '=es') !== FALSE);
94 $this->assertTrue(strpos($this->entity->getTranslation('fr')->urlInfo('canonical')->toString(), '/en/entity_test/' . $this->entity->id() . '?' . LanguageNegotiationContentEntity::QUERY_PARAMETER . '=fr') !== FALSE);
96 // Define the method language-url with a higher priority than
97 // language-content-entity. This configuration should match the default one,
98 // where the language-content-entity is turned off.
99 $config->set('negotiation.language_content.enabled', [
100 LanguageNegotiationUrl::METHOD_ID => 0,
101 LanguageNegotiationContentEntity::METHOD_ID => 1,
105 // The default entity URL tests should pass again with the current
107 $this->testEntityUrlLanguage();
111 * Creates a translated entity.
113 protected function createTranslatableEntity() {
114 $this->entity = EntityTest::create();
115 $this->entity->addTranslation('es', ['name' => 'name spanish']);
116 $this->entity->addTranslation('fr', ['name' => 'name french']);
117 $this->entity->save();
121 * Sets the current request to a specific path with the corresponding route.
123 * @param string $path
124 * The path for which the current request should be created.
125 * @param string $route_name
126 * The route name for which the route object for the request should be
129 protected function setCurrentRequestForRoute($path, $route_name) {
130 $request = Request::create($path);
131 $request->attributes->set(RouteObjectInterface::ROUTE_NAME, $route_name);
132 $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route($path));
133 $this->container->get('request_stack')->push($request);