menu.test 71.6 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740
<?php

/**
 * @file
 * Provides SimpleTests for menu.inc.
 */

class MenuWebTestCase extends DrupalWebTestCase {
  function setUp() {
    $modules = func_get_args();
    if (isset($modules[0]) && is_array($modules[0])) {
      $modules = $modules[0];
    }
    parent::setUp($modules);
  }

  /**
   * Assert that a given path shows certain breadcrumb links.
   *
   * @param string $goto
   *   (optional) A system path to pass to DrupalWebTestCase::drupalGet().
   * @param array $trail
   *   An associative array whose keys are expected breadcrumb link paths and
   *   whose values are expected breadcrumb link texts (not sanitized).
   * @param string $page_title
   *   (optional) A page title to additionally assert via
   *   DrupalWebTestCase::assertTitle(). Without site name suffix.
   * @param array $tree
   *   (optional) An associative array whose keys are link paths and whose
   *   values are link titles (not sanitized) of an expected active trail in a
   *   menu tree output on the page.
   * @param $last_active
   *   (optional) Whether the last link in $tree is expected to be active (TRUE)
   *   or just to be in the active trail (FALSE).
   */
  protected function assertBreadcrumb($goto, array $trail, $page_title = NULL, array $tree = array(), $last_active = TRUE) {
    if (isset($goto)) {
      $this->drupalGet($goto);
    }
    // Compare paths with actual breadcrumb.
    $parts = $this->getParts();
    $pass = TRUE;
    foreach ($trail as $path => $title) {
      $url = url($path);
      $part = array_shift($parts);
      $pass = ($pass && $part['href'] === $url && $part['text'] === check_plain($title));
    }
    // No parts must be left, or an expected "Home" will always pass.
    $pass = ($pass && empty($parts));

    $this->assertTrue($pass, format_string('Breadcrumb %parts found on @path.', array(
      '%parts' => implode(' » ', $trail),
      '@path' => $this->getUrl(),
    )));

    // Additionally assert page title, if given.
    if (isset($page_title)) {
      $this->assertTitle(strtr('@title | Drupal', array('@title' => $page_title)));
    }

    // Additionally assert active trail in a menu tree output, if given.
    if ($tree) {
      end($tree);
      $active_link_path = key($tree);
      $active_link_title = array_pop($tree);
      $xpath = '';
      if ($tree) {
        $i = 0;
        foreach ($tree as $link_path => $link_title) {
          $part_xpath = (!$i ? '//' : '/following-sibling::ul/descendant::');
          $part_xpath .= 'li[contains(@class, :class)]/a[contains(@href, :href) and contains(text(), :title)]';
          $part_args = array(
            ':class' => 'active-trail',
            ':href' => url($link_path),
            ':title' => $link_title,
          );
          $xpath .= $this->buildXPathQuery($part_xpath, $part_args);
          $i++;
        }
        $elements = $this->xpath($xpath);
        $this->assertTrue(!empty($elements), 'Active trail to current page was found in menu tree.');

        // Append prefix for active link asserted below.
        $xpath .= '/following-sibling::ul/descendant::';
      }
      else {
        $xpath .= '//';
      }
      $xpath_last_active = ($last_active ? 'and contains(@class, :class-active)' : '');
      $xpath .= 'li[contains(@class, :class-trail)]/a[contains(@href, :href) ' . $xpath_last_active . 'and contains(text(), :title)]';
      $args = array(
        ':class-trail' => 'active-trail',
        ':class-active' => 'active',
        ':href' => url($active_link_path),
        ':title' => $active_link_title,
      );
      $elements = $this->xpath($xpath, $args);
      $this->assertTrue(!empty($elements), format_string('Active link %title was found in menu tree, including active trail links %tree.', array(
        '%title' => $active_link_title,
        '%tree' => implode(' » ', $tree),
      )));
    }
  }

  /**
   * Returns the breadcrumb contents of the current page in the internal browser.
   */
  protected function getParts() {
    $parts = array();
    $elements = $this->xpath('//div[@class="breadcrumb"]/a');
    if (!empty($elements)) {
      foreach ($elements as $element) {
        $parts[] = array(
          'text' => (string) $element,
          'href' => (string) $element['href'],
          'title' => (string) $element['title'],
        );
      }
    }
    return $parts;
  }
}

class MenuRouterTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Menu router',
      'description' => 'Tests menu router and hook_menu() functionality.',
      'group' => 'Menu',
    );
  }

  function setUp() {
    // Enable dummy module that implements hook_menu.
    parent::setUp('menu_test');
    // Make the tests below more robust by explicitly setting the default theme
    // and administrative theme that they expect.
    theme_enable(array('bartik'));
    variable_set('theme_default', 'bartik');
    variable_set('admin_theme', 'seven');
  }

  /**
   * Test title callback set to FALSE.
   */
  function testTitleCallbackFalse() {
    $this->drupalGet('node');
    $this->assertText('A title with @placeholder', 'Raw text found on the page');
    $this->assertNoText(t('A title with @placeholder', array('@placeholder' => 'some other text')), 'Text with placeholder substitutions not found.');
  }

  /**
   * Tests page title of MENU_CALLBACKs.
   */
  function testTitleMenuCallback() {
    // Verify that the menu router item title is not visible.
    $this->drupalGet('');
    $this->assertNoText(t('Menu Callback Title'));
    // Verify that the menu router item title is output as page title.
    $this->drupalGet('menu_callback_title');
    $this->assertText(t('Menu Callback Title'));
  }

  /**
   * Test the theme callback when it is set to use an administrative theme.
   */
  function testThemeCallbackAdministrative() {
    $this->drupalGet('menu-test/theme-callback/use-admin-theme');
    $this->assertText('Custom theme: seven. Actual theme: seven.', 'The administrative theme can be correctly set in a theme callback.');
    $this->assertRaw('seven/style.css', "The administrative theme's CSS appears on the page.");
  }

  /**
   * Test that the theme callback is properly inherited.
   */
  function testThemeCallbackInheritance() {
    $this->drupalGet('menu-test/theme-callback/use-admin-theme/inheritance');
    $this->assertText('Custom theme: seven. Actual theme: seven. Theme callback inheritance is being tested.', 'Theme callback inheritance correctly uses the administrative theme.');
    $this->assertRaw('seven/style.css', "The administrative theme's CSS appears on the page.");
  }

  /**
   * Test that 'page callback', 'file' and 'file path' keys are properly
   * inherited from parent menu paths.
   */
  function testFileInheritance() {
    $this->drupalGet('admin/config/development/file-inheritance');
    $this->assertText('File inheritance test description', 'File inheritance works.');
  }

  /**
   * Test path containing "exotic" characters.
   */
  function testExoticPath() {
    $path = "menu-test/ -._~!$'\"()*@[]?&+%#,;=:" . // "Special" ASCII characters.
      "%23%25%26%2B%2F%3F" . // Characters that look like a percent-escaped string.
      "éøïвβ中國書۞"; // Characters from various non-ASCII alphabets.
    $this->drupalGet($path);
    $this->assertRaw('This is menu_test_callback().');
  }

  /**
   * Test the theme callback when the site is in maintenance mode.
   */
  function testThemeCallbackMaintenanceMode() {
    variable_set('maintenance_mode', TRUE);

    // For a regular user, the fact that the site is in maintenance mode means
    // we expect the theme callback system to be bypassed entirely.
    $this->drupalGet('menu-test/theme-callback/use-admin-theme');
    $this->assertRaw('bartik/css/style.css', "The maintenance theme's CSS appears on the page.");

    // An administrator, however, should continue to see the requested theme.
    $admin_user = $this->drupalCreateUser(array('access site in maintenance mode'));
    $this->drupalLogin($admin_user);
    $this->drupalGet('menu-test/theme-callback/use-admin-theme');
    $this->assertText('Custom theme: seven. Actual theme: seven.', 'The theme callback system is correctly triggered for an administrator when the site is in maintenance mode.');
    $this->assertRaw('seven/style.css', "The administrative theme's CSS appears on the page.");
  }

  /**
   * Make sure the maintenance mode can be bypassed using hook_menu_site_status_alter().
   *
   * @see hook_menu_site_status_alter().
   */
  function testMaintenanceModeLoginPaths() {
    variable_set('maintenance_mode', TRUE);

    $offline_message = t('@site is currently under maintenance. We should be back shortly. Thank you for your patience.', array('@site' => variable_get('site_name', 'Drupal')));
    $this->drupalLogout();
    $this->drupalGet('node');
    $this->assertText($offline_message);
    $this->drupalGet('menu_login_callback');
    $this->assertText('This is menu_login_callback().', t('Maintenance mode can be bypassed through hook_login_paths().'));
  }

  /**
   * Test that an authenticated user hitting 'user/login' gets redirected to
   * 'user' and 'user/register' gets redirected to the user edit page.
   */
  function testAuthUserUserLogin() {
    $loggedInUser = $this->drupalCreateUser(array());
    $this->drupalLogin($loggedInUser);

    $this->drupalGet('user/login');
    // Check that we got to 'user'.
    $this->assertTrue($this->url == url('user', array('absolute' => TRUE)), "Logged-in user redirected to q=user on accessing q=user/login");

    // user/register should redirect to user/UID/edit.
    $this->drupalGet('user/register');
    $this->assertTrue($this->url == url('user/' . $this->loggedInUser->uid . '/edit', array('absolute' => TRUE)), "Logged-in user redirected to q=user/UID/edit on accessing q=user/register");
  }

  /**
   * Test the theme callback when it is set to use an optional theme.
   */
  function testThemeCallbackOptionalTheme() {
    // Request a theme that is not enabled.
    $this->drupalGet('menu-test/theme-callback/use-stark-theme');
    $this->assertText('Custom theme: NONE. Actual theme: bartik.', 'The theme callback system falls back on the default theme when a theme that is not enabled is requested.');
    $this->assertRaw('bartik/css/style.css', "The default theme's CSS appears on the page.");

    // Now enable the theme and request it again.
    theme_enable(array('stark'));
    $this->drupalGet('menu-test/theme-callback/use-stark-theme');
    $this->assertText('Custom theme: stark. Actual theme: stark.', 'The theme callback system uses an optional theme once it has been enabled.');
    $this->assertRaw('stark/layout.css', "The optional theme's CSS appears on the page.");
  }

  /**
   * Test the theme callback when it is set to use a theme that does not exist.
   */
  function testThemeCallbackFakeTheme() {
    $this->drupalGet('menu-test/theme-callback/use-fake-theme');
    $this->assertText('Custom theme: NONE. Actual theme: bartik.', 'The theme callback system falls back on the default theme when a theme that does not exist is requested.');
    $this->assertRaw('bartik/css/style.css', "The default theme's CSS appears on the page.");
  }

  /**
   * Test the theme callback when no theme is requested.
   */
  function testThemeCallbackNoThemeRequested() {
    $this->drupalGet('menu-test/theme-callback/no-theme-requested');
    $this->assertText('Custom theme: NONE. Actual theme: bartik.', 'The theme callback system falls back on the default theme when no theme is requested.');
    $this->assertRaw('bartik/css/style.css', "The default theme's CSS appears on the page.");
  }

  /**
   * Test that hook_custom_theme() can control the theme of a page.
   */
  function testHookCustomTheme() {
    // Trigger hook_custom_theme() to dynamically request the Stark theme for
    // the requested page.
    variable_set('menu_test_hook_custom_theme_name', 'stark');
    theme_enable(array('stark'));

    // Visit a page that does not implement a theme callback. The above request
    // should be honored.
    $this->drupalGet('menu-test/no-theme-callback');
    $this->assertText('Custom theme: stark. Actual theme: stark.', 'The result of hook_custom_theme() is used as the theme for the current page.');
    $this->assertRaw('stark/layout.css', "The Stark theme's CSS appears on the page.");
  }

  /**
   * Test that the theme callback wins out over hook_custom_theme().
   */
  function testThemeCallbackHookCustomTheme() {
    // Trigger hook_custom_theme() to dynamically request the Stark theme for
    // the requested page.
    variable_set('menu_test_hook_custom_theme_name', 'stark');
    theme_enable(array('stark'));

    // The menu "theme callback" should take precedence over a value set in
    // hook_custom_theme().
    $this->drupalGet('menu-test/theme-callback/use-admin-theme');
    $this->assertText('Custom theme: seven. Actual theme: seven.', 'The result of hook_custom_theme() does not override what was set in a theme callback.');
    $this->assertRaw('seven/style.css', "The Seven theme's CSS appears on the page.");
  }

  /**
   * Tests for menu_link_maintain().
   */
  function testMenuLinkMaintain() {
    $admin_user = $this->drupalCreateUser(array('administer site configuration'));
    $this->drupalLogin($admin_user);

    // Create three menu items.
    menu_link_maintain('menu_test', 'insert', 'menu_test_maintain/1', 'Menu link #1');
    menu_link_maintain('menu_test', 'insert', 'menu_test_maintain/1', 'Menu link #1-1');
    menu_link_maintain('menu_test', 'insert', 'menu_test_maintain/2', 'Menu link #2');

    // Move second link to the main-menu, to test caching later on.
    db_update('menu_links')
      ->fields(array('menu_name' => 'main-menu'))
      ->condition('link_title', 'Menu link #1-1')
      ->condition('customized', 0)
      ->condition('module', 'menu_test')
      ->execute();
    menu_cache_clear('main-menu');

    // Load front page.
    $this->drupalGet('node');
    $this->assertLink(t('Menu link #1'), 0, 'Found menu link #1');
    $this->assertLink(t('Menu link #1-1'), 0, 'Found menu link #1-1');
    $this->assertLink(t('Menu link #2'), 0, 'Found menu link #2');

    // Rename all links for the given path.
    menu_link_maintain('menu_test', 'update', 'menu_test_maintain/1', 'Menu link updated');
    // Load a different page to be sure that we have up to date information.
    $this->drupalGet('menu_test_maintain/1');
    $this->assertLink(t('Menu link updated'), 0, 'Found updated menu link');
    $this->assertNoLink(t('Menu link #1'), 0, 'Not found menu link #1');
    $this->assertNoLink(t('Menu link #1'), 0, 'Not found menu link #1-1');
    $this->assertLink(t('Menu link #2'), 0, 'Found menu link #2');

    // Delete all links for the given path.
    menu_link_maintain('menu_test', 'delete', 'menu_test_maintain/1', '');
    // Load a different page to be sure that we have up to date information.
    $this->drupalGet('menu_test_maintain/2');
    $this->assertNoLink(t('Menu link updated'), 0, 'Not found deleted menu link');
    $this->assertNoLink(t('Menu link #1'), 0, 'Not found menu link #1');
    $this->assertNoLink(t('Menu link #1'), 0, 'Not found menu link #1-1');
    $this->assertLink(t('Menu link #2'), 0, 'Found menu link #2');
  }

  /**
   * Test menu_get_names().
   */
  function testMenuGetNames() {
    // Create three menu items.
    for ($i = 0; $i < 3; $i++) {
      $menu_link = array(
        'link_title' => 'Menu link #' . $i,
        'link_path' => 'menu_test/' . $i,
        'module' => 'menu_test',
        'menu_name' => 'menu_test_' . $i,
      );
      menu_link_save($menu_link);
    }

    drupal_static_reset('menu_get_names');

    // Verify that the menu names are correctly reported by menu_get_names().
    $menu_names = menu_get_names();
    $this->pass(implode(' | ', $menu_names));
    for ($i = 0; $i < 3; $i++) {
      $this->assertTrue(in_array('menu_test_' . $i, $menu_names), t('Expected menu name %expected is returned.', array('%expected' => 'menu_test_' . $i)));
    }
  }

  /**
   * Tests for menu_name parameter for hook_menu().
   */
  function testMenuName() {
    $admin_user = $this->drupalCreateUser(array('administer site configuration'));
    $this->drupalLogin($admin_user);

    $sql = "SELECT menu_name FROM {menu_links} WHERE router_path = 'menu_name_test'";
    $name = db_query($sql)->fetchField();
    $this->assertEqual($name, 'original', 'Menu name is "original".');

    // Change the menu_name parameter in menu_test.module, then force a menu
    // rebuild.
    menu_test_menu_name('changed');
    menu_rebuild();

    $sql = "SELECT menu_name FROM {menu_links} WHERE router_path = 'menu_name_test'";
    $name = db_query($sql)->fetchField();
    $this->assertEqual($name, 'changed', 'Menu name was successfully changed after rebuild.');
  }

  /**
   * Tests for menu hierarchy.
   */
  function testMenuHierarchy() {
    $parent_link = db_query('SELECT * FROM {menu_links} WHERE link_path = :link_path', array(':link_path' => 'menu-test/hierarchy/parent'))->fetchAssoc();
    $child_link = db_query('SELECT * FROM {menu_links} WHERE link_path = :link_path', array(':link_path' => 'menu-test/hierarchy/parent/child'))->fetchAssoc();
    $unattached_child_link = db_query('SELECT * FROM {menu_links} WHERE link_path = :link_path', array(':link_path' => 'menu-test/hierarchy/parent/child2/child'))->fetchAssoc();

    $this->assertEqual($child_link['plid'], $parent_link['mlid'], 'The parent of a directly attached child is correct.');
    $this->assertEqual($unattached_child_link['plid'], $parent_link['mlid'], 'The parent of a non-directly attached child is correct.');
  }

  /**
   * Tests menu link depth and parents of local tasks and menu callbacks.
   */
  function testMenuHidden() {
    // Verify links for one dynamic argument.
    $links = db_select('menu_links', 'ml')
      ->fields('ml')
      ->condition('ml.router_path', 'menu-test/hidden/menu%', 'LIKE')
      ->orderBy('ml.router_path')
      ->execute()
      ->fetchAllAssoc('router_path', PDO::FETCH_ASSOC);

    $parent = $links['menu-test/hidden/menu'];
    $depth = $parent['depth'] + 1;
    $plid = $parent['mlid'];

    $link = $links['menu-test/hidden/menu/list'];
    $this->assertEqual($link['depth'], $depth, format_string('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
    $this->assertEqual($link['plid'], $plid, format_string('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));

    $link = $links['menu-test/hidden/menu/add'];
    $this->assertEqual($link['depth'], $depth, format_string('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
    $this->assertEqual($link['plid'], $plid, format_string('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));

    $link = $links['menu-test/hidden/menu/settings'];
    $this->assertEqual($link['depth'], $depth, format_string('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
    $this->assertEqual($link['plid'], $plid, format_string('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));

    $link = $links['menu-test/hidden/menu/manage/%'];
    $this->assertEqual($link['depth'], $depth, format_string('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
    $this->assertEqual($link['plid'], $plid, format_string('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));

    $parent = $links['menu-test/hidden/menu/manage/%'];
    $depth = $parent['depth'] + 1;
    $plid = $parent['mlid'];

    $link = $links['menu-test/hidden/menu/manage/%/list'];
    $this->assertEqual($link['depth'], $depth, format_string('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
    $this->assertEqual($link['plid'], $plid, format_string('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));

    $link = $links['menu-test/hidden/menu/manage/%/add'];
    $this->assertEqual($link['depth'], $depth, format_string('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
    $this->assertEqual($link['plid'], $plid, format_string('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));

    $link = $links['menu-test/hidden/menu/manage/%/edit'];
    $this->assertEqual($link['depth'], $depth, format_string('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
    $this->assertEqual($link['plid'], $plid, format_string('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));

    $link = $links['menu-test/hidden/menu/manage/%/delete'];
    $this->assertEqual($link['depth'], $depth, format_string('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
    $this->assertEqual($link['plid'], $plid, format_string('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));

    // Verify links for two dynamic arguments.
    $links = db_select('menu_links', 'ml')
      ->fields('ml')
      ->condition('ml.router_path', 'menu-test/hidden/block%', 'LIKE')
      ->orderBy('ml.router_path')
      ->execute()
      ->fetchAllAssoc('router_path', PDO::FETCH_ASSOC);

    $parent = $links['menu-test/hidden/block'];
    $depth = $parent['depth'] + 1;
    $plid = $parent['mlid'];

    $link = $links['menu-test/hidden/block/list'];
    $this->assertEqual($link['depth'], $depth, format_string('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
    $this->assertEqual($link['plid'], $plid, format_string('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));

    $link = $links['menu-test/hidden/block/add'];
    $this->assertEqual($link['depth'], $depth, format_string('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
    $this->assertEqual($link['plid'], $plid, format_string('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));

    $link = $links['menu-test/hidden/block/manage/%/%'];
    $this->assertEqual($link['depth'], $depth, format_string('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
    $this->assertEqual($link['plid'], $plid, format_string('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));

    $parent = $links['menu-test/hidden/block/manage/%/%'];
    $depth = $parent['depth'] + 1;
    $plid = $parent['mlid'];

    $link = $links['menu-test/hidden/block/manage/%/%/configure'];
    $this->assertEqual($link['depth'], $depth, format_string('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
    $this->assertEqual($link['plid'], $plid, format_string('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));

    $link = $links['menu-test/hidden/block/manage/%/%/delete'];
    $this->assertEqual($link['depth'], $depth, format_string('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
    $this->assertEqual($link['plid'], $plid, format_string('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));
  }

  /**
   * Test menu_get_item() with empty ancestors.
   */
  function testMenuGetItemNoAncestors() {
    variable_set('menu_masks', array());
    $this->drupalGet('');
  }

  /**
   * Test menu_set_item().
   */
  function testMenuSetItem() {
    $item = menu_get_item('node');

    $this->assertEqual($item['path'], 'node', "Path from menu_get_item('node') is equal to 'node'", 'menu');

    // Modify the path for the item then save it.
    $item['path'] = 'node_test';
    $item['href'] = 'node_test';

    menu_set_item('node', $item);
    $compare_item = menu_get_item('node');
    $this->assertEqual($compare_item, $item, 'Modified menu item is equal to newly retrieved menu item.', 'menu');
  }

  /**
   * Test menu maintenance hooks.
   */
  function testMenuItemHooks() {
    // Create an item.
    menu_link_maintain('menu_test', 'insert', 'menu_test_maintain/4', 'Menu link #4');
    $this->assertEqual(menu_test_static_variable(), 'insert', 'hook_menu_link_insert() fired correctly');
    // Update the item.
    menu_link_maintain('menu_test', 'update', 'menu_test_maintain/4', 'Menu link updated');
    $this->assertEqual(menu_test_static_variable(), 'update', 'hook_menu_link_update() fired correctly');
    // Delete the item.
    menu_link_maintain('menu_test', 'delete', 'menu_test_maintain/4', '');
    $this->assertEqual(menu_test_static_variable(), 'delete', 'hook_menu_link_delete() fired correctly');
  }

  /**
   * Test menu link 'options' storage and rendering.
   */
  function testMenuLinkOptions() {
    // Create a menu link with options.
    $menu_link = array(
      'link_title' => 'Menu link options test',
      'link_path' => 'node',
      'module' => 'menu_test',
      'options' => array(
        'attributes' => array(
          'title' => 'Test title attribute',
        ),
        'query' => array(
          'testparam' => 'testvalue',
        ),
      ),
    );
    menu_link_save($menu_link);

    // Load front page.
    $this->drupalGet('node');
    $this->assertRaw('title="Test title attribute"', 'Title attribute of a menu link renders.');
    $this->assertRaw('testparam=testvalue', 'Query parameter added to menu link.');
  }

  /**
   * Tests the possible ways to set the title for menu items.
   * Also tests that menu item titles work with string overrides.
   */
  function testMenuItemTitlesCases() {

    // Build array with string overrides.
    $test_data = array(
      1 => array('Example title - Case 1' => 'Alternative example title - Case 1'),
      2 => array('Example @sub1 - Case @op2' => 'Alternative example @sub1 - Case @op2'),
      3 => array('Example title' => 'Alternative example title'),
      4 => array('Example title' => 'Alternative example title'),
    );

    foreach ($test_data as $case_no => $override) {
      $this->menuItemTitlesCasesHelper($case_no);
      variable_set('locale_custom_strings_en', array('' => $override));
      $this->menuItemTitlesCasesHelper($case_no, TRUE);
      variable_set('locale_custom_strings_en', array());
    }
  }

  /**
   * Get a URL and assert the title given a case number. If override is true,
   * the title is asserted to begin with "Alternative".
   */
  private function menuItemTitlesCasesHelper($case_no, $override = FALSE) {
    $this->drupalGet('menu-title-test/case' . $case_no);
    $this->assertResponse(200);
    $asserted_title = $override ? 'Alternative example title - Case ' . $case_no : 'Example title - Case ' . $case_no;
    $this->assertTitle($asserted_title . ' | Drupal', format_string('Menu title is: %title.', array('%title' => $asserted_title)), 'Menu');
  }

  /**
   * Load the router for a given path.
   */
  protected function menuLoadRouter($router_path) {
    return db_query('SELECT * FROM {menu_router} WHERE path = :path', array(':path' => $router_path))->fetchAssoc();
  }

  /**
   * Tests inheritance of 'load arguments'.
   */
  function testMenuLoadArgumentsInheritance() {
    $expected = array(
      'menu-test/arguments/%/%' => array(
        2 => array('menu_test_argument_load' => array(3)),
        3 => NULL,
      ),
      // Arguments are inherited to normal children.
      'menu-test/arguments/%/%/default' => array(
        2 => array('menu_test_argument_load' => array(3)),
        3 => NULL,
      ),
      // Arguments are inherited to tab children.
      'menu-test/arguments/%/%/task' => array(
        2 => array('menu_test_argument_load' => array(3)),
        3 => NULL,
      ),
      // Arguments are only inherited to the same loader functions.
      'menu-test/arguments/%/%/common-loader' => array(
        2 => array('menu_test_argument_load' => array(3)),
        3 => 'menu_test_other_argument_load',
      ),
      // Arguments are not inherited to children not using the same loader
      // function.
      'menu-test/arguments/%/%/different-loaders-1' => array(
        2 => NULL,
        3 => 'menu_test_argument_load',
      ),
      'menu-test/arguments/%/%/different-loaders-2' => array(
        2 => 'menu_test_other_argument_load',
        3 => NULL,
      ),
      'menu-test/arguments/%/%/different-loaders-3' => array(
        2 => NULL,
        3 => NULL,
      ),
      // Explicit loader arguments should not be overriden by parent.
      'menu-test/arguments/%/%/explicit-arguments' => array(
        2 => array('menu_test_argument_load' => array()),
        3 => NULL,
      ),
    );

    foreach ($expected as $router_path => $load_functions) {
      $router_item = $this->menuLoadRouter($router_path);
      $this->assertIdentical(unserialize($router_item['load_functions']), $load_functions, format_string('Expected load functions for router %router_path' , array('%router_path' => $router_path)));
    }
  }
}

/**
 * Tests for menu links.
 */
class MenuLinksUnitTestCase extends DrupalWebTestCase {
  // Use the lightweight testing profile for this test.
  protected $profile = 'testing';

  public static function getInfo() {
    return array(
      'name' => 'Menu links',
      'description' => 'Test handling of menu links hierarchies.',
      'group' => 'Menu',
    );
  }

  /**
   * Create a simple hierarchy of links.
   */
  function createLinkHierarchy($module = 'menu_test') {
    // First remove all the menu links.
    db_truncate('menu_links')->execute();

    // Then create a simple link hierarchy:
    // - $parent
    //   - $child-1
    //      - $child-1-1
    //      - $child-1-2
    //   - $child-2
    $base_options = array(
      'link_title' => 'Menu link test',
      'module' => $module,
      'menu_name' => 'menu_test',
    );

    $links['parent'] = $base_options + array(
      'link_path' => 'menu-test/parent',
    );
    menu_link_save($links['parent']);

    $links['child-1'] = $base_options + array(
      'link_path' => 'menu-test/parent/child-1',
      'plid' => $links['parent']['mlid'],
    );
    menu_link_save($links['child-1']);

    $links['child-1-1'] = $base_options + array(
      'link_path' => 'menu-test/parent/child-1/child-1-1',
      'plid' => $links['child-1']['mlid'],
    );
    menu_link_save($links['child-1-1']);

    $links['child-1-2'] = $base_options + array(
      'link_path' => 'menu-test/parent/child-1/child-1-2',
      'plid' => $links['child-1']['mlid'],
    );
    menu_link_save($links['child-1-2']);

    $links['child-2'] = $base_options + array(
      'link_path' => 'menu-test/parent/child-2',
      'plid' => $links['parent']['mlid'],
    );
    menu_link_save($links['child-2']);

    return $links;
  }

  /**
   * Assert that at set of links is properly parented.
   */
  function assertMenuLinkParents($links, $expected_hierarchy) {
    foreach ($expected_hierarchy as $child => $parent) {
      $mlid = $links[$child]['mlid'];
      $plid = $parent ? $links[$parent]['mlid'] : 0;

      $menu_link = menu_link_load($mlid);
      menu_link_save($menu_link);
      $this->assertEqual($menu_link['plid'], $plid, format_string('Menu link %mlid has parent of %plid, expected %expected_plid.', array('%mlid' => $mlid, '%plid' => $menu_link['plid'], '%expected_plid' => $plid)));
    }
  }

  /**
   * Test automatic reparenting of menu links.
   */
  function testMenuLinkReparenting($module = 'menu_test') {
    // Check the initial hierarchy.
    $links = $this->createLinkHierarchy($module);

    $expected_hierarchy = array(
      'parent' => FALSE,
      'child-1' => 'parent',
      'child-1-1' => 'child-1',
      'child-1-2' => 'child-1',
      'child-2' => 'parent',
    );
    $this->assertMenuLinkParents($links, $expected_hierarchy);

    // Start over, and move child-1 under child-2, and check that all the
    // childs of child-1 have been moved too.
    $links = $this->createLinkHierarchy($module);
    $links['child-1']['plid'] = $links['child-2']['mlid'];
    menu_link_save($links['child-1']);

    $expected_hierarchy = array(
      'parent' => FALSE,
      'child-1' => 'child-2',
      'child-1-1' => 'child-1',
      'child-1-2' => 'child-1',
      'child-2' => 'parent',
    );
    $this->assertMenuLinkParents($links, $expected_hierarchy);

    // Start over, and delete child-1, and check that the children of child-1
    // have been reassigned to the parent. menu_link_delete() will cowardly
    // refuse to delete a menu link defined by the system module, so skip the
    // test in that case.
    if ($module != 'system') {
      $links = $this->createLinkHierarchy($module);
      menu_link_delete($links['child-1']['mlid']);

      $expected_hierarchy = array(
        'parent' => FALSE,
        'child-1-1' => 'parent',
        'child-1-2' => 'parent',
        'child-2' => 'parent',
      );
      $this->assertMenuLinkParents($links, $expected_hierarchy);
    }

    // Start over, forcefully delete child-1 from the database, simulating a
    // database crash. Check that the children of child-1 have been reassigned
    // to the parent, going up on the old path hierarchy stored in each of the
    // links.
    $links = $this->createLinkHierarchy($module);
    // Don't do that at home.
    db_delete('menu_links')
      ->condition('mlid', $links['child-1']['mlid'])
      ->execute();

    $expected_hierarchy = array(
      'parent' => FALSE,
      'child-1-1' => 'parent',
      'child-1-2' => 'parent',
      'child-2' => 'parent',
    );
    $this->assertMenuLinkParents($links, $expected_hierarchy);

    // Start over, forcefully delete the parent from the database, simulating a
    // database crash. Check that the children of parent are now top-level.
    $links = $this->createLinkHierarchy($module);
    // Don't do that at home.
    db_delete('menu_links')
      ->condition('mlid', $links['parent']['mlid'])
      ->execute();

    $expected_hierarchy = array(
      'child-1-1' => 'child-1',
      'child-1-2' => 'child-1',
      'child-2' => FALSE,
    );
    $this->assertMenuLinkParents($links, $expected_hierarchy);
  }

  /**
   * Test automatic reparenting of menu links derived from menu routers.
   */
  function testMenuLinkRouterReparenting() {
    // Run all the standard parenting tests on menu links derived from
    // menu routers.
    $this->testMenuLinkReparenting('system');

    // Additionnaly, test reparenting based on path.
    $links = $this->createLinkHierarchy('system');

    // Move child-1-2 has a child of child-2, making the link hierarchy
    // inconsistent with the path hierarchy.
    $links['child-1-2']['plid'] = $links['child-2']['mlid'];
    menu_link_save($links['child-1-2']);

    // Check the new hierarchy.
    $expected_hierarchy = array(
      'parent' => FALSE,
      'child-1' => 'parent',
      'child-1-1' => 'child-1',
      'child-2' => 'parent',
      'child-1-2' => 'child-2',
    );
    $this->assertMenuLinkParents($links, $expected_hierarchy);

    // Now delete 'parent' directly from the database, simulating a database
    // crash. 'child-1' and 'child-2' should get moved to the
    // top-level.
    // Don't do that at home.
    db_delete('menu_links')
      ->condition('mlid', $links['parent']['mlid'])
      ->execute();
    $expected_hierarchy = array(
      'child-1' => FALSE,
      'child-1-1' => 'child-1',
      'child-2' => FALSE,
      'child-1-2' => 'child-2',
    );
    $this->assertMenuLinkParents($links, $expected_hierarchy);

    // Now delete 'child-2' directly from the database, simulating a database
    // crash. 'child-1-2' will get reparented under 'child-1' based on its
    // path.
    // Don't do that at home.
    db_delete('menu_links')
      ->condition('mlid', $links['child-2']['mlid'])
      ->execute();
    $expected_hierarchy = array(
      'child-1' => FALSE,
      'child-1-1' => 'child-1',
      'child-1-2' => 'child-1',
    );
    $this->assertMenuLinkParents($links, $expected_hierarchy);
  }
}

/**
 * Tests rebuilding the menu by setting 'menu_rebuild_needed.'
 */
class MenuRebuildTestCase extends DrupalWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Menu rebuild test',
      'description' => 'Test rebuilding of menu.',
      'group' => 'Menu',
    );
  }

  /**
   * Test if the 'menu_rebuild_needed' variable triggers a menu_rebuild() call.
   */
  function testMenuRebuildByVariable() {
    // Check if 'admin' path exists.
    $admin_exists = db_query('SELECT path from {menu_router} WHERE path = :path', array(':path' => 'admin'))->fetchField();
    $this->assertEqual($admin_exists, 'admin', "The path 'admin/' exists prior to deleting.");

    // Delete the path item 'admin', and test that the path doesn't exist in the database.
    $delete = db_delete('menu_router')
      ->condition('path', 'admin')
      ->execute();
    $admin_exists = db_query('SELECT path from {menu_router} WHERE path = :path', array(':path' => 'admin'))->fetchField();
    $this->assertFalse($admin_exists, "The path 'admin/' has been deleted and doesn't exist in the database.");

    // Now we enable the rebuild variable and trigger menu_execute_active_handler()
    // to rebuild the menu item. Now 'admin' should exist.
    variable_set('menu_rebuild_needed', TRUE);
    // menu_execute_active_handler() should trigger the rebuild.
    $this->drupalGet('<front>');
    $admin_exists = db_query('SELECT path from {menu_router} WHERE path = :path', array(':path' => 'admin'))->fetchField();
    $this->assertEqual($admin_exists, 'admin', "The menu has been rebuilt, the path 'admin' now exists again.");
  }

}

/**
 * Menu tree data related tests.
 */
class MenuTreeDataTestCase extends DrupalUnitTestCase {
  /**
   * Dummy link structure acceptable for menu_tree_data().
   */
  var $links = array(
    1 => array('mlid' => 1, 'depth' => 1),
    2 => array('mlid' => 2, 'depth' => 1),
    3 => array('mlid' => 3, 'depth' => 2),
    4 => array('mlid' => 4, 'depth' => 3),
    5 => array('mlid' => 5, 'depth' => 1),
  );

  public static function getInfo() {
    return array(
      'name' => 'Menu tree generation',
      'description' => 'Tests recursive menu tree generation functions.',
      'group' => 'Menu',
    );
  }

  /**
   * Validate the generation of a proper menu tree hierarchy.
   */
  function testMenuTreeData() {
    $tree = menu_tree_data($this->links);

    // Validate that parent items #1, #2, and #5 exist on the root level.
    $this->assertSameLink($this->links[1], $tree[1]['link'], 'Parent item #1 exists.');
    $this->assertSameLink($this->links[2], $tree[2]['link'], 'Parent item #2 exists.');
    $this->assertSameLink($this->links[5], $tree[5]['link'], 'Parent item #5 exists.');

    // Validate that child item #4 exists at the correct location in the hierarchy.
    $this->assertSameLink($this->links[4], $tree[2]['below'][3]['below'][4]['link'], 'Child item #4 exists in the hierarchy.');
  }

  /**
   * Check that two menu links are the same by comparing the mlid.
   *
   * @param $link1
   *   A menu link item.
   * @param $link2
   *   A menu link item.
   * @param $message
   *   The message to display along with the assertion.
   * @return
   *   TRUE if the assertion succeeded, FALSE otherwise.
   */
  protected function assertSameLink($link1, $link2, $message = '') {
    return $this->assert($link1['mlid'] == $link2['mlid'], $message ? $message : 'First link is identical to second link');
  }
}

/**
 * Menu tree output related tests.
 */
class MenuTreeOutputTestCase extends DrupalWebTestCase {
  /**
   * Dummy link structure acceptable for menu_tree_output().
   */
  var $tree_data = array(
    '1'=> array(
      'link' => array( 'menu_name' => 'main-menu', 'mlid' => 1, 'hidden'=>0, 'has_children' => 1, 'title' => 'Item 1', 'in_active_trail' => 1, 'access'=>1, 'href' => 'a', 'localized_options' => array('attributes' => array('title' =>'')) ),
      'below' => array(
        '2' => array('link' => array( 'menu_name' => 'main-menu', 'mlid' => 2, 'hidden'=>0, 'has_children' => 1, 'title' => 'Item 2', 'in_active_trail' => 1, 'access'=>1, 'href' => 'a/b', 'localized_options' => array('attributes' => array('title' =>'')) ),
          'below' => array(
            '3' => array('link' => array( 'menu_name' => 'main-menu', 'mlid' => 3, 'hidden'=>0, 'has_children' => 0, 'title' => 'Item 3', 'in_active_trail' => 0, 'access'=>1, 'href' => 'a/b/c', 'localized_options' => array('attributes' => array('title' =>'')) ),
              'below' => array() ),
            '4' => array('link' => array( 'menu_name' => 'main-menu', 'mlid' => 4, 'hidden'=>0, 'has_children' => 0, 'title' => 'Item 4', 'in_active_trail' => 0, 'access'=>1, 'href' => 'a/b/d', 'localized_options' => array('attributes' => array('title' =>'')) ),
              'below' => array() )
            )
          )
        )
      ),
    '5' => array('link' => array( 'menu_name' => 'main-menu', 'mlid' => 5, 'hidden'=>1, 'has_children' => 0, 'title' => 'Item 5', 'in_active_trail' => 0, 'access'=>1, 'href' => 'e', 'localized_options' => array('attributes' => array('title' =>'')) ), 'below' => array( ) ),
    '6' => array('link' => array( 'menu_name' => 'main-menu', 'mlid' => 6, 'hidden'=>0, 'has_children' => 0, 'title' => 'Item 6', 'in_active_trail' => 0, 'access'=>0, 'href' => 'f', 'localized_options' => array('attributes' => array('title' =>'')) ), 'below' => array( ) ),
    '7' => array('link' => array( 'menu_name' => 'main-menu', 'mlid' => 7, 'hidden'=>0, 'has_children' => 0, 'title' => 'Item 7', 'in_active_trail' => 0, 'access'=>1, 'href' => 'g', 'localized_options' => array('attributes' => array('title' =>'')) ), 'below' => array( ) )
  );

  public static function getInfo() {
    return array(
      'name' => 'Menu tree output',
      'description' => 'Tests menu tree output functions.',
      'group' => 'Menu',
    );
  }

  function setUp() {
    parent::setUp();
  }

  /**
   * Validate the generation of a proper menu tree output.
   */
  function testMenuTreeData() {
    $output = menu_tree_output($this->tree_data);

    // Validate that the - in main-menu is changed into an underscore
    $this->assertEqual($output['1']['#theme'], 'menu_link__main_menu', 'Hyphen is changed to an underscore on menu_link');
    $this->assertEqual($output['#theme_wrappers'][0], 'menu_tree__main_menu', 'Hyphen is changed to an underscore on menu_tree wrapper');
    // Looking for child items in the data
    $this->assertEqual( $output['1']['#below']['2']['#href'], 'a/b', 'Checking the href on a child item');
    $this->assertTrue( in_array('active-trail',$output['1']['#below']['2']['#attributes']['class']) , 'Checking the active trail class');
    // Validate that the hidden and no access items are missing
    $this->assertFalse( isset($output['5']), 'Hidden item should be missing');
    $this->assertFalse( isset($output['6']), 'False access should be missing');
    // Item 7 is after a couple hidden items. Just to make sure that 5 and 6 are skipped and 7 still included
    $this->assertTrue( isset($output['7']), 'Item after hidden items is present');
  }
}

/**
 * Menu breadcrumbs related tests.
 */
class MenuBreadcrumbTestCase extends MenuWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Breadcrumbs',
      'description' => 'Tests breadcrumbs functionality.',
      'group' => 'Menu',
    );
  }

  function setUp() {
    $modules = func_get_args();
    if (isset($modules[0]) && is_array($modules[0])) {
      $modules = $modules[0];
    }
    $modules[] = 'menu_test';
    parent::setUp($modules);
    $perms = array_keys(module_invoke_all('permission'));
    $this->admin_user = $this->drupalCreateUser($perms);
    $this->drupalLogin($this->admin_user);

    // This test puts menu links in the Navigation menu and then tests for
    // their presence on the page, so we need to ensure that the Navigation
    // block will be displayed in all active themes.
    db_update('block')
      ->fields(array(
        // Use a region that is valid for all themes.
        'region' => 'content',
        'status' => 1,
      ))
      ->condition('module', 'system')
      ->condition('delta', 'navigation')
      ->execute();
  }

  /**
   * Tests breadcrumbs on node and administrative paths.
   */
  function testBreadCrumbs() {
    // Prepare common base breadcrumb elements.
    $home = array('<front>' => 'Home');
    $admin = $home + array('admin' => t('Administration'));
    $config = $admin + array('admin/config' => t('Configuration'));
    $type = 'article';
    $langcode = LANGUAGE_NONE;

    // Verify breadcrumbs for default local tasks.
    $expected = array(
      'menu-test' => t('Menu test root'),
    );
    $title = t('Breadcrumbs test: Local tasks');
    $trail = $home + $expected;
    $tree = $expected + array(
      'menu-test/breadcrumb/tasks' => $title,
    );
    $this->assertBreadcrumb('menu-test/breadcrumb/tasks', $trail, $title, $tree);
    $this->assertBreadcrumb('menu-test/breadcrumb/tasks/first', $trail, $title, $tree);
    $this->assertBreadcrumb('menu-test/breadcrumb/tasks/first/first', $trail, $title, $tree);
    $trail += array(
      'menu-test/breadcrumb/tasks' => t('Breadcrumbs test: Local tasks'),
    );
    $this->assertBreadcrumb('menu-test/breadcrumb/tasks/first/second', $trail, $title, $tree);
    $this->assertBreadcrumb('menu-test/breadcrumb/tasks/second', $trail, $title, $tree);
    $this->assertBreadcrumb('menu-test/breadcrumb/tasks/second/first', $trail, $title, $tree);
    $trail += array(
      'menu-test/breadcrumb/tasks/second' => t('Second'),
    );
    $this->assertBreadcrumb('menu-test/breadcrumb/tasks/second/second', $trail, $title, $tree);

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

    $trail += array(
      'admin/structure/taxonomy' => t('Taxonomy'),
    );
    $this->assertBreadcrumb('admin/structure/taxonomy/tags', $trail);
    $trail += array(
      'admin/structure/taxonomy/tags' => t('Tags'),
    );
    $this->assertBreadcrumb('admin/structure/taxonomy/tags/edit', $trail);
    $this->assertBreadcrumb('admin/structure/taxonomy/tags/fields', $trail);
    $this->assertBreadcrumb('admin/structure/taxonomy/tags/add', $trail);

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

    $trail += array(
      'admin/structure/menu' => t('Menus'),
    );
    $this->assertBreadcrumb('admin/structure/menu/manage/navigation', $trail);
    $trail += array(
      'admin/structure/menu/manage/navigation' => t('Navigation'),
    );
    $this->assertBreadcrumb("admin/structure/menu/item/6/edit", $trail);
    $this->assertBreadcrumb('admin/structure/menu/manage/navigation/edit', $trail);
    $this->assertBreadcrumb('admin/structure/menu/manage/navigation/add', $trail);

    // 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/comment/fields", $trail);
    $this->assertBreadcrumb("admin/structure/types/manage/$type/comment/display", $trail);
    $this->assertBreadcrumb("admin/structure/types/manage/$type/delete", $trail);
    $trail += array(
      "admin/structure/types/manage/$type/fields" => t('Manage fields'),
    );
    $this->assertBreadcrumb("admin/structure/types/manage/$type/fields/body", $trail);
    $trail += array(
      "admin/structure/types/manage/$type/fields/body" => t('Body'),
    );
    $this->assertBreadcrumb("admin/structure/types/manage/$type/fields/body/widget-type", $trail);

    // Verify Filter text format administration breadcrumbs.
    $format = db_query_range("SELECT format, name FROM {filter_format}", 1, 1)->fetch();
    $format_id = $format->format;
    $trail = $config + array(
      'admin/config/content' => t('Content authoring'),
    );
    $this->assertBreadcrumb('admin/config/content/formats', $trail);

    $trail += array(
      'admin/config/content/formats' => t('Text formats'),
    );
    $this->assertBreadcrumb('admin/config/content/formats/add', $trail);
    $this->assertBreadcrumb("admin/config/content/formats/$format_id", $trail);
    $trail += array(
      "admin/config/content/formats/$format_id" => $format->name,
    );
    $this->assertBreadcrumb("admin/config/content/formats/$format_id/disable", $trail);

    // Verify node breadcrumbs (without menu link).
    $node1 = $this->drupalCreateNode();
    $nid1 = $node1->nid;
    $trail = $home;
    $this->assertBreadcrumb("node/$nid1", $trail);
    // Also verify that the node does not appear elsewhere (e.g., menu trees).
    $this->assertNoLink($node1->title);
    // The node itself should not be contained in the breadcrumb on the default
    // local task, since there is no difference between both pages.
    $this->assertBreadcrumb("node/$nid1/view", $trail);
    // Also verify that the node does not appear elsewhere (e.g., menu trees).
    $this->assertNoLink($node1->title);

    $trail += array(
      "node/$nid1" => $node1->title,
    );
    $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).
    // Do this separately for Main menu and Navigation menu, since only the
    // latter is a preferred menu by default.
    // @todo Also test all themes? Manually testing led to the suspicion that
    //   breadcrumbs may differ, possibly due to template.php overrides.
    $menus = array('main-menu', 'navigation');
    // Alter node type menu settings.
    variable_set("menu_options_$type", $menus);
    variable_set("menu_parent_$type", 'navigation:0');

    foreach ($menus as $menu) {
      // Create a parent node in the current menu.
      $title = $this->randomName();
      $node2 = $this->drupalCreateNode(array(
        'type' => $type,
        'title' => $title,
        'menu' => array(
          'enabled' => 1,
          'link_title' => 'Parent ' . $title,
          'description' => '',
          'menu_name' => $menu,
          'plid' => 0,
        ),
      ));
      $nid2 = $node2->nid;

      $trail = $home;
      $tree = array(
        "node/$nid2" => $node2->menu['link_title'],
      );
      $this->assertBreadcrumb("node/$nid2", $trail, $node2->title, $tree);
      // The node itself should not be contained in the breadcrumb on the
      // default local task, since there is no difference between both pages.
      $this->assertBreadcrumb("node/$nid2/view", $trail, $node2->title, $tree);
      $trail += array(
        "node/$nid2" => $node2->menu['link_title'],
      );
      $this->assertBreadcrumb("node/$nid2/edit", $trail);

      // Create a child node in the current menu.
      $title = $this->randomName();
      $node3 = $this->drupalCreateNode(array(
        'type' => $type,
        'title' => $title,
        'menu' => array(
          'enabled' => 1,
          'link_title' => 'Child ' . $title,
          'description' => '',
          'menu_name' => $menu,
          'plid' => $node2->menu['mlid'],
        ),
      ));
      $nid3 = $node3->nid;

      $this->assertBreadcrumb("node/$nid3", $trail, $node3->title, $tree, FALSE);
      // The node itself should not be contained in the breadcrumb on the
      // default local task, since there is no difference between both pages.
      $this->assertBreadcrumb("node/$nid3/view", $trail, $node3->title, $tree, FALSE);
      $trail += array(
        "node/$nid3" => $node3->menu['link_title'],
      );
      $tree += array(
        "node/$nid3" => $node3->menu['link_title'],
      );
      $this->assertBreadcrumb("node/$nid3/edit", $trail);

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

      if ($menu == 'navigation') {
        $parent = $node2;
        $child = $node3;
      }
    }

    // Create a Navigation menu link for 'node', move the last parent node menu
    // link below it, and verify a full breadcrumb for the last child node.
    $menu = 'navigation';
    $edit = array(
      'link_title' => 'Root',
      'link_path' => 'node',
    );
    $this->drupalPost("admin/structure/menu/manage/$menu/add", $edit, t('Save'));
    $link = db_query('SELECT * FROM {menu_links} WHERE link_title = :title', array(':title' => 'Root'))->fetchAssoc();

    $edit = array(
      'menu[parent]' => $link['menu_name'] . ':' . $link['mlid'],
    );
    $this->drupalPost("node/{$parent->nid}/edit", $edit, t('Save'));
    $expected = array(
      "node" => $link['link_title'],
    );
    $trail = $home + $expected;
    $tree = $expected + array(
      "node/{$parent->nid}" => $parent->menu['link_title'],
    );
    $this->assertBreadcrumb(NULL, $trail, $parent->title, $tree);
    $trail += array(
      "node/{$parent->nid}" => $parent->menu['link_title'],
    );
    $tree += array(
      "node/{$child->nid}" => $child->menu['link_title'],
    );
    $this->assertBreadcrumb("node/{$child->nid}", $trail, $child->title, $tree);

    // Add a taxonomy term/tag to last node, and add a link for that term to the
    // Navigation menu.
    $tags = array(
      'Drupal' => array(),
      'Breadcrumbs' => array(),
    );
    $edit = array(
      "field_tags[$langcode]" => implode(',', array_keys($tags)),
    );
    $this->drupalPost("node/{$parent->nid}/edit", $edit, t('Save'));

    // 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) {
      $terms = taxonomy_term_load_multiple(NULL, array('name' => $name));
      $term = reset($terms);
      $tags[$name]['term'] = $term;
      if ($parent_tid) {
        $edit = array(
          'parent[]' => array($parent_tid),
        );
        $this->drupalPost("taxonomy/term/{$term->tid}/edit", $edit, t('Save'));
      }
      $parent_tid = $term->tid;
    }
    $parent_mlid = 0;
    foreach ($tags as $name => $data) {
      $term = $data['term'];
      $edit = array(
        'link_title' => "$name link",
        'link_path' => "taxonomy/term/{$term->tid}",
        'parent' => "$menu:{$parent_mlid}",
      );
      $this->drupalPost("admin/structure/menu/manage/$menu/add", $edit, t('Save'));
      $tags[$name]['link'] = db_query('SELECT * FROM {menu_links} WHERE link_title = :title AND link_path = :href', array(
        ':title' => $edit['link_title'],
        ':href' => $edit['link_path'],
      ))->fetchAssoc();
      $tags[$name]['link']['link_path'] = $edit['link_path'];
      $parent_mlid = $tags[$name]['link']['mlid'];
    }

    // Verify expected breadcrumbs for menu links.
    $trail = $home;
    $tree = array();
    foreach ($tags as $name => $data) {
      $term = $data['term'];
      $link = $data['link'];

      $tree += array(
        $link['link_path'] => $link['link_title'],
      );
      $this->assertBreadcrumb($link['link_path'], $trail, $term->name, $tree);
      $this->assertRaw(check_plain($parent->title), 'Tagged node found.');

      // 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(
        ':menu' => 'block-system-navigation',
        ':href' => url($link['link_path']),
      ));
      $this->assertTrue(count($elements) == 1, "Link to {$link['link_path']} appears only once.");

      // Next iteration should expect this tag as parent link.
      // Note: Term name, not link name, due to taxonomy_term_page().
      $trail += array(
        $link['link_path'] => $term->name,
      );
    }

    // 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',
    ));
    $this->drupalLogout();

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

    // Verify breadcrumb on user pages (without menu link) for anonymous user.
    $trail = $home;
    $this->assertBreadcrumb('user', $trail, t('User account'));
    $this->assertBreadcrumb('user/' . $this->admin_user->uid, $trail, $this->admin_user->name);

    // Verify breadcrumb on user pages (without menu link) for registered users.
    $this->drupalLogin($this->admin_user);
    $trail = $home;
    $this->assertBreadcrumb('user', $trail, $this->admin_user->name);
    $this->assertBreadcrumb('user/' . $this->admin_user->uid, $trail, $this->admin_user->name);
    $trail += array(
      'user/' . $this->admin_user->uid => $this->admin_user->name,
    );
    $this->assertBreadcrumb('user/' . $this->admin_user->uid . '/edit', $trail, $this->admin_user->name);

    // 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);

    // Verify correct breadcrumb and page title on another user's account pages
    // (without menu link).
    $trail = $home;
    $this->assertBreadcrumb('user/' . $this->admin_user->uid, $trail, $this->admin_user->name);
    $trail += array(
      'user/' . $this->admin_user->uid => $this->admin_user->name,
    );
    $this->assertBreadcrumb('user/' . $this->admin_user->uid . '/edit', $trail, $this->admin_user->name);

    // Verify correct breadcrumb and page title when viewing own user account
    // pages (without menu link).
    $trail = $home;
    $this->assertBreadcrumb('user/' . $this->web_user->uid, $trail, $this->web_user->name);
    $trail += array(
      'user/' . $this->web_user->uid => $this->web_user->name,
    );
    $this->assertBreadcrumb('user/' . $this->web_user->uid . '/edit', $trail, $this->web_user->name);

    // Add a Navigation menu links for 'user' and $this->admin_user.
    // Although it may be faster to manage these links via low-level API
    // functions, there's a lot that can go wrong in doing so.
    $this->drupalLogin($this->admin_user);
    $edit = array(
      'link_title' => 'User',
      'link_path' => 'user',
    );
    $this->drupalPost("admin/structure/menu/manage/$menu/add", $edit, t('Save'));
    $link_user = db_query('SELECT * FROM {menu_links} WHERE link_title = :title AND link_path = :href', array(
      ':title' => $edit['link_title'],
      ':href' => $edit['link_path'],
    ))->fetchAssoc();

    $edit = array(
      'link_title' => $this->admin_user->name . ' link',
      'link_path' => 'user/' . $this->admin_user->uid,
    );
    $this->drupalPost("admin/structure/menu/manage/$menu/add", $edit, t('Save'));
    $link_admin_user = db_query('SELECT * FROM {menu_links} WHERE link_title = :title AND link_path = :href', array(
      ':title' => $edit['link_title'],
      ':href' => $edit['link_path'],
    ))->fetchAssoc();

    // Verify expected breadcrumbs for the two separate links.
    $this->drupalLogout();
    $trail = $home;
    $tree = array(
      $link_user['link_path'] => $link_user['link_title'],
    );
    $this->assertBreadcrumb('user', $trail, $link_user['link_title'], $tree);
    $tree = array(
      $link_admin_user['link_path'] => $link_admin_user['link_title'],
    );
    $this->assertBreadcrumb('user/' . $this->admin_user->uid, $trail, $link_admin_user['link_title'], $tree);

    $this->drupalLogin($this->admin_user);
    $trail += array(
      $link_admin_user['link_path'] => $link_admin_user['link_title'],
    );
    $this->assertBreadcrumb('user/' . $this->admin_user->uid . '/edit', $trail, $link_admin_user['link_title'], $tree, FALSE);

    // Move 'user/%' below 'user' and verify again.
    $edit = array(
      'parent' => "$menu:{$link_user['mlid']}",
    );
    $this->drupalPost("admin/structure/menu/item/{$link_admin_user['mlid']}/edit", $edit, t('Save'));

    $this->drupalLogout();
    $trail = $home;
    $tree = array(
      $link_user['link_path'] => $link_user['link_title'],
    );
    $this->assertBreadcrumb('user', $trail, $link_user['link_title'], $tree);
    $trail += array(
      $link_user['link_path'] => $link_user['link_title'],
    );
    $tree += array(
      $link_admin_user['link_path'] => $link_admin_user['link_title'],
    );
    $this->assertBreadcrumb('user/' . $this->admin_user->uid, $trail, $link_admin_user['link_title'], $tree);

    $this->drupalLogin($this->admin_user);
    $trail += array(
      $link_admin_user['link_path'] => $link_admin_user['link_title'],
    );
    $this->assertBreadcrumb('user/' . $this->admin_user->uid . '/edit', $trail, $link_admin_user['link_title'], $tree, FALSE);

    // 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
    // page title, and that the breadcrumb is empty (because the user is not
    // able to access "Administer", so the trail cannot recurse into it).
    $trail = array();
    $this->assertBreadcrumb('admin', $trail, t('Access denied'));
    $this->assertResponse(403);

    $trail = $home;
    $this->assertBreadcrumb('admin/reports', $trail, t('Reports'));
    $this->assertNoResponse(403);

    $this->assertBreadcrumb('admin/reports/dblog', $trail, t('Recent log messages'));
    $this->assertNoResponse(403);
  }
}

/**
 * Tests active menu trails.
 */
class MenuTrailTestCase extends MenuWebTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Active trail',
      'description' => 'Tests active menu trails and alteration functionality.',
      'group' => 'Menu',
    );
  }

  function setUp() {
    $modules = func_get_args();
    if (isset($modules[0]) && is_array($modules[0])) {
      $modules = $modules[0];
    }
    $modules[] = 'menu_test';
    parent::setUp($modules);
    $this->admin_user = $this->drupalCreateUser(array('administer site configuration', 'access administration pages'));
    $this->drupalLogin($this->admin_user);

    // This test puts menu links in the Navigation menu and then tests for
    // their presence on the page, so we need to ensure that the Navigation
    // block will be displayed in all active themes.
    db_update('block')
      ->fields(array(
        // Use a region that is valid for all themes.
        'region' => 'content',
        'status' => 1,
      ))
      ->condition('module', 'system')
      ->condition('delta', 'navigation')
      ->execute();

    // This test puts menu links in the Management menu and then tests for
    // their presence on the page, so we need to ensure that the Management
    // block will be displayed in all active themes.
    db_update('block')
      ->fields(array(
        // Use a region that is valid for all themes.
        'region' => 'content',
        'status' => 1,
      ))
      ->condition('module', 'system')
      ->condition('delta', 'management')
      ->execute();
  }

  /**
   * Tests active trails are properly affected by menu_tree_set_path().
   */
  function testMenuTreeSetPath() {
    $home = array('<front>' => 'Home');
    $config_tree = array(
      'admin' => t('Administration'),
      'admin/config' => t('Configuration'),
    );
    $config = $home + $config_tree;

    // The menu_test_menu_tree_set_path system variable controls whether or not
    // the menu_test_menu_trail_callback() callback (used by all paths in these
    // tests) issues an overriding call to menu_trail_set_path().
    $test_menu_path = array(
      'menu_name' => 'management',
      'path' => 'admin/config/system/site-information',
    );

    $breadcrumb = $home + array(
      'menu-test' => t('Menu test root'),
    );
    $tree = array(
      'menu-test' => t('Menu test root'),
      'menu-test/menu-trail' => t('Menu trail - Case 1'),
    );

    // Test the tree generation for the Navigation menu.
    variable_del('menu_test_menu_tree_set_path');
    $this->assertBreadcrumb('menu-test/menu-trail', $breadcrumb, t('Menu trail - Case 1'), $tree);

    // Override the active trail for the Management tree; it should not affect
    // the Navigation tree.
    variable_set('menu_test_menu_tree_set_path', $test_menu_path);
    $this->assertBreadcrumb('menu-test/menu-trail', $breadcrumb, t('Menu trail - Case 1'), $tree);

    $breadcrumb = $config + array(
      'admin/config/development' => t('Development'),
    );
    $tree = $config_tree + array(
      'admin/config/development' => t('Development'),
      'admin/config/development/menu-trail' => t('Menu trail - Case 2'),
    );

    $override_breadcrumb = $config + array(
      'admin/config/system' => t('System'),
      'admin/config/system/site-information' => t('Site information'),
    );
    $override_tree = $config_tree + array(
      'admin/config/system' => t('System'),
      'admin/config/system/site-information' => t('Site information'),
    );

    // Test the tree generation for the Management menu.
    variable_del('menu_test_menu_tree_set_path');
    $this->assertBreadcrumb('admin/config/development/menu-trail', $breadcrumb, t('Menu trail - Case 2'), $tree);

    // Override the active trail for the Management tree; it should affect the
    // breadcrumbs and Management tree.
    variable_set('menu_test_menu_tree_set_path', $test_menu_path);
    $this->assertBreadcrumb('admin/config/development/menu-trail', $override_breadcrumb, t('Menu trail - Case 2'), $override_tree);
  }

  /**
   * Tests that the active trail works correctly on custom 403 and 404 pages.
   */
  function testCustom403And404Pages() {
    // Set the custom 403 and 404 pages we will use.
    variable_set('site_403', 'menu-test/custom-403-page');
    variable_set('site_404', 'menu-test/custom-404-page');

    // Define the paths we'll visit to trigger 403 and 404 responses during
    // this test, and the expected active trail for each case.
    $paths = array(
      403 => 'admin/config',
      404 => $this->randomName(),
    );
    // For the 403 page, the initial trail during the Drupal bootstrap should
    // include the page that the user is trying to visit, while the final trail
    // should reflect the custom 403 page that the user was redirected to.
    $expected_trail[403]['initial'] = array(
      '<front>' => 'Home',
      'admin/config' => 'Configuration',
    );
    $expected_trail[403]['final'] = array(
      '<front>' => 'Home',
      'menu-test' => 'Menu test root',
      'menu-test/custom-403-page' => 'Custom 403 page',
    );
    // For the 404 page, the initial trail during the Drupal bootstrap should
    // only contain the link back to "Home" (since the page the user is trying
    // to visit doesn't have any menu items associated with it), while the
    // final trail should reflect the custom 404 page that the user was
    // redirected to.
    $expected_trail[404]['initial'] = array(
      '<front>' => 'Home',
    );
    $expected_trail[404]['final'] = array(
      '<front>' => 'Home',
      'menu-test' => 'Menu test root',
      'menu-test/custom-404-page' => 'Custom 404 page',
    );

    // Visit each path as an anonymous user so that we will actually get a 403
    // on admin/config.
    $this->drupalLogout();
    foreach (array(403, 404) as $status_code) {
      // Before visiting the page, trigger the code in the menu_test module
      // that will record the active trail (so we can check it in this test).
      variable_set('menu_test_record_active_trail', TRUE);
      $this->drupalGet($paths[$status_code]);
      $this->assertResponse($status_code);

      // Check that the initial trail (during the Drupal bootstrap) matches
      // what we expect.
      $initial_trail = variable_get('menu_test_active_trail_initial', array());
      $this->assertEqual(count($initial_trail), count($expected_trail[$status_code]['initial']), format_string('The initial active trail for a @status_code page contains the expected number of items (expected: @expected, found: @found).', array(
        '@status_code' => $status_code,
        '@expected' => count($expected_trail[$status_code]['initial']),
        '@found' => count($initial_trail),
      )));
      foreach (array_keys($expected_trail[$status_code]['initial']) as $index => $path) {
        $this->assertEqual($initial_trail[$index]['href'], $path, format_string('Element number @number of the initial active trail for a @status_code page contains the correct path (expected: @expected, found: @found)', array(
          '@number' => $index + 1,
          '@status_code' => $status_code,
          '@expected' => $path,
          '@found' => $initial_trail[$index]['href'],
        )));
      }

      // Check that the final trail (after the user has been redirected to the
      // custom 403/404 page) matches what we expect.
      $final_trail = variable_get('menu_test_active_trail_final', array());
      $this->assertEqual(count($final_trail), count($expected_trail[$status_code]['final']), format_string('The final active trail for a @status_code page contains the expected number of items (expected: @expected, found: @found).', array(
        '@status_code' => $status_code,
        '@expected' => count($expected_trail[$status_code]['final']),
        '@found' => count($final_trail),
      )));
      foreach (array_keys($expected_trail[$status_code]['final']) as $index => $path) {
        $this->assertEqual($final_trail[$index]['href'], $path, format_string('Element number @number of the final active trail for a @status_code page contains the correct path (expected: @expected, found: @found)', array(
          '@number' => $index + 1,
          '@status_code' => $status_code,
          '@expected' => $path,
          '@found' => $final_trail[$index]['href'],
        )));
      }

      // Check that the breadcrumb displayed on the final custom 403/404 page
      // matches what we expect. (The last item of the active trail represents
      // the current page, which is not supposed to appear in the breadcrumb,
      // so we need to remove it from the array before checking.)
      array_pop($expected_trail[$status_code]['final']);
      $this->assertBreadcrumb(NULL, $expected_trail[$status_code]['final']);
    }
  }
}