function LanguageFallbackTest::testCandidates

Same name and namespace in other branches
  1. 9 core/modules/language/tests/src/Kernel/LanguageFallbackTest.php \Drupal\Tests\language\Kernel\LanguageFallbackTest::testCandidates()
  2. 10 core/modules/language/tests/src/Kernel/LanguageFallbackTest.php \Drupal\Tests\language\Kernel\LanguageFallbackTest::testCandidates()
  3. 11.x core/modules/language/tests/src/Kernel/LanguageFallbackTest.php \Drupal\Tests\language\Kernel\LanguageFallbackTest::testCandidates()

Tests language fallback candidates.

File

core/modules/language/tests/src/Kernel/LanguageFallbackTest.php, line 32

Class

LanguageFallbackTest
Tests the language fallback behavior.

Namespace

Drupal\Tests\language\Kernel

Code

public function testCandidates() {
    $language_list = $this->languageManager
        ->getLanguages();
    $expected = array_keys($language_list + [
        LanguageInterface::LANGCODE_NOT_SPECIFIED => NULL,
    ]);
    // Check that language fallback candidates by default are all the available
    // languages sorted by weight.
    $candidates = $this->languageManager
        ->getFallbackCandidates();
    $this->assertEqual(array_values($candidates), $expected, 'Language fallback candidates are properly returned.');
    // Check that candidates are alterable.
    $this->state
        ->set('language_test.fallback_alter.candidates', TRUE);
    $expected = array_slice($expected, 0, count($expected) - 1);
    $candidates = $this->languageManager
        ->getFallbackCandidates();
    $this->assertEqual(array_values($candidates), $expected, 'Language fallback candidates are alterable.');
    // Check that candidates are alterable for specific operations.
    $this->state
        ->set('language_test.fallback_alter.candidates', FALSE);
    $this->state
        ->set('language_test.fallback_operation_alter.candidates', TRUE);
    $expected[] = LanguageInterface::LANGCODE_NOT_SPECIFIED;
    $expected[] = LanguageInterface::LANGCODE_NOT_APPLICABLE;
    $candidates = $this->languageManager
        ->getFallbackCandidates([
        'operation' => 'test',
    ]);
    $this->assertEqual(array_values($candidates), $expected, 'Language fallback candidates are alterable for specific operations.');
    // Check that when the site is monolingual no language fallback is applied.
    
    /** @var \Drupal\Core\Config\Entity\ConfigEntityStorageInterface $configurable_language_storage */
    $configurable_language_storage = $this->container
        ->get('entity_type.manager')
        ->getStorage('configurable_language');
    foreach ($language_list as $langcode => $language) {
        if (!$language->isDefault()) {
            $configurable_language_storage->load($langcode)
                ->delete();
        }
    }
    $candidates = $this->languageManager
        ->getFallbackCandidates();
    $this->assertEqual(array_values($candidates), [
        LanguageInterface::LANGCODE_DEFAULT,
    ], 'Language fallback is not applied when the Language module is not enabled.');
}

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