annotate test/functional/timelog_controller_test.rb @ 1519:afce8026aaeb redmine-2.4-integration

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