ShortcutLinksTest.php 11.1 KB
Newer Older
1 2 3 4
<?php

/**
 * @file
5
 * Contains \Drupal\shortcut\Tests\ShortcutLinksTest.
6 7 8
 */

namespace Drupal\shortcut\Tests;
9
use Drupal\shortcut\Entity\Shortcut;
10
use Drupal\shortcut\Entity\ShortcutSet;
11 12

/**
13 14 15
 * Create, view, edit, delete, and change shortcut links.
 *
 * @group shortcut
16 17 18
 */
class ShortcutLinksTest extends ShortcutTestBase {

19 20 21 22 23
  /**
   * Modules to enable.
   *
   * @var array
   */
24
  public static $modules = array('router_test', 'views');
25

26 27 28
  /**
   * Tests that creating a shortcut works properly.
   */
29
  public function testShortcutLinkAdd() {
30 31 32 33
    $set = $this->set;

    // Create an alias for the node so we can test aliases.
    $path = array(
34
      'source' => 'node/' . $this->node->id(),
35
      'alias' => $this->randomMachineName(8),
36
    );
37
    $this->container->get('path.alias_storage')->save($path['source'], $path['alias']);
38 39 40

    // Create some paths to test.
    $test_cases = array(
41
      array('path' => '', 'route_name' => '<front>'),
42 43 44 45 46 47 48
      array('path' => '<front>', 'route_name' => '<front>'),
      array('path' => 'admin', 'route_name' => 'system.admin'),
      array('path' => 'admin/config/system/site-information', 'route_name' => 'system.site_information_settings'),
      array('path' => 'node/' . $this->node->id() . '/edit', 'route_name' => 'entity.node.edit_form'),
      array('path' => $path['alias'], 'route_name' => 'entity.node.canonical'),
      array('path' => 'router_test/test2', 'route_name' => 'router_test.2'),
      array('path' => 'router_test/test3/value', 'route_name' => 'router_test.3'),
49 50 51 52
    );

    // Check that each new shortcut links where it should.
    foreach ($test_cases as $test) {
53
      $title = $this->randomMachineName();
54
      $form_data = array(
55
        'title[0][value]' => $title,
56
        'path' => $test['path'],
57
      );
58
      $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $set->id() . '/add-link', $form_data, t('Save'));
59
      $this->assertResponse(200);
60
      $saved_set = ShortcutSet::load($set->id());
61 62
      $routes = $this->getShortcutInformation($saved_set, 'route_name');
      $this->assertTrue(in_array($test['route_name'], $routes), 'Shortcut created: ' . $test['path']);
63 64
      $this->assertLink($title, 0, 'Shortcut link found on the page.');
    }
65 66 67 68 69 70 71 72 73 74 75 76
    $saved_set = ShortcutSet::load($set->id());
    // Test that saving and re-loading a shortcut preserves its values.
    $shortcuts = $saved_set->getShortcuts();
    foreach ($shortcuts as $entity) {
      // Test the node routes with parameters.
      if (strpos($entity->route_name->value, 'node.') === 0) {
        $entity->save();
        $loaded = Shortcut::load($entity->id());
        $this->assertEqual($entity->route_name->value, $loaded->route_name->value);
        $this->assertEqual($entity->get('route_parameters')->first()->getValue(), $loaded->get('route_parameters')->first()->getValue());
      }
    }
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95

    // Login as non admin user, to check that access is checked when creating
    // shortcuts.
    $this->drupalLogin($this->shortcut_user);
    $title = $this->randomMachineName();
    $form_data = [
      'title[0][value]' => $title,
      'path' => 'admin',
    ];
    $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $set->id() . '/add-link', $form_data, t('Save'));
    $this->assertResponse(200);
    $this->assertRaw(t('The shortcut must correspond to a valid path on the site.'));

    $form_data = [
      'title[0][value]' => $title,
      'path' => 'node',
    ];
    $this->drupalPostForm('admin/config/user-interface/shortcut/manage/' . $set->id() . '/add-link', $form_data, t('Save'));
    $this->assertLink($title, 0, 'Shortcut link found on the page.');
96 97 98
  }

  /**
99
   * Tests that the "add to shortcut" and "remove from shortcut" links work.
100
   */
