##// END OF EJS Templates
Fix test in 3.2-stable (#14817)....
Jean-Philippe Lang -
r15723:c5697e01e4a9
parent child
Show More
@@ -1,844 +1,844
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2 # Redmine - project management software
2 # Redmine - project management software
3 # Copyright (C) 2006-2016 Jean-Philippe Lang
3 # Copyright (C) 2006-2016 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
20
21 class TimelogControllerTest < ActionController::TestCase
21 class TimelogControllerTest < ActionController::TestCase
22 fixtures :projects, :enabled_modules, :roles, :members,
22 fixtures :projects, :enabled_modules, :roles, :members,
23 :member_roles, :issues, :time_entries, :users,
23 :member_roles, :issues, :time_entries, :users,
24 :trackers, :enumerations, :issue_statuses,
24 :trackers, :enumerations, :issue_statuses,
25 :custom_fields, :custom_values,
25 :custom_fields, :custom_values,
26 :projects_trackers, :custom_fields_trackers,
26 :projects_trackers, :custom_fields_trackers,
27 :custom_fields_projects
27 :custom_fields_projects
28
28
29 include Redmine::I18n
29 include Redmine::I18n
30
30
31 def test_new
31 def test_new
32 @request.session[:user_id] = 3
32 @request.session[:user_id] = 3
33 get :new
33 get :new
34 assert_response :success
34 assert_response :success
35 assert_template 'new'
35 assert_template 'new'
36 assert_select 'input[name=?][type=hidden]', 'project_id', 0
36 assert_select 'input[name=?][type=hidden]', 'project_id', 0
37 assert_select 'input[name=?][type=hidden]', 'issue_id', 0
37 assert_select 'input[name=?][type=hidden]', 'issue_id', 0
38 assert_select 'select[name=?]', 'time_entry[project_id]' do
38 assert_select 'select[name=?]', 'time_entry[project_id]' do
39 # blank option for project
39 # blank option for project
40 assert_select 'option[value=""]'
40 assert_select 'option[value=""]'
41 end
41 end
42 end
42 end
43
43
44 def test_new_with_project_id
44 def test_new_with_project_id
45 @request.session[:user_id] = 3
45 @request.session[:user_id] = 3
46 get :new, :project_id => 1
46 get :new, :project_id => 1
47 assert_response :success
47 assert_response :success
48 assert_template 'new'
48 assert_template 'new'
49 assert_select 'input[name=?][type=hidden]', 'project_id'
49 assert_select 'input[name=?][type=hidden]', 'project_id'
50 assert_select 'input[name=?][type=hidden]', 'issue_id', 0
50 assert_select 'input[name=?][type=hidden]', 'issue_id', 0
51 assert_select 'select[name=?]', 'time_entry[project_id]', 0
51 assert_select 'select[name=?]', 'time_entry[project_id]', 0
52 end
52 end
53
53
54 def test_new_with_issue_id
54 def test_new_with_issue_id
55 @request.session[:user_id] = 3
55 @request.session[:user_id] = 3
56 get :new, :issue_id => 2
56 get :new, :issue_id => 2
57 assert_response :success
57 assert_response :success
58 assert_template 'new'
58 assert_template 'new'
59 assert_select 'input[name=?][type=hidden]', 'project_id', 0
59 assert_select 'input[name=?][type=hidden]', 'project_id', 0
60 assert_select 'input[name=?][type=hidden]', 'issue_id'
60 assert_select 'input[name=?][type=hidden]', 'issue_id'
61 assert_select 'select[name=?]', 'time_entry[project_id]', 0
61 assert_select 'select[name=?]', 'time_entry[project_id]', 0
62 end
62 end
63
63
64 def test_new_without_project_should_prefill_the_form
64 def test_new_without_project_should_prefill_the_form
65 @request.session[:user_id] = 3
65 @request.session[:user_id] = 3
66 get :new, :time_entry => {:project_id => '1'}
66 get :new, :time_entry => {:project_id => '1'}
67 assert_response :success
67 assert_response :success
68 assert_template 'new'
68 assert_template 'new'
69 assert_select 'select[name=?]', 'time_entry[project_id]' do
69 assert_select 'select[name=?]', 'time_entry[project_id]' do
70 assert_select 'option[value="1"][selected=selected]'
70 assert_select 'option[value="1"][selected=selected]'
71 end
71 end
72 end
72 end
73
73
74 def test_new_without_project_should_deny_without_permission
74 def test_new_without_project_should_deny_without_permission
75 Role.all.each {|role| role.remove_permission! :log_time}
75 Role.all.each {|role| role.remove_permission! :log_time}
76 @request.session[:user_id] = 3
76 @request.session[:user_id] = 3
77
77
78 get :new
78 get :new
79 assert_response 403
79 assert_response 403
80 end
80 end
81
81
82 def test_new_should_select_default_activity
82 def test_new_should_select_default_activity
83 @request.session[:user_id] = 3
83 @request.session[:user_id] = 3
84 get :new, :project_id => 1
84 get :new, :project_id => 1
85 assert_response :success
85 assert_response :success
86 assert_select 'select[name=?]', 'time_entry[activity_id]' do
86 assert_select 'select[name=?]', 'time_entry[activity_id]' do
87 assert_select 'option[selected=selected]', :text => 'Development'
87 assert_select 'option[selected=selected]', :text => 'Development'
88 end
88 end
89 end
89 end
90
90
91 def test_new_should_only_show_active_time_entry_activities
91 def test_new_should_only_show_active_time_entry_activities
92 @request.session[:user_id] = 3
92 @request.session[:user_id] = 3
93 get :new, :project_id => 1
93 get :new, :project_id => 1
94 assert_response :success
94 assert_response :success
95 assert_select 'option', :text => 'Inactive Activity', :count => 0
95 assert_select 'option', :text => 'Inactive Activity', :count => 0
96 end
96 end
97
97
98 def test_post_new_as_js_should_update_activity_options
98 def test_post_new_as_js_should_update_activity_options
99 @request.session[:user_id] = 3
99 @request.session[:user_id] = 3
100 post :new, :time_entry => {:project_id => 1}, :format => 'js'
100 post :new, :time_entry => {:project_id => 1}, :format => 'js'
101 assert_response :success
101 assert_response :success
102 assert_include '#time_entry_activity_id', response.body
102 assert_include '#time_entry_activity_id', response.body
103 end
103 end
104
104
105 def test_get_edit_existing_time
105 def test_get_edit_existing_time
106 @request.session[:user_id] = 2
106 @request.session[:user_id] = 2
107 get :edit, :id => 2, :project_id => nil
107 get :edit, :id => 2, :project_id => nil
108 assert_response :success
108 assert_response :success
109 assert_template 'edit'
109 assert_template 'edit'
110 assert_select 'form[action=?]', '/time_entries/2'
110 assert_select 'form[action=?]', '/time_entries/2'
111 end
111 end
112
112
113 def test_get_edit_with_an_existing_time_entry_with_inactive_activity
113 def test_get_edit_with_an_existing_time_entry_with_inactive_activity
114 te = TimeEntry.find(1)
114 te = TimeEntry.find(1)
115 te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
115 te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
116 te.save!(:validate => false)
116 te.save!(:validate => false)
117
117
118 @request.session[:user_id] = 1
118 @request.session[:user_id] = 1
119 get :edit, :project_id => 1, :id => 1
119 get :edit, :project_id => 1, :id => 1
120 assert_response :success
120 assert_response :success
121 assert_template 'edit'
121 assert_template 'edit'
122 # Blank option since nothing is pre-selected
122 # Blank option since nothing is pre-selected
123 assert_select 'option', :text => '--- Please select ---'
123 assert_select 'option', :text => '--- Please select ---'
124 end
124 end
125
125
126 def test_post_create
126 def test_post_create
127 @request.session[:user_id] = 3
127 @request.session[:user_id] = 3
128 assert_difference 'TimeEntry.count' do
128 assert_difference 'TimeEntry.count' do
129 post :create, :project_id => 1,
129 post :create, :project_id => 1,
130 :time_entry => {:comments => 'Some work on TimelogControllerTest',
130 :time_entry => {:comments => 'Some work on TimelogControllerTest',
131 # Not the default activity
131 # Not the default activity
132 :activity_id => '11',
132 :activity_id => '11',
133 :spent_on => '2008-03-14',
133 :spent_on => '2008-03-14',
134 :issue_id => '1',
134 :issue_id => '1',
135 :hours => '7.3'}
135 :hours => '7.3'}
136 assert_redirected_to '/projects/ecookbook/time_entries'
136 assert_redirected_to '/projects/ecookbook/time_entries'
137 end
137 end
138
138
139 t = TimeEntry.order('id DESC').first
139 t = TimeEntry.order('id DESC').first
140 assert_not_nil t
140 assert_not_nil t
141 assert_equal 'Some work on TimelogControllerTest', t.comments
141 assert_equal 'Some work on TimelogControllerTest', t.comments
142 assert_equal 1, t.project_id
142 assert_equal 1, t.project_id
143 assert_equal 1, t.issue_id
143 assert_equal 1, t.issue_id
144 assert_equal 11, t.activity_id
144 assert_equal 11, t.activity_id
145 assert_equal 7.3, t.hours
145 assert_equal 7.3, t.hours
146 assert_equal 3, t.user_id
146 assert_equal 3, t.user_id
147 end
147 end
148
148
149 def test_post_create_with_blank_issue
149 def test_post_create_with_blank_issue
150 @request.session[:user_id] = 3
150 @request.session[:user_id] = 3
151 assert_difference 'TimeEntry.count' do
151 assert_difference 'TimeEntry.count' do
152 post :create, :project_id => 1,
152 post :create, :project_id => 1,
153 :time_entry => {:comments => 'Some work on TimelogControllerTest',
153 :time_entry => {:comments => 'Some work on TimelogControllerTest',
154 # Not the default activity
154 # Not the default activity
155 :activity_id => '11',
155 :activity_id => '11',
156 :issue_id => '',
156 :issue_id => '',
157 :spent_on => '2008-03-14',
157 :spent_on => '2008-03-14',
158 :hours => '7.3'}
158 :hours => '7.3'}
159 assert_redirected_to '/projects/ecookbook/time_entries'
159 assert_redirected_to '/projects/ecookbook/time_entries'
160 end
160 end
161
161
162 t = TimeEntry.order('id DESC').first
162 t = TimeEntry.order('id DESC').first
163 assert_not_nil t
163 assert_not_nil t
164 assert_equal 'Some work on TimelogControllerTest', t.comments
164 assert_equal 'Some work on TimelogControllerTest', t.comments
165 assert_equal 1, t.project_id
165 assert_equal 1, t.project_id
166 assert_nil t.issue_id
166 assert_nil t.issue_id
167 assert_equal 11, t.activity_id
167 assert_equal 11, t.activity_id
168 assert_equal 7.3, t.hours
168 assert_equal 7.3, t.hours
169 assert_equal 3, t.user_id
169 assert_equal 3, t.user_id
170 end
170 end
171
171
172 def test_create_on_project_with_time_tracking_disabled_should_fail
172 def test_create_on_project_with_time_tracking_disabled_should_fail
173 Project.find(1).disable_module! :time_tracking
173 Project.find(1).disable_module! :time_tracking
174
174
175 @request.session[:user_id] = 2
175 @request.session[:user_id] = 2
176 assert_no_difference 'TimeEntry.count' do
176 assert_no_difference 'TimeEntry.count' do
177 post :create, :time_entry => {
177 post :create, :time_entry => {
178 :project_id => '1', :issue_id => '',
178 :project_id => '1', :issue_id => '',
179 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
179 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
180 }
180 }
181 end
181 end
182 end
182 end
183
183
184 def test_create_on_project_without_permission_should_fail
184 def test_create_on_project_without_permission_should_fail
185 Role.find(1).remove_permission! :log_time
185 Role.find(1).remove_permission! :log_time
186
186
187 @request.session[:user_id] = 2
187 @request.session[:user_id] = 2
188 assert_no_difference 'TimeEntry.count' do
188 assert_no_difference 'TimeEntry.count' do
189 post :create, :time_entry => {
189 post :create, :time_entry => {
190 :project_id => '1', :issue_id => '',
190 :project_id => '1', :issue_id => '',
191 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
191 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
192 }
192 }
193 end
193 end
194 end
194 end
195
195
196 def test_create_on_issue_in_project_with_time_tracking_disabled_should_fail
196 def test_create_on_issue_in_project_with_time_tracking_disabled_should_fail
197 Project.find(1).disable_module! :time_tracking
197 Project.find(1).disable_module! :time_tracking
198
198
199 @request.session[:user_id] = 2
199 @request.session[:user_id] = 2
200 assert_no_difference 'TimeEntry.count' do
200 assert_no_difference 'TimeEntry.count' do
201 post :create, :time_entry => {
201 post :create, :time_entry => {
202 :project_id => '', :issue_id => '1',
202 :project_id => '', :issue_id => '1',
203 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
203 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
204 }
204 }
205 assert_select_error /Issue is invalid/
205 assert_select_error /Issue is invalid/
206 end
206 end
207 end
207 end
208
208
209 def test_create_on_issue_in_project_without_permission_should_fail
209 def test_create_on_issue_in_project_without_permission_should_fail
210 Role.find(1).remove_permission! :log_time
210 Role.find(1).remove_permission! :log_time
211
211
212 @request.session[:user_id] = 2
212 @request.session[:user_id] = 2
213 assert_no_difference 'TimeEntry.count' do
213 assert_no_difference 'TimeEntry.count' do
214 post :create, :time_entry => {
214 post :create, :time_entry => {
215 :project_id => '', :issue_id => '1',
215 :project_id => '', :issue_id => '1',
216 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
216 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
217 }
217 }
218 assert_select_error /Issue is invalid/
218 assert_select_error /Issue is invalid/
219 end
219 end
220 end
220 end
221
221
222 def test_create_on_issue_that_is_not_visible_should_not_disclose_subject
222 def test_create_on_issue_that_is_not_visible_should_not_disclose_subject
223 issue = Issue.generate!(:subject => "issue_that_is_not_visible", :is_private => true)
223 issue = Issue.generate!(:subject => "issue_that_is_not_visible", :is_private => true)
224 assert !issue.visible?(User.find(3))
224 assert !issue.visible?(User.find(3))
225
225
226 @request.session[:user_id] = 3
226 @request.session[:user_id] = 3
227 assert_no_difference 'TimeEntry.count' do
227 assert_no_difference 'TimeEntry.count' do
228 post :create, :time_entry => {
228 post :create, :time_entry => {
229 :project_id => '', :issue_id => issue.id.to_s,
229 :project_id => '', :issue_id => issue.id.to_s,
230 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
230 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
231 }
231 }
232 end
232 end
233 assert_select_error /Issue is invalid/
233 assert_select_error /Issue is invalid/
234 assert_select "input[name=?][value=?]", "time_entry[issue_id]", issue.id.to_s
234 assert_select "input[name=?][value=?]", "time_entry[issue_id]", issue.id.to_s
235 assert_select "#time_entry_issue", 0
235 assert_select "#time_entry_issue", 0
236 assert !response.body.include?('issue_that_is_not_visible')
236 assert !response.body.include?('issue_that_is_not_visible')
237 end
237 end
238
238
239 def test_create_and_continue_at_project_level
239 def test_create_and_continue_at_project_level
240 @request.session[:user_id] = 2
240 @request.session[:user_id] = 2
241 assert_difference 'TimeEntry.count' do
241 assert_difference 'TimeEntry.count' do
242 post :create, :time_entry => {:project_id => '1',
242 post :create, :time_entry => {:project_id => '1',
243 :activity_id => '11',
243 :activity_id => '11',
244 :issue_id => '',
244 :issue_id => '',
245 :spent_on => '2008-03-14',
245 :spent_on => '2008-03-14',
246 :hours => '7.3'},
246 :hours => '7.3'},
247 :continue => '1'
247 :continue => '1'
248 assert_redirected_to '/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=&time_entry%5Bproject_id%5D=1'
248 assert_redirected_to '/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=&time_entry%5Bproject_id%5D=1'
249 end
249 end
250 end
250 end
251
251
252 def test_create_and_continue_at_issue_level
252 def test_create_and_continue_at_issue_level
253 @request.session[:user_id] = 2
253 @request.session[:user_id] = 2
254 assert_difference 'TimeEntry.count' do
254 assert_difference 'TimeEntry.count' do
255 post :create, :time_entry => {:project_id => '',
255 post :create, :time_entry => {:project_id => '',
256 :activity_id => '11',
256 :activity_id => '11',
257 :issue_id => '1',
257 :issue_id => '1',
258 :spent_on => '2008-03-14',
258 :spent_on => '2008-03-14',
259 :hours => '7.3'},
259 :hours => '7.3'},
260 :continue => '1'
260 :continue => '1'
261 assert_redirected_to '/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=1&time_entry%5Bproject_id%5D='
261 assert_redirected_to '/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=1&time_entry%5Bproject_id%5D='
262 end
262 end
263 end
263 end
264
264
265 def test_create_and_continue_with_project_id
265 def test_create_and_continue_with_project_id
266 @request.session[:user_id] = 2
266 @request.session[:user_id] = 2
267 assert_difference 'TimeEntry.count' do
267 assert_difference 'TimeEntry.count' do
268 post :create, :project_id => 1,
268 post :create, :project_id => 1,
269 :time_entry => {:activity_id => '11',
269 :time_entry => {:activity_id => '11',
270 :issue_id => '',
270 :issue_id => '',
271 :spent_on => '2008-03-14',
271 :spent_on => '2008-03-14',
272 :hours => '7.3'},
272 :hours => '7.3'},
273 :continue => '1'
273 :continue => '1'
274 assert_redirected_to '/projects/ecookbook/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=&time_entry%5Bproject_id%5D='
274 assert_redirected_to '/projects/ecookbook/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=&time_entry%5Bproject_id%5D='
275 end
275 end
276 end
276 end
277
277
278 def test_create_and_continue_with_issue_id
278 def test_create_and_continue_with_issue_id
279 @request.session[:user_id] = 2
279 @request.session[:user_id] = 2
280 assert_difference 'TimeEntry.count' do
280 assert_difference 'TimeEntry.count' do
281 post :create, :issue_id => 1,
281 post :create, :issue_id => 1,
282 :time_entry => {:activity_id => '11',
282 :time_entry => {:activity_id => '11',
283 :issue_id => '1',
283 :issue_id => '1',
284 :spent_on => '2008-03-14',
284 :spent_on => '2008-03-14',
285 :hours => '7.3'},
285 :hours => '7.3'},
286 :continue => '1'
286 :continue => '1'
287 assert_redirected_to '/issues/1/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=1&time_entry%5Bproject_id%5D='
287 assert_redirected_to '/issues/1/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=1&time_entry%5Bproject_id%5D='
288 end
288 end
289 end
289 end
290
290
291 def test_create_without_log_time_permission_should_be_denied
291 def test_create_without_log_time_permission_should_be_denied
292 @request.session[:user_id] = 2
292 @request.session[:user_id] = 2
293 Role.find_by_name('Manager').remove_permission! :log_time
293 Role.find_by_name('Manager').remove_permission! :log_time
294 post :create, :project_id => 1,
294 post :create, :project_id => 1,
295 :time_entry => {:activity_id => '11',
295 :time_entry => {:activity_id => '11',
296 :issue_id => '',
296 :issue_id => '',
297 :spent_on => '2008-03-14',
297 :spent_on => '2008-03-14',
298 :hours => '7.3'}
298 :hours => '7.3'}
299
299
300 assert_response 403
300 assert_response 403
301 end
301 end
302
302
303 def test_create_without_project_and_issue_should_fail
303 def test_create_without_project_and_issue_should_fail
304 @request.session[:user_id] = 2
304 @request.session[:user_id] = 2
305 post :create, :time_entry => {:issue_id => ''}
305 post :create, :time_entry => {:issue_id => ''}
306
306
307 assert_response :success
307 assert_response :success
308 assert_template 'new'
308 assert_template 'new'
309 end
309 end
310
310
311 def test_create_with_failure
311 def test_create_with_failure
312 @request.session[:user_id] = 2
312 @request.session[:user_id] = 2
313 post :create, :project_id => 1,
313 post :create, :project_id => 1,
314 :time_entry => {:activity_id => '',
314 :time_entry => {:activity_id => '',
315 :issue_id => '',
315 :issue_id => '',
316 :spent_on => '2008-03-14',
316 :spent_on => '2008-03-14',
317 :hours => '7.3'}
317 :hours => '7.3'}
318
318
319 assert_response :success
319 assert_response :success
320 assert_template 'new'
320 assert_template 'new'
321 end
321 end
322
322
323 def test_create_without_project
323 def test_create_without_project
324 @request.session[:user_id] = 2
324 @request.session[:user_id] = 2
325 assert_difference 'TimeEntry.count' do
325 assert_difference 'TimeEntry.count' do
326 post :create, :time_entry => {:project_id => '1',
326 post :create, :time_entry => {:project_id => '1',
327 :activity_id => '11',
327 :activity_id => '11',
328 :issue_id => '',
328 :issue_id => '',
329 :spent_on => '2008-03-14',
329 :spent_on => '2008-03-14',
330 :hours => '7.3'}
330 :hours => '7.3'}
331 end
331 end
332
332
333 assert_redirected_to '/projects/ecookbook/time_entries'
333 assert_redirected_to '/projects/ecookbook/time_entries'
334 time_entry = TimeEntry.order('id DESC').first
334 time_entry = TimeEntry.order('id DESC').first
335 assert_equal 1, time_entry.project_id
335 assert_equal 1, time_entry.project_id
336 end
336 end
337
337
338 def test_create_without_project_should_fail_with_issue_not_inside_project
338 def test_create_without_project_should_fail_with_issue_not_inside_project
339 @request.session[:user_id] = 2
339 @request.session[:user_id] = 2
340 assert_no_difference 'TimeEntry.count' do
340 assert_no_difference 'TimeEntry.count' do
341 post :create, :time_entry => {:project_id => '1',
341 post :create, :time_entry => {:project_id => '1',
342 :activity_id => '11',
342 :activity_id => '11',
343 :issue_id => '5',
343 :issue_id => '5',
344 :spent_on => '2008-03-14',
344 :spent_on => '2008-03-14',
345 :hours => '7.3'}
345 :hours => '7.3'}
346 end
346 end
347
347
348 assert_response :success
348 assert_response :success
349 assert assigns(:time_entry).errors[:issue_id].present?
349 assert assigns(:time_entry).errors[:issue_id].present?
350 end
350 end
351
351
352 def test_create_without_project_should_deny_without_permission
352 def test_create_without_project_should_deny_without_permission
353 @request.session[:user_id] = 2
353 @request.session[:user_id] = 2
354 Project.find(3).disable_module!(:time_tracking)
354 Project.find(3).disable_module!(:time_tracking)
355
355
356 assert_no_difference 'TimeEntry.count' do
356 assert_no_difference 'TimeEntry.count' do
357 post :create, :time_entry => {:project_id => '3',
357 post :create, :time_entry => {:project_id => '3',
358 :activity_id => '11',
358 :activity_id => '11',
359 :issue_id => '',
359 :issue_id => '',
360 :spent_on => '2008-03-14',
360 :spent_on => '2008-03-14',
361 :hours => '7.3'}
361 :hours => '7.3'}
362 end
362 end
363
363
364 assert_response 403
364 assert_response 403
365 end
365 end
366
366
367 def test_create_without_project_with_failure
367 def test_create_without_project_with_failure
368 @request.session[:user_id] = 2
368 @request.session[:user_id] = 2
369 assert_no_difference 'TimeEntry.count' do
369 assert_no_difference 'TimeEntry.count' do
370 post :create, :time_entry => {:project_id => '1',
370 post :create, :time_entry => {:project_id => '1',
371 :activity_id => '11',
371 :activity_id => '11',
372 :issue_id => '',
372 :issue_id => '',
373 :spent_on => '2008-03-14',
373 :spent_on => '2008-03-14',
374 :hours => ''}
374 :hours => ''}
375 end
375 end
376
376
377 assert_response :success
377 assert_response :success
378 assert_select 'select[name=?]', 'time_entry[project_id]' do
378 assert_select 'select[name=?]', 'time_entry[project_id]' do
379 assert_select 'option[value="1"][selected=selected]'
379 assert_select 'option[value="1"][selected=selected]'
380 end
380 end
381 end
381 end
382
382
383 def test_update
383 def test_update
384 entry = TimeEntry.find(1)
384 entry = TimeEntry.find(1)
385 assert_equal 1, entry.issue_id
385 assert_equal 1, entry.issue_id
386 assert_equal 2, entry.user_id
386 assert_equal 2, entry.user_id
387
387
388 @request.session[:user_id] = 1
388 @request.session[:user_id] = 1
389 put :update, :id => 1,
389 put :update, :id => 1,
390 :time_entry => {:issue_id => '2',
390 :time_entry => {:issue_id => '2',
391 :hours => '8'}
391 :hours => '8'}
392 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
392 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
393 entry.reload
393 entry.reload
394
394
395 assert_equal 8, entry.hours
395 assert_equal 8, entry.hours
396 assert_equal 2, entry.issue_id
396 assert_equal 2, entry.issue_id
397 assert_equal 2, entry.user_id
397 assert_equal 2, entry.user_id
398 end
398 end
399
399
400 def test_update_should_allow_to_change_issue_to_another_project
400 def test_update_should_allow_to_change_issue_to_another_project
401 entry = TimeEntry.generate!(:issue_id => 1)
401 entry = TimeEntry.generate!(:issue_id => 1)
402
402
403 @request.session[:user_id] = 1
403 @request.session[:user_id] = 1
404 put :update, :id => entry.id, :time_entry => {:issue_id => '5'}
404 put :update, :id => entry.id, :time_entry => {:issue_id => '5'}
405 assert_response 302
405 assert_response 302
406 entry.reload
406 entry.reload
407
407
408 assert_equal 5, entry.issue_id
408 assert_equal 5, entry.issue_id
409 assert_equal 3, entry.project_id
409 assert_equal 3, entry.project_id
410 end
410 end
411
411
412 def test_update_should_not_allow_to_change_issue_to_an_invalid_project
412 def test_update_should_not_allow_to_change_issue_to_an_invalid_project
413 entry = TimeEntry.generate!(:issue_id => 1)
413 entry = TimeEntry.generate!(:issue_id => 1)
414 Project.find(3).disable_module!(:time_tracking)
414 Project.find(3).disable_module!(:time_tracking)
415
415
416 @request.session[:user_id] = 1
416 @request.session[:user_id] = 1
417 put :update, :id => entry.id, :time_entry => {:issue_id => '5'}
417 put :update, :id => entry.id, :time_entry => {:issue_id => '5'}
418 assert_response 200
418 assert_response 200
419 assert_include "Issue is invalid", assigns(:time_entry).errors.full_messages
419 assert_include "Issue is invalid", assigns(:time_entry).errors.full_messages
420 end
420 end
421
421
422 def test_get_bulk_edit
422 def test_get_bulk_edit
423 @request.session[:user_id] = 2
423 @request.session[:user_id] = 2
424 get :bulk_edit, :ids => [1, 2]
424 get :bulk_edit, :ids => [1, 2]
425 assert_response :success
425 assert_response :success
426 assert_template 'bulk_edit'
426 assert_template 'bulk_edit'
427
427
428 assert_select 'ul#bulk-selection' do
428 assert_select 'ul#bulk-selection' do
429 assert_select 'li', 2
429 assert_select 'li', 2
430 assert_select 'li a', :text => '03/23/2007 - eCookbook: 4.25 hours'
430 assert_select 'li a', :text => '03/23/2007 - eCookbook: 4.25 hours'
431 end
431 end
432
432
433 assert_select 'form#bulk_edit_form[action=?]', '/time_entries/bulk_update' do
433 assert_select 'form#bulk_edit_form[action=?]', '/time_entries/bulk_update' do
434 # System wide custom field
434 # System wide custom field
435 assert_select 'select[name=?]', 'time_entry[custom_field_values][10]'
435 assert_select 'select[name=?]', 'time_entry[custom_field_values][10]'
436
436
437 # Activities
437 # Activities
438 assert_select 'select[name=?]', 'time_entry[activity_id]' do
438 assert_select 'select[name=?]', 'time_entry[activity_id]' do
439 assert_select 'option[value=""]', :text => '(No change)'
439 assert_select 'option[value=""]', :text => '(No change)'
440 assert_select 'option[value="9"]', :text => 'Design'
440 assert_select 'option[value="9"]', :text => 'Design'
441 end
441 end
442 end
442 end
443 end
443 end
444
444
445 def test_get_bulk_edit_on_different_projects
445 def test_get_bulk_edit_on_different_projects
446 @request.session[:user_id] = 2
446 @request.session[:user_id] = 2
447 get :bulk_edit, :ids => [1, 2, 6]
447 get :bulk_edit, :ids => [1, 2, 6]
448 assert_response :success
448 assert_response :success
449 assert_template 'bulk_edit'
449 assert_template 'bulk_edit'
450 end
450 end
451
451
452 def test_bulk_edit_with_edit_own_time_entries_permission
452 def test_bulk_edit_with_edit_own_time_entries_permission
453 @request.session[:user_id] = 2
453 @request.session[:user_id] = 2
454 Role.find_by_name('Manager').remove_permission! :edit_time_entries
454 Role.find_by_name('Manager').remove_permission! :edit_time_entries
455 Role.find_by_name('Manager').add_permission! :edit_own_time_entries
455 Role.find_by_name('Manager').add_permission! :edit_own_time_entries
456 ids = (0..1).map {TimeEntry.generate!(:user => User.find(2)).id}
456 ids = (0..1).map {TimeEntry.generate!(:user => User.find(2)).id}
457
457
458 get :bulk_edit, :ids => ids
458 get :bulk_edit, :ids => ids
459 assert_response :success
459 assert_response :success
460 end
460 end
461
461
462 def test_bulk_update
462 def test_bulk_update
463 @request.session[:user_id] = 2
463 @request.session[:user_id] = 2
464 # update time entry activity
464 # update time entry activity
465 post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9}
465 post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9}
466
466
467 assert_response 302
467 assert_response 302
468 # check that the issues were updated
468 # check that the issues were updated
469 assert_equal [9, 9], TimeEntry.where(:id => [1, 2]).collect {|i| i.activity_id}
469 assert_equal [9, 9], TimeEntry.where(:id => [1, 2]).collect {|i| i.activity_id}
470 end
470 end
471
471
472 def test_bulk_update_with_failure
472 def test_bulk_update_with_failure
473 @request.session[:user_id] = 2
473 @request.session[:user_id] = 2
474 post :bulk_update, :ids => [1, 2], :time_entry => { :hours => 'A'}
474 post :bulk_update, :ids => [1, 2], :time_entry => { :hours => 'A'}
475
475
476 assert_response 302
476 assert_response 302
477 assert_match /Failed to save 2 time entrie/, flash[:error]
477 assert_match /Failed to save 2 time entrie/, flash[:error]
478 end
478 end
479
479
480 def test_bulk_update_on_different_projects
480 def test_bulk_update_on_different_projects
481 @request.session[:user_id] = 2
481 @request.session[:user_id] = 2
482 # makes user a manager on the other project
482 # makes user a manager on the other project
483 Member.create!(:user_id => 2, :project_id => 3, :role_ids => [1])
483 Member.create!(:user_id => 2, :project_id => 3, :role_ids => [1])
484
484
485 # update time entry activity
485 # update time entry activity
486 post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 }
486 post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 }
487
487
488 assert_response 302
488 assert_response 302
489 # check that the issues were updated
489 # check that the issues were updated
490 assert_equal [9, 9, 9], TimeEntry.where(:id => [1, 2, 4]).collect {|i| i.activity_id}
490 assert_equal [9, 9, 9], TimeEntry.where(:id => [1, 2, 4]).collect {|i| i.activity_id}
491 end
491 end
492
492
493 def test_bulk_update_on_different_projects_without_rights
493 def test_bulk_update_on_different_projects_without_rights
494 @request.session[:user_id] = 3
494 @request.session[:user_id] = 3
495 user = User.find(3)
495 user = User.find(3)
496 action = { :controller => "timelog", :action => "bulk_update" }
496 action = { :controller => "timelog", :action => "bulk_update" }
497 assert user.allowed_to?(action, TimeEntry.find(1).project)
497 assert user.allowed_to?(action, TimeEntry.find(1).project)
498 assert ! user.allowed_to?(action, TimeEntry.find(5).project)
498 assert ! user.allowed_to?(action, TimeEntry.find(5).project)
499 post :bulk_update, :ids => [1, 5], :time_entry => { :activity_id => 9 }
499 post :bulk_update, :ids => [1, 5], :time_entry => { :activity_id => 9 }
500 assert_response 403
500 assert_response 403
501 end
501 end
502
502
503 def test_bulk_update_with_edit_own_time_entries_permission
503 def test_bulk_update_with_edit_own_time_entries_permission
504 @request.session[:user_id] = 2
504 @request.session[:user_id] = 2
505 Role.find_by_name('Manager').remove_permission! :edit_time_entries
505 Role.find_by_name('Manager').remove_permission! :edit_time_entries
506 Role.find_by_name('Manager').add_permission! :edit_own_time_entries
506 Role.find_by_name('Manager').add_permission! :edit_own_time_entries
507 ids = (0..1).map {TimeEntry.generate!(:user => User.find(2)).id}
507 ids = (0..1).map {TimeEntry.generate!(:user => User.find(2)).id}
508
508
509 post :bulk_update, :ids => ids, :time_entry => { :activity_id => 9 }
509 post :bulk_update, :ids => ids, :time_entry => { :activity_id => 9 }
510 assert_response 302
510 assert_response 302
511 end
511 end
512
512
513 def test_bulk_update_with_edit_own_time_entries_permissions_should_be_denied_for_time_entries_of_other_user
513 def test_bulk_update_with_edit_own_time_entries_permissions_should_be_denied_for_time_entries_of_other_user
514 @request.session[:user_id] = 2
514 @request.session[:user_id] = 2
515 Role.find_by_name('Manager').remove_permission! :edit_time_entries
515 Role.find_by_name('Manager').remove_permission! :edit_time_entries
516 Role.find_by_name('Manager').add_permission! :edit_own_time_entries
516 Role.find_by_name('Manager').add_permission! :edit_own_time_entries
517
517
518 post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9 }
518 post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9 }
519 assert_response 403
519 assert_response 403
520 end
520 end
521
521
522 def test_bulk_update_custom_field
522 def test_bulk_update_custom_field
523 @request.session[:user_id] = 2
523 @request.session[:user_id] = 2
524 post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} }
524 post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} }
525
525
526 assert_response 302
526 assert_response 302
527 assert_equal ["0", "0"], TimeEntry.where(:id => [1, 2]).collect {|i| i.custom_value_for(10).value}
527 assert_equal ["0", "0"], TimeEntry.where(:id => [1, 2]).collect {|i| i.custom_value_for(10).value}
528 end
528 end
529
529
530 def test_bulk_update_clear_custom_field
530 def test_bulk_update_clear_custom_field
531 field = TimeEntryCustomField.generate!(:field_format => 'string')
531 field = TimeEntryCustomField.generate!(:field_format => 'string')
532 @request.session[:user_id] = 2
532 @request.session[:user_id] = 2
533 post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {field.id.to_s => '__none__'} }
533 post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {field.id.to_s => '__none__'} }
534
534
535 assert_response 302
535 assert_response 302
536 assert_equal ["", ""], TimeEntry.where(:id => [1, 2]).collect {|i| i.custom_value_for(field).value}
536 assert_equal ["", ""], TimeEntry.where(:id => [1, 2]).collect {|i| i.custom_value_for(field).value}
537 end
537 end
538
538
539 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
539 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
540 @request.session[:user_id] = 2
540 @request.session[:user_id] = 2
541 post :bulk_update, :ids => [1,2], :back_url => '/time_entries'
541 post :bulk_update, :ids => [1,2], :back_url => '/time_entries'
542
542
543 assert_response :redirect
543 assert_response :redirect
544 assert_redirected_to '/time_entries'
544 assert_redirected_to '/time_entries'
545 end
545 end
546
546
547 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
547 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
548 @request.session[:user_id] = 2
548 @request.session[:user_id] = 2
549 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
549 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
550
550
551 assert_response :redirect
551 assert_response :redirect
552 assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier
552 assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier
553 end
553 end
554
554
555 def test_post_bulk_update_without_edit_permission_should_be_denied
555 def test_post_bulk_update_without_edit_permission_should_be_denied
556 @request.session[:user_id] = 2
556 @request.session[:user_id] = 2
557 Role.find_by_name('Manager').remove_permission! :edit_time_entries
557 Role.find_by_name('Manager').remove_permission! :edit_time_entries
558 post :bulk_update, :ids => [1,2]
558 post :bulk_update, :ids => [1,2]
559
559
560 assert_response 403
560 assert_response 403
561 end
561 end
562
562
563 def test_destroy
563 def test_destroy
564 @request.session[:user_id] = 2
564 @request.session[:user_id] = 2
565 delete :destroy, :id => 1
565 delete :destroy, :id => 1
566 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
566 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
567 assert_equal I18n.t(:notice_successful_delete), flash[:notice]
567 assert_equal I18n.t(:notice_successful_delete), flash[:notice]
568 assert_nil TimeEntry.find_by_id(1)
568 assert_nil TimeEntry.find_by_id(1)
569 end
569 end
570
570
571 def test_destroy_should_fail
571 def test_destroy_should_fail
572 # simulate that this fails (e.g. due to a plugin), see #5700
572 # simulate that this fails (e.g. due to a plugin), see #5700
573 TimeEntry.any_instance.expects(:destroy).returns(false)
573 TimeEntry.any_instance.expects(:destroy).returns(false)
574
574
575 @request.session[:user_id] = 2
575 @request.session[:user_id] = 2
576 delete :destroy, :id => 1
576 delete :destroy, :id => 1
577 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
577 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
578 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
578 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
579 assert_not_nil TimeEntry.find_by_id(1)
579 assert_not_nil TimeEntry.find_by_id(1)
580 end
580 end
581
581
582 def test_destroy_should_redirect_to_referer
582 def test_destroy_should_redirect_to_referer
583 referer = 'http://test.host/time_entries?utf8=βœ“&set_filter=1&&f%5B%5D=user_id&op%5Buser_id%5D=%3D&v%5Buser_id%5D%5B%5D=me'
583 referer = 'http://test.host/time_entries?utf8=βœ“&set_filter=1&&f%5B%5D=user_id&op%5Buser_id%5D=%3D&v%5Buser_id%5D%5B%5D=me'
584 @request.env["HTTP_REFERER"] = referer
584 @request.env["HTTP_REFERER"] = referer
585 @request.session[:user_id] = 2
585 @request.session[:user_id] = 2
586
586
587 delete :destroy, :params => {:id => 1}
587 delete :destroy, :id => 1
588 assert_redirected_to referer
588 assert_redirected_to referer
589 end
589 end
590
590
591 def test_index_all_projects
591 def test_index_all_projects
592 get :index
592 get :index
593 assert_response :success
593 assert_response :success
594 assert_template 'index'
594 assert_template 'index'
595 assert_not_nil assigns(:total_hours)
595 assert_not_nil assigns(:total_hours)
596 assert_equal "162.90", "%.2f" % assigns(:total_hours)
596 assert_equal "162.90", "%.2f" % assigns(:total_hours)
597 assert_select 'form#query_form[action=?]', '/time_entries'
597 assert_select 'form#query_form[action=?]', '/time_entries'
598 end
598 end
599
599
600 def test_index_all_projects_should_show_log_time_link
600 def test_index_all_projects_should_show_log_time_link
601 @request.session[:user_id] = 2
601 @request.session[:user_id] = 2
602 get :index
602 get :index
603 assert_response :success
603 assert_response :success
604 assert_template 'index'
604 assert_template 'index'
605 assert_select 'a[href=?]', '/time_entries/new', :text => /Log time/
605 assert_select 'a[href=?]', '/time_entries/new', :text => /Log time/
606 end
606 end
607
607
608 def test_index_my_spent_time
608 def test_index_my_spent_time
609 @request.session[:user_id] = 2
609 @request.session[:user_id] = 2
610 get :index, :user_id => 'me'
610 get :index, :user_id => 'me'
611 assert_response :success
611 assert_response :success
612 assert_template 'index'
612 assert_template 'index'
613 assert assigns(:entries).all? {|entry| entry.user_id == 2}
613 assert assigns(:entries).all? {|entry| entry.user_id == 2}
614 end
614 end
615
615
616 def test_index_at_project_level
616 def test_index_at_project_level
617 get :index, :project_id => 'ecookbook'
617 get :index, :project_id => 'ecookbook'
618 assert_response :success
618 assert_response :success
619 assert_template 'index'
619 assert_template 'index'
620 assert_not_nil assigns(:entries)
620 assert_not_nil assigns(:entries)
621 assert_equal 4, assigns(:entries).size
621 assert_equal 4, assigns(:entries).size
622 # project and subproject
622 # project and subproject
623 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
623 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
624 assert_not_nil assigns(:total_hours)
624 assert_not_nil assigns(:total_hours)
625 assert_equal "162.90", "%.2f" % assigns(:total_hours)
625 assert_equal "162.90", "%.2f" % assigns(:total_hours)
626 assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
626 assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
627 end
627 end
628
628
629 def test_index_with_display_subprojects_issues_to_false_should_not_include_subproject_entries
629 def test_index_with_display_subprojects_issues_to_false_should_not_include_subproject_entries
630 entry = TimeEntry.generate!(:project => Project.find(3))
630 entry = TimeEntry.generate!(:project => Project.find(3))
631
631
632 with_settings :display_subprojects_issues => '0' do
632 with_settings :display_subprojects_issues => '0' do
633 get :index, :project_id => 'ecookbook'
633 get :index, :project_id => 'ecookbook'
634 assert_response :success
634 assert_response :success
635 assert_template 'index'
635 assert_template 'index'
636 assert_not_include entry, assigns(:entries)
636 assert_not_include entry, assigns(:entries)
637 end
637 end
638 end
638 end
639
639
640 def test_index_with_display_subprojects_issues_to_false_and_subproject_filter_should_include_subproject_entries
640 def test_index_with_display_subprojects_issues_to_false_and_subproject_filter_should_include_subproject_entries
641 entry = TimeEntry.generate!(:project => Project.find(3))
641 entry = TimeEntry.generate!(:project => Project.find(3))
642
642
643 with_settings :display_subprojects_issues => '0' do
643 with_settings :display_subprojects_issues => '0' do
644 get :index, :project_id => 'ecookbook', :subproject_id => 3
644 get :index, :project_id => 'ecookbook', :subproject_id => 3
645 assert_response :success
645 assert_response :success
646 assert_template 'index'
646 assert_template 'index'
647 assert_include entry, assigns(:entries)
647 assert_include entry, assigns(:entries)
648 end
648 end
649 end
649 end
650
650
651 def test_index_at_project_level_with_date_range
651 def test_index_at_project_level_with_date_range
652 get :index, :project_id => 'ecookbook',
652 get :index, :project_id => 'ecookbook',
653 :f => ['spent_on'],
653 :f => ['spent_on'],
654 :op => {'spent_on' => '><'},
654 :op => {'spent_on' => '><'},
655 :v => {'spent_on' => ['2007-03-20', '2007-04-30']}
655 :v => {'spent_on' => ['2007-03-20', '2007-04-30']}
656 assert_response :success
656 assert_response :success
657 assert_template 'index'
657 assert_template 'index'
658 assert_not_nil assigns(:entries)
658 assert_not_nil assigns(:entries)
659 assert_equal 3, assigns(:entries).size
659 assert_equal 3, assigns(:entries).size
660 assert_not_nil assigns(:total_hours)
660 assert_not_nil assigns(:total_hours)
661 assert_equal "12.90", "%.2f" % assigns(:total_hours)
661 assert_equal "12.90", "%.2f" % assigns(:total_hours)
662 assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
662 assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
663 end
663 end
664
664
665 def test_index_at_project_level_with_date_range_using_from_and_to_params
665 def test_index_at_project_level_with_date_range_using_from_and_to_params
666 get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30'
666 get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30'
667 assert_response :success
667 assert_response :success
668 assert_template 'index'
668 assert_template 'index'
669 assert_not_nil assigns(:entries)
669 assert_not_nil assigns(:entries)
670 assert_equal 3, assigns(:entries).size
670 assert_equal 3, assigns(:entries).size
671 assert_not_nil assigns(:total_hours)
671 assert_not_nil assigns(:total_hours)
672 assert_equal "12.90", "%.2f" % assigns(:total_hours)
672 assert_equal "12.90", "%.2f" % assigns(:total_hours)
673 assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
673 assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
674 end
674 end
675
675
676 def test_index_at_project_level_with_period
676 def test_index_at_project_level_with_period
677 get :index, :project_id => 'ecookbook',
677 get :index, :project_id => 'ecookbook',
678 :f => ['spent_on'],
678 :f => ['spent_on'],
679 :op => {'spent_on' => '>t-'},
679 :op => {'spent_on' => '>t-'},
680 :v => {'spent_on' => ['7']}
680 :v => {'spent_on' => ['7']}
681 assert_response :success
681 assert_response :success
682 assert_template 'index'
682 assert_template 'index'
683 assert_not_nil assigns(:entries)
683 assert_not_nil assigns(:entries)
684 assert_not_nil assigns(:total_hours)
684 assert_not_nil assigns(:total_hours)
685 assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
685 assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
686 end
686 end
687
687
688 def test_index_at_issue_level
688 def test_index_at_issue_level
689 get :index, :issue_id => 1
689 get :index, :issue_id => 1
690 assert_response :success
690 assert_response :success
691 assert_template 'index'
691 assert_template 'index'
692 assert_not_nil assigns(:entries)
692 assert_not_nil assigns(:entries)
693 assert_equal 2, assigns(:entries).size
693 assert_equal 2, assigns(:entries).size
694 assert_not_nil assigns(:total_hours)
694 assert_not_nil assigns(:total_hours)
695 assert_equal 154.25, assigns(:total_hours)
695 assert_equal 154.25, assigns(:total_hours)
696 # display all time
696 # display all time
697 assert_nil assigns(:from)
697 assert_nil assigns(:from)
698 assert_nil assigns(:to)
698 assert_nil assigns(:to)
699 assert_select 'form#query_form[action=?]', '/issues/1/time_entries'
699 assert_select 'form#query_form[action=?]', '/issues/1/time_entries'
700 end
700 end
701
701
702 def test_index_should_sort_by_spent_on_and_created_on
702 def test_index_should_sort_by_spent_on_and_created_on
703 t1 = TimeEntry.create!(:user => User.find(1), :project => Project.find(1), :hours => 1, :spent_on => '2012-06-16', :created_on => '2012-06-16 20:00:00', :activity_id => 10)
703 t1 = TimeEntry.create!(:user => User.find(1), :project => Project.find(1), :hours => 1, :spent_on => '2012-06-16', :created_on => '2012-06-16 20:00:00', :activity_id => 10)
704 t2 = TimeEntry.create!(:user => User.find(1), :project => Project.find(1), :hours => 1, :spent_on => '2012-06-16', :created_on => '2012-06-16 20:05:00', :activity_id => 10)
704 t2 = TimeEntry.create!(:user => User.find(1), :project => Project.find(1), :hours => 1, :spent_on => '2012-06-16', :created_on => '2012-06-16 20:05:00', :activity_id => 10)
705 t3 = TimeEntry.create!(:user => User.find(1), :project => Project.find(1), :hours => 1, :spent_on => '2012-06-15', :created_on => '2012-06-16 20:10:00', :activity_id => 10)
705 t3 = TimeEntry.create!(:user => User.find(1), :project => Project.find(1), :hours => 1, :spent_on => '2012-06-15', :created_on => '2012-06-16 20:10:00', :activity_id => 10)
706
706
707 get :index, :project_id => 1,
707 get :index, :project_id => 1,
708 :f => ['spent_on'],
708 :f => ['spent_on'],
709 :op => {'spent_on' => '><'},
709 :op => {'spent_on' => '><'},
710 :v => {'spent_on' => ['2012-06-15', '2012-06-16']}
710 :v => {'spent_on' => ['2012-06-15', '2012-06-16']}
711 assert_response :success
711 assert_response :success
712 assert_equal [t2, t1, t3], assigns(:entries)
712 assert_equal [t2, t1, t3], assigns(:entries)
713
713
714 get :index, :project_id => 1,
714 get :index, :project_id => 1,
715 :f => ['spent_on'],
715 :f => ['spent_on'],
716 :op => {'spent_on' => '><'},
716 :op => {'spent_on' => '><'},
717 :v => {'spent_on' => ['2012-06-15', '2012-06-16']},
717 :v => {'spent_on' => ['2012-06-15', '2012-06-16']},
718 :sort => 'spent_on'
718 :sort => 'spent_on'
719 assert_response :success
719 assert_response :success
720 assert_equal [t3, t1, t2], assigns(:entries)
720 assert_equal [t3, t1, t2], assigns(:entries)
721 end
721 end
722
722
723 def test_index_with_filter_on_issue_custom_field
723 def test_index_with_filter_on_issue_custom_field
724 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'})
724 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'})
725 entry = TimeEntry.generate!(:issue => issue, :hours => 2.5)
725 entry = TimeEntry.generate!(:issue => issue, :hours => 2.5)
726
726
727 get :index, :f => ['issue.cf_2'], :op => {'issue.cf_2' => '='}, :v => {'issue.cf_2' => ['filter_on_issue_custom_field']}
727 get :index, :f => ['issue.cf_2'], :op => {'issue.cf_2' => '='}, :v => {'issue.cf_2' => ['filter_on_issue_custom_field']}
728 assert_response :success
728 assert_response :success
729 assert_equal [entry], assigns(:entries)
729 assert_equal [entry], assigns(:entries)
730 end
730 end
731
731
732 def test_index_with_issue_custom_field_column
732 def test_index_with_issue_custom_field_column
733 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'})
733 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'})
734 entry = TimeEntry.generate!(:issue => issue, :hours => 2.5)
734 entry = TimeEntry.generate!(:issue => issue, :hours => 2.5)
735
735
736 get :index, :c => %w(project spent_on issue comments hours issue.cf_2)
736 get :index, :c => %w(project spent_on issue comments hours issue.cf_2)
737 assert_response :success
737 assert_response :success
738 assert_include :'issue.cf_2', assigns(:query).column_names
738 assert_include :'issue.cf_2', assigns(:query).column_names
739 assert_select 'td.issue_cf_2', :text => 'filter_on_issue_custom_field'
739 assert_select 'td.issue_cf_2', :text => 'filter_on_issue_custom_field'
740 end
740 end
741
741
742 def test_index_with_time_entry_custom_field_column
742 def test_index_with_time_entry_custom_field_column
743 field = TimeEntryCustomField.generate!(:field_format => 'string')
743 field = TimeEntryCustomField.generate!(:field_format => 'string')
744 entry = TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value'})
744 entry = TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value'})
745 field_name = "cf_#{field.id}"
745 field_name = "cf_#{field.id}"
746
746
747 get :index, :c => ["hours", field_name]
747 get :index, :c => ["hours", field_name]
748 assert_response :success
748 assert_response :success
749 assert_include field_name.to_sym, assigns(:query).column_names
749 assert_include field_name.to_sym, assigns(:query).column_names
750 assert_select "td.#{field_name}", :text => 'CF Value'
750 assert_select "td.#{field_name}", :text => 'CF Value'
751 end
751 end
752
752
753 def test_index_with_time_entry_custom_field_sorting
753 def test_index_with_time_entry_custom_field_sorting
754 field = TimeEntryCustomField.generate!(:field_format => 'string', :name => 'String Field')
754 field = TimeEntryCustomField.generate!(:field_format => 'string', :name => 'String Field')
755 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 1'})
755 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 1'})
756 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 3'})
756 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 3'})
757 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 2'})
757 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 2'})
758 field_name = "cf_#{field.id}"
758 field_name = "cf_#{field.id}"
759
759
760 get :index, :c => ["hours", field_name], :sort => field_name
760 get :index, :c => ["hours", field_name], :sort => field_name
761 assert_response :success
761 assert_response :success
762 assert_include field_name.to_sym, assigns(:query).column_names
762 assert_include field_name.to_sym, assigns(:query).column_names
763 assert_select "th a.sort", :text => 'String Field'
763 assert_select "th a.sort", :text => 'String Field'
764
764
765 # Make sure that values are properly sorted
765 # Make sure that values are properly sorted
766 values = assigns(:entries).map {|e| e.custom_field_value(field)}.compact
766 values = assigns(:entries).map {|e| e.custom_field_value(field)}.compact
767 assert_equal 3, values.size
767 assert_equal 3, values.size
768 assert_equal values.sort, values
768 assert_equal values.sort, values
769 end
769 end
770
770
771 def test_index_atom_feed
771 def test_index_atom_feed
772 get :index, :project_id => 1, :format => 'atom'
772 get :index, :project_id => 1, :format => 'atom'
773 assert_response :success
773 assert_response :success
774 assert_equal 'application/atom+xml', @response.content_type
774 assert_equal 'application/atom+xml', @response.content_type
775 assert_not_nil assigns(:items)
775 assert_not_nil assigns(:items)
776 assert assigns(:items).first.is_a?(TimeEntry)
776 assert assigns(:items).first.is_a?(TimeEntry)
777 end
777 end
778
778
779 def test_index_at_project_level_should_include_csv_export_dialog
779 def test_index_at_project_level_should_include_csv_export_dialog
780 get :index, :project_id => 'ecookbook',
780 get :index, :project_id => 'ecookbook',
781 :f => ['spent_on'],
781 :f => ['spent_on'],
782 :op => {'spent_on' => '>='},
782 :op => {'spent_on' => '>='},
783 :v => {'spent_on' => ['2007-04-01']},
783 :v => {'spent_on' => ['2007-04-01']},
784 :c => ['spent_on', 'user']
784 :c => ['spent_on', 'user']
785 assert_response :success
785 assert_response :success
786
786
787 assert_select '#csv-export-options' do
787 assert_select '#csv-export-options' do
788 assert_select 'form[action=?][method=get]', '/projects/ecookbook/time_entries.csv' do
788 assert_select 'form[action=?][method=get]', '/projects/ecookbook/time_entries.csv' do
789 # filter
789 # filter
790 assert_select 'input[name=?][value=?]', 'f[]', 'spent_on'
790 assert_select 'input[name=?][value=?]', 'f[]', 'spent_on'
791 assert_select 'input[name=?][value=?]', 'op[spent_on]', '>='
791 assert_select 'input[name=?][value=?]', 'op[spent_on]', '>='
792 assert_select 'input[name=?][value=?]', 'v[spent_on][]', '2007-04-01'
792 assert_select 'input[name=?][value=?]', 'v[spent_on][]', '2007-04-01'
793 # columns
793 # columns
794 assert_select 'input[name=?][value=?]', 'c[]', 'spent_on'
794 assert_select 'input[name=?][value=?]', 'c[]', 'spent_on'
795 assert_select 'input[name=?][value=?]', 'c[]', 'user'
795 assert_select 'input[name=?][value=?]', 'c[]', 'user'
796 assert_select 'input[name=?]', 'c[]', 2
796 assert_select 'input[name=?]', 'c[]', 2
797 end
797 end
798 end
798 end
799 end
799 end
800
800
801 def test_index_cross_project_should_include_csv_export_dialog
801 def test_index_cross_project_should_include_csv_export_dialog
802 get :index
802 get :index
803 assert_response :success
803 assert_response :success
804
804
805 assert_select '#csv-export-options' do
805 assert_select '#csv-export-options' do
806 assert_select 'form[action=?][method=get]', '/time_entries.csv'
806 assert_select 'form[action=?][method=get]', '/time_entries.csv'
807 end
807 end
808 end
808 end
809
809
810 def test_index_at_issue_level_should_include_csv_export_dialog
810 def test_index_at_issue_level_should_include_csv_export_dialog
811 get :index, :issue_id => 3
811 get :index, :issue_id => 3
812 assert_response :success
812 assert_response :success
813
813
814 assert_select '#csv-export-options' do
814 assert_select '#csv-export-options' do
815 assert_select 'form[action=?][method=get]', '/issues/3/time_entries.csv'
815 assert_select 'form[action=?][method=get]', '/issues/3/time_entries.csv'
816 end
816 end
817 end
817 end
818
818
819 def test_index_csv_all_projects
819 def test_index_csv_all_projects
820 with_settings :date_format => '%m/%d/%Y' do
820 with_settings :date_format => '%m/%d/%Y' do
821 get :index, :format => 'csv'
821 get :index, :format => 'csv'
822 assert_response :success
822 assert_response :success
823 assert_equal 'text/csv; header=present', response.content_type
823 assert_equal 'text/csv; header=present', response.content_type
824 end
824 end
825 end
825 end
826
826
827 def test_index_csv
827 def test_index_csv
828 with_settings :date_format => '%m/%d/%Y' do
828 with_settings :date_format => '%m/%d/%Y' do
829 get :index, :project_id => 1, :format => 'csv'
829 get :index, :project_id => 1, :format => 'csv'
830 assert_response :success
830 assert_response :success
831 assert_equal 'text/csv; header=present', response.content_type
831 assert_equal 'text/csv; header=present', response.content_type
832 end
832 end
833 end
833 end
834
834
835 def test_index_csv_should_fill_issue_column_with_tracker_id_and_subject
835 def test_index_csv_should_fill_issue_column_with_tracker_id_and_subject
836 issue = Issue.find(1)
836 issue = Issue.find(1)
837 entry = TimeEntry.generate!(:issue => issue, :comments => "Issue column content test")
837 entry = TimeEntry.generate!(:issue => issue, :comments => "Issue column content test")
838
838
839 get :index, :format => 'csv'
839 get :index, :format => 'csv'
840 line = response.body.split("\n").detect {|l| l.include?(entry.comments)}
840 line = response.body.split("\n").detect {|l| l.include?(entry.comments)}
841 assert_not_nil line
841 assert_not_nil line
842 assert_include "#{issue.tracker} #1: #{issue.subject}", line
842 assert_include "#{issue.tracker} #1: #{issue.subject}", line
843 end
843 end
844 end
844 end
General Comments 0
You need to be logged in to leave comments. Login now