##// END OF EJS Templates
Custom fields (list and boolean) can be used as criteria in time report (#1012)....
Jean-Philippe Lang -
r1325:a3c89d4f697e
parent child
Show More
@@ -1,245 +1,254
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 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 layout 'base'
19 layout 'base'
20 menu_item :issues
20 menu_item :issues
21 before_filter :find_project, :authorize
21 before_filter :find_project, :authorize
22
22
23 verify :method => :post, :only => :destroy, :redirect_to => { :action => :details }
23 verify :method => :post, :only => :destroy, :redirect_to => { :action => :details }
24
24
25 helper :sort
25 helper :sort
26 include SortHelper
26 include SortHelper
27 helper :issues
27 helper :issues
28 include TimelogHelper
28 include TimelogHelper
29 helper :custom_fields
30 include CustomFieldsHelper
29
31
30 def report
32 def report
31 @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
33 @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
32 :klass => Project,
34 :klass => Project,
33 :label => :label_project},
35 :label => :label_project},
34 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
36 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
35 :klass => Version,
37 :klass => Version,
36 :label => :label_version},
38 :label => :label_version},
37 'category' => {:sql => "#{Issue.table_name}.category_id",
39 'category' => {:sql => "#{Issue.table_name}.category_id",
38 :klass => IssueCategory,
40 :klass => IssueCategory,
39 :label => :field_category},
41 :label => :field_category},
40 'member' => {:sql => "#{TimeEntry.table_name}.user_id",
42 'member' => {:sql => "#{TimeEntry.table_name}.user_id",
41 :klass => User,
43 :klass => User,
42 :label => :label_member},
44 :label => :label_member},
43 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
45 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
44 :klass => Tracker,
46 :klass => Tracker,
45 :label => :label_tracker},
47 :label => :label_tracker},
46 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
48 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
47 :klass => Enumeration,
49 :klass => Enumeration,
48 :label => :label_activity},
50 :label => :label_activity},
49 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
51 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
50 :klass => Issue,
52 :klass => Issue,
51 :label => :label_issue}
53 :label => :label_issue}
52 }
54 }
53
55
56 # Add list and boolean custom fields as available criterias
57 @project.all_custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
58 @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM custom_values c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = issues.id)",
59 :format => cf.field_format,
60 :label => cf.name}
61 end
62
54 @criterias = params[:criterias] || []
63 @criterias = params[:criterias] || []
55 @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
64 @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
56 @criterias.uniq!
65 @criterias.uniq!
57 @criterias = @criterias[0,3]
66 @criterias = @criterias[0,3]
58
67
59 @columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month'
68 @columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month'
60
69
61 retrieve_date_range
70 retrieve_date_range
62
71
63 unless @criterias.empty?
72 unless @criterias.empty?
64 sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
73 sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
65 sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
74 sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
66
75
67 sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours"
76 sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours"
68 sql << " FROM #{TimeEntry.table_name}"
77 sql << " FROM #{TimeEntry.table_name}"
69 sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
78 sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
70 sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
79 sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
71 sql << " WHERE (%s)" % @project.project_condition(Setting.display_subprojects_issues?)
80 sql << " WHERE (%s)" % @project.project_condition(Setting.display_subprojects_issues?)
72 sql << " AND (%s)" % Project.allowed_to_condition(User.current, :view_time_entries)
81 sql << " AND (%s)" % Project.allowed_to_condition(User.current, :view_time_entries)
73 sql << " AND spent_on BETWEEN '%s' AND '%s'" % [ActiveRecord::Base.connection.quoted_date(@from.to_time), ActiveRecord::Base.connection.quoted_date(@to.to_time)]
82 sql << " AND spent_on BETWEEN '%s' AND '%s'" % [ActiveRecord::Base.connection.quoted_date(@from.to_time), ActiveRecord::Base.connection.quoted_date(@to.to_time)]
74 sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on"
83 sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on"
75
84
76 @hours = ActiveRecord::Base.connection.select_all(sql)
85 @hours = ActiveRecord::Base.connection.select_all(sql)
77
86
78 @hours.each do |row|
87 @hours.each do |row|
79 case @columns
88 case @columns
80 when 'year'
89 when 'year'
81 row['year'] = row['tyear']
90 row['year'] = row['tyear']
82 when 'month'
91 when 'month'
83 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
92 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
84 when 'week'
93 when 'week'
85 row['week'] = "#{row['tyear']}-#{row['tweek']}"
94 row['week'] = "#{row['tyear']}-#{row['tweek']}"
86 when 'day'
95 when 'day'
87 row['day'] = "#{row['spent_on']}"
96 row['day'] = "#{row['spent_on']}"
88 end
97 end
89 end
98 end
90
99
91 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
100 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
92
101
93 @periods = []
102 @periods = []
94 # Date#at_beginning_of_ not supported in Rails 1.2.x
103 # Date#at_beginning_of_ not supported in Rails 1.2.x
95 date_from = @from.to_time
104 date_from = @from.to_time
96 # 100 columns max
105 # 100 columns max
97 while date_from <= @to.to_time && @periods.length < 100
106 while date_from <= @to.to_time && @periods.length < 100
98 case @columns
107 case @columns
99 when 'year'
108 when 'year'
100 @periods << "#{date_from.year}"
109 @periods << "#{date_from.year}"
101 date_from = (date_from + 1.year).at_beginning_of_year
110 date_from = (date_from + 1.year).at_beginning_of_year
102 when 'month'
111 when 'month'
103 @periods << "#{date_from.year}-#{date_from.month}"
112 @periods << "#{date_from.year}-#{date_from.month}"
104 date_from = (date_from + 1.month).at_beginning_of_month
113 date_from = (date_from + 1.month).at_beginning_of_month
105 when 'week'
114 when 'week'
106 @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
115 @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
107 date_from = (date_from + 7.day).at_beginning_of_week
116 date_from = (date_from + 7.day).at_beginning_of_week
108 when 'day'
117 when 'day'
109 @periods << "#{date_from.to_date}"
118 @periods << "#{date_from.to_date}"
110 date_from = date_from + 1.day
119 date_from = date_from + 1.day
111 end
120 end
112 end
121 end
113 end
122 end
114
123
115 respond_to do |format|
124 respond_to do |format|
116 format.html { render :layout => !request.xhr? }
125 format.html { render :layout => !request.xhr? }
117 format.csv { send_data(report_to_csv(@criterias, @periods, @hours).read, :type => 'text/csv; header=present', :filename => 'timelog.csv') }
126 format.csv { send_data(report_to_csv(@criterias, @periods, @hours).read, :type => 'text/csv; header=present', :filename => 'timelog.csv') }
118 end
127 end
119 end
128 end
120
129
121 def details
130 def details
122 sort_init 'spent_on', 'desc'
131 sort_init 'spent_on', 'desc'
123 sort_update
132 sort_update
124
133
125 cond = ARCondition.new
134 cond = ARCondition.new
126 cond << (@issue.nil? ? @project.project_condition(Setting.display_subprojects_issues?) :
135 cond << (@issue.nil? ? @project.project_condition(Setting.display_subprojects_issues?) :
127 ["#{TimeEntry.table_name}.issue_id = ?", @issue.id])
136 ["#{TimeEntry.table_name}.issue_id = ?", @issue.id])
128
137
129 retrieve_date_range
138 retrieve_date_range
130 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
139 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
131
140
132 TimeEntry.visible_by(User.current) do
141 TimeEntry.visible_by(User.current) do
133 respond_to do |format|
142 respond_to do |format|
134 format.html {
143 format.html {
135 # Paginate results
144 # Paginate results
136 @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions)
145 @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions)
137 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
146 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
138 @entries = TimeEntry.find(:all,
147 @entries = TimeEntry.find(:all,
139 :include => [:project, :activity, :user, {:issue => :tracker}],
148 :include => [:project, :activity, :user, {:issue => :tracker}],
140 :conditions => cond.conditions,
149 :conditions => cond.conditions,
141 :order => sort_clause,
150 :order => sort_clause,
142 :limit => @entry_pages.items_per_page,
151 :limit => @entry_pages.items_per_page,
143 :offset => @entry_pages.current.offset)
152 :offset => @entry_pages.current.offset)
144 @total_hours = TimeEntry.sum(:hours, :include => :project, :conditions => cond.conditions).to_f
153 @total_hours = TimeEntry.sum(:hours, :include => :project, :conditions => cond.conditions).to_f
145
154
146 render :layout => !request.xhr?
155 render :layout => !request.xhr?
147 }
156 }
148 format.csv {
157 format.csv {
149 # Export all entries
158 # Export all entries
150 @entries = TimeEntry.find(:all,
159 @entries = TimeEntry.find(:all,
151 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
160 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
152 :conditions => cond.conditions,
161 :conditions => cond.conditions,
153 :order => sort_clause)
162 :order => sort_clause)
154 send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv')
163 send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv')
155 }
164 }
156 end
165 end
157 end
166 end
158 end
167 end
159
168
160 def edit
169 def edit
161 render_403 and return if @time_entry && !@time_entry.editable_by?(User.current)
170 render_403 and return if @time_entry && !@time_entry.editable_by?(User.current)
162 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
171 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
163 @time_entry.attributes = params[:time_entry]
172 @time_entry.attributes = params[:time_entry]
164 if request.post? and @time_entry.save
173 if request.post? and @time_entry.save
165 flash[:notice] = l(:notice_successful_update)
174 flash[:notice] = l(:notice_successful_update)
166 redirect_to :action => 'details', :project_id => @time_entry.project
175 redirect_to :action => 'details', :project_id => @time_entry.project
167 return
176 return
168 end
177 end
169 @activities = Enumeration::get_values('ACTI')
178 @activities = Enumeration::get_values('ACTI')
170 end
179 end
171
180
172 def destroy
181 def destroy
173 render_404 and return unless @time_entry
182 render_404 and return unless @time_entry
174 render_403 and return unless @time_entry.editable_by?(User.current)
183 render_403 and return unless @time_entry.editable_by?(User.current)
175 @time_entry.destroy
184 @time_entry.destroy
176 flash[:notice] = l(:notice_successful_delete)
185 flash[:notice] = l(:notice_successful_delete)
177 redirect_to :back
186 redirect_to :back
178 rescue RedirectBackError
187 rescue RedirectBackError
179 redirect_to :action => 'details', :project_id => @time_entry.project
188 redirect_to :action => 'details', :project_id => @time_entry.project
180 end
189 end
181
190
182 private
191 private
183 def find_project
192 def find_project
184 if params[:id]
193 if params[:id]
185 @time_entry = TimeEntry.find(params[:id])
194 @time_entry = TimeEntry.find(params[:id])
186 @project = @time_entry.project
195 @project = @time_entry.project
187 elsif params[:issue_id]
196 elsif params[:issue_id]
188 @issue = Issue.find(params[:issue_id])
197 @issue = Issue.find(params[:issue_id])
189 @project = @issue.project
198 @project = @issue.project
190 elsif params[:project_id]
199 elsif params[:project_id]
191 @project = Project.find(params[:project_id])
200 @project = Project.find(params[:project_id])
192 else
201 else
193 render_404
202 render_404
194 return false
203 return false
195 end
204 end
196 rescue ActiveRecord::RecordNotFound
205 rescue ActiveRecord::RecordNotFound
197 render_404
206 render_404
198 end
207 end
199
208
200 # Retrieves the date range based on predefined ranges or specific from/to param dates
209 # Retrieves the date range based on predefined ranges or specific from/to param dates
201 def retrieve_date_range
210 def retrieve_date_range
202 @free_period = false
211 @free_period = false
203 @from, @to = nil, nil
212 @from, @to = nil, nil
204
213
205 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
214 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
206 case params[:period].to_s
215 case params[:period].to_s
207 when 'today'
216 when 'today'
208 @from = @to = Date.today
217 @from = @to = Date.today
209 when 'yesterday'
218 when 'yesterday'
210 @from = @to = Date.today - 1
219 @from = @to = Date.today - 1
211 when 'current_week'
220 when 'current_week'
212 @from = Date.today - (Date.today.cwday - 1)%7
221 @from = Date.today - (Date.today.cwday - 1)%7
213 @to = @from + 6
222 @to = @from + 6
214 when 'last_week'
223 when 'last_week'
215 @from = Date.today - 7 - (Date.today.cwday - 1)%7
224 @from = Date.today - 7 - (Date.today.cwday - 1)%7
216 @to = @from + 6
225 @to = @from + 6
217 when '7_days'
226 when '7_days'
218 @from = Date.today - 7
227 @from = Date.today - 7
219 @to = Date.today
228 @to = Date.today
220 when 'current_month'
229 when 'current_month'
221 @from = Date.civil(Date.today.year, Date.today.month, 1)
230 @from = Date.civil(Date.today.year, Date.today.month, 1)
222 @to = (@from >> 1) - 1
231 @to = (@from >> 1) - 1
223 when 'last_month'
232 when 'last_month'
224 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
233 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
225 @to = (@from >> 1) - 1
234 @to = (@from >> 1) - 1
226 when '30_days'
235 when '30_days'
227 @from = Date.today - 30
236 @from = Date.today - 30
228 @to = Date.today
237 @to = Date.today
229 when 'current_year'
238 when 'current_year'
230 @from = Date.civil(Date.today.year, 1, 1)
239 @from = Date.civil(Date.today.year, 1, 1)
231 @to = Date.civil(Date.today.year, 12, 31)
240 @to = Date.civil(Date.today.year, 12, 31)
232 end
241 end
233 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
242 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
234 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
243 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
235 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
244 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
236 @free_period = true
245 @free_period = true
237 else
246 else
238 # default
247 # default
239 end
248 end
240
249
241 @from, @to = @to, @from if @from && @to && @from > @to
250 @from, @to = @to, @from if @from && @to && @from > @to
242 @from ||= (TimeEntry.minimum(:spent_on, :include => :project, :conditions => @project.project_condition(Setting.display_subprojects_issues?)) || Date.today) - 1
251 @from ||= (TimeEntry.minimum(:spent_on, :include => :project, :conditions => @project.project_condition(Setting.display_subprojects_issues?)) || Date.today) - 1
243 @to ||= (TimeEntry.maximum(:spent_on, :include => :project, :conditions => @project.project_condition(Setting.display_subprojects_issues?)) || Date.today)
252 @to ||= (TimeEntry.maximum(:spent_on, :include => :project, :conditions => @project.project_condition(Setting.display_subprojects_issues?)) || Date.today)
244 end
253 end
245 end
254 end
@@ -1,131 +1,135
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006 Jean-Philippe Lang
2 # Copyright (C) 2006 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 module TimelogHelper
18 module TimelogHelper
19 def select_hours(data, criteria, value)
19 def select_hours(data, criteria, value)
20 data.select {|row| row[criteria] == value}
20 data.select {|row| row[criteria] == value}
21 end
21 end
22
22
23 def sum_hours(data)
23 def sum_hours(data)
24 sum = 0
24 sum = 0
25 data.each do |row|
25 data.each do |row|
26 sum += row['hours'].to_f
26 sum += row['hours'].to_f
27 end
27 end
28 sum
28 sum
29 end
29 end
30
30
31 def options_for_period_select(value)
31 def options_for_period_select(value)
32 options_for_select([[l(:label_all_time), 'all'],
32 options_for_select([[l(:label_all_time), 'all'],
33 [l(:label_today), 'today'],
33 [l(:label_today), 'today'],
34 [l(:label_yesterday), 'yesterday'],
34 [l(:label_yesterday), 'yesterday'],
35 [l(:label_this_week), 'current_week'],
35 [l(:label_this_week), 'current_week'],
36 [l(:label_last_week), 'last_week'],
36 [l(:label_last_week), 'last_week'],
37 [l(:label_last_n_days, 7), '7_days'],
37 [l(:label_last_n_days, 7), '7_days'],
38 [l(:label_this_month), 'current_month'],
38 [l(:label_this_month), 'current_month'],
39 [l(:label_last_month), 'last_month'],
39 [l(:label_last_month), 'last_month'],
40 [l(:label_last_n_days, 30), '30_days'],
40 [l(:label_last_n_days, 30), '30_days'],
41 [l(:label_this_year), 'current_year']],
41 [l(:label_this_year), 'current_year']],
42 value)
42 value)
43 end
43 end
44
44
45 def entries_to_csv(entries)
45 def entries_to_csv(entries)
46 ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
46 ic = Iconv.new(l(:general_csv_encoding), 'UTF-8')
47 export = StringIO.new
47 export = StringIO.new
48 CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
48 CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
49 # csv header fields
49 # csv header fields
50 headers = [l(:field_spent_on),
50 headers = [l(:field_spent_on),
51 l(:field_user),
51 l(:field_user),
52 l(:field_activity),
52 l(:field_activity),
53 l(:field_project),
53 l(:field_project),
54 l(:field_issue),
54 l(:field_issue),
55 l(:field_tracker),
55 l(:field_tracker),
56 l(:field_subject),
56 l(:field_subject),
57 l(:field_hours),
57 l(:field_hours),
58 l(:field_comments)
58 l(:field_comments)
59 ]
59 ]
60 csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
60 csv << headers.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
61 # csv lines
61 # csv lines
62 entries.each do |entry|
62 entries.each do |entry|
63 fields = [l_date(entry.spent_on),
63 fields = [l_date(entry.spent_on),
64 entry.user,
64 entry.user,
65 entry.activity,
65 entry.activity,
66 entry.project,
66 entry.project,
67 (entry.issue ? entry.issue.id : nil),
67 (entry.issue ? entry.issue.id : nil),
68 (entry.issue ? entry.issue.tracker : nil),
68 (entry.issue ? entry.issue.tracker : nil),
69 (entry.issue ? entry.issue.subject : nil),
69 (entry.issue ? entry.issue.subject : nil),
70 entry.hours,
70 entry.hours,
71 entry.comments
71 entry.comments
72 ]
72 ]
73 csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
73 csv << fields.collect {|c| begin; ic.iconv(c.to_s); rescue; c.to_s; end }
74 end
74 end
75 end
75 end
76 export.rewind
76 export.rewind
77 export
77 export
78 end
78 end
79
79
80 def format_criteria_value(criteria, value)
81 value.blank? ? l(:label_none) : ((k = @available_criterias[criteria][:klass]) ? k.find_by_id(value.to_i) : format_value(value, @available_criterias[criteria][:format]))
82 end
83
80 def report_to_csv(criterias, periods, hours)
84 def report_to_csv(criterias, periods, hours)
81 export = StringIO.new
85 export = StringIO.new
82 CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
86 CSV::Writer.generate(export, l(:general_csv_separator)) do |csv|
83 # Column headers
87 # Column headers
84 headers = criterias.collect {|criteria| l(@available_criterias[criteria][:label]) }
88 headers = criterias.collect {|criteria| l(@available_criterias[criteria][:label]) }
85 headers += periods
89 headers += periods
86 headers << l(:label_total)
90 headers << l(:label_total)
87 csv << headers.collect {|c| to_utf8(c) }
91 csv << headers.collect {|c| to_utf8(c) }
88 # Content
92 # Content
89 report_criteria_to_csv(csv, criterias, periods, hours)
93 report_criteria_to_csv(csv, criterias, periods, hours)
90 # Total row
94 # Total row
91 row = [ l(:label_total) ] + [''] * (criterias.size - 1)
95 row = [ l(:label_total) ] + [''] * (criterias.size - 1)
92 total = 0
96 total = 0
93 periods.each do |period|
97 periods.each do |period|
94 sum = sum_hours(select_hours(hours, @columns, period.to_s))
98 sum = sum_hours(select_hours(hours, @columns, period.to_s))
95 total += sum
99 total += sum
96 row << (sum > 0 ? "%.2f" % sum : '')
100 row << (sum > 0 ? "%.2f" % sum : '')
97 end
101 end
98 row << "%.2f" %total
102 row << "%.2f" %total
99 csv << row
103 csv << row
100 end
104 end
101 export.rewind
105 export.rewind
102 export
106 export
103 end
107 end
104
108
105 def report_criteria_to_csv(csv, criterias, periods, hours, level=0)
109 def report_criteria_to_csv(csv, criterias, periods, hours, level=0)
106 hours.collect {|h| h[criterias[level]]}.uniq.each do |value|
110 hours.collect {|h| h[criterias[level]].to_s}.uniq.each do |value|
107 hours_for_value = select_hours(hours, criterias[level], value)
111 hours_for_value = select_hours(hours, criterias[level], value)
108 next if hours_for_value.empty?
112 next if hours_for_value.empty?
109 row = [''] * level
113 row = [''] * level
110 row << to_utf8(value.nil? ? l(:label_none) : @available_criterias[criterias[level]][:klass].find_by_id(value))
114 row << to_utf8(format_criteria_value(criterias[level], value))
111 row += [''] * (criterias.length - level - 1)
115 row += [''] * (criterias.length - level - 1)
112 total = 0
116 total = 0
113 periods.each do |period|
117 periods.each do |period|
114 sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s))
118 sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s))
115 total += sum
119 total += sum
116 row << (sum > 0 ? "%.2f" % sum : '')
120 row << (sum > 0 ? "%.2f" % sum : '')
117 end
121 end
118 row << "%.2f" %total
122 row << "%.2f" %total
119 csv << row
123 csv << row
120
124
121 if criterias.length > level + 1
125 if criterias.length > level + 1
122 report_criteria_to_csv(csv, criterias, periods, hours_for_value, level + 1)
126 report_criteria_to_csv(csv, criterias, periods, hours_for_value, level + 1)
123 end
127 end
124 end
128 end
125 end
129 end
126
130
127 def to_utf8(s)
131 def to_utf8(s)
128 @ic ||= Iconv.new(l(:general_csv_encoding), 'UTF-8')
132 @ic ||= Iconv.new(l(:general_csv_encoding), 'UTF-8')
129 begin; @ic.iconv(s.to_s); rescue; s.to_s; end
133 begin; @ic.iconv(s.to_s); rescue; s.to_s; end
130 end
134 end
131 end
135 end
@@ -1,19 +1,19
1 <% @hours.collect {|h| h[criterias[level]]}.uniq.each do |value| %>
1 <% @hours.collect {|h| h[criterias[level]].to_s}.uniq.each do |value| %>
2 <% hours_for_value = select_hours(hours, criterias[level], value) -%>
2 <% hours_for_value = select_hours(hours, criterias[level], value) -%>
3 <% next if hours_for_value.empty? -%>
3 <% next if hours_for_value.empty? -%>
4 <tr class="<%= cycle('odd', 'even') %> <%= 'last-level' unless criterias.length > level+1 %>">
4 <tr class="<%= cycle('odd', 'even') %> <%= 'last-level' unless criterias.length > level+1 %>">
5 <%= '<td></td>' * level %>
5 <%= '<td></td>' * level %>
6 <td><%= value.nil? ? l(:label_none) : @available_criterias[criterias[level]][:klass].find_by_id(value) %></td>
6 <td><%= format_criteria_value(criterias[level], value) %></td>
7 <%= '<td></td>' * (criterias.length - level - 1) -%>
7 <%= '<td></td>' * (criterias.length - level - 1) -%>
8 <% total = 0 -%>
8 <% total = 0 -%>
9 <% @periods.each do |period| -%>
9 <% @periods.each do |period| -%>
10 <% sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s)); total += sum -%>
10 <% sum = sum_hours(select_hours(hours_for_value, @columns, period.to_s)); total += sum -%>
11 <td class="hours"><%= html_hours("%.2f" % sum) if sum > 0 %></td>
11 <td class="hours"><%= html_hours("%.2f" % sum) if sum > 0 %></td>
12 <% end -%>
12 <% end -%>
13 <td class="hours"><%= html_hours("%.2f" % total) if total > 0 %></td>
13 <td class="hours"><%= html_hours("%.2f" % total) if total > 0 %></td>
14 </tr>
14 </tr>
15 <% if criterias.length > level+1 -%>
15 <% if criterias.length > level+1 -%>
16 <%= render(:partial => 'report_criteria', :locals => {:criterias => criterias, :hours => hours_for_value, :level => (level + 1)}) %>
16 <%= render(:partial => 'report_criteria', :locals => {:criterias => criterias, :hours => hours_for_value, :level => (level + 1)}) %>
17 <% end -%>
17 <% end -%>
18
18
19 <% end %>
19 <% end %>
@@ -1,63 +1,63
1 ---
1 ---
2 custom_fields_001:
2 custom_fields_001:
3 name: Database
3 name: Database
4 min_length: 0
4 min_length: 0
5 regexp: ""
5 regexp: ""
6 is_for_all: false
6 is_for_all: true
7 type: IssueCustomField
7 type: IssueCustomField
8 max_length: 0
8 max_length: 0
9 possible_values: MySQL|PostgreSQL|Oracle
9 possible_values: MySQL|PostgreSQL|Oracle
10 id: 1
10 id: 1
11 is_required: false
11 is_required: false
12 field_format: list
12 field_format: list
13 default_value: ""
13 default_value: ""
14 custom_fields_002:
14 custom_fields_002:
15 name: Searchable field
15 name: Searchable field
16 min_length: 1
16 min_length: 1
17 regexp: ""
17 regexp: ""
18 is_for_all: true
18 is_for_all: true
19 type: IssueCustomField
19 type: IssueCustomField
20 max_length: 100
20 max_length: 100
21 possible_values: ""
21 possible_values: ""
22 id: 2
22 id: 2
23 is_required: false
23 is_required: false
24 field_format: string
24 field_format: string
25 searchable: true
25 searchable: true
26 default_value: "Default string"
26 default_value: "Default string"
27 custom_fields_003:
27 custom_fields_003:
28 name: Development status
28 name: Development status
29 min_length: 0
29 min_length: 0
30 regexp: ""
30 regexp: ""
31 is_for_all: false
31 is_for_all: false
32 type: ProjectCustomField
32 type: ProjectCustomField
33 max_length: 0
33 max_length: 0
34 possible_values: Stable|Beta|Alpha|Planning
34 possible_values: Stable|Beta|Alpha|Planning
35 id: 3
35 id: 3
36 is_required: true
36 is_required: true
37 field_format: list
37 field_format: list
38 default_value: ""
38 default_value: ""
39 custom_fields_004:
39 custom_fields_004:
40 name: Phone number
40 name: Phone number
41 min_length: 0
41 min_length: 0
42 regexp: ""
42 regexp: ""
43 is_for_all: false
43 is_for_all: false
44 type: UserCustomField
44 type: UserCustomField
45 max_length: 0
45 max_length: 0
46 possible_values: ""
46 possible_values: ""
47 id: 4
47 id: 4
48 is_required: false
48 is_required: false
49 field_format: string
49 field_format: string
50 default_value: ""
50 default_value: ""
51 custom_fields_005:
51 custom_fields_005:
52 name: Money
52 name: Money
53 min_length: 0
53 min_length: 0
54 regexp: ""
54 regexp: ""
55 is_for_all: false
55 is_for_all: false
56 type: UserCustomField
56 type: UserCustomField
57 max_length: 0
57 max_length: 0
58 possible_values: ""
58 possible_values: ""
59 id: 5
59 id: 5
60 is_required: false
60 is_required: false
61 field_format: float
61 field_format: float
62 default_value: ""
62 default_value: ""
63 No newline at end of file
63
@@ -1,58 +1,58
1 ---
1 ---
2 time_entries_001:
2 time_entries_001:
3 created_on: 2007-03-23 12:54:18 +01:00
3 created_on: 2007-03-23 12:54:18 +01:00
4 tweek: 12
4 tweek: 12
5 tmonth: 3
5 tmonth: 3
6 project_id: 1
6 project_id: 1
7 comments: My hours
7 comments: My hours
8 updated_on: 2007-03-23 12:54:18 +01:00
8 updated_on: 2007-03-23 12:54:18 +01:00
9 activity_id: 9
9 activity_id: 9
10 spent_on: 2007-03-23
10 spent_on: 2007-03-23
11 issue_id: 1
11 issue_id: 1
12 id: 1
12 id: 1
13 hours: 4.25
13 hours: 4.25
14 user_id: 2
14 user_id: 2
15 tyear: 2007
15 tyear: 2007
16 time_entries_002:
16 time_entries_002:
17 created_on: 2007-03-23 14:11:04 +01:00
17 created_on: 2007-03-23 14:11:04 +01:00
18 tweek: 11
18 tweek: 11
19 tmonth: 3
19 tmonth: 3
20 project_id: 1
20 project_id: 1
21 comments: ""
21 comments: ""
22 updated_on: 2007-03-23 14:11:04 +01:00
22 updated_on: 2007-03-23 14:11:04 +01:00
23 activity_id: 9
23 activity_id: 9
24 spent_on: 2007-03-12
24 spent_on: 2007-03-12
25 issue_id: 1
25 issue_id: 1
26 id: 2
26 id: 2
27 hours: 150.0
27 hours: 150.0
28 user_id: 1
28 user_id: 1
29 tyear: 2007
29 tyear: 2007
30 time_entries_003:
30 time_entries_003:
31 created_on: 2007-04-21 12:20:48 +02:00
31 created_on: 2007-04-21 12:20:48 +02:00
32 tweek: 16
32 tweek: 16
33 tmonth: 4
33 tmonth: 4
34 project_id: 1
34 project_id: 1
35 comments: ""
35 comments: ""
36 updated_on: 2007-04-21 12:20:48 +02:00
36 updated_on: 2007-04-21 12:20:48 +02:00
37 activity_id: 9
37 activity_id: 9
38 spent_on: 2007-04-21
38 spent_on: 2007-04-21
39 issue_id: 2
39 issue_id: 3
40 id: 3
40 id: 3
41 hours: 1.0
41 hours: 1.0
42 user_id: 1
42 user_id: 1
43 tyear: 2007
43 tyear: 2007
44 time_entries_004:
44 time_entries_004:
45 created_on: 2007-04-22 12:20:48 +02:00
45 created_on: 2007-04-22 12:20:48 +02:00
46 tweek: 16
46 tweek: 16
47 tmonth: 4
47 tmonth: 4
48 project_id: 3
48 project_id: 3
49 comments: Time spent on a subproject
49 comments: Time spent on a subproject
50 updated_on: 2007-04-22 12:20:48 +02:00
50 updated_on: 2007-04-22 12:20:48 +02:00
51 activity_id: 10
51 activity_id: 10
52 spent_on: 2007-04-22
52 spent_on: 2007-04-22
53 issue_id:
53 issue_id:
54 id: 4
54 id: 4
55 hours: 7.65
55 hours: 7.65
56 user_id: 1
56 user_id: 1
57 tyear: 2007
57 tyear: 2007
58 No newline at end of file
58
@@ -1,506 +1,507
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 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 require File.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19 require 'issues_controller'
19 require 'issues_controller'
20
20
21 # Re-raise errors caught by the controller.
21 # Re-raise errors caught by the controller.
22 class IssuesController; def rescue_action(e) raise e end; end
22 class IssuesController; def rescue_action(e) raise e end; end
23
23
24 class IssuesControllerTest < Test::Unit::TestCase
24 class IssuesControllerTest < Test::Unit::TestCase
25 fixtures :projects,
25 fixtures :projects,
26 :users,
26 :users,
27 :roles,
27 :roles,
28 :members,
28 :members,
29 :issues,
29 :issues,
30 :issue_statuses,
30 :issue_statuses,
31 :trackers,
31 :trackers,
32 :projects_trackers,
32 :projects_trackers,
33 :issue_categories,
33 :issue_categories,
34 :enabled_modules,
34 :enabled_modules,
35 :enumerations,
35 :enumerations,
36 :attachments,
36 :attachments,
37 :workflows,
37 :workflows,
38 :custom_fields,
38 :custom_fields,
39 :custom_values,
39 :custom_values,
40 :custom_fields_trackers,
40 :custom_fields_trackers,
41 :time_entries
41 :time_entries
42
42
43 def setup
43 def setup
44 @controller = IssuesController.new
44 @controller = IssuesController.new
45 @request = ActionController::TestRequest.new
45 @request = ActionController::TestRequest.new
46 @response = ActionController::TestResponse.new
46 @response = ActionController::TestResponse.new
47 User.current = nil
47 User.current = nil
48 end
48 end
49
49
50 def test_index
50 def test_index
51 get :index
51 get :index
52 assert_response :success
52 assert_response :success
53 assert_template 'index.rhtml'
53 assert_template 'index.rhtml'
54 assert_not_nil assigns(:issues)
54 assert_not_nil assigns(:issues)
55 assert_nil assigns(:project)
55 assert_nil assigns(:project)
56 end
56 end
57
57
58 def test_index_with_project
58 def test_index_with_project
59 get :index, :project_id => 1
59 get :index, :project_id => 1
60 assert_response :success
60 assert_response :success
61 assert_template 'index.rhtml'
61 assert_template 'index.rhtml'
62 assert_not_nil assigns(:issues)
62 assert_not_nil assigns(:issues)
63 end
63 end
64
64
65 def test_index_with_project_and_filter
65 def test_index_with_project_and_filter
66 get :index, :project_id => 1, :set_filter => 1
66 get :index, :project_id => 1, :set_filter => 1
67 assert_response :success
67 assert_response :success
68 assert_template 'index.rhtml'
68 assert_template 'index.rhtml'
69 assert_not_nil assigns(:issues)
69 assert_not_nil assigns(:issues)
70 end
70 end
71
71
72 def test_index_csv_with_project
72 def test_index_csv_with_project
73 get :index, :format => 'csv'
73 get :index, :format => 'csv'
74 assert_response :success
74 assert_response :success
75 assert_not_nil assigns(:issues)
75 assert_not_nil assigns(:issues)
76 assert_equal 'text/csv', @response.content_type
76 assert_equal 'text/csv', @response.content_type
77
77
78 get :index, :project_id => 1, :format => 'csv'
78 get :index, :project_id => 1, :format => 'csv'
79 assert_response :success
79 assert_response :success
80 assert_not_nil assigns(:issues)
80 assert_not_nil assigns(:issues)
81 assert_equal 'text/csv', @response.content_type
81 assert_equal 'text/csv', @response.content_type
82 end
82 end
83
83
84 def test_index_pdf
84 def test_index_pdf
85 get :index, :format => 'pdf'
85 get :index, :format => 'pdf'
86 assert_response :success
86 assert_response :success
87 assert_not_nil assigns(:issues)
87 assert_not_nil assigns(:issues)
88 assert_equal 'application/pdf', @response.content_type
88 assert_equal 'application/pdf', @response.content_type
89
89
90 get :index, :project_id => 1, :format => 'pdf'
90 get :index, :project_id => 1, :format => 'pdf'
91 assert_response :success
91 assert_response :success
92 assert_not_nil assigns(:issues)
92 assert_not_nil assigns(:issues)
93 assert_equal 'application/pdf', @response.content_type
93 assert_equal 'application/pdf', @response.content_type
94 end
94 end
95
95
96 def test_changes
96 def test_changes
97 get :changes, :project_id => 1
97 get :changes, :project_id => 1
98 assert_response :success
98 assert_response :success
99 assert_not_nil assigns(:journals)
99 assert_not_nil assigns(:journals)
100 assert_equal 'application/atom+xml', @response.content_type
100 assert_equal 'application/atom+xml', @response.content_type
101 end
101 end
102
102
103 def test_show_by_anonymous
103 def test_show_by_anonymous
104 get :show, :id => 1
104 get :show, :id => 1
105 assert_response :success
105 assert_response :success
106 assert_template 'show.rhtml'
106 assert_template 'show.rhtml'
107 assert_not_nil assigns(:issue)
107 assert_not_nil assigns(:issue)
108 assert_equal Issue.find(1), assigns(:issue)
108 assert_equal Issue.find(1), assigns(:issue)
109
109
110 # anonymous role is allowed to add a note
110 # anonymous role is allowed to add a note
111 assert_tag :tag => 'form',
111 assert_tag :tag => 'form',
112 :descendant => { :tag => 'fieldset',
112 :descendant => { :tag => 'fieldset',
113 :child => { :tag => 'legend',
113 :child => { :tag => 'legend',
114 :content => /Notes/ } }
114 :content => /Notes/ } }
115 end
115 end
116
116
117 def test_show_by_manager
117 def test_show_by_manager
118 @request.session[:user_id] = 2
118 @request.session[:user_id] = 2
119 get :show, :id => 1
119 get :show, :id => 1
120 assert_response :success
120 assert_response :success
121
121
122 assert_tag :tag => 'form',
122 assert_tag :tag => 'form',
123 :descendant => { :tag => 'fieldset',
123 :descendant => { :tag => 'fieldset',
124 :child => { :tag => 'legend',
124 :child => { :tag => 'legend',
125 :content => /Change properties/ } },
125 :content => /Change properties/ } },
126 :descendant => { :tag => 'fieldset',
126 :descendant => { :tag => 'fieldset',
127 :child => { :tag => 'legend',
127 :child => { :tag => 'legend',
128 :content => /Log time/ } },
128 :content => /Log time/ } },
129 :descendant => { :tag => 'fieldset',
129 :descendant => { :tag => 'fieldset',
130 :child => { :tag => 'legend',
130 :child => { :tag => 'legend',
131 :content => /Notes/ } }
131 :content => /Notes/ } }
132 end
132 end
133
133
134 def test_get_new
134 def test_get_new
135 @request.session[:user_id] = 2
135 @request.session[:user_id] = 2
136 get :new, :project_id => 1, :tracker_id => 1
136 get :new, :project_id => 1, :tracker_id => 1
137 assert_response :success
137 assert_response :success
138 assert_template 'new'
138 assert_template 'new'
139
139
140 assert_tag :tag => 'input', :attributes => { :name => 'custom_fields[2]',
140 assert_tag :tag => 'input', :attributes => { :name => 'custom_fields[2]',
141 :value => 'Default string' }
141 :value => 'Default string' }
142 end
142 end
143
143
144 def test_get_new_without_tracker_id
144 def test_get_new_without_tracker_id
145 @request.session[:user_id] = 2
145 @request.session[:user_id] = 2
146 get :new, :project_id => 1
146 get :new, :project_id => 1
147 assert_response :success
147 assert_response :success
148 assert_template 'new'
148 assert_template 'new'
149
149
150 issue = assigns(:issue)
150 issue = assigns(:issue)
151 assert_not_nil issue
151 assert_not_nil issue
152 assert_equal Project.find(1).trackers.first, issue.tracker
152 assert_equal Project.find(1).trackers.first, issue.tracker
153 end
153 end
154
154
155 def test_update_new_form
155 def test_update_new_form
156 @request.session[:user_id] = 2
156 @request.session[:user_id] = 2
157 xhr :post, :new, :project_id => 1,
157 xhr :post, :new, :project_id => 1,
158 :issue => {:tracker_id => 2,
158 :issue => {:tracker_id => 2,
159 :subject => 'This is the test_new issue',
159 :subject => 'This is the test_new issue',
160 :description => 'This is the description',
160 :description => 'This is the description',
161 :priority_id => 5}
161 :priority_id => 5}
162 assert_response :success
162 assert_response :success
163 assert_template 'new'
163 assert_template 'new'
164 end
164 end
165
165
166 def test_post_new
166 def test_post_new
167 @request.session[:user_id] = 2
167 @request.session[:user_id] = 2
168 post :new, :project_id => 1,
168 post :new, :project_id => 1,
169 :issue => {:tracker_id => 1,
169 :issue => {:tracker_id => 1,
170 :subject => 'This is the test_new issue',
170 :subject => 'This is the test_new issue',
171 :description => 'This is the description',
171 :description => 'This is the description',
172 :priority_id => 5},
172 :priority_id => 5},
173 :custom_fields => {'2' => 'Value for field 2'}
173 :custom_fields => {'2' => 'Value for field 2'}
174 assert_redirected_to 'issues/show'
174 assert_redirected_to 'issues/show'
175
175
176 issue = Issue.find_by_subject('This is the test_new issue')
176 issue = Issue.find_by_subject('This is the test_new issue')
177 assert_not_nil issue
177 assert_not_nil issue
178 assert_equal 2, issue.author_id
178 assert_equal 2, issue.author_id
179 v = issue.custom_values.find_by_custom_field_id(2)
179 v = issue.custom_values.find_by_custom_field_id(2)
180 assert_not_nil v
180 assert_not_nil v
181 assert_equal 'Value for field 2', v.value
181 assert_equal 'Value for field 2', v.value
182 end
182 end
183
183
184 def test_post_new_without_custom_fields_param
184 def test_post_new_without_custom_fields_param
185 @request.session[:user_id] = 2
185 @request.session[:user_id] = 2
186 post :new, :project_id => 1,
186 post :new, :project_id => 1,
187 :issue => {:tracker_id => 1,
187 :issue => {:tracker_id => 1,
188 :subject => 'This is the test_new issue',
188 :subject => 'This is the test_new issue',
189 :description => 'This is the description',
189 :description => 'This is the description',
190 :priority_id => 5}
190 :priority_id => 5}
191 assert_redirected_to 'issues/show'
191 assert_redirected_to 'issues/show'
192 end
192 end
193
193
194 def test_copy_issue
194 def test_copy_issue
195 @request.session[:user_id] = 2
195 @request.session[:user_id] = 2
196 get :new, :project_id => 1, :copy_from => 1
196 get :new, :project_id => 1, :copy_from => 1
197 assert_template 'new'
197 assert_template 'new'
198 assert_not_nil assigns(:issue)
198 assert_not_nil assigns(:issue)
199 orig = Issue.find(1)
199 orig = Issue.find(1)
200 assert_equal orig.subject, assigns(:issue).subject
200 assert_equal orig.subject, assigns(:issue).subject
201 end
201 end
202
202
203 def test_get_edit
203 def test_get_edit
204 @request.session[:user_id] = 2
204 @request.session[:user_id] = 2
205 get :edit, :id => 1
205 get :edit, :id => 1
206 assert_response :success
206 assert_response :success
207 assert_template 'edit'
207 assert_template 'edit'
208 assert_not_nil assigns(:issue)
208 assert_not_nil assigns(:issue)
209 assert_equal Issue.find(1), assigns(:issue)
209 assert_equal Issue.find(1), assigns(:issue)
210 end
210 end
211
211
212 def test_get_edit_with_params
212 def test_get_edit_with_params
213 @request.session[:user_id] = 2
213 @request.session[:user_id] = 2
214 get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 }
214 get :edit, :id => 1, :issue => { :status_id => 5, :priority_id => 7 }
215 assert_response :success
215 assert_response :success
216 assert_template 'edit'
216 assert_template 'edit'
217
217
218 issue = assigns(:issue)
218 issue = assigns(:issue)
219 assert_not_nil issue
219 assert_not_nil issue
220
220
221 assert_equal 5, issue.status_id
221 assert_equal 5, issue.status_id
222 assert_tag :select, :attributes => { :name => 'issue[status_id]' },
222 assert_tag :select, :attributes => { :name => 'issue[status_id]' },
223 :child => { :tag => 'option',
223 :child => { :tag => 'option',
224 :content => 'Closed',
224 :content => 'Closed',
225 :attributes => { :selected => 'selected' } }
225 :attributes => { :selected => 'selected' } }
226
226
227 assert_equal 7, issue.priority_id
227 assert_equal 7, issue.priority_id
228 assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
228 assert_tag :select, :attributes => { :name => 'issue[priority_id]' },
229 :child => { :tag => 'option',
229 :child => { :tag => 'option',
230 :content => 'Urgent',
230 :content => 'Urgent',
231 :attributes => { :selected => 'selected' } }
231 :attributes => { :selected => 'selected' } }
232 end
232 end
233
233
234 def test_post_edit
234 def test_post_edit
235 @request.session[:user_id] = 2
235 @request.session[:user_id] = 2
236 ActionMailer::Base.deliveries.clear
236 ActionMailer::Base.deliveries.clear
237
237
238 issue = Issue.find(1)
238 issue = Issue.find(1)
239 old_subject = issue.subject
239 old_subject = issue.subject
240 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
240 new_subject = 'Subject modified by IssuesControllerTest#test_post_edit'
241
241
242 post :edit, :id => 1, :issue => {:subject => new_subject}
242 post :edit, :id => 1, :issue => {:subject => new_subject}
243 assert_redirected_to 'issues/show/1'
243 assert_redirected_to 'issues/show/1'
244 issue.reload
244 issue.reload
245 assert_equal new_subject, issue.subject
245 assert_equal new_subject, issue.subject
246
246
247 mail = ActionMailer::Base.deliveries.last
247 mail = ActionMailer::Base.deliveries.last
248 assert_kind_of TMail::Mail, mail
248 assert_kind_of TMail::Mail, mail
249 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
249 assert mail.subject.starts_with?("[#{issue.project.name} - #{issue.tracker.name} ##{issue.id}]")
250 assert mail.body.include?("Subject changed from #{old_subject} to #{new_subject}")
250 assert mail.body.include?("Subject changed from #{old_subject} to #{new_subject}")
251 end
251 end
252
252
253 def test_post_edit_with_status_and_assignee_change
253 def test_post_edit_with_status_and_assignee_change
254 issue = Issue.find(1)
254 issue = Issue.find(1)
255 assert_equal 1, issue.status_id
255 assert_equal 1, issue.status_id
256 @request.session[:user_id] = 2
256 @request.session[:user_id] = 2
257 post :edit,
257 post :edit,
258 :id => 1,
258 :id => 1,
259 :issue => { :status_id => 2, :assigned_to_id => 3 },
259 :issue => { :status_id => 2, :assigned_to_id => 3 },
260 :notes => 'Assigned to dlopper'
260 :notes => 'Assigned to dlopper'
261 assert_redirected_to 'issues/show/1'
261 assert_redirected_to 'issues/show/1'
262 issue.reload
262 issue.reload
263 assert_equal 2, issue.status_id
263 assert_equal 2, issue.status_id
264 j = issue.journals.find(:first, :order => 'id DESC')
264 j = issue.journals.find(:first, :order => 'id DESC')
265 assert_equal 'Assigned to dlopper', j.notes
265 assert_equal 'Assigned to dlopper', j.notes
266 assert_equal 2, j.details.size
266 assert_equal 2, j.details.size
267
267
268 mail = ActionMailer::Base.deliveries.last
268 mail = ActionMailer::Base.deliveries.last
269 assert mail.body.include?("Status changed from New to Assigned")
269 assert mail.body.include?("Status changed from New to Assigned")
270 end
270 end
271
271
272 def test_post_edit_with_note_only
272 def test_post_edit_with_note_only
273 notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
273 notes = 'Note added by IssuesControllerTest#test_update_with_note_only'
274 # anonymous user
274 # anonymous user
275 post :edit,
275 post :edit,
276 :id => 1,
276 :id => 1,
277 :notes => notes
277 :notes => notes
278 assert_redirected_to 'issues/show/1'
278 assert_redirected_to 'issues/show/1'
279 j = Issue.find(1).journals.find(:first, :order => 'id DESC')
279 j = Issue.find(1).journals.find(:first, :order => 'id DESC')
280 assert_equal notes, j.notes
280 assert_equal notes, j.notes
281 assert_equal 0, j.details.size
281 assert_equal 0, j.details.size
282 assert_equal User.anonymous, j.user
282 assert_equal User.anonymous, j.user
283
283
284 mail = ActionMailer::Base.deliveries.last
284 mail = ActionMailer::Base.deliveries.last
285 assert mail.body.include?(notes)
285 assert mail.body.include?(notes)
286 end
286 end
287
287
288 def test_post_edit_with_note_and_spent_time
288 def test_post_edit_with_note_and_spent_time
289 @request.session[:user_id] = 2
289 @request.session[:user_id] = 2
290 spent_hours_before = Issue.find(1).spent_hours
290 spent_hours_before = Issue.find(1).spent_hours
291 post :edit,
291 post :edit,
292 :id => 1,
292 :id => 1,
293 :notes => '2.5 hours added',
293 :notes => '2.5 hours added',
294 :time_entry => { :hours => '2.5', :comments => '', :activity_id => Enumeration.get_values('ACTI').first }
294 :time_entry => { :hours => '2.5', :comments => '', :activity_id => Enumeration.get_values('ACTI').first }
295 assert_redirected_to 'issues/show/1'
295 assert_redirected_to 'issues/show/1'
296
296
297 issue = Issue.find(1)
297 issue = Issue.find(1)
298
298
299 j = issue.journals.find(:first, :order => 'id DESC')
299 j = issue.journals.find(:first, :order => 'id DESC')
300 assert_equal '2.5 hours added', j.notes
300 assert_equal '2.5 hours added', j.notes
301 assert_equal 0, j.details.size
301 assert_equal 0, j.details.size
302
302
303 t = issue.time_entries.find(:first, :order => 'id DESC')
303 t = issue.time_entries.find(:first, :order => 'id DESC')
304 assert_not_nil t
304 assert_not_nil t
305 assert_equal 2.5, t.hours
305 assert_equal 2.5, t.hours
306 assert_equal spent_hours_before + 2.5, issue.spent_hours
306 assert_equal spent_hours_before + 2.5, issue.spent_hours
307 end
307 end
308
308
309 def test_post_edit_with_attachment_only
309 def test_post_edit_with_attachment_only
310 # anonymous user
310 # anonymous user
311 post :edit,
311 post :edit,
312 :id => 1,
312 :id => 1,
313 :notes => '',
313 :notes => '',
314 :attachments => {'1' => {'file' => test_uploaded_file('testfile.txt', 'text/plain')}}
314 :attachments => {'1' => {'file' => test_uploaded_file('testfile.txt', 'text/plain')}}
315 assert_redirected_to 'issues/show/1'
315 assert_redirected_to 'issues/show/1'
316 j = Issue.find(1).journals.find(:first, :order => 'id DESC')
316 j = Issue.find(1).journals.find(:first, :order => 'id DESC')
317 assert j.notes.blank?
317 assert j.notes.blank?
318 assert_equal 1, j.details.size
318 assert_equal 1, j.details.size
319 assert_equal 'testfile.txt', j.details.first.value
319 assert_equal 'testfile.txt', j.details.first.value
320 assert_equal User.anonymous, j.user
320 assert_equal User.anonymous, j.user
321
321
322 mail = ActionMailer::Base.deliveries.last
322 mail = ActionMailer::Base.deliveries.last
323 assert mail.body.include?('testfile.txt')
323 assert mail.body.include?('testfile.txt')
324 end
324 end
325
325
326 def test_post_edit_with_no_change
326 def test_post_edit_with_no_change
327 issue = Issue.find(1)
327 issue = Issue.find(1)
328 issue.journals.clear
328 issue.journals.clear
329 ActionMailer::Base.deliveries.clear
329 ActionMailer::Base.deliveries.clear
330
330
331 post :edit,
331 post :edit,
332 :id => 1,
332 :id => 1,
333 :notes => ''
333 :notes => ''
334 assert_redirected_to 'issues/show/1'
334 assert_redirected_to 'issues/show/1'
335
335
336 issue.reload
336 issue.reload
337 assert issue.journals.empty?
337 assert issue.journals.empty?
338 # No email should be sent
338 # No email should be sent
339 assert ActionMailer::Base.deliveries.empty?
339 assert ActionMailer::Base.deliveries.empty?
340 end
340 end
341
341
342 def test_bulk_edit
342 def test_bulk_edit
343 @request.session[:user_id] = 2
343 @request.session[:user_id] = 2
344 # update issues priority
344 # update issues priority
345 post :bulk_edit, :ids => [1, 2], :priority_id => 7, :notes => 'Bulk editing', :assigned_to_id => ''
345 post :bulk_edit, :ids => [1, 2], :priority_id => 7, :notes => 'Bulk editing', :assigned_to_id => ''
346 assert_response 302
346 assert_response 302
347 # check that the issues were updated
347 # check that the issues were updated
348 assert_equal [7, 7], Issue.find_all_by_id([1, 2]).collect {|i| i.priority.id}
348 assert_equal [7, 7], Issue.find_all_by_id([1, 2]).collect {|i| i.priority.id}
349 assert_equal 'Bulk editing', Issue.find(1).journals.find(:first, :order => 'created_on DESC').notes
349 assert_equal 'Bulk editing', Issue.find(1).journals.find(:first, :order => 'created_on DESC').notes
350 end
350 end
351
351
352 def test_bulk_unassign
352 def test_bulk_unassign
353 assert_not_nil Issue.find(2).assigned_to
353 assert_not_nil Issue.find(2).assigned_to
354 @request.session[:user_id] = 2
354 @request.session[:user_id] = 2
355 # unassign issues
355 # unassign issues
356 post :bulk_edit, :ids => [1, 2], :notes => 'Bulk unassigning', :assigned_to_id => 'none'
356 post :bulk_edit, :ids => [1, 2], :notes => 'Bulk unassigning', :assigned_to_id => 'none'
357 assert_response 302
357 assert_response 302
358 # check that the issues were updated
358 # check that the issues were updated
359 assert_nil Issue.find(2).assigned_to
359 assert_nil Issue.find(2).assigned_to
360 end
360 end
361
361
362 def test_move_one_issue_to_another_project
362 def test_move_one_issue_to_another_project
363 @request.session[:user_id] = 1
363 @request.session[:user_id] = 1
364 post :move, :id => 1, :new_project_id => 2
364 post :move, :id => 1, :new_project_id => 2
365 assert_redirected_to 'projects/ecookbook/issues'
365 assert_redirected_to 'projects/ecookbook/issues'
366 assert_equal 2, Issue.find(1).project_id
366 assert_equal 2, Issue.find(1).project_id
367 end
367 end
368
368
369 def test_bulk_move_to_another_project
369 def test_bulk_move_to_another_project
370 @request.session[:user_id] = 1
370 @request.session[:user_id] = 1
371 post :move, :ids => [1, 2], :new_project_id => 2
371 post :move, :ids => [1, 2], :new_project_id => 2
372 assert_redirected_to 'projects/ecookbook/issues'
372 assert_redirected_to 'projects/ecookbook/issues'
373 # Issues moved to project 2
373 # Issues moved to project 2
374 assert_equal 2, Issue.find(1).project_id
374 assert_equal 2, Issue.find(1).project_id
375 assert_equal 2, Issue.find(2).project_id
375 assert_equal 2, Issue.find(2).project_id
376 # No tracker change
376 # No tracker change
377 assert_equal 1, Issue.find(1).tracker_id
377 assert_equal 1, Issue.find(1).tracker_id
378 assert_equal 2, Issue.find(2).tracker_id
378 assert_equal 2, Issue.find(2).tracker_id
379 end
379 end
380
380
381 def test_bulk_move_to_another_tracker
381 def test_bulk_move_to_another_tracker
382 @request.session[:user_id] = 1
382 @request.session[:user_id] = 1
383 post :move, :ids => [1, 2], :new_tracker_id => 2
383 post :move, :ids => [1, 2], :new_tracker_id => 2
384 assert_redirected_to 'projects/ecookbook/issues'
384 assert_redirected_to 'projects/ecookbook/issues'
385 assert_equal 2, Issue.find(1).tracker_id
385 assert_equal 2, Issue.find(1).tracker_id
386 assert_equal 2, Issue.find(2).tracker_id
386 assert_equal 2, Issue.find(2).tracker_id
387 end
387 end
388
388
389 def test_context_menu_one_issue
389 def test_context_menu_one_issue
390 @request.session[:user_id] = 2
390 @request.session[:user_id] = 2
391 get :context_menu, :ids => [1]
391 get :context_menu, :ids => [1]
392 assert_response :success
392 assert_response :success
393 assert_template 'context_menu'
393 assert_template 'context_menu'
394 assert_tag :tag => 'a', :content => 'Edit',
394 assert_tag :tag => 'a', :content => 'Edit',
395 :attributes => { :href => '/issues/edit/1',
395 :attributes => { :href => '/issues/edit/1',
396 :class => 'icon-edit' }
396 :class => 'icon-edit' }
397 assert_tag :tag => 'a', :content => 'Closed',
397 assert_tag :tag => 'a', :content => 'Closed',
398 :attributes => { :href => '/issues/edit/1?issue%5Bstatus_id%5D=5',
398 :attributes => { :href => '/issues/edit/1?issue%5Bstatus_id%5D=5',
399 :class => '' }
399 :class => '' }
400 assert_tag :tag => 'a', :content => 'Immediate',
400 assert_tag :tag => 'a', :content => 'Immediate',
401 :attributes => { :href => '/issues/edit/1?issue%5Bpriority_id%5D=8',
401 :attributes => { :href => '/issues/edit/1?issue%5Bpriority_id%5D=8',
402 :class => '' }
402 :class => '' }
403 assert_tag :tag => 'a', :content => 'Dave Lopper',
403 assert_tag :tag => 'a', :content => 'Dave Lopper',
404 :attributes => { :href => '/issues/edit/1?issue%5Bassigned_to_id%5D=3',
404 :attributes => { :href => '/issues/edit/1?issue%5Bassigned_to_id%5D=3',
405 :class => '' }
405 :class => '' }
406 assert_tag :tag => 'a', :content => 'Copy',
406 assert_tag :tag => 'a', :content => 'Copy',
407 :attributes => { :href => '/projects/ecookbook/issues/new?copy_from=1',
407 :attributes => { :href => '/projects/ecookbook/issues/new?copy_from=1',
408 :class => 'icon-copy' }
408 :class => 'icon-copy' }
409 assert_tag :tag => 'a', :content => 'Move',
409 assert_tag :tag => 'a', :content => 'Move',
410 :attributes => { :href => '/issues/move?ids%5B%5D=1',
410 :attributes => { :href => '/issues/move?ids%5B%5D=1',
411 :class => 'icon-move' }
411 :class => 'icon-move' }
412 assert_tag :tag => 'a', :content => 'Delete',
412 assert_tag :tag => 'a', :content => 'Delete',
413 :attributes => { :href => '/issues/destroy?ids%5B%5D=1',
413 :attributes => { :href => '/issues/destroy?ids%5B%5D=1',
414 :class => 'icon-del' }
414 :class => 'icon-del' }
415 end
415 end
416
416
417 def test_context_menu_one_issue_by_anonymous
417 def test_context_menu_one_issue_by_anonymous
418 get :context_menu, :ids => [1]
418 get :context_menu, :ids => [1]
419 assert_response :success
419 assert_response :success
420 assert_template 'context_menu'
420 assert_template 'context_menu'
421 assert_tag :tag => 'a', :content => 'Delete',
421 assert_tag :tag => 'a', :content => 'Delete',
422 :attributes => { :href => '#',
422 :attributes => { :href => '#',
423 :class => 'icon-del disabled' }
423 :class => 'icon-del disabled' }
424 end
424 end
425
425
426 def test_context_menu_multiple_issues_of_same_project
426 def test_context_menu_multiple_issues_of_same_project
427 @request.session[:user_id] = 2
427 @request.session[:user_id] = 2
428 get :context_menu, :ids => [1, 2]
428 get :context_menu, :ids => [1, 2]
429 assert_response :success
429 assert_response :success
430 assert_template 'context_menu'
430 assert_template 'context_menu'
431 assert_tag :tag => 'a', :content => 'Edit',
431 assert_tag :tag => 'a', :content => 'Edit',
432 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;ids%5B%5D=2',
432 :attributes => { :href => '/issues/bulk_edit?ids%5B%5D=1&amp;ids%5B%5D=2',
433 :class => 'icon-edit' }
433 :class => 'icon-edit' }
434 assert_tag :tag => 'a', :content => 'Move',
434 assert_tag :tag => 'a', :content => 'Move',
435 :attributes => { :href => '/issues/move?ids%5B%5D=1&amp;ids%5B%5D=2',
435 :attributes => { :href => '/issues/move?ids%5B%5D=1&amp;ids%5B%5D=2',
436 :class => 'icon-move' }
436 :class => 'icon-move' }
437 assert_tag :tag => 'a', :content => 'Delete',
437 assert_tag :tag => 'a', :content => 'Delete',
438 :attributes => { :href => '/issues/destroy?ids%5B%5D=1&amp;ids%5B%5D=2',
438 :attributes => { :href => '/issues/destroy?ids%5B%5D=1&amp;ids%5B%5D=2',
439 :class => 'icon-del' }
439 :class => 'icon-del' }
440 end
440 end
441
441
442 def test_context_menu_multiple_issues_of_different_project
442 def test_context_menu_multiple_issues_of_different_project
443 @request.session[:user_id] = 2
443 @request.session[:user_id] = 2
444 get :context_menu, :ids => [1, 2, 4]
444 get :context_menu, :ids => [1, 2, 4]
445 assert_response :success
445 assert_response :success
446 assert_template 'context_menu'
446 assert_template 'context_menu'
447 assert_tag :tag => 'a', :content => 'Delete',
447 assert_tag :tag => 'a', :content => 'Delete',
448 :attributes => { :href => '#',
448 :attributes => { :href => '#',
449 :class => 'icon-del disabled' }
449 :class => 'icon-del disabled' }
450 end
450 end
451
451
452 def test_destroy_issue_with_no_time_entries
452 def test_destroy_issue_with_no_time_entries
453 assert_nil TimeEntry.find_by_issue_id(2)
453 @request.session[:user_id] = 2
454 @request.session[:user_id] = 2
454 post :destroy, :id => 3
455 post :destroy, :id => 2
455 assert_redirected_to 'projects/ecookbook/issues'
456 assert_redirected_to 'projects/ecookbook/issues'
456 assert_nil Issue.find_by_id(3)
457 assert_nil Issue.find_by_id(2)
457 end
458 end
458
459
459 def test_destroy_issues_with_time_entries
460 def test_destroy_issues_with_time_entries
460 @request.session[:user_id] = 2
461 @request.session[:user_id] = 2
461 post :destroy, :ids => [1, 3]
462 post :destroy, :ids => [1, 3]
462 assert_response :success
463 assert_response :success
463 assert_template 'destroy'
464 assert_template 'destroy'
464 assert_not_nil assigns(:hours)
465 assert_not_nil assigns(:hours)
465 assert Issue.find_by_id(1) && Issue.find_by_id(3)
466 assert Issue.find_by_id(1) && Issue.find_by_id(3)
466 end
467 end
467
468
468 def test_destroy_issues_and_destroy_time_entries
469 def test_destroy_issues_and_destroy_time_entries
469 @request.session[:user_id] = 2
470 @request.session[:user_id] = 2
470 post :destroy, :ids => [1, 3], :todo => 'destroy'
471 post :destroy, :ids => [1, 3], :todo => 'destroy'
471 assert_redirected_to 'projects/ecookbook/issues'
472 assert_redirected_to 'projects/ecookbook/issues'
472 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
473 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
473 assert_nil TimeEntry.find_by_id([1, 2])
474 assert_nil TimeEntry.find_by_id([1, 2])
474 end
475 end
475
476
476 def test_destroy_issues_and_assign_time_entries_to_project
477 def test_destroy_issues_and_assign_time_entries_to_project
477 @request.session[:user_id] = 2
478 @request.session[:user_id] = 2
478 post :destroy, :ids => [1, 3], :todo => 'nullify'
479 post :destroy, :ids => [1, 3], :todo => 'nullify'
479 assert_redirected_to 'projects/ecookbook/issues'
480 assert_redirected_to 'projects/ecookbook/issues'
480 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
481 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
481 assert_nil TimeEntry.find(1).issue_id
482 assert_nil TimeEntry.find(1).issue_id
482 assert_nil TimeEntry.find(2).issue_id
483 assert_nil TimeEntry.find(2).issue_id
483 end
484 end
484
485
485 def test_destroy_issues_and_reassign_time_entries_to_another_issue
486 def test_destroy_issues_and_reassign_time_entries_to_another_issue
486 @request.session[:user_id] = 2
487 @request.session[:user_id] = 2
487 post :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
488 post :destroy, :ids => [1, 3], :todo => 'reassign', :reassign_to_id => 2
488 assert_redirected_to 'projects/ecookbook/issues'
489 assert_redirected_to 'projects/ecookbook/issues'
489 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
490 assert !(Issue.find_by_id(1) || Issue.find_by_id(3))
490 assert_equal 2, TimeEntry.find(1).issue_id
491 assert_equal 2, TimeEntry.find(1).issue_id
491 assert_equal 2, TimeEntry.find(2).issue_id
492 assert_equal 2, TimeEntry.find(2).issue_id
492 end
493 end
493
494
494 def test_destroy_attachment
495 def test_destroy_attachment
495 issue = Issue.find(3)
496 issue = Issue.find(3)
496 a = issue.attachments.size
497 a = issue.attachments.size
497 @request.session[:user_id] = 2
498 @request.session[:user_id] = 2
498 post :destroy_attachment, :id => 3, :attachment_id => 1
499 post :destroy_attachment, :id => 3, :attachment_id => 1
499 assert_redirected_to 'issues/show/3'
500 assert_redirected_to 'issues/show/3'
500 assert_nil Attachment.find_by_id(1)
501 assert_nil Attachment.find_by_id(1)
501 issue.reload
502 issue.reload
502 assert_equal((a-1), issue.attachments.size)
503 assert_equal((a-1), issue.attachments.size)
503 j = issue.journals.find(:first, :order => 'created_on DESC')
504 j = issue.journals.find(:first, :order => 'created_on DESC')
504 assert_equal 'attachment', j.details.first.property
505 assert_equal 'attachment', j.details.first.property
505 end
506 end
506 end
507 end
@@ -1,191 +1,208
1 # redMine - project management software
1 # redMine - project management software
2 # Copyright (C) 2006-2007 Jean-Philippe Lang
2 # Copyright (C) 2006-2007 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 require File.dirname(__FILE__) + '/../test_helper'
18 require File.dirname(__FILE__) + '/../test_helper'
19 require 'timelog_controller'
19 require 'timelog_controller'
20
20
21 # Re-raise errors caught by the controller.
21 # Re-raise errors caught by the controller.
22 class TimelogController; def rescue_action(e) raise e end; end
22 class TimelogController; def rescue_action(e) raise e end; end
23
23
24 class TimelogControllerTest < Test::Unit::TestCase
24 class TimelogControllerTest < Test::Unit::TestCase
25 fixtures :projects, :enabled_modules, :roles, :members, :issues, :time_entries, :users, :trackers, :enumerations, :issue_statuses
25 fixtures :projects, :enabled_modules, :roles, :members, :issues, :time_entries, :users, :trackers, :enumerations, :issue_statuses, :custom_fields, :custom_values
26
26
27 def setup
27 def setup
28 @controller = TimelogController.new
28 @controller = TimelogController.new
29 @request = ActionController::TestRequest.new
29 @request = ActionController::TestRequest.new
30 @response = ActionController::TestResponse.new
30 @response = ActionController::TestResponse.new
31 end
31 end
32
32
33 def test_create
33 def test_create
34 @request.session[:user_id] = 3
34 @request.session[:user_id] = 3
35 post :edit, :project_id => 1,
35 post :edit, :project_id => 1,
36 :time_entry => {:comments => 'Some work on TimelogControllerTest',
36 :time_entry => {:comments => 'Some work on TimelogControllerTest',
37 :activity_id => '10',
37 :activity_id => '10',
38 :spent_on => '2008-03-14',
38 :spent_on => '2008-03-14',
39 :issue_id => '1',
39 :issue_id => '1',
40 :hours => '7.3'}
40 :hours => '7.3'}
41 assert_redirected_to 'projects/ecookbook/timelog/details'
41 assert_redirected_to 'projects/ecookbook/timelog/details'
42
42
43 i = Issue.find(1)
43 i = Issue.find(1)
44 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
44 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
45 assert_not_nil t
45 assert_not_nil t
46 assert_equal 7.3, t.hours
46 assert_equal 7.3, t.hours
47 assert_equal 3, t.user_id
47 assert_equal 3, t.user_id
48 assert_equal i, t.issue
48 assert_equal i, t.issue
49 assert_equal i.project, t.project
49 assert_equal i.project, t.project
50 end
50 end
51
51
52 def test_update
52 def test_update
53 entry = TimeEntry.find(1)
53 entry = TimeEntry.find(1)
54 assert_equal 1, entry.issue_id
54 assert_equal 1, entry.issue_id
55 assert_equal 2, entry.user_id
55 assert_equal 2, entry.user_id
56
56
57 @request.session[:user_id] = 1
57 @request.session[:user_id] = 1
58 post :edit, :id => 1,
58 post :edit, :id => 1,
59 :time_entry => {:issue_id => '2',
59 :time_entry => {:issue_id => '2',
60 :hours => '8'}
60 :hours => '8'}
61 assert_redirected_to 'projects/ecookbook/timelog/details'
61 assert_redirected_to 'projects/ecookbook/timelog/details'
62 entry.reload
62 entry.reload
63
63
64 assert_equal 8, entry.hours
64 assert_equal 8, entry.hours
65 assert_equal 2, entry.issue_id
65 assert_equal 2, entry.issue_id
66 assert_equal 2, entry.user_id
66 assert_equal 2, entry.user_id
67 end
67 end
68
68
69 def destroy
69 def destroy
70 @request.session[:user_id] = 2
70 @request.session[:user_id] = 2
71 post :destroy, :id => 1
71 post :destroy, :id => 1
72 assert_redirected_to 'projects/ecookbook/timelog/details'
72 assert_redirected_to 'projects/ecookbook/timelog/details'
73 assert_nil TimeEntry.find_by_id(1)
73 assert_nil TimeEntry.find_by_id(1)
74 end
74 end
75
75
76 def test_report_no_criteria
76 def test_report_no_criteria
77 get :report, :project_id => 1
77 get :report, :project_id => 1
78 assert_response :success
78 assert_response :success
79 assert_template 'report'
79 assert_template 'report'
80 end
80 end
81
81
82 def test_report_all_time
82 def test_report_all_time
83 get :report, :project_id => 1, :criterias => ['project', 'issue']
83 get :report, :project_id => 1, :criterias => ['project', 'issue']
84 assert_response :success
84 assert_response :success
85 assert_template 'report'
85 assert_template 'report'
86 assert_not_nil assigns(:total_hours)
86 assert_not_nil assigns(:total_hours)
87 assert_equal "162.90", "%.2f" % assigns(:total_hours)
87 assert_equal "162.90", "%.2f" % assigns(:total_hours)
88 end
88 end
89
89
90 def test_report_all_time_by_day
90 def test_report_all_time_by_day
91 get :report, :project_id => 1, :criterias => ['project', 'issue'], :columns => 'day'
91 get :report, :project_id => 1, :criterias => ['project', 'issue'], :columns => 'day'
92 assert_response :success
92 assert_response :success
93 assert_template 'report'
93 assert_template 'report'
94 assert_not_nil assigns(:total_hours)
94 assert_not_nil assigns(:total_hours)
95 assert_equal "162.90", "%.2f" % assigns(:total_hours)
95 assert_equal "162.90", "%.2f" % assigns(:total_hours)
96 assert_tag :tag => 'th', :content => '2007-03-12'
96 assert_tag :tag => 'th', :content => '2007-03-12'
97 end
97 end
98
98
99 def test_report_one_criteria
99 def test_report_one_criteria
100 get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
100 get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
101 assert_response :success
101 assert_response :success
102 assert_template 'report'
102 assert_template 'report'
103 assert_not_nil assigns(:total_hours)
103 assert_not_nil assigns(:total_hours)
104 assert_equal "8.65", "%.2f" % assigns(:total_hours)
104 assert_equal "8.65", "%.2f" % assigns(:total_hours)
105 end
105 end
106
106
107 def test_report_two_criterias
107 def test_report_two_criterias
108 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"]
108 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"]
109 assert_response :success
109 assert_response :success
110 assert_template 'report'
110 assert_template 'report'
111 assert_not_nil assigns(:total_hours)
111 assert_not_nil assigns(:total_hours)
112 assert_equal "162.90", "%.2f" % assigns(:total_hours)
112 assert_equal "162.90", "%.2f" % assigns(:total_hours)
113 end
113 end
114
114
115 def test_report_custom_field_criteria
116 get :report, :project_id => 1, :criterias => ['project', 'cf_1']
117 assert_response :success
118 assert_template 'report'
119 assert_not_nil assigns(:total_hours)
120 assert_not_nil assigns(:criterias)
121 assert_equal 2, assigns(:criterias).size
122 assert_equal "162.90", "%.2f" % assigns(:total_hours)
123 # Custom field column
124 assert_tag :tag => 'th', :content => 'Database'
125 # Custom field row
126 assert_tag :tag => 'td', :content => 'MySQL',
127 :sibling => { :tag => 'td', :attributes => { :class => 'hours' },
128 :child => { :tag => 'span', :attributes => { :class => 'hours hours-int' },
129 :content => '1' }}
130 end
131
115 def test_report_one_criteria_no_result
132 def test_report_one_criteria_no_result
116 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criterias => ['project']
133 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criterias => ['project']
117 assert_response :success
134 assert_response :success
118 assert_template 'report'
135 assert_template 'report'
119 assert_not_nil assigns(:total_hours)
136 assert_not_nil assigns(:total_hours)
120 assert_equal "0.00", "%.2f" % assigns(:total_hours)
137 assert_equal "0.00", "%.2f" % assigns(:total_hours)
121 end
138 end
122
139
123 def test_report_csv_export
140 def test_report_csv_export
124 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", :criterias => ["project", "member", "activity"], :format => "csv"
141 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", :criterias => ["project", "member", "activity"], :format => "csv"
125 assert_response :success
142 assert_response :success
126 assert_equal 'text/csv', @response.content_type
143 assert_equal 'text/csv', @response.content_type
127 lines = @response.body.chomp.split("\n")
144 lines = @response.body.chomp.split("\n")
128 # Headers
145 # Headers
129 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total', lines.first
146 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total', lines.first
130 # Total row
147 # Total row
131 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
148 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
132 end
149 end
133
150
134 def test_details_at_project_level
151 def test_details_at_project_level
135 get :details, :project_id => 1
152 get :details, :project_id => 1
136 assert_response :success
153 assert_response :success
137 assert_template 'details'
154 assert_template 'details'
138 assert_not_nil assigns(:entries)
155 assert_not_nil assigns(:entries)
139 assert_equal 4, assigns(:entries).size
156 assert_equal 4, assigns(:entries).size
140 # project and subproject
157 # project and subproject
141 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
158 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
142 assert_not_nil assigns(:total_hours)
159 assert_not_nil assigns(:total_hours)
143 assert_equal "162.90", "%.2f" % assigns(:total_hours)
160 assert_equal "162.90", "%.2f" % assigns(:total_hours)
144 # display all time by default
161 # display all time by default
145 assert_equal '2007-03-11'.to_date, assigns(:from)
162 assert_equal '2007-03-11'.to_date, assigns(:from)
146 assert_equal '2007-04-22'.to_date, assigns(:to)
163 assert_equal '2007-04-22'.to_date, assigns(:to)
147 end
164 end
148
165
149 def test_details_at_project_level_with_date_range
166 def test_details_at_project_level_with_date_range
150 get :details, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30'
167 get :details, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30'
151 assert_response :success
168 assert_response :success
152 assert_template 'details'
169 assert_template 'details'
153 assert_not_nil assigns(:entries)
170 assert_not_nil assigns(:entries)
154 assert_equal 3, assigns(:entries).size
171 assert_equal 3, assigns(:entries).size
155 assert_not_nil assigns(:total_hours)
172 assert_not_nil assigns(:total_hours)
156 assert_equal "12.90", "%.2f" % assigns(:total_hours)
173 assert_equal "12.90", "%.2f" % assigns(:total_hours)
157 assert_equal '2007-03-20'.to_date, assigns(:from)
174 assert_equal '2007-03-20'.to_date, assigns(:from)
158 assert_equal '2007-04-30'.to_date, assigns(:to)
175 assert_equal '2007-04-30'.to_date, assigns(:to)
159 end
176 end
160
177
161 def test_details_at_project_level_with_period
178 def test_details_at_project_level_with_period
162 get :details, :project_id => 1, :period => '7_days'
179 get :details, :project_id => 1, :period => '7_days'
163 assert_response :success
180 assert_response :success
164 assert_template 'details'
181 assert_template 'details'
165 assert_not_nil assigns(:entries)
182 assert_not_nil assigns(:entries)
166 assert_not_nil assigns(:total_hours)
183 assert_not_nil assigns(:total_hours)
167 assert_equal Date.today - 7, assigns(:from)
184 assert_equal Date.today - 7, assigns(:from)
168 assert_equal Date.today, assigns(:to)
185 assert_equal Date.today, assigns(:to)
169 end
186 end
170
187
171 def test_details_at_issue_level
188 def test_details_at_issue_level
172 get :details, :issue_id => 1
189 get :details, :issue_id => 1
173 assert_response :success
190 assert_response :success
174 assert_template 'details'
191 assert_template 'details'
175 assert_not_nil assigns(:entries)
192 assert_not_nil assigns(:entries)
176 assert_equal 2, assigns(:entries).size
193 assert_equal 2, assigns(:entries).size
177 assert_not_nil assigns(:total_hours)
194 assert_not_nil assigns(:total_hours)
178 assert_equal 154.25, assigns(:total_hours)
195 assert_equal 154.25, assigns(:total_hours)
179 # display all time by default
196 # display all time by default
180 assert_equal '2007-03-11'.to_date, assigns(:from)
197 assert_equal '2007-03-11'.to_date, assigns(:from)
181 assert_equal '2007-04-22'.to_date, assigns(:to)
198 assert_equal '2007-04-22'.to_date, assigns(:to)
182 end
199 end
183
200
184 def test_details_csv_export
201 def test_details_csv_export
185 get :details, :project_id => 1, :format => 'csv'
202 get :details, :project_id => 1, :format => 'csv'
186 assert_response :success
203 assert_response :success
187 assert_equal 'text/csv', @response.content_type
204 assert_equal 'text/csv', @response.content_type
188 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
205 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
189 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,2,Feature request,Add ingredients categories,1.0,\"\"\n")
206 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n")
190 end
207 end
191 end
208 end
General Comments 0
You need to be logged in to leave comments. Login now