Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / language / tests / src / Kernel / EntityUrlLanguageTest.php
1 <?php
2
3 namespace Drupal\Tests\language\Kernel;
4
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;
13
14 /**
15  * Tests the language of entity URLs.
16  * @group language
17  */
18 class EntityUrlLanguageTest extends LanguageTestBase {
19
20   /**
21    * Modules to enable.
22    *
23    * @var array
24    */
25   public static $modules = ['entity_test', 'user'];
26
27   /**
28    * The entity being used for testing.
29    *
30    * @var \Drupal\Core\Entity\ContentEntityInterface
31    */
32   protected $entity;
33
34   protected function setUp() {
35     parent::setUp();
36
37     $this->installEntitySchema('entity_test');
38     $this->installEntitySchema('configurable_language');
39     \Drupal::service('router.builder')->rebuild();
40
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();
45
46     $config = $this->config('language.negotiation');
47     $config->set('url.prefixes', ['en' => 'en', 'es' => 'es', 'fr' => 'fr'])
48       ->save();
49
50     \Drupal::service('kernel')->rebuildContainer();
51
52     $this->createTranslatableEntity();
53   }
54
55   /**
56    * Ensures that entity URLs in a language have the right language prefix.
57    */
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);
62   }
63
64   /**
65    * Ensures correct entity URLs with the method language-content-entity enabled.
66    *
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.
69    */
70   public function testEntityUrlLanguageWithLanguageContentEnabled() {
71     // Define the method language-content-entity with a higher priority than
72     // language-url.
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,
78     ]);
79     $config->save();
80
81     // Without being on an content entity route the default entity URL tests
82     // should still pass.
83     $this->testEntityUrlLanguage();
84
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');
88
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);
95
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,
102     ]);
103     $config->save();
104
105     // The default entity URL tests should pass again with the current
106     // configuration.
107     $this->testEntityUrlLanguage();
108   }
109
110   /**
111    * Creates a translated entity.
112    */
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();
118   }
119
120   /**
121    * Sets the current request to a specific path with the corresponding route.
122    *
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
127    *   created.
128    */
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);
134   }
135
136 }