annotate .svn/pristine/f2/f26d36151ad640dabd27f80fd822c939d9ecad04.svn-base @ 1298:4f746d8966dd redmine_2.3_integration

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