3 namespace Drupal\shortcut\Tests;
5 use Drupal\shortcut\Entity\ShortcutSet;
8 * Create, view, edit, delete, and change shortcut sets.
12 class ShortcutSetsTest extends ShortcutTestBase {
19 public static $modules = ['block'];
24 protected function setUp() {
27 $this->drupalPlaceBlock('local_actions_block');
31 * Tests creating a shortcut set.
33 public function testShortcutSetAdd() {
34 $this->drupalGet('admin/config/user-interface/shortcut');
35 $this->clickLink(t('Add shortcut set'));
37 'label' => $this->randomMachineName(),
38 'id' => strtolower($this->randomMachineName()),
40 $this->drupalPostForm(NULL, $edit, t('Save'));
41 $new_set = $this->container->get('entity.manager')->getStorage('shortcut_set')->load($edit['id']);
42 $this->assertIdentical($new_set->id(), $edit['id'], 'Successfully created a shortcut set.');
43 $this->drupalGet('user/' . $this->adminUser->id() . '/shortcuts');
44 $this->assertText($new_set->label(), 'Generated shortcut set was listed as a choice on the user account page.');
48 * Tests editing a shortcut set.
50 public function testShortcutSetEdit() {
52 $shortcuts = $set->getShortcuts();
54 // Visit the shortcut set edit admin ui.
55 $this->drupalGet('admin/config/user-interface/shortcut/manage/' . $set->id() . '/customize');
57 // Test for the page title.
58 $this->assertTitle(t('List links') . ' | Drupal');
60 // Test for the table.
61 $element = $this->xpath('//div[@class="layout-content"]//table');
62 $this->assertTrue($element, 'Shortcut entity list table found.');
64 // Test the table header.
65 $elements = $this->xpath('//div[@class="layout-content"]//table/thead/tr/th');
66 $this->assertEqual(count($elements), 3, 'Correct number of table header cells found.');
68 // Test the contents of each th cell.
69 $expected_items = [t('Name'), t('Weight'), t('Operations')];
70 foreach ($elements as $key => $element) {
71 $this->assertEqual((string) $element[0], $expected_items[$key]);
74 // Look for test shortcuts in the table.
75 $weight = count($shortcuts);
77 foreach ($shortcuts as $shortcut) {
78 $title = $shortcut->getTitle();
80 // Confirm that a link to the shortcut is found within the table.
81 $this->assertLink($title);
83 // Look for a test shortcut weight select form element.
84 $this->assertFieldByName('shortcuts[links][' . $shortcut->id() . '][weight]');
86 // Change the weight of the shortcut.
87 $edit['shortcuts[links][' . $shortcut->id() . '][weight]'] = $weight;
91 $this->drupalPostForm(NULL, $edit, t('Save'));
92 $this->assertRaw(t('The shortcut set has been updated.'));
94 \Drupal::entityManager()->getStorage('shortcut')->resetCache();
95 // Check to ensure that the shortcut weights have changed and that
96 // ShortcutSet::.getShortcuts() returns shortcuts in the new order.
97 $this->assertIdentical(array_reverse(array_keys($shortcuts)), array_keys($set->getShortcuts()));
101 * Tests switching a user's own shortcut set.
103 public function testShortcutSetSwitchOwn() {
104 $new_set = $this->generateShortcutSet($this->randomMachineName());
106 // Attempt to switch the default shortcut set to the newly created shortcut
108 $this->drupalPostForm('user/' . $this->adminUser->id() . '/shortcuts', ['set' => $new_set->id()], t('Change set'));
109 $this->assertResponse(200);
110 $current_set = shortcut_current_displayed_set($this->adminUser);
111 $this->assertTrue($new_set->id() == $current_set->id(), 'Successfully switched own shortcut set.');
115 * Tests switching another user's shortcut set.
117 public function testShortcutSetAssign() {
118 $new_set = $this->generateShortcutSet($this->randomMachineName());
120 \Drupal::entityManager()->getStorage('shortcut_set')->assignUser($new_set, $this->shortcutUser);
121 $current_set = shortcut_current_displayed_set($this->shortcutUser);
122 $this->assertTrue($new_set->id() == $current_set->id(), "Successfully switched another user's shortcut set.");
126 * Tests switching a user's shortcut set and creating one at the same time.
128 public function testShortcutSetSwitchCreate() {
131 'id' => strtolower($this->randomMachineName()),
132 'label' => $this->randomString(),
134 $this->drupalPostForm('user/' . $this->adminUser->id() . '/shortcuts', $edit, t('Change set'));
135 $current_set = shortcut_current_displayed_set($this->adminUser);
136 $this->assertNotEqual($current_set->id(), $this->set->id(), 'A shortcut set can be switched to at the same time as it is created.');
137 $this->assertEqual($current_set->label(), $edit['label'], 'The new set is correctly assigned to the user.');
141 * Tests switching a user's shortcut set without providing a new set name.
143 public function testShortcutSetSwitchNoSetName() {
144 $edit = ['set' => 'new'];
145 $this->drupalPostForm('user/' . $this->adminUser->id() . '/shortcuts', $edit, t('Change set'));
146 $this->assertText(t('The new set label is required.'));
147 $current_set = shortcut_current_displayed_set($this->adminUser);
148 $this->assertEqual($current_set->id(), $this->set->id(), 'Attempting to switch to a new shortcut set without providing a set name does not succeed.');
149 $this->assertFieldByXPath("//input[@name='label' and contains(concat(' ', normalize-space(@class), ' '), ' error ')]", NULL, 'The new set label field has the error class');
153 * Tests renaming a shortcut set.
155 public function testShortcutSetRename() {
158 $new_label = $this->randomMachineName();
159 $this->drupalGet('admin/config/user-interface/shortcut');
160 $this->clickLink(t('Edit shortcut set'));
161 $this->drupalPostForm(NULL, ['label' => $new_label], t('Save'));
162 $set = ShortcutSet::load($set->id());
163 $this->assertTrue($set->label() == $new_label, 'Shortcut set has been successfully renamed.');
167 * Tests unassigning a shortcut set.
169 public function testShortcutSetUnassign() {
170 $new_set = $this->generateShortcutSet($this->randomMachineName());
172 $shortcut_set_storage = \Drupal::entityManager()->getStorage('shortcut_set');
173 $shortcut_set_storage->assignUser($new_set, $this->shortcutUser);
174 $shortcut_set_storage->unassignUser($this->shortcutUser);
175 $current_set = shortcut_current_displayed_set($this->shortcutUser);
176 $default_set = shortcut_default_set($this->shortcutUser);
177 $this->assertTrue($current_set->id() == $default_set->id(), "Successfully unassigned another user's shortcut set.");
181 * Tests deleting a shortcut set.
183 public function testShortcutSetDelete() {
184 $new_set = $this->generateShortcutSet($this->randomMachineName());
186 $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $new_set->id() . '/delete', [], t('Delete'));
187 $sets = ShortcutSet::loadMultiple();
188 $this->assertFalse(isset($sets[$new_set->id()]), 'Successfully deleted a shortcut set.');
192 * Tests deleting the default shortcut set.
194 public function testShortcutSetDeleteDefault() {
195 $this->drupalGet('admin/config/user-interface/shortcut/manage/default/delete');
196 $this->assertResponse(403);
200 * Tests creating a new shortcut set with a defined set name.
202 public function testShortcutSetCreateWithSetName() {
203 $random_name = $this->randomMachineName();
204 $new_set = $this->generateShortcutSet($random_name, $random_name);
205 $sets = ShortcutSet::loadMultiple();
206 $this->assertTrue(isset($sets[$random_name]), 'Successfully created a shortcut set with a defined set name.');
207 $this->drupalGet('user/' . $this->adminUser->id() . '/shortcuts');
208 $this->assertText($new_set->label(), 'Generated shortcut set was listed as a choice on the user account page.');