View source
<?php
namespace Drupal\Tests\language\Kernel;
use Drupal\Core\Language\LanguageInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationContentEntity;
use Drupal\language\Plugin\LanguageNegotiation\LanguageNegotiationUrl;
use Drupal\Core\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
class EntityUrlLanguageTest extends LanguageTestBase {
protected static $modules = [
'entity_test',
'user',
];
protected $entity;
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('entity_test');
$this
->installEntitySchema('configurable_language');
ConfigurableLanguage::create([
'id' => 'es',
])
->save();
ConfigurableLanguage::create([
'id' => 'fr',
])
->save();
$config = $this
->config('language.negotiation');
$config
->set('url.prefixes', [
'en' => 'en',
'es' => 'es',
'fr' => 'fr',
])
->save();
\Drupal::service('kernel')
->rebuildContainer();
$this
->createTranslatableEntity();
}
public function testEntityUrlLanguage() {
$this
->assertStringContainsString('/en/entity_test/' . $this->entity
->id(), $this->entity
->toUrl()
->toString());
$this
->assertStringContainsString('/es/entity_test/' . $this->entity
->id(), $this->entity
->getTranslation('es')
->toUrl()
->toString());
$this
->assertStringContainsString('/fr/entity_test/' . $this->entity
->id(), $this->entity
->getTranslation('fr')
->toUrl()
->toString());
}
public function testEntityUrlLanguageWithLanguageContentEnabled() {
$config = $this
->config('language.types');
$config
->set('configurable', [
LanguageInterface::TYPE_INTERFACE,
LanguageInterface::TYPE_CONTENT,
]);
$config
->set('negotiation.language_content.enabled', [
LanguageNegotiationContentEntity::METHOD_ID => 0,
LanguageNegotiationUrl::METHOD_ID => 1,
]);
$config
->save();
$this
->testEntityUrlLanguage();
$this
->setCurrentRequestForRoute('/entity_test/{entity_test}', 'entity.entity_test.canonical');
$this
->assertStringContainsString('/en/entity_test/' . $this->entity
->id() . '?' . LanguageNegotiationContentEntity::QUERY_PARAMETER . '=en', $this->entity
->toUrl('canonical')
->toString());
$this
->assertStringContainsString('/en/entity_test/' . $this->entity
->id() . '?' . LanguageNegotiationContentEntity::QUERY_PARAMETER . '=es', $this->entity
->getTranslation('es')
->toUrl('canonical')
->toString());
$this
->assertStringContainsString('/en/entity_test/' . $this->entity
->id() . '?' . LanguageNegotiationContentEntity::QUERY_PARAMETER . '=fr', $this->entity
->getTranslation('fr')
->toUrl('canonical')
->toString());
$config
->set('negotiation.language_content.enabled', [
LanguageNegotiationUrl::METHOD_ID => 0,
LanguageNegotiationContentEntity::METHOD_ID => 1,
]);
$config
->save();
$this
->testEntityUrlLanguage();
}
protected function createTranslatableEntity() {
$this->entity = EntityTest::create();
$this->entity
->addTranslation('es', [
'name' => 'name spanish',
]);
$this->entity
->addTranslation('fr', [
'name' => 'name french',
]);
$this->entity
->save();
}
protected function setCurrentRequestForRoute($path, $route_name) {
$request = Request::create($path);
$request->attributes
->set(RouteObjectInterface::ROUTE_NAME, $route_name);
$request->attributes
->set(RouteObjectInterface::ROUTE_OBJECT, new Route($path));
$this->container
->get('request_stack')
->push($request);
}
}