menu.test 71 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740
  1. <?php
  2. /**
  3. * @file
  4. * Provides SimpleTests for menu.inc.
  5. */
  6. class MenuWebTestCase extends DrupalWebTestCase {
  7. function setUp() {
  8. $modules = func_get_args();
  9. if (isset($modules[0]) && is_array($modules[0])) {
  10. $modules = $modules[0];
  11. }
  12. parent::setUp($modules);
  13. }
  14. /**
  15. * Assert that a given path shows certain breadcrumb links.
  16. *
  17. * @param string $goto
  18. * (optional) A system path to pass to DrupalWebTestCase::drupalGet().
  19. * @param array $trail
  20. * An associative array whose keys are expected breadcrumb link paths and
  21. * whose values are expected breadcrumb link texts (not sanitized).
  22. * @param string $page_title
  23. * (optional) A page title to additionally assert via
  24. * DrupalWebTestCase::assertTitle(). Without site name suffix.
  25. * @param array $tree
  26. * (optional) An associative array whose keys are link paths and whose
  27. * values are link titles (not sanitized) of an expected active trail in a
  28. * menu tree output on the page.
  29. * @param $last_active
  30. * (optional) Whether the last link in $tree is expected to be active (TRUE)
  31. * or just to be in the active trail (FALSE).
  32. */
  33. protected function assertBreadcrumb($goto, array $trail, $page_title = NULL, array $tree = array(), $last_active = TRUE) {
  34. if (isset($goto)) {
  35. $this->drupalGet($goto);
  36. }
  37. // Compare paths with actual breadcrumb.
  38. $parts = $this->getParts();
  39. $pass = TRUE;
  40. foreach ($trail as $path => $title) {
  41. $url = url($path);
  42. $part = array_shift($parts);
  43. $pass = ($pass && $part['href'] === $url && $part['text'] === check_plain($title));
  44. }
  45. // No parts must be left, or an expected "Home" will always pass.
  46. $pass = ($pass && empty($parts));
  47. $this->assertTrue($pass, t('Breadcrumb %parts found on @path.', array(
  48. '%parts' => implode(' » ', $trail),
  49. '@path' => $this->getUrl(),
  50. )));
  51. // Additionally assert page title, if given.
  52. if (isset($page_title)) {
  53. $this->assertTitle(strtr('@title | Drupal', array('@title' => $page_title)));
  54. }
  55. // Additionally assert active trail in a menu tree output, if given.
  56. if ($tree) {
  57. end($tree);
  58. $active_link_path = key($tree);
  59. $active_link_title = array_pop($tree);
  60. $xpath = '';
  61. if ($tree) {
  62. $i = 0;
  63. foreach ($tree as $link_path => $link_title) {
  64. $part_xpath = (!$i ? '//' : '/following-sibling::ul/descendant::');
  65. $part_xpath .= 'li[contains(@class, :class)]/a[contains(@href, :href) and contains(text(), :title)]';
  66. $part_args = array(
  67. ':class' => 'active-trail',
  68. ':href' => url($link_path),
  69. ':title' => $link_title,
  70. );
  71. $xpath .= $this->buildXPathQuery($part_xpath, $part_args);
  72. $i++;
  73. }
  74. $elements = $this->xpath($xpath);
  75. $this->assertTrue(!empty($elements), t('Active trail to current page was found in menu tree.'));
  76. // Append prefix for active link asserted below.
  77. $xpath .= '/following-sibling::ul/descendant::';
  78. }
  79. else {
  80. $xpath .= '//';
  81. }
  82. $xpath_last_active = ($last_active ? 'and contains(@class, :class-active)' : '');
  83. $xpath .= 'li[contains(@class, :class-trail)]/a[contains(@href, :href) ' . $xpath_last_active . 'and contains(text(), :title)]';
  84. $args = array(
  85. ':class-trail' => 'active-trail',
  86. ':class-active' => 'active',
  87. ':href' => url($active_link_path),
  88. ':title' => $active_link_title,
  89. );
  90. $elements = $this->xpath($xpath, $args);
  91. $this->assertTrue(!empty($elements), t('Active link %title was found in menu tree, including active trail links %tree.', array(
  92. '%title' => $active_link_title,
  93. '%tree' => implode(' » ', $tree),
  94. )));
  95. }
  96. }
  97. /**
  98. * Returns the breadcrumb contents of the current page in the internal browser.
  99. */
  100. protected function getParts() {
  101. $parts = array();
  102. $elements = $this->xpath('//div[@class="breadcrumb"]/a');
  103. if (!empty($elements)) {
  104. foreach ($elements as $element) {
  105. $parts[] = array(
  106. 'text' => (string) $element,
  107. 'href' => (string) $element['href'],
  108. 'title' => (string) $element['title'],
  109. );
  110. }
  111. }
  112. return $parts;
  113. }
  114. }
  115. class MenuRouterTestCase extends DrupalWebTestCase {
  116. public static function getInfo() {
  117. return array(
  118. 'name' => 'Menu router',
  119. 'description' => 'Tests menu router and hook_menu() functionality.',
  120. 'group' => 'Menu',
  121. );
  122. }
  123. function setUp() {
  124. // Enable dummy module that implements hook_menu.
  125. parent::setUp('menu_test');
  126. // Make the tests below more robust by explicitly setting the default theme
  127. // and administrative theme that they expect.
  128. theme_enable(array('bartik'));
  129. variable_set('theme_default', 'bartik');
  130. variable_set('admin_theme', 'seven');
  131. }
  132. /**
  133. * Test title callback set to FALSE.
  134. */
  135. function testTitleCallbackFalse() {
  136. $this->drupalGet('node');
  137. $this->assertText('A title with @placeholder', t('Raw text found on the page'));
  138. $this->assertNoText(t('A title with @placeholder', array('@placeholder' => 'some other text')), t('Text with placeholder substitutions not found.'));
  139. }
  140. /**
  141. * Tests page title of MENU_CALLBACKs.
  142. */
  143. function testTitleMenuCallback() {
  144. // Verify that the menu router item title is not visible.
  145. $this->drupalGet('');
  146. $this->assertNoText(t('Menu Callback Title'));
  147. // Verify that the menu router item title is output as page title.
  148. $this->drupalGet('menu_callback_title');
  149. $this->assertText(t('Menu Callback Title'));
  150. }
  151. /**
  152. * Test the theme callback when it is set to use an administrative theme.
  153. */
  154. function testThemeCallbackAdministrative() {
  155. $this->drupalGet('menu-test/theme-callback/use-admin-theme');
  156. $this->assertText('Custom theme: seven. Actual theme: seven.', t('The administrative theme can be correctly set in a theme callback.'));
  157. $this->assertRaw('seven/style.css', t("The administrative theme's CSS appears on the page."));
  158. }
  159. /**
  160. * Test that the theme callback is properly inherited.
  161. */
  162. function testThemeCallbackInheritance() {
  163. $this->drupalGet('menu-test/theme-callback/use-admin-theme/inheritance');
  164. $this->assertText('Custom theme: seven. Actual theme: seven. Theme callback inheritance is being tested.', t('Theme callback inheritance correctly uses the administrative theme.'));
  165. $this->assertRaw('seven/style.css', t("The administrative theme's CSS appears on the page."));
  166. }
  167. /**
  168. * Test that 'page callback', 'file' and 'file path' keys are properly
  169. * inherited from parent menu paths.
  170. */
  171. function testFileInheritance() {
  172. $this->drupalGet('admin/config/development/file-inheritance');
  173. $this->assertText('File inheritance test description', t('File inheritance works.'));
  174. }
  175. /**
  176. * Test path containing "exotic" characters.
  177. */
  178. function testExoticPath() {
  179. $path = "menu-test/ -._~!$'\"()*@[]?&+%#,;=:" . // "Special" ASCII characters.
  180. "%23%25%26%2B%2F%3F" . // Characters that look like a percent-escaped string.
  181. "éøïвβ中國書۞"; // Characters from various non-ASCII alphabets.
  182. $this->drupalGet($path);
  183. $this->assertRaw('This is menu_test_callback().');
  184. }
  185. /**
  186. * Test the theme callback when the site is in maintenance mode.
  187. */
  188. function testThemeCallbackMaintenanceMode() {
  189. variable_set('maintenance_mode', TRUE);
  190. // For a regular user, the fact that the site is in maintenance mode means
  191. // we expect the theme callback system to be bypassed entirely.
  192. $this->drupalGet('menu-test/theme-callback/use-admin-theme');
  193. $this->assertRaw('bartik/css/style.css', t("The maintenance theme's CSS appears on the page."));
  194. // An administrator, however, should continue to see the requested theme.
  195. $admin_user = $this->drupalCreateUser(array('access site in maintenance mode'));
  196. $this->drupalLogin($admin_user);
  197. $this->drupalGet('menu-test/theme-callback/use-admin-theme');
  198. $this->assertText('Custom theme: seven. Actual theme: seven.', t('The theme callback system is correctly triggered for an administrator when the site is in maintenance mode.'));
  199. $this->assertRaw('seven/style.css', t("The administrative theme's CSS appears on the page."));
  200. }
  201. /**
  202. * Make sure the maintenance mode can be bypassed using hook_menu_site_status_alter().
  203. *
  204. * @see hook_menu_site_status_alter().
  205. */
  206. function testMaintenanceModeLoginPaths() {
  207. variable_set('maintenance_mode', TRUE);
  208. $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')));
  209. $this->drupalLogout();
  210. $this->drupalGet('node');
  211. $this->assertText($offline_message);
  212. $this->drupalGet('menu_login_callback');
  213. $this->assertText('This is menu_login_callback().', t('Maintenance mode can be bypassed through hook_login_paths().'));
  214. }
  215. /**
  216. * Test that an authenticated user hitting 'user/login' gets redirected to
  217. * 'user' and 'user/register' gets redirected to the user edit page.
  218. */
  219. function testAuthUserUserLogin() {
  220. $loggedInUser = $this->drupalCreateUser(array());
  221. $this->drupalLogin($loggedInUser);
  222. $this->drupalGet('user/login');
  223. // Check that we got to 'user'.
  224. $this->assertTrue($this->url == url('user', array('absolute' => TRUE)), t("Logged-in user redirected to q=user on accessing q=user/login"));
  225. // user/register should redirect to user/UID/edit.
  226. $this->drupalGet('user/register');
  227. $this->assertTrue($this->url == url('user/' . $this->loggedInUser->uid . '/edit', array('absolute' => TRUE)), t("Logged-in user redirected to q=user/UID/edit on accessing q=user/register"));
  228. }
  229. /**
  230. * Test the theme callback when it is set to use an optional theme.
  231. */
  232. function testThemeCallbackOptionalTheme() {
  233. // Request a theme that is not enabled.
  234. $this->drupalGet('menu-test/theme-callback/use-stark-theme');
  235. $this->assertText('Custom theme: NONE. Actual theme: bartik.', t('The theme callback system falls back on the default theme when a theme that is not enabled is requested.'));
  236. $this->assertRaw('bartik/css/style.css', t("The default theme's CSS appears on the page."));
  237. // Now enable the theme and request it again.
  238. theme_enable(array('stark'));
  239. $this->drupalGet('menu-test/theme-callback/use-stark-theme');
  240. $this->assertText('Custom theme: stark. Actual theme: stark.', t('The theme callback system uses an optional theme once it has been enabled.'));
  241. $this->assertRaw('stark/layout.css', t("The optional theme's CSS appears on the page."));
  242. }
  243. /**
  244. * Test the theme callback when it is set to use a theme that does not exist.
  245. */
  246. function testThemeCallbackFakeTheme() {
  247. $this->drupalGet('menu-test/theme-callback/use-fake-theme');
  248. $this->assertText('Custom theme: NONE. Actual theme: bartik.', t('The theme callback system falls back on the default theme when a theme that does not exist is requested.'));
  249. $this->assertRaw('bartik/css/style.css', t("The default theme's CSS appears on the page."));
  250. }
  251. /**
  252. * Test the theme callback when no theme is requested.
  253. */
  254. function testThemeCallbackNoThemeRequested() {
  255. $this->drupalGet('menu-test/theme-callback/no-theme-requested');
  256. $this->assertText('Custom theme: NONE. Actual theme: bartik.', t('The theme callback system falls back on the default theme when no theme is requested.'));
  257. $this->assertRaw('bartik/css/style.css', t("The default theme's CSS appears on the page."));
  258. }
  259. /**
  260. * Test that hook_custom_theme() can control the theme of a page.
  261. */
  262. function testHookCustomTheme() {
  263. // Trigger hook_custom_theme() to dynamically request the Stark theme for
  264. // the requested page.
  265. variable_set('menu_test_hook_custom_theme_name', 'stark');
  266. theme_enable(array('stark'));
  267. // Visit a page that does not implement a theme callback. The above request
  268. // should be honored.
  269. $this->drupalGet('menu-test/no-theme-callback');
  270. $this->assertText('Custom theme: stark. Actual theme: stark.', t('The result of hook_custom_theme() is used as the theme for the current page.'));
  271. $this->assertRaw('stark/layout.css', t("The Stark theme's CSS appears on the page."));
  272. }
  273. /**
  274. * Test that the theme callback wins out over hook_custom_theme().
  275. */
  276. function testThemeCallbackHookCustomTheme() {
  277. // Trigger hook_custom_theme() to dynamically request the Stark theme for
  278. // the requested page.
  279. variable_set('menu_test_hook_custom_theme_name', 'stark');
  280. theme_enable(array('stark'));
  281. // The menu "theme callback" should take precedence over a value set in
  282. // hook_custom_theme().
  283. $this->drupalGet('menu-test/theme-callback/use-admin-theme');
  284. $this->assertText('Custom theme: seven. Actual theme: seven.', t('The result of hook_custom_theme() does not override what was set in a theme callback.'));
  285. $this->assertRaw('seven/style.css', t("The Seven theme's CSS appears on the page."));
  286. }
  287. /**
  288. * Tests for menu_link_maintain().
  289. */
  290. function testMenuLinkMaintain() {
  291. $admin_user = $this->drupalCreateUser(array('administer site configuration'));
  292. $this->drupalLogin($admin_user);
  293. // Create three menu items.
  294. menu_link_maintain('menu_test', 'insert', 'menu_test_maintain/1', 'Menu link #1');
  295. menu_link_maintain('menu_test', 'insert', 'menu_test_maintain/1', 'Menu link #1-1');
  296. menu_link_maintain('menu_test', 'insert', 'menu_test_maintain/2', 'Menu link #2');
  297. // Move second link to the main-menu, to test caching later on.
  298. db_update('menu_links')
  299. ->fields(array('menu_name' => 'main-menu'))
  300. ->condition('link_title', 'Menu link #1-1')
  301. ->condition('customized', 0)
  302. ->condition('module', 'menu_test')
  303. ->execute();
  304. menu_cache_clear('main-menu');
  305. // Load front page.
  306. $this->drupalGet('node');
  307. $this->assertLink(t('Menu link #1'), 0, 'Found menu link #1');
  308. $this->assertLink(t('Menu link #1-1'), 0, 'Found menu link #1-1');
  309. $this->assertLink(t('Menu link #2'), 0, 'Found menu link #2');
  310. // Rename all links for the given path.
  311. menu_link_maintain('menu_test', 'update', 'menu_test_maintain/1', 'Menu link updated');
  312. // Load a different page to be sure that we have up to date information.
  313. $this->drupalGet('menu_test_maintain/1');
  314. $this->assertLink(t('Menu link updated'), 0, t('Found updated menu link'));
  315. $this->assertNoLink(t('Menu link #1'), 0, t('Not found menu link #1'));
  316. $this->assertNoLink(t('Menu link #1'), 0, t('Not found menu link #1-1'));
  317. $this->assertLink(t('Menu link #2'), 0, t('Found menu link #2'));
  318. // Delete all links for the given path.
  319. menu_link_maintain('menu_test', 'delete', 'menu_test_maintain/1', '');
  320. // Load a different page to be sure that we have up to date information.
  321. $this->drupalGet('menu_test_maintain/2');
  322. $this->assertNoLink(t('Menu link updated'), 0, t('Not found deleted menu link'));
  323. $this->assertNoLink(t('Menu link #1'), 0, t('Not found menu link #1'));
  324. $this->assertNoLink(t('Menu link #1'), 0, t('Not found menu link #1-1'));
  325. $this->assertLink(t('Menu link #2'), 0, t('Found menu link #2'));
  326. }
  327. /**
  328. * Test menu_get_names().
  329. */
  330. function testMenuGetNames() {
  331. // Create three menu items.
  332. for ($i = 0; $i < 3; $i++) {
  333. $menu_link = array(
  334. 'link_title' => 'Menu link #' . $i,
  335. 'link_path' => 'menu_test/' . $i,
  336. 'module' => 'menu_test',
  337. 'menu_name' => 'menu_test_' . $i,
  338. );
  339. menu_link_save($menu_link);
  340. }
  341. drupal_static_reset('menu_get_names');
  342. // Verify that the menu names are correctly reported by menu_get_names().
  343. $menu_names = menu_get_names();
  344. $this->pass(implode(' | ', $menu_names));
  345. for ($i = 0; $i < 3; $i++) {
  346. $this->assertTrue(in_array('menu_test_' . $i, $menu_names), t('Expected menu name %expected is returned.', array('%expected' => 'menu_test_' . $i)));
  347. }
  348. }
  349. /**
  350. * Tests for menu_name parameter for hook_menu().
  351. */
  352. function testMenuName() {
  353. $admin_user = $this->drupalCreateUser(array('administer site configuration'));
  354. $this->drupalLogin($admin_user);
  355. $sql = "SELECT menu_name FROM {menu_links} WHERE router_path = 'menu_name_test'";
  356. $name = db_query($sql)->fetchField();
  357. $this->assertEqual($name, 'original', t('Menu name is "original".'));
  358. // Change the menu_name parameter in menu_test.module, then force a menu
  359. // rebuild.
  360. menu_test_menu_name('changed');
  361. menu_rebuild();
  362. $sql = "SELECT menu_name FROM {menu_links} WHERE router_path = 'menu_name_test'";
  363. $name = db_query($sql)->fetchField();
  364. $this->assertEqual($name, 'changed', t('Menu name was successfully changed after rebuild.'));
  365. }
  366. /**
  367. * Tests for menu hierarchy.
  368. */
  369. function testMenuHierarchy() {
  370. $parent_link = db_query('SELECT * FROM {menu_links} WHERE link_path = :link_path', array(':link_path' => 'menu-test/hierarchy/parent'))->fetchAssoc();
  371. $child_link = db_query('SELECT * FROM {menu_links} WHERE link_path = :link_path', array(':link_path' => 'menu-test/hierarchy/parent/child'))->fetchAssoc();
  372. $unattached_child_link = db_query('SELECT * FROM {menu_links} WHERE link_path = :link_path', array(':link_path' => 'menu-test/hierarchy/parent/child2/child'))->fetchAssoc();
  373. $this->assertEqual($child_link['plid'], $parent_link['mlid'], t('The parent of a directly attached child is correct.'));
  374. $this->assertEqual($unattached_child_link['plid'], $parent_link['mlid'], t('The parent of a non-directly attached child is correct.'));
  375. }
  376. /**
  377. * Tests menu link depth and parents of local tasks and menu callbacks.
  378. */
  379. function testMenuHidden() {
  380. // Verify links for one dynamic argument.
  381. $links = db_select('menu_links', 'ml')
  382. ->fields('ml')
  383. ->condition('ml.router_path', 'menu-test/hidden/menu%', 'LIKE')
  384. ->orderBy('ml.router_path')
  385. ->execute()
  386. ->fetchAllAssoc('router_path', PDO::FETCH_ASSOC);
  387. $parent = $links['menu-test/hidden/menu'];
  388. $depth = $parent['depth'] + 1;
  389. $plid = $parent['mlid'];
  390. $link = $links['menu-test/hidden/menu/list'];
  391. $this->assertEqual($link['depth'], $depth, t('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
  392. $this->assertEqual($link['plid'], $plid, t('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));
  393. $link = $links['menu-test/hidden/menu/add'];
  394. $this->assertEqual($link['depth'], $depth, t('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
  395. $this->assertEqual($link['plid'], $plid, t('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));
  396. $link = $links['menu-test/hidden/menu/settings'];
  397. $this->assertEqual($link['depth'], $depth, t('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
  398. $this->assertEqual($link['plid'], $plid, t('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));
  399. $link = $links['menu-test/hidden/menu/manage/%'];
  400. $this->assertEqual($link['depth'], $depth, t('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
  401. $this->assertEqual($link['plid'], $plid, t('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));
  402. $parent = $links['menu-test/hidden/menu/manage/%'];
  403. $depth = $parent['depth'] + 1;
  404. $plid = $parent['mlid'];
  405. $link = $links['menu-test/hidden/menu/manage/%/list'];
  406. $this->assertEqual($link['depth'], $depth, t('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
  407. $this->assertEqual($link['plid'], $plid, t('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));
  408. $link = $links['menu-test/hidden/menu/manage/%/add'];
  409. $this->assertEqual($link['depth'], $depth, t('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
  410. $this->assertEqual($link['plid'], $plid, t('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));
  411. $link = $links['menu-test/hidden/menu/manage/%/edit'];
  412. $this->assertEqual($link['depth'], $depth, t('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
  413. $this->assertEqual($link['plid'], $plid, t('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));
  414. $link = $links['menu-test/hidden/menu/manage/%/delete'];
  415. $this->assertEqual($link['depth'], $depth, t('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
  416. $this->assertEqual($link['plid'], $plid, t('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));
  417. // Verify links for two dynamic arguments.
  418. $links = db_select('menu_links', 'ml')
  419. ->fields('ml')
  420. ->condition('ml.router_path', 'menu-test/hidden/block%', 'LIKE')
  421. ->orderBy('ml.router_path')
  422. ->execute()
  423. ->fetchAllAssoc('router_path', PDO::FETCH_ASSOC);
  424. $parent = $links['menu-test/hidden/block'];
  425. $depth = $parent['depth'] + 1;
  426. $plid = $parent['mlid'];
  427. $link = $links['menu-test/hidden/block/list'];
  428. $this->assertEqual($link['depth'], $depth, t('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
  429. $this->assertEqual($link['plid'], $plid, t('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));
  430. $link = $links['menu-test/hidden/block/add'];
  431. $this->assertEqual($link['depth'], $depth, t('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
  432. $this->assertEqual($link['plid'], $plid, t('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));
  433. $link = $links['menu-test/hidden/block/manage/%/%'];
  434. $this->assertEqual($link['depth'], $depth, t('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
  435. $this->assertEqual($link['plid'], $plid, t('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));
  436. $parent = $links['menu-test/hidden/block/manage/%/%'];
  437. $depth = $parent['depth'] + 1;
  438. $plid = $parent['mlid'];
  439. $link = $links['menu-test/hidden/block/manage/%/%/configure'];
  440. $this->assertEqual($link['depth'], $depth, t('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
  441. $this->assertEqual($link['plid'], $plid, t('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));
  442. $link = $links['menu-test/hidden/block/manage/%/%/delete'];
  443. $this->assertEqual($link['depth'], $depth, t('%path depth @link_depth is equal to @depth.', array('%path' => $link['router_path'], '@link_depth' => $link['depth'], '@depth' => $depth)));
  444. $this->assertEqual($link['plid'], $plid, t('%path plid @link_plid is equal to @plid.', array('%path' => $link['router_path'], '@link_plid' => $link['plid'], '@plid' => $plid)));
  445. }
  446. /**
  447. * Test menu_get_item() with empty ancestors.
  448. */
  449. function testMenuGetItemNoAncestors() {
  450. variable_set('menu_masks', array());
  451. $this->drupalGet('');
  452. }
  453. /**
  454. * Test menu_set_item().
  455. */
  456. function testMenuSetItem() {
  457. $item = menu_get_item('node');
  458. $this->assertEqual($item['path'], 'node', t("Path from menu_get_item('node') is equal to 'node'"), 'menu');
  459. // Modify the path for the item then save it.
  460. $item['path'] = 'node_test';
  461. $item['href'] = 'node_test';
  462. menu_set_item('node', $item);
  463. $compare_item = menu_get_item('node');
  464. $this->assertEqual($compare_item, $item, t('Modified menu item is equal to newly retrieved menu item.'), 'menu');
  465. }
  466. /**
  467. * Test menu maintenance hooks.
  468. */
  469. function testMenuItemHooks() {
  470. // Create an item.
  471. menu_link_maintain('menu_test', 'insert', 'menu_test_maintain/4', 'Menu link #4');
  472. $this->assertEqual(menu_test_static_variable(), 'insert', t('hook_menu_link_insert() fired correctly'));
  473. // Update the item.
  474. menu_link_maintain('menu_test', 'update', 'menu_test_maintain/4', 'Menu link updated');
  475. $this->assertEqual(menu_test_static_variable(), 'update', t('hook_menu_link_update() fired correctly'));
  476. // Delete the item.
  477. menu_link_maintain('menu_test', 'delete', 'menu_test_maintain/4', '');
  478. $this->assertEqual(menu_test_static_variable(), 'delete', t('hook_menu_link_delete() fired correctly'));
  479. }
  480. /**
  481. * Test menu link 'options' storage and rendering.
  482. */
  483. function testMenuLinkOptions() {
  484. // Create a menu link with options.
  485. $menu_link = array(
  486. 'link_title' => 'Menu link options test',
  487. 'link_path' => 'node',
  488. 'module' => 'menu_test',
  489. 'options' => array(
  490. 'attributes' => array(
  491. 'title' => 'Test title attribute',
  492. ),
  493. 'query' => array(
  494. 'testparam' => 'testvalue',
  495. ),
  496. ),
  497. );
  498. menu_link_save($menu_link);
  499. // Load front page.
  500. $this->drupalGet('node');
  501. $this->assertRaw('title="Test title attribute"', t('Title attribute of a menu link renders.'));
  502. $this->assertRaw('testparam=testvalue', t('Query parameter added to menu link.'));
  503. }
  504. /**
  505. * Tests the possible ways to set the title for menu items.
  506. * Also tests that menu item titles work with string overrides.
  507. */
  508. function testMenuItemTitlesCases() {
  509. // Build array with string overrides.
  510. $test_data = array(
  511. 1 => array('Example title - Case 1' => 'Alternative example title - Case 1'),
  512. 2 => array('Example @sub1 - Case @op2' => 'Alternative example @sub1 - Case @op2'),
  513. 3 => array('Example title' => 'Alternative example title'),
  514. 4 => array('Example title' => 'Alternative example title'),
  515. );
  516. foreach ($test_data as $case_no => $override) {
  517. $this->menuItemTitlesCasesHelper($case_no);
  518. variable_set('locale_custom_strings_en', array('' => $override));
  519. $this->menuItemTitlesCasesHelper($case_no, TRUE);
  520. variable_set('locale_custom_strings_en', array());
  521. }
  522. }
  523. /**
  524. * Get a URL and assert the title given a case number. If override is true,
  525. * the title is asserted to begin with "Alternative".
  526. */
  527. private function menuItemTitlesCasesHelper($case_no, $override = FALSE) {
  528. $this->drupalGet('menu-title-test/case' . $case_no);
  529. $this->assertResponse(200);
  530. $asserted_title = $override ? 'Alternative example title - Case ' . $case_no : 'Example title - Case ' . $case_no;
  531. $this->assertTitle($asserted_title . ' | Drupal', t('Menu title is') . ': ' . $asserted_title, 'Menu');
  532. }
  533. /**
  534. * Load the router for a given path.
  535. */
  536. protected function menuLoadRouter($router_path) {
  537. return db_query('SELECT * FROM {menu_router} WHERE path = :path', array(':path' => $router_path))->fetchAssoc();
  538. }
  539. /**
  540. * Tests inheritance of 'load arguments'.
  541. */
  542. function testMenuLoadArgumentsInheritance() {
  543. $expected = array(
  544. 'menu-test/arguments/%/%' => array(
  545. 2 => array('menu_test_argument_load' => array(3)),
  546. 3 => NULL,
  547. ),
  548. // Arguments are inherited to normal children.
  549. 'menu-test/arguments/%/%/default' => array(
  550. 2 => array('menu_test_argument_load' => array(3)),
  551. 3 => NULL,
  552. ),
  553. // Arguments are inherited to tab children.
  554. 'menu-test/arguments/%/%/task' => array(
  555. 2 => array('menu_test_argument_load' => array(3)),
  556. 3 => NULL,
  557. ),
  558. // Arguments are only inherited to the same loader functions.
  559. 'menu-test/arguments/%/%/common-loader' => array(
  560. 2 => array('menu_test_argument_load' => array(3)),
  561. 3 => 'menu_test_other_argument_load',
  562. ),
  563. // Arguments are not inherited to children not using the same loader
  564. // function.
  565. 'menu-test/arguments/%/%/different-loaders-1' => array(
  566. 2 => NULL,
  567. 3 => 'menu_test_argument_load',
  568. ),
  569. 'menu-test/arguments/%/%/different-loaders-2' => array(
  570. 2 => 'menu_test_other_argument_load',
  571. 3 => NULL,
  572. ),
  573. 'menu-test/arguments/%/%/different-loaders-3' => array(
  574. 2 => NULL,
  575. 3 => NULL,
  576. ),
  577. // Explicit loader arguments should not be overriden by parent.
  578. 'menu-test/arguments/%/%/explicit-arguments' => array(
  579. 2 => array('menu_test_argument_load' => array()),
  580. 3 => NULL,
  581. ),
  582. );
  583. foreach ($expected as $router_path => $load_functions) {
  584. $router_item = $this->menuLoadRouter($router_path);
  585. $this->assertIdentical(unserialize($router_item['load_functions']), $load_functions, t('Expected load functions for router %router_path' , array('%router_path' => $router_path)));
  586. }
  587. }
  588. }
  589. /**
  590. * Tests for menu links.
  591. */
  592. class MenuLinksUnitTestCase extends DrupalWebTestCase {
  593. // Use the lightweight testing profile for this test.
  594. protected $profile = 'testing';
  595. public static function getInfo() {
  596. return array(
  597. 'name' => 'Menu links',
  598. 'description' => 'Test handling of menu links hierarchies.',
  599. 'group' => 'Menu',
  600. );
  601. }
  602. /**
  603. * Create a simple hierarchy of links.
  604. */
  605. function createLinkHierarchy($module = 'menu_test') {
  606. // First remove all the menu links.
  607. db_truncate('menu_links')->execute();
  608. // Then create a simple link hierarchy:
  609. // - $parent
  610. // - $child-1
  611. // - $child-1-1
  612. // - $child-1-2
  613. // - $child-2
  614. $base_options = array(
  615. 'link_title' => 'Menu link test',
  616. 'module' => $module,
  617. 'menu_name' => 'menu_test',
  618. );
  619. $links['parent'] = $base_options + array(
  620. 'link_path' => 'menu-test/parent',
  621. );
  622. menu_link_save($links['parent']);
  623. $links['child-1'] = $base_options + array(
  624. 'link_path' => 'menu-test/parent/child-1',
  625. 'plid' => $links['parent']['mlid'],
  626. );
  627. menu_link_save($links['child-1']);
  628. $links['child-1-1'] = $base_options + array(
  629. 'link_path' => 'menu-test/parent/child-1/child-1-1',
  630. 'plid' => $links['child-1']['mlid'],
  631. );
  632. menu_link_save($links['child-1-1']);
  633. $links['child-1-2'] = $base_options + array(
  634. 'link_path' => 'menu-test/parent/child-1/child-1-2',
  635. 'plid' => $links['child-1']['mlid'],
  636. );
  637. menu_link_save($links['child-1-2']);
  638. $links['child-2'] = $base_options + array(
  639. 'link_path' => 'menu-test/parent/child-2',
  640. 'plid' => $links['parent']['mlid'],
  641. );
  642. menu_link_save($links['child-2']);
  643. return $links;
  644. }
  645. /**
  646. * Assert that at set of links is properly parented.
  647. */
  648. function assertMenuLinkParents($links, $expected_hierarchy) {
  649. foreach ($expected_hierarchy as $child => $parent) {
  650. $mlid = $links[$child]['mlid'];
  651. $plid = $parent ? $links[$parent]['mlid'] : 0;
  652. $menu_link = menu_link_load($mlid);
  653. menu_link_save($menu_link);
  654. $this->assertEqual($menu_link['plid'], $plid, t('Menu link %mlid has parent of %plid, expected %expected_plid.', array('%mlid' => $mlid, '%plid' => $menu_link['plid'], '%expected_plid' => $plid)));
  655. }
  656. }
  657. /**
  658. * Test automatic reparenting of menu links.
  659. */
  660. function testMenuLinkReparenting($module = 'menu_test') {
  661. // Check the initial hierarchy.
  662. $links = $this->createLinkHierarchy($module);
  663. $expected_hierarchy = array(
  664. 'parent' => FALSE,
  665. 'child-1' => 'parent',
  666. 'child-1-1' => 'child-1',
  667. 'child-1-2' => 'child-1',
  668. 'child-2' => 'parent',
  669. );
  670. $this->assertMenuLinkParents($links, $expected_hierarchy);
  671. // Start over, and move child-1 under child-2, and check that all the
  672. // childs of child-1 have been moved too.
  673. $links = $this->createLinkHierarchy($module);
  674. $links['child-1']['plid'] = $links['child-2']['mlid'];
  675. menu_link_save($links['child-1']);
  676. $expected_hierarchy = array(
  677. 'parent' => FALSE,
  678. 'child-1' => 'child-2',
  679. 'child-1-1' => 'child-1',
  680. 'child-1-2' => 'child-1',
  681. 'child-2' => 'parent',
  682. );
  683. $this->assertMenuLinkParents($links, $expected_hierarchy);
  684. // Start over, and delete child-1, and check that the children of child-1
  685. // have been reassigned to the parent. menu_link_delete() will cowardly
  686. // refuse to delete a menu link defined by the system module, so skip the
  687. // test in that case.
  688. if ($module != 'system') {
  689. $links = $this->createLinkHierarchy($module);
  690. menu_link_delete($links['child-1']['mlid']);
  691. $expected_hierarchy = array(
  692. 'parent' => FALSE,
  693. 'child-1-1' => 'parent',
  694. 'child-1-2' => 'parent',
  695. 'child-2' => 'parent',
  696. );
  697. $this->assertMenuLinkParents($links, $expected_hierarchy);
  698. }
  699. // Start over, forcefully delete child-1 from the database, simulating a
  700. // database crash. Check that the children of child-1 have been reassigned
  701. // to the parent, going up on the old path hierarchy stored in each of the
  702. // links.
  703. $links = $this->createLinkHierarchy($module);
  704. // Don't do that at home.
  705. db_delete('menu_links')
  706. ->condition('mlid', $links['child-1']['mlid'])
  707. ->execute();
  708. $expected_hierarchy = array(
  709. 'parent' => FALSE,
  710. 'child-1-1' => 'parent',
  711. 'child-1-2' => 'parent',
  712. 'child-2' => 'parent',
  713. );
  714. $this->assertMenuLinkParents($links, $expected_hierarchy);
  715. // Start over, forcefully delete the parent from the database, simulating a
  716. // database crash. Check that the children of parent are now top-level.
  717. $links = $this->createLinkHierarchy($module);
  718. // Don't do that at home.
  719. db_delete('menu_links')
  720. ->condition('mlid', $links['parent']['mlid'])
  721. ->execute();
  722. $expected_hierarchy = array(
  723. 'child-1-1' => 'child-1',
  724. 'child-1-2' => 'child-1',
  725. 'child-2' => FALSE,
  726. );
  727. $this->assertMenuLinkParents($links, $expected_hierarchy);
  728. }
  729. /**
  730. * Test automatic reparenting of menu links derived from menu routers.
  731. */
  732. function testMenuLinkRouterReparenting() {
  733. // Run all the standard parenting tests on menu links derived from
  734. // menu routers.
  735. $this->testMenuLinkReparenting('system');
  736. // Additionnaly, test reparenting based on path.
  737. $links = $this->createLinkHierarchy('system');
  738. // Move child-1-2 has a child of child-2, making the link hierarchy
  739. // inconsistent with the path hierarchy.
  740. $links['child-1-2']['plid'] = $links['child-2']['mlid'];
  741. menu_link_save($links['child-1-2']);
  742. // Check the new hierarchy.
  743. $expected_hierarchy = array(
  744. 'parent' => FALSE,
  745. 'child-1' => 'parent',
  746. 'child-1-1' => 'child-1',
  747. 'child-2' => 'parent',
  748. 'child-1-2' => 'child-2',
  749. );
  750. $this->assertMenuLinkParents($links, $expected_hierarchy);
  751. // Now delete 'parent' directly from the database, simulating a database
  752. // crash. 'child-1' and 'child-2' should get moved to the
  753. // top-level.
  754. // Don't do that at home.
  755. db_delete('menu_links')
  756. ->condition('mlid', $links['parent']['mlid'])
  757. ->execute();
  758. $expected_hierarchy = array(
  759. 'child-1' => FALSE,
  760. 'child-1-1' => 'child-1',
  761. 'child-2' => FALSE,
  762. 'child-1-2' => 'child-2',
  763. );
  764. $this->assertMenuLinkParents($links, $expected_hierarchy);
  765. // Now delete 'child-2' directly from the database, simulating a database
  766. // crash. 'child-1-2' will get reparented under 'child-1' based on its
  767. // path.
  768. // Don't do that at home.
  769. db_delete('menu_links')
  770. ->condition('mlid', $links['child-2']['mlid'])
  771. ->execute();
  772. $expected_hierarchy = array(
  773. 'child-1' => FALSE,
  774. 'child-1-1' => 'child-1',
  775. 'child-1-2' => 'child-1',
  776. );
  777. $this->assertMenuLinkParents($links, $expected_hierarchy);
  778. }
  779. }
  780. /**
  781. * Tests rebuilding the menu by setting 'menu_rebuild_needed.'
  782. */
  783. class MenuRebuildTestCase extends DrupalWebTestCase {
  784. public static function getInfo() {
  785. return array(
  786. 'name' => 'Menu rebuild test',
  787. 'description' => 'Test rebuilding of menu.',
  788. 'group' => 'Menu',
  789. );
  790. }
  791. /**
  792. * Test if the 'menu_rebuild_needed' variable triggers a menu_rebuild() call.
  793. */
  794. function testMenuRebuildByVariable() {
  795. // Check if 'admin' path exists.
  796. $admin_exists = db_query('SELECT path from {menu_router} WHERE path = :path', array(':path' => 'admin'))->fetchField();
  797. $this->assertEqual($admin_exists, 'admin', t("The path 'admin/' exists prior to deleting."));
  798. // Delete the path item 'admin', and test that the path doesn't exist in the database.
  799. $delete = db_delete('menu_router')
  800. ->condition('path', 'admin')
  801. ->execute();
  802. $admin_exists = db_query('SELECT path from {menu_router} WHERE path = :path', array(':path' => 'admin'))->fetchField();
  803. $this->assertFalse($admin_exists, t("The path 'admin/' has been deleted and doesn't exist in the database."));
  804. // Now we enable the rebuild variable and trigger menu_execute_active_handler()
  805. // to rebuild the menu item. Now 'admin' should exist.
  806. variable_set('menu_rebuild_needed', TRUE);
  807. // menu_execute_active_handler() should trigger the rebuild.
  808. $this->drupalGet('<front>');
  809. $admin_exists = db_query('SELECT path from {menu_router} WHERE path = :path', array(':path' => 'admin'))->fetchField();
  810. $this->assertEqual($admin_exists, 'admin', t("The menu has been rebuilt, the path 'admin' now exists again."));
  811. }
  812. }
  813. /**
  814. * Menu tree data related tests.
  815. */
  816. class MenuTreeDataTestCase extends DrupalUnitTestCase {
  817. /**
  818. * Dummy link structure acceptable for menu_tree_data().
  819. */
  820. var $links = array(
  821. 1 => array('mlid' => 1, 'depth' => 1),
  822. 2 => array('mlid' => 2, 'depth' => 1),
  823. 3 => array('mlid' => 3, 'depth' => 2),
  824. 4 => array('mlid' => 4, 'depth' => 3),
  825. 5 => array('mlid' => 5, 'depth' => 1),
  826. );
  827. public static function getInfo() {
  828. return array(
  829. 'name' => 'Menu tree generation',
  830. 'description' => 'Tests recursive menu tree generation functions.',
  831. 'group' => 'Menu',
  832. );
  833. }
  834. /**
  835. * Validate the generation of a proper menu tree hierarchy.
  836. */
  837. function testMenuTreeData() {
  838. $tree = menu_tree_data($this->links);
  839. // Validate that parent items #1, #2, and #5 exist on the root level.
  840. $this->assertSameLink($this->links[1], $tree[1]['link'], t('Parent item #1 exists.'));
  841. $this->assertSameLink($this->links[2], $tree[2]['link'], t('Parent item #2 exists.'));
  842. $this->assertSameLink($this->links[5], $tree[5]['link'], t('Parent item #5 exists.'));
  843. // Validate that child item #4 exists at the correct location in the hierarchy.
  844. $this->assertSameLink($this->links[4], $tree[2]['below'][3]['below'][4]['link'], t('Child item #4 exists in the hierarchy.'));
  845. }
  846. /**
  847. * Check that two menu links are the same by comparing the mlid.
  848. *
  849. * @param $link1
  850. * A menu link item.
  851. * @param $link2
  852. * A menu link item.
  853. * @param $message
  854. * The message to display along with the assertion.
  855. * @return
  856. * TRUE if the assertion succeeded, FALSE otherwise.
  857. */
  858. protected function assertSameLink($link1, $link2, $message = '') {
  859. return $this->assert($link1['mlid'] == $link2['mlid'], $message ? $message : t('First link is identical to second link'));
  860. }
  861. }
  862. /**
  863. * Menu tree output related tests.
  864. */
  865. class MenuTreeOutputTestCase extends DrupalWebTestCase {
  866. /**
  867. * Dummy link structure acceptable for menu_tree_output().
  868. */
  869. var $tree_data = array(
  870. '1'=> array(
  871. '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' =>'')) ),
  872. 'below' => array(
  873. '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' =>'')) ),
  874. 'below' => array(
  875. '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' =>'')) ),
  876. 'below' => array() ),
  877. '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' =>'')) ),
  878. 'below' => array() )
  879. )
  880. )
  881. )
  882. ),
  883. '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( ) ),
  884. '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( ) ),
  885. '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( ) )
  886. );
  887. public static function getInfo() {
  888. return array(
  889. 'name' => 'Menu tree output',
  890. 'description' => 'Tests menu tree output functions.',
  891. 'group' => 'Menu',
  892. );
  893. }
  894. function setUp() {
  895. parent::setUp();
  896. }
  897. /**
  898. * Validate the generation of a proper menu tree output.
  899. */
  900. function testMenuTreeData() {
  901. $output = menu_tree_output($this->tree_data);
  902. // Validate that the - in main-menu is changed into an underscore
  903. $this->assertEqual( $output['1']['#theme'], 'menu_link__main_menu', t('Hyphen is changed to a dash on menu_link'));
  904. $this->assertEqual( $output['#theme_wrappers'][0], 'menu_tree__main_menu', t('Hyphen is changed to a dash on menu_tree wrapper'));
  905. // Looking for child items in the data
  906. $this->assertEqual( $output['1']['#below']['2']['#href'], 'a/b', t('Checking the href on a child item'));
  907. $this->assertTrue( in_array('active-trail',$output['1']['#below']['2']['#attributes']['class']) , t('Checking the active trail class'));
  908. // Validate that the hidden and no access items are missing
  909. $this->assertFalse( isset($output['5']), t('Hidden item should be missing'));
  910. $this->assertFalse( isset($output['6']), t('False access should be missing'));
  911. // Item 7 is after a couple hidden items. Just to make sure that 5 and 6 are skipped and 7 still included
  912. $this->assertTrue( isset($output['7']), t('Item after hidden items is present'));
  913. }
  914. }
  915. /**
  916. * Menu breadcrumbs related tests.
  917. */
  918. class MenuBreadcrumbTestCase extends MenuWebTestCase {
  919. public static function getInfo() {
  920. return array(
  921. 'name' => 'Breadcrumbs',
  922. 'description' => 'Tests breadcrumbs functionality.',
  923. 'group' => 'Menu',
  924. );
  925. }
  926. function setUp() {
  927. $modules = func_get_args();
  928. if (isset($modules[0]) && is_array($modules[0])) {
  929. $modules = $modules[0];
  930. }
  931. $modules[] = 'menu_test';
  932. parent::setUp($modules);
  933. $perms = array_keys(module_invoke_all('permission'));
  934. $this->admin_user = $this->drupalCreateUser($perms);
  935. $this->drupalLogin($this->admin_user);
  936. // This test puts menu links in the Navigation menu and then tests for
  937. // their presence on the page, so we need to ensure that the Navigation
  938. // block will be displayed in all active themes.
  939. db_update('block')
  940. ->fields(array(
  941. // Use a region that is valid for all themes.
  942. 'region' => 'content',
  943. 'status' => 1,
  944. ))
  945. ->condition('module', 'system')
  946. ->condition('delta', 'navigation')
  947. ->execute();
  948. }
  949. /**
  950. * Tests breadcrumbs on node and administrative paths.
  951. */
  952. function testBreadCrumbs() {
  953. // Prepare common base breadcrumb elements.
  954. $home = array('<front>' => 'Home');
  955. $admin = $home + array('admin' => t('Administration'));
  956. $config = $admin + array('admin/config' => t('Configuration'));
  957. $type = 'article';
  958. $langcode = LANGUAGE_NONE;
  959. // Verify breadcrumbs for default local tasks.
  960. $expected = array(
  961. 'menu-test' => t('Menu test root'),
  962. );
  963. $title = t('Breadcrumbs test: Local tasks');
  964. $trail = $home + $expected;
  965. $tree = $expected + array(
  966. 'menu-test/breadcrumb/tasks' => $title,
  967. );
  968. $this->assertBreadcrumb('menu-test/breadcrumb/tasks', $trail, $title, $tree);
  969. $this->assertBreadcrumb('menu-test/breadcrumb/tasks/first', $trail, $title, $tree);
  970. $this->assertBreadcrumb('menu-test/breadcrumb/tasks/first/first', $trail, $title, $tree);
  971. $trail += array(
  972. 'menu-test/breadcrumb/tasks' => t('Breadcrumbs test: Local tasks'),
  973. );
  974. $this->assertBreadcrumb('menu-test/breadcrumb/tasks/first/second', $trail, $title, $tree);
  975. $this->assertBreadcrumb('menu-test/breadcrumb/tasks/second', $trail, $title, $tree);
  976. $this->assertBreadcrumb('menu-test/breadcrumb/tasks/second/first', $trail, $title, $tree);
  977. $trail += array(
  978. 'menu-test/breadcrumb/tasks/second' => t('Second'),
  979. );
  980. $this->assertBreadcrumb('menu-test/breadcrumb/tasks/second/second', $trail, $title, $tree);
  981. // Verify Taxonomy administration breadcrumbs.
  982. $trail = $admin + array(
  983. 'admin/structure' => t('Structure'),
  984. );
  985. $this->assertBreadcrumb('admin/structure/taxonomy', $trail);
  986. $trail += array(
  987. 'admin/structure/taxonomy' => t('Taxonomy'),
  988. );
  989. $this->assertBreadcrumb('admin/structure/taxonomy/tags', $trail);
  990. $trail += array(
  991. 'admin/structure/taxonomy/tags' => t('Tags'),
  992. );
  993. $this->assertBreadcrumb('admin/structure/taxonomy/tags/edit', $trail);
  994. $this->assertBreadcrumb('admin/structure/taxonomy/tags/fields', $trail);
  995. $this->assertBreadcrumb('admin/structure/taxonomy/tags/add', $trail);
  996. // Verify Menu administration breadcrumbs.
  997. $trail = $admin + array(
  998. 'admin/structure' => t('Structure'),
  999. );
  1000. $this->assertBreadcrumb('admin/structure/menu', $trail);
  1001. $trail += array(
  1002. 'admin/structure/menu' => t('Menus'),
  1003. );
  1004. $this->assertBreadcrumb('admin/structure/menu/manage/navigation', $trail);
  1005. $trail += array(
  1006. 'admin/structure/menu/manage/navigation' => t('Navigation'),
  1007. );
  1008. $this->assertBreadcrumb("admin/structure/menu/item/6/edit", $trail);
  1009. $this->assertBreadcrumb('admin/structure/menu/manage/navigation/edit', $trail);
  1010. $this->assertBreadcrumb('admin/structure/menu/manage/navigation/add', $trail);
  1011. // Verify Node administration breadcrumbs.
  1012. $trail = $admin + array(
  1013. 'admin/structure' => t('Structure'),
  1014. 'admin/structure/types' => t('Content types'),
  1015. );
  1016. $this->assertBreadcrumb('admin/structure/types/add', $trail);
  1017. $this->assertBreadcrumb("admin/structure/types/manage/$type", $trail);
  1018. $trail += array(
  1019. "admin/structure/types/manage/$type" => t('Article'),
  1020. );
  1021. $this->assertBreadcrumb("admin/structure/types/manage/$type/fields", $trail);
  1022. $this->assertBreadcrumb("admin/structure/types/manage/$type/display", $trail);
  1023. $trail_teaser = $trail + array(
  1024. "admin/structure/types/manage/$type/display" => t('Manage display'),
  1025. );
  1026. $this->assertBreadcrumb("admin/structure/types/manage/$type/display/teaser", $trail_teaser);
  1027. $this->assertBreadcrumb("admin/structure/types/manage/$type/comment/fields", $trail);
  1028. $this->assertBreadcrumb("admin/structure/types/manage/$type/comment/display", $trail);
  1029. $this->assertBreadcrumb("admin/structure/types/manage/$type/delete", $trail);
  1030. $trail += array(
  1031. "admin/structure/types/manage/$type/fields" => t('Manage fields'),
  1032. );
  1033. $this->assertBreadcrumb("admin/structure/types/manage/$type/fields/body", $trail);
  1034. $trail += array(
  1035. "admin/structure/types/manage/$type/fields/body" => t('Body'),
  1036. );
  1037. $this->assertBreadcrumb("admin/structure/types/manage/$type/fields/body/widget-type", $trail);
  1038. // Verify Filter text format administration breadcrumbs.
  1039. $format = db_query_range("SELECT format, name FROM {filter_format}", 1, 1)->fetch();
  1040. $format_id = $format->format;
  1041. $trail = $config + array(
  1042. 'admin/config/content' => t('Content authoring'),
  1043. );
  1044. $this->assertBreadcrumb('admin/config/content/formats', $trail);
  1045. $trail += array(
  1046. 'admin/config/content/formats' => t('Text formats'),
  1047. );
  1048. $this->assertBreadcrumb('admin/config/content/formats/add', $trail);
  1049. $this->assertBreadcrumb("admin/config/content/formats/$format_id", $trail);
  1050. $trail += array(
  1051. "admin/config/content/formats/$format_id" => $format->name,
  1052. );
  1053. $this->assertBreadcrumb("admin/config/content/formats/$format_id/disable", $trail);
  1054. // Verify node breadcrumbs (without menu link).
  1055. $node1 = $this->drupalCreateNode();
  1056. $nid1 = $node1->nid;
  1057. $trail = $home;
  1058. $this->assertBreadcrumb("node/$nid1", $trail);
  1059. // Also verify that the node does not appear elsewhere (e.g., menu trees).
  1060. $this->assertNoLink($node1->title);
  1061. // The node itself should not be contained in the breadcrumb on the default
  1062. // local task, since there is no difference between both pages.
  1063. $this->assertBreadcrumb("node/$nid1/view", $trail);
  1064. // Also verify that the node does not appear elsewhere (e.g., menu trees).
  1065. $this->assertNoLink($node1->title);
  1066. $trail += array(
  1067. "node/$nid1" => $node1->title,
  1068. );
  1069. $this->assertBreadcrumb("node/$nid1/edit", $trail);
  1070. // Verify that breadcrumb on node listing page contains "Home" only.
  1071. $trail = array();
  1072. $this->assertBreadcrumb('node', $trail);
  1073. // Verify node breadcrumbs (in menu).
  1074. // Do this separately for Main menu and Navigation menu, since only the
  1075. // latter is a preferred menu by default.
  1076. // @todo Also test all themes? Manually testing led to the suspicion that
  1077. // breadcrumbs may differ, possibly due to template.php overrides.
  1078. $menus = array('main-menu', 'navigation');
  1079. // Alter node type menu settings.
  1080. variable_set("menu_options_$type", $menus);
  1081. variable_set("menu_parent_$type", 'navigation:0');
  1082. foreach ($menus as $menu) {
  1083. // Create a parent node in the current menu.
  1084. $title = $this->randomName();
  1085. $node2 = $this->drupalCreateNode(array(
  1086. 'type' => $type,
  1087. 'title' => $title,
  1088. 'menu' => array(
  1089. 'enabled' => 1,
  1090. 'link_title' => 'Parent ' . $title,
  1091. 'description' => '',
  1092. 'menu_name' => $menu,
  1093. 'plid' => 0,
  1094. ),
  1095. ));
  1096. $nid2 = $node2->nid;
  1097. $trail = $home;
  1098. $tree = array(
  1099. "node/$nid2" => $node2->menu['link_title'],
  1100. );
  1101. $this->assertBreadcrumb("node/$nid2", $trail, $node2->title, $tree);
  1102. // The node itself should not be contained in the breadcrumb on the
  1103. // default local task, since there is no difference between both pages.
  1104. $this->assertBreadcrumb("node/$nid2/view", $trail, $node2->title, $tree);
  1105. $trail += array(
  1106. "node/$nid2" => $node2->menu['link_title'],
  1107. );
  1108. $this->assertBreadcrumb("node/$nid2/edit", $trail);
  1109. // Create a child node in the current menu.
  1110. $title = $this->randomName();
  1111. $node3 = $this->drupalCreateNode(array(
  1112. 'type' => $type,
  1113. 'title' => $title,
  1114. 'menu' => array(
  1115. 'enabled' => 1,
  1116. 'link_title' => 'Child ' . $title,
  1117. 'description' => '',
  1118. 'menu_name' => $menu,
  1119. 'plid' => $node2->menu['mlid'],
  1120. ),
  1121. ));
  1122. $nid3 = $node3->nid;
  1123. $this->assertBreadcrumb("node/$nid3", $trail, $node3->title, $tree, FALSE);
  1124. // The node itself should not be contained in the breadcrumb on the
  1125. // default local task, since there is no difference between both pages.
  1126. $this->assertBreadcrumb("node/$nid3/view", $trail, $node3->title, $tree, FALSE);
  1127. $trail += array(
  1128. "node/$nid3" => $node3->menu['link_title'],
  1129. );
  1130. $tree += array(
  1131. "node/$nid3" => $node3->menu['link_title'],
  1132. );
  1133. $this->assertBreadcrumb("node/$nid3/edit", $trail);
  1134. // Verify that node listing page still contains "Home" only.
  1135. $trail = array();
  1136. $this->assertBreadcrumb('node', $trail);
  1137. if ($menu == 'navigation') {
  1138. $parent = $node2;
  1139. $child = $node3;
  1140. }
  1141. }
  1142. // Create a Navigation menu link for 'node', move the last parent node menu
  1143. // link below it, and verify a full breadcrumb for the last child node.
  1144. $menu = 'navigation';
  1145. $edit = array(
  1146. 'link_title' => 'Root',
  1147. 'link_path' => 'node',
  1148. );
  1149. $this->drupalPost("admin/structure/menu/manage/$menu/add", $edit, t('Save'));
  1150. $link = db_query('SELECT * FROM {menu_links} WHERE link_title = :title', array(':title' => 'Root'))->fetchAssoc();
  1151. $edit = array(
  1152. 'menu[parent]' => $link['menu_name'] . ':' . $link['mlid'],
  1153. );
  1154. $this->drupalPost("node/{$parent->nid}/edit", $edit, t('Save'));
  1155. $expected = array(
  1156. "node" => $link['link_title'],
  1157. );
  1158. $trail = $home + $expected;
  1159. $tree = $expected + array(
  1160. "node/{$parent->nid}" => $parent->menu['link_title'],
  1161. );
  1162. $this->assertBreadcrumb(NULL, $trail, $parent->title, $tree);
  1163. $trail += array(
  1164. "node/{$parent->nid}" => $parent->menu['link_title'],
  1165. );
  1166. $tree += array(
  1167. "node/{$child->nid}" => $child->menu['link_title'],
  1168. );
  1169. $this->assertBreadcrumb("node/{$child->nid}", $trail, $child->title, $tree);
  1170. // Add a taxonomy term/tag to last node, and add a link for that term to the
  1171. // Navigation menu.
  1172. $tags = array(
  1173. 'Drupal' => array(),
  1174. 'Breadcrumbs' => array(),
  1175. );
  1176. $edit = array(
  1177. "field_tags[$langcode]" => implode(',', array_keys($tags)),
  1178. );
  1179. $this->drupalPost("node/{$parent->nid}/edit", $edit, t('Save'));
  1180. // Put both terms into a hierarchy Drupal » Breadcrumbs. Required for both
  1181. // the menu links and the terms itself, since taxonomy_term_page() resets
  1182. // the breadcrumb based on taxonomy term hierarchy.
  1183. $parent_tid = 0;
  1184. foreach ($tags as $name => $null) {
  1185. $terms = taxonomy_term_load_multiple(NULL, array('name' => $name));
  1186. $term = reset($terms);
  1187. $tags[$name]['term'] = $term;
  1188. if ($parent_tid) {
  1189. $edit = array(
  1190. 'parent[]' => array($parent_tid),
  1191. );
  1192. $this->drupalPost("taxonomy/term/{$term->tid}/edit", $edit, t('Save'));
  1193. }
  1194. $parent_tid = $term->tid;
  1195. }
  1196. $parent_mlid = 0;
  1197. foreach ($tags as $name => $data) {
  1198. $term = $data['term'];
  1199. $edit = array(
  1200. 'link_title' => "$name link",
  1201. 'link_path' => "taxonomy/term/{$term->tid}",
  1202. 'parent' => "$menu:{$parent_mlid}",
  1203. );
  1204. $this->drupalPost("admin/structure/menu/manage/$menu/add", $edit, t('Save'));
  1205. $tags[$name]['link'] = db_query('SELECT * FROM {menu_links} WHERE link_title = :title AND link_path = :href', array(
  1206. ':title' => $edit['link_title'],
  1207. ':href' => $edit['link_path'],
  1208. ))->fetchAssoc();
  1209. $tags[$name]['link']['link_path'] = $edit['link_path'];
  1210. $parent_mlid = $tags[$name]['link']['mlid'];
  1211. }
  1212. // Verify expected breadcrumbs for menu links.
  1213. $trail = $home;
  1214. $tree = array();
  1215. foreach ($tags as $name => $data) {
  1216. $term = $data['term'];
  1217. $link = $data['link'];
  1218. $tree += array(
  1219. $link['link_path'] => $link['link_title'],
  1220. );
  1221. $this->assertBreadcrumb($link['link_path'], $trail, $term->name, $tree);
  1222. $this->assertRaw(check_plain($parent->title), 'Tagged node found.');
  1223. // Additionally make sure that this link appears only once; i.e., the
  1224. // untranslated menu links automatically generated from menu router items
  1225. // ('taxonomy/term/%') should never be translated and appear in any menu
  1226. // other than the breadcrumb trail.
  1227. $elements = $this->xpath('//div[@id=:menu]/descendant::a[@href=:href]', array(
  1228. ':menu' => 'block-system-navigation',
  1229. ':href' => url($link['link_path']),
  1230. ));
  1231. $this->assertTrue(count($elements) == 1, "Link to {$link['link_path']} appears only once.");
  1232. // Next iteration should expect this tag as parent link.
  1233. // Note: Term name, not link name, due to taxonomy_term_page().
  1234. $trail += array(
  1235. $link['link_path'] => $term->name,
  1236. );
  1237. }
  1238. // Verify breadcrumbs on user and user/%.
  1239. // We need to log back in and out below, and cannot simply grant the
  1240. // 'administer users' permission, since user_page() makes your head explode.
  1241. user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, array(
  1242. 'access user profiles',
  1243. ));
  1244. $this->drupalLogout();
  1245. // Verify breadcrumb on front page.
  1246. $this->assertBreadcrumb('<front>', array());
  1247. // Verify breadcrumb on user pages (without menu link) for anonymous user.
  1248. $trail = $home;
  1249. $this->assertBreadcrumb('user', $trail, t('User account'));
  1250. $this->assertBreadcrumb('user/' . $this->admin_user->uid, $trail, $this->admin_user->name);
  1251. // Verify breadcrumb on user pages (without menu link) for registered users.
  1252. $this->drupalLogin($this->admin_user);
  1253. $trail = $home;
  1254. $this->assertBreadcrumb('user', $trail, $this->admin_user->name);
  1255. $this->assertBreadcrumb('user/' . $this->admin_user->uid, $trail, $this->admin_user->name);
  1256. $trail += array(
  1257. 'user/' . $this->admin_user->uid => $this->admin_user->name,
  1258. );
  1259. $this->assertBreadcrumb('user/' . $this->admin_user->uid . '/edit', $trail, $this->admin_user->name);
  1260. // Create a second user to verify breadcrumb on user pages again.
  1261. $this->web_user = $this->drupalCreateUser(array(
  1262. 'administer users',
  1263. 'access user profiles',
  1264. ));
  1265. $this->drupalLogin($this->web_user);
  1266. // Verify correct breadcrumb and page title on another user's account pages
  1267. // (without menu link).
  1268. $trail = $home;
  1269. $this->assertBreadcrumb('user/' . $this->admin_user->uid, $trail, $this->admin_user->name);
  1270. $trail += array(
  1271. 'user/' . $this->admin_user->uid => $this->admin_user->name,
  1272. );
  1273. $this->assertBreadcrumb('user/' . $this->admin_user->uid . '/edit', $trail, $this->admin_user->name);
  1274. // Verify correct breadcrumb and page title when viewing own user account
  1275. // pages (without menu link).
  1276. $trail = $home;
  1277. $this->assertBreadcrumb('user/' . $this->web_user->uid, $trail, $this->web_user->name);
  1278. $trail += array(
  1279. 'user/' . $this->web_user->uid => $this->web_user->name,
  1280. );
  1281. $this->assertBreadcrumb('user/' . $this->web_user->uid . '/edit', $trail, $this->web_user->name);
  1282. // Add a Navigation menu links for 'user' and $this->admin_user.
  1283. // Although it may be faster to manage these links via low-level API
  1284. // functions, there's a lot that can go wrong in doing so.
  1285. $this->drupalLogin($this->admin_user);
  1286. $edit = array(
  1287. 'link_title' => 'User',
  1288. 'link_path' => 'user',
  1289. );
  1290. $this->drupalPost("admin/structure/menu/manage/$menu/add", $edit, t('Save'));
  1291. $link_user = db_query('SELECT * FROM {menu_links} WHERE link_title = :title AND link_path = :href', array(
  1292. ':title' => $edit['link_title'],
  1293. ':href' => $edit['link_path'],
  1294. ))->fetchAssoc();
  1295. $edit = array(
  1296. 'link_title' => $this->admin_user->name . ' link',
  1297. 'link_path' => 'user/' . $this->admin_user->uid,
  1298. );
  1299. $this->drupalPost("admin/structure/menu/manage/$menu/add", $edit, t('Save'));
  1300. $link_admin_user = db_query('SELECT * FROM {menu_links} WHERE link_title = :title AND link_path = :href', array(
  1301. ':title' => $edit['link_title'],
  1302. ':href' => $edit['link_path'],
  1303. ))->fetchAssoc();
  1304. // Verify expected breadcrumbs for the two separate links.
  1305. $this->drupalLogout();
  1306. $trail = $home;
  1307. $tree = array(
  1308. $link_user['link_path'] => $link_user['link_title'],
  1309. );
  1310. $this->assertBreadcrumb('user', $trail, $link_user['link_title'], $tree);
  1311. $tree = array(
  1312. $link_admin_user['link_path'] => $link_admin_user['link_title'],
  1313. );
  1314. $this->assertBreadcrumb('user/' . $this->admin_user->uid, $trail, $link_admin_user['link_title'], $tree);
  1315. $this->drupalLogin($this->admin_user);
  1316. $trail += array(
  1317. $link_admin_user['link_path'] => $link_admin_user['link_title'],
  1318. );
  1319. $this->assertBreadcrumb('user/' . $this->admin_user->uid . '/edit', $trail, $link_admin_user['link_title'], $tree, FALSE);
  1320. // Move 'user/%' below 'user' and verify again.
  1321. $edit = array(
  1322. 'parent' => "$menu:{$link_user['mlid']}",
  1323. );
  1324. $this->drupalPost("admin/structure/menu/item/{$link_admin_user['mlid']}/edit", $edit, t('Save'));
  1325. $this->drupalLogout();
  1326. $trail = $home;
  1327. $tree = array(
  1328. $link_user['link_path'] => $link_user['link_title'],
  1329. );
  1330. $this->assertBreadcrumb('user', $trail, $link_user['link_title'], $tree);
  1331. $trail += array(
  1332. $link_user['link_path'] => $link_user['link_title'],
  1333. );
  1334. $tree += array(
  1335. $link_admin_user['link_path'] => $link_admin_user['link_title'],
  1336. );
  1337. $this->assertBreadcrumb('user/' . $this->admin_user->uid, $trail, $link_admin_user['link_title'], $tree);
  1338. $this->drupalLogin($this->admin_user);
  1339. $trail += array(
  1340. $link_admin_user['link_path'] => $link_admin_user['link_title'],
  1341. );
  1342. $this->assertBreadcrumb('user/' . $this->admin_user->uid . '/edit', $trail, $link_admin_user['link_title'], $tree, FALSE);
  1343. // Create an only slightly privileged user being able to access site reports
  1344. // but not administration pages.
  1345. $this->web_user = $this->drupalCreateUser(array(
  1346. 'access site reports',
  1347. ));
  1348. $this->drupalLogin($this->web_user);
  1349. // Verify that we can access recent log entries, there is a corresponding
  1350. // page title, and that the breadcrumb is empty (because the user is not
  1351. // able to access "Administer", so the trail cannot recurse into it).
  1352. $trail = array();
  1353. $this->assertBreadcrumb('admin', $trail, t('Access denied'));
  1354. $this->assertResponse(403);
  1355. $trail = $home;
  1356. $this->assertBreadcrumb('admin/reports', $trail, t('Reports'));
  1357. $this->assertNoResponse(403);
  1358. $this->assertBreadcrumb('admin/reports/dblog', $trail, t('Recent log messages'));
  1359. $this->assertNoResponse(403);
  1360. }
  1361. }
  1362. /**
  1363. * Tests active menu trails.
  1364. */
  1365. class MenuTrailTestCase extends MenuWebTestCase {
  1366. public static function getInfo() {
  1367. return array(
  1368. 'name' => 'Active trail',
  1369. 'description' => 'Tests active menu trails and alteration functionality.',
  1370. 'group' => 'Menu',
  1371. );
  1372. }
  1373. function setUp() {
  1374. $modules = func_get_args();
  1375. if (isset($modules[0]) && is_array($modules[0])) {
  1376. $modules = $modules[0];
  1377. }
  1378. $modules[] = 'menu_test';
  1379. parent::setUp($modules);
  1380. $this->admin_user = $this->drupalCreateUser(array('administer site configuration', 'access administration pages'));
  1381. $this->drupalLogin($this->admin_user);
  1382. // This test puts menu links in the Navigation menu and then tests for
  1383. // their presence on the page, so we need to ensure that the Navigation
  1384. // block will be displayed in all active themes.
  1385. db_update('block')
  1386. ->fields(array(
  1387. // Use a region that is valid for all themes.
  1388. 'region' => 'content',
  1389. 'status' => 1,
  1390. ))
  1391. ->condition('module', 'system')
  1392. ->condition('delta', 'navigation')
  1393. ->execute();
  1394. // This test puts menu links in the Management menu and then tests for
  1395. // their presence on the page, so we need to ensure that the Management
  1396. // block will be displayed in all active themes.
  1397. db_update('block')
  1398. ->fields(array(
  1399. // Use a region that is valid for all themes.
  1400. 'region' => 'content',
  1401. 'status' => 1,
  1402. ))
  1403. ->condition('module', 'system')
  1404. ->condition('delta', 'management')
  1405. ->execute();
  1406. }
  1407. /**
  1408. * Tests active trails are properly affected by menu_tree_set_path().
  1409. */
  1410. function testMenuTreeSetPath() {
  1411. $home = array('<front>' => 'Home');
  1412. $config_tree = array(
  1413. 'admin' => t('Administration'),
  1414. 'admin/config' => t('Configuration'),
  1415. );
  1416. $config = $home + $config_tree;
  1417. // The menu_test_menu_tree_set_path system variable controls whether or not
  1418. // the menu_test_menu_trail_callback() callback (used by all paths in these
  1419. // tests) issues an overriding call to menu_trail_set_path().
  1420. $test_menu_path = array(
  1421. 'menu_name' => 'management',
  1422. 'path' => 'admin/config/system/site-information',
  1423. );
  1424. $breadcrumb = $home + array(
  1425. 'menu-test' => t('Menu test root'),
  1426. );
  1427. $tree = array(
  1428. 'menu-test' => t('Menu test root'),
  1429. 'menu-test/menu-trail' => t('Menu trail - Case 1'),
  1430. );
  1431. // Test the tree generation for the Navigation menu.
  1432. variable_del('menu_test_menu_tree_set_path');
  1433. $this->assertBreadcrumb('menu-test/menu-trail', $breadcrumb, t('Menu trail - Case 1'), $tree);
  1434. // Override the active trail for the Management tree; it should not affect
  1435. // the Navigation tree.
  1436. variable_set('menu_test_menu_tree_set_path', $test_menu_path);
  1437. $this->assertBreadcrumb('menu-test/menu-trail', $breadcrumb, t('Menu trail - Case 1'), $tree);
  1438. $breadcrumb = $config + array(
  1439. 'admin/config/development' => t('Development'),
  1440. );
  1441. $tree = $config_tree + array(
  1442. 'admin/config/development' => t('Development'),
  1443. 'admin/config/development/menu-trail' => t('Menu trail - Case 2'),
  1444. );
  1445. $override_breadcrumb = $config + array(
  1446. 'admin/config/system' => t('System'),
  1447. 'admin/config/system/site-information' => t('Site information'),
  1448. );
  1449. $override_tree = $config_tree + array(
  1450. 'admin/config/system' => t('System'),
  1451. 'admin/config/system/site-information' => t('Site information'),
  1452. );
  1453. // Test the tree generation for the Management menu.
  1454. variable_del('menu_test_menu_tree_set_path');
  1455. $this->assertBreadcrumb('admin/config/development/menu-trail', $breadcrumb, t('Menu trail - Case 2'), $tree);
  1456. // Override the active trail for the Management tree; it should affect the
  1457. // breadcrumbs and Management tree.
  1458. variable_set('menu_test_menu_tree_set_path', $test_menu_path);
  1459. $this->assertBreadcrumb('admin/config/development/menu-trail', $override_breadcrumb, t('Menu trail - Case 2'), $override_tree);
  1460. }
  1461. /**
  1462. * Tests that the active trail works correctly on custom 403 and 404 pages.
  1463. */
  1464. function testCustom403And404Pages() {
  1465. // Set the custom 403 and 404 pages we will use.
  1466. variable_set('site_403', 'menu-test/custom-403-page');
  1467. variable_set('site_404', 'menu-test/custom-404-page');
  1468. // Define the paths we'll visit to trigger 403 and 404 responses during
  1469. // this test, and the expected active trail for each case.
  1470. $paths = array(
  1471. 403 => 'admin/config',
  1472. 404 => $this->randomName(),
  1473. );
  1474. // For the 403 page, the initial trail during the Drupal bootstrap should
  1475. // include the page that the user is trying to visit, while the final trail
  1476. // should reflect the custom 403 page that the user was redirected to.
  1477. $expected_trail[403]['initial'] = array(
  1478. '<front>' => 'Home',
  1479. 'admin/config' => 'Configuration',
  1480. );
  1481. $expected_trail[403]['final'] = array(
  1482. '<front>' => 'Home',
  1483. 'menu-test' => 'Menu test root',
  1484. 'menu-test/custom-403-page' => 'Custom 403 page',
  1485. );
  1486. // For the 404 page, the initial trail during the Drupal bootstrap should
  1487. // only contain the link back to "Home" (since the page the user is trying
  1488. // to visit doesn't have any menu items associated with it), while the
  1489. // final trail should reflect the custom 404 page that the user was
  1490. // redirected to.
  1491. $expected_trail[404]['initial'] = array(
  1492. '<front>' => 'Home',
  1493. );
  1494. $expected_trail[404]['final'] = array(
  1495. '<front>' => 'Home',
  1496. 'menu-test' => 'Menu test root',
  1497. 'menu-test/custom-404-page' => 'Custom 404 page',
  1498. );
  1499. // Visit each path as an anonymous user so that we will actually get a 403
  1500. // on admin/config.
  1501. $this->drupalLogout();
  1502. foreach (array(403, 404) as $status_code) {
  1503. // Before visiting the page, trigger the code in the menu_test module
  1504. // that will record the active trail (so we can check it in this test).
  1505. variable_set('menu_test_record_active_trail', TRUE);
  1506. $this->drupalGet($paths[$status_code]);
  1507. $this->assertResponse($status_code);
  1508. // Check that the initial trail (during the Drupal bootstrap) matches
  1509. // what we expect.
  1510. $initial_trail = variable_get('menu_test_active_trail_initial', array());
  1511. $this->assertEqual(count($initial_trail), count($expected_trail[$status_code]['initial']), t('The initial active trail for a @status_code page contains the expected number of items (expected: @expected, found: @found).', array(
  1512. '@status_code' => $status_code,
  1513. '@expected' => count($expected_trail[$status_code]['initial']),
  1514. '@found' => count($initial_trail),
  1515. )));
  1516. foreach (array_keys($expected_trail[$status_code]['initial']) as $index => $path) {
  1517. $this->assertEqual($initial_trail[$index]['href'], $path, t('Element number @number of the initial active trail for a @status_code page contains the correct path (expected: @expected, found: @found)', array(
  1518. '@number' => $index + 1,
  1519. '@status_code' => $status_code,
  1520. '@expected' => $path,
  1521. '@found' => $initial_trail[$index]['href'],
  1522. )));
  1523. }
  1524. // Check that the final trail (after the user has been redirected to the
  1525. // custom 403/404 page) matches what we expect.
  1526. $final_trail = variable_get('menu_test_active_trail_final', array());
  1527. $this->assertEqual(count($final_trail), count($expected_trail[$status_code]['final']), t('The final active trail for a @status_code page contains the expected number of items (expected: @expected, found: @found).', array(
  1528. '@status_code' => $status_code,
  1529. '@expected' => count($expected_trail[$status_code]['final']),
  1530. '@found' => count($final_trail),
  1531. )));
  1532. foreach (array_keys($expected_trail[$status_code]['final']) as $index => $path) {
  1533. $this->assertEqual($final_trail[$index]['href'], $path, t('Element number @number of the final active trail for a @status_code page contains the correct path (expected: @expected, found: @found)', array(
  1534. '@number' => $index + 1,
  1535. '@status_code' => $status_code,
  1536. '@expected' => $path,
  1537. '@found' => $final_trail[$index]['href'],
  1538. )));
  1539. }
  1540. // Check that the breadcrumb displayed on the final custom 403/404 page
  1541. // matches what we expect. (The last item of the active trail represents
  1542. // the current page, which is not supposed to appear in the breadcrumb,
  1543. // so we need to remove it from the array before checking.)
  1544. array_pop($expected_trail[$status_code]['final']);
  1545. $this->assertBreadcrumb(NULL, $expected_trail[$status_code]['final']);
  1546. }
  1547. }
  1548. }