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