BreadcrumbTest.php 13.5 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Definition of Drupal\system\Tests\Menu\BreadcrumbTest.
 */

namespace Drupal\system\Tests\Menu;

10
use Drupal\Component\Utility\String;
11 12
use Drupal\Component\Utility\Unicode;

13
/**
14 15 16
 * Tests breadcrumbs functionality.
 *
 * @group Menu
17 18
 */
class BreadcrumbTest extends MenuTestBase {
19 20 21 22 23 24

  /**
   * Modules to enable.
   *
   * @var array
   */
25
  public static $modules = array('menu_test', 'block');
26

27 28 29
  /**
   * Test paths in the Standard profile.
   */
30 31 32
  protected $profile = 'standard';

  function setUp() {
33
    parent::setUp();
34

35
    $perms = array_keys(\Drupal::moduleHandler()->invokeAll('permission'));
36 37 38
    $this->admin_user = $this->drupalCreateUser($perms);
    $this->drupalLogin($this->admin_user);

39 40
    // This test puts menu links in the Tools menu and then tests for their
    // presence on the page, so we need to ensure that the Tools block will be
41 42 43
    // displayed in the admin theme.
    $this->drupalPlaceBlock('system_menu_block:tools', array(
      'machine' => 'system_menu_tools',
44
      'region' => 'content',
45 46
      'theme' => \Drupal::config('system.theme')->get('admin'),
    ));
47 48 49 50 51 52 53
  }

