function ConfigEntityValidationTestBase::testLangcode

Same name in other branches
  1. 10 core/tests/Drupal/KernelTests/Core/Config/ConfigEntityValidationTestBase.php \Drupal\KernelTests\Core\Config\ConfigEntityValidationTestBase::testLangcode()

Tests that the config entity's langcode is validated.

File

core/tests/Drupal/KernelTests/Core/Config/ConfigEntityValidationTestBase.php, line 419

Class

ConfigEntityValidationTestBase
Base class for testing validation of config entities.

Namespace

Drupal\KernelTests\Core\Config

Code

public function testLangcode() : void {
    $this->entity
        ->set('langcode', NULL);
    $this->assertValidationErrors([
        'langcode' => 'This value should not be null.',
    ]);
    // A langcode from the standard list should always be acceptable.
    $standard_languages = LanguageManager::getStandardLanguageList();
    $this->assertNotEmpty($standard_languages);
    $this->entity
        ->set('langcode', key($standard_languages));
    $this->assertValidationErrors([]);
    // All special, internal langcodes should be acceptable.
    $system_langcodes = [
        LanguageInterface::LANGCODE_NOT_SPECIFIED,
        LanguageInterface::LANGCODE_NOT_APPLICABLE,
        LanguageInterface::LANGCODE_DEFAULT,
        LanguageInterface::LANGCODE_SITE_DEFAULT,
        LanguageInterface::LANGCODE_SYSTEM,
    ];
    foreach ($system_langcodes as $langcode) {
        $this->entity
            ->set('langcode', $langcode);
        $this->assertValidationErrors([]);
    }
    // An invalid langcode should be unacceptable, even if it "looks" right.
    $fake_langcode = 'definitely-not-a-language';
    $this->assertArrayNotHasKey($fake_langcode, LanguageReference::getAllValidLangcodes());
    $this->entity
        ->set('langcode', $fake_langcode);
    $this->assertValidationErrors([
        'langcode' => 'The value you selected is not a valid choice.',
    ]);
    // If a new configurable language is created with a non-standard langcode,
    // it should be acceptable.
    $this->enableModules([
        'language',
    ]);
    // The language doesn't exist yet, so it shouldn't be a valid choice.
    $this->entity
        ->set('langcode', 'kthxbai');
    $this->assertValidationErrors([
        'langcode' => 'The value you selected is not a valid choice.',
    ]);
    // Once we create the language, it should be a valid choice.
    ConfigurableLanguage::createFromLangcode('kthxbai')->save();
    $this->assertValidationErrors([]);
}

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