ShortcutSetsTest.php 7.73 KB
Newer Older
1
2
3
4
5
6
7
8
<?php

/**
 * @file
 * Definition of Drupal\shortcut\Tests\ShortcutSetsTest.
 */

namespace Drupal\shortcut\Tests;
9
use Drupal\shortcut\Entity\ShortcutSet;
10
11

/**
12
13
14
 * Create, view, edit, delete, and change shortcut sets.
 *
 * @group shortcut
15
16
17
18
19
20
21
 */
class ShortcutSetsTest extends ShortcutTestBase {

  /**
   * Tests creating a shortcut set.
   */
  function testShortcutSetAdd() {
22
23
24
    $this->drupalGet('admin/config/user-interface/shortcut');
    $this->clickLink(t('Add shortcut set'));
    $edit = array(
25
26
      'label' => $this->randomMachineName(),
      'id' => strtolower($this->randomMachineName()),
27
    );
28
    $this->drupalPostForm(NULL, $edit, t('Save'));
29
    $new_set = $this->container->get('entity.manager')->getStorage('shortcut_set')->load($edit['id']);
30
    $this->assertIdentical($new_set->id(), $edit['id'], 'Successfully created a shortcut set.');
31
    $this->drupalGet('user/' . $this->adminUser->id() . '/shortcuts');
32
    $this->assertText($new_set->label(), 'Generated shortcut set was listed as a choice on the user account page.');
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
  /**
   * Tests editing a shortcut set.
   */
  function testShortcutSetEdit() {
    $set = $this->set;
    $shortcuts = $set->getShortcuts();

    // Visit the shortcut set edit admin ui.
    $this->drupalGet('admin/config/user-interface/shortcut/manage/' . $set->id() . '/customize');

    // Test for the page title.
    $this->assertTitle(t('List links') . ' | Drupal');

    // Test for the table.
    $element = $this->xpath('//div[@class="layout-content"]//table');
    $this->assertTrue($element, 'Shortcut entity list table found.');

    // Test the table header.
    $elements = $this->xpath('//div[@class="layout-content"]//table/thead/tr/th');
    $this->assertEqual(count($elements), 3, 'Correct number of table header cells found.');

    // Test the contents of each th cell.
    $expected_items = array(t('Name'), t('Weight'), t('Operations'));
    foreach ($elements as $key => $element) {
      $this->assertIdentical((string) $element[0], $expected_items[$key]);
    }

    // Look for test shortcuts in the table.
    $weight = count($shortcuts);
    $edit = array();
    foreach ($shortcuts as $shortcut) {
      $title = $shortcut->getTitle();

      // Confirm that a link to the shortcut is found within the table.
      $this->assertLink($title);

      // Look for a test shortcut weight select form element.
      $this->assertFieldByName('shortcuts[links][' . $shortcut->id() . '][weight]');

      // Change the weight of the shortcut.
      $edit['shortcuts[links][' . $shortcut->id() . '][weight]'] = $weight;
      $weight--;
    }

    $this->drupalPostForm(NULL, $edit, t('Save changes'));
    $this->assertRaw(t('The shortcut set has been updated.'));

82
83
84
85
    \Drupal::entityManager()->getStorage('shortcut')->resetCache();
    // Check to ensure that the shortcut weights have changed and that
    // ShortcutSet::.getShortcuts() returns shortcuts in the new order.
    $this->assertIdentical(array_reverse(array_keys($shortcuts)), array_keys($set->getShortcuts()));
86
87
  }

88
89
90
91
  /**
   * Tests switching a user's own shortcut set.
   */
  function testShortcutSetSwitchOwn() {
92
    $new_set = $this->generateShortcutSet($this->randomMachineName());
93
94
95

    // Attempt to switch the default shortcut set to the newly created shortcut
    // set.
96
    $this->drupalPostForm('user/' . $this->adminUser->id() . '/shortcuts', array('set' => $new_set->id()), t('Change set'));
97
    $this->assertResponse(200);
98
    $current_set = shortcut_current_displayed_set($this->adminUser);
99
    $this->assertTrue($new_set->id() == $current_set->id(), 'Successfully switched own shortcut set.');
100
101
102
103
104
105
  }

  /**
   * Tests switching another user's shortcut set.
   */
  function testShortcutSetAssign() {
106
    $new_set = $this->generateShortcutSet($this->randomMachineName());
107

108
    \Drupal::entityManager()->getStorage('shortcut_set')->assignUser($new_set, $this->shortcutUser);
109
    $current_set = shortcut_current_displayed_set($this->shortcutUser);
110
    $this->assertTrue($new_set->id() == $current_set->id(), "Successfully switched another user's shortcut set.");
111
112
113
114
115
116
117
118
  }

  /**
   * Tests switching a user's shortcut set and creating one at the same time.
   */
  function testShortcutSetSwitchCreate() {
    $edit = array(
      'set' => 'new',
119
      'id' => strtolower($this->randomMachineName()),
120
      'label' => $this->randomString(),
121
    );
122
123
    $this->drupalPostForm('user/' . $this->adminUser->id() . '/shortcuts', $edit, t('Change set'));
    $current_set = shortcut_current_displayed_set($this->adminUser);
124
125
    $this->assertNotEqual($current_set->id(), $this->set->id(), 'A shortcut set can be switched to at the same time as it is created.');
    $this->assertEqual($current_set->label(), $edit['label'], 'The new set is correctly assigned to the user.');
126
127
128
129
130
131
132
  }

  /**
   * Tests switching a user's shortcut set without providing a new set name.
   */
  function testShortcutSetSwitchNoSetName() {
    $edit = array('set' => 'new');
133
    $this->drupalPostForm('user/' . $this->adminUser->id() . '/shortcuts', $edit, t('Change set'));
134
    $this->assertText(t('The new set label is required.'));
135
    $current_set = shortcut_current_displayed_set($this->adminUser);
136
    $this->assertEqual($current_set->id(), $this->set->id(), 'Attempting to switch to a new shortcut set without providing a set name does not succeed.');
137
    $this->assertFieldByXPath("//input[@name='label' and contains(concat(' ', normalize-space(@class), ' '), ' error ')]", NULL, 'The new set label field has the error class');
138
139
140
141
142
143
144
145
  }

  /**
   * Tests renaming a shortcut set.
   */
  function testShortcutSetRename() {
    $set = $this->set;

146
    $new_label = $this->randomMachineName();
147
    $this->drupalGet('admin/config/user-interface/shortcut');
148
    $this->clickLink(t('Edit shortcut set'));
149
    $this->drupalPostForm(NULL, array('label' => $new_label), t('Save'));
150
    $set = ShortcutSet::load($set->id());
151
    $this->assertTrue($set->label() == $new_label, 'Shortcut set has been successfully renamed.');
152
153
154
155
156
157
  }

  /**
   * Tests unassigning a shortcut set.
   */
  function testShortcutSetUnassign() {
158
    $new_set = $this->generateShortcutSet($this->randomMachineName());
159

160
161
162
    $shortcut_set_storage = \Drupal::entityManager()->getStorage('shortcut_set');
    $shortcut_set_storage->assignUser($new_set, $this->shortcutUser);
    $shortcut_set_storage->unassignUser($this->shortcutUser);
163
164
    $current_set = shortcut_current_displayed_set($this->shortcutUser);
    $default_set = shortcut_default_set($this->shortcutUser);
165
    $this->assertTrue($current_set->id() == $default_set->id(), "Successfully unassigned another user's shortcut set.");
166
167
168
169
170
171
  }

  /**
   * Tests deleting a shortcut set.
   */
  function testShortcutSetDelete() {
172
    $new_set = $this->generateShortcutSet($this->randomMachineName());
173

174
    $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $new_set->id() . '/delete', array(), t('Delete'));
175
    $sets = ShortcutSet::loadMultiple();
176
    $this->assertFalse(isset($sets[$new_set->id()]), 'Successfully deleted a shortcut set.');
177
178
179
180
181
182
  }

  /**
   * Tests deleting the default shortcut set.
   */
  function testShortcutSetDeleteDefault() {
183
    $this->drupalGet('admin/config/user-interface/shortcut/manage/default/delete');
184
185
    $this->assertResponse(403);
  }
186
187
188
189
190

  /**
   * Tests creating a new shortcut set with a defined set name.
   */
  function testShortcutSetCreateWithSetName() {
191
    $random_name = $this->randomMachineName();
192
    $new_set = $this->generateShortcutSet($random_name, $random_name);
193
    $sets = ShortcutSet::loadMultiple();
194
    $this->assertTrue(isset($sets[$random_name]), 'Successfully created a shortcut set with a defined set name.');
195
    $this->drupalGet('user/' . $this->adminUser->id() . '/shortcuts');
196
    $this->assertText($new_set->label(), 'Generated shortcut set was listed as a choice on the user account page.');
197
  }
198
}