menu_attributes.test 9.1 KB

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