101
  public function testShortcutQuickLink() {
102
    theme_enable(array('seven'));
103
    \Drupal::config('system.theme')->set('admin', 'seven')->save();
104
    $this->container->get('config.factory')->get('node.settings')->set('use_admin_theme', '1')->save();
105
    $this->container->get('router.builder')->rebuild();
106

107 108
    $this->drupalLogin($this->root_user);
    $this->drupalGet('admin/config/system/cron');
109

110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
    // Test the "Add to shortcuts" link.
    $this->clickLink('Add to Default shortcuts');
    $this->assertText('Added a shortcut for Cron.');
    $this->assertLink('Cron', 0, 'Shortcut link found on page');

    $this->drupalGet('admin/structure');
    $this->assertLink('Cron', 0, 'Shortcut link found on different page');

    // Test the "Remove from shortcuts" link.
    $this->clickLink('Cron');
    $this->clickLink('Remove from Default shortcuts');
    $this->assertText('The shortcut Cron has been deleted.');
    $this->assertNoLink('Cron', 'Shortcut link removed from page');

    $this->drupalGet('admin/structure');
    $this->assertNoLink('Cron', 'Shortcut link removed from different page');
126 127 128 129 130
  }

  /**
   * Tests that shortcut links can be renamed.
   */
131
  public function testShortcutLinkRename() {
132 133 134
    $set = $this->set;

    // Attempt to rename shortcut link.
135
    $new_link_name = $this->randomMachineName();
136

137 138
    $shortcuts = $set->getShortcuts();
    $shortcut = reset($shortcuts);
139
    $this->drupalPostForm('admin/config/user-interface/shortcut/link/' . $shortcut->id(), array('title[0][value]' => $new_link_name, 'path' => $shortcut->path->value), t('Save'));
140
    $saved_set = ShortcutSet::load($set->id());
141
    $titles = $this->getShortcutInformation($saved_set, 'title');
142 143 144 145 146 147 148
    $this->assertTrue(in_array($new_link_name, $titles), 'Shortcut renamed: ' . $new_link_name);
    $this->assertLink($new_link_name, 0, 'Renamed shortcut link appears on the page.');
  }

  /**
   * Tests that changing the path of a shortcut link works.
   */
149
  public function testShortcutLinkChangePath() {
150 151 152 153 154
    $set = $this->set;

    // Tests changing a shortcut path.
    $new_link_path = 'admin/config';

155 156
    $shortcuts = $set->getShortcuts();
    $shortcut = reset($shortcuts);
157
    $this->drupalPostForm('admin/config/user-interface/shortcut/link/' . $shortcut->id(), array('title[0][value]' => $shortcut->getTitle(), 'path' => $new_link_path), t('Save'));
158
    $saved_set = ShortcutSet::load($set->id());
159 160
    $routes = $this->getShortcutInformation($saved_set, 'route_name');
    $this->assertTrue(in_array('system.admin_config', $routes), 'Shortcut path changed: ' . $new_link_path);
161 162 163
    $this->assertLinkByHref($new_link_path, 0, 'Shortcut with new path appears on the page.');
  }

164 165 166 167 168 169 170 171 172 173 174 175 176
  /**
   * Tests that changing the route of a shortcut link works.
   */
  public function testShortcutLinkChangeRoute() {
    $this->drupalLogin($this->root_user);
    $this->drupalGet('admin/content');
    $this->assertResponse(200);
    // Disable the view.
    entity_load('view', 'content')->disable()->save();
    $this->drupalGet('admin/content');
    $this->assertResponse(200);
  }

177 178 179
  /**
   * Tests deleting a shortcut link.
   */
