menu_attributes.test 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. <?php
  2. /**
  3. * @file
  4. * Functionality tests for Menu attributes.
  5. *
  6. * @ingroup menu_attributes
  7. */
  8. /**
  9. * Helper test class with some added functions for testing.
  10. */
  11. class MenuAttributesTestHelper extends DrupalWebTestCase {
  12. protected $admin_user;
  13. protected $menu_attributes_new;
  14. protected $menu_attributes_edit;
  15. function setUp(array $modules = array()) {
  16. $modules[] = 'menu';
  17. $modules[] = 'menu_attributes';
  18. parent::setUp($modules);
  19. // Create and login user.
  20. $this->admin_user = $this->drupalCreateUser(array(
  21. 'access administration pages',
  22. 'administer content types',
  23. 'administer menu',
  24. 'create page content',
  25. 'edit any page content',
  26. 'delete any page content',
  27. ));
  28. $this->menu_attributes_new = array(
  29. 'title' => $this->randomName(10),
  30. 'id' => $this->randomName(10),
  31. 'name' => $this->randomName(10),
  32. 'rel' => $this->randomName(10),
  33. 'class' => $this->randomName(10),
  34. 'style' => $this->randomName(10),
  35. 'target' => '_top',
  36. 'accesskey' => $this->randomName(1),
  37. );
  38. $this->menu_attributes_edit = array(
  39. 'title' => $this->randomName(10),
  40. 'id' => $this->randomName(10),
  41. 'name' => $this->randomName(10),
  42. 'rel' => $this->randomName(10),
  43. 'class' => $this->randomName(10),
  44. 'style' => $this->randomName(10),
  45. 'target' => '_self',
  46. 'accesskey' => $this->randomName(1),
  47. );
  48. }
  49. /**
  50. * Add or edit a menu link using the menu module UI.
  51. *
  52. * @param integer $plid Parent menu link id.
  53. * @param string $link Link path.
  54. * @param string $menu_name Menu name.
  55. *
  56. * @return array Menu link created.
  57. */
  58. function crudMenuLink($mlid = 0, $plid = 0, $link = '<front>', $menu_name = 'navigation') {
  59. // View add/edit menu link page.
  60. if (empty($mlid)) {
  61. $this->drupalGet("admin/structure/menu/manage/$menu_name/add");
  62. $menu_attributes = $this->menu_attributes_new;
  63. }
  64. else {
  65. $this->drupalGet("admin/structure/menu/item/$mlid/edit");
  66. $menu_attributes = $this->menu_attributes_edit;
  67. }
  68. $this->assertResponse(200);
  69. $title = '!link_' . $this->randomName(16);
  70. $edit = array(
  71. 'link_path' => $link,
  72. 'link_title' => $title,
  73. 'enabled' => TRUE, // Use this to disable the menu and test.
  74. 'expanded' => TRUE, // Setting this to true should test whether it works when we do the std_user tests.
  75. 'parent' => $menu_name . ':' . $plid,
  76. 'weight' => '0',
  77. 'options[attributes][title]' => $menu_attributes['title'],
  78. 'options[attributes][id]' => $menu_attributes['id'],
  79. 'options[attributes][name]' => $menu_attributes['name'],
  80. 'options[attributes][rel]' => $menu_attributes['rel'],
  81. 'options[attributes][class]' => $menu_attributes['class'],
  82. 'options[attributes][style]' => $menu_attributes['style'],
  83. 'options[attributes][target]' => $menu_attributes['target'],
  84. 'options[attributes][accesskey]' => $menu_attributes['accesskey'],
  85. );
  86. // Add menu link.
  87. $this->drupalPost(NULL, $edit, t('Save'));
  88. $item = db_query('SELECT * FROM {menu_links} WHERE link_title = :title', array(':title' => $title))->fetchAssoc();
  89. return $item;
  90. }
  91. function assertMenuAttributes($form_parent, $action = 'new') {
  92. if ($action == 'new') {
  93. foreach ($this->menu_attributes_new as $attribute => $value) {
  94. $this->assertFieldByName($form_parent . '[' . $attribute . ']', $value, t("'$attribute' attribute correct in edit form."));
  95. }
  96. }
  97. else {
  98. foreach ($this->menu_attributes_edit as $attribute => $value) {
  99. $this->assertFieldByName($form_parent . '[' . $attribute . ']', $value, t("New '$attribute' attribute correct in edit form."));
  100. }
  101. }
  102. }
  103. }
  104. /**
  105. * Test basic functionality.
  106. */
  107. class MenuAttributesTestCase extends MenuAttributesTestHelper {
  108. public static function getInfo() {
  109. return array(
  110. 'name' => 'Menu attributes',
  111. 'description' => 'Tests menu attributes functionality.',
  112. 'group' => 'Menu',
  113. );
  114. }
  115. function setUp(array $modules = array()) {
  116. parent::setUp($modules);
  117. }
  118. /**
  119. * Tests menu attributes functionality.
  120. */
  121. function testMenuAttributes() {
  122. // Login the user.
  123. $this->drupalLogin($this->admin_user);
  124. $menu_name = 'navigation';
  125. // Add a node to be used as a link for menu links.
  126. $node = $this->drupalCreateNode(array('type' => 'page'));
  127. // Add a menu link.
  128. $item = $this->crudMenuLink(0, 0, 'node/' . $node->nid, $menu_name);
  129. $this->drupalGet('admin/structure/menu/item/' . $item['mlid'] . '/edit');
  130. $this->assertMenuAttributes('options[attributes]', 'new');
  131. // Edit the previously created menu link.
  132. $item = $this->crudMenuLink($item['mlid'], 0, 'node/' . $node->nid, $menu_name);
  133. $this->drupalGet('admin/structure/menu/item/' . $item['mlid'] . '/edit');
  134. $this->assertMenuAttributes('options[attributes]', 'edit');
  135. }
  136. }
  137. /**
  138. * Test menu attributes settings for nodes.
  139. */
  140. class MenuAttributesNodeTestCase extends MenuAttributesTestHelper {
  141. public static function getInfo() {
  142. return array(
  143. 'name' => 'Menu attributes settings for nodes',
  144. 'description' => 'Add, edit, and delete a node with menu link.',
  145. 'group' => 'Menu',
  146. );
  147. }
  148. function setUp(array $modules = array()) {
  149. parent::setUp($modules);
  150. $this->drupalLogin($this->admin_user);
  151. }
  152. /**
  153. * Test creating, editing, deleting menu links via node form widget.
  154. */
  155. function testMenuNodeFormWidget() {
  156. // Enable Navigation menu as available menu.
  157. $edit = array(
  158. 'menu_options[navigation]' => 1,
  159. );
  160. $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
  161. // Change default parent item to Navigation menu, so we can assert more
  162. // easily.
  163. $edit = array(
  164. 'menu_parent' => 'navigation:0',
  165. );
  166. $this->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
  167. // Create a node.
  168. $node_title = $this->randomName();
  169. $language = LANGUAGE_NONE;
  170. $edit = array(
  171. "title" => $node_title,
  172. "body[$language][0][value]" => $this->randomString(),
  173. );
  174. $this->drupalPost('node/add/page', $edit, t('Save'));
  175. $node = $this->drupalGetNodeByTitle($node_title);
  176. // Assert that there is no link for the node.
  177. $this->drupalGet('');
  178. $this->assertNoLink($node_title);
  179. // Edit the node, enable the menu link setting, but skip the link title.
  180. $edit = array(
  181. 'menu[enabled]' => 1,
  182. );
  183. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  184. // Assert that there is no link for the node.
  185. $this->drupalGet('');
  186. $this->assertNoLink($node_title);
  187. // Edit the node and create a menu link with attributes.
  188. $edit = array(
  189. 'menu[enabled]' => 1,
  190. 'menu[link_title]' => $node_title,
  191. 'menu[weight]' => 17,
  192. 'menu[options][attributes][title]' => $this->menu_attributes_new['title'],
  193. 'menu[options][attributes][id]' => $this->menu_attributes_new['id'],
  194. 'menu[options][attributes][name]' => $this->menu_attributes_new['name'],
  195. 'menu[options][attributes][rel]' => $this->menu_attributes_new['rel'],
  196. 'menu[options][attributes][class]' => $this->menu_attributes_new['class'],
  197. 'menu[options][attributes][style]' => $this->menu_attributes_new['style'],
  198. 'menu[options][attributes][target]' => $this->menu_attributes_new['target'],
  199. 'menu[options][attributes][accesskey]' => $this->menu_attributes_new['accesskey'],
  200. );
  201. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  202. // Assert that the link exists.
  203. $this->drupalGet('');
  204. $this->assertLink($node_title);
  205. // Assert that the link attributes exist.
  206. $this->drupalGet('node/' . $node->nid . '/edit');
  207. $this->assertMenuAttributes('menu[options][attributes]', 'new');
  208. // Edit the node again and change the menu link attributes.
  209. $edit = array(
  210. 'menu[enabled]' => 1,
  211. 'menu[link_title]' => $node_title,
  212. 'menu[weight]' => 17,
  213. 'menu[options][attributes][title]' => $this->menu_attributes_edit['title'],
  214. 'menu[options][attributes][id]' => $this->menu_attributes_edit['id'],
  215. 'menu[options][attributes][name]' => $this->menu_attributes_edit['name'],
  216. 'menu[options][attributes][rel]' => $this->menu_attributes_edit['rel'],
  217. 'menu[options][attributes][class]' => $this->menu_attributes_edit['class'],
  218. 'menu[options][attributes][style]' => $this->menu_attributes_edit['style'],
  219. 'menu[options][attributes][target]' => $this->menu_attributes_edit['target'],
  220. 'menu[options][attributes][accesskey]' => $this->menu_attributes_edit['accesskey'],
  221. );
  222. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  223. // Assert that the link attributes exist.
  224. $this->drupalGet('node/' . $node->nid . '/edit');
  225. $this->assertMenuAttributes('menu[options][attributes]', 'edit');
  226. // Edit the node and remove the menu link.
  227. $edit = array(
  228. 'menu[enabled]' => FALSE,
  229. );
  230. $this->drupalPost('node/' . $node->nid . '/edit', $edit, t('Save'));
  231. // Assert that there is no link for the node.
  232. $this->drupalGet('');
  233. $this->assertNoLink($node_title);
  234. }
  235. }