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