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