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