annotate core/modules/language/tests/src/Kernel/EntityUrlLanguageTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents af1871eacc83
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\language\Kernel;
Chris@0 4
Chris@0 5 use Drupal\Core\Language\LanguageInterface;
Chris@0 6 use Drupal\entity_test\Entity\EntityTest;
Chris@0 7 use Drupal\language\Entity\ConfigurableLanguage;
Chris@0 8 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationContentEntity;
Chris@0 9 use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
Chris@0 10 use Symfony\Cmf\Component\Routing\RouteObjectInterface;
Chris@0 11 use Symfony\Component\HttpFoundation\Request;
Chris@0 12 use Symfony\Component\Routing\Route;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Tests the language of entity URLs.
Chris@0 16 * @group language
Chris@0 17 */
Chris@0 18 class EntityUrlLanguageTest extends LanguageTestBase {
Chris@0 19
Chris@0 20 /**
Chris@0 21 * Modules to enable.
Chris@0 22 *
Chris@0 23 * @var array
Chris@0 24 */
Chris@0 25 public static $modules = ['entity_test', 'user'];
Chris@0 26
Chris@0 27 /**
Chris@0 28 * The entity being used for testing.
Chris@0 29 *
Chris@0 30 * @var \Drupal\Core\Entity\ContentEntityInterface
Chris@0 31 */
Chris@0 32 protected $entity;
Chris@0 33
Chris@0 34 protected function setUp() {
Chris@0 35 parent::setUp();
Chris@0 36
Chris@0 37 $this->installEntitySchema('entity_test');
Chris@0 38 $this->installEntitySchema('configurable_language');
Chris@0 39 \Drupal::service('router.builder')->rebuild();
Chris@0 40
Chris@0 41 // In order to reflect the changes for a multilingual site in the container
Chris@0 42 // we have to rebuild it.
Chris@0 43 ConfigurableLanguage::create(['id' => 'es'])->save();
Chris@0 44 ConfigurableLanguage::create(['id' => 'fr'])->save();
Chris@0 45
Chris@0 46 $config = $this->config('language.negotiation');
Chris@0 47 $config->set('url.prefixes', ['en' => 'en', 'es' => 'es', 'fr' => 'fr'])
Chris@0 48 ->save();
Chris@0 49
Chris@0 50 \Drupal::service('kernel')->rebuildContainer();
Chris@0 51
Chris@0 52 $this->createTranslatableEntity();
Chris@0 53 }
Chris@0 54
Chris@0 55 /**
Chris@0 56 * Ensures that entity URLs in a language have the right language prefix.
Chris@0 57 */
Chris@0 58 public function testEntityUrlLanguage() {
Chris@18 59 $this->assertTrue(strpos($this->entity->toUrl()->toString(), '/en/entity_test/' . $this->entity->id()) !== FALSE);
Chris@18 60 $this->assertTrue(strpos($this->entity->getTranslation('es')->toUrl()->toString(), '/es/entity_test/' . $this->entity->id()) !== FALSE);
Chris@18 61 $this->assertTrue(strpos($this->entity->getTranslation('fr')->toUrl()->toString(), '/fr/entity_test/' . $this->entity->id()) !== FALSE);
Chris@0 62 }
Chris@0 63
Chris@0 64 /**
Chris@0 65 * Ensures correct entity URLs with the method language-content-entity enabled.
Chris@0 66 *
Chris@0 67 * Test case with the method language-content-entity enabled and configured
Chris@0 68 * with higher and also with lower priority than the method language-url.
Chris@0 69 */
Chris@0 70 public function testEntityUrlLanguageWithLanguageContentEnabled() {
Chris@0 71 // Define the method language-content-entity with a higher priority than
Chris@0 72 // language-url.
Chris@0 73 $config = $this->config('language.types');
Chris@0 74 $config->set('configurable', [LanguageInterface::TYPE_INTERFACE, LanguageInterface::TYPE_CONTENT]);
Chris@0 75 $config->set('negotiation.language_content.enabled', [
Chris@0 76 LanguageNegotiationContentEntity::METHOD_ID => 0,
Chris@17 77 LanguageNegotiationUrl::METHOD_ID => 1,
Chris@0 78 ]);
Chris@0 79 $config->save();
Chris@0 80
Chris@0 81 // Without being on an content entity route the default entity URL tests
Chris@0 82 // should still pass.
Chris@0 83 $this->testEntityUrlLanguage();
Chris@0 84
Chris@0 85 // Now switching to an entity route, so that the URL links are generated
Chris@0 86 // while being on an entity route.
Chris@0 87 $this->setCurrentRequestForRoute('/entity_test/{entity_test}', 'entity.entity_test.canonical');
Chris@0 88
Chris@0 89 // The method language-content-entity should run before language-url and
Chris@0 90 // append query parameter for the content language and prevent language-url
Chris@0 91 // from overwriting the url.
Chris@18 92 $this->assertTrue(strpos($this->entity->toUrl('canonical')->toString(), '/en/entity_test/' . $this->entity->id() . '?' . LanguageNegotiationContentEntity::QUERY_PARAMETER . '=en') !== FALSE);
Chris@18 93 $this->assertTrue(strpos($this->entity->getTranslation('es')->toUrl('canonical')->toString(), '/en/entity_test/' . $this->entity->id() . '?' . LanguageNegotiationContentEntity::QUERY_PARAMETER . '=es') !== FALSE);
Chris@18 94 $this->assertTrue(strpos($this->entity->getTranslation('fr')->toUrl('canonical')->toString(), '/en/entity_test/' . $this->entity->id() . '?' . LanguageNegotiationContentEntity::QUERY_PARAMETER . '=fr') !== FALSE);
Chris@0 95
Chris@0 96 // Define the method language-url with a higher priority than
Chris@0 97 // language-content-entity. This configuration should match the default one,
Chris@0 98 // where the language-content-entity is turned off.
Chris@0 99 $config->set('negotiation.language_content.enabled', [
Chris@0 100 LanguageNegotiationUrl::METHOD_ID => 0,
Chris@17 101 LanguageNegotiationContentEntity::METHOD_ID => 1,
Chris@0 102 ]);
Chris@0 103 $config->save();
Chris@0 104
Chris@0 105 // The default entity URL tests should pass again with the current
Chris@0 106 // configuration.
Chris@0 107 $this->testEntityUrlLanguage();
Chris@0 108 }
Chris@0 109
Chris@0 110 /**
Chris@0 111 * Creates a translated entity.
Chris@0 112 */
Chris@0 113 protected function createTranslatableEntity() {
Chris@0 114 $this->entity = EntityTest::create();
Chris@0 115 $this->entity->addTranslation('es', ['name' => 'name spanish']);
Chris@0 116 $this->entity->addTranslation('fr', ['name' => 'name french']);
Chris@0 117 $this->entity->save();
Chris@0 118 }
Chris@0 119
Chris@0 120 /**
Chris@0 121 * Sets the current request to a specific path with the corresponding route.
Chris@0 122 *
Chris@0 123 * @param string $path
Chris@0 124 * The path for which the current request should be created.
Chris@0 125 * @param string $route_name
Chris@0 126 * The route name for which the route object for the request should be
Chris@0 127 * created.
Chris@0 128 */
Chris@0 129 protected function setCurrentRequestForRoute($path, $route_name) {
Chris@0 130 $request = Request::create($path);
Chris@0 131 $request->attributes->set(RouteObjectInterface::ROUTE_NAME, $route_name);
Chris@0 132 $request->attributes->set(RouteObjectInterface::ROUTE_OBJECT, new Route($path));
Chris@0 133 $this->container->get('request_stack')->push($request);
Chris@0 134 }
Chris@0 135
Chris@0 136 }