1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
|
# frozen_string_literal: true
# Redmine - project management software
# Copyright (C) 2006-2023 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require File.expand_path('../../../../../test_helper', __FILE__)
class Redmine::MenuManager::MenuHelperTest < Redmine::HelperTest
include Redmine::MenuManager::MenuHelper
include ERB::Util
include Rails.application.routes.url_helpers
fixtures :users, :members, :projects, :enabled_modules, :roles, :member_roles
def setup
setup_with_controller
# Stub the current menu item in the controller
self.class_eval do
def current_menu_item
:index
end
end
end
def test_render_single_menu_node
node = Redmine::MenuManager::MenuItem.new(:testing, '/test', {})
@output_buffer = render_single_menu_node(node, 'This is a test', node.url, false)
assert_select("a.testing", "This is a test")
end
def test_render_menu_node
single_node = Redmine::MenuManager::MenuItem.new(:single_node, '/test', {})
@output_buffer = render_menu_node(single_node, nil)
assert_select("li") do
assert_select("a.single-node", "Single node")
end
end
def test_render_menu_node_with_symbol_as_url
node = Redmine::MenuManager::MenuItem.new(:testing, :issues_path)
@output_buffer = render_menu_node(node, nil)
assert_select 'a[href="/issues"]', "Testing"
end
def test_render_menu_node_with_symbol_as_url_and_project
node = Redmine::MenuManager::MenuItem.new(:testing, :project_issues_path)
@output_buffer = render_menu_node(node, Project.find(1))
assert_select 'a[href="/projects/ecookbook/issues"]', "Testing"
end
def test_render_menu_node_with_nested_items
parent_node = Redmine::MenuManager::MenuItem.new(:parent_node, '/test', {})
parent_node << Redmine::MenuManager::MenuItem.new(:child_one_node, '/test', {})
parent_node << Redmine::MenuManager::MenuItem.new(:child_two_node, '/test', {})
parent_node <<
Redmine::MenuManager::MenuItem.new(:child_three_node, '/test', {}) <<
Redmine::MenuManager::MenuItem.new(:child_three_inner_node, '/test', {})
@output_buffer = render_menu_node(parent_node, nil)
assert_select("li") do
assert_select("a.parent-node", "Parent node")
assert_select("ul") do
assert_select("li a.child-one-node", "Child one node")
assert_select("li a.child-two-node", "Child two node")
assert_select("li") do
assert_select("a.child-three-node", "Child three node")
assert_select("ul") do
assert_select("li a.child-three-inner-node", "Child three inner node")
end
end
end
end
end
def test_render_menu_node_with_children
User.current = User.find(2)
parent_node =
Redmine::MenuManager::MenuItem.
new(
:parent_node,
'/test',
{
:children => Proc.new do |p|
children = []
3.times do |time|
children << Redmine::MenuManager::MenuItem.new(
"test_child_#{time}",
{:controller => 'issues', :action => 'index'},
{}
)
end
children
end
}
)
@output_buffer = render_menu_node(parent_node, Project.find(1))
assert_select("li") do
assert_select("a.parent-node", "Parent node")
assert_select("ul") do
assert_select("li a.test-child-0", "Test child 0")
assert_select("li a.test-child-1", "Test child 1")
assert_select("li a.test-child-2", "Test child 2")
end
end
end
def test_render_menu_node_with_nested_items_and_children
User.current = User.find(2)
parent_node =
Redmine::MenuManager::MenuItem.new(
:parent_node,
{:controller => 'issues', :action => 'index'},
{
:children => Proc.new do |p|
children = []
3.times do |time|
children <<
Redmine::MenuManager::MenuItem.new(
"test_child_#{time}",
{:controller => 'issues', :action => 'index'}, {}
)
end
children
end
}
)
parent_node <<
Redmine::MenuManager::MenuItem.new(
:child_node,
{:controller => 'issues', :action => 'index'},
{
:children =>
Proc.new do |p|
children = []
6.times do |time|
children <<
Redmine::MenuManager::MenuItem.new(
"test_dynamic_child_#{time}",
{:controller => 'issues', :action => 'index'}, {}
)
end
children
end
}
)
@output_buffer = render_menu_node(parent_node, Project.find(1))
assert_select("li") do
assert_select("a.parent-node", "Parent node")
assert_select("ul") do
assert_select("li a.child-node", "Child node")
assert_select("ul") do
assert_select("li a.test-dynamic-child-0", "Test dynamic child 0")
assert_select("li a.test-dynamic-child-1", "Test dynamic child 1")
assert_select("li a.test-dynamic-child-2", "Test dynamic child 2")
assert_select("li a.test-dynamic-child-3", "Test dynamic child 3")
assert_select("li a.test-dynamic-child-4", "Test dynamic child 4")
assert_select("li a.test-dynamic-child-5", "Test dynamic child 5")
end
assert_select("li a.test-child-0", "Test child 0")
assert_select("li a.test-child-1", "Test child 1")
assert_select("li a.test-child-2", "Test child 2")
end
end
end
def test_render_menu_node_with_allowed_and_unallowed_unattached_children
User.current = User.find(2)
parent_node =
Redmine::MenuManager::MenuItem.new(
:parent_node,
{:controller => 'issues', :action => 'index'},
{
:children => Proc.new do |p|
[
Redmine::MenuManager::MenuItem.new(
"test_child_allowed",
{:controller => 'issues', :action => 'index'}, {}
),
Redmine::MenuManager::MenuItem.new(
"test_child_unallowed",
{:controller => 'issues', :action => 'unallowed'}, {}
),
]
end
}
)
@output_buffer = render_menu_node(parent_node, Project.find(1))
assert_select("li") do
assert_select("a.parent-node", "Parent node")
assert_select("ul.menu-children.unattached") do
assert_select("li a.test-child-allowed", "Test child allowed")
assert_select("li a.test-child-unallowed", false)
end
end
end
def test_render_menu_node_with_allowed_and_unallowed_standard_children
User.current = User.find(6)
Redmine::MenuManager.map :some_menu do |menu|
menu.push(:parent_node, {:controller => 'issues', :action => 'index'}, {})
menu.push(:test_child_allowed, {:controller => 'issues', :action => 'index'}, {:parent => :parent_node})
menu.push(:test_child_unallowed, {:controller => 'issues', :action => 'new'}, {:parent => :parent_node})
end
@output_buffer = render_menu(:some_menu, Project.find(1))
assert_select("li") do
assert_select("a.parent-node", "Parent node")
assert_select("ul.menu-children.unattached", false)
assert_select("ul.menu-children") do
assert_select("li a.test-child-allowed", "Test child allowed")
assert_select("li a.test-child-unallowed", false)
end
end
end
def test_render_empty_virtual_menu_node_with_children
# only empty item with no click target
Redmine::MenuManager.map :menu1 do |menu|
menu.push(:parent_node, nil, {})
end
# parent with unallowed unattached child
Redmine::MenuManager.map :menu2 do |menu|
menu.push(
:parent_node, nil,
{
:children =>
Proc.new do |p|
[
Redmine::MenuManager::MenuItem.new(
"test_child_unallowed",
{:controller => 'issues', :action => 'new'}, {}
)
]
end
}
)
end
# parent with unallowed standard child
Redmine::MenuManager.map :menu3 do |menu|
menu.push(:parent_node, nil, {})
menu.push(:test_child_unallowed, {:controller =>'issues', :action => 'new'}, {:parent => :parent_node})
end
# should not be displayed to anonymous
User.current = User.find(6)
assert_nil render_menu(:menu1, Project.find(1))
assert_nil render_menu(:menu2, Project.find(1))
assert_nil render_menu(:menu3, Project.find(1))
# should be displayed to an admin
User.current = User.find(1)
@output_buffer = render_menu(:menu2, Project.find(1))
assert_select("ul li a.parent-node", "Parent node")
@output_buffer = render_menu(:menu3, Project.find(1))
assert_select("ul li a.parent-node", "Parent node")
end
def test_render_menu_node_with_children_without_an_array
parent_node =
Redmine::MenuManager::MenuItem.new(
:parent_node,
'/test',
{
:children =>
Proc.new do |p|
Redmine::MenuManager::MenuItem.new("test_child", "/testing", {})
end
}
)
assert_raises Redmine::MenuManager::MenuError, ":children must be an array of MenuItems" do
@output_buffer = render_menu_node(parent_node, Project.find(1))
end
end
def test_render_menu_node_with_incorrect_children
parent_node =
Redmine::MenuManager::MenuItem.new(
:parent_node,
'/test',
{
:children => Proc.new {|p| ["a string"]}
}
)
assert_raises Redmine::MenuManager::MenuError, ":children must be an array of MenuItems" do
@output_buffer = render_menu_node(parent_node, Project.find(1))
end
end
def test_menu_items_for_should_yield_all_items_if_passed_a_block
menu_name = :test_menu_items_for_should_yield_all_items_if_passed_a_block
Redmine::MenuManager.map menu_name do |menu|
menu.push(:a_menu, '/', {})
menu.push(:a_menu_2, '/', {})
menu.push(:a_menu_3, '/', {})
end
items_yielded = []
menu_items_for(menu_name) do |item|
items_yielded << item
end
assert_equal 3, items_yielded.size
end
def test_menu_items_for_should_return_all_items
menu_name = :test_menu_items_for_should_return_all_items
Redmine::MenuManager.map menu_name do |menu|
menu.push(:a_menu, '/', {})
menu.push(:a_menu_2, '/', {})
menu.push(:a_menu_3, '/', {})
end
items = menu_items_for(menu_name)
assert_equal 3, items.size
end
def test_menu_items_for_should_skip_unallowed_items_on_a_project
menu_name = :test_menu_items_for_should_skip_unallowed_items_on_a_project
Redmine::MenuManager.map menu_name do |menu|
menu.push(:a_menu, {:controller => 'issues', :action => 'index'}, {})
menu.push(:a_menu_2, {:controller => 'issues', :action => 'index'}, {})
menu.push(:unallowed, {:controller => 'issues', :action => 'unallowed'}, {})
end
User.current = User.find(2)
items = menu_items_for(menu_name, Project.find(1))
assert_equal 2, items.size
end
def test_menu_items_for_should_skip_items_that_fail_the_permission
menu_name = :test_menu_items_for_should_skip_items_that_fail_the_permission
Redmine::MenuManager.map menu_name do |menu|
menu.push(:a_menu, :project_issues_path)
menu.push(:unallowed, :project_issues_path, :permission => :unallowed)
end
User.current = User.find(2)
items = menu_items_for(menu_name, Project.find(1))
assert_equal 1, items.size
end
def test_menu_items_for_should_skip_items_that_fail_the_conditions
menu_name = :test_menu_items_for_should_skip_items_that_fail_the_conditions
Redmine::MenuManager.map menu_name do |menu|
menu.push(:a_menu, {:controller => 'issues', :action => 'index'}, {})
menu.push(:unallowed,
{:controller => 'issues', :action => 'index'},
{:if => Proc.new {false}})
end
User.current = User.find(2)
items = menu_items_for(menu_name, Project.find(1))
assert_equal 1, items.size
end
end
|