##// END OF EJS Templates
Makes timelog report work at issue level (#2935)....
Jean-Philippe Lang -
r2779:41cbd239c44e
parent child
Show More
@@ -1,293 +1,301
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 menu_item :issues
20 20 before_filter :find_project, :authorize, :only => [:edit, :destroy]
21 21 before_filter :find_optional_project, :only => [:report, :details]
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 29 helper :custom_fields
30 30 include CustomFieldsHelper
31 31
32 32 def report
33 33 @available_criterias = { 'project' => {:sql => "#{TimeEntry.table_name}.project_id",
34 34 :klass => Project,
35 35 :label => :label_project},
36 36 'version' => {:sql => "#{Issue.table_name}.fixed_version_id",
37 37 :klass => Version,
38 38 :label => :label_version},
39 39 'category' => {:sql => "#{Issue.table_name}.category_id",
40 40 :klass => IssueCategory,
41 41 :label => :field_category},
42 42 'member' => {:sql => "#{TimeEntry.table_name}.user_id",
43 43 :klass => User,
44 44 :label => :label_member},
45 45 'tracker' => {:sql => "#{Issue.table_name}.tracker_id",
46 46 :klass => Tracker,
47 47 :label => :label_tracker},
48 48 'activity' => {:sql => "#{TimeEntry.table_name}.activity_id",
49 49 :klass => TimeEntryActivity,
50 50 :label => :label_activity},
51 51 'issue' => {:sql => "#{TimeEntry.table_name}.issue_id",
52 52 :klass => Issue,
53 53 :label => :label_issue}
54 54 }
55 55
56 56 # Add list and boolean custom fields as available criterias
57 57 custom_fields = (@project.nil? ? IssueCustomField.for_all : @project.all_issue_custom_fields)
58 58 custom_fields.select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
59 59 @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'Issue' AND c.customized_id = #{Issue.table_name}.id)",
60 60 :format => cf.field_format,
61 61 :label => cf.name}
62 62 end if @project
63 63
64 64 # Add list and boolean time entry custom fields
65 65 TimeEntryCustomField.find(:all).select {|cf| %w(list bool).include? cf.field_format }.each do |cf|
66 66 @available_criterias["cf_#{cf.id}"] = {:sql => "(SELECT c.value FROM #{CustomValue.table_name} c WHERE c.custom_field_id = #{cf.id} AND c.customized_type = 'TimeEntry' AND c.customized_id = #{TimeEntry.table_name}.id)",
67 67 :format => cf.field_format,
68 68 :label => cf.name}
69 69 end
70 70
71 71 @criterias = params[:criterias] || []
72 72 @criterias = @criterias.select{|criteria| @available_criterias.has_key? criteria}
73 73 @criterias.uniq!
74 74 @criterias = @criterias[0,3]
75 75
76 76 @columns = (params[:columns] && %w(year month week day).include?(params[:columns])) ? params[:columns] : 'month'
77 77
78 78 retrieve_date_range
79 79
80 80 unless @criterias.empty?
81 81 sql_select = @criterias.collect{|criteria| @available_criterias[criteria][:sql] + " AS " + criteria}.join(', ')
82 82 sql_group_by = @criterias.collect{|criteria| @available_criterias[criteria][:sql]}.join(', ')
83 sql_condition = ''
84
85 if @project.nil?
86 sql_condition = Project.allowed_to_condition(User.current, :view_time_entries)
87 elsif @issue.nil?
88 sql_condition = @project.project_condition(Setting.display_subprojects_issues?)
89 else
90 sql_condition = "#{TimeEntry.table_name}.issue_id = #{@issue.id}"
91 end
83 92
84 93 sql = "SELECT #{sql_select}, tyear, tmonth, tweek, spent_on, SUM(hours) AS hours"
85 94 sql << " FROM #{TimeEntry.table_name}"
86 95 sql << " LEFT JOIN #{Issue.table_name} ON #{TimeEntry.table_name}.issue_id = #{Issue.table_name}.id"
87 96 sql << " LEFT JOIN #{Project.table_name} ON #{TimeEntry.table_name}.project_id = #{Project.table_name}.id"
88 97 sql << " WHERE"
89 sql << " (%s) AND" % @project.project_condition(Setting.display_subprojects_issues?) if @project
90 sql << " (%s) AND" % Project.allowed_to_condition(User.current, :view_time_entries)
98 sql << " (%s) AND" % sql_condition
91 99 sql << " (spent_on BETWEEN '%s' AND '%s')" % [ActiveRecord::Base.connection.quoted_date(@from.to_time), ActiveRecord::Base.connection.quoted_date(@to.to_time)]
92 100 sql << " GROUP BY #{sql_group_by}, tyear, tmonth, tweek, spent_on"
93 101
94 102 @hours = ActiveRecord::Base.connection.select_all(sql)
95 103
96 104 @hours.each do |row|
97 105 case @columns
98 106 when 'year'
99 107 row['year'] = row['tyear']
100 108 when 'month'
101 109 row['month'] = "#{row['tyear']}-#{row['tmonth']}"
102 110 when 'week'
103 111 row['week'] = "#{row['tyear']}-#{row['tweek']}"
104 112 when 'day'
105 113 row['day'] = "#{row['spent_on']}"
106 114 end
107 115 end
108 116
109 117 @total_hours = @hours.inject(0) {|s,k| s = s + k['hours'].to_f}
110 118
111 119 @periods = []
112 120 # Date#at_beginning_of_ not supported in Rails 1.2.x
113 121 date_from = @from.to_time
114 122 # 100 columns max
115 123 while date_from <= @to.to_time && @periods.length < 100
116 124 case @columns
117 125 when 'year'
118 126 @periods << "#{date_from.year}"
119 127 date_from = (date_from + 1.year).at_beginning_of_year
120 128 when 'month'
121 129 @periods << "#{date_from.year}-#{date_from.month}"
122 130 date_from = (date_from + 1.month).at_beginning_of_month
123 131 when 'week'
124 132 @periods << "#{date_from.year}-#{date_from.to_date.cweek}"
125 133 date_from = (date_from + 7.day).at_beginning_of_week
126 134 when 'day'
127 135 @periods << "#{date_from.to_date}"
128 136 date_from = date_from + 1.day
129 137 end
130 138 end
131 139 end
132 140
133 141 respond_to do |format|
134 142 format.html { render :layout => !request.xhr? }
135 143 format.csv { send_data(report_to_csv(@criterias, @periods, @hours).read, :type => 'text/csv; header=present', :filename => 'timelog.csv') }
136 144 end
137 145 end
138 146
139 147 def details
140 148 sort_init 'spent_on', 'desc'
141 149 sort_update 'spent_on' => 'spent_on',
142 150 'user' => 'user_id',
143 151 'activity' => 'activity_id',
144 152 'project' => "#{Project.table_name}.name",
145 153 'issue' => 'issue_id',
146 154 'hours' => 'hours'
147 155
148 156 cond = ARCondition.new
149 157 if @project.nil?
150 158 cond << Project.allowed_to_condition(User.current, :view_time_entries)
151 159 elsif @issue.nil?
152 160 cond << @project.project_condition(Setting.display_subprojects_issues?)
153 161 else
154 162 cond << ["#{TimeEntry.table_name}.issue_id = ?", @issue.id]
155 163 end
156 164
157 165 retrieve_date_range
158 166 cond << ['spent_on BETWEEN ? AND ?', @from, @to]
159 167
160 168 TimeEntry.visible_by(User.current) do
161 169 respond_to do |format|
162 170 format.html {
163 171 # Paginate results
164 172 @entry_count = TimeEntry.count(:include => :project, :conditions => cond.conditions)
165 173 @entry_pages = Paginator.new self, @entry_count, per_page_option, params['page']
166 174 @entries = TimeEntry.find(:all,
167 175 :include => [:project, :activity, :user, {:issue => :tracker}],
168 176 :conditions => cond.conditions,
169 177 :order => sort_clause,
170 178 :limit => @entry_pages.items_per_page,
171 179 :offset => @entry_pages.current.offset)
172 180 @total_hours = TimeEntry.sum(:hours, :include => :project, :conditions => cond.conditions).to_f
173 181
174 182 render :layout => !request.xhr?
175 183 }
176 184 format.atom {
177 185 entries = TimeEntry.find(:all,
178 186 :include => [:project, :activity, :user, {:issue => :tracker}],
179 187 :conditions => cond.conditions,
180 188 :order => "#{TimeEntry.table_name}.created_on DESC",
181 189 :limit => Setting.feeds_limit.to_i)
182 190 render_feed(entries, :title => l(:label_spent_time))
183 191 }
184 192 format.csv {
185 193 # Export all entries
186 194 @entries = TimeEntry.find(:all,
187 195 :include => [:project, :activity, :user, {:issue => [:tracker, :assigned_to, :priority]}],
188 196 :conditions => cond.conditions,
189 197 :order => sort_clause)
190 198 send_data(entries_to_csv(@entries).read, :type => 'text/csv; header=present', :filename => 'timelog.csv')
191 199 }
192 200 end
193 201 end
194 202 end
195 203
196 204 def edit
197 205 render_403 and return if @time_entry && !@time_entry.editable_by?(User.current)
198 206 @time_entry ||= TimeEntry.new(:project => @project, :issue => @issue, :user => User.current, :spent_on => Date.today)
199 207 @time_entry.attributes = params[:time_entry]
200 208
201 209 call_hook(:controller_timelog_edit_before_save, { :params => params, :time_entry => @time_entry })
202 210
203 211 if request.post? and @time_entry.save
204 212 flash[:notice] = l(:notice_successful_update)
205 213 redirect_back_or_default :action => 'details', :project_id => @time_entry.project
206 214 return
207 215 end
208 216 end
209 217
210 218 def destroy
211 219 render_404 and return unless @time_entry
212 220 render_403 and return unless @time_entry.editable_by?(User.current)
213 221 @time_entry.destroy
214 222 flash[:notice] = l(:notice_successful_delete)
215 223 redirect_to :back
216 224 rescue ::ActionController::RedirectBackError
217 225 redirect_to :action => 'details', :project_id => @time_entry.project
218 226 end
219 227
220 228 private
221 229 def find_project
222 230 if params[:id]
223 231 @time_entry = TimeEntry.find(params[:id])
224 232 @project = @time_entry.project
225 233 elsif params[:issue_id]
226 234 @issue = Issue.find(params[:issue_id])
227 235 @project = @issue.project
228 236 elsif params[:project_id]
229 237 @project = Project.find(params[:project_id])
230 238 else
231 239 render_404
232 240 return false
233 241 end
234 242 rescue ActiveRecord::RecordNotFound
235 243 render_404
236 244 end
237 245
238 246 def find_optional_project
239 247 if !params[:issue_id].blank?
240 248 @issue = Issue.find(params[:issue_id])
241 249 @project = @issue.project
242 250 elsif !params[:project_id].blank?
243 251 @project = Project.find(params[:project_id])
244 252 end
245 253 deny_access unless User.current.allowed_to?(:view_time_entries, @project, :global => true)
246 254 end
247 255
248 256 # Retrieves the date range based on predefined ranges or specific from/to param dates
249 257 def retrieve_date_range
250 258 @free_period = false
251 259 @from, @to = nil, nil
252 260
253 261 if params[:period_type] == '1' || (params[:period_type].nil? && !params[:period].nil?)
254 262 case params[:period].to_s
255 263 when 'today'
256 264 @from = @to = Date.today
257 265 when 'yesterday'
258 266 @from = @to = Date.today - 1
259 267 when 'current_week'
260 268 @from = Date.today - (Date.today.cwday - 1)%7
261 269 @to = @from + 6
262 270 when 'last_week'
263 271 @from = Date.today - 7 - (Date.today.cwday - 1)%7
264 272 @to = @from + 6
265 273 when '7_days'
266 274 @from = Date.today - 7
267 275 @to = Date.today
268 276 when 'current_month'
269 277 @from = Date.civil(Date.today.year, Date.today.month, 1)
270 278 @to = (@from >> 1) - 1
271 279 when 'last_month'
272 280 @from = Date.civil(Date.today.year, Date.today.month, 1) << 1
273 281 @to = (@from >> 1) - 1
274 282 when '30_days'
275 283 @from = Date.today - 30
276 284 @to = Date.today
277 285 when 'current_year'
278 286 @from = Date.civil(Date.today.year, 1, 1)
279 287 @to = Date.civil(Date.today.year, 12, 31)
280 288 end
281 289 elsif params[:period_type] == '2' || (params[:period_type].nil? && (!params[:from].nil? || !params[:to].nil?))
282 290 begin; @from = params[:from].to_s.to_date unless params[:from].blank?; rescue; end
283 291 begin; @to = params[:to].to_s.to_date unless params[:to].blank?; rescue; end
284 292 @free_period = true
285 293 else
286 294 # default
287 295 end
288 296
289 297 @from, @to = @to, @from if @from && @to && @from > @to
290 298 @from ||= (TimeEntry.minimum(:spent_on, :include => :project, :conditions => Project.allowed_to_condition(User.current, :view_time_entries)) || Date.today) - 1
291 299 @to ||= (TimeEntry.maximum(:spent_on, :include => :project, :conditions => Project.allowed_to_condition(User.current, :view_time_entries)) || Date.today)
292 300 end
293 301 end
@@ -1,35 +1,35
1 1 <fieldset id="date-range" class="collapsible">
2 2 <legend onclick="toggleFieldset(this);"><%= l(:label_date_range) %></legend>
3 3 <div>
4 4 <p>
5 5 <%= radio_button_tag 'period_type', '1', !@free_period %>
6 6 <%= select_tag 'period', options_for_period_select(params[:period]),
7 7 :onchange => 'this.form.onsubmit();',
8 8 :onfocus => '$("period_type_1").checked = true;' %>
9 9 </p>
10 10 <p>
11 11 <%= radio_button_tag 'period_type', '2', @free_period %>
12 12 <span onclick="$('period_type_2').checked = true;">
13 13 <%= l(:label_date_from_to, :start => (text_field_tag('from', @from, :size => 10) + calendar_for('from')),
14 14 :end => (text_field_tag('to', @to, :size => 10) + calendar_for('to'))) %>
15 15 </span>
16 16 </p>
17 17 </div>
18 18 </fieldset>
19 19 <p class="buttons">
20 20 <%= link_to_remote l(:button_apply),
21 21 { :url => { },
22 22 :update => "content",
23 23 :with => "Form.serialize('query_form')"
24 24 }, :class => 'icon icon-checked' %>
25 25 </p>
26 26
27 27 <div class="tabs">
28 28 <% url_params = @free_period ? { :from => @from, :to => @to } : { :period => params[:period] } %>
29 29 <ul>
30 <li><%= link_to(l(:label_details), url_params.merge({:controller => 'timelog', :action => 'details', :project_id => @project }),
30 <li><%= link_to(l(:label_details), url_params.merge({:controller => 'timelog', :action => 'details', :project_id => @project, :issue_id => @issue }),
31 31 :class => (@controller.action_name == 'details' ? 'selected' : nil)) %></li>
32 <li><%= link_to(l(:label_report), url_params.merge({:controller => 'timelog', :action => 'report', :project_id => @project}),
32 <li><%= link_to(l(:label_report), url_params.merge({:controller => 'timelog', :action => 'report', :project_id => @project, :issue_id => @issue}),
33 33 :class => (@controller.action_name == 'report' ? 'selected' : nil)) %></li>
34 34 </ul>
35 35 </div>
@@ -1,75 +1,76
1 1 <div class="contextual">
2 2 <%= link_to_if_authorized l(:button_log_time), {:controller => 'timelog', :action => 'edit', :project_id => @project, :issue_id => @issue}, :class => 'icon icon-time-add' %>
3 3 </div>
4 4
5 5 <%= render_timelog_breadcrumb %>
6 6
7 7 <h2><%= l(:label_spent_time) %></h2>
8 8
9 9 <% form_remote_tag(:url => {}, :html => {:method => :get, :id => 'query_form'}, :method => :get, :update => 'content') do %>
10 10 <% @criterias.each do |criteria| %>
11 11 <%= hidden_field_tag 'criterias[]', criteria, :id => nil %>
12 12 <% end %>
13 13 <%# TODO: get rid of the project_id field, that should already be in the URL %>
14 14 <%= hidden_field_tag('project_id', params[:project_id]) if @project %>
15 <%= hidden_field_tag('issue_id', params[:issue_id]) if @issue %>
15 16 <%= render :partial => 'date_range' %>
16 17
17 18 <p><%= l(:label_details) %>: <%= select_tag 'columns', options_for_select([[l(:label_year), 'year'],
18 19 [l(:label_month), 'month'],
19 20 [l(:label_week), 'week'],
20 21 [l(:label_day_plural).titleize, 'day']], @columns),
21 22 :onchange => "this.form.onsubmit();" %>
22 23
23 24 <%= l(:button_add) %>: <%= select_tag('criterias[]', options_for_select([[]] + (@available_criterias.keys - @criterias).collect{|k| [l_or_humanize(@available_criterias[k][:label]), k]}),
24 25 :onchange => "this.form.onsubmit();",
25 26 :style => 'width: 200px',
26 27 :id => nil,
27 28 :disabled => (@criterias.length >= 3)) %>
28 29 <%= link_to_remote l(:button_clear), {:url => {:project_id => @project, :period_type => params[:period_type], :period => params[:period], :from => @from, :to => @to, :columns => @columns},
29 30 :method => :get,
30 31 :update => 'content'
31 32 }, :class => 'icon icon-reload' %></p>
32 33 <% end %>
33 34
34 35 <% unless @criterias.empty? %>
35 36 <div class="total-hours">
36 37 <p><%= l(:label_total) %>: <%= html_hours(l_hours(@total_hours)) %></p>
37 38 </div>
38 39
39 40 <% unless @hours.empty? %>
40 41 <table class="list" id="time-report">
41 42 <thead>
42 43 <tr>
43 44 <% @criterias.each do |criteria| %>
44 45 <th><%= l_or_humanize(@available_criterias[criteria][:label]) %></th>
45 46 <% end %>
46 47 <% columns_width = (40 / (@periods.length+1)).to_i %>
47 48 <% @periods.each do |period| %>
48 49 <th class="period" width="<%= columns_width %>%"><%= period %></th>
49 50 <% end %>
50 51 <th class="total" width="<%= columns_width %>%"><%= l(:label_total) %></th>
51 52 </tr>
52 53 </thead>
53 54 <tbody>
54 55 <%= render :partial => 'report_criteria', :locals => {:criterias => @criterias, :hours => @hours, :level => 0} %>
55 56 <tr class="total">
56 57 <td><%= l(:label_total) %></td>
57 58 <%= '<td></td>' * (@criterias.size - 1) %>
58 59 <% total = 0 -%>
59 60 <% @periods.each do |period| -%>
60 61 <% sum = sum_hours(select_hours(@hours, @columns, period.to_s)); total += sum -%>
61 62 <td class="hours"><%= html_hours("%.2f" % sum) if sum > 0 %></td>
62 63 <% end -%>
63 64 <td class="hours"><%= html_hours("%.2f" % total) if total > 0 %></td>
64 65 </tr>
65 66 </tbody>
66 67 </table>
67 68
68 69 <% other_formats_links do |f| %>
69 70 <%= f.link_to 'CSV', :url => params %>
70 71 <% end %>
71 72 <% end %>
72 73 <% end %>
73 74
74 75 <% html_title l(:label_spent_time), l(:label_report) %>
75 76
@@ -1,396 +1,404
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 < ActionController::TestCase
25 25 fixtures :projects, :enabled_modules, :roles, :members, :member_roles, :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_edit_routing
34 34 assert_routing(
35 35 {:method => :get, :path => '/issues/567/time_entries/new'},
36 36 :controller => 'timelog', :action => 'edit', :issue_id => '567'
37 37 )
38 38 assert_routing(
39 39 {:method => :get, :path => '/projects/ecookbook/time_entries/new'},
40 40 :controller => 'timelog', :action => 'edit', :project_id => 'ecookbook'
41 41 )
42 42 assert_routing(
43 43 {:method => :get, :path => '/projects/ecookbook/issues/567/time_entries/new'},
44 44 :controller => 'timelog', :action => 'edit', :project_id => 'ecookbook', :issue_id => '567'
45 45 )
46 46
47 47 #TODO: change new form to POST to issue_time_entries_path instead of to edit action
48 48 #TODO: change edit form to PUT to time_entry_path
49 49 assert_routing(
50 50 {:method => :get, :path => '/time_entries/22/edit'},
51 51 :controller => 'timelog', :action => 'edit', :id => '22'
52 52 )
53 53 end
54 54
55 55 def test_get_edit
56 56 @request.session[:user_id] = 3
57 57 get :edit, :project_id => 1
58 58 assert_response :success
59 59 assert_template 'edit'
60 60 # Default activity selected
61 61 assert_tag :tag => 'option', :attributes => { :selected => 'selected' },
62 62 :content => 'Development'
63 63 end
64 64
65 65 def test_get_edit_existing_time
66 66 @request.session[:user_id] = 2
67 67 get :edit, :id => 2, :project_id => nil
68 68 assert_response :success
69 69 assert_template 'edit'
70 70 # Default activity selected
71 71 assert_tag :tag => 'form', :attributes => { :action => '/projects/ecookbook/timelog/edit/2' }
72 72 end
73 73
74 74 def test_post_edit
75 75 # TODO: should POST to issues’ time log instead of project. change form
76 76 # and routing
77 77 @request.session[:user_id] = 3
78 78 post :edit, :project_id => 1,
79 79 :time_entry => {:comments => 'Some work on TimelogControllerTest',
80 80 # Not the default activity
81 81 :activity_id => '11',
82 82 :spent_on => '2008-03-14',
83 83 :issue_id => '1',
84 84 :hours => '7.3'}
85 85 assert_redirected_to :action => 'details', :project_id => 'ecookbook'
86 86
87 87 i = Issue.find(1)
88 88 t = TimeEntry.find_by_comments('Some work on TimelogControllerTest')
89 89 assert_not_nil t
90 90 assert_equal 11, t.activity_id
91 91 assert_equal 7.3, t.hours
92 92 assert_equal 3, t.user_id
93 93 assert_equal i, t.issue
94 94 assert_equal i.project, t.project
95 95 end
96 96
97 97 def test_update
98 98 entry = TimeEntry.find(1)
99 99 assert_equal 1, entry.issue_id
100 100 assert_equal 2, entry.user_id
101 101
102 102 @request.session[:user_id] = 1
103 103 post :edit, :id => 1,
104 104 :time_entry => {:issue_id => '2',
105 105 :hours => '8'}
106 106 assert_redirected_to :action => 'details', :project_id => 'ecookbook'
107 107 entry.reload
108 108
109 109 assert_equal 8, entry.hours
110 110 assert_equal 2, entry.issue_id
111 111 assert_equal 2, entry.user_id
112 112 end
113 113
114 114 def test_destroy_routing
115 115 #TODO: use DELETE to time_entry_path
116 116 assert_routing(
117 117 {:method => :post, :path => '/time_entries/55/destroy'},
118 118 :controller => 'timelog', :action => 'destroy', :id => '55'
119 119 )
120 120 end
121 121
122 122 def test_destroy
123 123 @request.session[:user_id] = 2
124 124 post :destroy, :id => 1
125 125 assert_redirected_to :action => 'details', :project_id => 'ecookbook'
126 126 assert_nil TimeEntry.find_by_id(1)
127 127 end
128 128
129 129 def test_report_routing
130 130 assert_routing(
131 131 {:method => :get, :path => '/projects/567/time_entries/report'},
132 132 :controller => 'timelog', :action => 'report', :project_id => '567'
133 133 )
134 134 assert_routing(
135 135 {:method => :get, :path => '/projects/567/time_entries/report.csv'},
136 136 :controller => 'timelog', :action => 'report', :project_id => '567', :format => 'csv'
137 137 )
138 138 end
139 139
140 140 def test_report_no_criteria
141 141 get :report, :project_id => 1
142 142 assert_response :success
143 143 assert_template 'report'
144 144 end
145 145
146 146 def test_report_routing_for_all_projects
147 147 assert_routing(
148 148 {:method => :get, :path => '/time_entries/report'},
149 149 :controller => 'timelog', :action => 'report'
150 150 )
151 151 end
152 152
153 153 def test_report_all_projects
154 154 get :report
155 155 assert_response :success
156 156 assert_template 'report'
157 157 end
158 158
159 159 def test_report_all_projects_denied
160 160 r = Role.anonymous
161 161 r.permissions.delete(:view_time_entries)
162 162 r.permissions_will_change!
163 163 r.save
164 164 get :report
165 165 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Ftime_entries%2Freport'
166 166 end
167 167
168 168 def test_report_all_projects_one_criteria
169 169 get :report, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
170 170 assert_response :success
171 171 assert_template 'report'
172 172 assert_not_nil assigns(:total_hours)
173 173 assert_equal "8.65", "%.2f" % assigns(:total_hours)
174 174 end
175 175
176 176 def test_report_all_time
177 177 get :report, :project_id => 1, :criterias => ['project', 'issue']
178 178 assert_response :success
179 179 assert_template 'report'
180 180 assert_not_nil assigns(:total_hours)
181 181 assert_equal "162.90", "%.2f" % assigns(:total_hours)
182 182 end
183 183
184 184 def test_report_all_time_by_day
185 185 get :report, :project_id => 1, :criterias => ['project', 'issue'], :columns => 'day'
186 186 assert_response :success
187 187 assert_template 'report'
188 188 assert_not_nil assigns(:total_hours)
189 189 assert_equal "162.90", "%.2f" % assigns(:total_hours)
190 190 assert_tag :tag => 'th', :content => '2007-03-12'
191 191 end
192 192
193 193 def test_report_one_criteria
194 194 get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criterias => ['project']
195 195 assert_response :success
196 196 assert_template 'report'
197 197 assert_not_nil assigns(:total_hours)
198 198 assert_equal "8.65", "%.2f" % assigns(:total_hours)
199 199 end
200 200
201 201 def test_report_two_criterias
202 202 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"]
203 203 assert_response :success
204 204 assert_template 'report'
205 205 assert_not_nil assigns(:total_hours)
206 206 assert_equal "162.90", "%.2f" % assigns(:total_hours)
207 207 end
208 208
209 def test_report_at_issue_level
210 get :report, :project_id => 1, :issue_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criterias => ["member", "activity"]
211 assert_response :success
212 assert_template 'report'
213 assert_not_nil assigns(:total_hours)
214 assert_equal "154.25", "%.2f" % assigns(:total_hours)
215 end
216
209 217 def test_report_custom_field_criteria
210 218 get :report, :project_id => 1, :criterias => ['project', 'cf_1']
211 219 assert_response :success
212 220 assert_template 'report'
213 221 assert_not_nil assigns(:total_hours)
214 222 assert_not_nil assigns(:criterias)
215 223 assert_equal 2, assigns(:criterias).size
216 224 assert_equal "162.90", "%.2f" % assigns(:total_hours)
217 225 # Custom field column
218 226 assert_tag :tag => 'th', :content => 'Database'
219 227 # Custom field row
220 228 assert_tag :tag => 'td', :content => 'MySQL',
221 229 :sibling => { :tag => 'td', :attributes => { :class => 'hours' },
222 230 :child => { :tag => 'span', :attributes => { :class => 'hours hours-int' },
223 231 :content => '1' }}
224 232 end
225 233
226 234 def test_report_one_criteria_no_result
227 235 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criterias => ['project']
228 236 assert_response :success
229 237 assert_template 'report'
230 238 assert_not_nil assigns(:total_hours)
231 239 assert_equal "0.00", "%.2f" % assigns(:total_hours)
232 240 end
233 241
234 242 def test_report_all_projects_csv_export
235 243 get :report, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", :criterias => ["project", "member", "activity"], :format => "csv"
236 244 assert_response :success
237 245 assert_equal 'text/csv', @response.content_type
238 246 lines = @response.body.chomp.split("\n")
239 247 # Headers
240 248 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total', lines.first
241 249 # Total row
242 250 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
243 251 end
244 252
245 253 def test_report_csv_export
246 254 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", :criterias => ["project", "member", "activity"], :format => "csv"
247 255 assert_response :success
248 256 assert_equal 'text/csv', @response.content_type
249 257 lines = @response.body.chomp.split("\n")
250 258 # Headers
251 259 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total', lines.first
252 260 # Total row
253 261 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last
254 262 end
255 263
256 264 def test_details_all_projects
257 265 get :details
258 266 assert_response :success
259 267 assert_template 'details'
260 268 assert_not_nil assigns(:total_hours)
261 269 assert_equal "162.90", "%.2f" % assigns(:total_hours)
262 270 end
263 271
264 272 def test_project_details_routing
265 273 assert_routing(
266 274 {:method => :get, :path => '/projects/567/time_entries'},
267 275 :controller => 'timelog', :action => 'details', :project_id => '567'
268 276 )
269 277 end
270 278
271 279 def test_details_at_project_level
272 280 get :details, :project_id => 1
273 281 assert_response :success
274 282 assert_template 'details'
275 283 assert_not_nil assigns(:entries)
276 284 assert_equal 4, assigns(:entries).size
277 285 # project and subproject
278 286 assert_equal [1, 3], assigns(:entries).collect(&:project_id).uniq.sort
279 287 assert_not_nil assigns(:total_hours)
280 288 assert_equal "162.90", "%.2f" % assigns(:total_hours)
281 289 # display all time by default
282 290 assert_equal '2007-03-11'.to_date, assigns(:from)
283 291 assert_equal '2007-04-22'.to_date, assigns(:to)
284 292 end
285 293
286 294 def test_details_at_project_level_with_date_range
287 295 get :details, :project_id => 1, :from => '2007-03-20', :to => '2007-04-30'
288 296 assert_response :success
289 297 assert_template 'details'
290 298 assert_not_nil assigns(:entries)
291 299 assert_equal 3, assigns(:entries).size
292 300 assert_not_nil assigns(:total_hours)
293 301 assert_equal "12.90", "%.2f" % assigns(:total_hours)
294 302 assert_equal '2007-03-20'.to_date, assigns(:from)
295 303 assert_equal '2007-04-30'.to_date, assigns(:to)
296 304 end
297 305
298 306 def test_details_at_project_level_with_period
299 307 get :details, :project_id => 1, :period => '7_days'
300 308 assert_response :success
301 309 assert_template 'details'
302 310 assert_not_nil assigns(:entries)
303 311 assert_not_nil assigns(:total_hours)
304 312 assert_equal Date.today - 7, assigns(:from)
305 313 assert_equal Date.today, assigns(:to)
306 314 end
307 315
308 316 def test_issue_details_routing
309 317 assert_routing(
310 318 {:method => :get, :path => 'time_entries'},
311 319 :controller => 'timelog', :action => 'details'
312 320 )
313 321 assert_routing(
314 322 {:method => :get, :path => '/issues/234/time_entries'},
315 323 :controller => 'timelog', :action => 'details', :issue_id => '234'
316 324 )
317 325 # TODO: issue detail page shouldnt link to project_issue_time_entries_path but to normal issues one
318 326 # doesnt seem to have effect on resulting page so controller can be left untouched
319 327 assert_routing(
320 328 {:method => :get, :path => '/projects/ecookbook/issues/123/time_entries'},
321 329 :controller => 'timelog', :action => 'details', :project_id => 'ecookbook', :issue_id => '123'
322 330 )
323 331 end
324 332
325 333 def test_details_at_issue_level
326 334 get :details, :issue_id => 1
327 335 assert_response :success
328 336 assert_template 'details'
329 337 assert_not_nil assigns(:entries)
330 338 assert_equal 2, assigns(:entries).size
331 339 assert_not_nil assigns(:total_hours)
332 340 assert_equal 154.25, assigns(:total_hours)
333 341 # display all time by default
334 342 assert_equal '2007-03-11'.to_date, assigns(:from)
335 343 assert_equal '2007-04-22'.to_date, assigns(:to)
336 344 end
337 345
338 346 def test_details_formatted_routing
339 347 assert_routing(
340 348 {:method => :get, :path => 'time_entries.atom'},
341 349 :controller => 'timelog', :action => 'details', :format => 'atom'
342 350 )
343 351 assert_routing(
344 352 {:method => :get, :path => 'time_entries.csv'},
345 353 :controller => 'timelog', :action => 'details', :format => 'csv'
346 354 )
347 355 end
348 356
349 357 def test_details_for_project_formatted_routing
350 358 assert_routing(
351 359 {:method => :get, :path => '/projects/567/time_entries.atom'},
352 360 :controller => 'timelog', :action => 'details', :format => 'atom', :project_id => '567'
353 361 )
354 362 assert_routing(
355 363 {:method => :get, :path => '/projects/567/time_entries.csv'},
356 364 :controller => 'timelog', :action => 'details', :format => 'csv', :project_id => '567'
357 365 )
358 366 end
359 367
360 368 def test_details_for_issue_formatted_routing
361 369 assert_routing(
362 370 {:method => :get, :path => '/projects/ecookbook/issues/123/time_entries.atom'},
363 371 :controller => 'timelog', :action => 'details', :project_id => 'ecookbook', :issue_id => '123', :format => 'atom'
364 372 )
365 373 assert_routing(
366 374 {:method => :get, :path => '/projects/ecookbook/issues/123/time_entries.csv'},
367 375 :controller => 'timelog', :action => 'details', :project_id => 'ecookbook', :issue_id => '123', :format => 'csv'
368 376 )
369 377 end
370 378
371 379 def test_details_atom_feed
372 380 get :details, :project_id => 1, :format => 'atom'
373 381 assert_response :success
374 382 assert_equal 'application/atom+xml', @response.content_type
375 383 assert_not_nil assigns(:items)
376 384 assert assigns(:items).first.is_a?(TimeEntry)
377 385 end
378 386
379 387 def test_details_all_projects_csv_export
380 388 Setting.date_format = '%m/%d/%Y'
381 389 get :details, :format => 'csv'
382 390 assert_response :success
383 391 assert_equal 'text/csv', @response.content_type
384 392 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
385 393 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n")
386 394 end
387 395
388 396 def test_details_csv_export
389 397 Setting.date_format = '%m/%d/%Y'
390 398 get :details, :project_id => 1, :format => 'csv'
391 399 assert_response :success
392 400 assert_equal 'text/csv', @response.content_type
393 401 assert @response.body.include?("Date,User,Activity,Project,Issue,Tracker,Subject,Hours,Comment\n")
394 402 assert @response.body.include?("\n04/21/2007,redMine Admin,Design,eCookbook,3,Bug,Error 281 when updating a recipe,1.0,\"\"\n")
395 403 end
396 404 end
General Comments 0
You need to be logged in to leave comments. Login now