annotate test/functional/timelog_controller_test.rb @ 887:f210ac4c5b05 bug_89

Close obsolete branch bug_89
author Chris Cannam
date Wed, 16 Mar 2011 13:44:25 +0000
parents 94944d00e43c
children af80e5618e9b
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@0 19 require File.dirname(__FILE__) + '/../test_helper'
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@0 97
Chris@0 98 def test_update
Chris@0 99 entry = TimeEntry.find(1)
Chris@0 100 assert_equal 1, entry.issue_id
Chris@0 101 assert_equal 2, entry.user_id
Chris@0 102
Chris@0 103 @request.session[:user_id] = 1
chris@37 104 put :update, :id => 1,
Chris@0 105 :time_entry => {:issue_id => '2',
Chris@0 106 :hours => '8'}
chris@37 107 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
Chris@0 108 entry.reload
Chris@0 109
Chris@0 110 assert_equal 8, entry.hours
Chris@0 111 assert_equal 2, entry.issue_id
Chris@0 112 assert_equal 2, entry.user_id
Chris@0 113 end
Chris@0 114
Chris@0 115 def test_destroy
Chris@0 116 @request.session[:user_id] = 2
chris@37 117 delete :destroy, :id => 1
chris@37 118 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
Chris@0 119 assert_equal I18n.t(:notice_successful_delete), flash[:notice]
Chris@0 120 assert_nil TimeEntry.find_by_id(1)
Chris@0 121 end
Chris@0 122
Chris@0 123 def test_destroy_should_fail
Chris@0 124 # simulate that this fails (e.g. due to a plugin), see #5700
Chris@0 125 TimeEntry.class_eval do
Chris@0 126 before_destroy :stop_callback_chain
Chris@0 127 def stop_callback_chain ; return false ; end
Chris@0 128 end
Chris@0 129
Chris@0 130 @request.session[:user_id] = 2
chris@37 131 delete :destroy, :id => 1
chris@37 132 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
Chris@0 133 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
Chris@0 134 assert_not_nil TimeEntry.find_by_id(1)
Chris@0 135
Chris@0 136 # remove the simulation
Chris@0 137 TimeEntry.before_destroy.reject! {|callback| callback.method == :stop_callback_chain }
Chris@0 138 end
Chris@0 139
chris@37 140 def test_index_all_projects
chris@37 141 get :index
Chris@0 142 assert_response :success
chris@37 143 assert_template 'index'
Chris@0 144 assert_not_nil assigns(:total_hours)
Chris@0 145 assert_equal "162.90", "%.2f" % assigns(:total_hours)
Chris@0 146 end
Chris@0 147
chris@37 148 def test_index_at_project_level
chris@37 149 get :index, :project_id => 1
Chris@0 150 assert_response :success
chris@37 151 assert_template 'index'
Chris@0 152 assert_not_nil assigns(:entries)
Chris@0 153 assert_equal 4, assigns(:entries).size
Chris@0 154 # project and subproject
Chris@0 155 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
Chris@0 156 assert_not_nil assigns(:total_hours)
Chris@0 157 assert_equal "162.90", "%.2f" % assigns(:total_hours)
Chris@0 158 # display all time by default
chris@22 159 assert_equal '2007-03-12'.to_date, assigns(:from)
Chris@0 160 assert_equal '2007-04-22'.to_date, assigns(:to)
Chris@0 161 end
Chris@0 162
chris@37 163 def test_index_at_project_level_with_date_range
chris@37 164 get :index, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30'
Chris@0 165 assert_response :success
chris@37 166 assert_template 'index'
Chris@0 167 assert_not_nil assigns(:entries)
Chris@0 168 assert_equal 3, assigns(:entries).size
Chris@0 169 assert_not_nil assigns(:total_hours)
Chris@0 170 assert_equal "12.90", "%.2f" % assigns(:total_hours)
Chris@0 171 assert_equal '2007-03-20'.to_date, assigns(:from)
Chris@0 172 assert_equal '2007-04-30'.to_date, assigns(:to)
Chris@0 173 end
Chris@0 174
chris@37 175 def test_index_at_project_level_with_period
chris@37 176 get :index, :project_id => 1, :period => '7_days'
Chris@0 177 assert_response :success
chris@37 178 assert_template 'index'
Chris@0 179 assert_not_nil assigns(:entries)
Chris@0 180 assert_not_nil assigns(:total_hours)
Chris@0 181 assert_equal Date.today - 7, assigns(:from)
Chris@0 182 assert_equal Date.today, assigns(:to)
Chris@0 183 end
Chris@0 184
chris@37 185 def test_index_one_day
chris@37 186 get :index, :project_id => 1, :from => "2007-03-23", :to => "2007-03-23"
Chris@0 187 assert_response :success
chris@37 188 assert_template 'index'
Chris@0 189 assert_not_nil assigns(:total_hours)
Chris@0 190 assert_equal "4.25", "%.2f" % assigns(:total_hours)
Chris@0 191 end
Chris@0 192
chris@37 193 def test_index_at_issue_level
chris@37 194 get :index, :issue_id => 1
Chris@0 195 assert_response :success
chris@37 196 assert_template 'index'
Chris@0 197 assert_not_nil assigns(:entries)
Chris@0 198 assert_equal 2, assigns(:entries).size
Chris@0 199 assert_not_nil assigns(:total_hours)
Chris@0 200 assert_equal 154.25, assigns(:total_hours)
chris@22 201 # display all time based on what's been logged
chris@22 202 assert_equal '2007-03-12'.to_date, assigns(:from)
Chris@0 203 assert_equal '2007-04-22'.to_date, assigns(:to)
Chris@0 204 end
Chris@0 205
chris@37 206 def test_index_atom_feed
chris@37 207 get :index, :project_id => 1, :format => 'atom'
Chris@0 208 assert_response :success
Chris@0 209 assert_equal 'application/atom+xml', @response.content_type
Chris@0 210 assert_not_nil assigns(:items)
Chris@0 211 assert assigns(:items).first.is_a?(TimeEntry)
Chris@0 212 end
Chris@0 213
chris@37 214 def test_index_all_projects_csv_export
Chris@0 215 Setting.date_format = '%m/%d/%Y'
chris@37 216 get :index, :format => 'csv'
Chris@0 217 assert_response :success
Chris@0 218 assert_equal 'text/csv', @response.content_type
Chris@0 219 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
Chris@0 220 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n")
Chris@0 221 end
Chris@0 222
chris@37 223 def test_index_csv_export
Chris@0 224 Setting.date_format = '%m/%d/%Y'
chris@37 225 get :index, :project_id => 1, :format => 'csv'
Chris@0 226 assert_response :success
Chris@0 227 assert_equal 'text/csv', @response.content_type
Chris@0 228 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
Chris@0 229 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n")
Chris@0 230 end
Chris@0 231 end