;]
[askyou.git] / test / functional / timelog_controller_test.rb
blob0559a95ca55f83bb50e01b3627492b6eb4d0f67c
1 # -*- coding: utf-8 -*-
2 # redMine - project management software
3 # Copyright (C) 2006-2007  Jean-Philippe Lang
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14
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
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
19 require File.dirname(__FILE__) + '/../test_helper'
20 require 'timelog_controller'
22 # Re-raise errors caught by the controller.
23 class TimelogController; def rescue_action(e) raise e end; end
25 class TimelogControllerTest < ActionController::TestCase
26   fixtures :projects, :enabled_modules, :roles, :members, :member_roles, :issues, :time_entries, :users, :trackers, :enumerations, :issue_statuses, :custom_fields, :custom_values
28   def setup
29     @controller = TimelogController.new
30     @request    = ActionController::TestRequest.new
31     @response   = ActionController::TestResponse.new
32   end
33   
34   def test_get_edit
35     @request.session[:user_id] = 3
36     get :edit, :project_id => 1
37     assert_response :success
38     assert_template 'edit'
39     # Default activity selected
40     assert_tag :tag => 'option', :attributes => { :selected => 'selected' },
41                                  :content => 'Development'
42   end
43   
44   def test_get_edit_existing_time
45     @request.session[:user_id] = 2
46     get :edit, :id => 2, :project_id => nil
47     assert_response :success
48     assert_template 'edit'
49     # Default activity selected
50     assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/timelog/edit/2' }
51   end
52   
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
62   def test_get_edit_with_an_existing_time_entry_with_inactive_activity
63     te = TimeEntry.find(1)
64     te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
65     te.save!
67     @request.session[:user_id] = 1
68     get :edit, :project_id => 1, :id => 1
69     assert_response :success
70     assert_template 'edit'
71     # Blank option since nothing is pre-selected
72     assert_tag :tag => 'option', :content => '--- Please select ---'
73   end
74   
75   def test_post_edit
76     # TODO: should POST to issues’ time log instead of project. change form
77     # and routing
78     @request.session[:user_id] = 3
79     post :edit, :project_id => 1,
80                 :time_entry => {:comments => 'Some work on TimelogControllerTest',
81                                 # Not the default activity
82                                 :activity_id => '11',
83                                 :spent_on => '2008-03-14',
84                                 :issue_id => '1',
85                                 :hours => '7.3'}
86     assert_redirected_to :action => 'details', :project_id => 'ecookbook'
87     
88     i = Issue.find(1)
89     t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
90     assert_not_nil t
91     assert_equal 11, t.activity_id
92     assert_equal 7.3, t.hours
93     assert_equal 3, t.user_id
94     assert_equal i, t.issue
95     assert_equal i.project, t.project
96   end
97   
98   def test_update
99     entry = TimeEntry.find(1)
100     assert_equal 1, entry.issue_id
101     assert_equal 2, entry.user_id
102     
103     @request.session[:user_id] = 1
104     post :edit, :id => 1,
105                 :time_entry => {:issue_id => '2',
106                                 :hours => '8'}
107     assert_redirected_to :action => 'details', :project_id => 'ecookbook'
108     entry.reload
109     
110     assert_equal 8, entry.hours
111     assert_equal 2, entry.issue_id
112     assert_equal 2, entry.user_id
113   end
114   
115   def test_destroy
116     @request.session[:user_id] = 2
117     post :destroy, :id => 1
118     assert_redirected_to :action => 'details', :project_id => 'ecookbook'
119     assert_equal I18n.t(:notice_successful_delete), flash[:notice]
120     assert_nil TimeEntry.find_by_id(1)
121   end
122   
123   def test_destroy_should_fail
124     # simulate that this fails (e.g. due to a plugin), see #5700
125     TimeEntry.class_eval do
126       before_destroy :stop_callback_chain
127       def stop_callback_chain ; return false ; end
128     end
130     @request.session[:user_id] = 2
131     post :destroy, :id => 1
132     assert_redirected_to :action => 'details', :project_id => 'ecookbook'
133     assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
134     assert_not_nil TimeEntry.find_by_id(1)
136     # remove the simulation
137     TimeEntry.before_destroy.reject! {|callback| callback.method == :stop_callback_chain }
138   end
139   
140   def test_report_no_criteria
141     get :report, :project_id => 1
142     assert_response :success
143     assert_template 'report'
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
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)
174     assert_equal "162.90", "%.2f" % assigns(:total_hours)
175   end
177   def test_report_all_time_by_day
178     get :report, :project_id => 1, :criterias => ['project', 'issue'], :columns => 'day'
179     assert_response :success
180     assert_template 'report'
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)
280     assert_equal 4, assigns(:entries).size
281     # project and subproject
282     assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
283     assert_not_nil assigns(:total_hours)
284     assert_equal "162.90", "%.2f" % assigns(:total_hours)
285     # display all time by default
286     assert_equal '2007-03-12'.to_date, assigns(:from)
287     assert_equal '2007-04-22'.to_date, assigns(:to)
288   end
289   
290   def test_details_at_project_level_with_date_range
291     get :details, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30'
292     assert_response :success
293     assert_template 'details'
294     assert_not_nil assigns(:entries)
295     assert_equal 3, assigns(:entries).size
296     assert_not_nil assigns(:total_hours)
297     assert_equal "12.90", "%.2f" % assigns(:total_hours)
298     assert_equal '2007-03-20'.to_date, assigns(:from)
299     assert_equal '2007-04-30'.to_date, assigns(:to)
300   end
302   def test_details_at_project_level_with_period
303     get :details, :project_id => 1, :period => '7_days'
304     assert_response :success
305     assert_template 'details'
306     assert_not_nil assigns(:entries)
307     assert_not_nil assigns(:total_hours)
308     assert_equal Date.today - 7, assigns(:from)
309     assert_equal Date.today, assigns(:to)
310   end
312   def test_details_one_day
313     get :details, :project_id => 1, :from => "2007-03-23", :to => "2007-03-23"
314     assert_response :success
315     assert_template 'details'
316     assert_not_nil assigns(:total_hours)
317     assert_equal "4.25", "%.2f" % assigns(:total_hours)
318   end
319   
320   def test_details_at_issue_level
321     get :details, :issue_id => 1
322     assert_response :success
323     assert_template 'details'
324     assert_not_nil assigns(:entries)
325     assert_equal 2, assigns(:entries).size
326     assert_not_nil assigns(:total_hours)
327     assert_equal 154.25, assigns(:total_hours)
328     # display all time based on what's been logged
329     assert_equal '2007-03-12'.to_date, assigns(:from)
330     assert_equal '2007-04-22'.to_date, assigns(:to)
331   end
332   
333   def test_details_atom_feed
334     get :details, :project_id => 1, :format => 'atom'
335     assert_response :success
336     assert_equal 'application/atom+xml', @response.content_type
337     assert_not_nil assigns(:items)
338     assert assigns(:items).first.is_a?(TimeEntry)
339   end
340   
341   def test_details_all_projects_csv_export
342     Setting.date_format = '%m/%d/%Y'
343     get :details, :format => 'csv'
344     assert_response :success
345     assert_equal 'text/csv', @response.content_type
346     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")
348   end
349   
350   def test_details_csv_export
351     Setting.date_format = '%m/%d/%Y'
352     get :details, :project_id => 1, :format => 'csv'
353     assert_response :success
354     assert_equal 'text/csv', @response.content_type
355     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")
357   end