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

/**
 * @file
 * Definition of Drupal\language\Tests\LanguageConfigurationElementTest.
 */

namespace Drupal\language\Tests;

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

/**
15
16
17
 * Tests the features of the language configuration element field.
 *
 * @group language
18
19
20
21
22
23
24
25
 */
class LanguageConfigurationElementTest extends WebTestBase {

  /**
   * Modules to enable.
   *
   * @var array
   */
26
  public static $modules = array('node', 'language', 'language_elements_test');
27
28
29
30
31
32
33

  /**
   * Tests the language settings have been saved.
   */
  public function testLanguageConfigurationElement() {
    $this->drupalGet('language-tests/language_configuration_element');
    $edit['lang_configuration[langcode]'] = 'current_interface';
34
    $edit['lang_configuration[language_show]'] = FALSE;
35
    $this->drupalPostForm(NULL, $edit, 'Save');
36
37
38
39
    $lang_conf = language_get_default_configuration('some_custom_type', 'some_bundle');

    // Check that the settings have been saved.
    $this->assertEqual($lang_conf['langcode'], 'current_interface');
40
    $this->assertFalse($lang_conf['language_show']);
41
42
    $this->drupalGet('language-tests/language_configuration_element');
    $this->assertOptionSelected('edit-lang-configuration-langcode', 'current_interface');
43
    $this->assertNoFieldChecked('edit-lang-configuration-language-show');
44
45
46
47

    // Reload the page and save again.
    $this->drupalGet('language-tests/language_configuration_element');
    $edit['lang_configuration[langcode]'] = 'authors_default';
48
    $edit['lang_configuration[language_show]'] = TRUE;
49
    $this->drupalPostForm(NULL, $edit, 'Save');
50
51
52
53
    $lang_conf = language_get_default_configuration('some_custom_type', 'some_bundle');

    // Check that the settings have been saved.
    $this->assertEqual($lang_conf['langcode'], 'authors_default');
54
    $this->assertTrue($lang_conf['language_show']);
55
56
    $this->drupalGet('language-tests/language_configuration_element');
    $this->assertOptionSelected('edit-lang-configuration-langcode', 'authors_default');
57
    $this->assertFieldChecked('edit-lang-configuration-language-show');
58
59
60
61
62
63
64
65
  }

  /**
   * Tests that the language_get_default_langcode() returns the correct values.
   */
  public function testDefaultLangcode() {
    // Add some custom languages.
    foreach (array('aa', 'bb', 'cc') as $language_code) {
66
      ConfigurableLanguage::create(array(
67
        'id' => $language_code,
68
69
        'label' => $this->randomMachineName(),
      ))->save();
70
71
72
    }

    // Fixed language.
73
    language_save_default_configuration('custom_type', 'custom_bundle', array('langcode' => 'bb', 'language_show' => TRUE));
74
75
76
77
    $langcode = language_get_default_langcode('custom_type', 'custom_bundle');
    $this->assertEqual($langcode, 'bb');

    // Current interface.
78
    language_save_default_configuration('custom_type', 'custom_bundle', array('langcode' => 'current_interface', 'language_show' => TRUE));
79
    $langcode = language_get_default_langcode('custom_type', 'custom_bundle');
80
    $language_interface = \Drupal::languageManager()->getCurrentLanguage();
81
    $this->assertEqual($langcode, $language_interface->id);
82
83

    // Site's default.
84
    $old_default = \Drupal::languageManager()->getDefaultLanguage();
85
    // Ensure the language entity default value is correct.
86
    $configurable_language = entity_load('configurable_language', $old_default->getId());
87
    $this->assertTrue($configurable_language->isDefault(), 'The en language entity is flagged as the default language.');
88

89
    \Drupal::config('system.site')->set('langcode', 'cc')->save();
90
    language_save_default_configuration('custom_type', 'custom_bundle', array('langcode' => LanguageInterface::LANGCODE_SITE_DEFAULT, 'language_show' => TRUE));
91
92
93
    $langcode = language_get_default_langcode('custom_type', 'custom_bundle');
    $this->assertEqual($langcode, 'cc');

94
    // Ensure the language entity default value is correct.
95
    $configurable_language = entity_load('configurable_language', $old_default->getId());
96
    $this->assertFalse($configurable_language->isDefault(), 'The en language entity is not flagged as the default language.');
97
98
    $configurable_language = entity_load('configurable_language', 'cc');
    // Check calling the
99
    // \Drupal\language\ConfigurableLanguageInterface::isDefault() method
100
    // directly.
101
    $this->assertTrue($configurable_language->isDefault(), 'The cc language entity is flagged as the default language.');
102

103
104
105
106
107
108
109
    // Check the default value of a language field when authors preferred option
    // is selected.
    // Create first an user and assign a preferred langcode to him.
    $some_user = $this->drupalCreateUser();
    $some_user->preferred_langcode = 'bb';
    $some_user->save();
    $this->drupalLogin($some_user);
110
    language_save_default_configuration('custom_type', 'some_bundle', array('langcode' => 'authors_default', 'language_show' => TRUE));
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
    $this->drupalGet('language-tests/language_configuration_element_test');
    $this->assertOptionSelected('edit-langcode', 'bb');
  }

  /**
   * Tests that the configuration is updated when the node type is changed.
   */
  public function testNodeTypeUpdate() {
    // Create the article content type first if the profile used is not the
    // standard one.
    if ($this->profile != 'standard') {
      $this->drupalCreateContentType(array('type' => 'article', 'name' => 'Article'));
    }
    $admin_user = $this->drupalCreateUser(array('administer content types'));
    $this->drupalLogin($admin_user);
    $edit = array(
      'language_configuration[langcode]' => 'current_interface',
128
      'language_configuration[language_show]' => TRUE,
129
    );
130
    $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type'));
131
132
    // Check the language default configuration for the articles.
    $configuration = language_get_default_configuration('node', 'article');
133
    $this->assertEqual($configuration, array('langcode' => 'current_interface', 'language_show' => TRUE), 'The default language configuration has been saved on the Article content type.');
134
135
136
137
    // Rename the article content type.
    $edit = array(
      'type' => 'article_2'
    );
138
    $this->drupalPostForm('admin/structure/types/manage/article', $edit, t('Save content type'));
139
140
    // Check that we still have the settings for the new node type.
    $configuration = language_get_default_configuration('node', 'article_2');
141
    $this->assertEqual($configuration, array('langcode' => 'current_interface', 'language_show' => TRUE), 'The default language configuration has been kept on the new Article content type.');
142
143
  }
}