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