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