You are here

public function LanguageConfigurationElementTest::testDefaultLangcode in Zircon Profile 8

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

Tests that the language_get_default_langcode() returns the correct values.

File

core/modules/language/src/Tests/LanguageConfigurationElementTest.php, line 85
Contains \Drupal\language\Tests\LanguageConfigurationElementTest.

Class

LanguageConfigurationElementTest
Tests the features of the language configuration element field.

Namespace

Drupal\language\Tests

Code

public function testDefaultLangcode() {

  // Add some custom languages.
  foreach (array(
    'aa',
    'bb',
    'cc',
  ) as $language_code) {
    ConfigurableLanguage::create(array(
      '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 = entity_load('configurable_language', $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 = entity_load('configurable_language', $old_default
    ->getId());
  $this
    ->assertFalse($configurable_language
    ->isDefault(), 'The en language entity is not flagged as the default language.');
  $configurable_language = entity_load('configurable_language', '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 to him.
  $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');
}