comparison test/functional/timelog_controller_test.rb @ 511:107d36338b70 live

Merge from branch "cannam"
author Chris Cannam
date Thu, 14 Jul 2011 10:43:07 +0100
parents cbce1fd3b1b7
children cbb26bc654de
comparison
equal deleted inserted replaced
451:a9f6345cb43d 511:107d36338b70
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.dirname(__FILE__) + '/../test_helper' 19 require File.expand_path('../../test_helper', __FILE__)
20 require 'timelog_controller' 20 require 'timelog_controller'
21 21
22 # Re-raise errors caught by the controller. 22 # Re-raise errors caught by the controller.
23 class TimelogController; def rescue_action(e) raise e end; end 23 class TimelogController; def rescue_action(e) raise e end; end
24 24
92 assert_equal 7.3, t.hours 92 assert_equal 7.3, t.hours
93 assert_equal 3, t.user_id 93 assert_equal 3, t.user_id
94 assert_equal i, t.issue 94 assert_equal i, t.issue
95 assert_equal i.project, t.project 95 assert_equal i.project, t.project
96 end 96 end
97
98 def test_post_create_with_blank_issue
99 # TODO: should POST to issues’ time log instead of project. change form
100 # and routing
101 @request.session[:user_id] = 3
102 post :create, :project_id => 1,
103 :time_entry => {:comments => 'Some work on TimelogControllerTest',
104 # Not the default activity
105 :activity_id => '11',
106 :issue_id => '',
107 :spent_on => '2008-03-14',
108 :hours => '7.3'}
109 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
110
111 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
112 assert_not_nil t
113 assert_equal 11, t.activity_id
114 assert_equal 7.3, t.hours
115 assert_equal 3, t.user_id
116 end
97 117
98 def test_update 118 def test_update
99 entry = TimeEntry.find(1) 119 entry = TimeEntry.find(1)
100 assert_equal 1, entry.issue_id 120 assert_equal 1, entry.issue_id
101 assert_equal 2, entry.user_id 121 assert_equal 2, entry.user_id
109 129
110 assert_equal 8, entry.hours 130 assert_equal 8, entry.hours
111 assert_equal 2, entry.issue_id 131 assert_equal 2, entry.issue_id
112 assert_equal 2, entry.user_id 132 assert_equal 2, entry.user_id
113 end 133 end
134
135 def test_get_bulk_edit
136 @request.session[:user_id] = 2
137 get :bulk_edit, :ids => [1, 2]
138 assert_response :success
139 assert_template 'bulk_edit'
140
141 # System wide custom field
142 assert_tag :select, :attributes => {:name => 'time_entry[custom_field_values][10]'}
143 end
144
145 def test_get_bulk_edit_on_different_projects
146 @request.session[:user_id] = 2
147 get :bulk_edit, :ids => [1, 2, 6]
148 assert_response :success
149 assert_template 'bulk_edit'
150 end
151
152 def test_bulk_update
153 @request.session[:user_id] = 2
154 # update time entry activity
155 post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9}
156
157 assert_response 302
158 # check that the issues were updated
159 assert_equal [9, 9], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.activity_id}
160 end
161
162 def test_bulk_update_on_different_projects
163 @request.session[:user_id] = 2
164 # update time entry activity
165 post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 }
166
167 assert_response 302
168 # check that the issues were updated
169 assert_equal [9, 9, 9], TimeEntry.find_all_by_id([1, 2, 4]).collect {|i| i.activity_id}
170 end
171
172 def test_bulk_update_on_different_projects_without_rights
173 @request.session[:user_id] = 3
174 user = User.find(3)
175 action = { :controller => "timelog", :action => "bulk_update" }
176 assert user.allowed_to?(action, TimeEntry.find(1).project)
177 assert ! user.allowed_to?(action, TimeEntry.find(5).project)
178 post :bulk_update, :ids => [1, 5], :time_entry => { :activity_id => 9 }
179 assert_response 403
180 end
181
182 def test_bulk_update_custom_field
183 @request.session[:user_id] = 2
184 post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} }
185
186 assert_response 302
187 assert_equal ["0", "0"], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.custom_value_for(10).value}
188 end
189
190 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
191 @request.session[:user_id] = 2
192 post :bulk_update, :ids => [1,2], :back_url => '/time_entries'
193
194 assert_response :redirect
195 assert_redirected_to '/time_entries'
196 end
197
198 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
199 @request.session[:user_id] = 2
200 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
201
202 assert_response :redirect
203 assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier
204 end
114 205
115 def test_destroy 206 def test_destroy
116 @request.session[:user_id] = 2 207 @request.session[:user_id] = 2
117 delete :destroy, :id => 1 208 delete :destroy, :id => 1
118 assert_redirected_to :action => 'index', :project_id => 'ecookbook' 209 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
120 assert_nil TimeEntry.find_by_id(1) 211 assert_nil TimeEntry.find_by_id(1)
121 end 212 end
122 213
123 def test_destroy_should_fail 214 def test_destroy_should_fail
124 # simulate that this fails (e.g. due to a plugin), see #5700 215 # simulate that this fails (e.g. due to a plugin), see #5700
125 TimeEntry.class_eval do 216 TimeEntry.any_instance.expects(:destroy).returns(false)
126 before_destroy :stop_callback_chain
127 def stop_callback_chain ; return false ; end
128 end
129 217
130 @request.session[:user_id] = 2 218 @request.session[:user_id] = 2
131 delete :destroy, :id => 1 219 delete :destroy, :id => 1
132 assert_redirected_to :action => 'index', :project_id => 'ecookbook' 220 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
133 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error] 221 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
134 assert_not_nil TimeEntry.find_by_id(1) 222 assert_not_nil TimeEntry.find_by_id(1)
135
136 # remove the simulation
137 TimeEntry.before_destroy.reject! {|callback| callback.method == :stop_callback_chain }
138 end 223 end
139 224
140 def test_index_all_projects 225 def test_index_all_projects
141 get :index 226 get :index
142 assert_response :success 227 assert_response :success
143 assert_template 'index' 228 assert_template 'index'
144 assert_not_nil assigns(:total_hours) 229 assert_not_nil assigns(:total_hours)
145 assert_equal "162.90", "%.2f" % assigns(:total_hours) 230 assert_equal "162.90", "%.2f" % assigns(:total_hours)
231 assert_tag :form,
232 :attributes => {:action => "/time_entries", :id => 'query_form'}
146 end 233 end
147 234
148 def test_index_at_project_level 235 def test_index_at_project_level
149 get :index, :project_id => 1 236 get :index, :project_id => 'ecookbook'
150 assert_response :success 237 assert_response :success
151 assert_template 'index' 238 assert_template 'index'
152 assert_not_nil assigns(:entries) 239 assert_not_nil assigns(:entries)
153 assert_equal 4, assigns(:entries).size 240 assert_equal 4, assigns(:entries).size
154 # project and subproject 241 # project and subproject
156 assert_not_nil assigns(:total_hours) 243 assert_not_nil assigns(:total_hours)
157 assert_equal "162.90", "%.2f" % assigns(:total_hours) 244 assert_equal "162.90", "%.2f" % assigns(:total_hours)
158 # display all time by default 245 # display all time by default
159 assert_equal '2007-03-12'.to_date, assigns(:from) 246 assert_equal '2007-03-12'.to_date, assigns(:from)
160 assert_equal '2007-04-22'.to_date, assigns(:to) 247 assert_equal '2007-04-22'.to_date, assigns(:to)
248 assert_tag :form,
249 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
161 end 250 end
162 251
163 def test_index_at_project_level_with_date_range 252 def test_index_at_project_level_with_date_range
164 get :index, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30' 253 get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30'
165 assert_response :success 254 assert_response :success
166 assert_template 'index' 255 assert_template 'index'
167 assert_not_nil assigns(:entries) 256 assert_not_nil assigns(:entries)
168 assert_equal 3, assigns(:entries).size 257 assert_equal 3, assigns(:entries).size
169 assert_not_nil assigns(:total_hours) 258 assert_not_nil assigns(:total_hours)
170 assert_equal "12.90", "%.2f" % assigns(:total_hours) 259 assert_equal "12.90", "%.2f" % assigns(:total_hours)
171 assert_equal '2007-03-20'.to_date, assigns(:from) 260 assert_equal '2007-03-20'.to_date, assigns(:from)
172 assert_equal '2007-04-30'.to_date, assigns(:to) 261 assert_equal '2007-04-30'.to_date, assigns(:to)
262 assert_tag :form,
263 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
173 end 264 end
174 265
175 def test_index_at_project_level_with_period 266 def test_index_at_project_level_with_period
176 get :index, :project_id => 1, :period => '7_days' 267 get :index, :project_id => 'ecookbook', :period => '7_days'
177 assert_response :success 268 assert_response :success
178 assert_template 'index' 269 assert_template 'index'
179 assert_not_nil assigns(:entries) 270 assert_not_nil assigns(:entries)
180 assert_not_nil assigns(:total_hours) 271 assert_not_nil assigns(:total_hours)
181 assert_equal Date.today - 7, assigns(:from) 272 assert_equal Date.today - 7, assigns(:from)
182 assert_equal Date.today, assigns(:to) 273 assert_equal Date.today, assigns(:to)
274 assert_tag :form,
275 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
183 end 276 end
184 277
185 def test_index_one_day 278 def test_index_one_day
186 get :index, :project_id => 1, :from => "2007-03-23", :to => "2007-03-23" 279 get :index, :project_id => 'ecookbook', :from => "2007-03-23", :to => "2007-03-23"
187 assert_response :success 280 assert_response :success
188 assert_template 'index' 281 assert_template 'index'
189 assert_not_nil assigns(:total_hours) 282 assert_not_nil assigns(:total_hours)
190 assert_equal "4.25", "%.2f" % assigns(:total_hours) 283 assert_equal "4.25", "%.2f" % assigns(:total_hours)
284 assert_tag :form,
285 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
191 end 286 end
192 287
193 def test_index_at_issue_level 288 def test_index_at_issue_level
194 get :index, :issue_id => 1 289 get :index, :issue_id => 1
195 assert_response :success 290 assert_response :success
199 assert_not_nil assigns(:total_hours) 294 assert_not_nil assigns(:total_hours)
200 assert_equal 154.25, assigns(:total_hours) 295 assert_equal 154.25, assigns(:total_hours)
201 # display all time based on what's been logged 296 # display all time based on what's been logged
202 assert_equal '2007-03-12'.to_date, assigns(:from) 297 assert_equal '2007-03-12'.to_date, assigns(:from)
203 assert_equal '2007-04-22'.to_date, assigns(:to) 298 assert_equal '2007-04-22'.to_date, assigns(:to)
299 # TODO: remove /projects/:project_id/issues/:issue_id/time_entries routes
300 # to use /issues/:issue_id/time_entries
301 assert_tag :form,
302 :attributes => {:action => "/projects/ecookbook/issues/1/time_entries", :id => 'query_form'}
204 end 303 end
205 304
206 def test_index_atom_feed 305 def test_index_atom_feed
207 get :index, :project_id => 1, :format => 'atom' 306 get :index, :project_id => 1, :format => 'atom'
208 assert_response :success 307 assert_response :success
214 def test_index_all_projects_csv_export 313 def test_index_all_projects_csv_export
215 Setting.date_format = '%m/%d/%Y' 314 Setting.date_format = '%m/%d/%Y'
216 get :index, :format => 'csv' 315 get :index, :format => 'csv'
217 assert_response :success 316 assert_response :success
218 assert_equal 'text/csv', @response.content_type 317 assert_equal 'text/csv', @response.content_type
219 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n") 318 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
220 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n") 319 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
221 end 320 end
222 321
223 def test_index_csv_export 322 def test_index_csv_export
224 Setting.date_format = '%m/%d/%Y' 323 Setting.date_format = '%m/%d/%Y'
225 get :index, :project_id => 1, :format => 'csv' 324 get :index, :project_id => 1, :format => 'csv'
226 assert_response :success 325 assert_response :success
227 assert_equal 'text/csv', @response.content_type 326 assert_equal 'text/csv', @response.content_type
228 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n") 327 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
229 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n") 328 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
230 end 329 end
231 end 330 end