OSDN Git Service

Move database.yml to template
[redminele/redminele.git] / redmine / test / functional / projects_controller_test.rb
1 # Redmine - project management software
2 # Copyright (C) 2006-2008  Jean-Philippe Lang
3 #
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
8
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU General Public License for more details.
13
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
17
18 require File.dirname(__FILE__) + '/../test_helper'
19 require 'projects_controller'
20
21 # Re-raise errors caught by the controller.
22 class ProjectsController; def rescue_action(e) raise e end; end
23
24 class ProjectsControllerTest < ActionController::TestCase
25   fixtures :projects, :versions, :users, :roles, :members, :member_roles, :issues, :journals, :journal_details,
26            :trackers, :projects_trackers, :issue_statuses, :enabled_modules, :enumerations, :boards, :messages,
27            :attachments, :custom_fields, :custom_values, :time_entries
28
29   def setup
30     @controller = ProjectsController.new
31     @request    = ActionController::TestRequest.new
32     @response   = ActionController::TestResponse.new
33     @request.session[:user_id] = nil
34     Setting.default_language = 'en'
35   end
36   
37   def test_index_routing
38     assert_routing(
39       {:method => :get, :path => '/projects'},
40       :controller => 'projects', :action => 'index'
41     )
42   end
43   
44   def test_index
45     get :index
46     assert_response :success
47     assert_template 'index'
48     assert_not_nil assigns(:projects)
49     
50     assert_tag :ul, :child => {:tag => 'li',
51                                :descendant => {:tag => 'a', :content => 'eCookbook'},
52                                :child => { :tag => 'ul',
53                                            :descendant => { :tag => 'a',
54                                                             :content => 'Child of private child'
55                                                            }
56                                           }
57                                }
58                                
59     assert_no_tag :a, :content => /Private child of eCookbook/
60   end
61   
62   def test_index_atom_routing
63     assert_routing(
64       {:method => :get, :path => '/projects.atom'},
65       :controller => 'projects', :action => 'index', :format => 'atom'
66     )
67   end
68   
69   def test_index_atom
70     get :index, :format => 'atom'
71     assert_response :success
72     assert_template 'common/feed.atom.rxml'
73     assert_select 'feed>title', :text => 'Redmine: Latest projects'
74     assert_select 'feed>entry', :count => Project.count(:conditions => Project.visible_by(User.current))
75   end
76   
77   def test_add_routing
78     assert_routing(
79       {:method => :get, :path => '/projects/new'},
80       :controller => 'projects', :action => 'add'
81     )
82     assert_recognizes(
83       {:controller => 'projects', :action => 'add'},
84       {:method => :post, :path => '/projects/new'}
85     )
86     assert_recognizes(
87       {:controller => 'projects', :action => 'add'},
88       {:method => :post, :path => '/projects'}
89     )
90   end
91   
92   context "#add" do
93     context "by admin user" do
94       setup do
95         @request.session[:user_id] = 1
96       end
97       
98       should "accept get" do
99         get :add
100         assert_response :success
101         assert_template 'add'
102       end
103       
104       should "accept post" do
105         post :add, :project => { :name => "blog", 
106                                  :description => "weblog",
107                                  :identifier => "blog",
108                                  :is_public => 1,
109                                  :custom_field_values => { '3' => 'Beta' }
110                                 }
111         assert_redirected_to '/projects/blog/settings'
112         
113         project = Project.find_by_name('blog')
114         assert_kind_of Project, project
115         assert_equal 'weblog', project.description 
116         assert_equal true, project.is_public?
117         assert_nil project.parent
118       end
119       
120       should "accept post with parent" do
121         post :add, :project => { :name => "blog", 
122                                  :description => "weblog",
123                                  :identifier => "blog",
124                                  :is_public => 1,
125                                  :custom_field_values => { '3' => 'Beta' },
126                                  :parent_id => 1
127                                 }
128         assert_redirected_to '/projects/blog/settings'
129         
130         project = Project.find_by_name('blog')
131         assert_kind_of Project, project
132         assert_equal Project.find(1), project.parent
133       end
134     end
135     
136     context "by non-admin user with add_project permission" do
137       setup do
138         Role.non_member.add_permission! :add_project
139         @request.session[:user_id] = 9
140       end
141       
142       should "accept get" do
143         get :add
144         assert_response :success
145         assert_template 'add'
146         assert_no_tag :select, :attributes => {:name => 'project[parent_id]'}
147       end
148       
149       should "accept post" do
150         post :add, :project => { :name => "blog", 
151                                  :description => "weblog",
152                                  :identifier => "blog",
153                                  :is_public => 1,
154                                  :custom_field_values => { '3' => 'Beta' }
155                                 }
156         
157         assert_redirected_to '/projects/blog/settings'
158         
159         project = Project.find_by_name('blog')
160         assert_kind_of Project, project
161         assert_equal 'weblog', project.description 
162         assert_equal true, project.is_public?
163         
164         # User should be added as a project member
165         assert User.find(9).member_of?(project)
166         assert_equal 1, project.members.size
167       end
168       
169       should "fail with parent_id" do
170         assert_no_difference 'Project.count' do
171           post :add, :project => { :name => "blog", 
172                                    :description => "weblog",
173                                    :identifier => "blog",
174                                    :is_public => 1,
175                                    :custom_field_values => { '3' => 'Beta' },
176                                    :parent_id => 1
177                                   }
178         end
179         assert_response :success
180         project = assigns(:project)
181         assert_kind_of Project, project
182         assert_not_nil project.errors.on(:parent_id)
183       end
184     end
185     
186     context "by non-admin user with add_subprojects permission" do
187       setup do
188         Role.find(1).remove_permission! :add_project
189         Role.find(1).add_permission! :add_subprojects
190         @request.session[:user_id] = 2
191       end
192       
193       should "accept get" do
194         get :add, :parent_id => 'ecookbook'
195         assert_response :success
196         assert_template 'add'
197         # parent project selected
198         assert_tag :select, :attributes => {:name => 'project[parent_id]'},
199                             :child => {:tag => 'option', :attributes => {:value => '1', :selected => 'selected'}}
200         # no empty value
201         assert_no_tag :select, :attributes => {:name => 'project[parent_id]'},
202                                :child => {:tag => 'option', :attributes => {:value => ''}}
203       end
204       
205       should "accept post with parent_id" do
206         post :add, :project => { :name => "blog", 
207                                  :description => "weblog",
208                                  :identifier => "blog",
209                                  :is_public => 1,
210                                  :custom_field_values => { '3' => 'Beta' },
211                                  :parent_id => 1
212                                 }
213         assert_redirected_to '/projects/blog/settings'
214         project = Project.find_by_name('blog')
215       end
216       
217       should "fail without parent_id" do
218         assert_no_difference 'Project.count' do
219           post :add, :project => { :name => "blog", 
220                                    :description => "weblog",
221                                    :identifier => "blog",
222                                    :is_public => 1,
223                                    :custom_field_values => { '3' => 'Beta' }
224                                   }
225         end
226         assert_response :success
227         project = assigns(:project)
228         assert_kind_of Project, project
229         assert_not_nil project.errors.on(:parent_id)
230       end
231       
232       should "fail with unauthorized parent_id" do
233         assert !User.find(2).member_of?(Project.find(6))
234         assert_no_difference 'Project.count' do
235           post :add, :project => { :name => "blog", 
236                                    :description => "weblog",
237                                    :identifier => "blog",
238                                    :is_public => 1,
239                                    :custom_field_values => { '3' => 'Beta' },
240                                    :parent_id => 6
241                                   }
242         end
243         assert_response :success
244         project = assigns(:project)
245         assert_kind_of Project, project
246         assert_not_nil project.errors.on(:parent_id)
247       end
248     end
249   end
250   
251   def test_show_routing
252     assert_routing(
253       {:method => :get, :path => '/projects/test'},
254       :controller => 'projects', :action => 'show', :id => 'test'
255     )
256   end
257   
258   def test_show_by_id
259     get :show, :id => 1
260     assert_response :success
261     assert_template 'show'
262     assert_not_nil assigns(:project)
263   end
264
265   def test_show_by_identifier
266     get :show, :id => 'ecookbook'
267     assert_response :success
268     assert_template 'show'
269     assert_not_nil assigns(:project)
270     assert_equal Project.find_by_identifier('ecookbook'), assigns(:project)
271   end
272   
273   def test_show_should_not_fail_when_custom_values_are_nil
274     project = Project.find_by_identifier('ecookbook')
275     project.custom_values.first.update_attribute(:value, nil)
276     get :show, :id => 'ecookbook'
277     assert_response :success
278     assert_template 'show'
279     assert_not_nil assigns(:project)
280     assert_equal Project.find_by_identifier('ecookbook'), assigns(:project)
281   end
282   
283   def test_private_subprojects_hidden
284     get :show, :id => 'ecookbook'
285     assert_response :success
286     assert_template 'show'
287     assert_no_tag :tag => 'a', :content => /Private child/
288   end
289
290   def test_private_subprojects_visible
291     @request.session[:user_id] = 2 # manager who is a member of the private subproject
292     get :show, :id => 'ecookbook'
293     assert_response :success
294     assert_template 'show'
295     assert_tag :tag => 'a', :content => /Private child/
296   end
297   
298   def test_settings_routing
299     assert_routing(
300       {:method => :get, :path => '/projects/4223/settings'},
301       :controller => 'projects', :action => 'settings', :id => '4223'
302     )
303     assert_routing(
304       {:method => :get, :path => '/projects/4223/settings/members'},
305       :controller => 'projects', :action => 'settings', :id => '4223', :tab => 'members'
306     )
307   end
308   
309   def test_settings
310     @request.session[:user_id] = 2 # manager
311     get :settings, :id => 1
312     assert_response :success
313     assert_template 'settings'
314   end
315   
316   def test_edit
317     @request.session[:user_id] = 2 # manager
318     post :edit, :id => 1, :project => {:name => 'Test changed name',
319                                        :issue_custom_field_ids => ['']}
320     assert_redirected_to 'projects/ecookbook/settings'
321     project = Project.find(1)
322     assert_equal 'Test changed name', project.name
323   end
324   
325   def test_add_version_routing
326     assert_routing(
327       {:method => :get, :path => 'projects/64/versions/new'},
328       :controller => 'projects', :action => 'add_version', :id => '64'
329     )
330     assert_routing(
331     #TODO: use PUT
332       {:method => :post, :path => 'projects/64/versions/new'},
333       :controller => 'projects', :action => 'add_version', :id => '64'
334     )
335   end
336   
337   def test_add_version
338     @request.session[:user_id] = 2 # manager
339     assert_difference 'Version.count' do
340       post :add_version, :id => '1', :version => {:name => 'test_add_version'}
341     end
342     assert_redirected_to '/projects/ecookbook/settings/versions'
343     version = Version.find_by_name('test_add_version')
344     assert_not_nil version
345     assert_equal 1, version.project_id
346   end
347   
348   def test_add_version_from_issue_form
349     @request.session[:user_id] = 2 # manager
350     assert_difference 'Version.count' do
351       xhr :post, :add_version, :id => '1', :version => {:name => 'test_add_version_from_issue_form'}
352     end
353     assert_response :success
354     assert_select_rjs :replace, 'issue_fixed_version_id'
355     version = Version.find_by_name('test_add_version_from_issue_form')
356     assert_not_nil version
357     assert_equal 1, version.project_id
358   end
359   
360   def test_add_issue_category_routing
361     assert_routing(
362       {:method => :get, :path => 'projects/test/categories/new'},
363       :controller => 'projects', :action => 'add_issue_category', :id => 'test'
364     )
365     assert_routing(
366     #TODO: use PUT and update form
367       {:method => :post, :path => 'projects/64/categories/new'},
368       :controller => 'projects', :action => 'add_issue_category', :id => '64'
369     )
370   end
371   
372   def test_destroy_routing
373     assert_routing(
374       {:method => :get, :path => '/projects/567/destroy'},
375       :controller => 'projects', :action => 'destroy', :id => '567'
376     )
377     assert_routing(
378     #TODO: use DELETE and update form
379       {:method => :post, :path => 'projects/64/destroy'},
380       :controller => 'projects', :action => 'destroy', :id => '64'
381     )
382   end
383   
384   def test_get_destroy
385     @request.session[:user_id] = 1 # admin
386     get :destroy, :id => 1
387     assert_response :success
388     assert_template 'destroy'
389     assert_not_nil Project.find_by_id(1)
390   end
391
392   def test_post_destroy
393     @request.session[:user_id] = 1 # admin
394     post :destroy, :id => 1, :confirm => 1
395     assert_redirected_to 'admin/projects'
396     assert_nil Project.find_by_id(1)
397   end
398   
399   def test_add_file
400     set_tmp_attachments_directory
401     @request.session[:user_id] = 2
402     Setting.notified_events = ['file_added']
403     ActionMailer::Base.deliveries.clear
404     
405     assert_difference 'Attachment.count' do
406       post :add_file, :id => 1, :version_id => '',
407            :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
408     end
409     assert_redirected_to 'projects/ecookbook/files'
410     a = Attachment.find(:first, :order => 'created_on DESC')
411     assert_equal 'testfile.txt', a.filename
412     assert_equal Project.find(1), a.container
413
414     mail = ActionMailer::Base.deliveries.last
415     assert_kind_of TMail::Mail, mail
416     assert_equal "[eCookbook] New file", mail.subject
417     assert mail.body.include?('testfile.txt')
418   end
419   
420   def test_add_file_routing
421     assert_routing(
422       {:method => :get, :path => '/projects/33/files/new'},
423       :controller => 'projects', :action => 'add_file', :id => '33'
424     )
425     assert_routing(
426       {:method => :post, :path => '/projects/33/files/new'},
427       :controller => 'projects', :action => 'add_file', :id => '33'
428     )
429   end
430   
431   def test_add_version_file
432     set_tmp_attachments_directory
433     @request.session[:user_id] = 2
434     Setting.notified_events = ['file_added']
435     
436     assert_difference 'Attachment.count' do
437       post :add_file, :id => 1, :version_id => '2',
438            :attachments => {'1' => {'file' => uploaded_test_file('testfile.txt', 'text/plain')}}
439     end
440     assert_redirected_to 'projects/ecookbook/files'
441     a = Attachment.find(:first, :order => 'created_on DESC')
442     assert_equal 'testfile.txt', a.filename
443     assert_equal Version.find(2), a.container
444   end
445   
446   def test_list_files
447     get :list_files, :id => 1
448     assert_response :success
449     assert_template 'list_files'
450     assert_not_nil assigns(:containers)
451     
452     # file attached to the project
453     assert_tag :a, :content => 'project_file.zip',
454                    :attributes => { :href => '/attachments/download/8/project_file.zip' }
455     
456     # file attached to a project's version
457     assert_tag :a, :content => 'version_file.zip',
458                    :attributes => { :href => '/attachments/download/9/version_file.zip' }
459   end
460
461   def test_list_files_routing
462     assert_routing(
463       {:method => :get, :path => '/projects/33/files'},
464       :controller => 'projects', :action => 'list_files', :id => '33'
465     )
466   end
467
468   def test_roadmap_routing
469     assert_routing(
470       {:method => :get, :path => 'projects/33/roadmap'},
471       :controller => 'projects', :action => 'roadmap', :id => '33'
472     )
473   end
474   
475   def test_roadmap
476     get :roadmap, :id => 1
477     assert_response :success
478     assert_template 'roadmap'
479     assert_not_nil assigns(:versions)
480     # Version with no date set appears
481     assert assigns(:versions).include?(Version.find(3))
482     # Completed version doesn't appear
483     assert !assigns(:versions).include?(Version.find(1))
484   end
485   
486   def test_roadmap_with_completed_versions
487     get :roadmap, :id => 1, :completed => 1
488     assert_response :success
489     assert_template 'roadmap'
490     assert_not_nil assigns(:versions)
491     # Version with no date set appears
492     assert assigns(:versions).include?(Version.find(3))
493     # Completed version appears
494     assert assigns(:versions).include?(Version.find(1))
495   end
496
497   def test_roadmap_showing_subprojects_versions
498     get :roadmap, :id => 1, :with_subprojects => 1
499     assert_response :success
500     assert_template 'roadmap'
501     assert_not_nil assigns(:versions)
502     # Version on subproject appears
503     assert assigns(:versions).include?(Version.find(4))
504   end
505   
506   def test_project_activity_routing
507     assert_routing(
508       {:method => :get, :path => '/projects/1/activity'},
509        :controller => 'projects', :action => 'activity', :id => '1'
510     )
511   end
512   
513   def test_project_activity_atom_routing
514     assert_routing(
515       {:method => :get, :path => '/projects/1/activity.atom'},
516        :controller => 'projects', :action => 'activity', :id => '1', :format => 'atom'
517     )    
518   end
519   
520   def test_project_activity
521     get :activity, :id => 1, :with_subprojects => 0
522     assert_response :success
523     assert_template 'activity'
524     assert_not_nil assigns(:events_by_day)
525     
526     assert_tag :tag => "h3", 
527                :content => /#{2.days.ago.to_date.day}/,
528                :sibling => { :tag => "dl",
529                  :child => { :tag => "dt",
530                    :attributes => { :class => /issue-edit/ },
531                    :child => { :tag => "a",
532                      :content => /(#{IssueStatus.find(2).name})/,
533                    }
534                  }
535                }
536   end
537   
538   def test_previous_project_activity
539     get :activity, :id => 1, :from => 3.days.ago.to_date
540     assert_response :success
541     assert_template 'activity'
542     assert_not_nil assigns(:events_by_day)
543                
544     assert_tag :tag => "h3", 
545                :content => /#{3.day.ago.to_date.day}/,
546                :sibling => { :tag => "dl",
547                  :child => { :tag => "dt",
548                    :attributes => { :class => /issue/ },
549                    :child => { :tag => "a",
550                      :content => /#{Issue.find(1).subject}/,
551                    }
552                  }
553                }
554   end
555   
556   def test_global_activity_routing
557     assert_routing({:method => :get, :path => '/activity'}, :controller => 'projects', :action => 'activity', :id => nil)
558   end
559   
560   def test_global_activity
561     get :activity
562     assert_response :success
563     assert_template 'activity'
564     assert_not_nil assigns(:events_by_day)
565     
566     assert_tag :tag => "h3", 
567                :content => /#{5.day.ago.to_date.day}/,
568                :sibling => { :tag => "dl",
569                  :child => { :tag => "dt",
570                    :attributes => { :class => /issue/ },
571                    :child => { :tag => "a",
572                      :content => /#{Issue.find(5).subject}/,
573                    }
574                  }
575                }
576   end
577   
578   def test_user_activity
579     get :activity, :user_id => 2
580     assert_response :success
581     assert_template 'activity'
582     assert_not_nil assigns(:events_by_day)
583     
584     assert_tag :tag => "h3", 
585                :content => /#{3.day.ago.to_date.day}/,
586                :sibling => { :tag => "dl",
587                  :child => { :tag => "dt",
588                    :attributes => { :class => /issue/ },
589                    :child => { :tag => "a",
590                      :content => /#{Issue.find(1).subject}/,
591                    }
592                  }
593                }
594   end
595   
596   def test_global_activity_atom_routing
597     assert_routing({:method => :get, :path => '/activity.atom'}, :controller => 'projects', :action => 'activity', :id => nil, :format => 'atom')
598   end
599   
600   def test_activity_atom_feed
601     get :activity, :format => 'atom'
602     assert_response :success
603     assert_template 'common/feed.atom.rxml'
604   end
605   
606   def test_archive_routing
607     assert_routing(
608     #TODO: use PUT to project path and modify form
609       {:method => :post, :path => 'projects/64/archive'},
610       :controller => 'projects', :action => 'archive', :id => '64'
611     )
612   end
613   
614   def test_archive
615     @request.session[:user_id] = 1 # admin
616     post :archive, :id => 1
617     assert_redirected_to 'admin/projects'
618     assert !Project.find(1).active?
619   end
620   
621   def test_unarchive_routing
622     assert_routing(
623     #TODO: use PUT to project path and modify form
624       {:method => :post, :path => '/projects/567/unarchive'},
625       :controller => 'projects', :action => 'unarchive', :id => '567'
626     )
627   end
628   
629   def test_unarchive
630     @request.session[:user_id] = 1 # admin
631     Project.find(1).archive
632     post :unarchive, :id => 1
633     assert_redirected_to 'admin/projects'
634     assert Project.find(1).active?
635   end
636   
637   def test_project_breadcrumbs_should_be_limited_to_3_ancestors
638     CustomField.delete_all
639     parent = nil
640     6.times do |i|
641       p = Project.create!(:name => "Breadcrumbs #{i}", :identifier => "breadcrumbs-#{i}")
642       p.set_parent!(parent)
643       get :show, :id => p
644       assert_tag :h1, :parent => { :attributes => {:id => 'header'}},
645                       :children => { :count => [i, 3].min,
646                                      :only => { :tag => 'a' } }
647                                      
648       parent = p
649     end
650   end
651
652   def test_copy_with_project
653     @request.session[:user_id] = 1 # admin
654     get :copy, :id => 1
655     assert_response :success
656     assert_template 'copy'
657     assert assigns(:project)
658     assert_equal Project.find(1).description, assigns(:project).description
659     assert_nil assigns(:project).id
660   end
661
662   def test_copy_without_project
663     @request.session[:user_id] = 1 # admin
664     get :copy
665     assert_response :redirect
666     assert_redirected_to :controller => 'admin', :action => 'projects'
667   end
668
669   def test_jump_should_redirect_to_active_tab
670     get :show, :id => 1, :jump => 'issues'
671     assert_redirected_to 'projects/ecookbook/issues'
672   end
673   
674   def test_jump_should_not_redirect_to_inactive_tab
675     get :show, :id => 3, :jump => 'documents'
676     assert_response :success
677     assert_template 'show'
678   end
679   
680   def test_jump_should_not_redirect_to_unknown_tab
681     get :show, :id => 3, :jump => 'foobar'
682     assert_response :success
683     assert_template 'show'
684   end
685
686   def test_reset_activities_routing
687     assert_routing({:method => :delete, :path => 'projects/64/reset_activities'},
688                    :controller => 'projects', :action => 'reset_activities', :id => '64')
689   end
690
691   def test_reset_activities
692     @request.session[:user_id] = 2 # manager
693     project_activity = TimeEntryActivity.new({
694                                                :name => 'Project Specific',
695                                                :parent => TimeEntryActivity.find(:first),
696                                                :project => Project.find(1),
697                                                :active => true
698                                              })
699     assert project_activity.save
700     project_activity_two = TimeEntryActivity.new({
701                                                    :name => 'Project Specific Two',
702                                                    :parent => TimeEntryActivity.find(:last),
703                                                    :project => Project.find(1),
704                                                    :active => true
705                                                  })
706     assert project_activity_two.save
707
708     delete :reset_activities, :id => 1
709     assert_response :redirect
710     assert_redirected_to 'projects/ecookbook/settings/activities'
711
712     assert_nil TimeEntryActivity.find_by_id(project_activity.id)
713     assert_nil TimeEntryActivity.find_by_id(project_activity_two.id)
714   end
715   
716   def test_reset_activities_should_reassign_time_entries_back_to_the_system_activity
717     @request.session[:user_id] = 2 # manager
718     project_activity = TimeEntryActivity.new({
719                                                :name => 'Project Specific Design',
720                                                :parent => TimeEntryActivity.find(9),
721                                                :project => Project.find(1),
722                                                :active => true
723                                              })
724     assert project_activity.save
725     assert TimeEntry.update_all("activity_id = '#{project_activity.id}'", ["project_id = ? AND activity_id = ?", 1, 9])
726     assert 3, TimeEntry.find_all_by_activity_id_and_project_id(project_activity.id, 1).size
727     
728     delete :reset_activities, :id => 1
729     assert_response :redirect
730     assert_redirected_to 'projects/ecookbook/settings/activities'
731
732     assert_nil TimeEntryActivity.find_by_id(project_activity.id)
733     assert_equal 0, TimeEntry.find_all_by_activity_id_and_project_id(project_activity.id, 1).size, "TimeEntries still assigned to project specific activity"
734     assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(9, 1).size, "TimeEntries still assigned to project specific activity"
735   end
736   
737   def test_save_activities_routing
738     assert_routing({:method => :post, :path => 'projects/64/activities/save'},
739                    :controller => 'projects', :action => 'save_activities', :id => '64')
740   end
741
742   def test_save_activities_to_override_system_activities
743     @request.session[:user_id] = 2 # manager
744     billable_field = TimeEntryActivityCustomField.find_by_name("Billable")
745
746     post :save_activities, :id => 1, :enumerations => {
747       "9"=> {"parent_id"=>"9", "custom_field_values"=>{"7" => "1"}, "active"=>"0"}, # Design, De-activate
748       "10"=> {"parent_id"=>"10", "custom_field_values"=>{"7"=>"0"}, "active"=>"1"}, # Development, Change custom value
749       "14"=>{"parent_id"=>"14", "custom_field_values"=>{"7"=>"1"}, "active"=>"1"}, # Inactive Activity, Activate with custom value
750       "11"=>{"parent_id"=>"11", "custom_field_values"=>{"7"=>"1"}, "active"=>"1"} # QA, no changes
751     }
752
753     assert_response :redirect
754     assert_redirected_to 'projects/ecookbook/settings/activities'
755
756     # Created project specific activities...
757     project = Project.find('ecookbook')
758
759     # ... Design
760     design = project.time_entry_activities.find_by_name("Design")
761     assert design, "Project activity not found"
762
763     assert_equal 9, design.parent_id # Relate to the system activity
764     assert_not_equal design.parent.id, design.id # Different records
765     assert_equal design.parent.name, design.name # Same name
766     assert !design.active?
767
768     # ... Development
769     development = project.time_entry_activities.find_by_name("Development")
770     assert development, "Project activity not found"
771
772     assert_equal 10, development.parent_id # Relate to the system activity
773     assert_not_equal development.parent.id, development.id # Different records
774     assert_equal development.parent.name, development.name # Same name
775     assert development.active?
776     assert_equal "0", development.custom_value_for(billable_field).value
777
778     # ... Inactive Activity
779     previously_inactive = project.time_entry_activities.find_by_name("Inactive Activity")
780     assert previously_inactive, "Project activity not found"
781
782     assert_equal 14, previously_inactive.parent_id # Relate to the system activity
783     assert_not_equal previously_inactive.parent.id, previously_inactive.id # Different records
784     assert_equal previously_inactive.parent.name, previously_inactive.name # Same name
785     assert previously_inactive.active?
786     assert_equal "1", previously_inactive.custom_value_for(billable_field).value
787
788     # ... QA
789     assert_equal nil, project.time_entry_activities.find_by_name("QA"), "Custom QA activity created when it wasn't modified"
790   end
791
792   def test_save_activities_will_update_project_specific_activities
793     @request.session[:user_id] = 2 # manager
794
795     project_activity = TimeEntryActivity.new({
796                                                :name => 'Project Specific',
797                                                :parent => TimeEntryActivity.find(:first),
798                                                :project => Project.find(1),
799                                                :active => true
800                                              })
801     assert project_activity.save
802     project_activity_two = TimeEntryActivity.new({
803                                                    :name => 'Project Specific Two',
804                                                    :parent => TimeEntryActivity.find(:last),
805                                                    :project => Project.find(1),
806                                                    :active => true
807                                                  })
808     assert project_activity_two.save
809
810     
811     post :save_activities, :id => 1, :enumerations => {
812       project_activity.id => {"custom_field_values"=>{"7" => "1"}, "active"=>"0"}, # De-activate
813       project_activity_two.id => {"custom_field_values"=>{"7" => "1"}, "active"=>"0"} # De-activate
814     }
815
816     assert_response :redirect
817     assert_redirected_to 'projects/ecookbook/settings/activities'
818
819     # Created project specific activities...
820     project = Project.find('ecookbook')
821     assert_equal 2, project.time_entry_activities.count
822
823     activity_one = project.time_entry_activities.find_by_name(project_activity.name)
824     assert activity_one, "Project activity not found"
825     assert_equal project_activity.id, activity_one.id
826     assert !activity_one.active?
827
828     activity_two = project.time_entry_activities.find_by_name(project_activity_two.name)
829     assert activity_two, "Project activity not found"
830     assert_equal project_activity_two.id, activity_two.id
831     assert !activity_two.active?
832   end
833
834   def test_save_activities_when_creating_new_activities_will_convert_existing_data
835     assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(9, 1).size
836     
837     @request.session[:user_id] = 2 # manager
838     post :save_activities, :id => 1, :enumerations => {
839       "9"=> {"parent_id"=>"9", "custom_field_values"=>{"7" => "1"}, "active"=>"0"} # Design, De-activate
840     }
841     assert_response :redirect
842
843     # No more TimeEntries using the system activity
844     assert_equal 0, TimeEntry.find_all_by_activity_id_and_project_id(9, 1).size, "Time Entries still assigned to system activities"
845     # All TimeEntries using project activity
846     project_specific_activity = TimeEntryActivity.find_by_parent_id_and_project_id(9, 1)
847     assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(project_specific_activity.id, 1).size, "No Time Entries assigned to the project activity"
848   end
849
850   def test_save_activities_when_creating_new_activities_will_not_convert_existing_data_if_an_exception_is_raised
851     # TODO: Need to cause an exception on create but these tests
852     # aren't setup for mocking.  Just create a record now so the
853     # second one is a dupicate
854     parent = TimeEntryActivity.find(9)
855     TimeEntryActivity.create!({:name => parent.name, :project_id => 1, :position => parent.position, :active => true})
856     TimeEntry.create!({:project_id => 1, :hours => 1.0, :user => User.find(1), :issue_id => 3, :activity_id => 10, :spent_on => '2009-01-01'})
857
858     assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(9, 1).size
859     assert_equal 1, TimeEntry.find_all_by_activity_id_and_project_id(10, 1).size
860     
861     @request.session[:user_id] = 2 # manager
862     post :save_activities, :id => 1, :enumerations => {
863       "9"=> {"parent_id"=>"9", "custom_field_values"=>{"7" => "1"}, "active"=>"0"}, # Design
864       "10"=> {"parent_id"=>"10", "custom_field_values"=>{"7"=>"0"}, "active"=>"1"} # Development, Change custom value
865     }
866     assert_response :redirect
867
868     # TimeEntries shouldn't have been reassigned on the failed record
869     assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(9, 1).size, "Time Entries are not assigned to system activities"
870     # TimeEntries shouldn't have been reassigned on the saved record either
871     assert_equal 1, TimeEntry.find_all_by_activity_id_and_project_id(10, 1).size, "Time Entries are not assigned to system activities"
872   end
873
874   # A hook that is manually registered later
875   class ProjectBasedTemplate < Redmine::Hook::ViewListener
876     def view_layouts_base_html_head(context)
877       # Adds a project stylesheet
878       stylesheet_link_tag(context[:project].identifier) if context[:project]
879     end
880   end
881   # Don't use this hook now
882   Redmine::Hook.clear_listeners
883   
884   def test_hook_response
885     Redmine::Hook.add_listener(ProjectBasedTemplate)
886     get :show, :id => 1
887     assert_tag :tag => 'link', :attributes => {:href => '/stylesheets/ecookbook.css'},
888                                :parent => {:tag => 'head'}
889     
890     Redmine::Hook.clear_listeners
891   end
892 end