summaryrefslogtreecommitdiffstats
path: root/core/modules/menu_ui/lib/Drupal/menu_ui/Tests/MenuLanguageTest.php
blob: 73374aa4b784708e0568385f8d85a7f1c82f5144 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php

/**
 * @file
 * Tests for menu_ui language settings.
 */

namespace Drupal\menu_ui\Tests;

use Drupal\Component\Utility\Unicode;
use Drupal\Core\Language\Language;

/**
 * Defines a test class for testing menu language functionality.
 */
class MenuLanguageTest extends MenuWebTestBase {

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

  protected $admin_user;
  protected $menu;

  public static function getInfo() {
    return array(
      'name' => 'Menu language',
      'description' => 'Create menu and menu links in non-English language, and edit language settings.',
      'group' => 'Menu',
    );
  }

  function setUp() {
    parent::setUp();

    // Create user.
    $this->admin_user = $this->drupalCreateUser(array('access administration pages', 'administer menu'));
    $this->drupalLogin($this->admin_user);

    // Add some custom languages.
    foreach (array('aa', 'bb', 'cc', 'cs') as $language_code) {
      $language = new Language(array(
        'id' => $language_code,
        'name' => $this->randomName(),
      ));
      language_save($language);
    }
  }

  /**
   * Tests menu language settings and the defaults for menu link items.
   */
  function testMenuLanguage() {
    // Create a test menu to test the various language-related settings.
    // Machine name has to be lowercase.
    $menu_name = Unicode::strtolower($this->randomName(16));
    $label = $this->randomString();
    $edit = array(
      'id' => $menu_name,
      'description' => '',
      'label' =>  $label,
      'langcode' => 'aa',
      'default_language[langcode]' => 'bb',
      'default_language[language_show]' => TRUE,
    );
    $this->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));

    // Check that the language settings were saved.
    $this->assertEqual(entity_load('menu', $menu_name)->langcode, $edit['langcode']);
    $language_settings = language_get_default_configuration('menu_link', $menu_name);
    $this->assertEqual($language_settings['langcode'], 'bb');
    $this->assertEqual($language_settings['language_show'], TRUE);

    // Check menu language and item language configuration.
    $this->assertOptionSelected('edit-langcode', $edit['langcode'], 'The menu language was correctly selected.');
    $this->assertOptionSelected('edit-default-language-langcode', $edit['default_language[langcode]'], 'The menu link default language was correctly selected.');
    $this->assertFieldChecked('edit-default-language-language-show');

    // Test menu link language.
    $link_path = '<front>';

    // Add a menu link.
    $link_title = $this->randomString();
    $edit = array(
      'link_title' => $link_title,
      'link_path' => $link_path,
    );
    $this->drupalPostForm("admin/structure/menu/manage/$menu_name/add", $edit, t('Save'));
    // Check the link was added with the correct menu link default language.
    $menu_links = entity_load_multiple_by_properties('menu_link', array('link_title' => $link_title));
    $menu_link = reset($menu_links);
    $this->assertMenuLink($menu_link->id(), array(
      'menu_name' => $menu_name,
      'link_path' => $link_path,
      'langcode' => 'bb',
    ));

    // Edit menu link default, changing it to cc.
    $edit = array(
      'default_language[langcode]' => 'cc',
    );
    $this->drupalPostForm("admin/structure/menu/manage/$menu_name", $edit, t('Save'));

    // Check cc is the menu link default.
    $this->assertOptionSelected('edit-default-language-langcode', $edit['default_language[langcode]'], 'The menu link default language was correctly selected.');

    // Add a menu link.
    $link_title = $this->randomString();
    $edit = array(
      'link_title' => $link_title,
      'link_path' => $link_path,
    );
    $this->drupalPostForm("admin/structure/menu/manage/$menu_name/add", $edit, t('Save'));
    // Check the link was added with the correct new menu link default language.
    $menu_links = entity_load_multiple_by_properties('menu_link', array('link_title' => $link_title));
    $menu_link = reset($menu_links);
    $this->assertMenuLink($menu_link->id(), array(
      'menu_name' => $menu_name,
      'link_path' => $link_path,
      'langcode' => 'cc',
    ));

    // Now change the language of the new link to 'bb'.
    $edit = array(
      'langcode' => 'bb',
    );
    $this->drupalPostForm('admin/structure/menu/item/' . $menu_link->id() . '/edit', $edit, t('Save'));
    $this->assertMenuLink($menu_link->id(), array(
      'menu_name' => $menu_name,
      'link_path' => $link_path,
      'langcode' => 'bb',
    ));

    // Saving menu link items ends up on the edit menu page. To check the menu
    // link has the correct language default on edit, go to the menu link edit
    // page first.
    $this->drupalGet('admin/structure/menu/item/' . $menu_link->id() . '/edit');
    // Check that the language selector has the correct default value.
    $this->assertOptionSelected('edit-langcode', 'bb', 'The menu link language was correctly selected.');

    // Edit menu to hide the language select on menu link item add.
    $edit = array(
      'default_language[language_show]' => FALSE,
    );
    $this->drupalPostForm("admin/structure/menu/manage/$menu_name", $edit, t('Save'));
    $this->assertNoFieldChecked('edit-default-language-language-show');

    // Check that the language settings were saved.
    $language_settings = language_get_default_configuration('menu_link', $menu_name);
    $this->assertEqual($language_settings['langcode'], 'cc');
    $this->assertEqual($language_settings['language_show'], FALSE);

    // Check that the language selector is not available on menu link add page.
    $this->drupalGet("admin/structure/menu/manage/$menu_name/add");
    $this->assertNoField('edit-langcode', 'The language selector field was hidden the page');
  }

  /**
   * Tests menu configuration is still English after English has been deleted.
   */
  function testMenuLanguageRemovedEnglish() {
    // Create a test menu to test language settings.
    // Machine name has to be lowercase.
    $menu_name = Unicode::strtolower($this->randomName(16));
    $edit = array(
      'id' => $menu_name,
      'description' => '',
      'label' => $this->randomString(),
      'langcode' => 'en',
    );
    $this->drupalPostForm('admin/structure/menu/add', $edit, t('Save'));

    // Check that the language settings were saved.
    $menu = menu_ui_load($menu_name);
    $this->assertEqual($menu->langcode, 'en');

    // Remove English language. To do that another language has to be set as
    // default.
    $language = language_load('cs');
    $language->default = TRUE;
    language_save($language);
    language_delete('en');

    // Save the menu again and check if the language is still the same.
    $this->drupalPostForm("admin/structure/menu/manage/$menu_name", array(), t('Save'));
    $menu = menu_ui_load($menu_name);
    $this->assertEqual($menu->langcode, 'en');
  }

}