To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.

Statistics Download as Zip
| Branch: | Tag: | Revision:

root / test / functional / project_enumerations_controller_test.rb @ 441:cbce1fd3b1b7

History | View | Annotate | Download (9.32 KB)

1
require File.expand_path('../../test_helper', __FILE__)
2

    
3
class ProjectEnumerationsControllerTest < ActionController::TestCase
4
  fixtures :all
5
  
6
  def setup
7
    @request.session[:user_id] = nil
8
    Setting.default_language = 'en'
9
  end
10

    
11
  def test_update_to_override_system_activities
12
    @request.session[:user_id] = 2 # manager
13
    billable_field = TimeEntryActivityCustomField.find_by_name("Billable")
14

    
15
    put :update, :project_id => 1, :enumerations => {
16
      "9"=> {"parent_id"=>"9", "custom_field_values"=>{"7" => "1"}, "active"=>"0"}, # Design, De-activate
17
      "10"=> {"parent_id"=>"10", "custom_field_values"=>{"7"=>"0"}, "active"=>"1"}, # Development, Change custom value
18
      "14"=>{"parent_id"=>"14", "custom_field_values"=>{"7"=>"1"}, "active"=>"1"}, # Inactive Activity, Activate with custom value
19
      "11"=>{"parent_id"=>"11", "custom_field_values"=>{"7"=>"1"}, "active"=>"1"} # QA, no changes
20
    }
21

    
22
    assert_response :redirect
23
    assert_redirected_to '/projects/ecookbook/settings/activities'
24

    
25
    # Created project specific activities...
26
    project = Project.find('ecookbook')
27

    
28
    # ... Design
29
    design = project.time_entry_activities.find_by_name("Design")
30
    assert design, "Project activity not found"
31

    
32
    assert_equal 9, design.parent_id # Relate to the system activity
33
    assert_not_equal design.parent.id, design.id # Different records
34
    assert_equal design.parent.name, design.name # Same name
35
    assert !design.active?
36

    
37
    # ... Development
38
    development = project.time_entry_activities.find_by_name("Development")
39
    assert development, "Project activity not found"
40

    
41
    assert_equal 10, development.parent_id # Relate to the system activity
42
    assert_not_equal development.parent.id, development.id # Different records
43
    assert_equal development.parent.name, development.name # Same name
44
    assert development.active?
45
    assert_equal "0", development.custom_value_for(billable_field).value
46

    
47
    # ... Inactive Activity
48
    previously_inactive = project.time_entry_activities.find_by_name("Inactive Activity")
49
    assert previously_inactive, "Project activity not found"
50

    
51
    assert_equal 14, previously_inactive.parent_id # Relate to the system activity
52
    assert_not_equal previously_inactive.parent.id, previously_inactive.id # Different records
53
    assert_equal previously_inactive.parent.name, previously_inactive.name # Same name
54
    assert previously_inactive.active?
55
    assert_equal "1", previously_inactive.custom_value_for(billable_field).value
56

    
57
    # ... QA
58
    assert_equal nil, project.time_entry_activities.find_by_name("QA"), "Custom QA activity created when it wasn't modified"
59
  end
60

    
61
  def test_update_will_update_project_specific_activities
62
    @request.session[:user_id] = 2 # manager
63

    
64
    project_activity = TimeEntryActivity.new({
65
                                               :name => 'Project Specific',
66
                                               :parent => TimeEntryActivity.find(:first),
67
                                               :project => Project.find(1),
68
                                               :active => true
69
                                             })
70
    assert project_activity.save
71
    project_activity_two = TimeEntryActivity.new({
72
                                                   :name => 'Project Specific Two',
73
                                                   :parent => TimeEntryActivity.find(:last),
74
                                                   :project => Project.find(1),
75
                                                   :active => true
76
                                                 })
77
    assert project_activity_two.save
78

    
79
    
80
    put :update, :project_id => 1, :enumerations => {
81
      project_activity.id => {"custom_field_values"=>{"7" => "1"}, "active"=>"0"}, # De-activate
82
      project_activity_two.id => {"custom_field_values"=>{"7" => "1"}, "active"=>"0"} # De-activate
83
    }
84

    
85
    assert_response :redirect
86
    assert_redirected_to '/projects/ecookbook/settings/activities'
87

    
88
    # Created project specific activities...
89
    project = Project.find('ecookbook')
90
    assert_equal 2, project.time_entry_activities.count
91

    
92
    activity_one = project.time_entry_activities.find_by_name(project_activity.name)
93
    assert activity_one, "Project activity not found"
94
    assert_equal project_activity.id, activity_one.id
95
    assert !activity_one.active?
96

    
97
    activity_two = project.time_entry_activities.find_by_name(project_activity_two.name)
