You are here

public function LanguageNegotiationContentEntityTest::testEnabledLanguageContentNegotiator in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/language/src/Tests/LanguageNegotiationContentEntityTest.php \Drupal\language\Tests\LanguageNegotiationContentEntityTest::testEnabledLanguageContentNegotiator()

Tests enabling the language negotiator language_content_entity.

File

core/modules/language/src/Tests/LanguageNegotiationContentEntityTest.php, line 90
Contains \Drupal\language\Tests\LanguageNegotiationLanguageContentEntityTest.

Class

LanguageNegotiationContentEntityTest
Tests language negotiation with the language negotiator content entity.

Namespace

Drupal\language\Tests

Code

public function testEnabledLanguageContentNegotiator() {

  // 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
    ->urlInfo());
  $last = $this->container
    ->get('state')
    ->get('language_test.language_negotiation_last');
  $last_content_language = $last[LanguageInterface::TYPE_CONTENT];
  $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
  $this
    ->assertTrue($last_interface_language == $default_site_langcode && $last_interface_language == $last_content_language && $last_content_language == $translation
    ->language()
    ->getId(), 'Interface language and Content language are the same as the default translation language of the entity.');
  $this
    ->assertTrue($last_interface_language == $default_site_langcode, 'Interface language did not change from the default site language.');
  $this
    ->assertTrue($last_content_language == $translation
    ->language()
    ->getId(), 'Content language matches the current entity translation language.');
  $translation = $this->entity
    ->getTranslation('es');
  $this
    ->drupalGet($translation
    ->urlInfo());
  $last = $this->container
    ->get('state')
    ->get('language_test.language_negotiation_last');
  $last_content_language = $last[LanguageInterface::TYPE_CONTENT];
  $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
  $this
    ->assertTrue($last_interface_language == $default_site_langcode, 'Interface language did not change from the default site language.');
  $this
    ->assertTrue($last_content_language == $translation
    ->language()
    ->getId(), 'Content language matches the current entity translation language.');
  $translation = $this->entity
    ->getTranslation('fr');
  $this
    ->drupalGet($translation
    ->urlInfo());
  $last = $this->container
    ->get('state')
    ->get('language_test.language_negotiation_last');
  $last_content_language = $last[LanguageInterface::TYPE_CONTENT];
  $last_interface_language = $last[LanguageInterface::TYPE_INTERFACE];
  $this
    ->assertTrue($last_interface_language == $default_site_langcode, 'Interface language did not change from the default site language.');
  $this
    ->assertTrue($last_content_language == $translation
    ->language()
    ->getId(), 'Content language matches the current entity translation language.');
}