shortcut.test 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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' => 'admin'),
  109. array('path' => 'admin/config/system/site-information'),
  110. array('path' => "node/{$this->node->nid}/edit"),
  111. array('path' => $path['alias']),
  112. );
  113. // Check that each new shortcut links where it should.
  114. foreach ($test_cases as $test) {
  115. $title = $this->randomName(10);
  116. $form_data = array(
  117. 'shortcut_link[link_title]' => $title,
  118. 'shortcut_link[link_path]' => $test['path'],
  119. );
  120. $this->drupalPost('admin/config/user-interface/shortcut/' . $set->set_name . '/add-link', $form_data, t('Save'));
  121. $this->assertResponse(200);
  122. $saved_set = shortcut_set_load($set->set_name);
  123. $paths = $this->getShortcutInformation($saved_set, 'link_path');
  124. $this->assertTrue(in_array(drupal_get_normal_path($test['path']), $paths), 'Shortcut created: '. $test['path']);
  125. $this->assertLink($title, 0, 'Shortcut link found on the page.');
  126. }
  127. }
  128. /**
  129. * Tests that the "add to shortcut" link changes to "remove shortcut".
  130. */
  131. function testShortcutQuickLink() {
  132. $this->drupalGet($this->set->links[0]['link_path']);
  133. $this->assertRaw(t('Remove from %title shortcuts', array('%title' => $this->set->title)), '"Add to shortcuts" link properly switched to "Remove from shortcuts".');
  134. }
  135. /**
  136. * Tests that shortcut links can be renamed.
  137. */
  138. function testShortcutLinkRename() {
  139. $set = $this->set;
  140. // Attempt to rename shortcut link.
  141. $new_link_name = $this->randomName(10);
  142. $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'));
  143. $saved_set = shortcut_set_load($set->set_name);
  144. $titles = $this->getShortcutInformation($saved_set, 'link_title');
  145. $this->assertTrue(in_array($new_link_name, $titles), 'Shortcut renamed: ' . $new_link_name);
  146. $this->assertLink($new_link_name, 0, 'Renamed shortcut link appears on the page.');
  147. }
  148. /**
  149. * Tests that changing the path of a shortcut link works.
  150. */
  151. function testShortcutLinkChangePath() {
  152. $set = $this->set;
  153. // Tests changing a shortcut path.
  154. $new_link_path = 'admin/config';
  155. $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'));
  156. $saved_set = shortcut_set_load($set->set_name);
  157. $paths = $this->getShortcutInformation($saved_set, 'link_path');
  158. $this->assertTrue(in_array($new_link_path, $paths), 'Shortcut path changed: ' . $new_link_path);
  159. $this->assertLinkByHref($new_link_path, 0, 'Shortcut with new path appears on the page.');
  160. }
  161. /**
  162. * Tests deleting a shortcut link.
  163. */
  164. function testShortcutLinkDelete() {
  165. $set = $this->set;
  166. $this->drupalPost('admin/config/user-interface/shortcut/link/' . $set->links[0]['mlid'] . '/delete', array(), 'Delete');
  167. $saved_set = shortcut_set_load($set->set_name);
  168. $mlids = $this->getShortcutInformation($saved_set, 'mlid');
  169. $this->assertFalse(in_array($set->links[0]['mlid'], $mlids), 'Successfully deleted a shortcut.');
  170. }
  171. /**
  172. * Tests that the add shortcut link is not displayed for 404/403 errors.
  173. *
  174. * Tests that the "Add to shortcuts" link is not displayed on a page not
  175. * found or a page the user does not have access to.
  176. */
  177. function testNoShortcutLink() {
  178. // Change to a theme that displays shortcuts.
  179. variable_set('theme_default', 'seven');
  180. $this->drupalGet('page-that-does-not-exist');
  181. $this->assertNoRaw('add-shortcut', t('Add to shortcuts link was not shown on a page not found.'));
  182. // The user does not have access to this path.
  183. $this->drupalGet('admin/modules');
  184. $this->assertNoRaw('add-shortcut', t('Add to shortcuts link was not shown on a page the user does not have access to.'));
  185. // Verify that the testing mechanism works by verifying the shortcut
  186. // link appears on admin/content/node.
  187. $this->drupalGet('admin/content/node');
  188. $this->assertRaw('add-shortcut', t('Add to shortcuts link was shown on a page the user does have access to.'));
  189. }
  190. }
  191. /**
  192. * Defines shortcut set test cases.
  193. */
  194. class ShortcutSetsTestCase extends ShortcutTestCase {
  195. public static function getInfo() {
  196. return array(
  197. 'name' => 'Shortcut set functionality',
  198. 'description' => 'Create, view, edit, delete, and change shortcut sets.',
  199. 'group' => 'Shortcut',
  200. );
  201. }
  202. /**
  203. * Tests creating a shortcut set.
  204. */
  205. function testShortcutSetAdd() {
  206. $new_set = $this->generateShortcutSet($this->randomName(10));
  207. $sets = shortcut_sets();
  208. $this->assertTrue(isset($sets[$new_set->set_name]), 'Successfully created a shortcut set.');
  209. $this->drupalGet('user/' . $this->admin_user->uid . '/shortcuts');
  210. $this->assertText($new_set->title, 'Generated shortcut set was listed as a choice on the user account page.');
  211. }
  212. /**
  213. * Tests switching a user's own shortcut set.
  214. */
  215. function testShortcutSetSwitchOwn() {
  216. $new_set = $this->generateShortcutSet($this->randomName(10));
  217. // Attempt to switch the default shortcut set to the newly created shortcut
  218. // set.
  219. $this->drupalPost('user/' . $this->admin_user->uid . '/shortcuts', array('set' => $new_set->set_name), t('Change set'));
  220. $this->assertResponse(200);
  221. $current_set = shortcut_current_displayed_set($this->admin_user);
  222. $this->assertTrue($new_set->set_name == $current_set->set_name, 'Successfully switched own shortcut set.');
  223. }
  224. /**
  225. * Tests switching another user's shortcut set.
  226. */
  227. function testShortcutSetAssign() {
  228. $new_set = $this->generateShortcutSet($this->randomName(10));
  229. shortcut_set_assign_user($new_set, $this->shortcut_user);
  230. $current_set = shortcut_current_displayed_set($this->shortcut_user);
  231. $this->assertTrue($new_set->set_name == $current_set->set_name, "Successfully switched another user's shortcut set.");
  232. }
  233. /**
  234. * Tests switching a user's shortcut set and creating one at the same time.
  235. */
  236. function testShortcutSetSwitchCreate() {
  237. $edit = array(
  238. 'set' => 'new',
  239. 'new' => $this->randomName(10),
  240. );
  241. $this->drupalPost('user/' . $this->admin_user->uid . '/shortcuts', $edit, t('Change set'));
  242. $current_set = shortcut_current_displayed_set($this->admin_user);
  243. $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.');
  244. $this->assertEqual($current_set->title, $edit['new'], 'The new set is correctly assigned to the user.');
  245. }
  246. /**
  247. * Tests switching a user's shortcut set without providing a new set name.
  248. */
  249. function testShortcutSetSwitchNoSetName() {
  250. $edit = array('set' => 'new');
  251. $this->drupalPost('user/' . $this->admin_user->uid . '/shortcuts', $edit, t('Change set'));
  252. $this->assertText(t('The new set name is required.'));
  253. $current_set = shortcut_current_displayed_set($this->admin_user);
  254. $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.');
  255. }
  256. /**
  257. * Tests that shortcut_set_save() correctly updates existing links.
  258. */
  259. function testShortcutSetSave() {
  260. $set = $this->set;
  261. $old_mlids = $this->getShortcutInformation($set, 'mlid');
  262. $set->links[] = $this->generateShortcutLink('admin', $this->randomName(10));
  263. shortcut_set_save($set);
  264. $saved_set = shortcut_set_load($set->set_name);
  265. $new_mlids = $this->getShortcutInformation($saved_set, 'mlid');
  266. $this->assertTrue(count(array_intersect($old_mlids, $new_mlids)) == count($old_mlids), 'shortcut_set_save() did not inadvertently change existing mlids.');
  267. }
  268. /**
  269. * Tests renaming a shortcut set.
  270. */
  271. function testShortcutSetRename() {
  272. $set = $this->set;
  273. $new_title = $this->randomName(10);
  274. $this->drupalPost('admin/config/user-interface/shortcut/' . $set->set_name . '/edit', array('title' => $new_title), t('Save'));
  275. $set = shortcut_set_load($set->set_name);
  276. $this->assertTrue($set->title == $new_title, 'Shortcut set has been successfully renamed.');
  277. }
  278. /**
  279. * Tests renaming a shortcut set to the same name as another set.
  280. */
  281. function testShortcutSetRenameAlreadyExists() {
  282. $set = $this->generateShortcutSet($this->randomName(10));
  283. $existing_title = $this->set->title;
  284. $this->drupalPost('admin/config/user-interface/shortcut/' . $set->set_name . '/edit', array('title' => $existing_title), t('Save'));
  285. $this->assertRaw(t('The shortcut set %name already exists. Choose another name.', array('%name' => $existing_title)));
  286. $set = shortcut_set_load($set->set_name);
  287. $this->assertNotEqual($set->title, $existing_title, t('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)));
  288. }
  289. /**
  290. * Tests unassigning a shortcut set.
  291. */
  292. function testShortcutSetUnassign() {
  293. $new_set = $this->generateShortcutSet($this->randomName(10));
  294. shortcut_set_assign_user($new_set, $this->shortcut_user);
  295. shortcut_set_unassign_user($this->shortcut_user);
  296. $current_set = shortcut_current_displayed_set($this->shortcut_user);
  297. $default_set = shortcut_default_set($this->shortcut_user);
  298. $this->assertTrue($current_set->set_name == $default_set->set_name, "Successfully unassigned another user's shortcut set.");
  299. }
  300. /**
  301. * Tests deleting a shortcut set.
  302. */
  303. function testShortcutSetDelete() {
  304. $new_set = $this->generateShortcutSet($this->randomName(10));
  305. $this->drupalPost('admin/config/user-interface/shortcut/' . $new_set->set_name . '/delete', array(), t('Delete'));
  306. $sets = shortcut_sets();
  307. $this->assertFalse(isset($sets[$new_set->set_name]), 'Successfully deleted a shortcut set.');
  308. }
  309. /**
  310. * Tests deleting the default shortcut set.
  311. */
  312. function testShortcutSetDeleteDefault() {
  313. $this->drupalGet('admin/config/user-interface/shortcut/' . SHORTCUT_DEFAULT_SET_NAME . '/delete');
  314. $this->assertResponse(403);
  315. }
  316. }