annotate .svn/pristine/f2/f26d36151ad640dabd27f80fd822c939d9ecad04.svn-base @ 1327:287f201c2802 redmine-2.2-integration

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