##// END OF EJS Templates
Rails4: replace deprecated Relation#update_all at ProjectEnumerationsControllerTest...
Toshi MARUYAMA -
r12357:6e8d8da1f3a2
parent child
Show More
@@ -1,234 +1,235
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 ProjectEnumerationsControllerTest < ActionController::TestCase
21 21 fixtures :projects, :trackers, :issue_statuses, :issues,
22 22 :enumerations, :users, :issue_categories,
23 23 :projects_trackers,
24 24 :roles,
25 25 :member_roles,
26 26 :members,
27 27 :enabled_modules,
28 28 :custom_fields, :custom_fields_projects,
29 29 :custom_fields_trackers, :custom_values,
30 30 :time_entries
31 31
32 32 self.use_transactional_fixtures = false
33 33
34 34 def setup
35 35 @request.session[:user_id] = nil
36 36 Setting.default_language = 'en'
37 37 end
38 38
39 39 def test_update_to_override_system_activities
40 40 @request.session[:user_id] = 2 # manager
41 41 billable_field = TimeEntryActivityCustomField.find_by_name("Billable")
42 42
43 43 put :update, :project_id => 1, :enumerations => {
44 44 "9"=> {"parent_id"=>"9", "custom_field_values"=>{"7" => "1"}, "active"=>"0"}, # Design, De-activate
45 45 "10"=> {"parent_id"=>"10", "custom_field_values"=>{"7"=>"0"}, "active"=>"1"}, # Development, Change custom value
46 46 "14"=>{"parent_id"=>"14", "custom_field_values"=>{"7"=>"1"}, "active"=>"1"}, # Inactive Activity, Activate with custom value
47 47 "11"=>{"parent_id"=>"11", "custom_field_values"=>{"7"=>"1"}, "active"=>"1"} # QA, no changes
48 48 }
49 49
50 50 assert_response :redirect
51 51 assert_redirected_to '/projects/ecookbook/settings/activities'
52 52
53 53 # Created project specific activities...
54 54 project = Project.find('ecookbook')
55 55
56 56 # ... Design
57 57 design = project.time_entry_activities.find_by_name("Design")
58 58 assert design, "Project activity not found"
59 59
60 60 assert_equal 9, design.parent_id # Relate to the system activity
61 61 assert_not_equal design.parent.id, design.id # Different records
62 62 assert_equal design.parent.name, design.name # Same name
63 63 assert !design.active?
64 64
65 65 # ... Development
66 66 development = project.time_entry_activities.find_by_name("Development")
67 67 assert development, "Project activity not found"
68 68
69 69 assert_equal 10, development.parent_id # Relate to the system activity
70 70 assert_not_equal development.parent.id, development.id # Different records
71 71 assert_equal development.parent.name, development.name # Same name
72 72 assert development.active?
73 73 assert_equal "0", development.custom_value_for(billable_field).value
74 74
75 75 # ... Inactive Activity
76 76 previously_inactive = project.time_entry_activities.find_by_name("Inactive Activity")
77 77 assert previously_inactive, "Project activity not found"
78 78
79 79 assert_equal 14, previously_inactive.parent_id # Relate to the system activity
80 80 assert_not_equal previously_inactive.parent.id, previously_inactive.id # Different records
81 81 assert_equal previously_inactive.parent.name, previously_inactive.name # Same name
82 82 assert previously_inactive.active?
83 83 assert_equal "1", previously_inactive.custom_value_for(billable_field).value
84 84
85 85 # ... QA
86 86 assert_equal nil, project.time_entry_activities.find_by_name("QA"), "Custom QA activity created when it wasn't modified"
87 87 end
88 88
89 89 def test_update_will_update_project_specific_activities
90 90 @request.session[:user_id] = 2 # manager
91 91
92 92 project_activity = TimeEntryActivity.new({
93 93 :name => 'Project Specific',
94 94 :parent => TimeEntryActivity.first,
95 95 :project => Project.find(1),
96 96 :active => true
97 97 })
98 98 assert project_activity.save
99 99 project_activity_two = TimeEntryActivity.new({
100 100 :name => 'Project Specific Two',
101 101 :parent => TimeEntryActivity.last,
102 102 :project => Project.find(1),
103 103 :active => true
104 104 })
105 105 assert project_activity_two.save
106 106
107 107
108 108 put :update, :project_id => 1, :enumerations => {
109 109 project_activity.id => {"custom_field_values"=>{"7" => "1"}, "active"=>"0"}, # De-activate
110 110 project_activity_two.id => {"custom_field_values"=>{"7" => "1"}, "active"=>"0"} # De-activate
111 111 }
112 112
113 113 assert_response :redirect
114 114 assert_redirected_to '/projects/ecookbook/settings/activities'
115 115
116 116 # Created project specific activities...
117 117 project = Project.find('ecookbook')
118 118 assert_equal 2, project.time_entry_activities.count
119 119
120 120 activity_one = project.time_entry_activities.find_by_name(project_activity.name)
121 121 assert activity_one, "Project activity not found"
122 122 assert_equal project_activity.id, activity_one.id
123 123 assert !activity_one.active?
124 124
125 125 activity_two = project.time_entry_activities.find_by_name(project_activity_two.name)
126 126 assert activity_two, "Project activity not found"
127 127 assert_equal project_activity_two.id, activity_two.id
128 128 assert !activity_two.active?
129 129 end
130 130
131 131 def test_update_when_creating_new_activities_will_convert_existing_data
132 132 assert_equal 3, TimeEntry.where(:activity_id => 9, :project_id => 1).count
133 133
134 134 @request.session[:user_id] = 2 # manager
135 135 put :update, :project_id => 1, :enumerations => {
136 136 "9"=> {"parent_id"=>"9", "custom_field_values"=>{"7" => "1"}, "active"=>"0"} # Design, De-activate
137 137 }
138 138 assert_response :redirect
139 139
140 140 # No more TimeEntries using the system activity
141 141 assert_equal 0, TimeEntry.where(:activity_id => 9, :project_id => 1).count,
142 142 "Time Entries still assigned to system activities"
143 143 # All TimeEntries using project activity
144 144 project_specific_activity = TimeEntryActivity.find_by_parent_id_and_project_id(9, 1)
145 145 assert_equal 3, TimeEntry.where(:activity_id => project_specific_activity.id,
146 146 :project_id => 1).count
147 147 "No Time Entries assigned to the project activity"
148 148 end
149 149
150 150 def test_update_when_creating_new_activities_will_not_convert_existing_data_if_an_exception_is_raised
151 151 # TODO: Need to cause an exception on create but these tests
152 152 # aren't setup for mocking. Just create a record now so the
153 153 # second one is a dupicate
154 154 parent = TimeEntryActivity.find(9)
155 155 TimeEntryActivity.create!({:name => parent.name, :project_id => 1,
156 156 :position => parent.position, :active => true})
157 157 TimeEntry.create!({:project_id => 1, :hours => 1.0, :user => User.find(1),
158 158 :issue_id => 3, :activity_id => 10, :spent_on => '2009-01-01'})
159 159 assert_equal 3, TimeEntry.where(:activity_id => 9, :project_id => 1).count
160 160 assert_equal 1, TimeEntry.where(:activity_id => 10, :project_id => 1).count
161 161
162 162 @request.session[:user_id] = 2 # manager
163 163 put :update, :project_id => 1, :enumerations => {
164 164 # Design
165 165 "9"=> {"parent_id"=>"9", "custom_field_values"=>{"7" => "1"}, "active"=>"0"},
166 166 # Development, Change custom value
167 167 "10"=> {"parent_id"=>"10", "custom_field_values"=>{"7"=>"0"}, "active"=>"1"}
168 168 }
169 169 assert_response :redirect
170 170
171 171 # TimeEntries shouldn't have been reassigned on the failed record
172 172 assert_equal 3, TimeEntry.where(:activity_id => 9,
173 173 :project_id => 1).count
174 174 "Time Entries are not assigned to system activities"
175 175 # TimeEntries shouldn't have been reassigned on the saved record either
176 176 assert_equal 1, TimeEntry.where(:activity_id => 10,
177 177 :project_id => 1).count
178 178 "Time Entries are not assigned to system activities"
179 179 end
180 180
181 181 def test_destroy
182 182 @request.session[:user_id] = 2 # manager
183 183 project_activity = TimeEntryActivity.new({
184 184 :name => 'Project Specific',
185 185 :parent => TimeEntryActivity.first,
186 186 :project => Project.find(1),
187 187 :active => true
188 188 })
189 189 assert project_activity.save
190 190 project_activity_two = TimeEntryActivity.new({
191 191 :name => 'Project Specific Two',
192 192 :parent => TimeEntryActivity.last,
193 193 :project => Project.find(1),
194 194 :active => true
195 195 })
196 196 assert project_activity_two.save
197 197
198 198 delete :destroy, :project_id => 1
199 199 assert_response :redirect
200 200 assert_redirected_to '/projects/ecookbook/settings/activities'
201 201
202 202 assert_nil TimeEntryActivity.find_by_id(project_activity.id)
203 203 assert_nil TimeEntryActivity.find_by_id(project_activity_two.id)
204 204 end
205 205
206 206 def test_destroy_should_reassign_time_entries_back_to_the_system_activity
207 207 @request.session[:user_id] = 2 # manager
208 208 project_activity = TimeEntryActivity.new({
209 209 :name => 'Project Specific Design',
210 210 :parent => TimeEntryActivity.find(9),
211 211 :project => Project.find(1),
212 212 :active => true
213 213 })
214 214 assert project_activity.save
215 assert TimeEntry.update_all("activity_id = '#{project_activity.id}'", ["project_id = ? AND activity_id = ?", 1, 9])
215 assert TimeEntry.where(["project_id = ? AND activity_id = ?", 1, 9]).
216 update_all("activity_id = '#{project_activity.id}'")
216 217 assert_equal 3, TimeEntry.where(:activity_id => project_activity.id,
217 218 :project_id => 1).count
218 219 delete :destroy, :project_id => 1
219 220 assert_response :redirect
220 221 assert_redirected_to '/projects/ecookbook/settings/activities'
221 222
222 223 assert_nil TimeEntryActivity.find_by_id(project_activity.id)
223 224 assert_equal 0, TimeEntry.where(
224 225 :activity_id => project_activity.id,
225 226 :project_id => 1
226 227 ).count,
227 228 "TimeEntries still assigned to project specific activity"
228 229 assert_equal 3, TimeEntry.where(
229 230 :activity_id => 9,
230 231 :project_id => 1
231 232 ).count,
232 233 "TimeEntries still assigned to project specific activity"
233 234 end
234 235 end
General Comments 0
You need to be logged in to leave comments. Login now