annotate .svn/pristine/62/62c1677139f8e602a41e89d444679c525b9002b9.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

Merge from redmine-2.3 branch to create new branch redmine-2.3-integration
author Chris Cannam
date Fri, 14 Jun 2013 09:28:30 +0100
parents 622f24f53b42
children
rev   line source
Chris@1295 1 # -*- coding: utf-8 -*-
Chris@1295 2 # Redmine - project management software
Chris@1295 3 # Copyright (C) 2006-2013 Jean-Philippe Lang
Chris@1295 4 #
Chris@1295 5 # This program is free software; you can redistribute it and/or
Chris@1295 6 # modify it under the terms of the GNU General Public License
Chris@1295 7 # as published by the Free Software Foundation; either version 2
Chris@1295 8 # of the License, or (at your option) any later version.
Chris@1295 9 #
Chris@1295 10 # This program is distributed in the hope that it will be useful,
Chris@1295 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@1295 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@1295 13 # GNU General Public License for more details.
Chris@1295 14 #
Chris@1295 15 # You should have received a copy of the GNU General Public License
Chris@1295 16 # along with this program; if not, write to the Free Software
Chris@1295 17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@1295 18
Chris@1295 19 require File.expand_path('../../test_helper', __FILE__)
Chris@1295 20
Chris@1295 21 class TimelogControllerTest < ActionController::TestCase
Chris@1295 22 fixtures :projects, :enabled_modules, :roles, :members,
Chris@1295 23 :member_roles, :issues, :time_entries, :users,
Chris@1295 24 :trackers, :enumerations, :issue_statuses,
Chris@1295 25 :custom_fields, :custom_values,
Chris@1295 26 :projects_trackers, :custom_fields_trackers,
Chris@1295 27 :custom_fields_projects
Chris@1295 28
Chris@1295 29 include Redmine::I18n
Chris@1295 30
Chris@1295 31 def test_new_with_project_id
Chris@1295 32 @request.session[:user_id] = 3
Chris@1295 33 get :new, :project_id => 1
Chris@1295 34 assert_response :success
Chris@1295 35 assert_template 'new'
Chris@1295 36 assert_select 'select[name=?]', 'time_entry[project_id]', 0
Chris@1295 37 assert_select 'input[name=?][value=1][type=hidden]', 'time_entry[project_id]'
Chris@1295 38 end
Chris@1295 39
Chris@1295 40 def test_new_with_issue_id
Chris@1295 41 @request.session[:user_id] = 3
Chris@1295 42 get :new, :issue_id => 2
Chris@1295 43 assert_response :success
Chris@1295 44 assert_template 'new'
Chris@1295 45 assert_select 'select[name=?]', 'time_entry[project_id]', 0
Chris@1295 46 assert_select 'input[name=?][value=1][type=hidden]', 'time_entry[project_id]'
Chris@1295 47 end
Chris@1295 48
Chris@1295 49 def test_new_without_project
Chris@1295 50 @request.session[:user_id] = 3
Chris@1295 51 get :new
Chris@1295 52 assert_response :success
Chris@1295 53 assert_template 'new'
Chris@1295 54 assert_select 'select[name=?]', 'time_entry[project_id]'
Chris@1295 55 assert_select 'input[name=?]', 'time_entry[project_id]', 0
Chris@1295 56 end
Chris@1295 57
Chris@1295 58 def test_new_without_project_should_prefill_the_form
Chris@1295 59 @request.session[:user_id] = 3
Chris@1295 60 get :new, :time_entry => {:project_id => '1'}
Chris@1295 61 assert_response :success
Chris@1295 62 assert_template 'new'
Chris@1295 63 assert_select 'select[name=?]', 'time_entry[project_id]' do
Chris@1295 64 assert_select 'option[value=1][selected=selected]'
Chris@1295 65 end
Chris@1295 66 assert_select 'input[name=?]', 'time_entry[project_id]', 0
Chris@1295 67 end
Chris@1295 68
Chris@1295 69 def test_new_without_project_should_deny_without_permission
Chris@1295 70 Role.all.each {|role| role.remove_permission! :log_time}
Chris@1295 71 @request.session[:user_id] = 3
Chris@1295 72
Chris@1295 73 get :new
Chris@1295 74 assert_response 403
Chris@1295 75 end
Chris@1295 76
Chris@1295 77 def test_new_should_select_default_activity
Chris@1295 78 @request.session[:user_id] = 3
Chris@1295 79 get :new, :project_id => 1
Chris@1295 80 assert_response :success
Chris@1295 81 assert_select 'select[name=?]', 'time_entry[activity_id]' do
Chris@1295 82 assert_select 'option[selected=selected]', :text => 'Development'
Chris@1295 83 end
Chris@1295 84 end
Chris@1295 85
Chris@1295 86 def test_new_should_only_show_active_time_entry_activities
Chris@1295 87 @request.session[:user_id] = 3
Chris@1295 88 get :new, :project_id => 1
Chris@1295 89 assert_response :success
Chris@1295 90 assert_no_tag 'option', :content => 'Inactive Activity'
Chris@1295 91 end
Chris@1295 92
Chris@1295 93 def test_get_edit_existing_time
Chris@1295 94 @request.session[:user_id] = 2
Chris@1295 95 get :edit, :id => 2, :project_id => nil
Chris@1295 96 assert_response :success
Chris@1295 97 assert_template 'edit'
Chris@1295 98 # Default activity selected
Chris@1295 99 assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/time_entries/2' }
Chris@1295 100 end
Chris@1295 101
Chris@1295 102 def test_get_edit_with_an_existing_time_entry_with_inactive_activity
Chris@1295 103 te = TimeEntry.find(1)
Chris@1295 104 te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
Chris@1295 105 te.save!
Chris@1295 106
Chris@1295 107 @request.session[:user_id] = 1
Chris@1295 108 get :edit, :project_id => 1, :id => 1
Chris@1295 109 assert_response :success
Chris@1295 110 assert_template 'edit'
Chris@1295 111 # Blank option since nothing is pre-selected
Chris@1295 112 assert_tag :tag => 'option', :content => '--- Please select ---'
Chris@1295 113 end
Chris@1295 114
Chris@1295 115 def test_post_create
Chris@1295 116 # TODO: should POST to issues’ time log instead of project. change form
Chris@1295 117 # and routing
Chris@1295 118 @request.session[:user_id] = 3
Chris@1295 119 post :create, :project_id => 1,
Chris@1295 120 :time_entry => {:comments => 'Some work on TimelogControllerTest',
Chris@1295 121 # Not the default activity
Chris@1295 122 :activity_id => '11',
Chris@1295 123 :spent_on => '2008-03-14',
Chris@1295 124 :issue_id => '1',
Chris@1295 125 :hours => '7.3'}
Chris@1295 126 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
Chris@1295 127
Chris@1295 128 i = Issue.find(1)
Chris@1295 129 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
Chris@1295 130 assert_not_nil t
Chris@1295 131 assert_equal 11, t.activity_id
Chris@1295 132 assert_equal 7.3, t.hours
Chris@1295 133 assert_equal 3, t.user_id
Chris@1295 134 assert_equal i, t.issue
Chris@1295 135 assert_equal i.project, t.project
Chris@1295 136 end
Chris@1295 137
Chris@1295 138 def test_post_create_with_blank_issue
Chris@1295 139 # TODO: should POST to issues’ time log instead of project. change form
Chris@1295 140 # and routing
Chris@1295 141 @request.session[:user_id] = 3
Chris@1295 142 post :create, :project_id => 1,
Chris@1295 143 :time_entry => {:comments => 'Some work on TimelogControllerTest',
Chris@1295 144 # Not the default activity
Chris@1295 145 :activity_id => '11',
Chris@1295 146 :issue_id => '',
Chris@1295 147 :spent_on => '2008-03-14',
Chris@1295 148 :hours => '7.3'}
Chris@1295 149 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
Chris@1295 150
Chris@1295 151 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
Chris@1295 152 assert_not_nil t
Chris@1295 153 assert_equal 11, t.activity_id
Chris@1295 154 assert_equal 7.3, t.hours
Chris@1295 155 assert_equal 3, t.user_id
Chris@1295 156 end
Chris@1295 157
Chris@1295 158 def test_create_and_continue
Chris@1295 159 @request.session[:user_id] = 2
Chris@1295 160 post :create, :project_id => 1,
Chris@1295 161 :time_entry => {:activity_id => '11',
Chris@1295 162 :issue_id => '',
Chris@1295 163 :spent_on => '2008-03-14',
Chris@1295 164 :hours => '7.3'},
Chris@1295 165 :continue => '1'
Chris@1295 166 assert_redirected_to '/projects/ecookbook/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D='
Chris@1295 167 end
Chris@1295 168
Chris@1295 169 def test_create_and_continue_with_issue_id
Chris@1295 170 @request.session[:user_id] = 2
Chris@1295 171 post :create, :project_id => 1,
Chris@1295 172 :time_entry => {:activity_id => '11',
Chris@1295 173 :issue_id => '1',
Chris@1295 174 :spent_on => '2008-03-14',
Chris@1295 175 :hours => '7.3'},
Chris@1295 176 :continue => '1'
Chris@1295 177 assert_redirected_to '/projects/ecookbook/issues/1/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=1'
Chris@1295 178 end
Chris@1295 179
Chris@1295 180 def test_create_and_continue_without_project
Chris@1295 181 @request.session[:user_id] = 2
Chris@1295 182 post :create, :time_entry => {:project_id => '1',
Chris@1295 183 :activity_id => '11',
Chris@1295 184 :issue_id => '',
Chris@1295 185 :spent_on => '2008-03-14',
Chris@1295 186 :hours => '7.3'},
Chris@1295 187 :continue => '1'
Chris@1295 188
Chris@1295 189 assert_redirected_to '/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=&time_entry%5Bproject_id%5D=1'
Chris@1295 190 end
Chris@1295 191
Chris@1295 192 def test_create_without_log_time_permission_should_be_denied
Chris@1295 193 @request.session[:user_id] = 2
Chris@1295 194 Role.find_by_name('Manager').remove_permission! :log_time
Chris@1295 195 post :create, :project_id => 1,
Chris@1295 196 :time_entry => {:activity_id => '11',
Chris@1295 197 :issue_id => '',
Chris@1295 198 :spent_on => '2008-03-14',
Chris@1295 199 :hours => '7.3'}
Chris@1295 200
Chris@1295 201 assert_response 403
Chris@1295 202 end
Chris@1295 203
Chris@1295 204 def test_create_with_failure
Chris@1295 205 @request.session[:user_id] = 2
Chris@1295 206 post :create, :project_id => 1,
Chris@1295 207 :time_entry => {:activity_id => '',
Chris@1295 208 :issue_id => '',
Chris@1295 209 :spent_on => '2008-03-14',
Chris@1295 210 :hours => '7.3'}
Chris@1295 211
Chris@1295 212 assert_response :success
Chris@1295 213 assert_template 'new'
Chris@1295 214 end
Chris@1295 215
Chris@1295 216 def test_create_without_project
Chris@1295 217 @request.session[:user_id] = 2
Chris@1295 218 assert_difference 'TimeEntry.count' do
Chris@1295 219 post :create, :time_entry => {:project_id => '1',
Chris@1295 220 :activity_id => '11',
Chris@1295 221 :issue_id => '',
Chris@1295 222 :spent_on => '2008-03-14',
Chris@1295 223 :hours => '7.3'}
Chris@1295 224 end
Chris@1295 225
Chris@1295 226 assert_redirected_to '/projects/ecookbook/time_entries'
Chris@1295 227 time_entry = TimeEntry.first(:order => 'id DESC')
Chris@1295 228 assert_equal 1, time_entry.project_id
Chris@1295 229 end
Chris@1295 230
Chris@1295 231 def test_create_without_project_should_fail_with_issue_not_inside_project
Chris@1295 232 @request.session[:user_id] = 2
Chris@1295 233 assert_no_difference 'TimeEntry.count' do
Chris@1295 234 post :create, :time_entry => {:project_id => '1',
Chris@1295 235 :activity_id => '11',
Chris@1295 236 :issue_id => '5',
Chris@1295 237 :spent_on => '2008-03-14',
Chris@1295 238 :hours => '7.3'}
Chris@1295 239 end
Chris@1295 240
Chris@1295 241 assert_response :success
Chris@1295 242 assert assigns(:time_entry).errors[:issue_id].present?
Chris@1295 243 end
Chris@1295 244
Chris@1295 245 def test_create_without_project_should_deny_without_permission
Chris@1295 246 @request.session[:user_id] = 2
Chris@1295 247 Project.find(3).disable_module!(:time_tracking)
Chris@1295 248
Chris@1295 249 assert_no_difference 'TimeEntry.count' do
Chris@1295 250 post :create, :time_entry => {:project_id => '3',
Chris@1295 251 :activity_id => '11',
Chris@1295 252 :issue_id => '',
Chris@1295 253 :spent_on => '2008-03-14',
Chris@1295 254 :hours => '7.3'}
Chris@1295 255 end
Chris@1295 256
Chris@1295 257 assert_response 403
Chris@1295 258 end
Chris@1295 259
Chris@1295 260 def test_create_without_project_with_failure
Chris@1295 261 @request.session[:user_id] = 2
Chris@1295 262 assert_no_difference 'TimeEntry.count' do
Chris@1295 263 post :create, :time_entry => {:project_id => '1',
Chris@1295 264 :activity_id => '11',
Chris@1295 265 :issue_id => '',
Chris@1295 266 :spent_on => '2008-03-14',
Chris@1295 267 :hours => ''}
Chris@1295 268 end
Chris@1295 269
Chris@1295 270 assert_response :success
Chris@1295 271 assert_tag 'select', :attributes => {:name => 'time_entry[project_id]'},
Chris@1295 272 :child => {:tag => 'option', :attributes => {:value => '1', :selected => 'selected'}}
Chris@1295 273 end
Chris@1295 274
Chris@1295 275 def test_update
Chris@1295 276 entry = TimeEntry.find(1)
Chris@1295 277 assert_equal 1, entry.issue_id
Chris@1295 278 assert_equal 2, entry.user_id
Chris@1295 279
Chris@1295 280 @request.session[:user_id] = 1
Chris@1295 281 put :update, :id => 1,
Chris@1295 282 :time_entry => {:issue_id => '2',
Chris@1295 283 :hours => '8'}
Chris@1295 284 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
Chris@1295 285 entry.reload
Chris@1295 286
Chris@1295 287 assert_equal 8, entry.hours
Chris@1295 288 assert_equal 2, entry.issue_id
Chris@1295 289 assert_equal 2, entry.user_id
Chris@1295 290 end
Chris@1295 291
Chris@1295 292 def test_get_bulk_edit
Chris@1295 293 @request.session[:user_id] = 2
Chris@1295 294 get :bulk_edit, :ids => [1, 2]
Chris@1295 295 assert_response :success
Chris@1295 296 assert_template 'bulk_edit'
Chris@1295 297
Chris@1295 298 assert_select 'ul#bulk-selection' do
Chris@1295 299 assert_select 'li', 2
Chris@1295 300 assert_select 'li a', :text => '03/23/2007 - eCookbook: 4.25 hours'
Chris@1295 301 end
Chris@1295 302
Chris@1295 303 assert_select 'form#bulk_edit_form[action=?]', '/time_entries/bulk_update' do
Chris@1295 304 # System wide custom field
Chris@1295 305 assert_select 'select[name=?]', 'time_entry[custom_field_values][10]'
Chris@1295 306
Chris@1295 307 # Activities
Chris@1295 308 assert_select 'select[name=?]', 'time_entry[activity_id]' do
Chris@1295 309 assert_select 'option[value=]', :text => '(No change)'
Chris@1295 310 assert_select 'option[value=9]', :text => 'Design'
Chris@1295 311 end
Chris@1295 312 end
Chris@1295 313 end
Chris@1295 314
Chris@1295 315 def test_get_bulk_edit_on_different_projects
Chris@1295 316 @request.session[:user_id] = 2
Chris@1295 317 get :bulk_edit, :ids => [1, 2, 6]
Chris@1295 318 assert_response :success
Chris@1295 319 assert_template 'bulk_edit'
Chris@1295 320 end
Chris@1295 321
Chris@1295 322 def test_bulk_update
Chris@1295 323 @request.session[:user_id] = 2
Chris@1295 324 # update time entry activity
Chris@1295 325 post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9}
Chris@1295 326
Chris@1295 327 assert_response 302
Chris@1295 328 # check that the issues were updated
Chris@1295 329 assert_equal [9, 9], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.activity_id}
Chris@1295 330 end
Chris@1295 331
Chris@1295 332 def test_bulk_update_with_failure
Chris@1295 333 @request.session[:user_id] = 2
Chris@1295 334 post :bulk_update, :ids => [1, 2], :time_entry => { :hours => 'A'}
Chris@1295 335
Chris@1295 336 assert_response 302
Chris@1295 337 assert_match /Failed to save 2 time entrie/, flash[:error]
Chris@1295 338 end
Chris@1295 339
Chris@1295 340 def test_bulk_update_on_different_projects
Chris@1295 341 @request.session[:user_id] = 2
Chris@1295 342 # makes user a manager on the other project
Chris@1295 343 Member.create!(:user_id => 2, :project_id => 3, :role_ids => [1])
Chris@1295 344
Chris@1295 345 # update time entry activity
Chris@1295 346 post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 }
Chris@1295 347
Chris@1295 348 assert_response 302
Chris@1295 349 # check that the issues were updated
Chris@1295 350 assert_equal [9, 9, 9], TimeEntry.find_all_by_id([1, 2, 4]).collect {|i| i.activity_id}
Chris@1295 351 end
Chris@1295 352
Chris@1295 353 def test_bulk_update_on_different_projects_without_rights
Chris@1295 354 @request.session[:user_id] = 3
Chris@1295 355 user = User.find(3)
Chris@1295 356 action = { :controller => "timelog", :action => "bulk_update" }
Chris@1295 357 assert user.allowed_to?(action, TimeEntry.find(1).project)
Chris@1295 358 assert ! user.allowed_to?(action, TimeEntry.find(5).project)
Chris@1295 359 post :bulk_update, :ids => [1, 5], :time_entry => { :activity_id => 9 }
Chris@1295 360 assert_response 403
Chris@1295 361 end
Chris@1295 362
Chris@1295 363 def test_bulk_update_custom_field
Chris@1295 364 @request.session[:user_id] = 2
Chris@1295 365 post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} }
Chris@1295 366
Chris@1295 367 assert_response 302
Chris@1295 368 assert_equal ["0", "0"], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.custom_value_for(10).value}
Chris@1295 369 end
Chris@1295 370
Chris@1295 371 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
Chris@1295 372 @request.session[:user_id] = 2
Chris@1295 373 post :bulk_update, :ids => [1,2], :back_url => '/time_entries'
Chris@1295 374
Chris@1295 375 assert_response :redirect
Chris@1295 376 assert_redirected_to '/time_entries'
Chris@1295 377 end
Chris@1295 378
Chris@1295 379 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
Chris@1295 380 @request.session[:user_id] = 2
Chris@1295 381 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
Chris@1295 382
Chris@1295 383 assert_response :redirect
Chris@1295 384 assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier
Chris@1295 385 end
Chris@1295 386
Chris@1295 387 def test_post_bulk_update_without_edit_permission_should_be_denied
Chris@1295 388 @request.session[:user_id] = 2
Chris@1295 389 Role.find_by_name('Manager').remove_permission! :edit_time_entries
Chris@1295 390 post :bulk_update, :ids => [1,2]
Chris@1295 391
Chris@1295 392 assert_response 403
Chris@1295 393 end
Chris@1295 394
Chris@1295 395 def test_destroy
Chris@1295 396 @request.session[:user_id] = 2
Chris@1295 397 delete :destroy, :id => 1
Chris@1295 398 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
Chris@1295 399 assert_equal I18n.t(:notice_successful_delete), flash[:notice]
Chris@1295 400 assert_nil TimeEntry.find_by_id(1)
Chris@1295 401 end
Chris@1295 402
Chris@1295 403 def test_destroy_should_fail
Chris@1295 404 # simulate that this fails (e.g. due to a plugin), see #5700
Chris@1295 405 TimeEntry.any_instance.expects(:destroy).returns(false)
Chris@1295 406
Chris@1295 407 @request.session[:user_id] = 2
Chris@1295 408 delete :destroy, :id => 1
Chris@1295 409 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
Chris@1295 410 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
Chris@1295 411 assert_not_nil TimeEntry.find_by_id(1)
Chris@1295 412 end
Chris@1295 413
Chris@1295 414 def test_index_all_projects
Chris@1295 415 get :index
Chris@1295 416 assert_response :success
Chris@1295 417 assert_template 'index'
Chris@1295 418 assert_not_nil assigns(:total_hours)
Chris@1295 419 assert_equal "162.90", "%.2f" % assigns(:total_hours)
Chris@1295 420 assert_tag :form,
Chris@1295 421 :attributes => {:action => "/time_entries", :id => 'query_form'}
Chris@1295 422 end
Chris@1295 423
Chris@1295 424 def test_index_all_projects_should_show_log_time_link
Chris@1295 425 @request.session[:user_id] = 2
Chris@1295 426 get :index
Chris@1295 427 assert_response :success
Chris@1295 428 assert_template 'index'
Chris@1295 429 assert_tag 'a', :attributes => {:href => '/time_entries/new'}, :content => /Log time/
Chris@1295 430 end
Chris@1295 431
Chris@1295 432 def test_index_at_project_level
Chris@1295 433 get :index, :project_id => 'ecookbook'
Chris@1295 434 assert_response :success
Chris@1295 435 assert_template 'index'
Chris@1295 436 assert_not_nil assigns(:entries)
Chris@1295 437 assert_equal 4, assigns(:entries).size
Chris@1295 438 # project and subproject
Chris@1295 439 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
Chris@1295 440 assert_not_nil assigns(:total_hours)
Chris@1295 441 assert_equal "162.90", "%.2f" % assigns(:total_hours)
Chris@1295 442 assert_tag :form,
Chris@1295 443 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
Chris@1295 444 end
Chris@1295 445
Chris@1295 446 def test_index_at_project_level_with_date_range
Chris@1295 447 get :index, :project_id => 'ecookbook',
Chris@1295 448 :f => ['spent_on'],
Chris@1295 449 :op => {'spent_on' => '><'},
Chris@1295 450 :v => {'spent_on' => ['2007-03-20', '2007-04-30']}
Chris@1295 451 assert_response :success
Chris@1295 452 assert_template 'index'
Chris@1295 453 assert_not_nil assigns(:entries)
Chris@1295 454 assert_equal 3, assigns(:entries).size
Chris@1295 455 assert_not_nil assigns(:total_hours)
Chris@1295 456 assert_equal "12.90", "%.2f" % assigns(:total_hours)
Chris@1295 457 assert_tag :form,
Chris@1295 458 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
Chris@1295 459 end
Chris@1295 460
Chris@1295 461 def test_index_at_project_level_with_date_range_using_from_and_to_params
Chris@1295 462 get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30'
Chris@1295 463 assert_response :success
Chris@1295 464 assert_template 'index'
Chris@1295 465 assert_not_nil assigns(:entries)
Chris@1295 466 assert_equal 3, assigns(:entries).size
Chris@1295 467 assert_not_nil assigns(:total_hours)
Chris@1295 468 assert_equal "12.90", "%.2f" % assigns(:total_hours)
Chris@1295 469 assert_tag :form,
Chris@1295 470 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
Chris@1295 471 end
Chris@1295 472
Chris@1295 473 def test_index_at_project_level_with_period
Chris@1295 474 get :index, :project_id => 'ecookbook',
Chris@1295 475 :f => ['spent_on'],
Chris@1295 476 :op => {'spent_on' => '>t-'},
Chris@1295 477 :v => {'spent_on' => ['7']}
Chris@1295 478 assert_response :success
Chris@1295 479 assert_template 'index'
Chris@1295 480 assert_not_nil assigns(:entries)
Chris@1295 481 assert_not_nil assigns(:total_hours)
Chris@1295 482 assert_tag :form,
Chris@1295 483 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
Chris@1295 484 end
Chris@1295 485
Chris@1295 486 def test_index_at_issue_level
Chris@1295 487 get :index, :issue_id => 1
Chris@1295 488 assert_response :success
Chris@1295 489 assert_template 'index'
Chris@1295 490 assert_not_nil assigns(:entries)
Chris@1295 491 assert_equal 2, assigns(:entries).size
Chris@1295 492 assert_not_nil assigns(:total_hours)
Chris@1295 493 assert_equal 154.25, assigns(:total_hours)
Chris@1295 494 # display all time
Chris@1295 495 assert_nil assigns(:from)
Chris@1295 496 assert_nil assigns(:to)
Chris@1295 497 # TODO: remove /projects/:project_id/issues/:issue_id/time_entries routes
Chris@1295 498 # to use /issues/:issue_id/time_entries
Chris@1295 499 assert_tag :form,
Chris@1295 500 :attributes => {:action => "/projects/ecookbook/issues/1/time_entries", :id => 'query_form'}
Chris@1295 501 end
Chris@1295 502
Chris@1295 503 def test_index_should_sort_by_spent_on_and_created_on
Chris@1295 504 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)
Chris@1295 505 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)
Chris@1295 506 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)
Chris@1295 507
Chris@1295 508 get :index, :project_id => 1,
Chris@1295 509 :f => ['spent_on'],
Chris@1295 510 :op => {'spent_on' => '><'},
Chris@1295 511 :v => {'spent_on' => ['2012-06-15', '2012-06-16']}
Chris@1295 512 assert_response :success
Chris@1295 513 assert_equal [t2, t1, t3], assigns(:entries)
Chris@1295 514
Chris@1295 515 get :index, :project_id => 1,
Chris@1295 516 :f => ['spent_on'],
Chris@1295 517 :op => {'spent_on' => '><'},
Chris@1295 518 :v => {'spent_on' => ['2012-06-15', '2012-06-16']},
Chris@1295 519 :sort => 'spent_on'
Chris@1295 520 assert_response :success
Chris@1295 521 assert_equal [t3, t1, t2], assigns(:entries)
Chris@1295 522 end
Chris@1295 523
Chris@1295 524 def test_index_with_filter_on_issue_custom_field
Chris@1295 525 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'})
Chris@1295 526 entry = TimeEntry.generate!(:issue => issue, :hours => 2.5)
Chris@1295 527
Chris@1295 528 get :index, :f => ['issue.cf_2'], :op => {'issue.cf_2' => '='}, :v => {'issue.cf_2' => ['filter_on_issue_custom_field']}
Chris@1295 529 assert_response :success
Chris@1295 530 assert_equal [entry], assigns(:entries)
Chris@1295 531 end
Chris@1295 532
Chris@1295 533 def test_index_with_issue_custom_field_column
Chris@1295 534 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'})
Chris@1295 535 entry = TimeEntry.generate!(:issue => issue, :hours => 2.5)
Chris@1295 536
Chris@1295 537 get :index, :c => %w(project spent_on issue comments hours issue.cf_2)
Chris@1295 538 assert_response :success
Chris@1295 539 assert_include :'issue.cf_2', assigns(:query).column_names
Chris@1295 540 assert_select 'td.issue_cf_2', :text => 'filter_on_issue_custom_field'
Chris@1295 541 end
Chris@1295 542
Chris@1295 543 def test_index_atom_feed
Chris@1295 544 get :index, :project_id => 1, :format => 'atom'
Chris@1295 545 assert_response :success
Chris@1295 546 assert_equal 'application/atom+xml', @response.content_type
Chris@1295 547 assert_not_nil assigns(:items)
Chris@1295 548 assert assigns(:items).first.is_a?(TimeEntry)
Chris@1295 549 end
Chris@1295 550
Chris@1295 551 def test_index_at_project_level_should_include_csv_export_dialog
Chris@1295 552 get :index, :project_id => 'ecookbook',
Chris@1295 553 :f => ['spent_on'],
Chris@1295 554 :op => {'spent_on' => '>='},
Chris@1295 555 :v => {'spent_on' => ['2007-04-01']},
Chris@1295 556 :c => ['spent_on', 'user']
Chris@1295 557 assert_response :success
Chris@1295 558
Chris@1295 559 assert_select '#csv-export-options' do
Chris@1295 560 assert_select 'form[action=?][method=get]', '/projects/ecookbook/time_entries.csv' do
Chris@1295 561 # filter
Chris@1295 562 assert_select 'input[name=?][value=?]', 'f[]', 'spent_on'
Chris@1295 563 assert_select 'input[name=?][value=?]', 'op[spent_on]', '&gt;='
Chris@1295 564 assert_select 'input[name=?][value=?]', 'v[spent_on][]', '2007-04-01'
Chris@1295 565 # columns
Chris@1295 566 assert_select 'input[name=?][value=?]', 'c[]', 'spent_on'
Chris@1295 567 assert_select 'input[name=?][value=?]', 'c[]', 'user'
Chris@1295 568 assert_select 'input[name=?]', 'c[]', 2
Chris@1295 569 end
Chris@1295 570 end
Chris@1295 571 end
Chris@1295 572
Chris@1295 573 def test_index_cross_project_should_include_csv_export_dialog
Chris@1295 574 get :index
Chris@1295 575 assert_response :success
Chris@1295 576
Chris@1295 577 assert_select '#csv-export-options' do
Chris@1295 578 assert_select 'form[action=?][method=get]', '/time_entries.csv'
Chris@1295 579 end
Chris@1295 580 end
Chris@1295 581
Chris@1295 582 def test_index_at_issue_level_should_include_csv_export_dialog
Chris@1295 583 get :index, :project_id => 'ecookbook', :issue_id => 3
Chris@1295 584 assert_response :success
Chris@1295 585
Chris@1295 586 assert_select '#csv-export-options' do
Chris@1295 587 assert_select 'form[action=?][method=get]', '/projects/ecookbook/issues/3/time_entries.csv'
Chris@1295 588 end
Chris@1295 589 end
Chris@1295 590
Chris@1295 591 def test_index_csv_all_projects
Chris@1295 592 Setting.date_format = '%m/%d/%Y'
Chris@1295 593 get :index, :format => 'csv'
Chris@1295 594 assert_response :success
Chris@1295 595 assert_equal 'text/csv; header=present', response.content_type
Chris@1295 596 end
Chris@1295 597
Chris@1295 598 def test_index_csv
Chris@1295 599 Setting.date_format = '%m/%d/%Y'
Chris@1295 600 get :index, :project_id => 1, :format => 'csv'
Chris@1295 601 assert_response :success
Chris@1295 602 assert_equal 'text/csv; header=present', response.content_type
Chris@1295 603 end
Chris@1295 604 end