function LanguageConditionTest::testConditions

Same name in other branches
  1. 8.9.x core/modules/language/tests/src/Kernel/Condition/LanguageConditionTest.php \Drupal\Tests\language\Kernel\Condition\LanguageConditionTest::testConditions()
  2. 10 core/modules/language/tests/src/Kernel/Condition/LanguageConditionTest.php \Drupal\Tests\language\Kernel\Condition\LanguageConditionTest::testConditions()
  3. 11.x core/modules/language/tests/src/Kernel/Condition/LanguageConditionTest.php \Drupal\Tests\language\Kernel\Condition\LanguageConditionTest::testConditions()

Tests the language condition.

File

core/modules/language/tests/src/Kernel/Condition/LanguageConditionTest.php, line 52

Class

LanguageConditionTest
Tests the language condition plugin.

Namespace

Drupal\Tests\language\Kernel\Condition

Code

public function testConditions() {
    // Grab the language condition and configure it to check the content
    // language.
    $language = \Drupal::languageManager()->getLanguage('en');
    $condition = $this->manager
        ->createInstance('language')
        ->setConfig('langcodes', [
        'en' => 'en',
        'it' => 'it',
    ])
        ->setContextValue('language', $language);
    $this->assertTrue($condition->execute(), 'Language condition passes as expected.');
    // Check for the proper summary.
    $this->assertEquals('The language is English, Italian.', $condition->summary());
    // Change to Italian only.
    $condition->setConfig('langcodes', [
        'it' => 'it',
    ]);
    $this->assertFalse($condition->execute(), 'Language condition fails as expected.');
    // Check for the proper summary.
    $this->assertEquals('The language is Italian.', $condition->summary());
    // Negate the condition
    $condition->setConfig('negate', TRUE);
    $this->assertTrue($condition->execute(), 'Language condition passes as expected.');
    // Check for the proper summary.
    $this->assertEquals('The language is not Italian.', $condition->summary());
    // Change the default language to Italian.
    $language = \Drupal::languageManager()->getLanguage('it');
    $condition = $this->manager
        ->createInstance('language')
        ->setConfig('langcodes', [
        'en' => 'en',
        'it' => 'it',
    ])
        ->setContextValue('language', $language);
    $this->assertTrue($condition->execute(), 'Language condition passes as expected.');
    // Check for the proper summary.
    $this->assertEquals('The language is English, Italian.', $condition->summary());
    // Change to Italian only.
    $condition->setConfig('langcodes', [
        'it' => 'it',
    ]);
    $this->assertTrue($condition->execute(), 'Language condition passes as expected.');
    // Check for the proper summary.
    $this->assertEquals('The language is Italian.', $condition->summary());
    // Negate the condition
    $condition->setConfig('negate', TRUE);
    $this->assertFalse($condition->execute(), 'Language condition fails as expected.');
    // Check for the proper summary.
    $this->assertEquals('The language is not Italian.', $condition->summary());
}

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