  /**
   * Tests breadcrumbs on node and administrative paths.
   */
  function testBreadCrumbs() {
    // Prepare common base breadcrumb elements.
54
    $home = array('' => 'Home');
55 56 57 58 59 60 61 62 63 64 65 66 67
    $admin = $home + array('admin' => t('Administration'));
    $config = $admin + array('admin/config' => t('Configuration'));
    $type = 'article';

    // Verify Taxonomy administration breadcrumbs.
    $trail = $admin + array(
      'admin/structure' => t('Structure'),
    );
    $this->assertBreadcrumb('admin/structure/taxonomy', $trail);

    $trail += array(
      'admin/structure/taxonomy' => t('Taxonomy'),
    );
68
    $this->assertBreadcrumb('admin/structure/taxonomy/manage/tags', $trail);
69
    $trail += array(
70
      'admin/structure/taxonomy/manage/tags' => t('Tags'),
71
    );
72
    $this->assertBreadcrumb('admin/structure/taxonomy/manage/tags/overview', $trail);
73
    $this->assertBreadcrumb('admin/structure/taxonomy/manage/tags/add', $trail);
74 75 76 77 78 79 80 81 82 83

    // Verify Menu administration breadcrumbs.
    $trail = $admin + array(
      'admin/structure' => t('Structure'),
    );
    $this->assertBreadcrumb('admin/structure/menu', $trail);

    $trail += array(
      'admin/structure/menu' => t('Menus'),
    );
84
    $this->assertBreadcrumb('admin/structure/menu/manage/tools', $trail);
85

86
    $trail += array(
87
      'admin/structure/menu/manage/tools' => t('Tools'),
88
    );
89
    $this->assertBreadcrumb("admin/structure/menu/link/node.add_page/edit", $trail);
90
    $this->assertBreadcrumb('admin/structure/menu/manage/tools/add', $trail);
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111

    // Verify Node administration breadcrumbs.
    $trail = $admin + array(
      'admin/structure' => t('Structure'),
      'admin/structure/types' => t('Content types'),
    );
    $this->assertBreadcrumb('admin/structure/types/add', $trail);
    $this->assertBreadcrumb("admin/structure/types/manage/$type", $trail);
    $trail += array(
      "admin/structure/types/manage/$type" => t('Article'),
    );
    $this->assertBreadcrumb("admin/structure/types/manage/$type/fields", $trail);
    $this->assertBreadcrumb("admin/structure/types/manage/$type/display", $trail);
    $trail_teaser = $trail + array(
      "admin/structure/types/manage/$type/display" => t('Manage display'),
    );
    $this->assertBreadcrumb("admin/structure/types/manage/$type/display/teaser", $trail_teaser);
    $this->assertBreadcrumb("admin/structure/types/manage/$type/delete", $trail);
    $trail += array(
      "admin/structure/types/manage/$type/fields" => t('Manage fields'),
    );
112
    $this->assertBreadcrumb("admin/structure/types/manage/$type/fields/node.$type.body", $trail);
113 114

    // Verify Filter text format administration breadcrumbs.
115 116
    $filter_formats = filter_formats();
    $format = reset($filter_formats);
117 118 119 120 121 122 123
    $format_id = $format->format;
    $trail = $config + array(
      'admin/config/content' => t('Content authoring'),
    );
    $this->assertBreadcrumb('admin/config/content/formats', $trail);

    $trail += array(
124
      'admin/config/content/formats' => t('Text formats and editors'),
125 126
    );
    $this->assertBreadcrumb('admin/config/content/formats/add', $trail);
127
    $this->assertBreadcrumb("admin/config/content/formats/manage/$format_id", $trail);
128 129
    // @todo Remove this part once we have a _title_callback, see
    //   https://drupal.org/node/2076085.
130
    $trail += array(
131
      "admin/config/content/formats/manage/$format_id" => $format->label(),
132
    );
133
    $this->assertBreadcrumb("admin/config/content/formats/manage/$format_id/disable", $trail);
134 135 136

    // Verify node breadcrumbs (without menu link).
    $node1 = $this->drupalCreateNode();
137
    $nid1 = $node1->id();
138 139 140
    $trail = $home;
    $this->assertBreadcrumb("node/$nid1", $trail);
    // Also verify that the node does not appear elsewhere (e.g., menu trees).
141
    $this->assertNoLink($node1->getTitle());
142
    // Also verify that the node does not appear elsewhere (e.g., menu trees).
143
    $this->assertNoLink($node1->getTitle());
144 145

    $trail += array(
146
      "node/$nid1" => $node1->getTitle(),
147 148 149 150 151 152 153 154
    );
    $this->assertBreadcrumb("node/$nid1/edit", $trail);

    // Verify that breadcrumb on node listing page contains "Home" only.
    $trail = array();
    $this->assertBreadcrumb('node', $trail);

    // Verify node breadcrumbs (in menu).
155
    // Do this separately for Main menu and Tools menu, since only the
156 157
    // latter is a preferred menu by default.
    // @todo Also test all themes? Manually testing led to the suspicion that
158
    //   breadcrumbs may differ, possibly due to theme overrides.
159
    $menus = array('main', 'tools');
160
    // Alter node type menu settings.
161 162
    \Drupal::config("menu.entity.node.$type")
      ->set('available_menus', $menus)
163
      ->set('parent', 'tools:')
164
      ->save();
165 166 167

    foreach ($menus as $menu) {
      // Create a parent node in the current menu.
168
      $title = $this->randomMachineName();
169 170 171
      $node2 = $this->drupalCreateNode(array(
        'type' => $type,
        'title' => $title,
172
        'menu' => array(
173
          'enabled' => 1,
174
          'title' => 'Parent ' . $title,
175 176
          'description' => '',
          'menu_name' => $menu,
177 178
          'parent' => '',
        ),
179 180
      ));

181
      if ($menu == 'tools') {
182 183 184 185
        $parent = $node2;
      }
    }

186
    // Create a Tools menu link for 'node', move the last parent node menu
187
    // link below it, and verify a full breadcrumb for the last child node.
188
    $menu = 'tools';
189
    $edit = array(
190 191
      'title[0][value]' => 'Root',
      'url' => 'node',
192
    );
193
    $this->drupalPostForm("admin/structure/menu/manage/$menu/add", $edit, t('Save'));
194
    $menu_links = entity_load_multiple_by_properties('menu_link_content', array('title' => 'Root'));
195
    $link = reset($menu_links);
196 197

    $edit = array(
198
      'menu[menu_parent]' => $link->getMenuName() . ':' . $link->getPluginId(),
199
    );
200
    $this->drupalPostForm('node/' . $parent->id() . '/edit', $edit, t('Save and keep published'));
201
    $expected = array(
202
      "node" => $link->getTitle(),
203 204 205
    );
    $trail = $home + $expected;
    $tree = $expected + array(
206
      'node/' . $parent->id() => $parent->menu['title'],
207 208
    );
    $trail += array(
209
      'node/' . $parent->id() => $parent->menu['title'],
210 211 212
    );

    // Add a taxonomy term/tag to last node, and add a link for that term to the
213
    // Tools menu.
214 215 216 217 218
    $tags = array(
      'Drupal' => array(),
      'Breadcrumbs' => array(),
    );
    $edit = array(
219
      'field_tags' => implode(',', array_keys($tags)),
220
    );
221
    $this->drupalPostForm('node/' . $parent->id() . '/edit', $edit, t('Save and keep published'));
222 223 224 225 226 227

    // Put both terms into a hierarchy Drupal » Breadcrumbs. Required for both
    // the menu links and the terms itself, since taxonomy_term_page() resets
    // the breadcrumb based on taxonomy term hierarchy.
    $parent_tid = 0;
    foreach ($tags as $name => $null) {
228
      $terms = entity_load_multiple_by_properties('taxonomy_term', array('name' => $name));
229 230 231 232 233 234
      $term = reset($terms);
      $tags[$name]['term'] = $term;
      if ($parent_tid) {
        $edit = array(
          'parent[]' => array($parent_tid),
        );
235
        $this->drupalPostForm("taxonomy/term/{$term->id()}/edit", $edit, t('Save'));
236
      }
237
      $parent_tid = $term->id();
238
    }
239
    $parent_mlid = '';
240 241 242
    foreach ($tags as $name => $data) {
      $term = $data['term'];
      $edit = array(
243 244 245
        'title[0][value]' => "$name link",
        'url' => "taxonomy/term/{$term->id()}",
        'menu_parent' => "$menu:{$parent_mlid}",
246
        'enabled[value]' => 1,
247
      );
248
      $this->drupalPostForm("admin/structure/menu/manage/$menu/add", $edit, t('Save'));
249
      $menu_links = entity_load_multiple_by_properties('menu_link_content', array('title' => $edit['title[0][value]'], 'route_name' => 'taxonomy.term_page', 'route_parameters' => serialize(array('taxonomy_term' => $term->id()))));
250
      $tags[$name]['link'] = reset($menu_links);
251
      $parent_mlid = $tags[$name]['link']->getPluginId();
252 253 254 255 256
    }

    // Verify expected breadcrumbs for menu links.
    $trail = $home;
    $tree = array();
257 258 259
    // Logout the user because we want to check the active class as well, which
    // is just rendered as anonymous user.
    $this->drupalLogout();
260 261
    foreach ($tags as $name => $data) {
      $term = $data['term'];
262
      /** @var \Drupal\menu_link_content\MenuLinkContentInterface $link */
263 264
      $link = $data['link'];

265
      $link_path = $link->getUrlObject()->getInternalPath();
266
      $tree += array(
267
        $link_path => $link->getTitle(),
268
      );
269
      $this->assertBreadcrumb($link_path, $trail, $term->getName(), $tree);
270
      $this->assertRaw(String::checkPlain($parent->getTitle()), 'Tagged node found.');
271 272 273 274 275 276

      // Additionally make sure that this link appears only once; i.e., the
      // untranslated menu links automatically generated from menu router items
      // ('taxonomy/term/%') should never be translated and appear in any menu
      // other than the breadcrumb trail.
      $elements = $this->xpath('//div[@id=:menu]/descendant::a[@href=:href]', array(
277
        ':menu' => 'block-bartik-tools',
278
        ':href' => url($link_path),
279
      ));
280
      $this->assertTrue(count($elements) == 1, "Link to {$link_path} appears only once.");
281 282 283 284

      // Next iteration should expect this tag as parent link.
      // Note: Term name, not link name, due to taxonomy_term_page().
      $trail += array(
285
        $link_path => $term->getName(),
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
      );
    }

    // Verify breadcrumbs on user and user/%.
    // We need to log back in and out below, and cannot simply grant the
    // 'administer users' permission, since user_page() makes your head explode.
    user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array(
      'access user profiles',
    ));

