annotate .svn/pristine/b2/b206ba598e9ac15edc3848e43e3c17a1d4cc2ebb.svn-base @ 1464:261b3d9a4903 redmine-2.4

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