LanguageConditionTest.php

Same filename and directory in other branches
  1. 9 core/modules/language/tests/src/Kernel/Condition/LanguageConditionTest.php
  2. 10 core/modules/language/tests/src/Kernel/Condition/LanguageConditionTest.php
  3. 11.x core/modules/language/tests/src/Kernel/Condition/LanguageConditionTest.php

Namespace

Drupal\Tests\language\Kernel\Condition

File

core/modules/language/tests/src/Kernel/Condition/LanguageConditionTest.php

View source
<?php

namespace Drupal\Tests\language\Kernel\Condition;

use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\KernelTests\KernelTestBase;

/**
 * Tests that the language condition, provided by the language module, is
 * working properly.
 *
 * @group language
 */
class LanguageConditionTest extends KernelTestBase {
    
    /**
     * The condition plugin manager.
     *
     * @var \Drupal\Core\Condition\ConditionManager
     */
    protected $manager;
    
    /**
     * The language manager.
     *
     * @var \Drupal\Core\Language\LanguageManagerInterface
     */
    protected $languageManager;
    
    /**
     * Modules to enable.
     *
     * @var array
     */
    public static $modules = [
        'system',
        'language',
    ];
    protected function setUp() {
        parent::setUp();
        $this->installConfig([
            'language',
        ]);
        // Setup Italian.
        ConfigurableLanguage::createFromLangcode('it')->save();
        $this->manager = $this->container
            ->get('plugin.manager.condition');
    }
    
    /**
     * Test the language condition.
     */
    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->assertEqual($condition->summary(), 'The language is English, Italian.');
        // Change to Italian only.
        $condition->setConfig('langcodes', [
            'it' => 'it',
        ]);
        $this->assertFalse($condition->execute(), 'Language condition fails as expected.');
        // Check for the proper summary.
        $this->assertEqual($condition->summary(), 'The language is Italian.');
        // Negate the condition
        $condition->setConfig('negate', TRUE);
        $this->assertTrue($condition->execute(), 'Language condition passes as expected.');
        // Check for the proper summary.
        $this->assertEqual($condition->summary(), 'The language is not Italian.');
        // 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->assertEqual($condition->summary(), 'The language is English, Italian.');
        // Change to Italian only.
        $condition->setConfig('langcodes', [
            'it' => 'it',
        ]);
        $this->assertTrue($condition->execute(), 'Language condition passes as expected.');
        // Check for the proper summary.
        $this->assertEqual($condition->summary(), 'The language is Italian.');
        // Negate the condition
        $condition->setConfig('negate', TRUE);
        $this->assertFalse($condition->execute(), 'Language condition fails as expected.');
        // Check for the proper summary.
        $this->assertEqual($condition->summary(), 'The language is not Italian.');
    }

}

Classes

Title Deprecated Summary
LanguageConditionTest Tests that the language condition, provided by the language module, is working properly.

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