function LanguageNegotiationContentEntityTest::testEnabledLanguageContentNegotiator

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

Tests enabling the language negotiator language_content_entity.

File

core/modules/language/tests/src/Functional/LanguageNegotiationContentEntityTest.php, line 99

Class

LanguageNegotiationContentEntityTest
Tests language negotiation with the language negotiator content entity.

Namespace

Drupal\Tests\language\Functional

Code

public function testEnabledLanguageContentNegotiator() : void {
    // 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 = $this->config('language.types');
    $config->set('configurable', [
        LanguageInterface::TYPE_INTERFACE,
        LanguageInterface::TYPE_CONTENT,
    ]);
    $config->set('negotiation.language_content.enabled', [
        LanguageNegotiationUrl::METHOD_ID => 0,
        LanguageNegotiationContentEntity::METHOD_ID => 1,
    ]);
    $config->save();
    // In order to reflect the changes for a multilingual site in the container
    // we have to rebuild it.
    $this->rebuildContainer();
    // The tests for the default configuration should still pass.
    $this->testDefaultConfiguration();
    // Define the method language-content-entity with a higher priority than
    // language-url.
    $config->set('negotiation.language_content.enabled', [
        LanguageNegotiationContentEntity::METHOD_ID => 0,
        LanguageNegotiationUrl::METHOD_ID => 1,
    ]);
    $config->save();
    // In order to reflect the changes for a multilingual site in the container
    // we have to rebuild it.
    $this->rebuildContainer();
    // 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.
    $default_site_langcode = $this->config('system.site')
        ->get('default_langcode');
    // 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');
    $translation = $this->entity;
    $this->drupalGet($translation->toUrl());
    $last = \Drupal::keyValue('language_test')->get('language_negotiation_last');
    $last_content_language = $last[LanguageInterface::TYPE_CONTENT];
    $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
    // Check that interface language and content language are the same as the
    // default translation language of the entity.
    $this->assertSame($default_site_langcode, $last_interface_language);
    $this->assertSame($last_content_language, $last_interface_language);
    $this->assertSame($translation->language()
        ->getId(), $last_content_language);
    $translation = $this->entity
        ->getTranslation('es');
    $this->drupalGet($translation->toUrl());
    $last = \Drupal::keyValue('language_test')->get('language_negotiation_last');
    $last_content_language = $last[LanguageInterface::TYPE_CONTENT];
    $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
    $this->assertSame($last_interface_language, $default_site_langcode, 'Interface language did not change from the default site language.');
    $this->assertSame($last_content_language, $translation->language()
        ->getId(), 'Content language matches the current entity translation language.');
    $translation = $this->entity
        ->getTranslation('fr');
    $this->drupalGet($translation->toUrl());
    $last = \Drupal::keyValue('language_test')->get('language_negotiation_last');
    $last_content_language = $last[LanguageInterface::TYPE_CONTENT];
    $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
    $this->assertSame($last_interface_language, $default_site_langcode, 'Interface language did not change from the default site language.');
    $this->assertSame($last_content_language, $translation->language()
        ->getId(), 'Content language matches the current entity translation language.');
}

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