shortcut.test 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. <?php
  2. /**
  3. * @file
  4. * Tests for shortcut.module.
  5. */
  6. /**
  7. * Defines base class for shortcut test cases.
  8. */
  9. class ShortcutTestCase extends DrupalWebTestCase {
  10. /**
  11. * User with permission to administer shortcuts.
  12. */
  13. protected $admin_user;
  14. /**
  15. * User with permission to use shortcuts, but not administer them.
  16. */
  17. protected $shortcut_user;
  18. /**
  19. * Generic node used for testing.
  20. */
  21. protected $node;
  22. /**
  23. * Site-wide default shortcut set.
  24. */
  25. protected $set;
  26. function setUp() {
  27. parent::setUp('toolbar', 'shortcut');
  28. // Create users.
  29. $this->admin_user = $this->drupalCreateUser(array('access toolbar', 'administer shortcuts', 'view the administration theme', 'create article content', 'create page content', 'access content overview'));
  30. $this->shortcut_user = $this->drupalCreateUser(array('customize shortcut links', 'switch shortcut sets'));
  31. // Create a node.
  32. $this->node = $this->drupalCreateNode(array('type' => 'article'));
  33. // Log in as admin and grab the default shortcut set.
  34. $this->drupalLogin($this->admin_user);
  35. $this->set = shortcut_set_load(SHORTCUT_DEFAULT_SET_NAME);
  36. shortcut_set_assign_user($this->set, $this->admin_user);
  37. }
  38. /**
  39. * Creates a generic shortcut set.
  40. */
  41. function generateShortcutSet($title = '', $default_links = TRUE) {
  42. $set = new stdClass();
  43. $set->title = empty($title) ? $this->randomName(10) : $title;
  44. if ($default_links) {
  45. $set->links = array();
  46. $set->links[] = $this->generateShortcutLink('node/add');
  47. $set->links[] = $this->generateShortcutLink('admin/content');
  48. }
  49. shortcut_set_save($set);
  50. return $set;
  51. }
  52. /**
  53. * Creates a generic shortcut link.
  54. */
  55. function generateShortcutLink($path, $title = '') {
  56. $link = array(
  57. 'link_path' => $path,
  58. 'link_title' => !empty($title) ? $title : $this->randomName(10),
  59. );
  60. return $link;
  61. }
  62. /**
  63. * Extracts information from shortcut set links.
  64. *
  65. * @param object $set
  66. * The shortcut set object to extract information from.
  67. * @param string $key
  68. * The array key indicating what information to extract from each link:
  69. * - 'link_path': Extract link paths.
  70. * - 'link_title': Extract link titles.
  71. * - 'mlid': Extract the menu link item ID numbers.
  72. *
  73. * @return array
  74. * Array of the requested information from each link.
  75. */
  76. function getShortcutInformation($set, $key) {
  77. $info = array();
  78. foreach ($set->links as $link) {
  79. $info[] = $link[$key];
  80. }
  81. return $info;
  82. }
  83. }
  84. /**
  85. * Defines shortcut links test cases.
  86. */
  87. class ShortcutLinksTestCase extends ShortcutTestCase {
  88. public static function getInfo() {
  89. return array(
  90. 'name' => 'Shortcut link functionality',
  91. 'description' => 'Create, view, edit, delete, and change shortcut links.',
  92. 'group' => 'Shortcut',
  93. );
  94. }
  95. /**
  96. * Tests that creating a shortcut works properly.
  97. */
  98. function testShortcutLinkAdd() {
  99. $set = $this->set;
  100. // Create an alias for the node so we can test aliases.
  101. $path = array(
  102. 'source' => 'node/' . $this->node->nid,
  103. 'alias' => $this->randomName(8),
  104. );
  105. path_save($path);
  106. // Create some paths to test.
  107. $test_cases = array(
  108. array('path' => ''),
  109. array('path' => 'admin'),
  110. array('path' => 'admin/config/system/site-information'),
  111. array('path' => "node/{$this->node->nid}/edit"),
  112. array('path' => $path['alias']),
  113. );
  114. // Check that each new shortcut links where it should.
  115. foreach ($test_cases as $test) {
  116. $title = $this->randomName(10);
  117. $form_data = array(
  118. 'shortcut_link[link_title]' => $title,
  119. 'shortcut_link[link_path]' => $test['path'],
  120. );
  121. $this->drupalPost('admin/config/user-interface/shortcut/' . $set->set_name . '/add-link', $form_data, t('Save'));
  122. $this->assertResponse(200);
  123. $saved_set = shortcut_set_load($set->set_name);
  124. $paths = $this->getShortcutInformation($saved_set, 'link_path');
  125. $test_path = empty($test['path']) ? '<front>' : $test['path'];
  126. $this->assertTrue(in_array(drupal_get_normal_path($test_path), $paths), 'Shortcut created: '. $test['path']);
  127. $this->assertLink($title, 0, 'Shortcut link found on the page.');
  128. }
  129. }
  130. /**
  131. * Tests that the "add to shortcut" link changes to "remove shortcut".
  132. */
  133. function testShortcutQuickLink() {
  134. $this->drupalGet($this->set->links[0]['link_path']);
  135. $this->assertRaw(t('Remove from %title shortcuts', array('%title' => $this->set->title)), '"Add to shortcuts" link properly switched to "Remove from shortcuts".');
  136. }
  137. /**
  138. * Tests that shortcut links can be renamed.
  139. */
  140. function testShortcutLinkRename() {
  141. $set = $this->set;
  142. // Attempt to rename shortcut link.
  143. $new_link_name = $this->randomName(10);
  144. $this->drupalPost('admin/config/user-interface/shortcut/link/' . $set->links[0]['mlid'], array('shortcut_link[link_title]' => $new_link_name, 'shortcut_link[link_path]' => $set->links[0]['link_path']), t('Save'));
  145. $saved_set = shortcut_set_load($set->set_name);
  146. $titles = $this->getShortcutInformation($saved_set, 'link_title');
  147. $this->assertTrue(in_array($new_link_name, $titles), 'Shortcut renamed: ' . $new_link_name);
  148. $this->assertLink($new_link_name, 0, 'Renamed shortcut link appears on the page.');
  149. }
  150. /**
  151. * Tests that changing the path of a shortcut link works.
  152. */
  153. function testShortcutLinkChangePath() {
  154. $set = $this->set;
  155. // Tests changing a shortcut path.
  156. $new_link_path = 'admin/config';
  157. $this->drupalPost('admin/config/user-interface/shortcut/link/' . $set->links[0]['mlid'], array('shortcut_link[link_title]' => $set->links[0]['link_title'], 'shortcut_link[link_path]' => $new_link_path), t('Save'));
  158. $saved_set = shortcut_set_load($set->set_name);
  159. $paths = $this->getShortcutInformation($saved_set, 'link_path');
  160. $this->assertTrue(in_array($new_link_path, $paths), 'Shortcut path changed: ' . $new_link_path);
  161. $this->assertLinkByHref($new_link_path, 0, 'Shortcut with new path appears on the page.');
  162. }
  163. /**
  164. * Tests deleting a shortcut link.
  165. */
  166. function testShortcutLinkDelete() {
  167. $set = $this->set;
  168. $this->drupalPost('admin/config/user-interface/shortcut/link/' . $set->links[0]['mlid'] . '/delete', array(), 'Delete');
  169. $saved_set = shortcut_set_load($set->set_name);
  170. $mlids = $this->getShortcutInformation($saved_set, 'mlid');
  171. $this->assertFalse(in_array($set->links[0]['mlid'], $mlids), 'Successfully deleted a shortcut.');
  172. }
  173. /**
  174. * Tests that the add shortcut link is not displayed for 404/403 errors.
  175. *
  176. * Tests that the "Add to shortcuts" link is not displayed on a page not
  177. * found or a page the user does not have access to.
  178. */
  179. function testNoShortcutLink() {
  180. // Change to a theme that displays shortcuts.
  181. variable_set('theme_default', 'seven');
  182. $this->drupalGet('page-that-does-not-exist');
  183. $this->assertNoRaw('add-shortcut', 'Add to shortcuts link was not shown on a page not found.');
  184. // The user does not have access to this path.
  185. $this->drupalGet('admin/modules');
  186. $this->assertNoRaw('add-shortcut', 'Add to shortcuts link was not shown on a page the user does not have access to.');
  187. // Verify that the testing mechanism works by verifying the shortcut
  188. // link appears on admin/content/node.
  189. $this->drupalGet('admin/content/node');
  190. $this->assertRaw('add-shortcut', 'Add to shortcuts link was shown on a page the user does have access to.');
  191. }
  192. }
  193. /**
  194. * Defines shortcut set test cases.
  195. */
  196. class ShortcutSetsTestCase extends ShortcutTestCase {
  197. public static function getInfo() {
  198. return array(
  199. 'name' => 'Shortcut set functionality',
  200. 'description' => 'Create, view, edit, delete, and change shortcut sets.',
  201. 'group' => 'Shortcut',
  202. );
  203. }
  204. /**
  205. * Tests creating a shortcut set.
  206. */
  207. function testShortcutSetAdd() {
  208. $new_set = $this->generateShortcutSet($this->randomName(10));
  209. $sets = shortcut_sets();
  210. $this->assertTrue(isset($sets[$new_set->set_name]), 'Successfully created a shortcut set.');
  211. $this->drupalGet('user/' . $this->admin_user->uid . '/shortcuts');
  212. $this->assertText($new_set->title, 'Generated shortcut set was listed as a choice on the user account page.');
  213. }
  214. /**
  215. * Tests switching a user's own shortcut set.
  216. */
  217. function testShortcutSetSwitchOwn() {
  218. $new_set = $this->generateShortcutSet($this->randomName(10));
  219. // Attempt to switch the default shortcut set to the newly created shortcut
  220. // set.
  221. $this->drupalPost('user/' . $this->admin_user->uid . '/shortcuts', array('set' => $new_set->set_name), t('Change set'));
  222. $this->assertResponse(200);
  223. $current_set = shortcut_current_displayed_set($this->admin_user);
  224. $this->assertTrue($new_set->set_name == $current_set->set_name, 'Successfully switched own shortcut set.');
  225. }
  226. /**
  227. * Tests switching another user's shortcut set.
  228. */
  229. function testShortcutSetAssign() {
  230. $new_set = $this->generateShortcutSet($this->randomName(10));
  231. shortcut_set_assign_user($new_set, $this->shortcut_user);
  232. $current_set = shortcut_current_displayed_set($this->shortcut_user);
  233. $this->assertTrue($new_set->set_name == $current_set->set_name, "Successfully switched another user's shortcut set.");
  234. }
  235. /**
  236. * Tests switching a user's shortcut set and creating one at the same time.
  237. */
  238. function testShortcutSetSwitchCreate() {
  239. $edit = array(
  240. 'set' => 'new',
  241. 'new' => $this->randomName(10),
  242. );
  243. $this->drupalPost('user/' . $this->admin_user->uid . '/shortcuts', $edit, t('Change set'));
  244. $current_set = shortcut_current_displayed_set($this->admin_user);
  245. $this->assertNotEqual($current_set->set_name, $this->set->set_name, 'A shortcut set can be switched to at the same time as it is created.');
  246. $this->assertEqual($current_set->title, $edit['new'], 'The new set is correctly assigned to the user.');
  247. }
  248. /**
  249. * Tests switching a user's shortcut set without providing a new set name.
  250. */
  251. function testShortcutSetSwitchNoSetName() {
  252. $edit = array('set' => 'new');
  253. $this->drupalPost('user/' . $this->admin_user->uid . '/shortcuts', $edit, t('Change set'));
  254. $this->assertText(t('The new set name is required.'));
  255. $current_set = shortcut_current_displayed_set($this->admin_user);
  256. $this->assertEqual($current_set->set_name, $this->set->set_name, 'Attempting to switch to a new shortcut set without providing a set name does not succeed.');
  257. }
  258. /**
  259. * Tests that shortcut_set_save() correctly updates existing links.
  260. */
  261. function testShortcutSetSave() {
  262. $set = $this->set;
  263. $old_mlids = $this->getShortcutInformation($set, 'mlid');
  264. $set->links[] = $this->generateShortcutLink('admin', $this->randomName(10));
  265. shortcut_set_save($set);
  266. $saved_set = shortcut_set_load($set->set_name);
  267. $new_mlids = $this->getShortcutInformation($saved_set, 'mlid');
  268. $this->assertTrue(count(array_intersect($old_mlids, $new_mlids)) == count($old_mlids), 'shortcut_set_save() did not inadvertently change existing mlids.');
  269. }
  270. /**
  271. * Tests renaming a shortcut set.
  272. */
  273. function testShortcutSetRename() {
  274. $set = $this->set;
  275. $new_title = $this->randomName(10);
  276. $this->drupalPost('admin/config/user-interface/shortcut/' . $set->set_name . '/edit', array('title' => $new_title), t('Save'));
  277. $set = shortcut_set_load($set->set_name);
  278. $this->assertTrue($set->title == $new_title, 'Shortcut set has been successfully renamed.');
  279. }
  280. /**
  281. * Tests renaming a shortcut set to the same name as another set.
  282. */
  283. function testShortcutSetRenameAlreadyExists() {
  284. $set = $this->generateShortcutSet($this->randomName(10));
  285. $existing_title = $this->set->title;
  286. $this->drupalPost('admin/config/user-interface/shortcut/' . $set->set_name . '/edit', array('title' => $existing_title), t('Save'));
  287. $this->assertRaw(t('The shortcut set %name already exists. Choose another name.', array('%name' => $existing_title)));
  288. $set = shortcut_set_load($set->set_name);
  289. $this->assertNotEqual($set->title, $existing_title, format_string('The shortcut set %title cannot be renamed to %new-title because a shortcut set with that title already exists.', array('%title' => $set->title, '%new-title' => $existing_title)));
  290. }
  291. /**
  292. * Tests unassigning a shortcut set.
  293. */
  294. function testShortcutSetUnassign() {
  295. $new_set = $this->generateShortcutSet($this->randomName(10));
  296. shortcut_set_assign_user($new_set, $this->shortcut_user);
  297. shortcut_set_unassign_user($this->shortcut_user);
  298. $current_set = shortcut_current_displayed_set($this->shortcut_user);
  299. $default_set = shortcut_default_set($this->shortcut_user);
  300. $this->assertTrue($current_set->set_name == $default_set->set_name, "Successfully unassigned another user's shortcut set.");
  301. }
  302. /**
  303. * Tests deleting a shortcut set.
  304. */
  305. function testShortcutSetDelete() {
  306. $new_set = $this->generateShortcutSet($this->randomName(10));
  307. $this->drupalPost('admin/config/user-interface/shortcut/' . $new_set->set_name . '/delete', array(), t('Delete'));
  308. $sets = shortcut_sets();
  309. $this->assertFalse(isset($sets[$new_set->set_name]), 'Successfully deleted a shortcut set.');
  310. }
  311. /**
  312. * Tests deleting the default shortcut set.
  313. */
  314. function testShortcutSetDeleteDefault() {
  315. $this->drupalGet('admin/config/user-interface/shortcut/' . SHORTCUT_DEFAULT_SET_NAME . '/delete');
  316. $this->assertResponse(403);
  317. }
  318. }