##// END OF EJS Templates
Fixed that sorting time entries by custom field raises a SQL error (#14366)....
Jean-Philippe Lang -
r11812:21fc903c0424
parent child
Show More
@@ -1,315 +1,311
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 :issues
20 20
21 21 before_filter :find_project_for_new_time_entry, :only => [:create]
22 22 before_filter :find_time_entry, :only => [:show, :edit, :update]
23 23 before_filter :find_time_entries, :only => [:bulk_edit, :bulk_update, :destroy]
24 24 before_filter :authorize, :except => [:new, :index, :report]
25 25
26 26 before_filter :find_optional_project, :only => [:index, :report]
27 27 before_filter :find_optional_project_for_new_time_entry, :only => [:new]
28 28 before_filter :authorize_global, :only => [:new, :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 @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_')
46 scope = time_entry_scope
47 46
48 47 sort_init(@query.sort_criteria.empty? ? [['spent_on', 'desc']] : @query.sort_criteria)
49 48 sort_update(@query.sortable_columns)
49 scope = time_entry_scope(:order => sort_clause)
50 50
51 51 respond_to do |format|
52 52 format.html {
53 53 # Paginate results
54 54 @entry_count = scope.count
55 55 @entry_pages = Paginator.new @entry_count, per_page_option, params['page']
56 56 @entries = scope.all(
57 57 :include => [:project, :activity, :user, {:issue => :tracker}],
58 :order => sort_clause,
59 58 :limit => @entry_pages.per_page,
60 59 :offset => @entry_pages.offset
61 60 )
62 61 @total_hours = scope.sum(:hours).to_f
63 62
64 63 render :layout => !request.xhr?
65 64 }
66 65 format.api {
67 66 @entry_count = scope.count
68 67 @offset, @limit = api_offset_and_limit
69 68 @entries = scope.all(
70 69 :include => [:project, :activity, :user, {:issue => :tracker}],
71 :order => sort_clause,
72 70 :limit => @limit,
73 71 :offset => @offset
74 72 )
75 73 }
76 74 format.atom {
77 entries = scope.all(
75 entries = scope.reorder("#{TimeEntry.table_name}.created_on DESC").all(
78 76 :include => [:project, :activity, :user, {:issue => :tracker}],
79 :order => "#{TimeEntry.table_name}.created_on DESC",
80 77 :limit => Setting.feeds_limit.to_i
81 78 )
82 79 render_feed(entries, :title => l(:label_spent_time))
83 80 }
84 81 format.csv {
85 82 # Export all entries
86 83 @entries = scope.all(
87 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
88 :order => sort_clause
84 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}]
89 85 )
90 86 send_data(query_to_csv(@entries, @query, params), :type => 'text/csv; header=present', :filename => 'timelog.csv')
91 87 }
92 88 end
93 89 end
94 90
95 91 def report
96 92 @query = TimeEntryQuery.build_from_params(params, :project => @project, :name => '_')
97 93 scope = time_entry_scope
98 94
99 95 @report = Redmine::Helpers::TimeReport.new(@project, @issue, params[:criteria], params[:columns], scope)
100 96
101 97 respond_to do |format|
102 98 format.html { render :layout => !request.xhr? }
103 99 format.csv { send_data(report_to_csv(@report), :type => 'text/csv; header=present', :filename => 'timelog.csv') }
104 100 end
105 101 end
106 102
107 103 def show
108 104 respond_to do |format|
109 105 # TODO: Implement html response
110 106 format.html { render :nothing => true, :status => 406 }
111 107 format.api
112 108 end
113 109 end
114 110
115 111 def new
116 112 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
117 113 @time_entry.safe_attributes = params[:time_entry]
118 114 end
119 115
120 116 def create
121 117 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => User.current.today)
122 118 @time_entry.safe_attributes = params[:time_entry]
123 119
124 120 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
125 121
126 122 if @time_entry.save
127 123 respond_to do |format|
128 124 format.html {
129 125 flash[:notice] = l(:notice_successful_create)
130 126 if params[:continue]
131 127 if params[:project_id]
132 128 options = {
133 129 :time_entry => {:issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id},
134 130 :back_url => params[:back_url]
135 131 }
136 132 if @time_entry.issue
137 133 redirect_to new_project_issue_time_entry_path(@time_entry.project, @time_entry.issue, options)
138 134 else
139 135 redirect_to new_project_time_entry_path(@time_entry.project, options)
140 136 end
141 137 else
142 138 options = {
143 139 :time_entry => {:project_id => @time_entry.project_id, :issue_id => @time_entry.issue_id, :activity_id => @time_entry.activity_id},
144 140 :back_url => params[:back_url]
145 141 }
146 142 redirect_to new_time_entry_path(options)
147 143 end
148 144 else
149 145 redirect_back_or_default project_time_entries_path(@time_entry.project)
150 146 end
151 147 }
152 148 format.api { render :action => 'show', :status => :created, :location => time_entry_url(@time_entry) }
153 149 end
154 150 else
155 151 respond_to do |format|
156 152 format.html { render :action => 'new' }
157 153 format.api { render_validation_errors(@time_entry) }
158 154 end
159 155 end
160 156 end
161 157
162 158 def edit
163 159 @time_entry.safe_attributes = params[:time_entry]
164 160 end
165 161
166 162 def update
167 163 @time_entry.safe_attributes = params[:time_entry]
168 164
169 165 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
170 166
171 167 if @time_entry.save
172 168 respond_to do |format|
173 169 format.html {
174 170 flash[:notice] = l(:notice_successful_update)
175 171 redirect_back_or_default project_time_entries_path(@time_entry.project)
176 172 }
177 173 format.api { render_api_ok }
178 174 end
179 175 else
180 176 respond_to do |format|
181 177 format.html { render :action => 'edit' }
182 178 format.api { render_validation_errors(@time_entry) }
183 179 end
184 180 end
185 181 end
186 182
187 183 def bulk_edit
188 184 @available_activities = TimeEntryActivity.shared.active
189 185 @custom_fields = TimeEntry.first.available_custom_fields
190 186 end
191 187
192 188 def bulk_update
193 189 attributes = parse_params_for_bulk_time_entry_attributes(params)
194 190
195 191 unsaved_time_entry_ids = []
196 192 @time_entries.each do |time_entry|
197 193 time_entry.reload
198 194 time_entry.safe_attributes = attributes
199 195 call_hook(:controller_time_entries_bulk_edit_before_save, { :params => params, :time_entry => time_entry })
200 196 unless time_entry.save
201 197 logger.info "time entry could not be updated: #{time_entry.errors.full_messages}" if logger && logger.info
202 198 # Keep unsaved time_entry ids to display them in flash error
203 199 unsaved_time_entry_ids << time_entry.id
204 200 end
205 201 end
206 202 set_flash_from_bulk_time_entry_save(@time_entries, unsaved_time_entry_ids)
207 203 redirect_back_or_default project_time_entries_path(@projects.first)
208 204 end
209 205
210 206 def destroy
211 207 destroyed = TimeEntry.transaction do
212 208 @time_entries.each do |t|
213 209 unless t.destroy && t.destroyed?
214 210 raise ActiveRecord::Rollback
215 211 end
216 212 end
217 213 end
218 214
219 215 respond_to do |format|
220 216 format.html {
221 217 if destroyed
222 218 flash[:notice] = l(:notice_successful_delete)
223 219 else
224 220 flash[:error] = l(:notice_unable_delete_time_entry)
225 221 end
226 222 redirect_back_or_default project_time_entries_path(@projects.first)
227 223 }
228 224 format.api {
229 225 if destroyed
230 226 render_api_ok
231 227 else
232 228 render_validation_errors(@time_entries)
233 229 end
234 230 }
235 231 end
236 232 end
237 233
238 234 private
239 235 def find_time_entry
240 236 @time_entry = TimeEntry.find(params[:id])
241 237 unless @time_entry.editable_by?(User.current)
242 238 render_403
243 239 return false
244 240 end
245 241 @project = @time_entry.project
246 242 rescue ActiveRecord::RecordNotFound
247 243 render_404
248 244 end
249 245
250 246 def find_time_entries
251 247 @time_entries = TimeEntry.find_all_by_id(params[:id] || params[:ids])
252 248 raise ActiveRecord::RecordNotFound if @time_entries.empty?
253 249 @projects = @time_entries.collect(&:project).compact.uniq
254 250 @project = @projects.first if @projects.size == 1
255 251 rescue ActiveRecord::RecordNotFound
256 252 render_404
257 253 end
258 254
259 255 def set_flash_from_bulk_time_entry_save(time_entries, unsaved_time_entry_ids)
260 256 if unsaved_time_entry_ids.empty?
261 257 flash[:notice] = l(:notice_successful_update) unless time_entries.empty?
262 258 else
263 259 flash[:error] = l(:notice_failed_to_save_time_entries,
264 260 :count => unsaved_time_entry_ids.size,
265 261 :total => time_entries.size,
266 262 :ids => '#' + unsaved_time_entry_ids.join(', #'))
267 263 end
268 264 end
269 265
270 266 def find_optional_project_for_new_time_entry
271 267 if (project_id = (params[:project_id] || params[:time_entry] && params[:time_entry][:project_id])).present?
272 268 @project = Project.find(project_id)
273 269 end
274 270 if (issue_id = (params[:issue_id] || params[:time_entry] && params[:time_entry][:issue_id])).present?
275 271 @issue = Issue.find(issue_id)
276 272 @project ||= @issue.project
277 273 end
278 274 rescue ActiveRecord::RecordNotFound
279 275 render_404
280 276 end
281 277
282 278 def find_project_for_new_time_entry
283 279 find_optional_project_for_new_time_entry
284 280 if @project.nil?
285 281 render_404
286 282 end
287 283 end
288 284
289 285 def find_optional_project
290 286 if !params[:issue_id].blank?
291 287 @issue = Issue.find(params[:issue_id])
292 288 @project = @issue.project
293 289 elsif !params[:project_id].blank?
294 290 @project = Project.find(params[:project_id])
295 291 end
296 292 end
297 293
298 294 # Returns the TimeEntry scope for index and report actions
299 def time_entry_scope
300 scope = TimeEntry.visible.where(@query.statement)
295 def time_entry_scope(options={})
296 scope = @query.results_scope(options)
301 297 if @issue
302 298 scope = scope.on_issue(@issue)
303 299 elsif @project
304 300 scope = scope.on_project(@project, Setting.display_subprojects_issues?)
305 301 end
306 302 scope
307 303 end
308 304
309 305 def parse_params_for_bulk_time_entry_attributes(params)
310 306 attributes = (params[:time_entry] || {}).reject {|k,v| v.blank?}
311 307 attributes.keys.each {|k| attributes[k] = '' if attributes[k] == 'none'}
312 308 attributes[:custom_field_values].reject! {|k,v| v.blank?} if attributes[:custom_field_values]
313 309 attributes
314 310 end
315 311 end
@@ -1,115 +1,124
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 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 TimeEntryQuery < Query
19 19
20 20 self.queried_class = TimeEntry
21 21
22 22 self.available_columns = [
23 23 QueryColumn.new(:project, :sortable => "#{Project.table_name}.name", :groupable => true),
24 24 QueryColumn.new(:spent_on, :sortable => ["#{TimeEntry.table_name}.spent_on", "#{TimeEntry.table_name}.created_on"], :default_order => 'desc', :groupable => true),
25 25 QueryColumn.new(:user, :sortable => lambda {User.fields_for_order_statement}, :groupable => true),
26 26 QueryColumn.new(:activity, :sortable => "#{TimeEntryActivity.table_name}.position", :groupable => true),
27 27 QueryColumn.new(:issue, :sortable => "#{Issue.table_name}.id"),
28 28 QueryColumn.new(:comments),
29 29 QueryColumn.new(:hours, :sortable => "#{TimeEntry.table_name}.hours"),
30 30 ]
31 31
32 32 def initialize(attributes=nil, *args)
33 33 super attributes
34 34 self.filters ||= {}
35 35 add_filter('spent_on', '*') unless filters.present?
36 36 end
37 37
38 38 def initialize_available_filters
39 39 add_available_filter "spent_on", :type => :date_past
40 40
41 41 principals = []
42 42 if project
43 43 principals += project.principals.sort
44 44 unless project.leaf?
45 45 subprojects = project.descendants.visible.all
46 46 if subprojects.any?
47 47 add_available_filter "subproject_id",
48 48 :type => :list_subprojects,
49 49 :values => subprojects.collect{|s| [s.name, s.id.to_s] }
50 50 principals += Principal.member_of(subprojects)
51 51 end
52 52 end
53 53 else
54 54 if all_projects.any?
55 55 # members of visible projects
56 56 principals += Principal.member_of(all_projects)
57 57 # project filter
58 58 project_values = []
59 59 if User.current.logged? && User.current.memberships.any?
60 60 project_values << ["<< #{l(:label_my_projects).downcase} >>", "mine"]
61 61 end
62 62 project_values += all_projects_values
63 63 add_available_filter("project_id",
64 64 :type => :list, :values => project_values
65 65 ) unless project_values.empty?
66 66 end
67 67 end
68 68 principals.uniq!
69 69 principals.sort!
70 70 users = principals.select {|p| p.is_a?(User)}
71 71
72 72 users_values = []
73 73 users_values << ["<< #{l(:label_me)} >>", "me"] if User.current.logged?
74 74 users_values += users.collect{|s| [s.name, s.id.to_s] }
75 75 add_available_filter("user_id",
76 76 :type => :list_optional, :values => users_values
77 77 ) unless users_values.empty?
78 78
79 79 activities = (project ? project.activities : TimeEntryActivity.shared.active)
80 80 add_available_filter("activity_id",
81 81 :type => :list, :values => activities.map {|a| [a.name, a.id.to_s]}
82 82 ) unless activities.empty?
83 83
84 84 add_available_filter "comments", :type => :text
85 85 add_available_filter "hours", :type => :float
86 86
87 87 add_custom_fields_filters(TimeEntryCustomField)
88 88 add_associations_custom_fields_filters :project, :issue, :user
89 89 end
90 90
91 91 def available_columns
92 92 return @available_columns if @available_columns
93 93 @available_columns = self.class.available_columns.dup
94 94 @available_columns += TimeEntryCustomField.visible.all.map {|cf| QueryCustomFieldColumn.new(cf) }
95 95 @available_columns += IssueCustomField.visible.all.map {|cf| QueryAssociationCustomFieldColumn.new(:issue, cf) }
96 96 @available_columns
97 97 end
98 98
99 99 def default_columns_names
100 100 @default_columns_names ||= [:project, :spent_on, :user, :activity, :issue, :comments, :hours]
101 101 end
102 102
103 def results_scope(options={})
104 order_option = [group_by_sort_order, options[:order]].flatten.reject(&:blank?)
105
106 TimeEntry.visible.
107 where(statement).
108 order(order_option).
109 joins(joins_for_order_statement(order_option.join(',')))
110 end
111
103 112 # Accepts :from/:to params as shortcut filters
104 113 def build_from_params(params)
105 114 super
106 115 if params[:from].present? && params[:to].present?
107 116 add_filter('spent_on', '><', [params[:from], params[:to]])
108 117 elsif params[:from].present?
109 118 add_filter('spent_on', '>=', [params[:from]])
110 119 elsif params[:to].present?
111 120 add_filter('spent_on', '<=', [params[:to]])
112 121 end
113 122 self
114 123 end
115 124 end
@@ -1,623 +1,641
1 1 # -*- coding: utf-8 -*-
2 2 # Redmine - project management software
3 3 # Copyright (C) 2006-2013 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 < ActionController::TestCase
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_with_project_id
32 32 @request.session[:user_id] = 3
33 33 get :new, :project_id => 1
34 34 assert_response :success
35 35 assert_template 'new'
36 36 assert_select 'select[name=?]', 'time_entry[project_id]', 0
37 37 assert_select 'input[name=?][value=1][type=hidden]', 'time_entry[project_id]'
38 38 end
39 39
40 40 def test_new_with_issue_id
41 41 @request.session[:user_id] = 3
42 42 get :new, :issue_id => 2
43 43 assert_response :success
44 44 assert_template 'new'
45 45 assert_select 'select[name=?]', 'time_entry[project_id]', 0
46 46 assert_select 'input[name=?][value=1][type=hidden]', 'time_entry[project_id]'
47 47 end
48 48
49 49 def test_new_without_project
50 50 @request.session[:user_id] = 3
51 51 get :new
52 52 assert_response :success
53 53 assert_template 'new'
54 54 assert_select 'select[name=?]', 'time_entry[project_id]'
55 55 assert_select 'input[name=?]', 'time_entry[project_id]', 0
56 56 end
57 57
58 58 def test_new_without_project_should_prefill_the_form
59 59 @request.session[:user_id] = 3
60 60 get :new, :time_entry => {:project_id => '1'}
61 61 assert_response :success
62 62 assert_template 'new'
63 63 assert_select 'select[name=?]', 'time_entry[project_id]' do
64 64 assert_select 'option[value=1][selected=selected]'
65 65 end
66 66 assert_select 'input[name=?]', 'time_entry[project_id]', 0
67 67 end
68 68
69 69 def test_new_without_project_should_deny_without_permission
70 70 Role.all.each {|role| role.remove_permission! :log_time}
71 71 @request.session[:user_id] = 3
72 72
73 73 get :new
74 74 assert_response 403
75 75 end
76 76
77 77 def test_new_should_select_default_activity
78 78 @request.session[:user_id] = 3
79 79 get :new, :project_id => 1
80 80 assert_response :success
81 81 assert_select 'select[name=?]', 'time_entry[activity_id]' do
82 82 assert_select 'option[selected=selected]', :text => 'Development'
83 83 end
84 84 end
85 85
86 86 def test_new_should_only_show_active_time_entry_activities
87 87 @request.session[:user_id] = 3
88 88 get :new, :project_id => 1
89 89 assert_response :success
90 90 assert_no_tag 'option', :content => 'Inactive Activity'
91 91 end
92 92
93 93 def test_get_edit_existing_time
94 94 @request.session[:user_id] = 2
95 95 get :edit, :id => 2, :project_id => nil
96 96 assert_response :success
97 97 assert_template 'edit'
98 98 # Default activity selected
99 99 assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/time_entries/2' }
100 100 end
101 101
102 102 def test_get_edit_with_an_existing_time_entry_with_inactive_activity
103 103 te = TimeEntry.find(1)
104 104 te.activity = TimeEntryActivity.find_by_name("Inactive Activity")
105 105 te.save!
106 106
107 107 @request.session[:user_id] = 1
108 108 get :edit, :project_id => 1, :id => 1
109 109 assert_response :success
110 110 assert_template 'edit'
111 111 # Blank option since nothing is pre-selected
112 112 assert_tag :tag => 'option', :content => '--- Please select ---'
113 113 end
114 114
115 115 def test_post_create
116 116 # TODO: should POST to issues’ time log instead of project. change form
117 117 # and routing
118 118 @request.session[:user_id] = 3
119 119 post :create, :project_id => 1,
120 120 :time_entry => {:comments => 'Some work on TimelogControllerTest',
121 121 # Not the default activity
122 122 :activity_id => '11',
123 123 :spent_on => '2008-03-14',
124 124 :issue_id => '1',
125 125 :hours => '7.3'}
126 126 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
127 127
128 128 i = Issue.find(1)
129 129 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
130 130 assert_not_nil t
131 131 assert_equal 11, t.activity_id
132 132 assert_equal 7.3, t.hours
133 133 assert_equal 3, t.user_id
134 134 assert_equal i, t.issue
135 135 assert_equal i.project, t.project
136 136 end
137 137
138 138 def test_post_create_with_blank_issue
139 139 # TODO: should POST to issues’ time log instead of project. change form
140 140 # and routing
141 141 @request.session[:user_id] = 3
142 142 post :create, :project_id => 1,
143 143 :time_entry => {:comments => 'Some work on TimelogControllerTest',
144 144 # Not the default activity
145 145 :activity_id => '11',
146 146 :issue_id => '',
147 147 :spent_on => '2008-03-14',
148 148 :hours => '7.3'}
149 149 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
150 150
151 151 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
152 152 assert_not_nil t
153 153 assert_equal 11, t.activity_id
154 154 assert_equal 7.3, t.hours
155 155 assert_equal 3, t.user_id
156 156 end
157 157
158 158 def test_create_and_continue
159 159 @request.session[:user_id] = 2
160 160 post :create, :project_id => 1,
161 161 :time_entry => {:activity_id => '11',
162 162 :issue_id => '',
163 163 :spent_on => '2008-03-14',
164 164 :hours => '7.3'},
165 165 :continue => '1'
166 166 assert_redirected_to '/projects/ecookbook/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D='
167 167 end
168 168
169 169 def test_create_and_continue_with_issue_id
170 170 @request.session[:user_id] = 2
171 171 post :create, :project_id => 1,
172 172 :time_entry => {:activity_id => '11',
173 173 :issue_id => '1',
174 174 :spent_on => '2008-03-14',
175 175 :hours => '7.3'},
176 176 :continue => '1'
177 177 assert_redirected_to '/projects/ecookbook/issues/1/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=1'
178 178 end
179 179
180 180 def test_create_and_continue_without_project
181 181 @request.session[:user_id] = 2
182 182 post :create, :time_entry => {:project_id => '1',
183 183 :activity_id => '11',
184 184 :issue_id => '',
185 185 :spent_on => '2008-03-14',
186 186 :hours => '7.3'},
187 187 :continue => '1'
188 188
189 189 assert_redirected_to '/time_entries/new?time_entry%5Bactivity_id%5D=11&time_entry%5Bissue_id%5D=&time_entry%5Bproject_id%5D=1'
190 190 end
191 191
192 192 def test_create_without_log_time_permission_should_be_denied
193 193 @request.session[:user_id] = 2
194 194 Role.find_by_name('Manager').remove_permission! :log_time
195 195 post :create, :project_id => 1,
196 196 :time_entry => {:activity_id => '11',
197 197 :issue_id => '',
198 198 :spent_on => '2008-03-14',
199 199 :hours => '7.3'}
200 200
201 201 assert_response 403
202 202 end
203 203
204 204 def test_create_with_failure
205 205 @request.session[:user_id] = 2
206 206 post :create, :project_id => 1,
207 207 :time_entry => {:activity_id => '',
208 208 :issue_id => '',
209 209 :spent_on => '2008-03-14',
210 210 :hours => '7.3'}
211 211
212 212 assert_response :success
213 213 assert_template 'new'
214 214 end
215 215
216 216 def test_create_without_project
217 217 @request.session[:user_id] = 2
218 218 assert_difference 'TimeEntry.count' do
219 219 post :create, :time_entry => {:project_id => '1',
220 220 :activity_id => '11',
221 221 :issue_id => '',
222 222 :spent_on => '2008-03-14',
223 223 :hours => '7.3'}
224 224 end
225 225
226 226 assert_redirected_to '/projects/ecookbook/time_entries'
227 227 time_entry = TimeEntry.first(:order => 'id DESC')
228 228 assert_equal 1, time_entry.project_id
229 229 end
230 230
231 231 def test_create_without_project_should_fail_with_issue_not_inside_project
232 232 @request.session[:user_id] = 2
233 233 assert_no_difference 'TimeEntry.count' do
234 234 post :create, :time_entry => {:project_id => '1',
235 235 :activity_id => '11',
236 236 :issue_id => '5',
237 237 :spent_on => '2008-03-14',
238 238 :hours => '7.3'}
239 239 end
240 240
241 241 assert_response :success
242 242 assert assigns(:time_entry).errors[:issue_id].present?
243 243 end
244 244
245 245 def test_create_without_project_should_deny_without_permission
246 246 @request.session[:user_id] = 2
247 247 Project.find(3).disable_module!(:time_tracking)
248 248
249 249 assert_no_difference 'TimeEntry.count' do
250 250 post :create, :time_entry => {:project_id => '3',
251 251 :activity_id => '11',
252 252 :issue_id => '',
253 253 :spent_on => '2008-03-14',
254 254 :hours => '7.3'}
255 255 end
256 256
257 257 assert_response 403
258 258 end
259 259
260 260 def test_create_without_project_with_failure
261 261 @request.session[:user_id] = 2
262 262 assert_no_difference 'TimeEntry.count' do
263 263 post :create, :time_entry => {:project_id => '1',
264 264 :activity_id => '11',
265 265 :issue_id => '',
266 266 :spent_on => '2008-03-14',
267 267 :hours => ''}
268 268 end
269 269
270 270 assert_response :success
271 271 assert_tag 'select', :attributes => {:name => 'time_entry[project_id]'},
272 272 :child => {:tag => 'option', :attributes => {:value => '1', :selected => 'selected'}}
273 273 end
274 274
275 275 def test_update
276 276 entry = TimeEntry.find(1)
277 277 assert_equal 1, entry.issue_id
278 278 assert_equal 2, entry.user_id
279 279
280 280 @request.session[:user_id] = 1
281 281 put :update, :id => 1,
282 282 :time_entry => {:issue_id => '2',
283 283 :hours => '8'}
284 284 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
285 285 entry.reload
286 286
287 287 assert_equal 8, entry.hours
288 288 assert_equal 2, entry.issue_id
289 289 assert_equal 2, entry.user_id
290 290 end
291 291
292 292 def test_get_bulk_edit
293 293 @request.session[:user_id] = 2
294 294 get :bulk_edit, :ids => [1, 2]
295 295 assert_response :success
296 296 assert_template 'bulk_edit'
297 297
298 298 assert_select 'ul#bulk-selection' do
299 299 assert_select 'li', 2
300 300 assert_select 'li a', :text => '03/23/2007 - eCookbook: 4.25 hours'
301 301 end
302 302
303 303 assert_select 'form#bulk_edit_form[action=?]', '/time_entries/bulk_update' do
304 304 # System wide custom field
305 305 assert_select 'select[name=?]', 'time_entry[custom_field_values][10]'
306 306
307 307 # Activities
308 308 assert_select 'select[name=?]', 'time_entry[activity_id]' do
309 309 assert_select 'option[value=]', :text => '(No change)'
310 310 assert_select 'option[value=9]', :text => 'Design'
311 311 end
312 312 end
313 313 end
314 314
315 315 def test_get_bulk_edit_on_different_projects
316 316 @request.session[:user_id] = 2
317 317 get :bulk_edit, :ids => [1, 2, 6]
318 318 assert_response :success
319 319 assert_template 'bulk_edit'
320 320 end
321 321
322 322 def test_bulk_update
323 323 @request.session[:user_id] = 2
324 324 # update time entry activity
325 325 post :bulk_update, :ids => [1, 2], :time_entry => { :activity_id => 9}
326 326
327 327 assert_response 302
328 328 # check that the issues were updated
329 329 assert_equal [9, 9], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.activity_id}
330 330 end
331 331
332 332 def test_bulk_update_with_failure
333 333 @request.session[:user_id] = 2
334 334 post :bulk_update, :ids => [1, 2], :time_entry => { :hours => 'A'}
335 335
336 336 assert_response 302
337 337 assert_match /Failed to save 2 time entrie/, flash[:error]
338 338 end
339 339
340 340 def test_bulk_update_on_different_projects
341 341 @request.session[:user_id] = 2
342 342 # makes user a manager on the other project
343 343 Member.create!(:user_id => 2, :project_id => 3, :role_ids => [1])
344 344
345 345 # update time entry activity
346 346 post :bulk_update, :ids => [1, 2, 4], :time_entry => { :activity_id => 9 }
347 347
348 348 assert_response 302
349 349 # check that the issues were updated
350 350 assert_equal [9, 9, 9], TimeEntry.find_all_by_id([1, 2, 4]).collect {|i| i.activity_id}
351 351 end
352 352
353 353 def test_bulk_update_on_different_projects_without_rights
354 354 @request.session[:user_id] = 3
355 355 user = User.find(3)
356 356 action = { :controller => "timelog", :action => "bulk_update" }
357 357 assert user.allowed_to?(action, TimeEntry.find(1).project)
358 358 assert ! user.allowed_to?(action, TimeEntry.find(5).project)
359 359 post :bulk_update, :ids => [1, 5], :time_entry => { :activity_id => 9 }
360 360 assert_response 403
361 361 end
362 362
363 363 def test_bulk_update_custom_field
364 364 @request.session[:user_id] = 2
365 365 post :bulk_update, :ids => [1, 2], :time_entry => { :custom_field_values => {'10' => '0'} }
366 366
367 367 assert_response 302
368 368 assert_equal ["0", "0"], TimeEntry.find_all_by_id([1, 2]).collect {|i| i.custom_value_for(10).value}
369 369 end
370 370
371 371 def test_post_bulk_update_should_redirect_back_using_the_back_url_parameter
372 372 @request.session[:user_id] = 2
373 373 post :bulk_update, :ids => [1,2], :back_url => '/time_entries'
374 374
375 375 assert_response :redirect
376 376 assert_redirected_to '/time_entries'
377 377 end
378 378
379 379 def test_post_bulk_update_should_not_redirect_back_using_the_back_url_parameter_off_the_host
380 380 @request.session[:user_id] = 2
381 381 post :bulk_update, :ids => [1,2], :back_url => 'http://google.com'
382 382
383 383 assert_response :redirect
384 384 assert_redirected_to :controller => 'timelog', :action => 'index', :project_id => Project.find(1).identifier
385 385 end
386 386
387 387 def test_post_bulk_update_without_edit_permission_should_be_denied
388 388 @request.session[:user_id] = 2
389 389 Role.find_by_name('Manager').remove_permission! :edit_time_entries
390 390 post :bulk_update, :ids => [1,2]
391 391
392 392 assert_response 403
393 393 end
394 394
395 395 def test_destroy
396 396 @request.session[:user_id] = 2
397 397 delete :destroy, :id => 1
398 398 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
399 399 assert_equal I18n.t(:notice_successful_delete), flash[:notice]
400 400 assert_nil TimeEntry.find_by_id(1)
401 401 end
402 402
403 403 def test_destroy_should_fail
404 404 # simulate that this fails (e.g. due to a plugin), see #5700
405 405 TimeEntry.any_instance.expects(:destroy).returns(false)
406 406
407 407 @request.session[:user_id] = 2
408 408 delete :destroy, :id => 1
409 409 assert_redirected_to :action => 'index', :project_id => 'ecookbook'
410 410 assert_equal I18n.t(:notice_unable_delete_time_entry), flash[:error]
411 411 assert_not_nil TimeEntry.find_by_id(1)
412 412 end
413 413
414 414 def test_index_all_projects
415 415 get :index
416 416 assert_response :success
417 417 assert_template 'index'
418 418 assert_not_nil assigns(:total_hours)
419 419 assert_equal "162.90", "%.2f" % assigns(:total_hours)
420 420 assert_tag :form,
421 421 :attributes => {:action => "/time_entries", :id => 'query_form'}
422 422 end
423 423
424 424 def test_index_all_projects_should_show_log_time_link
425 425 @request.session[:user_id] = 2
426 426 get :index
427 427 assert_response :success
428 428 assert_template 'index'
429 429 assert_tag 'a', :attributes => {:href => '/time_entries/new'}, :content => /Log time/
430 430 end
431 431
432 432 def test_index_my_spent_time
433 433 @request.session[:user_id] = 2
434 434 get :index, :user_id => 'me'
435 435 assert_response :success
436 436 assert_template 'index'
437 437 assert assigns(:entries).all? {|entry| entry.user_id == 2}
438 438 end
439 439
440 440 def test_index_at_project_level
441 441 get :index, :project_id => 'ecookbook'
442 442 assert_response :success
443 443 assert_template 'index'
444 444 assert_not_nil assigns(:entries)
445 445 assert_equal 4, assigns(:entries).size
446 446 # project and subproject
447 447 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
448 448 assert_not_nil assigns(:total_hours)
449 449 assert_equal "162.90", "%.2f" % assigns(:total_hours)
450 450 assert_tag :form,
451 451 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
452 452 end
453 453
454 454 def test_index_at_project_level_with_date_range
455 455 get :index, :project_id => 'ecookbook',
456 456 :f => ['spent_on'],
457 457 :op => {'spent_on' => '><'},
458 458 :v => {'spent_on' => ['2007-03-20', '2007-04-30']}
459 459 assert_response :success
460 460 assert_template 'index'
461 461 assert_not_nil assigns(:entries)
462 462 assert_equal 3, assigns(:entries).size
463 463 assert_not_nil assigns(:total_hours)
464 464 assert_equal "12.90", "%.2f" % assigns(:total_hours)
465 465 assert_tag :form,
466 466 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
467 467 end
468 468
469 469 def test_index_at_project_level_with_date_range_using_from_and_to_params
470 470 get :index, :project_id => 'ecookbook', :from => '2007-03-20', :to => '2007-04-30'
471 471 assert_response :success
472 472 assert_template 'index'
473 473 assert_not_nil assigns(:entries)
474 474 assert_equal 3, assigns(:entries).size
475 475 assert_not_nil assigns(:total_hours)
476 476 assert_equal "12.90", "%.2f" % assigns(:total_hours)
477 477 assert_tag :form,
478 478 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
479 479 end
480 480
481 481 def test_index_at_project_level_with_period
482 482 get :index, :project_id => 'ecookbook',
483 483 :f => ['spent_on'],
484 484 :op => {'spent_on' => '>t-'},
485 485 :v => {'spent_on' => ['7']}
486 486 assert_response :success
487 487 assert_template 'index'
488 488 assert_not_nil assigns(:entries)
489 489 assert_not_nil assigns(:total_hours)
490 490 assert_tag :form,
491 491 :attributes => {:action => "/projects/ecookbook/time_entries", :id => 'query_form'}
492 492 end
493 493
494 494 def test_index_at_issue_level
495 495 get :index, :issue_id => 1
496 496 assert_response :success
497 497 assert_template 'index'
498 498 assert_not_nil assigns(:entries)
499 499 assert_equal 2, assigns(:entries).size
500 500 assert_not_nil assigns(:total_hours)
501 501 assert_equal 154.25, assigns(:total_hours)
502 502 # display all time
503 503 assert_nil assigns(:from)
504 504 assert_nil assigns(:to)
505 505 # TODO: remove /projects/:project_id/issues/:issue_id/time_entries routes
506 506 # to use /issues/:issue_id/time_entries
507 507 assert_tag :form,
508 508 :attributes => {:action => "/projects/ecookbook/issues/1/time_entries", :id => 'query_form'}
509 509 end
510 510
511 511 def test_index_should_sort_by_spent_on_and_created_on
512 512 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)
513 513 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)
514 514 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)
515 515
516 516 get :index, :project_id => 1,
517 517 :f => ['spent_on'],
518 518 :op => {'spent_on' => '><'},
519 519 :v => {'spent_on' => ['2012-06-15', '2012-06-16']}
520 520 assert_response :success
521 521 assert_equal [t2, t1, t3], assigns(:entries)
522 522
523 523 get :index, :project_id => 1,
524 524 :f => ['spent_on'],
525 525 :op => {'spent_on' => '><'},
526 526 :v => {'spent_on' => ['2012-06-15', '2012-06-16']},
527 527 :sort => 'spent_on'
528 528 assert_response :success
529 529 assert_equal [t3, t1, t2], assigns(:entries)
530 530 end
531 531
532 532 def test_index_with_filter_on_issue_custom_field
533 533 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'})
534 534 entry = TimeEntry.generate!(:issue => issue, :hours => 2.5)
535 535
536 536 get :index, :f => ['issue.cf_2'], :op => {'issue.cf_2' => '='}, :v => {'issue.cf_2' => ['filter_on_issue_custom_field']}
537 537 assert_response :success
538 538 assert_equal [entry], assigns(:entries)
539 539 end
540 540
541 541 def test_index_with_issue_custom_field_column
542 542 issue = Issue.generate!(:project_id => 1, :tracker_id => 1, :custom_field_values => {2 => 'filter_on_issue_custom_field'})
543 543 entry = TimeEntry.generate!(:issue => issue, :hours => 2.5)
544 544
545 545 get :index, :c => %w(project spent_on issue comments hours issue.cf_2)
546 546 assert_response :success
547 547 assert_include :'issue.cf_2', assigns(:query).column_names
548 548 assert_select 'td.issue_cf_2', :text => 'filter_on_issue_custom_field'
549 549 end
550 550
551 551 def test_index_with_time_entry_custom_field_column
552 552 field = TimeEntryCustomField.generate!(:field_format => 'string')
553 553 entry = TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value'})
554 554 field_name = "cf_#{field.id}"
555 555
556 556 get :index, :c => ["hours", field_name]
557 557 assert_response :success
558 558 assert_include field_name.to_sym, assigns(:query).column_names
559 559 assert_select "td.#{field_name}", :text => 'CF Value'
560 560 end
561 561
562 def test_index_with_time_entry_custom_field_sorting
563 field = TimeEntryCustomField.generate!(:field_format => 'string', :name => 'String Field')
564 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 1'})
565 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 3'})
566 TimeEntry.generate!(:hours => 2.5, :custom_field_values => {field.id => 'CF Value 2'})
567 field_name = "cf_#{field.id}"
568
569 get :index, :c => ["hours", field_name], :sort => field_name
570 assert_response :success
571 assert_include field_name.to_sym, assigns(:query).column_names
572 assert_select "th a.sort", :text => 'String Field'
573
574 # Make sure that values are properly sorted
575 values = assigns(:entries).map {|e| e.custom_field_value(field)}.compact
576 assert_equal 3, values.size
577 assert_equal values.sort, values
578 end
579
562 580 def test_index_atom_feed
563 581 get :index, :project_id => 1, :format => 'atom'
564 582 assert_response :success
565 583 assert_equal 'application/atom+xml', @response.content_type
566 584 assert_not_nil assigns(:items)
567 585 assert assigns(:items).first.is_a?(TimeEntry)
568 586 end
569 587
570 588 def test_index_at_project_level_should_include_csv_export_dialog
571 589 get :index, :project_id => 'ecookbook',
572 590 :f => ['spent_on'],
573 591 :op => {'spent_on' => '>='},
574 592 :v => {'spent_on' => ['2007-04-01']},
575 593 :c => ['spent_on', 'user']
576 594 assert_response :success
577 595
578 596 assert_select '#csv-export-options' do
579 597 assert_select 'form[action=?][method=get]', '/projects/ecookbook/time_entries.csv' do
580 598 # filter
581 599 assert_select 'input[name=?][value=?]', 'f[]', 'spent_on'
582 600 assert_select 'input[name=?][value=?]', 'op[spent_on]', '&gt;='
583 601 assert_select 'input[name=?][value=?]', 'v[spent_on][]', '2007-04-01'
584 602 # columns
585 603 assert_select 'input[name=?][value=?]', 'c[]', 'spent_on'
586 604 assert_select 'input[name=?][value=?]', 'c[]', 'user'
587 605 assert_select 'input[name=?]', 'c[]', 2
588 606 end
589 607 end
590 608 end
591 609
592 610 def test_index_cross_project_should_include_csv_export_dialog
593 611 get :index
594 612 assert_response :success
595 613
596 614 assert_select '#csv-export-options' do
597 615 assert_select 'form[action=?][method=get]', '/time_entries.csv'
598 616 end
599 617 end
600 618
601 619 def test_index_at_issue_level_should_include_csv_export_dialog
602 620 get :index, :project_id => 'ecookbook', :issue_id => 3
603 621 assert_response :success
604 622
605 623 assert_select '#csv-export-options' do
606 624 assert_select 'form[action=?][method=get]', '/projects/ecookbook/issues/3/time_entries.csv'
607 625 end
608 626 end
609 627
610 628 def test_index_csv_all_projects
611 629 Setting.date_format = '%m/%d/%Y'
612 630 get :index, :format => 'csv'
613 631 assert_response :success
614 632 assert_equal 'text/csv; header=present', response.content_type
615 633 end
616 634
617 635 def test_index_csv
618 636 Setting.date_format = '%m/%d/%Y'
619 637 get :index, :project_id => 1, :format => 'csv'
620 638 assert_response :success
621 639 assert_equal 'text/csv; header=present', response.content_type
622 640 end
623 641 end
General Comments 0
You need to be logged in to leave comments. Login now