You are here

public function LanguageConfigurationElementTest::testDefaultLangcode in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/language/tests/src/Functional/LanguageConfigurationElementTest.php \Drupal\Tests\language\Functional\LanguageConfigurationElementTest::testDefaultLangcode()

Tests that the language_get_default_langcode() returns the correct values.

File

core/modules/language/tests/src/Functional/LanguageConfigurationElementTest.php, line 96

Class

LanguageConfigurationElementTest
Tests the features of the language configuration element field.

Namespace

Drupal\Tests\language\Functional

Code

public function testDefaultLangcode() {

  // Add some custom languages.
  foreach ([
    'aa',
    'bb',
    'cc',
  ] as $language_code) {
    ConfigurableLanguage::create([
      'id' => $language_code,
      'label' => $this
        ->randomMachineName(),
    ])
      ->save();
  }

  // Fixed language.
  ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')
    ->setLanguageAlterable(TRUE)
    ->setDefaultLangcode('bb')
    ->save();
  $langcode = language_get_default_langcode('entity_test', 'custom_bundle');
  $this
    ->assertEqual($langcode, 'bb');

  // Current interface.
  ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')
    ->setLanguageAlterable(TRUE)
    ->setDefaultLangcode('current_interface')
    ->save();
  $langcode = language_get_default_langcode('entity_test', 'custom_bundle');
  $language_interface = \Drupal::languageManager()
    ->getCurrentLanguage();
  $this
    ->assertEqual($langcode, $language_interface
    ->getId());

  // Site's default.
  $old_default = \Drupal::languageManager()
    ->getDefaultLanguage();

  // Ensure the language entity default value is correct.
  $configurable_language = ConfigurableLanguage::load($old_default
    ->getId());
  $this
    ->assertTrue($configurable_language
    ->isDefault(), 'The en language entity is flagged as the default language.');
  $this
    ->config('system.site')
    ->set('default_langcode', 'cc')
    ->save();
  ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')
    ->setLanguageAlterable(TRUE)
    ->setDefaultLangcode(LanguageInterface::LANGCODE_SITE_DEFAULT)
    ->save();
  $langcode = language_get_default_langcode('entity_test', 'custom_bundle');
  $this
    ->assertEqual($langcode, 'cc');

  // Ensure the language entity default value is correct.
  $configurable_language = ConfigurableLanguage::load($old_default
    ->getId());
  $this
    ->assertFalse($configurable_language
    ->isDefault(), 'The en language entity is not flagged as the default language.');
  $configurable_language = ConfigurableLanguage::load('cc');

  // Check calling the
  // \Drupal\language\ConfigurableLanguageInterface::isDefault() method
  // directly.
  $this
    ->assertTrue($configurable_language
    ->isDefault(), 'The cc language entity is flagged as the default language.');

  // Check the default value of a language field when authors preferred option
  // is selected.
  // Create first an user and assign a preferred langcode.
  $some_user = $this
    ->drupalCreateUser();
  $some_user->preferred_langcode = 'bb';
  $some_user
    ->save();
  $this
    ->drupalLogin($some_user);
  ContentLanguageSettings::create([
    'target_entity_type_id' => 'entity_test',
    'target_bundle' => 'some_bundle',
  ])
    ->setLanguageAlterable(TRUE)
    ->setDefaultLangcode('authors_default')
    ->save();
  $this
    ->drupalGet('language-tests/language_configuration_element_test');
  $this
    ->assertOptionSelected('edit-langcode', 'bb');
}