98
    assert activity_two, "Project activity not found"
99
    assert_equal project_activity_two.id, activity_two.id
100
    assert !activity_two.active?
101
  end
102

    
103
  def test_update_when_creating_new_activities_will_convert_existing_data
104
    assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(9, 1).size
105
    
106
    @request.session[:user_id] = 2 # manager
107
    put :update, :project_id => 1, :enumerations => {
108
      "9"=> {"parent_id"=>"9", "custom_field_values"=>{"7" => "1"}, "active"=>"0"} # Design, De-activate
109
    }
110
    assert_response :redirect
111

    
112
    # No more TimeEntries using the system activity
113
    assert_equal 0, TimeEntry.find_all_by_activity_id_and_project_id(9, 1).size, "Time Entries still assigned to system activities"
114
    # All TimeEntries using project activity
115
    project_specific_activity = TimeEntryActivity.find_by_parent_id_and_project_id(9, 1)
116
    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"
117
  end
118

    
119
  def test_update_when_creating_new_activities_will_not_convert_existing_data_if_an_exception_is_raised
120
    # TODO: Need to cause an exception on create but these tests
121
    # aren't setup for mocking.  Just create a record now so the
122
    # second one is a dupicate
123
    parent = TimeEntryActivity.find(9)
124
    TimeEntryActivity.create!({:name => parent.name, :project_id => 1, :position => parent.position, :active => true})
125
    TimeEntry.create!({:project_id => 1, :hours => 1.0, :user => User.find(1), :issue_id => 3, :activity_id => 10, :spent_on => '2009-01-01'})
126

    
127
    assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(9, 1).size
128
    assert_equal 1, TimeEntry.find_all_by_activity_id_and_project_id(10, 1).size
129
    
130
    @request.session[:user_id] = 2 # manager
131
    put :update, :project_id => 1, :enumerations => {
132
      "9"=> {"parent_id"=>"9", "custom_field_values"=>{"7" => "1"}, "active"=>"0"}, # Design
133
      "10"=> {"parent_id"=>"10", "custom_field_values"=>{"7"=>"0"}, "active"=>"1"} # Development, Change custom value
134
    }
135
    assert_response :redirect
136

    
137
    # TimeEntries shouldn't have been reassigned on the failed record
138
    assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(9, 1).size, "Time Entries are not assigned to system activities"
139
    # TimeEntries shouldn't have been reassigned on the saved record either
140
    assert_equal 1, TimeEntry.find_all_by_activity_id_and_project_id(10, 1).size, "Time Entries are not assigned to system activities"
141
  end
142

    
143
  def test_destroy
144
    @request.session[:user_id] = 2 # manager
145
    project_activity = TimeEntryActivity.new({
146
                                               :name => 'Project Specific',
147
                                               :parent => TimeEntryActivity.find(:first),
148
                                               :project => Project.find(1),
149
                                               :active => true
150
                                             })
151
    assert project_activity.save
152
    project_activity_two = TimeEntryActivity.new({
153
                                                   :name => 'Project Specific Two',
154
                                                   :parent => TimeEntryActivity.find(:last),
155
                                                   :project => Project.find(1),
156
                                                   :active => true
157
                                                 })
158
    assert project_activity_two.save
159

    
160
    delete :destroy, :project_id => 1
161
    assert_response :redirect
162
    assert_redirected_to '/projects/ecookbook/settings/activities'
163

    
164
    assert_nil TimeEntryActivity.find_by_id(project_activity.id)
165
    assert_nil TimeEntryActivity.find_by_id(project_activity_two.id)
166
  end
167
  
168
  def test_destroy_should_reassign_time_entries_back_to_the_system_activity
169
    @request.session[:user_id] = 2 # manager
170
    project_activity = TimeEntryActivity.new({
171
                                               :name => 'Project Specific Design',
172
                                               :parent => TimeEntryActivity.find(9),
173
                                               :project => Project.find(1),
174
                                               :active => true
175
                                             })
176
    assert project_activity.save
177
    assert TimeEntry.update_all("activity_id = '#{project_activity.id}'", ["project_id = ? AND activity_id = ?", 1, 9])
178
    assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(project_activity.id, 1).size
179
    
180
    delete :destroy, :project_id => 1
181
    assert_response :redirect
182
    assert_redirected_to '/projects/ecookbook/settings/activities'
183

    
184
    assert_nil TimeEntryActivity.find_by_id(project_activity.id)
185
    assert_equal 0, TimeEntry.find_all_by_activity_id_and_project_id(project_activity.id, 1).size, "TimeEntries still assigned to project specific activity"
186
    assert_equal 3, TimeEntry.find_all_by_activity_id_and_project_id(9, 1).size, "TimeEntries still assigned to project specific activity"
187
  end
188

    
189
end