##// END OF EJS Templates
Redmine loses filters after deleting a spent time (#14817)....
Jean-Philippe Lang -
r15674:d05d559d152d
parent child
Show More
@@ -1,286 +1,286
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2016 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class TimelogController < ApplicationController
19 19 menu_item :time_entries
20 20
21 21 before_action :find_time_entry, :only => [:show, :edit, :update]
22 22 before_action :check_editability, :only => [:edit, :update]
23 23 before_action :find_time_entries, :only => [:bulk_edit, :bulk_update, :destroy]
24 24 before_action :authorize, :only => [:show, :edit, :update, :bulk_edit, :bulk_update, :destroy]
25 25
26 26 before_action :find_optional_issue, :only => [:new, :create]
27 27 before_action :find_optional_project, :only => [:index, :report]
28 28 before_action :authorize_global, :only => [:new, :create, :index, :report]
29 29
30 30 accept_rss_auth :index
31 31 accept_api_auth :index, :show, :create, :update, :destroy
32 32
33 33 rescue_from Query::StatementInvalid, :with => :query_statement_invalid
34 34
35 35 helper :sort
36 36 include SortHelper
37 37 helper :issues
38 38 include TimelogHelper
39 39 helper :custom_fields
40 40 include CustomFieldsHelper
41 41 helper :queries
42 42 include QueriesHelper
43 43
44 44 def index
45 45 retrieve_time_entry_query
46 46 sort_init(@query.sort_criteria.empty? ? [['spent_on', 'desc']] : @query.sort_criteria)
47 47 sort_update(@query.sortable_columns)
48 48 scope = time_entry_scope(:order => sort_clause).
49 49 preload(:issue => [:project, :tracker, :status, :assigned_to, :priority]).
50 50 preload(:project, :user)
51 51
52 52 respond_to do |format|
53 53 format.html {
54 54 @entry_count = scope.count
55 55 @entry_pages = Paginator.new @entry_count, per_page_option, params['page']
56 56 @entries = scope.offset(@entry_pages.offset).limit(@entry_pages.per_page).to_a
57 57
58 58 render :layout => !request.xhr?
59 59 }
60 60 format.api {
61 61 @entry_count = scope.count
62 62 @offset, @limit = api_offset_and_limit
63 63 @entries = scope.offset(@offset).limit(@limit).preload(:custom_values => :custom_field).to_a
64 64 }
65 65 format.atom {
66 66 entries = scope.limit(Setting.feeds_limit.to_i).reorder("#{TimeEntry.table_name}.created_on DESC").to_a
67 67 render_feed(entries, :title => l(:label_spent_time))
68 68 }
69 69 format.csv {
70 70 # Export all entries
71 71 @entries = scope.to_a
72 72 send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv')
73 73 }
74 74 end
75 75 end
76 76
77 77 def report
78 78 retrieve_time_entry_query
79 79 scope = time_entry_scope
80 80
81 81 @report = Redmine::Helpers::TimeReport.new(@project, @issue, params[:criteria], params[:columns], scope)
82 82
83 83 respond_to do |format|
84 84 format.html { render :layout => !request.xhr? }
85 85 format.csv { send_data(report_to_csv(@report), :type => 'text/csv; header=present', :filename => 'timelog.csv') }
86 86 end
87 87 end
88 88
89 89 def show
90 90 respond_to do |format|
91 91 # TODO: Implement html response
92 92 format.html { head 406 }
93 93 format.api
94 94 end
95 95 end
96 96
97 97 def new
98 98 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
99 99 @time_entry.safe_attributes = params[:time_entry]
100 100 end
101 101
102 102 def create
103 103 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
104 104 @time_entry.safe_attributes = params[:time_entry]
105 105 if @time_entry.project && !User.current.allowed_to?(:log_time, @time_entry.project)
106 106 render_403
107 107 return
108 108 end
109 109
110 110 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
111 111
112 112 if @time_entry.save
113 113 respond_to do |format|
114 114 format.html {
115 115 flash[:notice] = l(:notice_successful_create)
116 116 if params[:continue]
117 117 options = {
118 118 :time_entry => {
119 119 :project_id => params[:time_entry][:project_id],
120 120 :issue_id => @time_entry.issue_id,
121 121 :activity_id => @time_entry.activity_id
122 122 },
123 123 :back_url => params[:back_url]
124 124 }
125 125 if params[:project_id] && @time_entry.project
126 126 redirect_to new_project_time_entry_path(@time_entry.project, options)
127 127 elsif params[:issue_id] && @time_entry.issue
128 128 redirect_to new_issue_time_entry_path(@time_entry.issue, options)
129 129 else
130 130 redirect_to new_time_entry_path(options)
131 131 end
132 132 else
133 133 redirect_back_or_default project_time_entries_path(@time_entry.project)
134 134 end
135 135 }
136 136 format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) }
137 137 end
138 138 else
139 139 respond_to do |format|
140 140 format.html { render :action => 'new' }
141 141 format.api { render_validation_errors(@time_entry) }
142 142 end
143 143 end
144 144 end
145 145
146 146 def edit
147 147 @time_entry.safe_attributes = params[:time_entry]
148 148 end
149 149
150 150 def update
151 151 @time_entry.safe_attributes = params[:time_entry]
152 152
153 153 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
154 154
155 155 if @time_entry.save
156 156 respond_to do |format|
157 157 format.html {
158 158 flash[:notice] = l(:notice_successful_update)
159 159 redirect_back_or_default project_time_entries_path(@time_entry.project)
160 160 }
161 161 format.api { render_api_ok }
162 162 end
163 163 else
164 164 respond_to do |format|
165 165 format.html { render :action => 'edit' }
166 166 format.api { render_validation_errors(@time_entry) }
167 167 end
168 168 end
169 169 end
170 170
171 171 def bulk_edit
172 172 @available_activities = @projects.map(&:activities).reduce(:&)
173 173 @custom_fields = TimeEntry.first.available_custom_fields.select {|field| field.format.bulk_edit_supported}
174 174 end
175 175
176 176 def bulk_update
177 177 attributes = parse_params_for_bulk_update(params[:time_entry])
178 178
179 179 unsaved_time_entry_ids = []
180 180 @time_entries.each do |time_entry|
181 181 time_entry.reload
182 182 time_entry.safe_attributes = attributes
183 183 call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry })
184 184 unless time_entry.save
185 185 logger.info "time entry could not be updated: #{time_entry.errors.full_messages}" if logger && logger.info?
186 186 # Keep unsaved time_entry ids to display them in flash error
187 187 unsaved_time_entry_ids << time_entry.id
188 188 end
189 189 end
190 190 set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids)
191 191 redirect_back_or_default project_time_entries_path(@projects.first)
192 192 end
193 193
194 194 def destroy
195 195 destroyed = TimeEntry.transaction do
196 196 @time_entries.each do |t|
197 197 unless t.destroy && t.destroyed?
198 198 raise ActiveRecord::Rollback
199 199 end
200 200 end
201 201 end
202 202
203 203 respond_to do |format|
204 204 format.html {
205 205 if destroyed
206 206 flash[:notice] = l(:notice_successful_delete)
207 207 else
208 208 flash[:error] = l(:notice_unable_delete_time_entry)
209 209 end
210 redirect_back_or_default project_time_entries_path(@projects.first)
210 redirect_back_or_default project_time_entries_path(@projects.first), :referer => true
211 211 }
212 212 format.api {
213 213 if destroyed
214 214 render_api_ok
215 215 else
216 216 render_validation_errors(@time_entries)
217 217 end
218 218 }
219 219 end
220 220 end
221 221
222 222 private
223 223 def find_time_entry
224 224 @time_entry = TimeEntry.find(params[:id])
225 225 @project = @time_entry.project
226 226 rescue ActiveRecord::RecordNotFound
227 227 render_404
228 228 end
229 229
230 230 def check_editability
231 231 unless @time_entry.editable_by?(User.current)
232 232 render_403
233 233 return false
234 234 end
235 235 end
236 236
237 237 def find_time_entries
238 238 @time_entries = TimeEntry.where(:id => params[:id] || params[:ids]).
239 239 preload(:project => :time_entry_activities).
240 240 preload(:user).to_a
241 241
242 242 raise ActiveRecord::RecordNotFound if @time_entries.empty?
243 243 raise Unauthorized unless @time_entries.all? {|t| t.editable_by?(User.current)}
244 244 @projects = @time_entries.collect(&:project).compact.uniq
245 245 @project = @projects.first if @projects.size == 1
246 246 rescue ActiveRecord::RecordNotFound
247 247 render_404
248 248 end
249 249
250 250 def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids)
251 251 if unsaved_time_entry_ids.empty?
252 252 flash[:notice] = l(:notice_successful_update) unless time_entries.empty?
253 253 else
254 254 flash[:error] = l(:notice_failed_to_save_time_entries,
255 255 :count => unsaved_time_entry_ids.size,
256 256 :total => time_entries.size,
257 257 :ids => '#' + unsaved_time_entry_ids.join(', #'))
258 258 end
259 259 end
260 260
261 261 def find_optional_issue
262 262 if params[:issue_id].present?
263 263 @issue = Issue.find(params[:issue_id])
264 264 @project = @issue.project
265 265 else
266 266 find_optional_project
267 267 end
268 268 end
269 269
270 270 def find_optional_project
271 271 if params[:project_id].present?
272 272 @project = Project.find(params[:project_id])
273 273 end
274 274 rescue ActiveRecord::RecordNotFound
275 275 render_404
276 276 end
277 277
278 278 # Returns the TimeEntry scope for index and report actions
279 279 def time_entry_scope(options={})
280 280 @query.results_scope(options)
281 281 end
282 282
283 283 def retrieve_time_entry_query
284 284 retrieve_query(TimeEntryQuery, false)
285 285 end
286 286 end
@@ -1,1020 +1,1029
1 1 # -*- coding: utf-8 -*-
2 2 # Redmine - project management software
3 3 # Copyright (C) 2006-2016 Jean-Philippe Lang
4 4 #
5 5 # This program is free software; you can redistribute it and/or
6 6 # modify it under the terms of the GNU General Public License
7 7 # as published by the Free Software Foundation; either version 2
8 8 # of the License, or (at your option) any later version.
9 9 #
10 10 # This program is distributed in the hope that it will be useful,
11 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 13 # GNU General Public License for more details.
14 14 #
15 15 # You should have received a copy of the GNU General Public License
16 16 # along with this program; if not, write to the Free Software
17 17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 18
19 19 require File.expand_path('../../test_helper', __FILE__)
20 20
21 21 class TimelogControllerTest < Redmine::ControllerTest
22 22 fixtures :projects, :enabled_modules, :roles, :members,
23 23 :member_roles, :issues, :time_entries, :users,
24 24 :trackers, :enumerations, :issue_statuses,
25 25 :custom_fields, :custom_values,
26 26 :projects_trackers, :custom_fields_trackers,
27 27 :custom_fields_projects
28 28
29 29 include Redmine::I18n
30 30
31 31 def test_new
32 32 @request.session[:user_id] = 3
33 33 get :new
34 34 assert_response :success
35 35
36 36 assert_select 'input[name=?][type=hidden]', 'project_id', 0
37 37 assert_select 'input[name=?][type=hidden]', 'issue_id', 0
38 38 assert_select 'span[id=?]', 'time_entry_issue'
39 39 assert_select 'select[name=?]', 'time_entry[project_id]' do
40 40 # blank option for project
41 41 assert_select 'option[value=""]'
42 42 end
43 43 end
44 44
45 45 def test_new_with_project_id
46 46 @request.session[:user_id] = 3
47 47 get :new, :params => {:project_id => 1}
48 48 assert_response :success
49 49
50 50 assert_select 'input[name=?][type=hidden]', 'project_id'
51 51 assert_select 'input[name=?][type=hidden]', 'issue_id', 0
52 52 assert_select 'select[name=?]', 'time_entry[project_id]', 0
53 53 end
54 54
55 55 def test_new_with_issue_id
56 56 @request.session[:user_id] = 3
57 57 get :new, :params => {:issue_id => 2}
58 58 assert_response :success
59 59
60 60 assert_select 'input[name=?][type=hidden]', 'project_id', 0
61 61 assert_select 'input[name=?][type=hidden]', 'issue_id'
62 62 assert_select 'a[href=?]', '/issues/2', :text => /Feature request #2/
63 63 assert_select 'select[name=?]', 'time_entry[project_id]', 0
64 64 end
65 65
66 66 def test_new_without_project_should_prefill_the_form
67 67 @request.session[:user_id] = 3
68 68 get :new, :params => {:time_entry => {:project_id => '1'}}
69 69 assert_response :success
70 70
71 71 assert_select 'select[name=?]', 'time_entry[project_id]' do
72 72 assert_select 'option[value="1"][selected=selected]'
73 73 end
74 74 end
75 75
76 76 def test_new_without_project_should_deny_without_permission
77 77 Role.all.each {|role| role.remove_permission! :log_time}
78 78 @request.session[:user_id] = 3
79 79
80 80 get :new
81 81 assert_response 403
82 82 end
83 83
84 84 def test_new_should_select_default_activity
85 85 @request.session[:user_id] = 3
86 86 get :new, :params => {:project_id => 1}
87 87 assert_response :success
88 88 assert_select 'select[name=?]', 'time_entry[activity_id]' do
89 89 assert_select 'option[selected=selected]', :text => 'Development'
90 90 end
91 91 end
92 92
93 93 def test_new_should_only_show_active_time_entry_activities
94 94 @request.session[:user_id] = 3
95 95 get :new, :params => {:project_id => 1}
96 96 assert_response :success
97 97 assert_select 'option', :text => 'Inactive Activity', :count => 0
98 98 end
99 99
100 100 def test_post_new_as_js_should_update_activity_options
101 101 @request.session[:user_id] = 3
102 102 post :new, :params => {:time_entry => {:project_id => 1}, :format => 'js'}
103 103 assert_response :success
104 104 assert_include '#time_entry_activity_id', response.body
105 105 end
106 106
107 107 def test_get_edit_existing_time
108 108 @request.session[:user_id] = 2
109 109 get :edit, :params => {:id => 2, :project_id => nil}
110 110 assert_response :success
111 111
112 112 assert_select 'form[action=?]', '/time_entries/2'
113 113 end
114 114
115 115 def test_get_edit_with_an_existing_time_entry_with_inactive_activity
116 116 te = TimeEntry.find(1)
117 117 te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
118 118 te.save!(:validate => false)
119 119
120 120 @request.session[:user_id] = 1
121 121 get :edit, :params => {:project_id => 1, :id => 1}
122 122 assert_response :success
123 123
124 124 # Blank option since nothing is pre-selected
125 125 assert_select 'option', :text => '--- Please select ---'
126 126 end
127 127
128 128 def test_post_create
129 129 @request.session[:user_id] = 3
130 130 assert_difference 'TimeEntry.count' do
131 131 post :create, :params => {
132 132 :project_id => 1,
133 133 :time_entry => {:comments => 'Some work on TimelogControllerTest',
134 134 # Not the default activity
135 135 :activity_id => '11',
136 136 :spent_on => '2008-03-14',
137 137 :issue_id => '1',
138 138 :hours => '7.3'
139 139 }
140 140 }
141 141 assert_redirected_to '/projects/ecookbook/time_entries'
142 142 end
143 143
144 144 t = TimeEntry.order('id DESC').first
145 145 assert_not_nil t
146 146 assert_equal 'Some work on TimelogControllerTest', t.comments
147 147 assert_equal 1, t.project_id
148 148 assert_equal 1, t.issue_id
149 149 assert_equal 11, t.activity_id
150 150 assert_equal 7.3, t.hours
151 151 assert_equal 3, t.user_id
152 152 end
153 153
154 154 def test_post_create_with_blank_issue
155 155 @request.session[:user_id] = 3
156 156 assert_difference 'TimeEntry.count' do
157 157 post :create, :params => {
158 158 :project_id => 1,
159 159 :time_entry => {
160 160 :comments => 'Some work on TimelogControllerTest',
161 161 # Not the default activity
162 162 :activity_id => '11',
163 163 :issue_id => '',
164 164 :spent_on => '2008-03-14',
165 165 :hours => '7.3'
166 166 }
167 167 }
168 168 assert_redirected_to '/projects/ecookbook/time_entries'
169 169 end
170 170
171 171 t = TimeEntry.order('id DESC').first
172 172 assert_not_nil t
173 173 assert_equal 'Some work on TimelogControllerTest', t.comments
174 174 assert_equal 1, t.project_id
175 175 assert_nil t.issue_id
176 176 assert_equal 11, t.activity_id
177 177 assert_equal 7.3, t.hours
178 178 assert_equal 3, t.user_id
179 179 end
180 180
181 181 def test_create_on_project_with_time_tracking_disabled_should_fail
182 182 Project.find(1).disable_module! :time_tracking
183 183
184 184 @request.session[:user_id] = 2
185 185 assert_no_difference 'TimeEntry.count' do
186 186 post :create, :params => {
187 187 :time_entry => {
188 188 :project_id => '1', :issue_id => '',
189 189 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
190 190 }
191 191 }
192 192 end
193 193 end
194 194
195 195 def test_create_on_project_without_permission_should_fail
196 196 Role.find(1).remove_permission! :log_time
197 197
198 198 @request.session[:user_id] = 2
199 199 assert_no_difference 'TimeEntry.count' do
200 200 post :create, :params => {
201 201 :time_entry => {
202 202 :project_id => '1', :issue_id => '',
203 203 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
204 204 }
205 205 }
206 206 end
207 207 end
208 208
209 209 def test_create_on_issue_in_project_with_time_tracking_disabled_should_fail
210 210 Project.find(1).disable_module! :time_tracking
211 211
212 212 @request.session[:user_id] = 2
213 213 assert_no_difference 'TimeEntry.count' do
214 214 post :create, :params => {
215 215 :time_entry => {
216 216 :project_id => '', :issue_id => '1',
217 217 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
218 218 }
219 219 }
220 220 assert_select_error /Issue is invalid/
221 221 end
222 222 end
223 223
224 224 def test_create_on_issue_in_project_without_permission_should_fail
225 225 Role.find(1).remove_permission! :log_time
226 226
227 227 @request.session[:user_id] = 2
228 228 assert_no_difference 'TimeEntry.count' do
229 229 post :create, :params => {
230 230 :time_entry => {
231 231 :project_id => '', :issue_id => '1',
232 232 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
233 233 }
234 234 }
235 235 assert_select_error /Issue is invalid/
236 236 end
237 237 end
238 238
239 239 def test_create_on_issue_that_is_not_visible_should_not_disclose_subject
240 240 issue = Issue.generate!(:subject => "issue_that_is_not_visible", :is_private => true)
241 241 assert !issue.visible?(User.find(3))
242 242
243 243 @request.session[:user_id] = 3
244 244 assert_no_difference 'TimeEntry.count' do
245 245 post :create, :params => {
246 246 :time_entry => {
247 247 :project_id => '', :issue_id => issue.id.to_s,
248 248 :activity_id => '11', :spent_on => '2008-03-14', :hours => '7.3'
249 249 }
250 250 }
251 251 end
252 252 assert_select_error /Issue is invalid/
253 253 assert_select "input[name=?][value=?]", "time_entry[issue_id]", issue.id.to_s
254 254 assert_select "#time_entry_issue a", 0
255 255 assert !response.body.include?('issue_that_is_not_visible')
256 256 end
257 257
258 258 def test_create_and_continue_at_project_level
259 259 @request.session[:user_id] = 2
260 260 assert_difference 'TimeEntry.count' do
261 261 post :create, :params => {
262 262 :time_entry => {
263 263 :project_id => '1',
264 264 :activity_id => '11',
265 265 :issue_id => '',
266 266 :spent_on => '2008-03-14',
267 267 :hours => '7.3'
268 268 },
269 269 :continue => '1'
270 270 }
271 271 assert_redirected_to '/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=&time_entry%5Bproject_id%5D=1'
272 272 end
273 273 end
274 274
275 275 def test_create_and_continue_at_issue_level
276 276 @request.session[:user_id] = 2
277 277 assert_difference 'TimeEntry.count' do
278 278 post :create, :params => {
279 279 :time_entry => {
280 280 :project_id => '',
281 281 :activity_id => '11',
282 282 :issue_id => '1',
283 283 :spent_on => '2008-03-14',
284 284 :hours => '7.3'
285 285 },
286 286 :continue => '1'
287 287 }
288 288 assert_redirected_to '/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=1&time_entry%5Bproject_id%5D='
289 289 end
290 290 end
291 291
292 292 def test_create_and_continue_with_project_id
293 293 @request.session[:user_id] = 2
294 294 assert_difference 'TimeEntry.count' do
295 295 post :create, :params => {
296 296 :project_id => 1,
297 297 :time_entry => {
298 298 :activity_id => '11',
299 299 :issue_id => '',
300 300 :spent_on => '2008-03-14',
301 301 :hours => '7.3'
302 302 },
303 303 :continue => '1'
304 304 }
305 305 assert_redirected_to '/projects/ecookbook/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=&time_entry%5Bproject_id%5D='
306 306 end
307 307 end
308 308
309 309 def test_create_and_continue_with_issue_id
310 310 @request.session[:user_id] = 2
311 311 assert_difference 'TimeEntry.count' do
312 312 post :create, :params => {
313 313 :issue_id => 1,
314 314 :time_entry => {
315 315 :activity_id => '11',
316 316 :issue_id => '1',
317 317 :spent_on => '2008-03-14',
318 318 :hours => '7.3'
319 319 },
320 320 :continue => '1'
321 321 }
322 322 assert_redirected_to '/issues/1/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=1&time_entry%5Bproject_id%5D='
323 323 end
324 324 end
325 325
326 326 def test_create_without_log_time_permission_should_be_denied
327 327 @request.session[:user_id] = 2
328 328 Role.find_by_name('Manager').remove_permission! :log_time
329 329 post :create, :params => {
330 330 :project_id => 1,
331 331 :time_entry => {
332 332 :activity_id => '11',
333 333 :issue_id => '',
334 334 :spent_on => '2008-03-14',
335 335 :hours => '7.3'
336 336 }
337 337 }
338 338 assert_response 403
339 339 end
340 340
341 341 def test_create_without_project_and_issue_should_fail
342 342 @request.session[:user_id] = 2
343 343 post :create, :params => {:time_entry => {:issue_id => ''}}
344 344
345 345 assert_response :success
346 346 assert_select_error /Project cannot be blank/
347 347 end
348 348
349 349 def test_create_with_failure
350 350 @request.session[:user_id] = 2
351 351 post :create, :params => {
352 352 :project_id => 1,
353 353 :time_entry => {
354 354 :activity_id => '',
355 355 :issue_id => '',
356 356 :spent_on => '2008-03-14',
357 357 :hours => '7.3'
358 358 }
359 359 }
360 360 assert_response :success
361 361 end
362 362
363 363 def test_create_without_project
364 364 @request.session[:user_id] = 2
365 365 assert_difference 'TimeEntry.count' do
366 366 post :create, :params => {
367 367 :time_entry => {
368 368 :project_id => '1',
369 369 :activity_id => '11',
370 370 :issue_id => '',
371 371 :spent_on => '2008-03-14',
372 372 :hours => '7.3'
373 373 }
374 374 }
375 375 end
376 376
377 377 assert_redirected_to '/projects/ecookbook/time_entries'
378 378 time_entry = TimeEntry.order('id DESC').first
379 379 assert_equal 1, time_entry.project_id
380 380 end
381 381
382 382 def test_create_without_project_should_fail_with_issue_not_inside_project
383 383 @request.session[:user_id] = 2
384 384 assert_no_difference 'TimeEntry.count' do
385 385 post :create, :params => {
386 386 :time_entry => {
387 387 :project_id => '1',
388 388 :activity_id => '11',
389 389 :issue_id => '5',
390 390 :spent_on => '2008-03-14',
391 391 :hours => '7.3'
392 392 }
393 393 }
394 394 end
395 395
396 396 assert_response :success
397 397 assert_select_error /Issue is invalid/
398 398 end
399 399
400 400 def test_create_without_project_should_deny_without_permission
401 401 @request.session[:user_id] = 2
402 402 Project.find(3).disable_module!(:time_tracking)
403 403
404 404 assert_no_difference 'TimeEntry.count' do
405 405 post :create, :params => {
406 406 :time_entry => {
407 407 :project_id => '3',
408 408 :activity_id => '11',
409 409 :issue_id => '',
410 410 :spent_on => '2008-03-14',
411 411 :hours => '7.3'
412 412 }
413 413 }
414 414 end
415 415
416 416 assert_response 403
417 417 end
418 418
419 419 def test_create_without_project_with_failure
420 420 @request.session[:user_id] = 2
421 421 assert_no_difference 'TimeEntry.count' do
422 422 post :create, :params => {
423 423 :time_entry => {
424 424 :project_id => '1',
425 425 :activity_id => '11',
426 426 :issue_id => '',
427 427 :spent_on => '2008-03-14',
428 428 :hours => ''
429 429 }
430 430 }
431 431 end
432 432
433 433 assert_response :success
434 434 assert_select 'select[name=?]', 'time_entry[project_id]' do
435 435 assert_select 'option[value="1"][selected=selected]'
436 436 end
437 437 end
438 438
439 439 def test_update
440 440 entry = TimeEntry.find(1)
441 441 assert_equal 1, entry.issue_id
442 442 assert_equal 2, entry.user_id
443 443
444 444 @request.session[:user_id] = 1
445 445 put :update, :params => {
446 446 :id => 1,
447 447 :time_entry => {
448 448 :issue_id => '2',
449 449 :hours => '8'
450 450 }
451 451 }
452 452 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
453 453 entry.reload
454 454
455 455 assert_equal 8, entry.hours
456 456 assert_equal 2, entry.issue_id
457 457 assert_equal 2, entry.user_id
458 458 end
459 459
460 460 def test_update_should_allow_to_change_issue_to_another_project
461 461 entry = TimeEntry.generate!(:issue_id => 1)
462 462
463 463 @request.session[:user_id] = 1
464 464 put :update, :params => {
465 465 :id => entry.id,
466 466 :time_entry => {
467 467 :issue_id => '5'
468 468 }
469 469 }
470 470 assert_response 302
471 471 entry.reload
472 472
473 473 assert_equal 5, entry.issue_id
474 474 assert_equal 3, entry.project_id
475 475 end
476 476
477 477 def test_update_should_not_allow_to_change_issue_to_an_invalid_project
478 478 entry = TimeEntry.generate!(:issue_id => 1)
479 479 Project.find(3).disable_module!(:time_tracking)
480 480
481 481 @request.session[:user_id] = 1
482 482 put :update, :params => {
483 483 :id => entry.id,
484 484 :time_entry => {
485 485 :issue_id => '5'
486 486 }
487 487 }
488 488 assert_response :success
489 489 assert_select_error /Issue is invalid/
490 490 end
491 491
492 492 def test_get_bulk_edit
493 493 @request.session[:user_id] = 2
494 494
495 495 get :bulk_edit, :params => {:ids => [1, 2]}
496 496 assert_response :success
497 497
498 498 assert_select 'ul#bulk-selection' do
499 499 assert_select 'li', 2
500 500 assert_select 'li a', :text => '03/23/2007 - eCookbook: 4.25 hours'
501 501 end
502 502
503 503 assert_select 'form#bulk_edit_form[action=?]', '/time_entries/bulk_update' do
504 504 # System wide custom field
505 505 assert_select 'select[name=?]', 'time_entry[custom_field_values][10]'
506 506
507 507 # Activities
508 508 assert_select 'select[name=?]', 'time_entry[activity_id]' do
509 509 assert_select 'option[value=""]', :text => '(No change)'
510 510 assert_select 'option[value="9"]', :text => 'Design'
511 511 end
512 512 end
513 513 end
514 514
515 515 def test_get_bulk_edit_on_different_projects
516 516 @request.session[:user_id] = 2
517 517
518 518 get :bulk_edit, :params => {:ids => [1, 2, 6]}
519 519 assert_response :success
520 520 end
521 521
522 522 def test_bulk_edit_with_edit_own_time_entries_permission
523 523 @request.session[:user_id] = 2
524 524 Role.find_by_name('Manager').remove_permission! :edit_time_entries
525 525 Role.find_by_name('Manager').add_permission! :edit_own_time_entries
526 526 ids = (0..1).map {TimeEntry.generate!(:user => User.find(2)).id}
527 527
528 528 get :bulk_edit, :params => {:ids => ids}
529 529 assert_response :success
530 530 end
531 531
532 532 def test_bulk_update
533 533 @request.session[:user_id] = 2
534 534 # update time entry activity
535 535 post :bulk_update, :params => {:ids => [1, 2], :time_entry => { :activity_id => 9}}
536 536
537 537 assert_response 302
538 538 # check that the issues were updated
539 539 assert_equal [9, 9], TimeEntry.where(:id => [1, 2]).collect {|i| i.activity_id}
540 540 end
541 541
542 542 def test_bulk_update_with_failure
543 543 @request.session[:user_id] = 2
544 544 post :bulk_update, :params => {:ids => [1, 2], :time_entry => { :hours => 'A'}}
545 545
546 546 assert_response 302
547 547 assert_match /Failed to save 2 time entrie/, flash[:error]
548 548 end
549 549
550 550 def test_bulk_update_on_different_projects
551 551 @request.session[:user_id] = 2
552 552 # makes user a manager on the other project
553 553 Member.create!(:user_id => 2, :project_id => 3, :role_ids => [1])
554 554
555 555 # update time entry activity
556 556 post :bulk_update, :params => {:ids => [1, 2, 4], :time_entry => { :activity_id => 9 }}
557 557
558 558 assert_response 302
559 559 # check that the issues were updated
560 560 assert_equal [9, 9, 9], TimeEntry.where(:id => [1, 2, 4]).collect {|i| i.activity_id}
561 561 end
562 562
563 563 def test_bulk_update_on_different_projects_without_rights
564 564 @request.session[:user_id] = 3
565 565 user = User.find(3)
566 566 action = { :controller => "timelog", :action => "bulk_update" }
567 567 assert user.allowed_to?(action, TimeEntry.find(1).project)
568 568 assert ! user.allowed_to?(action, TimeEntry.find(5).project)
569 569
570 570 post :bulk_update, :params => {:ids => [1, 5], :time_entry => { :activity_id => 9 }}
571 571 assert_response 403
572 572 end
573 573
574 574 def test_bulk_update_with_edit_own_time_entries_permission
575 575 @request.session[:user_id] = 2
576 576 Role.find_by_name('Manager').remove_permission! :edit_time_entries
577 577 Role.find_by_name('Manager').add_permission! :edit_own_time_entries
578 578 ids = (0..1).map {TimeEntry.generate!(:user => User.find(2)).id}
579 579
580 580 post :bulk_update, :params => {:ids => ids, :time_entry => { :activity_id => 9 }}
581 581 assert_response 302
582 582 end
583 583
584 584 def test_bulk_update_with_edit_own_time_entries_permissions_should_be_denied_for_time_entries_of_other_user
585 585 @request.session[:user_id] = 2
586 586 Role.find_by_name('Manager').remove_permission! :edit_time_entries
587 587 Role.find_by_name('Manager').add_permission! :edit_own_time_entries
588 588
589 589 post :bulk_update, :params => {:ids => [1, 2], :time_entry => { :activity_id => 9 }}
590 590 assert_response 403
591 591 end
592 592
593 593 def test_bulk_update_custom_field
594 594 @request.session[:user_id] = 2
595 595 post :bulk_update, :params => {:ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} }}
596 596
597 597 assert_response 302
598 598 assert_equal ["0", "0"], TimeEntry.where(:id => [1, 2]).collect {|i| i.custom_value_for(10).value}
599 599 end
600 600
601 601 def test_bulk_update_clear_custom_field
602 602 field = TimeEntryCustomField.generate!(:field_format => 'string')
603 603 @request.session[:user_id] = 2
604 604 post :bulk_update, :params => {:ids => [1, 2], :time_entry => { :custom_field_values => {field.id.to_s => '__none__'} }}
605 605
606 606 assert_response 302
607 607 assert_equal ["", ""], TimeEntry.where(:id => [1, 2]).collect {|i| i.custom_value_for(field).value}
608 608 end
609 609
610 610 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
611 611 @request.session[:user_id] = 2
612 612 post :bulk_update, :params => {:ids => [1,2], :back_url => '/time_entries'}
613 613
614 614 assert_response :redirect
615 615 assert_redirected_to '/time_entries'
616 616 end
617 617
618 618 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
619 619 @request.session[:user_id] = 2
620 620 post :bulk_update, :params => {:ids => [1,2], :back_url => 'http://google.com'}
621 621
622 622 assert_response :redirect
623 623 assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier
624 624 end
625 625
626 626 def test_post_bulk_update_without_edit_permission_should_be_denied
627 627 @request.session[:user_id] = 2
628 628 Role.find_by_name('Manager').remove_permission! :edit_time_entries
629 629
630 630 post :bulk_update, :params => {:ids => [1,2]}
631 631 assert_response 403
632 632 end
633 633
634 634 def test_destroy
635 635 @request.session[:user_id] = 2
636 636
637 637 delete :destroy, :params => {:id => 1}
638 638 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
639 639 assert_equal I18n.t(:notice_successful_delete), flash[:notice]
640 640 assert_nil TimeEntry.find_by_id(1)
641 641 end
642 642
643 643 def test_destroy_should_fail
644 644 # simulate that this fails (e.g. due to a plugin), see #5700
645 645 TimeEntry.any_instance.expects(:destroy).returns(false)
646 646 @request.session[:user_id] = 2
647 647
648 648 delete :destroy, :params => {:id => 1}
649 649 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
650 650 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
651 651 assert_not_nil TimeEntry.find_by_id(1)
652 652 end
653 653
654 def test_destroy_should_redirect_to_referer
655 referer = 'http://test.host/time_entries?utf8=βœ“&set_filter=1&&f%5B%5D=user_id&op%5Buser_id%5D=%3D&v%5Buser_id%5D%5B%5D=me'
656 @request.env["HTTP_REFERER"] = referer
657 @request.session[:user_id] = 2
658
659 delete :destroy, :params => {:id => 1}
660 assert_redirected_to referer
661 end
662
654 663 def test_index_all_projects
655 664 get :index
656 665 assert_response :success
657 666
658 667 assert_select '.total-for-hours', :text => 'Hours: 162.90'
659 668 assert_select 'form#query_form[action=?]', '/time_entries'
660 669 end
661 670
662 671 def test_index_all_projects_should_show_log_time_link
663 672 @request.session[:user_id] = 2
664 673 get :index
665 674 assert_response :success
666 675
667 676 assert_select 'a[href=?]', '/time_entries/new', :text => /Log time/
668 677 end
669 678
670 679 def test_index_my_spent_time
671 680 @request.session[:user_id] = 2
672 681 get :index, :params => {:user_id => 'me', :c => ['user']}
673 682 assert_response :success
674 683
675 684 users = css_select('table.time-entries tbody td.user').map(&:text).uniq
676 685 assert_equal ["John Smith"], users
677 686 end
678 687
679 688 def test_index_at_project_level
680 689 get :index, :params => {:project_id => 'ecookbook', :c => ['project']}
681 690 assert_response :success
682 691
683 692 assert_select 'tr.time-entry', 4
684 693
685 694 # project and subproject
686 695 projects = css_select('table.time-entries tbody td.project').map(&:text).uniq.sort
687 696 assert_equal ["eCookbook", "eCookbook Subproject 1"], projects
688 697
689 698 assert_select '.total-for-hours', :text => 'Hours: 162.90'
690 699 assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
691 700 end
692 701
693 702 def test_index_with_display_subprojects_issues_to_false_should_not_include_subproject_entries
694 703 entry = TimeEntry.generate!(:project => Project.find(3))
695 704
696 705 with_settings :display_subprojects_issues => '0' do
697 706 get :index, :params => {:project_id => 'ecookbook', :c => ['project']}
698 707 assert_response :success
699 708
700 709 projects = css_select('table.time-entries tbody td.project').map(&:text).uniq.sort
701 710 assert_equal ["eCookbook"], projects
702 711 end
703 712 end
704 713
705 714 def test_index_with_display_subprojects_issues_to_false_and_subproject_filter_should_include_subproject_entries
706 715 entry = TimeEntry.generate!(:project => Project.find(3))
707 716
708 717 with_settings :display_subprojects_issues => '0' do
709 718 get :index, :params => {:project_id => 'ecookbook', :c => ['project'], :subproject_id => 3}
710 719 assert_response :success
711 720
712 721 projects = css_select('table.time-entries tbody td.project').map(&:text).uniq.sort
713 722 assert_equal ["eCookbook", "eCookbook Subproject 1"], projects
714 723 end
715 724 end
716 725
717 726 def test_index_at_project_level_with_issue_id_short_filter
718 727 issue = Issue.generate!(:project_id => 1)
719 728 TimeEntry.generate!(:issue => issue, :hours => 4)
720 729 TimeEntry.generate!(:issue => issue, :hours => 3)
721 730 @request.session[:user_id] = 2
722 731
723 732 get :index, :params => {:project_id => 'ecookbook', :issue_id => issue.id.to_s, :set_filter => 1}
724 733 assert_select '.total-for-hours', :text => 'Hours: 7.00'
725 734 end
726 735
727 736 def test_index_at_project_level_with_issue_fixed_version_id_short_filter
728 737 version = Version.generate!(:project_id => 1)
729 738 issue = Issue.generate!(:project_id => 1, :fixed_version => version)
730 739 TimeEntry.generate!(:issue => issue, :hours => 2)
731 740 TimeEntry.generate!(:issue => issue, :hours => 3)
732 741 @request.session[:user_id] = 2
733 742
734 743 get :index, :params => {:project_id => 'ecookbook', :"issue.fixed_version_id" => version.id.to_s, :set_filter => 1}
735 744 assert_select '.total-for-hours', :text => 'Hours: 5.00'
736 745 end
737 746
738 747 def test_index_at_project_level_with_date_range
739 748 get :index, :params => {
740 749 :project_id => 'ecookbook',
741 750 :f => ['spent_on'],
742 751 :op => {'spent_on' => '><'},
743 752 :v => {'spent_on' => ['2007-03-20', '2007-04-30']}
744 753 }
745 754 assert_response :success
746 755
747 756 assert_select 'tr.time-entry', 3
748 757 assert_select '.total-for-hours', :text => 'Hours: 12.90'
749 758 assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
750 759 end
751 760
752 761 def test_index_at_project_level_with_date_range_using_from_and_to_params
753 762 get :index, :params => {
754 763 :project_id => 'ecookbook',
755 764 :from => '2007-03-20',
756 765 :to => '2007-04-30'
757 766 }
758 767 assert_response :success
759 768
760 769 assert_select 'tr.time-entry', 3
761 770 assert_select '.total-for-hours', :text => 'Hours: 12.90'
762 771 assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
763 772 end
764 773
765 774 def test_index_at_project_level_with_period
766 775 get :index, :params => {
767 776 :project_id => 'ecookbook',
768 777 :f => ['spent_on'],
769 778 :op => {'spent_on' => '>t-'},
770 779 :v => {'spent_on' => ['7']}
771 780 }
772 781 assert_response :success
773 782
774 783 assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries'
775 784 end
776 785
777 786 def test_index_should_sort_by_spent_on_and_created_on
778 787 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)
779 788 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)
780 789 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)
781 790
782 791 get :index, :params => {
783 792 :project_id => 1,
784 793 :f => ['spent_on'],
785 794 :op => {'spent_on' => '><'},
786 795 :v => {'spent_on' => ['2012-06-15', '2012-06-16']}
787 796 }
788 797 assert_response :success
789 798 assert_equal [t2, t1, t3].map(&:id).map(&:to_s), css_select('input[name="ids[]"]').map {|e| e.attr('value')}
790 799
791 800 get :index, :params => {
792 801 :project_id => 1,
793 802 :f => ['spent_on'],
794 803 :op => {'spent_on' => '><'},
795 804 :v => {'spent_on' => ['2012-06-15', '2012-06-16']},
796 805 :sort => 'spent_on'
797 806 }
798 807 assert_response :success
799 808 assert_equal [t3, t1, t2].map(&:id).map(&:to_s), css_select('input[name="ids[]"]').map {|e| e.attr('value')}
800 809 end
801 810
802 811 def test_index_with_issue_status_filter
803 812 Issue.where(:status_id => 4).update_all(:status_id => 2)
804 813 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :status_id => 4)
805 814 entry = TimeEntry.generate!(:issue => issue, :hours => 4.5)
806 815
807 816 get :index, :params => {
808 817 :f => ['issue.status_id'],
809 818 :op => {'issue.status_id' => '='},
810 819 :v => {'issue.status_id' => ['4']}
811 820 }
812 821 assert_response :success
813 822 assert_equal [entry].map(&:id).map(&:to_s), css_select('input[name="ids[]"]').map {|e| e.attr('value')}
814 823 end
815 824
816 825 def test_index_with_issue_status_column
817 826 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :status_id => 4)
818 827 entry = TimeEntry.generate!(:issue => issue)
819 828
820 829 get :index, :params => {
821 830 :c => %w(project spent_on issue comments hours issue.status)
822 831 }
823 832 assert_response :success
824 833 assert_select 'td.issue-status', :text => issue.status.name
825 834 end
826 835
827 836 def test_index_with_issue_status_sort
828 837 TimeEntry.delete_all
829 838 TimeEntry.generate!(:issue => Issue.generate!(:project_id => 1, :tracker_id => 1, :status_id => 1))
830 839 TimeEntry.generate!(:issue => Issue.generate!(:project_id => 1, :tracker_id => 1, :status_id => 5))
831 840 TimeEntry.generate!(:issue => Issue.generate!(:project_id => 1, :tracker_id => 1, :status_id => 3))
832 841 TimeEntry.generate!(:project_id => 1)
833 842
834 843 get :index, :params => {
835 844 :c => ["hours", 'issue.status'],
836 845 :sort => 'issue.status'
837 846 }
838 847 assert_response :success
839 848
840 849 # Make sure that values are properly sorted
841 850 values = css_select("td.issue-status").map(&:text).reject(&:blank?)
842 851 assert_equal IssueStatus.where(:id => [1, 5, 3]).sorted.pluck(:name), values
843 852 end
844 853
845 854 def test_index_with_issue_tracker_filter
846 855 Issue.where(:tracker_id => 2).update_all(:tracker_id => 1)
847 856 issue = Issue.generate!(:project_id => 1, :tracker_id => 2)
848 857 entry = TimeEntry.generate!(:issue => issue, :hours => 4.5)
849 858
850 859 get :index, :params => {
851 860 :f => ['issue.tracker_id'],
852 861 :op => {'issue.tracker_id' => '='},
853 862 :v => {'issue.tracker_id' => ['2']}
854 863 }
855 864 assert_response :success
856 865 assert_equal [entry].map(&:id).map(&:to_s), css_select('input[name="ids[]"]').map {|e| e.attr('value')}
857 866 end
858 867
859 868 def test_index_with_issue_tracker_column
860 869 issue = Issue.generate!(:project_id => 1, :tracker_id => 2)
861 870 entry = TimeEntry.generate!(:issue => issue)
862 871
863 872 get :index, :params => {
864 873 :c => %w(project spent_on issue comments hours issue.tracker)
865 874 }
866 875 assert_response :success
867 876 assert_select 'td.issue-tracker', :text => issue.tracker.name
868 877 end
869 878
870 879 def test_index_with_issue_tracker_sort
871 880 TimeEntry.delete_all
872 881 TimeEntry.generate!(:issue => Issue.generate!(:tracker_id => 1))
873 882 TimeEntry.generate!(:issue => Issue.generate!(:tracker_id => 3))
874 883 TimeEntry.generate!(:issue => Issue.generate!(:tracker_id => 2))
875 884 TimeEntry.generate!(:project_id => 1)
876 885
877 886 get :index, :params => {
878 887 :c => ["hours", 'issue.tracker'],
879 888 :sort => 'issue.tracker'
880 889 }
881 890 assert_response :success
882 891
883 892 # Make sure that values are properly sorted
884 893 values = css_select("td.issue-tracker").map(&:text).reject(&:blank?)
885 894 assert_equal Tracker.where(:id => [1, 2, 3]).sorted.pluck(:name), values
886 895 end
887 896
888 897 def test_index_with_filter_on_issue_custom_field
889 898 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'})
890 899 entry = TimeEntry.generate!(:issue => issue, :hours => 2.5)
891 900
892 901 get :index, :params => {
893 902 :f => ['issue.cf_2'],
894 903 :op => {'issue.cf_2' => '='},
895 904 :v => {'issue.cf_2' => ['filter_on_issue_custom_field']}
896 905 }
897 906 assert_response :success
898 907 assert_equal [entry].map(&:id).map(&:to_s), css_select('input[name="ids[]"]').map {|e| e.attr('value')}
899 908 end
900 909
901 910 def test_index_with_issue_custom_field_column
902 911 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'})
903 912 entry = TimeEntry.generate!(:issue => issue, :hours => 2.5)
904 913
905 914 get :index, :params => {
906 915 :c => %w(project spent_on issue comments hours issue.cf_2)
907 916 }
908 917 assert_response :success
909 918 assert_select 'td.issue_cf_2', :text => 'filter_on_issue_custom_field'
910 919 end
911 920
912 921 def test_index_with_time_entry_custom_field_column
913 922 field = TimeEntryCustomField.generate!(:field_format => 'string')
914 923 entry = TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value'})
915 924 field_name = "cf_#{field.id}"
916 925
917 926 get :index, :params => {
918 927 :c => ["hours", field_name]
919 928 }
920 929 assert_response :success
921 930 assert_select "td.#{field_name}", :text => 'CF Value'
922 931 end
923 932
924 933 def test_index_with_time_entry_custom_field_sorting
925 934 field = TimeEntryCustomField.generate!(:field_format => 'string', :name => 'String Field')
926 935 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 1'})
927 936 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 3'})
928 937 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 2'})
929 938 field_name = "cf_#{field.id}"
930 939
931 940 get :index, :params => {
932 941 :c => ["hours", field_name],
933 942 :sort => field_name
934 943 }
935 944 assert_response :success
936 945 assert_select "th a.sort", :text => 'String Field'
937 946
938 947 # Make sure that values are properly sorted
939 948 values = css_select("td.#{field_name}").map(&:text).reject(&:blank?)
940 949 assert_equal values.sort, values
941 950 assert_equal 3, values.size
942 951 end
943 952
944 953 def test_index_with_query
945 954 query = TimeEntryQuery.new(:project_id => 1, :name => 'Time Entry Query', :visibility => 2)
946 955 query.save!
947 956 @request.session[:user_id] = 2
948 957
949 958 get :index, :params => {:project_id => 'ecookbook', :query_id => query.id}
950 959 assert_response :success
951 960 assert_select 'h2', :text => query.name
952 961 assert_select '#sidebar a.selected', :text => query.name
953 962 end
954 963
955 964 def test_index_atom_feed
956 965 get :index, :params => {:project_id => 1, :format => 'atom'}
957 966 assert_response :success
958 967 assert_equal 'application/atom+xml', @response.content_type
959 968 assert_select 'entry > title', :text => /7\.65 hours/
960 969 end
961 970
962 971 def test_index_at_project_level_should_include_csv_export_dialog
963 972 get :index, :params => {
964 973 :project_id => 'ecookbook',
965 974 :f => ['spent_on'],
966 975 :op => {'spent_on' => '>='},
967 976 :v => {'spent_on' => ['2007-04-01']},
968 977 :c => ['spent_on', 'user']
969 978 }
970 979 assert_response :success
971 980
972 981 assert_select '#csv-export-options' do
973 982 assert_select 'form[action=?][method=get]', '/projects/ecookbook/time_entries.csv' do
974 983 # filter
975 984 assert_select 'input[name=?][value=?]', 'f[]', 'spent_on'
976 985 assert_select 'input[name=?][value=?]', 'op[spent_on]', '>='
977 986 assert_select 'input[name=?][value=?]', 'v[spent_on][]', '2007-04-01'
978 987 # columns
979 988 assert_select 'input[name=?][value=?]', 'c[]', 'spent_on'
980 989 assert_select 'input[name=?][value=?]', 'c[]', 'user'
981 990 assert_select 'input[name=?]', 'c[]', 2
982 991 end
983 992 end
984 993 end
985 994
986 995 def test_index_cross_project_should_include_csv_export_dialog
987 996 get :index
988 997 assert_response :success
989 998
990 999 assert_select '#csv-export-options' do
991 1000 assert_select 'form[action=?][method=get]', '/time_entries.csv'
992 1001 end
993 1002 end
994 1003
995 1004 def test_index_csv_all_projects
996 1005 with_settings :date_format => '%m/%d/%Y' do
997 1006 get :index, :params => {:format => 'csv'}
998 1007 assert_response :success
999 1008 assert_equal 'text/csv; header=present', response.content_type
1000 1009 end
1001 1010 end
1002 1011
1003 1012 def test_index_csv
1004 1013 with_settings :date_format => '%m/%d/%Y' do
1005 1014 get :index, :params => {:project_id => 1, :format => 'csv'}
1006 1015 assert_response :success
1007 1016 assert_equal 'text/csv; header=present', response.content_type
1008 1017 end
1009 1018 end
1010 1019
1011 1020 def test_index_csv_should_fill_issue_column_with_tracker_id_and_subject
1012 1021 issue = Issue.find(1)
1013 1022 entry = TimeEntry.generate!(:issue => issue, :comments => "Issue column content test")
1014 1023
1015 1024 get :index, :params => {:format => 'csv'}
1016 1025 line = response.body.split("\n").detect {|l| l.include?(entry.comments)}
1017 1026 assert_not_nil line
1018 1027 assert_include "#{issue.tracker} #1: #{issue.subject}", line
1019 1028 end
1020 1029 end
General Comments 0
You need to be logged in to leave comments. Login now