LanguageFallbackTest.php in Drupal 10
File
core/modules/language/tests/src/Kernel/LanguageFallbackTest.php
View source
<?php
namespace Drupal\Tests\language\Kernel;
use Drupal\Core\Language\LanguageInterface;
use Drupal\language\Entity\ConfigurableLanguage;
class LanguageFallbackTest extends LanguageTestBase {
protected function setUp() : void {
parent::setUp();
$i = 0;
foreach ([
'af',
'am',
'ar',
] as $langcode) {
$language = ConfigurableLanguage::createFromLangcode($langcode);
$language
->set('weight', $i--);
$language
->save();
}
}
public function testCandidates() {
$language_list = $this->languageManager
->getLanguages();
$expected = array_keys($language_list + [
LanguageInterface::LANGCODE_NOT_SPECIFIED => NULL,
]);
$candidates = $this->languageManager
->getFallbackCandidates();
$this
->assertEquals($expected, array_values($candidates), 'Language fallback candidates are properly returned.');
$this->state
->set('language_test.fallback_alter.candidates', TRUE);
$expected = array_slice($expected, 0, count($expected) - 1);
$candidates = $this->languageManager
->getFallbackCandidates();
$this
->assertEquals($expected, array_values($candidates), 'Language fallback candidates are alterable.');
$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
->assertEquals($expected, array_values($candidates), 'Language fallback candidates are alterable for specific operations.');
$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
->assertEquals([
LanguageInterface::LANGCODE_DEFAULT,
], array_values($candidates), 'Language fallback is not applied when the Language module is not enabled.');
}
}