e34518e0ae0100e693ff2b79b5de0f0c1c5a6440
[yaffs-website] / web / core / modules / shortcut / src / Tests / ShortcutSetsTest.php
1 <?php
2
3 namespace Drupal\shortcut\Tests;
4
5 use Drupal\shortcut\Entity\ShortcutSet;
6
7 /**
8  * Create, view, edit, delete, and change shortcut sets.
9  *
10  * @group shortcut
11  */
12 class ShortcutSetsTest extends ShortcutTestBase {
13
14   /**
15    * Modules to enable.
16    *
17    * @var string[]
18    */
19   public static $modules = ['block'];
20
21   /**
22    * {@inheritdoc}
23    */
24   protected function setUp() {
25     parent::setUp();
26
27     $this->drupalPlaceBlock('local_actions_block');
28   }
29
30   /**
31    * Tests creating a shortcut set.
32    */
33   public function testShortcutSetAdd() {
34     $this->drupalGet('admin/config/user-interface/shortcut');
35     $this->clickLink(t('Add shortcut set'));
36     $edit = [
37       'label' => $this->randomMachineName(),
38       'id' => strtolower($this->randomMachineName()),
39     ];
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.');
45   }
46
47   /**
48    * Tests editing a shortcut set.
49    */
50   public function testShortcutSetEdit() {
51     $set = $this->set;
52     $shortcuts = $set->getShortcuts();
53
54     // Visit the shortcut set edit admin ui.
55     $this->drupalGet('admin/config/user-interface/shortcut/manage/' . $set->id() . '/customize');
56
57     // Test for the page title.
58     $this->assertTitle(t('List links') . ' | Drupal');
59
60     // Test for the table.
61     $element = $this->xpath('//div[@class="layout-content"]//table');
62     $this->assertTrue($element, 'Shortcut entity list table found.');
63
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.');
67
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]);
72     }
73
74     // Look for test shortcuts in the table.
75     $weight = count($shortcuts);
76     $edit = [];
77     foreach ($shortcuts as $shortcut) {
78       $title = $shortcut->getTitle();
79
80       // Confirm that a link to the shortcut is found within the table.
81       $this->assertLink($title);
82
83       // Look for a test shortcut weight select form element.
84       $this->assertFieldByName('shortcuts[links][' . $shortcut->id() . '][weight]');
85
86       // Change the weight of the shortcut.
87       $edit['shortcuts[links][' . $shortcut->id() . '][weight]'] = $weight;
88       $weight--;
89     }
90
91     $this->drupalPostForm(NULL, $edit, t('Save'));
92     $this->assertRaw(t('The shortcut set has been updated.'));
93
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()));
98   }
99
100   /**
101    * Tests switching a user's own shortcut set.
102    */
103   public function testShortcutSetSwitchOwn() {
104     $new_set = $this->generateShortcutSet($this->randomMachineName());
105
106     // Attempt to switch the default shortcut set to the newly created shortcut
107     // set.
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.');
112   }
113
114   /**
115    * Tests switching another user's shortcut set.
116    */
117   public function testShortcutSetAssign() {
118     $new_set = $this->generateShortcutSet($this->randomMachineName());
119
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.");
123   }
124
125   /**
126    * Tests switching a user's shortcut set and creating one at the same time.
127    */
128   public function testShortcutSetSwitchCreate() {
129     $edit = [
130       'set' => 'new',
131       'id' => strtolower($this->randomMachineName()),
132       'label' => $this->randomString(),
133     ];
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.');
138   }
139
140   /**
141    * Tests switching a user's shortcut set without providing a new set name.
142    */
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');
150   }
151
152   /**
153    * Tests renaming a shortcut set.
154    */
155   public function testShortcutSetRename() {
156     $set = $this->set;
157
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.');
164   }
165
166   /**
167    * Tests unassigning a shortcut set.
168    */
169   public function testShortcutSetUnassign() {
170     $new_set = $this->generateShortcutSet($this->randomMachineName());
171
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.");
178   }
179
180   /**
181    * Tests deleting a shortcut set.
182    */
183   public function testShortcutSetDelete() {
184     $new_set = $this->generateShortcutSet($this->randomMachineName());
185
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.');
189   }
190
191   /**
192    * Tests deleting the default shortcut set.
193    */
194   public function testShortcutSetDeleteDefault() {
195     $this->drupalGet('admin/config/user-interface/shortcut/manage/default/delete');
196     $this->assertResponse(403);
197   }
198
199   /**
200    * Tests creating a new shortcut set with a defined set name.
201    */
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.');
209   }
210
211 }