LanguageConditionTest.php

Contains \Drupal\language\Tests\Condition\LanguageConditionTest.

Namespace

Drupal\language\Tests\Condition

File

drupal/core/modules/language/lib/Drupal/language/Tests/Condition/LanguageConditionTest.php
View source
<?php

/**
 * @file
 * Contains \Drupal\language\Tests\Condition\LanguageConditionTest.
 */
namespace Drupal\language\Tests\Condition;

use Drupal\simpletest\DrupalUnitTestBase;
use Drupal\Core\Condition\ConditionManager;
use Drupal\Core\Language\Language;

/**
 * Tests the language condition.
 */
class LanguageConditionTest extends DrupalUnitTestBase {

  /**
   * The condition plugin manager.
   *
   * @var \Drupal\Core\Condition\ConditionManager
   */
  protected $manager;

  /**
   * The language manager.
   *
   * @var \Drupal\Core\Language\LanguageManager
   */
  protected $languageManager;

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array(
    'system',
    'language',
  );
  public static function getInfo() {
    return array(
      'name' => 'Language Condition Plugin',
      'description' => 'Tests that the language condition, provided by the language module, is working properly.',
      'group' => 'Condition API',
    );
  }
  protected function setUp() {
    parent::setUp();
    $this
      ->installSchema('language', 'language');

    // This is needed for language_default().
    // @todo remove this when language_default() no longer needs variable_get().
    $this
      ->installSchema('system', 'variable');

    // Setup English.
    language_save(language_default());

    // Setup Italian.
    $language = new Language(array(
      'langcode' => 'it',
      'name' => 'Italian',
      'direction' => '0',
    ));
    language_save($language);
    $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 = language_load('en');
    $condition = $this->manager
      ->createInstance('language')
      ->setConfig('langcodes', array(
      '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', array(
      '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 = language_load('it');
    $condition = $this->manager
      ->createInstance('language')
      ->setConfig('langcodes', array(
      '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', array(
      '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

Namesort descending Description
LanguageConditionTest Tests the language condition.