##// END OF EJS Templates
use escaped "can't" constant at ProjectsControllerTest...
Toshi MARUYAMA -
r12527:031e4fde63a0
parent child
Show More
@@ -1,609 +1,609
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2014 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class ProjectsControllerTest < ActionController::TestCase
21 21 fixtures :projects, :versions, :users, :roles, :members,
22 22 :member_roles, :issues, :journals, :journal_details,
23 23 :trackers, :projects_trackers, :issue_statuses,
24 24 :enabled_modules, :enumerations, :boards, :messages,
25 25 :attachments, :custom_fields, :custom_values, :time_entries
26 26
27 27 def setup
28 28 @request.session[:user_id] = nil
29 29 Setting.default_language = 'en'
30 30 end
31 31
32 32 def test_index_by_anonymous_should_not_show_private_projects
33 33 get :index
34 34 assert_response :success
35 35 assert_template 'index'
36 36 projects = assigns(:projects)
37 37 assert_not_nil projects
38 38 assert projects.all?(&:is_public?)
39 39
40 40 assert_select 'ul' do
41 41 assert_select 'li' do
42 42 assert_select 'a', :text => 'eCookbook'
43 43 assert_select 'ul' do
44 44 assert_select 'a', :text => 'Child of private child'
45 45 end
46 46 end
47 47 end
48 48 assert_select 'a', :text => /Private child of eCookbook/, :count => 0
49 49 end
50 50
51 51 def test_index_atom
52 52 get :index, :format => 'atom'
53 53 assert_response :success
54 54 assert_template 'common/feed'
55 55 assert_select 'feed>title', :text => 'Redmine: Latest projects'
56 56 assert_select 'feed>entry', :count => Project.visible(User.current).count
57 57 end
58 58
59 59 test "#index by non-admin user with view_time_entries permission should show overall spent time link" do
60 60 @request.session[:user_id] = 3
61 61 get :index
62 62 assert_template 'index'
63 63 assert_select 'a[href=?]', '/time_entries'
64 64 end
65 65
66 66 test "#index by non-admin user without view_time_entries permission should not show overall spent time link" do
67 67 Role.find(2).remove_permission! :view_time_entries
68 68 Role.non_member.remove_permission! :view_time_entries
69 69 Role.anonymous.remove_permission! :view_time_entries
70 70 @request.session[:user_id] = 3
71 71
72 72 get :index
73 73 assert_template 'index'
74 74 assert_select 'a[href=?]', '/time_entries', 0
75 75 end
76 76
77 77 test "#new by admin user should accept get" do
78 78 @request.session[:user_id] = 1
79 79
80 80 get :new
81 81 assert_response :success
82 82 assert_template 'new'
83 83 end
84 84
85 85 test "#new by non-admin user with add_project permission should accept get" do
86 86 Role.non_member.add_permission! :add_project
87 87 @request.session[:user_id] = 9
88 88
89 89 get :new
90 90 assert_response :success
91 91 assert_template 'new'
92 92 assert_select 'select[name=?]', 'project[parent_id]', 0
93 93 end
94 94
95 95 test "#new by non-admin user with add_subprojects permission should accept get" do
96 96 Role.find(1).remove_permission! :add_project
97 97 Role.find(1).add_permission! :add_subprojects
98 98 @request.session[:user_id] = 2
99 99
100 100 get :new, :parent_id => 'ecookbook'
101 101 assert_response :success
102 102 assert_template 'new'
103 103
104 104 assert_select 'select[name=?]', 'project[parent_id]' do
105 105 # parent project selected
106 106 assert_select 'option[value=1][selected=selected]'
107 107 # no empty value
108 108 assert_select 'option[value=]', 0
109 109 end
110 110 end
111 111
112 112 test "#create by admin user should create a new project" do
113 113 @request.session[:user_id] = 1
114 114
115 115 post :create,
116 116 :project => {
117 117 :name => "blog",
118 118 :description => "weblog",
119 119 :homepage => 'http://weblog',
120 120 :identifier => "blog",
121 121 :is_public => 1,
122 122 :custom_field_values => { '3' => 'Beta' },
123 123 :tracker_ids => ['1', '3'],
124 124 # an issue custom field that is not for all project
125 125 :issue_custom_field_ids => ['9'],
126 126 :enabled_module_names => ['issue_tracking', 'news', 'repository']
127 127 }
128 128 assert_redirected_to '/projects/blog/settings'
129 129
130 130 project = Project.find_by_name('blog')
131 131 assert_kind_of Project, project
132 132 assert project.active?
133 133 assert_equal 'weblog', project.description
134 134 assert_equal 'http://weblog', project.homepage
135 135 assert_equal true, project.is_public?
136 136 assert_nil project.parent
137 137 assert_equal 'Beta', project.custom_value_for(3).value
138 138 assert_equal [1, 3], project.trackers.map(&:id).sort
139 139 assert_equal ['issue_tracking', 'news', 'repository'], project.enabled_module_names.sort
140 140 assert project.issue_custom_fields.include?(IssueCustomField.find(9))
141 141 end
142 142
143 143 test "#create by admin user should create a new subproject" do
144 144 @request.session[:user_id] = 1
145 145
146 146 assert_difference 'Project.count' do
147 147 post :create, :project => { :name => "blog",
148 148 :description => "weblog",
149 149 :identifier => "blog",
150 150 :is_public => 1,
151 151 :custom_field_values => { '3' => 'Beta' },
152 152 :parent_id => 1
153 153 }
154 154 assert_redirected_to '/projects/blog/settings'
155 155 end
156 156
157 157 project = Project.find_by_name('blog')
158 158 assert_kind_of Project, project
159 159 assert_equal Project.find(1), project.parent
160 160 end
161 161
162 162 test "#create by admin user should continue" do
163 163 @request.session[:user_id] = 1
164 164
165 165 assert_difference 'Project.count' do
166 166 post :create, :project => {:name => "blog", :identifier => "blog"}, :continue => 'Create and continue'
167 167 end
168 168 assert_redirected_to '/projects/new'
169 169 end
170 170
171 171 test "#create by non-admin user with add_project permission should create a new project" do
172 172 Role.non_member.add_permission! :add_project
173 173 @request.session[:user_id] = 9
174 174
175 175 post :create, :project => { :name => "blog",
176 176 :description => "weblog",
177 177 :identifier => "blog",
178 178 :is_public => 1,
179 179 :custom_field_values => { '3' => 'Beta' },
180 180 :tracker_ids => ['1', '3'],
181 181 :enabled_module_names => ['issue_tracking', 'news', 'repository']
182 182 }
183 183
184 184 assert_redirected_to '/projects/blog/settings'
185 185
186 186 project = Project.find_by_name('blog')
187 187 assert_kind_of Project, project
188 188 assert_equal 'weblog', project.description
189 189 assert_equal true, project.is_public?
190 190 assert_equal [1, 3], project.trackers.map(&:id).sort
191 191 assert_equal ['issue_tracking', 'news', 'repository'], project.enabled_module_names.sort
192 192
193 193 # User should be added as a project member
194 194 assert User.find(9).member_of?(project)
195 195 assert_equal 1, project.members.size
196 196 end
197 197
198 198 test "#create by non-admin user with add_project permission should fail with parent_id" do
199 199 Role.non_member.add_permission! :add_project
200 200 @request.session[:user_id] = 9
201 201
202 202 assert_no_difference 'Project.count' do
203 203 post :create, :project => { :name => "blog",
204 204 :description => "weblog",
205 205 :identifier => "blog",
206 206 :is_public => 1,
207 207 :custom_field_values => { '3' => 'Beta' },
208 208 :parent_id => 1
209 209 }
210 210 end
211 211 assert_response :success
212 212 project = assigns(:project)
213 213 assert_kind_of Project, project
214 214 assert_not_equal [], project.errors[:parent_id]
215 215 end
216 216
217 217 test "#create by non-admin user with add_subprojects permission should create a project with a parent_id" do
218 218 Role.find(1).remove_permission! :add_project
219 219 Role.find(1).add_permission! :add_subprojects
220 220 @request.session[:user_id] = 2
221 221
222 222 post :create, :project => { :name => "blog",
223 223 :description => "weblog",
224 224 :identifier => "blog",
225 225 :is_public => 1,
226 226 :custom_field_values => { '3' => 'Beta' },
227 227 :parent_id => 1
228 228 }
229 229 assert_redirected_to '/projects/blog/settings'
230 230 project = Project.find_by_name('blog')
231 231 end
232 232
233 233 test "#create by non-admin user with add_subprojects permission should fail without parent_id" do
234 234 Role.find(1).remove_permission! :add_project
235 235 Role.find(1).add_permission! :add_subprojects
236 236 @request.session[:user_id] = 2
237 237
238 238 assert_no_difference 'Project.count' do
239 239 post :create, :project => { :name => "blog",
240 240 :description => "weblog",
241 241 :identifier => "blog",
242 242 :is_public => 1,
243 243 :custom_field_values => { '3' => 'Beta' }
244 244 }
245 245 end
246 246 assert_response :success
247 247 project = assigns(:project)
248 248 assert_kind_of Project, project
249 249 assert_not_equal [], project.errors[:parent_id]
250 250 end
251 251
252 252 test "#create by non-admin user with add_subprojects permission should fail with unauthorized parent_id" do
253 253 Role.find(1).remove_permission! :add_project
254 254 Role.find(1).add_permission! :add_subprojects
255 255 @request.session[:user_id] = 2
256 256
257 257 assert !User.find(2).member_of?(Project.find(6))
258 258 assert_no_difference 'Project.count' do
259 259 post :create, :project => { :name => "blog",
260 260 :description => "weblog",
261 261 :identifier => "blog",
262 262 :is_public => 1,
263 263 :custom_field_values => { '3' => 'Beta' },
264 264 :parent_id => 6
265 265 }
266 266 end
267 267 assert_response :success
268 268 project = assigns(:project)
269 269 assert_kind_of Project, project
270 270 assert_not_equal [], project.errors[:parent_id]
271 271 end
272 272
273 273 def test_create_subproject_with_inherit_members_should_inherit_members
274 274 Role.find_by_name('Manager').add_permission! :add_subprojects
275 275 parent = Project.find(1)
276 276 @request.session[:user_id] = 2
277 277
278 278 assert_difference 'Project.count' do
279 279 post :create, :project => {
280 280 :name => 'inherited', :identifier => 'inherited', :parent_id => parent.id, :inherit_members => '1'
281 281 }
282 282 assert_response 302
283 283 end
284 284
285 285 project = Project.order('id desc').first
286 286 assert_equal 'inherited', project.name
287 287 assert_equal parent, project.parent
288 288 assert project.memberships.count > 0
289 289 assert_equal parent.memberships.count, project.memberships.count
290 290 end
291 291
292 292 def test_create_should_preserve_modules_on_validation_failure
293 293 with_settings :default_projects_modules => ['issue_tracking', 'repository'] do
294 294 @request.session[:user_id] = 1
295 295 assert_no_difference 'Project.count' do
296 296 post :create, :project => {
297 297 :name => "blog",
298 298 :identifier => "",
299 299 :enabled_module_names => %w(issue_tracking news)
300 300 }
301 301 end
302 302 assert_response :success
303 303 project = assigns(:project)
304 304 assert_equal %w(issue_tracking news), project.enabled_module_names.sort
305 305 end
306 306 end
307 307
308 308 def test_show_by_id
309 309 get :show, :id => 1
310 310 assert_response :success
311 311 assert_template 'show'
312 312 assert_not_nil assigns(:project)
313 313 end
314 314
315 315 def test_show_by_identifier
316 316 get :show, :id => 'ecookbook'
317 317 assert_response :success
318 318 assert_template 'show'
319 319 assert_not_nil assigns(:project)
320 320 assert_equal Project.find_by_identifier('ecookbook'), assigns(:project)
321 321
322 322 assert_select 'li', :text => /Development status/
323 323 end
324 324
325 325 def test_show_should_not_display_empty_sidebar
326 326 p = Project.find(1)
327 327 p.enabled_module_names = []
328 328 p.save!
329 329
330 330 get :show, :id => 'ecookbook'
331 331 assert_response :success
332 332 assert_select '#main.nosidebar'
333 333 end
334 334
335 335 def test_show_should_not_display_hidden_custom_fields
336 336 ProjectCustomField.find_by_name('Development status').update_attribute :visible, false
337 337 get :show, :id => 'ecookbook'
338 338 assert_response :success
339 339 assert_template 'show'
340 340 assert_not_nil assigns(:project)
341 341
342 342 assert_select 'li', :text => /Development status/, :count => 0
343 343 end
344 344
345 345 def test_show_should_not_fail_when_custom_values_are_nil
346 346 project = Project.find_by_identifier('ecookbook')
347 347 project.custom_values.first.update_attribute(:value, nil)
348 348 get :show, :id => 'ecookbook'
349 349 assert_response :success
350 350 assert_template 'show'
351 351 assert_not_nil assigns(:project)
352 352 assert_equal Project.find_by_identifier('ecookbook'), assigns(:project)
353 353 end
354 354
355 355 def show_archived_project_should_be_denied
356 356 project = Project.find_by_identifier('ecookbook')
357 357 project.archive!
358 358
359 359 get :show, :id => 'ecookbook'
360 360 assert_response 403
361 361 assert_nil assigns(:project)
362 362 assert_select 'p', :text => /archived/
363 363 end
364 364
365 365 def test_show_should_not_show_private_subprojects_that_are_not_visible
366 366 get :show, :id => 'ecookbook'
367 367 assert_response :success
368 368 assert_template 'show'
369 369 assert_select 'a', :text => /Private child/, :count => 0
370 370 end
371 371
372 372 def test_show_should_show_private_subprojects_that_are_visible
373 373 @request.session[:user_id] = 2 # manager who is a member of the private subproject
374 374 get :show, :id => 'ecookbook'
375 375 assert_response :success
376 376 assert_template 'show'
377 377 assert_select 'a', :text => /Private child/
378 378 end
379 379
380 380 def test_settings
381 381 @request.session[:user_id] = 2 # manager
382 382 get :settings, :id => 1
383 383 assert_response :success
384 384 assert_template 'settings'
385 385 end
386 386
387 387 def test_settings_of_subproject
388 388 @request.session[:user_id] = 2
389 389 get :settings, :id => 'private-child'
390 390 assert_response :success
391 391 assert_template 'settings'
392 392
393 393 assert_select 'input[type=checkbox][name=?]', 'project[inherit_members]'
394 394 end
395 395
396 396 def test_settings_should_be_denied_for_member_on_closed_project
397 397 Project.find(1).close
398 398 @request.session[:user_id] = 2 # manager
399 399
400 400 get :settings, :id => 1
401 401 assert_response 403
402 402 end
403 403
404 404 def test_settings_should_be_denied_for_anonymous_on_closed_project
405 405 Project.find(1).close
406 406
407 407 get :settings, :id => 1
408 408 assert_response 302
409 409 end
410 410
411 411 def test_update
412 412 @request.session[:user_id] = 2 # manager
413 413 post :update, :id => 1, :project => {:name => 'Test changed name',
414 414 :issue_custom_field_ids => ['']}
415 415 assert_redirected_to '/projects/ecookbook/settings'
416 416 project = Project.find(1)
417 417 assert_equal 'Test changed name', project.name
418 418 end
419 419
420 420 def test_update_with_failure
421 421 @request.session[:user_id] = 2 # manager
422 422 post :update, :id => 1, :project => {:name => ''}
423 423 assert_response :success
424 424 assert_template 'settings'
425 assert_error_tag :content => /name can&#x27;t be blank/i
425 assert_error_tag :content => /name #{ESCAPED_CANT} be blank/i
426 426 end
427 427
428 428 def test_update_should_be_denied_for_member_on_closed_project
429 429 Project.find(1).close
430 430 @request.session[:user_id] = 2 # manager
431 431
432 432 post :update, :id => 1, :project => {:name => 'Closed'}
433 433 assert_response 403
434 434 assert_equal 'eCookbook', Project.find(1).name
435 435 end
436 436
437 437 def test_update_should_be_denied_for_anonymous_on_closed_project
438 438 Project.find(1).close
439 439
440 440 post :update, :id => 1, :project => {:name => 'Closed'}
441 441 assert_response 302
442 442 assert_equal 'eCookbook', Project.find(1).name
443 443 end
444 444
445 445 def test_modules
446 446 @request.session[:user_id] = 2
447 447 Project.find(1).enabled_module_names = ['issue_tracking', 'news']
448 448
449 449 post :modules, :id => 1, :enabled_module_names => ['issue_tracking', 'repository', 'documents']
450 450 assert_redirected_to '/projects/ecookbook/settings/modules'
451 451 assert_equal ['documents', 'issue_tracking', 'repository'], Project.find(1).enabled_module_names.sort
452 452 end
453 453
454 454 def test_destroy_leaf_project_without_confirmation_should_show_confirmation
455 455 @request.session[:user_id] = 1 # admin
456 456
457 457 assert_no_difference 'Project.count' do
458 458 delete :destroy, :id => 2
459 459 assert_response :success
460 460 assert_template 'destroy'
461 461 end
462 462 end
463 463
464 464 def test_destroy_without_confirmation_should_show_confirmation_with_subprojects
465 465 @request.session[:user_id] = 1 # admin
466 466
467 467 assert_no_difference 'Project.count' do
468 468 delete :destroy, :id => 1
469 469 assert_response :success
470 470 assert_template 'destroy'
471 471 end
472 472 assert_select 'strong',
473 473 :text => ['Private child of eCookbook',
474 474 'Child of private child, eCookbook Subproject 1',
475 475 'eCookbook Subproject 2'].join(', ')
476 476 end
477 477
478 478 def test_destroy_with_confirmation_should_destroy_the_project_and_subprojects
479 479 @request.session[:user_id] = 1 # admin
480 480
481 481 assert_difference 'Project.count', -5 do
482 482 delete :destroy, :id => 1, :confirm => 1
483 483 assert_redirected_to '/admin/projects'
484 484 end
485 485 assert_nil Project.find_by_id(1)
486 486 end
487 487
488 488 def test_archive
489 489 @request.session[:user_id] = 1 # admin
490 490 post :archive, :id => 1
491 491 assert_redirected_to '/admin/projects'
492 492 assert !Project.find(1).active?
493 493 end
494 494
495 495 def test_archive_with_failure
496 496 @request.session[:user_id] = 1
497 497 Project.any_instance.stubs(:archive).returns(false)
498 498 post :archive, :id => 1
499 499 assert_redirected_to '/admin/projects'
500 500 assert_match /project cannot be archived/i, flash[:error]
501 501 end
502 502
503 503 def test_unarchive
504 504 @request.session[:user_id] = 1 # admin
505 505 Project.find(1).archive
506 506 post :unarchive, :id => 1
507 507 assert_redirected_to '/admin/projects'
508 508 assert Project.find(1).active?
509 509 end
510 510
511 511 def test_close
512 512 @request.session[:user_id] = 2
513 513 post :close, :id => 1
514 514 assert_redirected_to '/projects/ecookbook'
515 515 assert_equal Project::STATUS_CLOSED, Project.find(1).status
516 516 end
517 517
518 518 def test_reopen
519 519 Project.find(1).close
520 520 @request.session[:user_id] = 2
521 521 post :reopen, :id => 1
522 522 assert_redirected_to '/projects/ecookbook'
523 523 assert Project.find(1).active?
524 524 end
525 525
526 526 def test_project_breadcrumbs_should_be_limited_to_3_ancestors
527 527 CustomField.delete_all
528 528 parent = nil
529 529 6.times do |i|
530 530 p = Project.generate_with_parent!(parent)
531 531 get :show, :id => p
532 532 assert_select '#header h1' do
533 533 assert_select 'a', :count => [i, 3].min
534 534 end
535 535
536 536 parent = p
537 537 end
538 538 end
539 539
540 540 def test_get_copy
541 541 @request.session[:user_id] = 1 # admin
542 542 get :copy, :id => 1
543 543 assert_response :success
544 544 assert_template 'copy'
545 545 assert assigns(:project)
546 546 assert_equal Project.find(1).description, assigns(:project).description
547 547 assert_nil assigns(:project).id
548 548
549 549 assert_select 'input[name=?][value=?]', 'project[enabled_module_names][]', 'issue_tracking', 1
550 550 end
551 551
552 552 def test_get_copy_with_invalid_source_should_respond_with_404
553 553 @request.session[:user_id] = 1
554 554 get :copy, :id => 99
555 555 assert_response 404
556 556 end
557 557
558 558 def test_post_copy_should_copy_requested_items
559 559 @request.session[:user_id] = 1 # admin
560 560 CustomField.delete_all
561 561
562 562 assert_difference 'Project.count' do
563 563 post :copy, :id => 1,
564 564 :project => {
565 565 :name => 'Copy',
566 566 :identifier => 'unique-copy',
567 567 :tracker_ids => ['1', '2', '3', ''],
568 568 :enabled_module_names => %w(issue_tracking time_tracking)
569 569 },
570 570 :only => %w(issues versions)
571 571 end
572 572 project = Project.find('unique-copy')
573 573 source = Project.find(1)
574 574 assert_equal %w(issue_tracking time_tracking), project.enabled_module_names.sort
575 575
576 576 assert_equal source.versions.count, project.versions.count, "All versions were not copied"
577 577 assert_equal source.issues.count, project.issues.count, "All issues were not copied"
578 578 assert_equal 0, project.members.count
579 579 end
580 580
581 581 def test_post_copy_should_redirect_to_settings_when_successful
582 582 @request.session[:user_id] = 1 # admin
583 583 post :copy, :id => 1, :project => {:name => 'Copy', :identifier => 'unique-copy'}
584 584 assert_response :redirect
585 585 assert_redirected_to :controller => 'projects', :action => 'settings', :id => 'unique-copy'
586 586 end
587 587
588 588 def test_jump_should_redirect_to_active_tab
589 589 get :show, :id => 1, :jump => 'issues'
590 590 assert_redirected_to '/projects/ecookbook/issues'
591 591 end
592 592
593 593 def test_jump_should_not_redirect_to_inactive_tab
594 594 get :show, :id => 3, :jump => 'documents'
595 595 assert_response :success
596 596 assert_template 'show'
597 597 end
598 598
599 599 def test_jump_should_not_redirect_to_unknown_tab
600 600 get :show, :id => 3, :jump => 'foobar'
601 601 assert_response :success
602 602 assert_template 'show'
603 603 end
604 604
605 605 def test_body_should_have_project_css_class
606 606 get :show, :id => 1
607 607 assert_select 'body.project-ecookbook'
608 608 end
609 609 end
General Comments 0
You need to be logged in to leave comments. Login now