180
  public function testShortcutLinkDelete() {
181 182
    $set = $this->set;

183 184 185
    $shortcuts = $set->getShortcuts();
    $shortcut = reset($shortcuts);
    $this->drupalPostForm('admin/config/user-interface/shortcut/link/' . $shortcut->id() . '/delete', array(), 'Delete');
186
    $saved_set = ShortcutSet::load($set->id());
187 188
    $ids = $this->getShortcutInformation($saved_set, 'id');
    $this->assertFalse(in_array($shortcut->id(), $ids), 'Successfully deleted a shortcut.');
189

190 191
    // Delete all the remaining shortcut links.
    entity_delete_multiple('shortcut', array_filter($ids));
192 193 194

    // Get the front page to check that no exceptions occur.
    $this->drupalGet('');
195 196 197 198 199 200 201 202
  }

  /**
   * Tests that the add shortcut link is not displayed for 404/403 errors.
   *
   * Tests that the "Add to shortcuts" link is not displayed on a page not
   * found or a page the user does not have access to.
   */
203
  public function testNoShortcutLink() {
204
    // Change to a theme that displays shortcuts.
205
    theme_enable(array('seven'));
206
    \Drupal::config('system.theme')
207 208
      ->set('default', 'seven')
      ->save();
209 210

    $this->drupalGet('page-that-does-not-exist');
211 212
    $result = $this->xpath('//div[contains(@class, "add-shortcut")]');
    $this->assertTrue(empty($result), 'Add to shortcuts link was not shown on a page not found.');
213 214 215

    // The user does not have access to this path.
    $this->drupalGet('admin/modules');
216 217
    $result = $this->xpath('//div[contains(@class, "add-shortcut")]');
    $this->assertTrue(empty($result), 'Add to shortcuts link was not shown on a page the user does not have access to.');
218 219

    // Verify that the testing mechanism works by verifying the shortcut
220 221
    // link appears on admin/people.
    $this->drupalGet('admin/people');
222 223 224 225 226 227 228
    $result = $this->xpath('//div[contains(@class, "remove-shortcut")]');
    $this->assertTrue(!empty($result), 'Remove from shortcuts link was shown on a page the user does have access to.');

    // Verify that the shortcut link appears on routing only pages.
    $this->drupalGet('router_test/test2');
    $result = $this->xpath('//div[contains(@class, "add-shortcut")]');
    $this->assertTrue(!empty($result), 'Add to shortcuts link was shown on a page the user does have access to.');
229
  }
230

231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
  /**
   * Tests that the 'access shortcuts' permissions works properly.
   */
  public function testAccessShortcutsPermission() {
    // Change to a theme that displays shortcuts.
    \Drupal::service('theme_handler')->enable(array('seven'));
    \Drupal::config('system.theme')
      ->set('default', 'seven')
      ->save();

    // Add cron to the default shortcut set.
    $this->drupalLogin($this->root_user);
    $this->drupalGet('admin/config/system/cron');
    $this->clickLink('Add to Default shortcuts');

    // Verify that users without the 'access shortcuts' permission can't see the
    // shortcuts.
    $this->drupalLogin($this->drupalCreateUser(array('access toolbar')));
    $this->assertNoLink('Shortcuts', 0, 'Shortcut link not found on page.');

    // Verify that users with the 'access shortcuts' permission can see the
    // shortcuts.
    $this->drupalLogin($this->drupalCreateUser(array('access toolbar', 'access shortcuts')));
    $this->clickLink('Shortcuts', 0, 'Shortcut link found on page.');
    $this->assertLink('Cron', 0, 'Cron shortcut link found on page.');

    $this->verifyAccessShortcutsPermissionForEditPages();
  }

  /**
   * Tests that the 'access shortcuts' permission is required for shortcut set
   * administration page access.
   */
  private function verifyAccessShortcutsPermissionForEditPages() {
    // Create a user with customize links and switch sets permissions  but
    // without the 'access shortcuts' permission.
    $test_permissions = array(
      'customize shortcut links',
      'switch shortcut sets',
    );
    $noaccess_user = $this->drupalCreateUser($test_permissions);
    $this->drupalLogin($noaccess_user);

    // Verify that set administration pages are inaccessible without the
    // 'access shortcuts' permission.
    $edit_paths = array(
      'admin/config/user-interface/shortcut/manage/default/customize',
      'admin/config/user-interface/shortcut/manage/default',
      'user/' . $noaccess_user->id() . '/shortcuts',
    );

    foreach ($edit_paths as $path) {
      $this->drupalGet($path);
      $message = format_string('Access is denied on %s', array('%s' => $path));
      $this->assertResponse(403, $message);
    }
  }

289
}