function LanguageConfigurationElementTest::testDefaultLangcode

Same name in other branches
  1. 9 core/modules/language/tests/src/Functional/LanguageConfigurationElementTest.php \Drupal\Tests\language\Functional\LanguageConfigurationElementTest::testDefaultLangcode()
  2. 8.9.x core/modules/language/tests/src/Functional/LanguageConfigurationElementTest.php \Drupal\Tests\language\Functional\LanguageConfigurationElementTest::testDefaultLangcode()
  3. 11.x 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 100

Class

LanguageConfigurationElementTest
Tests the features of the language configuration element field.

Namespace

Drupal\Tests\language\Functional

Code

public function testDefaultLangcode() : void {
    // Add some custom languages.
    foreach ([
        'aa',
        'bb',
        'cc',
    ] as $language_code) {
        ConfigurableLanguage::create([
            'id' => $language_code,
            'label' => $this->randomMachineName(),
        ])
            ->save();
    }
    // Ensure the bundles under test exist, to avoid config validation errors.
    entity_test_create_bundle('custom_bundle');
    entity_test_create_bundle('some_bundle');
    // Fixed language.
    ContentLanguageSettings::loadByEntityTypeBundle('entity_test', 'custom_bundle')->setLanguageAlterable(TRUE)
        ->setDefaultLangcode('bb')
        ->save();
    $langcode = language_get_default_langcode('entity_test', 'custom_bundle');
    $this->assertEquals('bb', $langcode);
    // 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->assertEquals($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->assertEquals('cc', $langcode);
    // 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.
    // First create a user, then assign a 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->assertTrue($this->assertSession()
        ->optionExists('edit-langcode', 'bb')
        ->isSelected());
}

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