@@ -1,330 +1,329 | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 |
# |
|
|
3 |
# Copyright (C) 2006-20 |
|
|
2 | # Redmine - project management software | |
|
3 | # Copyright (C) 2006-2011 Jean-Philippe Lang | |
|
4 | 4 | # |
|
5 | 5 | # This program is free software; you can redistribute it and/or |
|
6 | 6 | # modify it under the terms of the GNU General Public License |
|
7 | 7 | # as published by the Free Software Foundation; either version 2 |
|
8 | 8 | # of the License, or (at your option) any later version. |
|
9 | 9 |
# |
|
10 | 10 | # This program is distributed in the hope that it will be useful, |
|
11 | 11 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 | 12 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
13 | 13 | # GNU General Public License for more details. |
|
14 | 14 |
# |
|
15 | 15 | # You should have received a copy of the GNU General Public License |
|
16 | 16 | # along with this program; if not, write to the Free Software |
|
17 | 17 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
18 | 18 | |
|
19 | 19 | require File.expand_path('../../test_helper', __FILE__) |
|
20 | 20 | require 'timelog_controller' |
|
21 | 21 | |
|
22 | 22 | # Re-raise errors caught by the controller. |
|
23 | 23 | class TimelogController; def rescue_action(e) raise e end; end |
|
24 | 24 | |
|
25 | 25 | class TimelogControllerTest < ActionController::TestCase |
|
26 | 26 | fixtures :projects, :enabled_modules, :roles, :members, :member_roles, :issues, :time_entries, :users, :trackers, :enumerations, :issue_statuses, :custom_fields, :custom_values |
|
27 | 27 | |
|
28 | 28 | def setup |
|
29 | 29 | @controller = TimelogController.new |
|
30 | 30 | @request = ActionController::TestRequest.new |
|
31 | 31 | @response = ActionController::TestResponse.new |
|
32 | 32 | end |
|
33 | 33 | |
|
34 | 34 | def test_get_new |
|
35 | 35 | @request.session[:user_id] = 3 |
|
36 | 36 | get :new, :project_id => 1 |
|
37 | 37 | assert_response :success |
|
38 | 38 | assert_template 'edit' |
|
39 | 39 | # Default activity selected |
|
40 | 40 | assert_tag :tag => 'option', :attributes => { :selected => 'selected' }, |
|
41 | 41 | :content => 'Development' |
|
42 | 42 | end |
|
43 | 43 | |
|
44 | 44 | def test_get_new_should_only_show_active_time_entry_activities |
|
45 | 45 | @request.session[:user_id] = 3 |
|
46 | 46 | get :new, :project_id => 1 |
|
47 | 47 | assert_response :success |
|
48 | 48 | assert_template 'edit' |
|
49 | 49 | assert_no_tag :tag => 'option', :content => 'Inactive Activity' |
|
50 | ||
|
51 | 50 | end |
|
52 | 51 | |
|
53 | 52 | def test_get_edit_existing_time |
|
54 | 53 | @request.session[:user_id] = 2 |
|
55 | 54 | get :edit, :id => 2, :project_id => nil |
|
56 | 55 | assert_response :success |
|
57 | 56 | assert_template 'edit' |
|
58 | 57 | # Default activity selected |
|
59 | 58 | assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/time_entries/2' } |
|
60 | 59 | end |
|
61 | 60 | |
|
62 | 61 | def test_get_edit_with_an_existing_time_entry_with_inactive_activity |
|
63 | 62 | te = TimeEntry.find(1) |
|
64 | 63 | te.activity = TimeEntryActivity.find_by_name("Inactive Activity") |
|
65 | 64 | te.save! |
|
66 | 65 | |
|
67 | 66 | @request.session[:user_id] = 1 |
|
68 | 67 | get :edit, :project_id => 1, :id => 1 |
|
69 | 68 | assert_response :success |
|
70 | 69 | assert_template 'edit' |
|
71 | 70 | # Blank option since nothing is pre-selected |
|
72 | 71 | assert_tag :tag => 'option', :content => '--- Please select ---' |
|
73 | 72 | end |
|
74 | 73 | |
|
75 | 74 | def test_post_create |
|
76 | 75 | # TODO: should POST to issuesβ time log instead of project. change form |
|
77 | 76 | # and routing |
|
78 | 77 | @request.session[:user_id] = 3 |
|
79 | 78 | post :create, :project_id => 1, |
|
80 | 79 | :time_entry => {:comments => 'Some work on TimelogControllerTest', |
|
81 | 80 | # Not the default activity |
|
82 | 81 | :activity_id => '11', |
|
83 | 82 | :spent_on => '2008-03-14', |
|
84 | 83 | :issue_id => '1', |
|
85 | 84 | :hours => '7.3'} |
|
86 | 85 | assert_redirected_to :action => 'index', :project_id => 'ecookbook' |
|
87 | 86 | |
|
88 | 87 | i = Issue.find(1) |
|
89 | 88 | t = TimeEntry.find_by_comments('Some work on TimelogControllerTest') |
|
90 | 89 | assert_not_nil t |
|
91 | 90 | assert_equal 11, t.activity_id |
|
92 | 91 | assert_equal 7.3, t.hours |
|
93 | 92 | assert_equal 3, t.user_id |
|
94 | 93 | assert_equal i, t.issue |
|
95 | 94 | assert_equal i.project, t.project |
|
96 | 95 | end |
|
97 | 96 | |
|
98 | 97 | def test_post_create_with_blank_issue |
|
99 | 98 | # TODO: should POST to issuesβ time log instead of project. change form |
|
100 | 99 | # and routing |
|
101 | 100 | @request.session[:user_id] = 3 |
|
102 | 101 | post :create, :project_id => 1, |
|
103 | 102 | :time_entry => {:comments => 'Some work on TimelogControllerTest', |
|
104 | 103 | # Not the default activity |
|
105 | 104 | :activity_id => '11', |
|
106 | 105 | :issue_id => '', |
|
107 | 106 | :spent_on => '2008-03-14', |
|
108 | 107 | :hours => '7.3'} |
|
109 | 108 | assert_redirected_to :action => 'index', :project_id => 'ecookbook' |
|
110 | 109 | |
|
111 | 110 | t = TimeEntry.find_by_comments('Some work on TimelogControllerTest') |
|
112 | 111 | assert_not_nil t |
|
113 | 112 | assert_equal 11, t.activity_id |
|
114 | 113 | assert_equal 7.3, t.hours |
|
115 | 114 | assert_equal 3, t.user_id |
|
116 | 115 | end |
|
117 | 116 | |
|
118 | 117 | def test_update |
|
119 | 118 | entry = TimeEntry.find(1) |
|
120 | 119 | assert_equal 1, entry.issue_id |
|
121 | 120 | assert_equal 2, entry.user_id |
|
122 | 121 | |
|
123 | 122 | @request.session[:user_id] = 1 |
|
124 | 123 | put :update, :id => 1, |
|
125 | 124 | :time_entry => {:issue_id => '2', |
|
126 | 125 | :hours => '8'} |
|
127 | 126 | assert_redirected_to :action => 'index', :project_id => 'ecookbook' |
|
128 | 127 | entry.reload |
|
129 | 128 | |
|
130 | 129 | assert_equal 8, entry.hours |
|
131 | 130 | assert_equal 2, entry.issue_id |
|
132 | 131 | assert_equal 2, entry.user_id |
|
133 | 132 | end |
|
134 | 133 | |
|
135 | 134 | def test_get_bulk_edit |
|
136 | 135 | @request.session[:user_id] = 2 |
|
137 | 136 | get :bulk_edit, :ids => [1, 2] |
|
138 | 137 | assert_response :success |
|
139 | 138 | assert_template 'bulk_edit' |
|
140 | 139 | |
|
141 | 140 | # System wide custom field |
|
142 | 141 | assert_tag :select, :attributes => {:name => 'time_entry[custom_field_values][10]'} |
|
143 | 142 | end |
|
144 | 143 | |
|
145 | 144 | def test_get_bulk_edit_on_different_projects |
|
146 | 145 | @request.session[:user_id] = 2 |
|
147 | 146 | get :bulk_edit, :ids => [1, 2, 6] |
|
148 | 147 | assert_response :success |
|
149 | 148 | assert_template 'bulk_edit' |
|
150 | 149 | end |
|
151 | 150 | |
|
152 | 151 | def test_bulk_update |
|
153 | 152 | @request.session[:user_id] = 2 |
|
154 | 153 | # update time entry activity |
|
155 | 154 | post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9} |
|
156 | 155 | |
|
157 | 156 | assert_response 302 |
|
158 | 157 | # check that the issues were updated |
|
159 | 158 | assert_equal [9, 9], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.activity_id} |
|
160 | 159 | end |
|
161 | 160 | |
|
162 | 161 | def test_bulk_update_on_different_projects |
|
163 | 162 | @request.session[:user_id] = 2 |
|
164 | 163 | # update time entry activity |
|
165 | 164 | post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 } |
|
166 | 165 | |
|
167 | 166 | assert_response 302 |
|
168 | 167 | # check that the issues were updated |
|
169 | 168 | assert_equal [9, 9, 9], TimeEntry.find_all_by_id([1, 2, 4]).collect {|i| i.activity_id} |
|
170 | 169 | end |
|
171 | 170 | |
|
172 | 171 | def test_bulk_update_on_different_projects_without_rights |
|
173 | 172 | @request.session[:user_id] = 3 |
|
174 | 173 | user = User.find(3) |
|
175 | 174 | action = { :controller => "timelog", :action => "bulk_update" } |
|
176 | 175 | assert user.allowed_to?(action, TimeEntry.find(1).project) |
|
177 | 176 | assert ! user.allowed_to?(action, TimeEntry.find(5).project) |
|
178 | 177 | post :bulk_update, :ids => [1, 5], :time_entry => { :activity_id => 9 } |
|
179 | 178 | assert_response 403 |
|
180 | 179 | end |
|
181 | 180 | |
|
182 | 181 | def test_bulk_update_custom_field |
|
183 | 182 | @request.session[:user_id] = 2 |
|
184 | 183 | post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} } |
|
185 | 184 | |
|
186 | 185 | assert_response 302 |
|
187 | 186 | assert_equal ["0", "0"], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.custom_value_for(10).value} |
|
188 | 187 | end |
|
189 | 188 | |
|
190 | 189 | def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter |
|
191 | 190 | @request.session[:user_id] = 2 |
|
192 | 191 | post :bulk_update, :ids => [1,2], :back_url => '/time_entries' |
|
193 | 192 | |
|
194 | 193 | assert_response :redirect |
|
195 | 194 | assert_redirected_to '/time_entries' |
|
196 | 195 | end |
|
197 | 196 | |
|
198 | 197 | def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host |
|
199 | 198 | @request.session[:user_id] = 2 |
|
200 | 199 | post :bulk_update, :ids => [1,2], :back_url => 'http://google.com' |
|
201 | 200 | |
|
202 | 201 | assert_response :redirect |
|
203 | 202 | assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier |
|
204 | 203 | end |
|
205 | 204 | |
|
206 | 205 | def test_destroy |
|
207 | 206 | @request.session[:user_id] = 2 |
|
208 | 207 | delete :destroy, :id => 1 |
|
209 | 208 | assert_redirected_to :action => 'index', :project_id => 'ecookbook' |
|
210 | 209 | assert_equal I18n.t(:notice_successful_delete), flash[:notice] |
|
211 | 210 | assert_nil TimeEntry.find_by_id(1) |
|
212 | 211 | end |
|
213 | 212 | |
|
214 | 213 | def test_destroy_should_fail |
|
215 | 214 | # simulate that this fails (e.g. due to a plugin), see #5700 |
|
216 | 215 | TimeEntry.any_instance.expects(:destroy).returns(false) |
|
217 | 216 | |
|
218 | 217 | @request.session[:user_id] = 2 |
|
219 | 218 | delete :destroy, :id => 1 |
|
220 | 219 | assert_redirected_to :action => 'index', :project_id => 'ecookbook' |
|
221 | 220 | assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error] |
|
222 | 221 | assert_not_nil TimeEntry.find_by_id(1) |
|
223 | 222 | end |
|
224 | 223 | |
|
225 | 224 | def test_index_all_projects |
|
226 | 225 | get :index |
|
227 | 226 | assert_response :success |
|
228 | 227 | assert_template 'index' |
|
229 | 228 | assert_not_nil assigns(:total_hours) |
|
230 | 229 | assert_equal "162.90", "%.2f" % assigns(:total_hours) |
|
231 | 230 | assert_tag :form, |
|
232 | 231 | :attributes => {:action => "/time_entries", :id => 'query_form'} |
|
233 | 232 | end |
|
234 | 233 | |
|
235 | 234 | def test_index_at_project_level |
|
236 | 235 | get :index, :project_id => 'ecookbook' |
|
237 | 236 | assert_response :success |
|
238 | 237 | assert_template 'index' |
|
239 | 238 | assert_not_nil assigns(:entries) |
|
240 | 239 | assert_equal 4, assigns(:entries).size |
|
241 | 240 | # project and subproject |
|
242 | 241 | assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort |
|
243 | 242 | assert_not_nil assigns(:total_hours) |
|
244 | 243 | assert_equal "162.90", "%.2f" % assigns(:total_hours) |
|
245 | 244 | # display all time by default |
|
246 | 245 | assert_equal '2007-03-12'.to_date, assigns(:from) |
|
247 | 246 | assert_equal '2007-04-22'.to_date, assigns(:to) |
|
248 | 247 | assert_tag :form, |
|
249 | 248 | :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'} |
|
250 | 249 | end |
|
251 | 250 | |
|
252 | 251 | def test_index_at_project_level_with_date_range |
|
253 | 252 | get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30' |
|
254 | 253 | assert_response :success |
|
255 | 254 | assert_template 'index' |
|
256 | 255 | assert_not_nil assigns(:entries) |
|
257 | 256 | assert_equal 3, assigns(:entries).size |
|
258 | 257 | assert_not_nil assigns(:total_hours) |
|
259 | 258 | assert_equal "12.90", "%.2f" % assigns(:total_hours) |
|
260 | 259 | assert_equal '2007-03-20'.to_date, assigns(:from) |
|
261 | 260 | assert_equal '2007-04-30'.to_date, assigns(:to) |
|
262 | 261 | assert_tag :form, |
|
263 | 262 | :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'} |
|
264 | 263 | end |
|
265 | 264 | |
|
266 | 265 | def test_index_at_project_level_with_period |
|
267 | 266 | get :index, :project_id => 'ecookbook', :period => '7_days' |
|
268 | 267 | assert_response :success |
|
269 | 268 | assert_template 'index' |
|
270 | 269 | assert_not_nil assigns(:entries) |
|
271 | 270 | assert_not_nil assigns(:total_hours) |
|
272 | 271 | assert_equal Date.today - 7, assigns(:from) |
|
273 | 272 | assert_equal Date.today, assigns(:to) |
|
274 | 273 | assert_tag :form, |
|
275 | 274 | :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'} |
|
276 | 275 | end |
|
277 | 276 | |
|
278 | 277 | def test_index_one_day |
|
279 | 278 | get :index, :project_id => 'ecookbook', :from => "2007-03-23", :to => "2007-03-23" |
|
280 | 279 | assert_response :success |
|
281 | 280 | assert_template 'index' |
|
282 | 281 | assert_not_nil assigns(:total_hours) |
|
283 | 282 | assert_equal "4.25", "%.2f" % assigns(:total_hours) |
|
284 | 283 | assert_tag :form, |
|
285 | 284 | :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'} |
|
286 | 285 | end |
|
287 | 286 | |
|
288 | 287 | def test_index_at_issue_level |
|
289 | 288 | get :index, :issue_id => 1 |
|
290 | 289 | assert_response :success |
|
291 | 290 | assert_template 'index' |
|
292 | 291 | assert_not_nil assigns(:entries) |
|
293 | 292 | assert_equal 2, assigns(:entries).size |
|
294 | 293 | assert_not_nil assigns(:total_hours) |
|
295 | 294 | assert_equal 154.25, assigns(:total_hours) |
|
296 | 295 | # display all time based on what's been logged |
|
297 | 296 | assert_equal '2007-03-12'.to_date, assigns(:from) |
|
298 | 297 | assert_equal '2007-04-22'.to_date, assigns(:to) |
|
299 | 298 | # TODO: remove /projects/:project_id/issues/:issue_id/time_entries routes |
|
300 | 299 | # to use /issues/:issue_id/time_entries |
|
301 | 300 | assert_tag :form, |
|
302 | 301 | :attributes => {:action => "/projects/ecookbook/issues/1/time_entries", :id => 'query_form'} |
|
303 | 302 | end |
|
304 | 303 | |
|
305 | 304 | def test_index_atom_feed |
|
306 | 305 | get :index, :project_id => 1, :format => 'atom' |
|
307 | 306 | assert_response :success |
|
308 | 307 | assert_equal 'application/atom+xml', @response.content_type |
|
309 | 308 | assert_not_nil assigns(:items) |
|
310 | 309 | assert assigns(:items).first.is_a?(TimeEntry) |
|
311 | 310 | end |
|
312 | 311 | |
|
313 | 312 | def test_index_all_projects_csv_export |
|
314 | 313 | Setting.date_format = '%m/%d/%Y' |
|
315 | 314 | get :index, :format => 'csv' |
|
316 | 315 | assert_response :success |
|
317 | 316 | assert_equal 'text/csv', @response.content_type |
|
318 | 317 | assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n") |
|
319 | 318 | assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n") |
|
320 | 319 | end |
|
321 | 320 | |
|
322 | 321 | def test_index_csv_export |
|
323 | 322 | Setting.date_format = '%m/%d/%Y' |
|
324 | 323 | get :index, :project_id => 1, :format => 'csv' |
|
325 | 324 | assert_response :success |
|
326 | 325 | assert_equal 'text/csv', @response.content_type |
|
327 | 326 | assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n") |
|
328 | 327 | assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n") |
|
329 | 328 | end |
|
330 | 329 | end |
General Comments 0
You need to be logged in to leave comments.
Login now