function EntityUrlLanguageTest::testEntityUrlLanguageWithLanguageContentEnabled

Same name in other branches
  1. 9 core/modules/language/tests/src/Kernel/EntityUrlLanguageTest.php \Drupal\Tests\language\Kernel\EntityUrlLanguageTest::testEntityUrlLanguageWithLanguageContentEnabled()
  2. 8.9.x core/modules/language/tests/src/Kernel/EntityUrlLanguageTest.php \Drupal\Tests\language\Kernel\EntityUrlLanguageTest::testEntityUrlLanguageWithLanguageContentEnabled()
  3. 11.x core/modules/language/tests/src/Kernel/EntityUrlLanguageTest.php \Drupal\Tests\language\Kernel\EntityUrlLanguageTest::testEntityUrlLanguageWithLanguageContentEnabled()

Ensures correct entity URLs with the method language-content-entity enabled.

Test case with the method language-content-entity enabled and configured with higher and also with lower priority than the method language-url.

File

core/modules/language/tests/src/Kernel/EntityUrlLanguageTest.php, line 74

Class

EntityUrlLanguageTest
Tests the language of entity URLs. @group language

Namespace

Drupal\Tests\language\Kernel

Code

public function testEntityUrlLanguageWithLanguageContentEnabled() : void {
    // Define the method language-content-entity with a higher priority than
    // language-url.
    $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();
    // Without being on a content entity route the default entity URL tests
    // should still pass.
    $this->testEntityUrlLanguage();
    // Now switching to an entity route, so that the URL links are generated
    // while being on an entity route.
    $this->setCurrentRequestForRoute('/entity_test/{entity_test}', 'entity.entity_test.canonical');
    // The method language-content-entity should run before language-url and
    // append query parameter for the content language and prevent language-url
    // from overwriting the URL.
    $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());
    // Define the method language-url with a higher priority than
    // language-content-entity. This configuration should match the default one,
    // where the language-content-entity is turned off.
    $config->set('negotiation.language_content.enabled', [
        LanguageNegotiationUrl::METHOD_ID => 0,
        LanguageNegotiationContentEntity::METHOD_ID => 1,
    ]);
    $config->save();
    // The default entity URL tests should pass again with the current
    // configuration.
    $this->testEntityUrlLanguage();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.