comparison test/functional/timelog_controller_test.rb @ 37:94944d00e43c

* Update to SVN trunk rev 4411
author Chris Cannam <chris.cannam@soundsoftware.ac.uk>
date Fri, 19 Nov 2010 13:24:41 +0000
parents 40f7cfd4df19
children af80e5618e9b
comparison
equal deleted inserted replaced
22:40f7cfd4df19 37:94944d00e43c
29 @controller = TimelogController.new 29 @controller = TimelogController.new
30 @request = ActionController::TestRequest.new 30 @request = ActionController::TestRequest.new
31 @response = ActionController::TestResponse.new 31 @response = ActionController::TestResponse.new
32 end 32 end
33 33
34 def test_get_edit 34 def test_get_new
35 @request.session[:user_id] = 3 35 @request.session[:user_id] = 3
36 get :edit, :project_id => 1 36 get :new, :project_id => 1
37 assert_response :success 37 assert_response :success
38 assert_template 'edit' 38 assert_template 'edit'
39 # Default activity selected 39 # Default activity selected
40 assert_tag :tag => 'option', :attributes => { :selected => 'selected' }, 40 assert_tag :tag => 'option', :attributes => { :selected => 'selected' },
41 :content => 'Development' 41 :content => 'Development'
42 end 42 end
43 43
44 def test_get_new_should_only_show_active_time_entry_activities
45 @request.session[:user_id] = 3
46 get :new, :project_id => 1
47 assert_response :success
48 assert_template 'edit'
49 assert_no_tag :tag => 'option', :content => 'Inactive Activity'
50
51 end
52
44 def test_get_edit_existing_time 53 def test_get_edit_existing_time
45 @request.session[:user_id] = 2 54 @request.session[:user_id] = 2
46 get :edit, :id => 2, :project_id => nil 55 get :edit, :id => 2, :project_id => nil
47 assert_response :success 56 assert_response :success
48 assert_template 'edit' 57 assert_template 'edit'
49 # Default activity selected 58 # Default activity selected
50 assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/timelog/edit/2' } 59 assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/time_entries/2' }
51 end 60 end
52 61
53 def test_get_edit_should_only_show_active_time_entry_activities
54 @request.session[:user_id] = 3
55 get :edit, :project_id => 1
56 assert_response :success
57 assert_template 'edit'
58 assert_no_tag :tag => 'option', :content => 'Inactive Activity'
59
60 end
61
62 def test_get_edit_with_an_existing_time_entry_with_inactive_activity 62 def test_get_edit_with_an_existing_time_entry_with_inactive_activity
63 te = TimeEntry.find(1) 63 te = TimeEntry.find(1)
64 te.activity = TimeEntryActivity.find_by_name("Inactive Activity") 64 te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
65 te.save! 65 te.save!
66 66
70 assert_template 'edit' 70 assert_template 'edit'
71 # Blank option since nothing is pre-selected 71 # Blank option since nothing is pre-selected
72 assert_tag :tag => 'option', :content => '--- Please select ---' 72 assert_tag :tag => 'option', :content => '--- Please select ---'
73 end 73 end
74 74
75 def test_post_edit 75 def test_post_create
76 # TODO: should POST to issues’ time log instead of project. change form 76 # TODO: should POST to issues’ time log instead of project. change form
77 # and routing 77 # and routing
78 @request.session[:user_id] = 3 78 @request.session[:user_id] = 3
79 post :edit, :project_id => 1, 79 post :create, :project_id => 1,
80 :time_entry => {:comments => 'Some work on TimelogControllerTest', 80 :time_entry => {:comments => 'Some work on TimelogControllerTest',
81 # Not the default activity 81 # Not the default activity
82 :activity_id => '11', 82 :activity_id => '11',
83 :spent_on => '2008-03-14', 83 :spent_on => '2008-03-14',
84 :issue_id => '1', 84 :issue_id => '1',
85 :hours => '7.3'} 85 :hours => '7.3'}
86 assert_redirected_to :action => 'details', :project_id => 'ecookbook' 86 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
87 87
88 i = Issue.find(1) 88 i = Issue.find(1)
89 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest') 89 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
90 assert_not_nil t 90 assert_not_nil t
91 assert_equal 11, t.activity_id 91 assert_equal 11, t.activity_id
99 entry = TimeEntry.find(1) 99 entry = TimeEntry.find(1)
100 assert_equal 1, entry.issue_id 100 assert_equal 1, entry.issue_id
101 assert_equal 2, entry.user_id 101 assert_equal 2, entry.user_id
102 102
103 @request.session[:user_id] = 1 103 @request.session[:user_id] = 1
104 post :edit, :id => 1, 104 put :update, :id => 1,
105 :time_entry => {:issue_id => '2', 105 :time_entry => {:issue_id => '2',
106 :hours => '8'} 106 :hours => '8'}
107 assert_redirected_to :action => 'details', :project_id => 'ecookbook' 107 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
108 entry.reload 108 entry.reload
109 109
110 assert_equal 8, entry.hours 110 assert_equal 8, entry.hours
111 assert_equal 2, entry.issue_id 111 assert_equal 2, entry.issue_id
112 assert_equal 2, entry.user_id 112 assert_equal 2, entry.user_id
113 end 113 end
114 114
115 def test_destroy 115 def test_destroy
116 @request.session[:user_id] = 2 116 @request.session[:user_id] = 2
117 post :destroy, :id => 1 117 delete :destroy, :id => 1
118 assert_redirected_to :action => 'details', :project_id => 'ecookbook' 118 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
119 assert_equal I18n.t(:notice_successful_delete), flash[:notice] 119 assert_equal I18n.t(:notice_successful_delete), flash[:notice]
120 assert_nil TimeEntry.find_by_id(1) 120 assert_nil TimeEntry.find_by_id(1)
121 end 121 end
122 122
123 def test_destroy_should_fail 123 def test_destroy_should_fail
126 before_destroy :stop_callback_chain 126 before_destroy :stop_callback_chain
127 def stop_callback_chain ; return false ; end 127 def stop_callback_chain ; return false ; end
128 end 128 end
129 129
130 @request.session[:user_id] = 2 130 @request.session[:user_id] = 2
131 post :destroy, :id => 1 131 delete :destroy, :id => 1
132 assert_redirected_to :action => 'details', :project_id => 'ecookbook' 132 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
133 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error] 133 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
134 assert_not_nil TimeEntry.find_by_id(1) 134 assert_not_nil TimeEntry.find_by_id(1)
135 135
136 # remove the simulation 136 # remove the simulation
137 TimeEntry.before_destroy.reject! {|callback| callback.method == :stop_callback_chain } 137 TimeEntry.before_destroy.reject! {|callback| callback.method == :stop_callback_chain }
138 end 138 end
139 139
140 def test_report_no_criteria 140 def test_index_all_projects
141 get :report, :project_id => 1 141 get :index
142 assert_response :success 142 assert_response :success
143 assert_template 'report' 143 assert_template 'index'
144 end
145
146 def test_report_all_projects
147 get :report
148 assert_response :success
149 assert_template 'report'
150 end
151
152 def test_report_all_projects_denied
153 r = Role.anonymous
154 r.permissions.delete(:view_time_entries)
155 r.permissions_will_change!
156 r.save
157 get :report
158 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Ftime_entries%2Freport'
159 end
160
161 def test_report_all_projects_one_criteria
162 get :report, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
163 assert_response :success
164 assert_template 'report'
165 assert_not_nil assigns(:total_hours)
166 assert_equal "8.65", "%.2f" % assigns(:total_hours)
167 end
168
169 def test_report_all_time
170 get :report, :project_id => 1, :criterias => ['project', 'issue']
171 assert_response :success
172 assert_template 'report'
173 assert_not_nil assigns(:total_hours) 144 assert_not_nil assigns(:total_hours)
174 assert_equal "162.90", "%.2f" % assigns(:total_hours) 145 assert_equal "162.90", "%.2f" % assigns(:total_hours)
175 end 146 end
176 147
177 def test_report_all_time_by_day 148 def test_index_at_project_level
178 get :report, :project_id => 1, :criterias => ['project', 'issue'], :columns => 'day' 149 get :index, :project_id => 1
179 assert_response :success 150 assert_response :success
180 assert_template 'report' 151 assert_template 'index'
181 assert_not_nil assigns(:total_hours)
182 assert_equal "162.90", "%.2f" % assigns(:total_hours)
183 assert_tag :tag => 'th', :content => '2007-03-12'
184 end
185
186 def test_report_one_criteria
187 get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
188 assert_response :success
189 assert_template 'report'
190 assert_not_nil assigns(:total_hours)
191 assert_equal "8.65", "%.2f" % assigns(:total_hours)
192 end
193
194 def test_report_two_criterias
195 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"]
196 assert_response :success
197 assert_template 'report'
198 assert_not_nil assigns(:total_hours)
199 assert_equal "162.90", "%.2f" % assigns(:total_hours)
200 end
201
202 def test_report_one_day
203 get :report, :project_id => 1, :columns => 'day', :from => "2007-03-23", :to => "2007-03-23", :criterias => ["member", "activity"]
204 assert_response :success
205 assert_template 'report'
206 assert_not_nil assigns(:total_hours)
207 assert_equal "4.25", "%.2f" % assigns(:total_hours)
208 end
209
210 def test_report_at_issue_level
211 get :report, :project_id => 1, :issue_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"]
212 assert_response :success
213 assert_template 'report'
214 assert_not_nil assigns(:total_hours)
215 assert_equal "154.25", "%.2f" % assigns(:total_hours)
216 end
217
218 def test_report_custom_field_criteria
219 get :report, :project_id => 1, :criterias => ['project', 'cf_1', 'cf_7']
220 assert_response :success
221 assert_template 'report'
222 assert_not_nil assigns(:total_hours)
223 assert_not_nil assigns(:criterias)
224 assert_equal 3, assigns(:criterias).size
225 assert_equal "162.90", "%.2f" % assigns(:total_hours)
226 # Custom field column
227 assert_tag :tag => 'th', :content => 'Database'
228 # Custom field row
229 assert_tag :tag => 'td', :content => 'MySQL',
230 :sibling => { :tag => 'td', :attributes => { :class => 'hours' },
231 :child => { :tag => 'span', :attributes => { :class => 'hours hours-int' },
232 :content => '1' }}
233 # Second custom field column
234 assert_tag :tag => 'th', :content => 'Billable'
235 end
236
237 def test_report_one_criteria_no_result
238 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criterias => ['project']
239 assert_response :success
240 assert_template 'report'
241 assert_not_nil assigns(:total_hours)
242 assert_equal "0.00", "%.2f" % assigns(:total_hours)
243 end
244
245 def test_report_all_projects_csv_export
246 get :report, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", :criterias => ["project", "member", "activity"], :format => "csv"
247 assert_response :success
248 assert_equal 'text/csv', @response.content_type
249 lines = @response.body.chomp.split("\n")
250 # Headers
251 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total', lines.first
252 # Total row
253 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
254 end
255
256 def test_report_csv_export
257 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", :criterias => ["project", "member", "activity"], :format => "csv"
258 assert_response :success
259 assert_equal 'text/csv', @response.content_type
260 lines = @response.body.chomp.split("\n")
261 # Headers
262 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total', lines.first
263 # Total row
264 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
265 end
266
267 def test_details_all_projects
268 get :details
269 assert_response :success
270 assert_template 'details'
271 assert_not_nil assigns(:total_hours)
272 assert_equal "162.90", "%.2f" % assigns(:total_hours)
273 end
274
275 def test_details_at_project_level
276 get :details, :project_id => 1
277 assert_response :success
278 assert_template 'details'
279 assert_not_nil assigns(:entries) 152 assert_not_nil assigns(:entries)
280 assert_equal 4, assigns(:entries).size 153 assert_equal 4, assigns(:entries).size
281 # project and subproject 154 # project and subproject
282 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort 155 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
283 assert_not_nil assigns(:total_hours) 156 assert_not_nil assigns(:total_hours)
285 # display all time by default 158 # display all time by default
286 assert_equal '2007-03-12'.to_date, assigns(:from) 159 assert_equal '2007-03-12'.to_date, assigns(:from)
287 assert_equal '2007-04-22'.to_date, assigns(:to) 160 assert_equal '2007-04-22'.to_date, assigns(:to)
288 end 161 end
289 162
290 def test_details_at_project_level_with_date_range 163 def test_index_at_project_level_with_date_range
291 get :details, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30' 164 get :index, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30'
292 assert_response :success 165 assert_response :success
293 assert_template 'details' 166 assert_template 'index'
294 assert_not_nil assigns(:entries) 167 assert_not_nil assigns(:entries)
295 assert_equal 3, assigns(:entries).size 168 assert_equal 3, assigns(:entries).size
296 assert_not_nil assigns(:total_hours) 169 assert_not_nil assigns(:total_hours)
297 assert_equal "12.90", "%.2f" % assigns(:total_hours) 170 assert_equal "12.90", "%.2f" % assigns(:total_hours)
298 assert_equal '2007-03-20'.to_date, assigns(:from) 171 assert_equal '2007-03-20'.to_date, assigns(:from)
299 assert_equal '2007-04-30'.to_date, assigns(:to) 172 assert_equal '2007-04-30'.to_date, assigns(:to)
300 end 173 end
301 174
302 def test_details_at_project_level_with_period 175 def test_index_at_project_level_with_period
303 get :details, :project_id => 1, :period => '7_days' 176 get :index, :project_id => 1, :period => '7_days'
304 assert_response :success 177 assert_response :success
305 assert_template 'details' 178 assert_template 'index'
306 assert_not_nil assigns(:entries) 179 assert_not_nil assigns(:entries)
307 assert_not_nil assigns(:total_hours) 180 assert_not_nil assigns(:total_hours)
308 assert_equal Date.today - 7, assigns(:from) 181 assert_equal Date.today - 7, assigns(:from)
309 assert_equal Date.today, assigns(:to) 182 assert_equal Date.today, assigns(:to)
310 end 183 end
311 184
312 def test_details_one_day 185 def test_index_one_day
313 get :details, :project_id => 1, :from => "2007-03-23", :to => "2007-03-23" 186 get :index, :project_id => 1, :from => "2007-03-23", :to => "2007-03-23"
314 assert_response :success 187 assert_response :success
315 assert_template 'details' 188 assert_template 'index'
316 assert_not_nil assigns(:total_hours) 189 assert_not_nil assigns(:total_hours)
317 assert_equal "4.25", "%.2f" % assigns(:total_hours) 190 assert_equal "4.25", "%.2f" % assigns(:total_hours)
318 end 191 end
319 192
320 def test_details_at_issue_level 193 def test_index_at_issue_level
321 get :details, :issue_id => 1 194 get :index, :issue_id => 1
322 assert_response :success 195 assert_response :success
323 assert_template 'details' 196 assert_template 'index'
324 assert_not_nil assigns(:entries) 197 assert_not_nil assigns(:entries)
325 assert_equal 2, assigns(:entries).size 198 assert_equal 2, assigns(:entries).size
326 assert_not_nil assigns(:total_hours) 199 assert_not_nil assigns(:total_hours)
327 assert_equal 154.25, assigns(:total_hours) 200 assert_equal 154.25, assigns(:total_hours)
328 # display all time based on what's been logged 201 # display all time based on what's been logged
329 assert_equal '2007-03-12'.to_date, assigns(:from) 202 assert_equal '2007-03-12'.to_date, assigns(:from)
330 assert_equal '2007-04-22'.to_date, assigns(:to) 203 assert_equal '2007-04-22'.to_date, assigns(:to)
331 end 204 end
332 205
333 def test_details_atom_feed 206 def test_index_atom_feed
334 get :details, :project_id => 1, :format => 'atom' 207 get :index, :project_id => 1, :format => 'atom'
335 assert_response :success 208 assert_response :success
336 assert_equal 'application/atom+xml', @response.content_type 209 assert_equal 'application/atom+xml', @response.content_type
337 assert_not_nil assigns(:items) 210 assert_not_nil assigns(:items)
338 assert assigns(:items).first.is_a?(TimeEntry) 211 assert assigns(:items).first.is_a?(TimeEntry)
339 end 212 end
340 213
341 def test_details_all_projects_csv_export 214 def test_index_all_projects_csv_export
342 Setting.date_format = '%m/%d/%Y' 215 Setting.date_format = '%m/%d/%Y'
343 get :details, :format => 'csv' 216 get :index, :format => 'csv'
344 assert_response :success 217 assert_response :success
345 assert_equal 'text/csv', @response.content_type 218 assert_equal 'text/csv', @response.content_type
346 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n") 219 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
347 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n") 220 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n")
348 end 221 end
349 222
350 def test_details_csv_export 223 def test_index_csv_export
351 Setting.date_format = '%m/%d/%Y' 224 Setting.date_format = '%m/%d/%Y'
352 get :details, :project_id => 1, :format => 'csv' 225 get :index, :project_id => 1, :format => 'csv'
353 assert_response :success 226 assert_response :success
354 assert_equal 'text/csv', @response.content_type 227 assert_equal 'text/csv', @response.content_type
355 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n") 228 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
356 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n") 229 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n")
357 end 230 end