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