annotate .svn/pristine/ca/ca2c1fbd679c047639155d4edf6d5f5cff2202c1.svn-base @ 1524:82fac3dcf466 redmine-2.5-integration

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