LanguageSelectElementTest.php 4.63 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\system\Tests\Form\LanguageSelectElementTest.
 */

namespace Drupal\system\Tests\Form;

10
use Drupal\Component\Serialization\Json;
11
use Drupal\Core\Language\LanguageInterface;
12
use Drupal\language\Entity\ConfigurableLanguage;
13 14 15
use Drupal\simpletest\WebTestBase;

/**
16 17 18 19
 * Tests that the language select form element prints and submits the right
 * options.
 *
 * @group Form
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
 */
class LanguageSelectElementTest extends WebTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
  public static $modules = array('form_test', 'language');

  /**
   * Tests that the options printed by the language select element are correct.
   */
  function testLanguageSelectElementOptions() {
    // Add some languages.
35
    ConfigurableLanguage::create(array(
36
      'id' => 'aaa',
37 38
      'label' => $this->randomMachineName(),
    ))->save();
39

40
    ConfigurableLanguage::create(array(
41
      'id' => 'bbb',
42 43
      'label' => $this->randomMachineName(),
    ))->save();
44

45 46
    \Drupal::languageManager()->reset();

47 48
    $this->drupalGet('form-test/language_select');
    // Check that the language fields were rendered on the page.
49 50 51 52 53 54
    $ids = array(
        'edit-languages-all' => LanguageInterface::STATE_ALL,
        'edit-languages-configurable' => LanguageInterface::STATE_CONFIGURABLE,
        'edit-languages-locked' => LanguageInterface::STATE_LOCKED,
        'edit-languages-config-and-locked' => LanguageInterface::STATE_CONFIGURABLE | LanguageInterface::STATE_LOCKED
    );
55
    foreach ($ids as $id => $flags) {
56
      $this->assertField($id, format_string('The @id field was found on the page.', array('@id' => $id)));
57
      $options = array();
58
      foreach ($this->container->get('language_manager')->getLanguages($flags) as $langcode => $language) {
59
        $options[$langcode] = $language->isLocked() ? t('- @name -', array('@name' => $language->name)) : $language->name;
60 61 62 63 64
      }
      $this->_testLanguageSelectElementOptions($id, $options);
    }

    // Test that the #options were not altered by #languages.
65
    $this->assertField('edit-language-custom-options', format_string('The @id field was found on the page.', array('@id' => 'edit-language-custom-options')));
66 67 68 69 70 71 72 73 74 75 76
    $this->_testLanguageSelectElementOptions('edit-language-custom-options', array('opt1' => 'First option', 'opt2' => 'Second option', 'opt3' => 'Third option'));
  }

  /**
   * Tests the case when the language select elements should not be printed.
   *
   * This happens when the language module is disabled.
   */
  function testHiddenLanguageSelectElement() {
    // Disable the language module, so that the language select field will not
    // be rendered.
77
    $this->container->get('module_handler')->uninstall(array('language'));
78 79 80 81
    $this->drupalGet('form-test/language_select');
    // Check that the language fields were rendered on the page.
    $ids = array('edit-languages-all', 'edit-languages-configurable', 'edit-languages-locked', 'edit-languages-config-and-locked');
    foreach ($ids as $id) {
82
      $this->assertNoField($id, format_string('The @id field was not found on the page.', array('@id' => $id)));
83 84 85 86 87
    }

    // Check that the submitted values were the default values of the language
    // field elements.
    $edit = array();
88
    $this->drupalPostForm(NULL, $edit, t('Submit'));
89
    $values = Json::decode($this->drupalGetContent());
90 91
    $this->assertEqual($values['languages_all'], 'xx');
    $this->assertEqual($values['languages_configurable'], 'en');
92
    $this->assertEqual($values['languages_locked'], LanguageInterface::LANGCODE_NOT_SPECIFIED);
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    $this->assertEqual($values['languages_config_and_locked'], 'dummy_value');
    $this->assertEqual($values['language_custom_options'], 'opt2');
  }

  /**
   * Helper function to check the options of a language select form element.
   *
   * @param string $id
   *   The id of the language select element to check.
   *
   * @param array $options
   *   An array with options to compare with.
   */
  protected function _testLanguageSelectElementOptions($id, $options) {
    // Check that the options in the language field are exactly the same,
    // including the order, as the languages sent as a parameter.
    $elements = $this->xpath("//select[@id='" . $id . "']");
    $count = 0;
    foreach ($elements[0]->option as $option) {
      $count++;
      $option_title = current($options);
      $this->assertEqual((string) $option, $option_title);
      next($options);
    }
117
    $this->assertEqual($count, count($options), format_string('The number of languages and the number of options shown by the language element are the same: @languages languages, @number options', array('@languages' => count($options), '@number' => $count)));
118 119
  }
}