LanguageCustomLanguageConfigurationTest.php 3.93 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Drupal\language\Tests;

use Drupal\simpletest\WebTestBase;
use Drupal\Core\Language\Language;
7
use Drupal\Core\Language\LanguageInterface;
8 9

/**
10 11 12
 * Adds and configures custom languages.
 *
 * @group language
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 */
class LanguageCustomLanguageConfigurationTest extends WebTestBase {

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

  /**
   * Functional tests for adding, editing and deleting languages.
   */
  public function testLanguageConfiguration() {

    // Create user with permissions to add and remove languages.
    $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
    $this->drupalLogin($admin_user);

    // Add custom language.
    $edit = array(
      'predefined_langcode' => 'custom',
    );
36
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
37
    // Test validation on missing values.
38 39
    $this->assertText(t('@name field is required.', array('@name' => t('Language code'))));
    $this->assertText(t('@name field is required.', array('@name' => t('Language name'))));
40
    $empty_language = new Language();
41
    $this->assertFieldChecked('edit-direction-' . $empty_language->getDirection(), 'Consistent usage of language direction.');
42
    $this->assertUrl(\Drupal::url('language.add', array(), array('absolute' => TRUE)), [], 'Correct page redirection.');
43 44 45 46 47

    // Test validation of invalid values.
    $edit = array(
      'predefined_langcode' => 'custom',
      'langcode' => 'white space',
48
      'label' => '<strong>evil markup</strong>',
49
      'direction' => LanguageInterface::DIRECTION_LTR,
50
    );
51
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
52

53
    $this->assertRaw(t('%field must be a valid language tag as <a href=":url">defined by the W3C</a>.', array(
54
      '%field' => t('Language code'),
55
      ':url' => 'http://www.w3.org/International/articles/language-tags/',
56 57
    )));

58
    $this->assertRaw(t('%field cannot contain any markup.', array('%field' => t('Language name'))));
59
    $this->assertUrl(\Drupal::url('language.add', array(), array('absolute' => TRUE)), [], 'Correct page redirection.');
60

61 62 63 64 65 66 67 68 69 70 71 72 73
    // Test adding a custom language with a numeric region code.
    $edit = array(
      'predefined_langcode' => 'custom',
      'langcode' => 'es-419',
      'label' => 'Latin American Spanish',
      'direction' => LanguageInterface::DIRECTION_LTR,
    );

    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
    $this->assertRaw(t(
      'The language %language has been created and can now be used.',
      array('%language' => $edit['label'])
    ));
74
    $this->assertUrl(\Drupal::url('entity.configurable_language.collection', array(), array('absolute' => TRUE)), [], 'Correct page redirection.');
75

76 77 78 79
    // Test validation of existing language values.
    $edit = array(
      'predefined_langcode' => 'custom',
      'langcode' => 'de',
80
      'label' => 'German',
81
      'direction' => LanguageInterface::DIRECTION_LTR,
82 83 84
    );

    // Add the language the first time.
85
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
86 87
    $this->assertRaw(t(
      'The language %language has been created and can now be used.',
88
      array('%language' => $edit['label'])
89
    ));
90
    $this->assertUrl(\Drupal::url('entity.configurable_language.collection', array(), array('absolute' => TRUE)), [], 'Correct page redirection.');
91 92

    // Add the language a second time and confirm that this is not allowed.
93
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
94 95
    $this->assertRaw(t(
      'The language %language (%langcode) already exists.',
96
      array('%language' => $edit['label'], '%langcode' => $edit['langcode'])
97
    ));
98
    $this->assertUrl(\Drupal::url('language.add', array(), array('absolute' => TRUE)), [], 'Correct page redirection.');
99
  }
100

101
}