UserLanguageTest.php 2.22 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
<?php

/**
 * @file
 * Definition of Drupal\user\Tests\UserLanguageTest.
 */

namespace Drupal\user\Tests;

use Drupal\simpletest\WebTestBase;

/**
 * Functional tests for a user's ability to change their default language.
 */
class UserLanguageTest extends WebTestBase {
16 17 18 19 20 21 22 23

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

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  public static function getInfo() {
    return array(
      'name' => 'User language settings',
      'description' => "Tests user's ability to change their default language.",
      'group' => 'User',
    );
  }

  /**
   * Test if user can change their default language.
   */
  function testUserLanguageConfiguration() {
    // User to add and remove language.
    $admin_user = $this->drupalCreateUser(array('administer languages', 'access administration pages'));
    // User to change their default language.
    $web_user = $this->drupalCreateUser();

    // Add custom language.
    $this->drupalLogin($admin_user);
    // Code for the language.
    $langcode = 'xx';
    // The English name for the language.
    $name = $this->randomName(16);
    $edit = array(
      'predefined_langcode' => 'custom',
      'langcode' => $langcode,
      'name' => $name,
      'direction' => '0',
    );
53
    $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
54 55 56 57
    $this->drupalLogout();

    // Login as normal user and edit account settings.
    $this->drupalLogin($web_user);
58
    $path = 'user/' . $web_user->id() . '/edit';
59
    $this->drupalGet($path);
60
    // Ensure language settings widget is available.
61
    $this->assertText(t('Language'), 'Language selector available.');
62
    // Ensure custom language is present.
63
    $this->assertText($name, 'Language present on form.');
64 65 66 67
    // Switch to our custom language.
    $edit = array(
      'preferred_langcode' => $langcode,
    );
68
    $this->drupalPostForm($path, $edit, t('Save'));
69
    // Ensure form was submitted successfully.
70
    $this->assertText(t('The changes have been saved.'), 'Changes were saved.');
71
    // Check if language was changed.
72
    $this->assertOptionSelected('edit-preferred-langcode', $langcode, 'Default language successfully updated.');
73 74 75 76

    $this->drupalLogout();
  }
}