##// END OF EJS Templates
Additional tests for TimelogController....
Jean-Philippe Lang -
r9306:f79961f1c533
parent child
Show More
@@ -1,730 +1,742
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 # Redmine - project management software
2 # Redmine - project management software
3 # Copyright (C) 2006-2011 Jean-Philippe Lang
3 # Copyright (C) 2006-2011 Jean-Philippe Lang
4 #
4 #
5 # This program is free software; you can redistribute it and/or
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
8 # of the License, or (at your option) any later version.
9 #
9 #
10 # This program is distributed in the hope that it will be useful,
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
13 # GNU General Public License for more details.
14 #
14 #
15 # You should have received a copy of the GNU General Public License
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
18
19 require File.expand_path('../../test_helper', __FILE__)
19 require File.expand_path('../../test_helper', __FILE__)
20 require 'timelog_controller'
20 require 'timelog_controller'
21
21
22 # Re-raise errors caught by the controller.
22 # Re-raise errors caught by the controller.
23 class TimelogController; def rescue_action(e) raise e end; end
23 class TimelogController; def rescue_action(e) raise e end; end
24
24
25 class TimelogControllerTest < ActionController::TestCase
25 class TimelogControllerTest < ActionController::TestCase
26 fixtures :projects, :enabled_modules, :roles, :members,
26 fixtures :projects, :enabled_modules, :roles, :members,
27 :member_roles, :issues, :time_entries, :users,
27 :member_roles, :issues, :time_entries, :users,
28 :trackers, :enumerations, :issue_statuses,
28 :trackers, :enumerations, :issue_statuses,
29 :custom_fields, :custom_values
29 :custom_fields, :custom_values
30
30
31 include Redmine::I18n
31 include Redmine::I18n
32
32
33 def setup
33 def setup
34 @controller = TimelogController.new
34 @controller = TimelogController.new
35 @request = ActionController::TestRequest.new
35 @request = ActionController::TestRequest.new
36 @response = ActionController::TestResponse.new
36 @response = ActionController::TestResponse.new
37 end
37 end
38
38
39 def test_get_new
39 def test_get_new
40 @request.session[:user_id] = 3
40 @request.session[:user_id] = 3
41 get :new, :project_id => 1
41 get :new, :project_id => 1
42 assert_response :success
42 assert_response :success
43 assert_template 'new'
43 assert_template 'new'
44 # Default activity selected
44 # Default activity selected
45 assert_tag :tag => 'option', :attributes => { :selected => 'selected' },
45 assert_tag :tag => 'option', :attributes => { :selected => 'selected' },
46 :content => 'Development'
46 :content => 'Development'
47 end
47 end
48
48
49 def test_get_new_should_only_show_active_time_entry_activities
49 def test_get_new_should_only_show_active_time_entry_activities
50 @request.session[:user_id] = 3
50 @request.session[:user_id] = 3
51 get :new, :project_id => 1
51 get :new, :project_id => 1
52 assert_response :success
52 assert_response :success
53 assert_template 'new'
53 assert_template 'new'
54 assert_no_tag 'select', :attributes => {:name => 'time_entry[project_id]'}
54 assert_no_tag 'select', :attributes => {:name => 'time_entry[project_id]'}
55 assert_no_tag 'option', :content => 'Inactive Activity'
55 assert_no_tag 'option', :content => 'Inactive Activity'
56 end
56 end
57
57
58 def test_new_without_project
58 def test_new_without_project
59 @request.session[:user_id] = 3
59 @request.session[:user_id] = 3
60 get :new
60 get :new
61 assert_response :success
61 assert_response :success
62 assert_template 'new'
62 assert_template 'new'
63 assert_tag 'select', :attributes => {:name => 'time_entry[project_id]'}
63 assert_tag 'select', :attributes => {:name => 'time_entry[project_id]'}
64 end
64 end
65
65
66 def test_new_without_project_should_deny_without_permission
66 def test_new_without_project_should_deny_without_permission
67 Role.all.each {|role| role.remove_permission! :log_time}
67 Role.all.each {|role| role.remove_permission! :log_time}
68 @request.session[:user_id] = 3
68 @request.session[:user_id] = 3
69
69
70 get :new
70 get :new
71 assert_response 403
71 assert_response 403
72 end
72 end
73
73
74 def test_get_edit_existing_time
74 def test_get_edit_existing_time
75 @request.session[:user_id] = 2
75 @request.session[:user_id] = 2
76 get :edit, :id => 2, :project_id => nil
76 get :edit, :id => 2, :project_id => nil
77 assert_response :success
77 assert_response :success
78 assert_template 'edit'
78 assert_template 'edit'
79 # Default activity selected
79 # Default activity selected
80 assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/time_entries/2' }
80 assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/time_entries/2' }
81 end
81 end
82
82
83 def test_get_edit_with_an_existing_time_entry_with_inactive_activity
83 def test_get_edit_with_an_existing_time_entry_with_inactive_activity
84 te = TimeEntry.find(1)
84 te = TimeEntry.find(1)
85 te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
85 te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
86 te.save!
86 te.save!
87
87
88 @request.session[:user_id] = 1
88 @request.session[:user_id] = 1
89 get :edit, :project_id => 1, :id => 1
89 get :edit, :project_id => 1, :id => 1
90 assert_response :success
90 assert_response :success
91 assert_template 'edit'
91 assert_template 'edit'
92 # Blank option since nothing is pre-selected
92 # Blank option since nothing is pre-selected
93 assert_tag :tag => 'option', :content => '--- Please select ---'
93 assert_tag :tag => 'option', :content => '--- Please select ---'
94 end
94 end
95
95
96 def test_post_create
96 def test_post_create
97 # TODO: should POST to issues’ time log instead of project. change form
97 # TODO: should POST to issues’ time log instead of project. change form
98 # and routing
98 # and routing
99 @request.session[:user_id] = 3
99 @request.session[:user_id] = 3
100 post :create, :project_id => 1,
100 post :create, :project_id => 1,
101 :time_entry => {:comments => 'Some work on TimelogControllerTest',
101 :time_entry => {:comments => 'Some work on TimelogControllerTest',
102 # Not the default activity
102 # Not the default activity
103 :activity_id => '11',
103 :activity_id => '11',
104 :spent_on => '2008-03-14',
104 :spent_on => '2008-03-14',
105 :issue_id => '1',
105 :issue_id => '1',
106 :hours => '7.3'}
106 :hours => '7.3'}
107 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
107 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
108
108
109 i = Issue.find(1)
109 i = Issue.find(1)
110 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
110 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
111 assert_not_nil t
111 assert_not_nil t
112 assert_equal 11, t.activity_id
112 assert_equal 11, t.activity_id
113 assert_equal 7.3, t.hours
113 assert_equal 7.3, t.hours
114 assert_equal 3, t.user_id
114 assert_equal 3, t.user_id
115 assert_equal i, t.issue
115 assert_equal i, t.issue
116 assert_equal i.project, t.project
116 assert_equal i.project, t.project
117 end
117 end
118
118
119 def test_post_create_with_blank_issue
119 def test_post_create_with_blank_issue
120 # TODO: should POST to issues’ time log instead of project. change form
120 # TODO: should POST to issues’ time log instead of project. change form
121 # and routing
121 # and routing
122 @request.session[:user_id] = 3
122 @request.session[:user_id] = 3
123 post :create, :project_id => 1,
123 post :create, :project_id => 1,
124 :time_entry => {:comments => 'Some work on TimelogControllerTest',
124 :time_entry => {:comments => 'Some work on TimelogControllerTest',
125 # Not the default activity
125 # Not the default activity
126 :activity_id => '11',
126 :activity_id => '11',
127 :issue_id => '',
127 :issue_id => '',
128 :spent_on => '2008-03-14',
128 :spent_on => '2008-03-14',
129 :hours => '7.3'}
129 :hours => '7.3'}
130 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
130 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
131
131
132 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
132 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
133 assert_not_nil t
133 assert_not_nil t
134 assert_equal 11, t.activity_id
134 assert_equal 11, t.activity_id
135 assert_equal 7.3, t.hours
135 assert_equal 7.3, t.hours
136 assert_equal 3, t.user_id
136 assert_equal 3, t.user_id
137 end
137 end
138
138
139 def test_create_and_continue
139 def test_create_and_continue
140 @request.session[:user_id] = 2
140 @request.session[:user_id] = 2
141 post :create, :project_id => 1,
141 post :create, :project_id => 1,
142 :time_entry => {:activity_id => '11',
142 :time_entry => {:activity_id => '11',
143 :issue_id => '',
143 :issue_id => '',
144 :spent_on => '2008-03-14',
144 :spent_on => '2008-03-14',
145 :hours => '7.3'},
145 :hours => '7.3'},
146 :continue => '1'
146 :continue => '1'
147 assert_redirected_to '/projects/ecookbook/time_entries/new'
147 assert_redirected_to '/projects/ecookbook/time_entries/new'
148 end
148 end
149
149
150 def test_create_and_continue_with_issue_id
150 def test_create_and_continue_with_issue_id
151 @request.session[:user_id] = 2
151 @request.session[:user_id] = 2
152 post :create, :project_id => 1,
152 post :create, :project_id => 1,
153 :time_entry => {:activity_id => '11',
153 :time_entry => {:activity_id => '11',
154 :issue_id => '1',
154 :issue_id => '1',
155 :spent_on => '2008-03-14',
155 :spent_on => '2008-03-14',
156 :hours => '7.3'},
156 :hours => '7.3'},
157 :continue => '1'
157 :continue => '1'
158 assert_redirected_to '/projects/ecookbook/issues/1/time_entries/new'
158 assert_redirected_to '/projects/ecookbook/issues/1/time_entries/new'
159 end
159 end
160
160
161 def test_create_and_continue_without_project
161 def test_create_and_continue_without_project
162 @request.session[:user_id] = 2
162 @request.session[:user_id] = 2
163 post :create, :time_entry => {:project_id => '1',
163 post :create, :time_entry => {:project_id => '1',
164 :activity_id => '11',
164 :activity_id => '11',
165 :issue_id => '',
165 :issue_id => '',
166 :spent_on => '2008-03-14',
166 :spent_on => '2008-03-14',
167 :hours => '7.3'},
167 :hours => '7.3'},
168 :continue => '1'
168 :continue => '1'
169
169
170 assert_redirected_to '/time_entries/new'
170 assert_redirected_to '/time_entries/new'
171 end
171 end
172
172
173 def test_create_without_log_time_permission_should_be_denied
173 def test_create_without_log_time_permission_should_be_denied
174 @request.session[:user_id] = 2
174 @request.session[:user_id] = 2
175 Role.find_by_name('Manager').remove_permission! :log_time
175 Role.find_by_name('Manager').remove_permission! :log_time
176 post :create, :project_id => 1,
176 post :create, :project_id => 1,
177 :time_entry => {:activity_id => '11',
177 :time_entry => {:activity_id => '11',
178 :issue_id => '',
178 :issue_id => '',
179 :spent_on => '2008-03-14',
179 :spent_on => '2008-03-14',
180 :hours => '7.3'}
180 :hours => '7.3'}
181
181
182 assert_response 403
182 assert_response 403
183 end
183 end
184
184
185 def test_create_with_failure
185 def test_create_with_failure
186 @request.session[:user_id] = 2
186 @request.session[:user_id] = 2
187 post :create, :project_id => 1,
187 post :create, :project_id => 1,
188 :time_entry => {:activity_id => '',
188 :time_entry => {:activity_id => '',
189 :issue_id => '',
189 :issue_id => '',
190 :spent_on => '2008-03-14',
190 :spent_on => '2008-03-14',
191 :hours => '7.3'}
191 :hours => '7.3'}
192
192
193 assert_response :success
193 assert_response :success
194 assert_template 'new'
194 assert_template 'new'
195 end
195 end
196
196
197 def test_create_without_project
197 def test_create_without_project
198 @request.session[:user_id] = 2
198 @request.session[:user_id] = 2
199 assert_difference 'TimeEntry.count' do
199 assert_difference 'TimeEntry.count' do
200 post :create, :time_entry => {:project_id => '1',
200 post :create, :time_entry => {:project_id => '1',
201 :activity_id => '11',
201 :activity_id => '11',
202 :issue_id => '',
202 :issue_id => '',
203 :spent_on => '2008-03-14',
203 :spent_on => '2008-03-14',
204 :hours => '7.3'}
204 :hours => '7.3'}
205 end
205 end
206
206
207 assert_redirected_to '/projects/ecookbook/time_entries'
207 assert_redirected_to '/projects/ecookbook/time_entries'
208 time_entry = TimeEntry.first(:order => 'id DESC')
208 time_entry = TimeEntry.first(:order => 'id DESC')
209 assert_equal 1, time_entry.project_id
209 assert_equal 1, time_entry.project_id
210 end
210 end
211
211
212 def test_create_without_project_should_fail_with_issue_not_inside_project
212 def test_create_without_project_should_fail_with_issue_not_inside_project
213 @request.session[:user_id] = 2
213 @request.session[:user_id] = 2
214 assert_no_difference 'TimeEntry.count' do
214 assert_no_difference 'TimeEntry.count' do
215 post :create, :time_entry => {:project_id => '1',
215 post :create, :time_entry => {:project_id => '1',
216 :activity_id => '11',
216 :activity_id => '11',
217 :issue_id => '5',
217 :issue_id => '5',
218 :spent_on => '2008-03-14',
218 :spent_on => '2008-03-14',
219 :hours => '7.3'}
219 :hours => '7.3'}
220 end
220 end
221
221
222 assert_response :success
222 assert_response :success
223 assert assigns(:time_entry).errors[:issue_id].present?
223 assert assigns(:time_entry).errors[:issue_id].present?
224 end
224 end
225
225
226 def test_create_without_project_should_deny_without_permission
226 def test_create_without_project_should_deny_without_permission
227 @request.session[:user_id] = 2
227 @request.session[:user_id] = 2
228 Project.find(3).disable_module!(:time_tracking)
228 Project.find(3).disable_module!(:time_tracking)
229
229
230 assert_no_difference 'TimeEntry.count' do
230 assert_no_difference 'TimeEntry.count' do
231 post :create, :time_entry => {:project_id => '3',
231 post :create, :time_entry => {:project_id => '3',
232 :activity_id => '11',
232 :activity_id => '11',
233 :issue_id => '',
233 :issue_id => '',
234 :spent_on => '2008-03-14',
234 :spent_on => '2008-03-14',
235 :hours => '7.3'}
235 :hours => '7.3'}
236 end
236 end
237
237
238 assert_response 403
238 assert_response 403
239 end
239 end
240
240
241 def test_create_without_project_with_failure
241 def test_create_without_project_with_failure
242 @request.session[:user_id] = 2
242 @request.session[:user_id] = 2
243 assert_no_difference 'TimeEntry.count' do
243 assert_no_difference 'TimeEntry.count' do
244 post :create, :time_entry => {:project_id => '1',
244 post :create, :time_entry => {:project_id => '1',
245 :activity_id => '11',
245 :activity_id => '11',
246 :issue_id => '',
246 :issue_id => '',
247 :spent_on => '2008-03-14',
247 :spent_on => '2008-03-14',
248 :hours => ''}
248 :hours => ''}
249 end
249 end
250
250
251 assert_response :success
251 assert_response :success
252 assert_tag 'select', :attributes => {:name => 'time_entry[project_id]'},
252 assert_tag 'select', :attributes => {:name => 'time_entry[project_id]'},
253 :child => {:tag => 'option', :attributes => {:value => '1', :selected => 'selected'}}
253 :child => {:tag => 'option', :attributes => {:value => '1', :selected => 'selected'}}
254 end
254 end
255
255
256 def test_update
256 def test_update
257 entry = TimeEntry.find(1)
257 entry = TimeEntry.find(1)
258 assert_equal 1, entry.issue_id
258 assert_equal 1, entry.issue_id
259 assert_equal 2, entry.user_id
259 assert_equal 2, entry.user_id
260
260
261 @request.session[:user_id] = 1
261 @request.session[:user_id] = 1
262 put :update, :id => 1,
262 put :update, :id => 1,
263 :time_entry => {:issue_id => '2',
263 :time_entry => {:issue_id => '2',
264 :hours => '8'}
264 :hours => '8'}
265 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
265 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
266 entry.reload
266 entry.reload
267
267
268 assert_equal 8, entry.hours
268 assert_equal 8, entry.hours
269 assert_equal 2, entry.issue_id
269 assert_equal 2, entry.issue_id
270 assert_equal 2, entry.user_id
270 assert_equal 2, entry.user_id
271 end
271 end
272
272
273 def test_get_bulk_edit
273 def test_get_bulk_edit
274 @request.session[:user_id] = 2
274 @request.session[:user_id] = 2
275 get :bulk_edit, :ids => [1, 2]
275 get :bulk_edit, :ids => [1, 2]
276 assert_response :success
276 assert_response :success
277 assert_template 'bulk_edit'
277 assert_template 'bulk_edit'
278
278
279 # System wide custom field
279 # System wide custom field
280 assert_tag :select, :attributes => {:name => 'time_entry[custom_field_values][10]'}
280 assert_tag :select, :attributes => {:name => 'time_entry[custom_field_values][10]'}
281 end
281 end
282
282
283 def test_get_bulk_edit_on_different_projects
283 def test_get_bulk_edit_on_different_projects
284 @request.session[:user_id] = 2
284 @request.session[:user_id] = 2
285 get :bulk_edit, :ids => [1, 2, 6]
285 get :bulk_edit, :ids => [1, 2, 6]
286 assert_response :success
286 assert_response :success
287 assert_template 'bulk_edit'
287 assert_template 'bulk_edit'
288 end
288 end
289
289
290 def test_bulk_update
290 def test_bulk_update
291 @request.session[:user_id] = 2
291 @request.session[:user_id] = 2
292 # update time entry activity
292 # update time entry activity
293 post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9}
293 post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9}
294
294
295 assert_response 302
295 assert_response 302
296 # check that the issues were updated
296 # check that the issues were updated
297 assert_equal [9, 9], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.activity_id}
297 assert_equal [9, 9], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.activity_id}
298 end
298 end
299
299
300 def test_bulk_update_with_failure
300 def test_bulk_update_with_failure
301 @request.session[:user_id] = 2
301 @request.session[:user_id] = 2
302 post :bulk_update, :ids => [1, 2], :time_entry => { :hours => 'A'}
302 post :bulk_update, :ids => [1, 2], :time_entry => { :hours => 'A'}
303
303
304 assert_response 302
304 assert_response 302
305 assert_match /Failed to save 2 time entrie/, flash[:error]
305 assert_match /Failed to save 2 time entrie/, flash[:error]
306 end
306 end
307
307
308 def test_bulk_update_on_different_projects
308 def test_bulk_update_on_different_projects
309 @request.session[:user_id] = 2
309 @request.session[:user_id] = 2
310 # makes user a manager on the other project
310 # makes user a manager on the other project
311 Member.create!(:user_id => 2, :project_id => 3, :role_ids => [1])
311 Member.create!(:user_id => 2, :project_id => 3, :role_ids => [1])
312
312
313 # update time entry activity
313 # update time entry activity
314 post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 }
314 post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 }
315
315
316 assert_response 302
316 assert_response 302
317 # check that the issues were updated
317 # check that the issues were updated
318 assert_equal [9, 9, 9], TimeEntry.find_all_by_id([1, 2, 4]).collect {|i| i.activity_id}
318 assert_equal [9, 9, 9], TimeEntry.find_all_by_id([1, 2, 4]).collect {|i| i.activity_id}
319 end
319 end
320
320
321 def test_bulk_update_on_different_projects_without_rights
321 def test_bulk_update_on_different_projects_without_rights
322 @request.session[:user_id] = 3
322 @request.session[:user_id] = 3
323 user = User.find(3)
323 user = User.find(3)
324 action = { :controller => "timelog", :action => "bulk_update" }
324 action = { :controller => "timelog", :action => "bulk_update" }
325 assert user.allowed_to?(action, TimeEntry.find(1).project)
325 assert user.allowed_to?(action, TimeEntry.find(1).project)
326 assert ! user.allowed_to?(action, TimeEntry.find(5).project)
326 assert ! user.allowed_to?(action, TimeEntry.find(5).project)
327 post :bulk_update, :ids => [1, 5], :time_entry => { :activity_id => 9 }
327 post :bulk_update, :ids => [1, 5], :time_entry => { :activity_id => 9 }
328 assert_response 403
328 assert_response 403
329 end
329 end
330
330
331 def test_bulk_update_custom_field
331 def test_bulk_update_custom_field
332 @request.session[:user_id] = 2
332 @request.session[:user_id] = 2
333 post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} }
333 post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} }
334
334
335 assert_response 302
335 assert_response 302
336 assert_equal ["0", "0"], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.custom_value_for(10).value}
336 assert_equal ["0", "0"], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.custom_value_for(10).value}
337 end
337 end
338
338
339 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
339 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
340 @request.session[:user_id] = 2
340 @request.session[:user_id] = 2
341 post :bulk_update, :ids => [1,2], :back_url => '/time_entries'
341 post :bulk_update, :ids => [1,2], :back_url => '/time_entries'
342
342
343 assert_response :redirect
343 assert_response :redirect
344 assert_redirected_to '/time_entries'
344 assert_redirected_to '/time_entries'
345 end
345 end
346
346
347 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
347 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
348 @request.session[:user_id] = 2
348 @request.session[:user_id] = 2
349 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
349 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
350
350
351 assert_response :redirect
351 assert_response :redirect
352 assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier
352 assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier
353 end
353 end
354
354
355 def test_post_bulk_update_without_edit_permission_should_be_denied
355 def test_post_bulk_update_without_edit_permission_should_be_denied
356 @request.session[:user_id] = 2
356 @request.session[:user_id] = 2
357 Role.find_by_name('Manager').remove_permission! :edit_time_entries
357 Role.find_by_name('Manager').remove_permission! :edit_time_entries
358 post :bulk_update, :ids => [1,2]
358 post :bulk_update, :ids => [1,2]
359
359
360 assert_response 403
360 assert_response 403
361 end
361 end
362
362
363 def test_destroy
363 def test_destroy
364 @request.session[:user_id] = 2
364 @request.session[:user_id] = 2
365 delete :destroy, :id => 1
365 delete :destroy, :id => 1
366 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
366 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
367 assert_equal I18n.t(:notice_successful_delete), flash[:notice]
367 assert_equal I18n.t(:notice_successful_delete), flash[:notice]
368 assert_nil TimeEntry.find_by_id(1)
368 assert_nil TimeEntry.find_by_id(1)
369 end
369 end
370
370
371 def test_destroy_should_fail
371 def test_destroy_should_fail
372 # simulate that this fails (e.g. due to a plugin), see #5700
372 # simulate that this fails (e.g. due to a plugin), see #5700
373 TimeEntry.any_instance.expects(:destroy).returns(false)
373 TimeEntry.any_instance.expects(:destroy).returns(false)
374
374
375 @request.session[:user_id] = 2
375 @request.session[:user_id] = 2
376 delete :destroy, :id => 1
376 delete :destroy, :id => 1
377 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
377 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
378 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
378 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
379 assert_not_nil TimeEntry.find_by_id(1)
379 assert_not_nil TimeEntry.find_by_id(1)
380 end
380 end
381
381
382 def test_index_all_projects
382 def test_index_all_projects
383 get :index
383 get :index
384 assert_response :success
384 assert_response :success
385 assert_template 'index'
385 assert_template 'index'
386 assert_not_nil assigns(:total_hours)
386 assert_not_nil assigns(:total_hours)
387 assert_equal "162.90", "%.2f" % assigns(:total_hours)
387 assert_equal "162.90", "%.2f" % assigns(:total_hours)
388 assert_tag :form,
388 assert_tag :form,
389 :attributes => {:action => "/time_entries", :id => 'query_form'}
389 :attributes => {:action => "/time_entries", :id => 'query_form'}
390 end
390 end
391
391
392 def test_index_all_projects_should_show_log_time_link
392 def test_index_all_projects_should_show_log_time_link
393 @request.session[:user_id] = 2
393 @request.session[:user_id] = 2
394 get :index
394 get :index
395 assert_response :success
395 assert_response :success
396 assert_template 'index'
396 assert_template 'index'
397 assert_tag 'a', :attributes => {:href => '/time_entries/new'}, :content => /Log time/
397 assert_tag 'a', :attributes => {:href => '/time_entries/new'}, :content => /Log time/
398 end
398 end
399
399
400 def test_index_at_project_level
400 def test_index_at_project_level
401 get :index, :project_id => 'ecookbook'
401 get :index, :project_id => 'ecookbook'
402 assert_response :success
402 assert_response :success
403 assert_template 'index'
403 assert_template 'index'
404 assert_not_nil assigns(:entries)
404 assert_not_nil assigns(:entries)
405 assert_equal 4, assigns(:entries).size
405 assert_equal 4, assigns(:entries).size
406 # project and subproject
406 # project and subproject
407 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
407 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
408 assert_not_nil assigns(:total_hours)
408 assert_not_nil assigns(:total_hours)
409 assert_equal "162.90", "%.2f" % assigns(:total_hours)
409 assert_equal "162.90", "%.2f" % assigns(:total_hours)
410 # display all time by default
410 # display all time by default
411 assert_nil assigns(:from)
411 assert_nil assigns(:from)
412 assert_nil assigns(:to)
412 assert_nil assigns(:to)
413 assert_tag :form,
413 assert_tag :form,
414 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
414 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
415 end
415 end
416
416
417 def test_index_at_project_level_with_date_range
417 def test_index_at_project_level_with_date_range
418 get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30'
418 get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30'
419 assert_response :success
419 assert_response :success
420 assert_template 'index'
420 assert_template 'index'
421 assert_not_nil assigns(:entries)
421 assert_not_nil assigns(:entries)
422 assert_equal 3, assigns(:entries).size
422 assert_equal 3, assigns(:entries).size
423 assert_not_nil assigns(:total_hours)
423 assert_not_nil assigns(:total_hours)
424 assert_equal "12.90", "%.2f" % assigns(:total_hours)
424 assert_equal "12.90", "%.2f" % assigns(:total_hours)
425 assert_equal '2007-03-20'.to_date, assigns(:from)
425 assert_equal '2007-03-20'.to_date, assigns(:from)
426 assert_equal '2007-04-30'.to_date, assigns(:to)
426 assert_equal '2007-04-30'.to_date, assigns(:to)
427 assert_tag :form,
427 assert_tag :form,
428 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
428 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
429 end
429 end
430
430
431 def test_index_at_project_level_with_period
431 def test_index_at_project_level_with_period
432 get :index, :project_id => 'ecookbook', :period => '7_days'
432 get :index, :project_id => 'ecookbook', :period => '7_days'
433 assert_response :success
433 assert_response :success
434 assert_template 'index'
434 assert_template 'index'
435 assert_not_nil assigns(:entries)
435 assert_not_nil assigns(:entries)
436 assert_not_nil assigns(:total_hours)
436 assert_not_nil assigns(:total_hours)
437 assert_equal Date.today - 7, assigns(:from)
437 assert_equal Date.today - 7, assigns(:from)
438 assert_equal Date.today, assigns(:to)
438 assert_equal Date.today, assigns(:to)
439 assert_tag :form,
439 assert_tag :form,
440 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
440 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
441 end
441 end
442
442
443 def test_index_one_day
443 def test_index_one_day
444 get :index, :project_id => 'ecookbook', :from => "2007-03-23", :to => "2007-03-23"
444 get :index, :project_id => 'ecookbook', :from => "2007-03-23", :to => "2007-03-23"
445 assert_response :success
445 assert_response :success
446 assert_template 'index'
446 assert_template 'index'
447 assert_not_nil assigns(:total_hours)
447 assert_not_nil assigns(:total_hours)
448 assert_equal "4.25", "%.2f" % assigns(:total_hours)
448 assert_equal "4.25", "%.2f" % assigns(:total_hours)
449 assert_tag :form,
449 assert_tag :form,
450 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
450 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
451 end
451 end
452
452
453 def test_index_from_a_date
454 get :index, :project_id => 'ecookbook', :from => "2007-03-23", :to => ""
455 assert_equal '2007-03-23'.to_date, assigns(:from)
456 assert_nil assigns(:to)
457 end
458
459 def test_index_to_a_date
460 get :index, :project_id => 'ecookbook', :from => "", :to => "2007-03-23"
461 assert_nil assigns(:from)
462 assert_equal '2007-03-23'.to_date, assigns(:to)
463 end
464
453 def test_index_today
465 def test_index_today
454 Date.stubs(:today).returns('2011-12-15'.to_date)
466 Date.stubs(:today).returns('2011-12-15'.to_date)
455 get :index, :period => 'today'
467 get :index, :period => 'today'
456 assert_equal '2011-12-15'.to_date, assigns(:from)
468 assert_equal '2011-12-15'.to_date, assigns(:from)
457 assert_equal '2011-12-15'.to_date, assigns(:to)
469 assert_equal '2011-12-15'.to_date, assigns(:to)
458 end
470 end
459
471
460 def test_index_yesterday
472 def test_index_yesterday
461 Date.stubs(:today).returns('2011-12-15'.to_date)
473 Date.stubs(:today).returns('2011-12-15'.to_date)
462 get :index, :period => 'yesterday'
474 get :index, :period => 'yesterday'
463 assert_equal '2011-12-14'.to_date, assigns(:from)
475 assert_equal '2011-12-14'.to_date, assigns(:from)
464 assert_equal '2011-12-14'.to_date, assigns(:to)
476 assert_equal '2011-12-14'.to_date, assigns(:to)
465 end
477 end
466
478
467 def test_index_current_week
479 def test_index_current_week
468 Date.stubs(:today).returns('2011-12-15'.to_date)
480 Date.stubs(:today).returns('2011-12-15'.to_date)
469 get :index, :period => 'current_week'
481 get :index, :period => 'current_week'
470 assert_equal '2011-12-12'.to_date, assigns(:from)
482 assert_equal '2011-12-12'.to_date, assigns(:from)
471 assert_equal '2011-12-18'.to_date, assigns(:to)
483 assert_equal '2011-12-18'.to_date, assigns(:to)
472 end
484 end
473
485
474 def test_index_last_week
486 def test_index_last_week
475 Date.stubs(:today).returns('2011-12-15'.to_date)
487 Date.stubs(:today).returns('2011-12-15'.to_date)
476 get :index, :period => 'current_week'
488 get :index, :period => 'current_week'
477 assert_equal '2011-12-05'.to_date, assigns(:from)
489 assert_equal '2011-12-05'.to_date, assigns(:from)
478 assert_equal '2011-12-11'.to_date, assigns(:to)
490 assert_equal '2011-12-11'.to_date, assigns(:to)
479 end
491 end
480
492
481 def test_index_last_week
493 def test_index_last_week
482 Date.stubs(:today).returns('2011-12-15'.to_date)
494 Date.stubs(:today).returns('2011-12-15'.to_date)
483 get :index, :period => 'last_week'
495 get :index, :period => 'last_week'
484 assert_equal '2011-12-05'.to_date, assigns(:from)
496 assert_equal '2011-12-05'.to_date, assigns(:from)
485 assert_equal '2011-12-11'.to_date, assigns(:to)
497 assert_equal '2011-12-11'.to_date, assigns(:to)
486 end
498 end
487
499
488 def test_index_7_days
500 def test_index_7_days
489 Date.stubs(:today).returns('2011-12-15'.to_date)
501 Date.stubs(:today).returns('2011-12-15'.to_date)
490 get :index, :period => '7_days'
502 get :index, :period => '7_days'
491 assert_equal '2011-12-08'.to_date, assigns(:from)
503 assert_equal '2011-12-08'.to_date, assigns(:from)
492 assert_equal '2011-12-15'.to_date, assigns(:to)
504 assert_equal '2011-12-15'.to_date, assigns(:to)
493 end
505 end
494
506
495 def test_index_current_month
507 def test_index_current_month
496 Date.stubs(:today).returns('2011-12-15'.to_date)
508 Date.stubs(:today).returns('2011-12-15'.to_date)
497 get :index, :period => 'current_month'
509 get :index, :period => 'current_month'
498 assert_equal '2011-12-01'.to_date, assigns(:from)
510 assert_equal '2011-12-01'.to_date, assigns(:from)
499 assert_equal '2011-12-31'.to_date, assigns(:to)
511 assert_equal '2011-12-31'.to_date, assigns(:to)
500 end
512 end
501
513
502 def test_index_last_month
514 def test_index_last_month
503 Date.stubs(:today).returns('2011-12-15'.to_date)
515 Date.stubs(:today).returns('2011-12-15'.to_date)
504 get :index, :period => 'last_month'
516 get :index, :period => 'last_month'
505 assert_equal '2011-11-01'.to_date, assigns(:from)
517 assert_equal '2011-11-01'.to_date, assigns(:from)
506 assert_equal '2011-11-30'.to_date, assigns(:to)
518 assert_equal '2011-11-30'.to_date, assigns(:to)
507 end
519 end
508
520
509 def test_index_30_days
521 def test_index_30_days
510 Date.stubs(:today).returns('2011-12-15'.to_date)
522 Date.stubs(:today).returns('2011-12-15'.to_date)
511 get :index, :period => '30_days'
523 get :index, :period => '30_days'
512 assert_equal '2011-11-15'.to_date, assigns(:from)
524 assert_equal '2011-11-15'.to_date, assigns(:from)
513 assert_equal '2011-12-15'.to_date, assigns(:to)
525 assert_equal '2011-12-15'.to_date, assigns(:to)
514 end
526 end
515
527
516 def test_index_current_year
528 def test_index_current_year
517 Date.stubs(:today).returns('2011-12-15'.to_date)
529 Date.stubs(:today).returns('2011-12-15'.to_date)
518 get :index, :period => 'current_year'
530 get :index, :period => 'current_year'
519 assert_equal '2011-01-01'.to_date, assigns(:from)
531 assert_equal '2011-01-01'.to_date, assigns(:from)
520 assert_equal '2011-12-31'.to_date, assigns(:to)
532 assert_equal '2011-12-31'.to_date, assigns(:to)
521 end
533 end
522
534
523 def test_index_at_issue_level
535 def test_index_at_issue_level
524 get :index, :issue_id => 1
536 get :index, :issue_id => 1
525 assert_response :success
537 assert_response :success
526 assert_template 'index'
538 assert_template 'index'
527 assert_not_nil assigns(:entries)
539 assert_not_nil assigns(:entries)
528 assert_equal 2, assigns(:entries).size
540 assert_equal 2, assigns(:entries).size
529 assert_not_nil assigns(:total_hours)
541 assert_not_nil assigns(:total_hours)
530 assert_equal 154.25, assigns(:total_hours)
542 assert_equal 154.25, assigns(:total_hours)
531 # display all time
543 # display all time
532 assert_nil assigns(:from)
544 assert_nil assigns(:from)
533 assert_nil assigns(:to)
545 assert_nil assigns(:to)
534 # TODO: remove /projects/:project_id/issues/:issue_id/time_entries routes
546 # TODO: remove /projects/:project_id/issues/:issue_id/time_entries routes
535 # to use /issues/:issue_id/time_entries
547 # to use /issues/:issue_id/time_entries
536 assert_tag :form,
548 assert_tag :form,
537 :attributes => {:action => "/projects/ecookbook/issues/1/time_entries", :id => 'query_form'}
549 :attributes => {:action => "/projects/ecookbook/issues/1/time_entries", :id => 'query_form'}
538 end
550 end
539
551
540 def test_index_atom_feed
552 def test_index_atom_feed
541 get :index, :project_id => 1, :format => 'atom'
553 get :index, :project_id => 1, :format => 'atom'
542 assert_response :success
554 assert_response :success
543 assert_equal 'application/atom+xml', @response.content_type
555 assert_equal 'application/atom+xml', @response.content_type
544 assert_not_nil assigns(:items)
556 assert_not_nil assigns(:items)
545 assert assigns(:items).first.is_a?(TimeEntry)
557 assert assigns(:items).first.is_a?(TimeEntry)
546 end
558 end
547
559
548 def test_index_all_projects_csv_export
560 def test_index_all_projects_csv_export
549 Setting.date_format = '%m/%d/%Y'
561 Setting.date_format = '%m/%d/%Y'
550 get :index, :format => 'csv'
562 get :index, :format => 'csv'
551 assert_response :success
563 assert_response :success
552 assert_equal 'text/csv', @response.content_type
564 assert_equal 'text/csv', @response.content_type
553 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
565 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
554 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
566 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
555 end
567 end
556
568
557 def test_index_csv_export
569 def test_index_csv_export
558 Setting.date_format = '%m/%d/%Y'
570 Setting.date_format = '%m/%d/%Y'
559 get :index, :project_id => 1, :format => 'csv'
571 get :index, :project_id => 1, :format => 'csv'
560 assert_response :success
572 assert_response :success
561 assert_equal 'text/csv', @response.content_type
573 assert_equal 'text/csv', @response.content_type
562 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
574 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
563 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
575 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
564 end
576 end
565
577
566 def test_index_csv_export_with_multi_custom_field
578 def test_index_csv_export_with_multi_custom_field
567 field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'list',
579 field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'list',
568 :multiple => true, :possible_values => ['value1', 'value2'])
580 :multiple => true, :possible_values => ['value1', 'value2'])
569 entry = TimeEntry.find(1)
581 entry = TimeEntry.find(1)
570 entry.custom_field_values = {field.id => ['value1', 'value2']}
582 entry.custom_field_values = {field.id => ['value1', 'value2']}
571 entry.save!
583 entry.save!
572
584
573 get :index, :project_id => 1, :format => 'csv'
585 get :index, :project_id => 1, :format => 'csv'
574 assert_response :success
586 assert_response :success
575 assert_include '"value1, value2"', @response.body
587 assert_include '"value1, value2"', @response.body
576 end
588 end
577
589
578 def test_csv_big_5
590 def test_csv_big_5
579 user = User.find_by_id(3)
591 user = User.find_by_id(3)
580 user.language = "zh-TW"
592 user.language = "zh-TW"
581 assert user.save
593 assert user.save
582 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
594 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
583 str_big5 = "\xa4@\xa4\xeb"
595 str_big5 = "\xa4@\xa4\xeb"
584 if str_utf8.respond_to?(:force_encoding)
596 if str_utf8.respond_to?(:force_encoding)
585 str_utf8.force_encoding('UTF-8')
597 str_utf8.force_encoding('UTF-8')
586 str_big5.force_encoding('Big5')
598 str_big5.force_encoding('Big5')
587 end
599 end
588 @request.session[:user_id] = 3
600 @request.session[:user_id] = 3
589 post :create, :project_id => 1,
601 post :create, :project_id => 1,
590 :time_entry => {:comments => str_utf8,
602 :time_entry => {:comments => str_utf8,
591 # Not the default activity
603 # Not the default activity
592 :activity_id => '11',
604 :activity_id => '11',
593 :issue_id => '',
605 :issue_id => '',
594 :spent_on => '2011-11-10',
606 :spent_on => '2011-11-10',
595 :hours => '7.3'}
607 :hours => '7.3'}
596 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
608 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
597
609
598 t = TimeEntry.find_by_comments(str_utf8)
610 t = TimeEntry.find_by_comments(str_utf8)
599 assert_not_nil t
611 assert_not_nil t
600 assert_equal 11, t.activity_id
612 assert_equal 11, t.activity_id
601 assert_equal 7.3, t.hours
613 assert_equal 7.3, t.hours
602 assert_equal 3, t.user_id
614 assert_equal 3, t.user_id
603
615
604 get :index, :project_id => 1, :format => 'csv',
616 get :index, :project_id => 1, :format => 'csv',
605 :from => '2011-11-10', :to => '2011-11-10'
617 :from => '2011-11-10', :to => '2011-11-10'
606 assert_response :success
618 assert_response :success
607 assert_equal 'text/csv', @response.content_type
619 assert_equal 'text/csv', @response.content_type
608 ar = @response.body.chomp.split("\n")
620 ar = @response.body.chomp.split("\n")
609 s1 = "\xa4\xe9\xb4\xc1"
621 s1 = "\xa4\xe9\xb4\xc1"
610 if str_utf8.respond_to?(:force_encoding)
622 if str_utf8.respond_to?(:force_encoding)
611 s1.force_encoding('Big5')
623 s1.force_encoding('Big5')
612 end
624 end
613 assert ar[0].include?(s1)
625 assert ar[0].include?(s1)
614 assert ar[1].include?(str_big5)
626 assert ar[1].include?(str_big5)
615 end
627 end
616
628
617 def test_csv_cannot_convert_should_be_replaced_big_5
629 def test_csv_cannot_convert_should_be_replaced_big_5
618 user = User.find_by_id(3)
630 user = User.find_by_id(3)
619 user.language = "zh-TW"
631 user.language = "zh-TW"
620 assert user.save
632 assert user.save
621 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85"
633 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85"
622 if str_utf8.respond_to?(:force_encoding)
634 if str_utf8.respond_to?(:force_encoding)
623 str_utf8.force_encoding('UTF-8')
635 str_utf8.force_encoding('UTF-8')
624 end
636 end
625 @request.session[:user_id] = 3
637 @request.session[:user_id] = 3
626 post :create, :project_id => 1,
638 post :create, :project_id => 1,
627 :time_entry => {:comments => str_utf8,
639 :time_entry => {:comments => str_utf8,
628 # Not the default activity
640 # Not the default activity
629 :activity_id => '11',
641 :activity_id => '11',
630 :issue_id => '',
642 :issue_id => '',
631 :spent_on => '2011-11-10',
643 :spent_on => '2011-11-10',
632 :hours => '7.3'}
644 :hours => '7.3'}
633 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
645 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
634
646
635 t = TimeEntry.find_by_comments(str_utf8)
647 t = TimeEntry.find_by_comments(str_utf8)
636 assert_not_nil t
648 assert_not_nil t
637 assert_equal 11, t.activity_id
649 assert_equal 11, t.activity_id
638 assert_equal 7.3, t.hours
650 assert_equal 7.3, t.hours
639 assert_equal 3, t.user_id
651 assert_equal 3, t.user_id
640
652
641 get :index, :project_id => 1, :format => 'csv',
653 get :index, :project_id => 1, :format => 'csv',
642 :from => '2011-11-10', :to => '2011-11-10'
654 :from => '2011-11-10', :to => '2011-11-10'
643 assert_response :success
655 assert_response :success
644 assert_equal 'text/csv', @response.content_type
656 assert_equal 'text/csv', @response.content_type
645 ar = @response.body.chomp.split("\n")
657 ar = @response.body.chomp.split("\n")
646 s1 = "\xa4\xe9\xb4\xc1"
658 s1 = "\xa4\xe9\xb4\xc1"
647 if str_utf8.respond_to?(:force_encoding)
659 if str_utf8.respond_to?(:force_encoding)
648 s1.force_encoding('Big5')
660 s1.force_encoding('Big5')
649 end
661 end
650 assert ar[0].include?(s1)
662 assert ar[0].include?(s1)
651 s2 = ar[1].split(",")[8]
663 s2 = ar[1].split(",")[8]
652 if s2.respond_to?(:force_encoding)
664 if s2.respond_to?(:force_encoding)
653 s3 = "\xa5H?"
665 s3 = "\xa5H?"
654 s3.force_encoding('Big5')
666 s3.force_encoding('Big5')
655 assert_equal s3, s2
667 assert_equal s3, s2
656 elsif RUBY_PLATFORM == 'java'
668 elsif RUBY_PLATFORM == 'java'
657 assert_equal "??", s2
669 assert_equal "??", s2
658 else
670 else
659 assert_equal "\xa5H???", s2
671 assert_equal "\xa5H???", s2
660 end
672 end
661 end
673 end
662
674
663 def test_csv_tw
675 def test_csv_tw
664 with_settings :default_language => "zh-TW" do
676 with_settings :default_language => "zh-TW" do
665 str1 = "test_csv_tw"
677 str1 = "test_csv_tw"
666 user = User.find_by_id(3)
678 user = User.find_by_id(3)
667 te1 = TimeEntry.create(:spent_on => '2011-11-10',
679 te1 = TimeEntry.create(:spent_on => '2011-11-10',
668 :hours => 999.9,
680 :hours => 999.9,
669 :project => Project.find(1),
681 :project => Project.find(1),
670 :user => user,
682 :user => user,
671 :activity => TimeEntryActivity.find_by_name('Design'),
683 :activity => TimeEntryActivity.find_by_name('Design'),
672 :comments => str1)
684 :comments => str1)
673 te2 = TimeEntry.find_by_comments(str1)
685 te2 = TimeEntry.find_by_comments(str1)
674 assert_not_nil te2
686 assert_not_nil te2
675 assert_equal 999.9, te2.hours
687 assert_equal 999.9, te2.hours
676 assert_equal 3, te2.user_id
688 assert_equal 3, te2.user_id
677
689
678 get :index, :project_id => 1, :format => 'csv',
690 get :index, :project_id => 1, :format => 'csv',
679 :from => '2011-11-10', :to => '2011-11-10'
691 :from => '2011-11-10', :to => '2011-11-10'
680 assert_response :success
692 assert_response :success
681 assert_equal 'text/csv', @response.content_type
693 assert_equal 'text/csv', @response.content_type
682
694
683 ar = @response.body.chomp.split("\n")
695 ar = @response.body.chomp.split("\n")
684 s2 = ar[1].split(",")[7]
696 s2 = ar[1].split(",")[7]
685 assert_equal '999.9', s2
697 assert_equal '999.9', s2
686
698
687 str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
699 str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
688 if str_tw.respond_to?(:force_encoding)
700 if str_tw.respond_to?(:force_encoding)
689 str_tw.force_encoding('UTF-8')
701 str_tw.force_encoding('UTF-8')
690 end
702 end
691 assert_equal str_tw, l(:general_lang_name)
703 assert_equal str_tw, l(:general_lang_name)
692 assert_equal ',', l(:general_csv_separator)
704 assert_equal ',', l(:general_csv_separator)
693 assert_equal '.', l(:general_csv_decimal_separator)
705 assert_equal '.', l(:general_csv_decimal_separator)
694 end
706 end
695 end
707 end
696
708
697 def test_csv_fr
709 def test_csv_fr
698 with_settings :default_language => "fr" do
710 with_settings :default_language => "fr" do
699 str1 = "test_csv_fr"
711 str1 = "test_csv_fr"
700 user = User.find_by_id(3)
712 user = User.find_by_id(3)
701 te1 = TimeEntry.create(:spent_on => '2011-11-10',
713 te1 = TimeEntry.create(:spent_on => '2011-11-10',
702 :hours => 999.9,
714 :hours => 999.9,
703 :project => Project.find(1),
715 :project => Project.find(1),
704 :user => user,
716 :user => user,
705 :activity => TimeEntryActivity.find_by_name('Design'),
717 :activity => TimeEntryActivity.find_by_name('Design'),
706 :comments => str1)
718 :comments => str1)
707 te2 = TimeEntry.find_by_comments(str1)
719 te2 = TimeEntry.find_by_comments(str1)
708 assert_not_nil te2
720 assert_not_nil te2
709 assert_equal 999.9, te2.hours
721 assert_equal 999.9, te2.hours
710 assert_equal 3, te2.user_id
722 assert_equal 3, te2.user_id
711
723
712 get :index, :project_id => 1, :format => 'csv',
724 get :index, :project_id => 1, :format => 'csv',
713 :from => '2011-11-10', :to => '2011-11-10'
725 :from => '2011-11-10', :to => '2011-11-10'
714 assert_response :success
726 assert_response :success
715 assert_equal 'text/csv', @response.content_type
727 assert_equal 'text/csv', @response.content_type
716
728
717 ar = @response.body.chomp.split("\n")
729 ar = @response.body.chomp.split("\n")
718 s2 = ar[1].split(";")[7]
730 s2 = ar[1].split(";")[7]
719 assert_equal '999,9', s2
731 assert_equal '999,9', s2
720
732
721 str_fr = "Fran\xc3\xa7ais"
733 str_fr = "Fran\xc3\xa7ais"
722 if str_fr.respond_to?(:force_encoding)
734 if str_fr.respond_to?(:force_encoding)
723 str_fr.force_encoding('UTF-8')
735 str_fr.force_encoding('UTF-8')
724 end
736 end
725 assert_equal str_fr, l(:general_lang_name)
737 assert_equal str_fr, l(:general_lang_name)
726 assert_equal ';', l(:general_csv_separator)
738 assert_equal ';', l(:general_csv_separator)
727 assert_equal ',', l(:general_csv_decimal_separator)
739 assert_equal ',', l(:general_csv_decimal_separator)
728 end
740 end
729 end
741 end
730 end
742 end
General Comments 0
You need to be logged in to leave comments. Login now