    // Verify breadcrumb on front page.
    $this->assertBreadcrumb('<front>', array());

    // Verify breadcrumb on user pages (without menu link) for anonymous user.
    $trail = $home;
301
    $this->assertBreadcrumb('user', $trail, t('Log in'));
302
    $this->assertBreadcrumb('user/' . $this->admin_user->id(), $trail, $this->admin_user->getUsername());
303 304 305 306

    // Verify breadcrumb on user pages (without menu link) for registered users.
    $this->drupalLogin($this->admin_user);
    $trail = $home;
307 308
    $this->assertBreadcrumb('user', $trail, $this->admin_user->getUsername());
    $this->assertBreadcrumb('user/' . $this->admin_user->id(), $trail, $this->admin_user->getUsername());
309
    $trail += array(
310
      'user/' . $this->admin_user->id() => $this->admin_user->getUsername(),
311
    );
312
    $this->assertBreadcrumb('user/' . $this->admin_user->id() . '/edit', $trail, $this->admin_user->getUsername());
313 314 315 316 317 318 319 320

    // Create a second user to verify breadcrumb on user pages again.
    $this->web_user = $this->drupalCreateUser(array(
      'administer users',
      'access user profiles',
    ));
    $this->drupalLogin($this->web_user);

321
    // Verify correct breadcrumb and page title on another user's account pages.
322
    $trail = $home;
323
    $this->assertBreadcrumb('user/' . $this->admin_user->id(), $trail, $this->admin_user->getUsername());
324
    $trail += array(
325
      'user/' . $this->admin_user->id() => $this->admin_user->getUsername(),
326
    );
327
    $this->assertBreadcrumb('user/' . $this->admin_user->id() . '/edit', $trail, $this->admin_user->getUsername());
328

329
    // Verify correct breadcrumb and page title when viewing own user account.
330
    $trail = $home;
331
    $this->assertBreadcrumb('user/' . $this->web_user->id(), $trail, $this->web_user->getUsername());
332
    $trail += array(
333
      'user/' . $this->web_user->id() => $this->web_user->getUsername(),
334
    );
335
    $this->assertBreadcrumb('user/' . $this->web_user->id() . '/edit', $trail, $this->web_user->getUsername());
336 337 338 339 340 341 342 343 344

    // Create an only slightly privileged user being able to access site reports
    // but not administration pages.
    $this->web_user = $this->drupalCreateUser(array(
      'access site reports',
    ));
    $this->drupalLogin($this->web_user);

    // Verify that we can access recent log entries, there is a corresponding
345 346 347
    // page title, and that the breadcrumb is just the Home link (because the
    // user is not able to access "Administer".
    $trail = $home;
348 349 350
    $this->assertBreadcrumb('admin', $trail, t('Access denied'));
    $this->assertResponse(403);

351 352
    // Since the 'admin' path is not accessible, we still expect only the Home
    // link.
353 354 355
    $this->assertBreadcrumb('admin/reports', $trail, t('Reports'));
    $this->assertNoResponse(403);

356 357
    // Since the Reports page is accessible, that will show.
    $trail += array('admin/reports' => t('Reports'));
358 359 360
    $this->assertBreadcrumb('admin/reports/dblog', $trail, t('Recent log messages'));
    $this->assertNoResponse(403);
  }
361

362
}