annotate test/functional/timelog_controller_test.rb @ 1452:d6b9fd02bb89 feature_36_js_refactoring

Deprecated develoment branch.
author luisf <luis.figueira@eecs.qmul.ac.uk>
date Fri, 11 Oct 2013 17:01:24 +0100
parents cbce1fd3b1b7
children cbb26bc654de
rev   line source
Chris@0 1 # -*- coding: utf-8 -*-
Chris@0 2 # redMine - project management software
Chris@0 3 # Copyright (C) 2006-2007 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@0 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@0 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@0 26 fixtures :projects, :enabled_modules, :roles, :members, :member_roles, :issues, :time_entries, :users, :trackers, :enumerations, :issue_statuses, :custom_fields, :custom_values
Chris@0 27
Chris@0 28 def setup
Chris@0 29 @controller = TimelogController.new
Chris@0 30 @request = ActionController::TestRequest.new
Chris@0 31 @response = ActionController::TestResponse.new
Chris@0 32 end
Chris@0 33
chris@37 34 def test_get_new
Chris@0 35 @request.session[:user_id] = 3
chris@37 36 get :new, :project_id => 1
Chris@0 37 assert_response :success
Chris@0 38 assert_template 'edit'
Chris@0 39 # Default activity selected
Chris@0 40 assert_tag :tag => 'option', :attributes => { :selected => 'selected' },
Chris@0 41 :content => 'Development'
Chris@0 42 end
Chris@0 43
chris@37 44 def test_get_new_should_only_show_active_time_entry_activities
chris@37 45 @request.session[:user_id] = 3
chris@37 46 get :new, :project_id => 1
chris@37 47 assert_response :success
chris@37 48 assert_template 'edit'
chris@37 49 assert_no_tag :tag => 'option', :content => 'Inactive Activity'
chris@37 50
chris@37 51 end
chris@37 52
Chris@0 53 def test_get_edit_existing_time
Chris@0 54 @request.session[:user_id] = 2
Chris@0 55 get :edit, :id => 2, :project_id => nil
Chris@0 56 assert_response :success
Chris@0 57 assert_template 'edit'
Chris@0 58 # Default activity selected
chris@37 59 assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/time_entries/2' }
Chris@0 60 end
Chris@0 61
Chris@0 62 def test_get_edit_with_an_existing_time_entry_with_inactive_activity
Chris@0 63 te = TimeEntry.find(1)
Chris@0 64 te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
Chris@0 65 te.save!
Chris@0 66
Chris@0 67 @request.session[:user_id] = 1
Chris@0 68 get :edit, :project_id => 1, :id => 1
Chris@0 69 assert_response :success
Chris@0 70 assert_template 'edit'
Chris@0 71 # Blank option since nothing is pre-selected
Chris@0 72 assert_tag :tag => 'option', :content => '--- Please select ---'
Chris@0 73 end
Chris@0 74
chris@37 75 def test_post_create
Chris@0 76 # TODO: should POST to issues’ time log instead of project. change form
Chris@0 77 # and routing
Chris@0 78 @request.session[:user_id] = 3
chris@37 79 post :create, :project_id => 1,
Chris@0 80 :time_entry => {:comments => 'Some work on TimelogControllerTest',
Chris@0 81 # Not the default activity
Chris@0 82 :activity_id => '11',
Chris@0 83 :spent_on => '2008-03-14',
Chris@0 84 :issue_id => '1',
Chris@0 85 :hours => '7.3'}
chris@37 86 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
Chris@0 87
Chris@0 88 i = Issue.find(1)
Chris@0 89 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
Chris@0 90 assert_not_nil t
Chris@0 91 assert_equal 11, t.activity_id
Chris@0 92 assert_equal 7.3, t.hours
Chris@0 93 assert_equal 3, t.user_id
Chris@0 94 assert_equal i, t.issue
Chris@0 95 assert_equal i.project, t.project
Chris@0 96 end
Chris@119 97
Chris@119 98 def test_post_create_with_blank_issue
Chris@119 99 # TODO: should POST to issues’ time log instead of project. change form
Chris@119 100 # and routing
Chris@119 101 @request.session[:user_id] = 3
Chris@119 102 post :create, :project_id => 1,
Chris@119 103 :time_entry => {:comments => 'Some work on TimelogControllerTest',
Chris@119 104 # Not the default activity
Chris@119 105 :activity_id => '11',
Chris@119 106 :issue_id => '',
Chris@119 107 :spent_on => '2008-03-14',
Chris@119 108 :hours => '7.3'}
Chris@119 109 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
Chris@119 110
Chris@119 111 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
Chris@119 112 assert_not_nil t
Chris@119 113 assert_equal 11, t.activity_id
Chris@119 114 assert_equal 7.3, t.hours
Chris@119 115 assert_equal 3, t.user_id
Chris@119 116 end
Chris@0 117
Chris@0 118 def test_update
Chris@0 119 entry = TimeEntry.find(1)
Chris@0 120 assert_equal 1, entry.issue_id
Chris@0 121 assert_equal 2, entry.user_id
Chris@0 122
Chris@0 123 @request.session[:user_id] = 1
chris@37 124 put :update, :id => 1,
Chris@0 125 :time_entry => {:issue_id => '2',
Chris@0 126 :hours => '8'}
chris@37 127 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
Chris@0 128 entry.reload
Chris@0 129
Chris@0 130 assert_equal 8, entry.hours
Chris@0 131 assert_equal 2, entry.issue_id
Chris@0 132 assert_equal 2, entry.user_id
Chris@0 133 end
Chris@441 134
Chris@441 135 def test_get_bulk_edit
Chris@441 136 @request.session[:user_id] = 2
Chris@441 137 get :bulk_edit, :ids => [1, 2]
Chris@441 138 assert_response :success
Chris@441 139 assert_template 'bulk_edit'
Chris@441 140
Chris@441 141 # System wide custom field
Chris@441 142 assert_tag :select, :attributes => {:name => 'time_entry[custom_field_values][10]'}
Chris@441 143 end
Chris@441 144
Chris@441 145 def test_get_bulk_edit_on_different_projects
Chris@441 146 @request.session[:user_id] = 2
Chris@441 147 get :bulk_edit, :ids => [1, 2, 6]
Chris@441 148 assert_response :success
Chris@441 149 assert_template 'bulk_edit'
Chris@441 150 end
Chris@441 151
Chris@441 152 def test_bulk_update
Chris@441 153 @request.session[:user_id] = 2
Chris@441 154 # update time entry activity
Chris@441 155 post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9}
Chris@441 156
Chris@441 157 assert_response 302
Chris@441 158 # check that the issues were updated
Chris@441 159 assert_equal [9, 9], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.activity_id}
Chris@441 160 end
Chris@441 161
Chris@441 162 def test_bulk_update_on_different_projects
Chris@441 163 @request.session[:user_id] = 2
Chris@441 164 # update time entry activity
Chris@441 165 post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 }
Chris@441 166
Chris@441 167 assert_response 302
Chris@441 168 # check that the issues were updated
Chris@441 169 assert_equal [9, 9, 9], TimeEntry.find_all_by_id([1, 2, 4]).collect {|i| i.activity_id}
Chris@441 170 end
Chris@441 171
Chris@441 172 def test_bulk_update_on_different_projects_without_rights
Chris@441 173 @request.session[:user_id] = 3
Chris@441 174 user = User.find(3)
Chris@441 175 action = { :controller => "timelog", :action => "bulk_update" }
Chris@441 176 assert user.allowed_to?(action, TimeEntry.find(1).project)
Chris@441 177 assert ! user.allowed_to?(action, TimeEntry.find(5).project)
Chris@441 178 post :bulk_update, :ids => [1, 5], :time_entry => { :activity_id => 9 }
Chris@441 179 assert_response 403
Chris@441 180 end
Chris@441 181
Chris@441 182 def test_bulk_update_custom_field
Chris@441 183 @request.session[:user_id] = 2
Chris@441 184 post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} }
Chris@441 185
Chris@441 186 assert_response 302
Chris@441 187 assert_equal ["0", "0"], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.custom_value_for(10).value}
Chris@441 188 end
Chris@441 189
Chris@441 190 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
Chris@441 191 @request.session[:user_id] = 2
Chris@441 192 post :bulk_update, :ids => [1,2], :back_url => '/time_entries'
Chris@441 193
Chris@441 194 assert_response :redirect
Chris@441 195 assert_redirected_to '/time_entries'
Chris@441 196 end
Chris@441 197
Chris@441 198 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
Chris@441 199 @request.session[:user_id] = 2
Chris@441 200 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
Chris@441 201
Chris@441 202 assert_response :redirect
Chris@441 203 assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier
Chris@441 204 end
Chris@0 205
Chris@0 206 def test_destroy
Chris@0 207 @request.session[:user_id] = 2
chris@37 208 delete :destroy, :id => 1
chris@37 209 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
Chris@0 210 assert_equal I18n.t(:notice_successful_delete), flash[:notice]
Chris@0 211 assert_nil TimeEntry.find_by_id(1)
Chris@0 212 end
Chris@0 213
Chris@0 214 def test_destroy_should_fail
Chris@0 215 # simulate that this fails (e.g. due to a plugin), see #5700
Chris@441 216 TimeEntry.any_instance.expects(:destroy).returns(false)
Chris@0 217
Chris@0 218 @request.session[:user_id] = 2
chris@37 219 delete :destroy, :id => 1
chris@37 220 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
Chris@0 221 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
Chris@0 222 assert_not_nil TimeEntry.find_by_id(1)
Chris@0 223 end
Chris@0 224
chris@37 225 def test_index_all_projects
chris@37 226 get :index
Chris@0 227 assert_response :success
chris@37 228 assert_template 'index'
Chris@0 229 assert_not_nil assigns(:total_hours)
Chris@0 230 assert_equal "162.90", "%.2f" % assigns(:total_hours)
Chris@441 231 assert_tag :form,
Chris@441 232 :attributes => {:action => "/time_entries", :id => 'query_form'}
Chris@0 233 end
Chris@0 234
chris@37 235 def test_index_at_project_level
Chris@441 236 get :index, :project_id => 'ecookbook'
Chris@0 237 assert_response :success
chris@37 238 assert_template 'index'
Chris@0 239 assert_not_nil assigns(:entries)
Chris@0 240 assert_equal 4, assigns(:entries).size
Chris@0 241 # project and subproject
Chris@0 242 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
Chris@0 243 assert_not_nil assigns(:total_hours)
Chris@0 244 assert_equal "162.90", "%.2f" % assigns(:total_hours)
Chris@0 245 # display all time by default
chris@22 246 assert_equal '2007-03-12'.to_date, assigns(:from)
Chris@0 247 assert_equal '2007-04-22'.to_date, assigns(:to)
Chris@441 248 assert_tag :form,
Chris@441 249 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
Chris@0 250 end
Chris@0 251
chris@37 252 def test_index_at_project_level_with_date_range
Chris@441 253 get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30'
Chris@0 254 assert_response :success
chris@37 255 assert_template 'index'
Chris@0 256 assert_not_nil assigns(:entries)
Chris@0 257 assert_equal 3, assigns(:entries).size
Chris@0 258 assert_not_nil assigns(:total_hours)
Chris@0 259 assert_equal "12.90", "%.2f" % assigns(:total_hours)
Chris@0 260 assert_equal '2007-03-20'.to_date, assigns(:from)
Chris@0 261 assert_equal '2007-04-30'.to_date, assigns(:to)
Chris@441 262 assert_tag :form,
Chris@441 263 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
Chris@0 264 end
Chris@0 265
chris@37 266 def test_index_at_project_level_with_period
Chris@441 267 get :index, :project_id => 'ecookbook', :period => '7_days'
Chris@0 268 assert_response :success
chris@37 269 assert_template 'index'
Chris@0 270 assert_not_nil assigns(:entries)
Chris@0 271 assert_not_nil assigns(:total_hours)
Chris@0 272 assert_equal Date.today - 7, assigns(:from)
Chris@0 273 assert_equal Date.today, assigns(:to)
Chris@441 274 assert_tag :form,
Chris@441 275 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
Chris@0 276 end
Chris@0 277
chris@37 278 def test_index_one_day
Chris@441 279 get :index, :project_id => 'ecookbook', :from => "2007-03-23", :to => "2007-03-23"
Chris@0 280 assert_response :success
chris@37 281 assert_template 'index'
Chris@0 282 assert_not_nil assigns(:total_hours)
Chris@0 283 assert_equal "4.25", "%.2f" % assigns(:total_hours)
Chris@441 284 assert_tag :form,
Chris@441 285 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
Chris@0 286 end
Chris@0 287
chris@37 288 def test_index_at_issue_level
chris@37 289 get :index, :issue_id => 1
Chris@0 290 assert_response :success
chris@37 291 assert_template 'index'
Chris@0 292 assert_not_nil assigns(:entries)
Chris@0 293 assert_equal 2, assigns(:entries).size
Chris@0 294 assert_not_nil assigns(:total_hours)
Chris@0 295 assert_equal 154.25, assigns(:total_hours)
chris@22 296 # display all time based on what's been logged
chris@22 297 assert_equal '2007-03-12'.to_date, assigns(:from)
Chris@0 298 assert_equal '2007-04-22'.to_date, assigns(:to)
Chris@441 299 # TODO: remove /projects/:project_id/issues/:issue_id/time_entries routes
Chris@441 300 # to use /issues/:issue_id/time_entries
Chris@441 301 assert_tag :form,
Chris@441 302 :attributes => {:action => "/projects/ecookbook/issues/1/time_entries", :id => 'query_form'}
Chris@0 303 end
Chris@0 304
chris@37 305 def test_index_atom_feed
chris@37 306 get :index, :project_id => 1, :format => 'atom'
Chris@0 307 assert_response :success
Chris@0 308 assert_equal 'application/atom+xml', @response.content_type
Chris@0 309 assert_not_nil assigns(:items)
Chris@0 310 assert assigns(:items).first.is_a?(TimeEntry)
Chris@0 311 end
Chris@0 312
chris@37 313 def test_index_all_projects_csv_export
Chris@0 314 Setting.date_format = '%m/%d/%Y'
chris@37 315 get :index, :format => 'csv'
Chris@0 316 assert_response :success
Chris@0 317 assert_equal 'text/csv', @response.content_type
Chris@441 318 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
Chris@441 319 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
Chris@0 320 end
Chris@0 321
chris@37 322 def test_index_csv_export
Chris@0 323 Setting.date_format = '%m/%d/%Y'
chris@37 324 get :index, :project_id => 1, :format => 'csv'
Chris@0 325 assert_response :success
Chris@0 326 assert_equal 'text/csv', @response.content_type
Chris@441 327 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment,Overtime\n")
Chris@441 328 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\",\"\"\n")
Chris@0 329 end
Chris@0 330 end