annotate test/functional/timelog_controller_test.rb @ 1466:834828a14f2b bug_598

Close obsolete branch bug_598
author Chris Cannam
date Fri, 21 Jun 2013 14:51:55 +0100
parents 433d4f72a19b
children 622f24f53b42 261b3d9a4903
rev   line source
Chris@0 1 # -*- coding: utf-8 -*-
Chris@909 2 # Redmine - project management software
Chris@1115 3 # Copyright (C) 2006-2012 Jean-Philippe Lang
Chris@0 4 #
Chris@0 5 # This program is free software; you can redistribute it and/or
Chris@0 6 # modify it under the terms of the GNU General Public License
Chris@0 7 # as published by the Free Software Foundation; either version 2
Chris@0 8 # of the License, or (at your option) any later version.
Chris@909 9 #
Chris@0 10 # This program is distributed in the hope that it will be useful,
Chris@0 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Chris@0 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Chris@0 13 # GNU General Public License for more details.
Chris@909 14 #
Chris@0 15 # You should have received a copy of the GNU General Public License
Chris@0 16 # along with this program; if not, write to the Free Software
Chris@0 17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Chris@0 18
Chris@119 19 require File.expand_path('../../test_helper', __FILE__)
Chris@0 20 require 'timelog_controller'
Chris@0 21
Chris@0 22 # Re-raise errors caught by the controller.
Chris@0 23 class TimelogController; def rescue_action(e) raise e end; end
Chris@0 24
Chris@0 25 class TimelogControllerTest < ActionController::TestCase
Chris@909 26 fixtures :projects, :enabled_modules, :roles, :members,
Chris@909 27 :member_roles, :issues, :time_entries, :users,
Chris@909 28 :trackers, :enumerations, :issue_statuses,
Chris@909 29 :custom_fields, :custom_values
Chris@909 30
Chris@909 31 include Redmine::I18n
Chris@0 32
Chris@0 33 def setup
Chris@0 34 @controller = TimelogController.new
Chris@0 35 @request = ActionController::TestRequest.new
Chris@0 36 @response = ActionController::TestResponse.new
Chris@0 37 end
Chris@909 38
Chris@1115 39 def test_new_with_project_id
Chris@0 40 @request.session[:user_id] = 3
chris@37 41 get :new, :project_id => 1
Chris@0 42 assert_response :success
Chris@1115 43 assert_template 'new'
Chris@1115 44 assert_select 'select[name=?]', 'time_entry[project_id]', 0
Chris@1115 45 assert_select 'input[name=?][value=1][type=hidden]', 'time_entry[project_id]'
Chris@0 46 end
Chris@909 47
Chris@1115 48 def test_new_with_issue_id
Chris@1115 49 @request.session[:user_id] = 3
Chris@1115 50 get :new, :issue_id => 2
Chris@1115 51 assert_response :success
Chris@1115 52 assert_template 'new'
Chris@1115 53 assert_select 'select[name=?]', 'time_entry[project_id]', 0
Chris@1115 54 assert_select 'input[name=?][value=1][type=hidden]', 'time_entry[project_id]'
Chris@1115 55 end
Chris@1115 56
Chris@1115 57 def test_new_without_project
Chris@1115 58 @request.session[:user_id] = 3
Chris@1115 59 get :new
Chris@1115 60 assert_response :success
Chris@1115 61 assert_template 'new'
Chris@1115 62 assert_select 'select[name=?]', 'time_entry[project_id]'
Chris@1115 63 assert_select 'input[name=?]', 'time_entry[project_id]', 0
Chris@1115 64 end
Chris@1115 65
Chris@1115 66 def test_new_without_project_should_prefill_the_form
Chris@1115 67 @request.session[:user_id] = 3
Chris@1115 68 get :new, :time_entry => {:project_id => '1'}
Chris@1115 69 assert_response :success
Chris@1115 70 assert_template 'new'
Chris@1115 71 assert_select 'select[name=?]', 'time_entry[project_id]' do
Chris@1115 72 assert_select 'option[value=1][selected=selected]'
Chris@1115 73 end
Chris@1115 74 assert_select 'input[name=?]', 'time_entry[project_id]', 0
Chris@1115 75 end
Chris@1115 76
Chris@1115 77 def test_new_without_project_should_deny_without_permission
Chris@1115 78 Role.all.each {|role| role.remove_permission! :log_time}
Chris@1115 79 @request.session[:user_id] = 3
Chris@1115 80
Chris@1115 81 get :new
Chris@1115 82 assert_response 403
Chris@1115 83 end
Chris@1115 84
Chris@1115 85 def test_new_should_select_default_activity
chris@37 86 @request.session[:user_id] = 3
chris@37 87 get :new, :project_id => 1
chris@37 88 assert_response :success
Chris@1115 89 assert_select 'select[name=?]', 'time_entry[activity_id]' do
Chris@1115 90 assert_select 'option[selected=selected]', :text => 'Development'
Chris@1115 91 end
Chris@1115 92 end
Chris@1115 93
Chris@1115 94 def test_new_should_only_show_active_time_entry_activities
Chris@1115 95 @request.session[:user_id] = 3
Chris@1115 96 get :new, :project_id => 1
Chris@1115 97 assert_response :success
Chris@1115 98 assert_no_tag 'option', :content => 'Inactive Activity'
chris@37 99 end
chris@37 100
Chris@0 101 def test_get_edit_existing_time
Chris@0 102 @request.session[:user_id] = 2
Chris@0 103 get :edit, :id => 2, :project_id => nil
Chris@0 104 assert_response :success
Chris@0 105 assert_template 'edit'
Chris@0 106 # Default activity selected
chris@37 107 assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/time_entries/2' }
Chris@0 108 end
Chris@909 109
Chris@0 110 def test_get_edit_with_an_existing_time_entry_with_inactive_activity
Chris@0 111 te = TimeEntry.find(1)
Chris@0 112 te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
Chris@0 113 te.save!
Chris@0 114
Chris@0 115 @request.session[:user_id] = 1
Chris@0 116 get :edit, :project_id => 1, :id => 1
Chris@0 117 assert_response :success
Chris@0 118 assert_template 'edit'
Chris@0 119 # Blank option since nothing is pre-selected
Chris@0 120 assert_tag :tag => 'option', :content => '--- Please select ---'
Chris@0 121 end
Chris@909 122
chris@37 123 def test_post_create
Chris@0 124 # TODO: should POST to issues’ time log instead of project. change form
Chris@0 125 # and routing
Chris@0 126 @request.session[:user_id] = 3
chris@37 127 post :create, :project_id => 1,
Chris@0 128 :time_entry => {:comments => 'Some work on TimelogControllerTest',
Chris@0 129 # Not the default activity
Chris@0 130 :activity_id => '11',
Chris@0 131 :spent_on => '2008-03-14',
Chris@0 132 :issue_id => '1',
Chris@0 133 :hours => '7.3'}
chris@37 134 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
Chris@909 135
Chris@0 136 i = Issue.find(1)
Chris@0 137 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
Chris@0 138 assert_not_nil t
Chris@0 139 assert_equal 11, t.activity_id
Chris@0 140 assert_equal 7.3, t.hours
Chris@0 141 assert_equal 3, t.user_id
Chris@0 142 assert_equal i, t.issue
Chris@0 143 assert_equal i.project, t.project
Chris@0 144 end
Chris@119 145
Chris@119 146 def test_post_create_with_blank_issue
Chris@119 147 # TODO: should POST to issues’ time log instead of project. change form
Chris@119 148 # and routing
Chris@119 149 @request.session[:user_id] = 3
Chris@119 150 post :create, :project_id => 1,
Chris@119 151 :time_entry => {:comments => 'Some work on TimelogControllerTest',
Chris@119 152 # Not the default activity
Chris@119 153 :activity_id => '11',
Chris@119 154 :issue_id => '',
Chris@119 155 :spent_on => '2008-03-14',
Chris@119 156 :hours => '7.3'}
Chris@119 157 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
Chris@909 158
Chris@119 159 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
Chris@119 160 assert_not_nil t
Chris@119 161 assert_equal 11, t.activity_id
Chris@119 162 assert_equal 7.3, t.hours
Chris@119 163 assert_equal 3, t.user_id
Chris@119 164 end
Chris@909 165
Chris@1115 166 def test_create_and_continue
Chris@1115 167 @request.session[:user_id] = 2
Chris@1115 168 post :create, :project_id => 1,
Chris@1115 169 :time_entry => {:activity_id => '11',
Chris@1115 170 :issue_id => '',
Chris@1115 171 :spent_on => '2008-03-14',
Chris@1115 172 :hours => '7.3'},
Chris@1115 173 :continue => '1'
Chris@1115 174 assert_redirected_to '/projects/ecookbook/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D='
Chris@1115 175 end
Chris@1115 176
Chris@1115 177 def test_create_and_continue_with_issue_id
Chris@1115 178 @request.session[:user_id] = 2
Chris@1115 179 post :create, :project_id => 1,
Chris@1115 180 :time_entry => {:activity_id => '11',
Chris@1115 181 :issue_id => '1',
Chris@1115 182 :spent_on => '2008-03-14',
Chris@1115 183 :hours => '7.3'},
Chris@1115 184 :continue => '1'
Chris@1115 185 assert_redirected_to '/projects/ecookbook/issues/1/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=1'
Chris@1115 186 end
Chris@1115 187
Chris@1115 188 def test_create_and_continue_without_project
Chris@1115 189 @request.session[:user_id] = 2
Chris@1115 190 post :create, :time_entry => {:project_id => '1',
Chris@1115 191 :activity_id => '11',
Chris@1115 192 :issue_id => '',
Chris@1115 193 :spent_on => '2008-03-14',
Chris@1115 194 :hours => '7.3'},
Chris@1115 195 :continue => '1'
Chris@1115 196
Chris@1115 197 assert_redirected_to '/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=&time_entry%5Bproject_id%5D=1'
Chris@1115 198 end
Chris@1115 199
Chris@909 200 def test_create_without_log_time_permission_should_be_denied
Chris@909 201 @request.session[:user_id] = 2
Chris@909 202 Role.find_by_name('Manager').remove_permission! :log_time
Chris@909 203 post :create, :project_id => 1,
Chris@909 204 :time_entry => {:activity_id => '11',
Chris@909 205 :issue_id => '',
Chris@909 206 :spent_on => '2008-03-14',
Chris@909 207 :hours => '7.3'}
Chris@909 208
Chris@909 209 assert_response 403
Chris@909 210 end
Chris@909 211
Chris@1115 212 def test_create_with_failure
Chris@1115 213 @request.session[:user_id] = 2
Chris@1115 214 post :create, :project_id => 1,
Chris@1115 215 :time_entry => {:activity_id => '',
Chris@1115 216 :issue_id => '',
Chris@1115 217 :spent_on => '2008-03-14',
Chris@1115 218 :hours => '7.3'}
Chris@1115 219
Chris@1115 220 assert_response :success
Chris@1115 221 assert_template 'new'
Chris@1115 222 end
Chris@1115 223
Chris@1115 224 def test_create_without_project
Chris@1115 225 @request.session[:user_id] = 2
Chris@1115 226 assert_difference 'TimeEntry.count' do
Chris@1115 227 post :create, :time_entry => {:project_id => '1',
Chris@1115 228 :activity_id => '11',
Chris@1115 229 :issue_id => '',
Chris@1115 230 :spent_on => '2008-03-14',
Chris@1115 231 :hours => '7.3'}
Chris@1115 232 end
Chris@1115 233
Chris@1115 234 assert_redirected_to '/projects/ecookbook/time_entries'
Chris@1115 235 time_entry = TimeEntry.first(:order => 'id DESC')
Chris@1115 236 assert_equal 1, time_entry.project_id
Chris@1115 237 end
Chris@1115 238
Chris@1115 239 def test_create_without_project_should_fail_with_issue_not_inside_project
Chris@1115 240 @request.session[:user_id] = 2
Chris@1115 241 assert_no_difference 'TimeEntry.count' do
Chris@1115 242 post :create, :time_entry => {:project_id => '1',
Chris@1115 243 :activity_id => '11',
Chris@1115 244 :issue_id => '5',
Chris@1115 245 :spent_on => '2008-03-14',
Chris@1115 246 :hours => '7.3'}
Chris@1115 247 end
Chris@1115 248
Chris@1115 249 assert_response :success
Chris@1115 250 assert assigns(:time_entry).errors[:issue_id].present?
Chris@1115 251 end
Chris@1115 252
Chris@1115 253 def test_create_without_project_should_deny_without_permission
Chris@1115 254 @request.session[:user_id] = 2
Chris@1115 255 Project.find(3).disable_module!(:time_tracking)
Chris@1115 256
Chris@1115 257 assert_no_difference 'TimeEntry.count' do
Chris@1115 258 post :create, :time_entry => {:project_id => '3',
Chris@1115 259 :activity_id => '11',
Chris@1115 260 :issue_id => '',
Chris@1115 261 :spent_on => '2008-03-14',
Chris@1115 262 :hours => '7.3'}
Chris@1115 263 end
Chris@1115 264
Chris@1115 265 assert_response 403
Chris@1115 266 end
Chris@1115 267
Chris@1115 268 def test_create_without_project_with_failure
Chris@1115 269 @request.session[:user_id] = 2
Chris@1115 270 assert_no_difference 'TimeEntry.count' do
Chris@1115 271 post :create, :time_entry => {:project_id => '1',
Chris@1115 272 :activity_id => '11',
Chris@1115 273 :issue_id => '',
Chris@1115 274 :spent_on => '2008-03-14',
Chris@1115 275 :hours => ''}
Chris@1115 276 end
Chris@1115 277
Chris@1115 278 assert_response :success
Chris@1115 279 assert_tag 'select', :attributes => {:name => 'time_entry[project_id]'},
Chris@1115 280 :child => {:tag => 'option', :attributes => {:value => '1', :selected => 'selected'}}
Chris@1115 281 end
Chris@1115 282
Chris@0 283 def test_update
Chris@0 284 entry = TimeEntry.find(1)
Chris@0 285 assert_equal 1, entry.issue_id
Chris@0 286 assert_equal 2, entry.user_id
Chris@909 287
Chris@0 288 @request.session[:user_id] = 1
chris@37 289 put :update, :id => 1,
Chris@0 290 :time_entry => {:issue_id => '2',
Chris@0 291 :hours => '8'}
chris@37 292 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
Chris@0 293 entry.reload
Chris@909 294
Chris@0 295 assert_equal 8, entry.hours
Chris@0 296 assert_equal 2, entry.issue_id
Chris@0 297 assert_equal 2, entry.user_id
Chris@0 298 end
Chris@441 299
Chris@441 300 def test_get_bulk_edit
Chris@441 301 @request.session[:user_id] = 2
Chris@441 302 get :bulk_edit, :ids => [1, 2]
Chris@441 303 assert_response :success
Chris@441 304 assert_template 'bulk_edit'
Chris@909 305
Chris@441 306 # System wide custom field
Chris@441 307 assert_tag :select, :attributes => {:name => 'time_entry[custom_field_values][10]'}
Chris@1115 308
Chris@1115 309 # Activities
Chris@1115 310 assert_select 'select[name=?]', 'time_entry[activity_id]' do
Chris@1115 311 assert_select 'option[value=]', :text => '(No change)'
Chris@1115 312 assert_select 'option[value=9]', :text => 'Design'
Chris@1115 313 end
Chris@441 314 end
Chris@441 315
Chris@441 316 def test_get_bulk_edit_on_different_projects
Chris@441 317 @request.session[:user_id] = 2
Chris@441 318 get :bulk_edit, :ids => [1, 2, 6]
Chris@441 319 assert_response :success
Chris@441 320 assert_template 'bulk_edit'
Chris@441 321 end
Chris@441 322
Chris@441 323 def test_bulk_update
Chris@441 324 @request.session[:user_id] = 2
Chris@441 325 # update time entry activity
Chris@441 326 post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9}
Chris@909 327
Chris@441 328 assert_response 302
Chris@441 329 # check that the issues were updated
Chris@441 330 assert_equal [9, 9], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.activity_id}
Chris@441 331 end
Chris@441 332
Chris@1115 333 def test_bulk_update_with_failure
Chris@1115 334 @request.session[:user_id] = 2
Chris@1115 335 post :bulk_update, :ids => [1, 2], :time_entry => { :hours => 'A'}
Chris@1115 336
Chris@1115 337 assert_response 302
Chris@1115 338 assert_match /Failed to save 2 time entrie/, flash[:error]
Chris@1115 339 end
Chris@1115 340
Chris@441 341 def test_bulk_update_on_different_projects
Chris@441 342 @request.session[:user_id] = 2
Chris@909 343 # makes user a manager on the other project
Chris@909 344 Member.create!(:user_id => 2, :project_id => 3, :role_ids => [1])
Chris@909 345
Chris@441 346 # update time entry activity
Chris@441 347 post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 }
Chris@909 348
Chris@441 349 assert_response 302
Chris@441 350 # check that the issues were updated
Chris@441 351 assert_equal [9, 9, 9], TimeEntry.find_all_by_id([1, 2, 4]).collect {|i| i.activity_id}
Chris@441 352 end
Chris@441 353
Chris@441 354 def test_bulk_update_on_different_projects_without_rights
Chris@441 355 @request.session[:user_id] = 3
Chris@441 356 user = User.find(3)
Chris@441 357 action = { :controller => "timelog", :action => "bulk_update" }
Chris@441 358 assert user.allowed_to?(action, TimeEntry.find(1).project)
Chris@441 359 assert ! user.allowed_to?(action, TimeEntry.find(5).project)
Chris@441 360 post :bulk_update, :ids => [1, 5], :time_entry => { :activity_id => 9 }
Chris@441 361 assert_response 403
Chris@441 362 end
Chris@441 363
Chris@441 364 def test_bulk_update_custom_field
Chris@441 365 @request.session[:user_id] = 2
Chris@441 366 post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} }
Chris@909 367
Chris@441 368 assert_response 302
Chris@441 369 assert_equal ["0", "0"], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.custom_value_for(10).value}
Chris@441 370 end
Chris@441 371
Chris@441 372 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
Chris@441 373 @request.session[:user_id] = 2
Chris@441 374 post :bulk_update, :ids => [1,2], :back_url => '/time_entries'
Chris@441 375
Chris@441 376 assert_response :redirect
Chris@441 377 assert_redirected_to '/time_entries'
Chris@441 378 end
Chris@441 379
Chris@441 380 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
Chris@441 381 @request.session[:user_id] = 2
Chris@441 382 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
Chris@441 383
Chris@441 384 assert_response :redirect
Chris@441 385 assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier
Chris@441 386 end
Chris@909 387
Chris@909 388 def test_post_bulk_update_without_edit_permission_should_be_denied
Chris@909 389 @request.session[:user_id] = 2
Chris@909 390 Role.find_by_name('Manager').remove_permission! :edit_time_entries
Chris@909 391 post :bulk_update, :ids => [1,2]
Chris@909 392
Chris@909 393 assert_response 403
Chris@909 394 end
Chris@909 395
Chris@0 396 def test_destroy
Chris@0 397 @request.session[:user_id] = 2
chris@37 398 delete :destroy, :id => 1
chris@37 399 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
Chris@0 400 assert_equal I18n.t(:notice_successful_delete), flash[:notice]
Chris@0 401 assert_nil TimeEntry.find_by_id(1)
Chris@0 402 end
Chris@909 403
Chris@0 404 def test_destroy_should_fail
Chris@0 405 # simulate that this fails (e.g. due to a plugin), see #5700
Chris@441 406 TimeEntry.any_instance.expects(:destroy).returns(false)
Chris@0 407
Chris@0 408 @request.session[:user_id] = 2
chris@37 409 delete :destroy, :id => 1
chris@37 410 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
Chris@0 411 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
Chris@0 412 assert_not_nil TimeEntry.find_by_id(1)
Chris@0 413 end
Chris@909 414
chris@37 415 def test_index_all_projects
chris@37 416 get :index
Chris@0 417 assert_response :success
chris@37 418 assert_template 'index'
Chris@0 419 assert_not_nil assigns(:total_hours)
Chris@0 420 assert_equal "162.90", "%.2f" % assigns(:total_hours)
Chris@441 421 assert_tag :form,
Chris@441 422 :attributes => {:action => "/time_entries", :id => 'query_form'}
Chris@0 423 end
Chris@909 424
Chris@1115 425 def test_index_all_projects_should_show_log_time_link
Chris@1115 426 @request.session[:user_id] = 2
Chris@1115 427 get :index
Chris@1115 428 assert_response :success
Chris@1115 429 assert_template 'index'
Chris@1115 430 assert_tag 'a', :attributes => {:href => '/time_entries/new'}, :content => /Log time/
Chris@1115 431 end
Chris@1115 432
chris@37 433 def test_index_at_project_level
Chris@441 434 get :index, :project_id => 'ecookbook'
Chris@0 435 assert_response :success
chris@37 436 assert_template 'index'
Chris@0 437 assert_not_nil assigns(:entries)
Chris@0 438 assert_equal 4, assigns(:entries).size
Chris@0 439 # project and subproject
Chris@0 440 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
Chris@0 441 assert_not_nil assigns(:total_hours)
Chris@0 442 assert_equal "162.90", "%.2f" % assigns(:total_hours)
Chris@0 443 # display all time by default
Chris@1115 444 assert_nil assigns(:from)
Chris@1115 445 assert_nil assigns(:to)
Chris@441 446 assert_tag :form,
Chris@441 447 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
Chris@0 448 end
Chris@909 449
chris@37 450 def test_index_at_project_level_with_date_range
Chris@441 451 get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30'
Chris@0 452 assert_response :success
chris@37 453 assert_template 'index'
Chris@0 454 assert_not_nil assigns(:entries)
Chris@0 455 assert_equal 3, assigns(:entries).size
Chris@0 456 assert_not_nil assigns(:total_hours)
Chris@0 457 assert_equal "12.90", "%.2f" % assigns(:total_hours)
Chris@0 458 assert_equal '2007-03-20'.to_date, assigns(:from)
Chris@0 459 assert_equal '2007-04-30'.to_date, assigns(:to)
Chris@441 460 assert_tag :form,
Chris@441 461 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
Chris@0 462 end
Chris@0 463
chris@37 464 def test_index_at_project_level_with_period
Chris@441 465 get :index, :project_id => 'ecookbook', :period => '7_days'
Chris@0 466 assert_response :success
chris@37 467 assert_template 'index'
Chris@0 468 assert_not_nil assigns(:entries)
Chris@0 469 assert_not_nil assigns(:total_hours)
Chris@0 470 assert_equal Date.today - 7, assigns(:from)
Chris@0 471 assert_equal Date.today, assigns(:to)
Chris@441 472 assert_tag :form,
Chris@441 473 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
Chris@0 474 end
Chris@0 475
chris@37 476 def test_index_one_day
Chris@441 477 get :index, :project_id => 'ecookbook', :from => "2007-03-23", :to => "2007-03-23"
Chris@0 478 assert_response :success
chris@37 479 assert_template 'index'
Chris@0 480 assert_not_nil assigns(:total_hours)
Chris@0 481 assert_equal "4.25", "%.2f" % assigns(:total_hours)
Chris@441 482 assert_tag :form,
Chris@441 483 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
Chris@0 484 end
Chris@909 485
Chris@1115 486 def test_index_from_a_date
Chris@1115 487 get :index, :project_id => 'ecookbook', :from => "2007-03-23", :to => ""
Chris@1115 488 assert_equal '2007-03-23'.to_date, assigns(:from)
Chris@1115 489 assert_nil assigns(:to)
Chris@1115 490 end
Chris@1115 491
Chris@1115 492 def test_index_to_a_date
Chris@1115 493 get :index, :project_id => 'ecookbook', :from => "", :to => "2007-03-23"
Chris@1115 494 assert_nil assigns(:from)
Chris@1115 495 assert_equal '2007-03-23'.to_date, assigns(:to)
Chris@1115 496 end
Chris@1115 497
Chris@1115 498 def test_index_today
Chris@1115 499 Date.stubs(:today).returns('2011-12-15'.to_date)
Chris@1115 500 get :index, :period => 'today'
Chris@1115 501 assert_equal '2011-12-15'.to_date, assigns(:from)
Chris@1115 502 assert_equal '2011-12-15'.to_date, assigns(:to)
Chris@1115 503 end
Chris@1115 504
Chris@1115 505 def test_index_yesterday
Chris@1115 506 Date.stubs(:today).returns('2011-12-15'.to_date)
Chris@1115 507 get :index, :period => 'yesterday'
Chris@1115 508 assert_equal '2011-12-14'.to_date, assigns(:from)
Chris@1115 509 assert_equal '2011-12-14'.to_date, assigns(:to)
Chris@1115 510 end
Chris@1115 511
Chris@1115 512 def test_index_current_week
Chris@1115 513 Date.stubs(:today).returns('2011-12-15'.to_date)
Chris@1115 514 get :index, :period => 'current_week'
Chris@1115 515 assert_equal '2011-12-12'.to_date, assigns(:from)
Chris@1115 516 assert_equal '2011-12-18'.to_date, assigns(:to)
Chris@1115 517 end
Chris@1115 518
Chris@1115 519 def test_index_last_week
Chris@1115 520 Date.stubs(:today).returns('2011-12-15'.to_date)
Chris@1115 521 get :index, :period => 'last_week'
Chris@1115 522 assert_equal '2011-12-05'.to_date, assigns(:from)
Chris@1115 523 assert_equal '2011-12-11'.to_date, assigns(:to)
Chris@1115 524 end
Chris@1115 525
Chris@1115 526 def test_index_last_2_week
Chris@1115 527 Date.stubs(:today).returns('2011-12-15'.to_date)
Chris@1115 528 get :index, :period => 'last_2_weeks'
Chris@1115 529 assert_equal '2011-11-28'.to_date, assigns(:from)
Chris@1115 530 assert_equal '2011-12-11'.to_date, assigns(:to)
Chris@1115 531 end
Chris@1115 532
Chris@1115 533 def test_index_7_days
Chris@1115 534 Date.stubs(:today).returns('2011-12-15'.to_date)
Chris@1115 535 get :index, :period => '7_days'
Chris@1115 536 assert_equal '2011-12-08'.to_date, assigns(:from)
Chris@1115 537 assert_equal '2011-12-15'.to_date, assigns(:to)
Chris@1115 538 end
Chris@1115 539
Chris@1115 540 def test_index_current_month
Chris@1115 541 Date.stubs(:today).returns('2011-12-15'.to_date)
Chris@1115 542 get :index, :period => 'current_month'
Chris@1115 543 assert_equal '2011-12-01'.to_date, assigns(:from)
Chris@1115 544 assert_equal '2011-12-31'.to_date, assigns(:to)
Chris@1115 545 end
Chris@1115 546
Chris@1115 547 def test_index_last_month
Chris@1115 548 Date.stubs(:today).returns('2011-12-15'.to_date)
Chris@1115 549 get :index, :period => 'last_month'
Chris@1115 550 assert_equal '2011-11-01'.to_date, assigns(:from)
Chris@1115 551 assert_equal '2011-11-30'.to_date, assigns(:to)
Chris@1115 552 end
Chris@1115 553
Chris@1115 554 def test_index_30_days
Chris@1115 555 Date.stubs(:today).returns('2011-12-15'.to_date)
Chris@1115 556 get :index, :period => '30_days'
Chris@1115 557 assert_equal '2011-11-15'.to_date, assigns(:from)
Chris@1115 558 assert_equal '2011-12-15'.to_date, assigns(:to)
Chris@1115 559 end
Chris@1115 560
Chris@1115 561 def test_index_current_year
Chris@1115 562 Date.stubs(:today).returns('2011-12-15'.to_date)
Chris@1115 563 get :index, :period => 'current_year'
Chris@1115 564 assert_equal '2011-01-01'.to_date, assigns(:from)
Chris@1115 565 assert_equal '2011-12-31'.to_date, assigns(:to)
Chris@1115 566 end
Chris@1115 567
chris@37 568 def test_index_at_issue_level
chris@37 569 get :index, :issue_id => 1
Chris@0 570 assert_response :success
chris@37 571 assert_template 'index'
Chris@0 572 assert_not_nil assigns(:entries)
Chris@0 573 assert_equal 2, assigns(:entries).size
Chris@0 574 assert_not_nil assigns(:total_hours)
Chris@0 575 assert_equal 154.25, assigns(:total_hours)
Chris@1115 576 # display all time
Chris@1115 577 assert_nil assigns(:from)
Chris@1115 578 assert_nil assigns(:to)
Chris@441 579 # TODO: remove /projects/:project_id/issues/:issue_id/time_entries routes
Chris@441 580 # to use /issues/:issue_id/time_entries
Chris@441 581 assert_tag :form,
Chris@441 582 :attributes => {:action => "/projects/ecookbook/issues/1/time_entries", :id => 'query_form'}
Chris@0 583 end
Chris@909 584
Chris@1115 585 def test_index_should_sort_by_spent_on_and_created_on
Chris@1115 586 t1 = TimeEntry.create!(:user => User.find(1), :project => Project.find(1), :hours => 1, :spent_on => '2012-06-16', :created_on => '2012-06-16 20:00:00', :activity_id => 10)
Chris@1115 587 t2 = TimeEntry.create!(:user => User.find(1), :project => Project.find(1), :hours => 1, :spent_on => '2012-06-16', :created_on => '2012-06-16 20:05:00', :activity_id => 10)
Chris@1115 588 t3 = TimeEntry.create!(:user => User.find(1), :project => Project.find(1), :hours => 1, :spent_on => '2012-06-15', :created_on => '2012-06-16 20:10:00', :activity_id => 10)
Chris@1115 589
Chris@1115 590 get :index, :project_id => 1, :from => '2012-06-15', :to => '2012-06-16'
Chris@1115 591 assert_response :success
Chris@1115 592 assert_equal [t2, t1, t3], assigns(:entries)
Chris@1115 593
Chris@1115 594 get :index, :project_id => 1, :from => '2012-06-15', :to => '2012-06-16', :sort => 'spent_on'
Chris@1115 595 assert_response :success
Chris@1115 596 assert_equal [t3, t1, t2], assigns(:entries)
Chris@1115 597 end
Chris@1115 598
chris@37 599 def test_index_atom_feed
chris@37 600 get :index, :project_id => 1, :format => 'atom'
Chris@0 601 assert_response :success
Chris@0 602 assert_equal 'application/atom+xml', @response.content_type
Chris@0 603 assert_not_nil assigns(:items)
Chris@0 604 assert assigns(:items).first.is_a?(TimeEntry)
Chris@0 605 end
Chris@909 606
chris@37 607 def test_index_all_projects_csv_export
Chris@0 608 Setting.date_format = '%m/%d/%Y'
chris@37 609 get :index, :format => 'csv'
Chris@0 610 assert_response :success
Chris@1115 611 assert_equal 'text/csv; header=present', @response.content_type
Chris@441 612 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
Chris@441 613 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
Chris@0 614 end
Chris@909 615
chris@37 616 def test_index_csv_export
Chris@0 617 Setting.date_format = '%m/%d/%Y'
chris@37 618 get :index, :project_id => 1, :format => 'csv'
Chris@0 619 assert_response :success
Chris@1115 620 assert_equal 'text/csv; header=present', @response.content_type
Chris@441 621 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
Chris@441 622 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
Chris@0 623 end
Chris@909 624
Chris@1115 625 def test_index_csv_export_with_multi_custom_field
Chris@1115 626 field = TimeEntryCustomField.create!(:name => 'Test', :field_format => 'list',
Chris@1115 627 :multiple => true, :possible_values => ['value1', 'value2'])
Chris@1115 628 entry = TimeEntry.find(1)
Chris@1115 629 entry.custom_field_values = {field.id => ['value1', 'value2']}
Chris@1115 630 entry.save!
Chris@1115 631
Chris@1115 632 get :index, :project_id => 1, :format => 'csv'
Chris@1115 633 assert_response :success
Chris@1115 634 assert_include '"value1, value2"', @response.body
Chris@1115 635 end
Chris@1115 636
Chris@909 637 def test_csv_big_5
Chris@909 638 user = User.find_by_id(3)
Chris@909 639 user.language = "zh-TW"
Chris@909 640 assert user.save
Chris@909 641 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
Chris@909 642 str_big5 = "\xa4@\xa4\xeb"
Chris@909 643 if str_utf8.respond_to?(:force_encoding)
Chris@909 644 str_utf8.force_encoding('UTF-8')
Chris@909 645 str_big5.force_encoding('Big5')
Chris@909 646 end
Chris@909 647 @request.session[:user_id] = 3
Chris@909 648 post :create, :project_id => 1,
Chris@909 649 :time_entry => {:comments => str_utf8,
Chris@909 650 # Not the default activity
Chris@909 651 :activity_id => '11',
Chris@909 652 :issue_id => '',
Chris@909 653 :spent_on => '2011-11-10',
Chris@909 654 :hours => '7.3'}
Chris@909 655 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
Chris@909 656
Chris@909 657 t = TimeEntry.find_by_comments(str_utf8)
Chris@909 658 assert_not_nil t
Chris@909 659 assert_equal 11, t.activity_id
Chris@909 660 assert_equal 7.3, t.hours
Chris@909 661 assert_equal 3, t.user_id
Chris@909 662
Chris@909 663 get :index, :project_id => 1, :format => 'csv',
Chris@909 664 :from => '2011-11-10', :to => '2011-11-10'
Chris@909 665 assert_response :success
Chris@1115 666 assert_equal 'text/csv; header=present', @response.content_type
Chris@909 667 ar = @response.body.chomp.split("\n")
Chris@909 668 s1 = "\xa4\xe9\xb4\xc1"
Chris@909 669 if str_utf8.respond_to?(:force_encoding)
Chris@909 670 s1.force_encoding('Big5')
Chris@909 671 end
Chris@909 672 assert ar[0].include?(s1)
Chris@909 673 assert ar[1].include?(str_big5)
Chris@909 674 end
Chris@909 675
Chris@909 676 def test_csv_cannot_convert_should_be_replaced_big_5
Chris@909 677 user = User.find_by_id(3)
Chris@909 678 user.language = "zh-TW"
Chris@909 679 assert user.save
Chris@909 680 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85"
Chris@909 681 if str_utf8.respond_to?(:force_encoding)
Chris@909 682 str_utf8.force_encoding('UTF-8')
Chris@909 683 end
Chris@909 684 @request.session[:user_id] = 3
Chris@909 685 post :create, :project_id => 1,
Chris@909 686 :time_entry => {:comments => str_utf8,
Chris@909 687 # Not the default activity
Chris@909 688 :activity_id => '11',
Chris@909 689 :issue_id => '',
Chris@909 690 :spent_on => '2011-11-10',
Chris@909 691 :hours => '7.3'}
Chris@909 692 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
Chris@909 693
Chris@909 694 t = TimeEntry.find_by_comments(str_utf8)
Chris@909 695 assert_not_nil t
Chris@909 696 assert_equal 11, t.activity_id
Chris@909 697 assert_equal 7.3, t.hours
Chris@909 698 assert_equal 3, t.user_id
Chris@909 699
Chris@909 700 get :index, :project_id => 1, :format => 'csv',
Chris@909 701 :from => '2011-11-10', :to => '2011-11-10'
Chris@909 702 assert_response :success
Chris@1115 703 assert_equal 'text/csv; header=present', @response.content_type
Chris@909 704 ar = @response.body.chomp.split("\n")
Chris@909 705 s1 = "\xa4\xe9\xb4\xc1"
Chris@909 706 if str_utf8.respond_to?(:force_encoding)
Chris@909 707 s1.force_encoding('Big5')
Chris@909 708 end
Chris@909 709 assert ar[0].include?(s1)
Chris@909 710 s2 = ar[1].split(",")[8]
Chris@909 711 if s2.respond_to?(:force_encoding)
Chris@909 712 s3 = "\xa5H?"
Chris@909 713 s3.force_encoding('Big5')
Chris@909 714 assert_equal s3, s2
Chris@909 715 elsif RUBY_PLATFORM == 'java'
Chris@909 716 assert_equal "??", s2
Chris@909 717 else
Chris@909 718 assert_equal "\xa5H???", s2
Chris@909 719 end
Chris@909 720 end
Chris@909 721
Chris@909 722 def test_csv_tw
Chris@909 723 with_settings :default_language => "zh-TW" do
Chris@909 724 str1 = "test_csv_tw"
Chris@909 725 user = User.find_by_id(3)
Chris@909 726 te1 = TimeEntry.create(:spent_on => '2011-11-10',
Chris@909 727 :hours => 999.9,
Chris@909 728 :project => Project.find(1),
Chris@909 729 :user => user,
Chris@909 730 :activity => TimeEntryActivity.find_by_name('Design'),
Chris@909 731 :comments => str1)
Chris@909 732 te2 = TimeEntry.find_by_comments(str1)
Chris@909 733 assert_not_nil te2
Chris@909 734 assert_equal 999.9, te2.hours
Chris@909 735 assert_equal 3, te2.user_id
Chris@909 736
Chris@909 737 get :index, :project_id => 1, :format => 'csv',
Chris@909 738 :from => '2011-11-10', :to => '2011-11-10'
Chris@909 739 assert_response :success
Chris@1115 740 assert_equal 'text/csv; header=present', @response.content_type
Chris@909 741
Chris@909 742 ar = @response.body.chomp.split("\n")
Chris@909 743 s2 = ar[1].split(",")[7]
Chris@909 744 assert_equal '999.9', s2
Chris@909 745
Chris@909 746 str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
Chris@909 747 if str_tw.respond_to?(:force_encoding)
Chris@909 748 str_tw.force_encoding('UTF-8')
Chris@909 749 end
Chris@909 750 assert_equal str_tw, l(:general_lang_name)
Chris@909 751 assert_equal ',', l(:general_csv_separator)
Chris@909 752 assert_equal '.', l(:general_csv_decimal_separator)
Chris@909 753 end
Chris@909 754 end
Chris@909 755
Chris@909 756 def test_csv_fr
Chris@909 757 with_settings :default_language => "fr" do
Chris@909 758 str1 = "test_csv_fr"
Chris@909 759 user = User.find_by_id(3)
Chris@909 760 te1 = TimeEntry.create(:spent_on => '2011-11-10',
Chris@909 761 :hours => 999.9,
Chris@909 762 :project => Project.find(1),
Chris@909 763 :user => user,
Chris@909 764 :activity => TimeEntryActivity.find_by_name('Design'),
Chris@909 765 :comments => str1)
Chris@909 766 te2 = TimeEntry.find_by_comments(str1)
Chris@909 767 assert_not_nil te2
Chris@909 768 assert_equal 999.9, te2.hours
Chris@909 769 assert_equal 3, te2.user_id
Chris@909 770
Chris@909 771 get :index, :project_id => 1, :format => 'csv',
Chris@909 772 :from => '2011-11-10', :to => '2011-11-10'
Chris@909 773 assert_response :success
Chris@1115 774 assert_equal 'text/csv; header=present', @response.content_type
Chris@909 775
Chris@909 776 ar = @response.body.chomp.split("\n")
Chris@909 777 s2 = ar[1].split(";")[7]
Chris@909 778 assert_equal '999,9', s2
Chris@909 779
Chris@909 780 str_fr = "Fran\xc3\xa7ais"
Chris@909 781 if str_fr.respond_to?(:force_encoding)
Chris@909 782 str_fr.force_encoding('UTF-8')
Chris@909 783 end
Chris@909 784 assert_equal str_fr, l(:general_lang_name)
Chris@909 785 assert_equal ';', l(:general_csv_separator)
Chris@909 786 assert_equal ',', l(:general_csv_decimal_separator)
Chris@909 787 end
Chris@909 788 end
Chris@0 789 end