comparison .svn/pristine/fe/fe260260eb5a4d5517ce882020d2298022f2b0e4.svn-base @ 1517:dffacf8a6908 redmine-2.5

Update to Redmine SVN revision 13367 on 2.5-stable branch
author Chris Cannam
date Tue, 09 Sep 2014 09:29:00 +0100
parents
children
comparison
equal deleted inserted replaced
1516:b450a9d58aed 1517:dffacf8a6908
1 # -*- coding: utf-8 -*-
2 # Redmine - project management software
3 # Copyright (C) 2006-2014 Jean-Philippe Lang
4 #
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18
19 require File.expand_path('../../test_helper', __FILE__)
20
21 class TimeEntryReportsControllerTest < ActionController::TestCase
22 tests TimelogController
23
24 fixtures :projects, :enabled_modules, :roles, :members, :member_roles,
25 :issues, :time_entries, :users, :trackers, :enumerations,
26 :issue_statuses, :custom_fields, :custom_values,
27 :projects_trackers, :custom_fields_trackers,
28 :custom_fields_projects
29
30 include Redmine::I18n
31
32 def setup
33 Setting.default_language = "en"
34 end
35
36 def test_report_at_project_level
37 get :report, :project_id => 'ecookbook'
38 assert_response :success
39 assert_template 'report'
40 assert_tag :form,
41 :attributes => {:action => "/projects/ecookbook/time_entries/report", :id => 'query_form'}
42 end
43
44 def test_report_all_projects
45 get :report
46 assert_response :success
47 assert_template 'report'
48 assert_tag :form,
49 :attributes => {:action => "/time_entries/report", :id => 'query_form'}
50 end
51
52 def test_report_all_projects_denied
53 r = Role.anonymous
54 r.permissions.delete(:view_time_entries)
55 r.permissions_will_change!
56 r.save
57 get :report
58 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Ftime_entries%2Freport'
59 end
60
61 def test_report_all_projects_one_criteria
62 get :report, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']
63 assert_response :success
64 assert_template 'report'
65 assert_not_nil assigns(:report)
66 assert_equal "8.65", "%.2f" % assigns(:report).total_hours
67 end
68
69 def test_report_all_time
70 get :report, :project_id => 1, :criteria => ['project', 'issue']
71 assert_response :success
72 assert_template 'report'
73 assert_not_nil assigns(:report)
74 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
75 end
76
77 def test_report_all_time_by_day
78 get :report, :project_id => 1, :criteria => ['project', 'issue'], :columns => 'day'
79 assert_response :success
80 assert_template 'report'
81 assert_not_nil assigns(:report)
82 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
83 assert_tag :tag => 'th', :content => '2007-03-12'
84 end
85
86 def test_report_one_criteria
87 get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']
88 assert_response :success
89 assert_template 'report'
90 assert_not_nil assigns(:report)
91 assert_equal "8.65", "%.2f" % assigns(:report).total_hours
92 end
93
94 def test_report_two_criteria
95 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["user", "activity"]
96 assert_response :success
97 assert_template 'report'
98 assert_not_nil assigns(:report)
99 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
100 end
101
102 def test_report_custom_field_criteria_with_multiple_values_on_single_value_custom_field_should_not_fail
103 field = TimeEntryCustomField.create!(:name => 'multi', :field_format => 'list', :possible_values => ['value1', 'value2'])
104 entry = TimeEntry.create!(:project => Project.find(1), :hours => 1, :activity_id => 10, :user => User.find(2), :spent_on => Date.today)
105 CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value1')
106 CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value2')
107
108 get :report, :project_id => 1, :columns => 'day', :criteria => ["cf_#{field.id}"]
109 assert_response :success
110 end
111
112 def test_report_multiple_values_custom_fields_should_not_be_proposed
113 TimeEntryCustomField.create!(:name => 'Single', :field_format => 'list', :possible_values => ['value1', 'value2'])
114 TimeEntryCustomField.create!(:name => 'Multi', :field_format => 'list', :multiple => true, :possible_values => ['value1', 'value2'])
115
116 get :report, :project_id => 1
117 assert_response :success
118 assert_select 'select[name=?]', 'criteria[]' do
119 assert_select 'option', :text => 'Single'
120 assert_select 'option', :text => 'Multi', :count => 0
121 end
122 end
123
124 def test_report_one_day
125 get :report, :project_id => 1, :columns => 'day', :from => "2007-03-23", :to => "2007-03-23", :criteria => ["user", "activity"]
126 assert_response :success
127 assert_template 'report'
128 assert_not_nil assigns(:report)
129 assert_equal "4.25", "%.2f" % assigns(:report).total_hours
130 end
131
132 def test_report_at_issue_level
133 get :report, :project_id => 1, :issue_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["user", "activity"]
134 assert_response :success
135 assert_template 'report'
136 assert_not_nil assigns(:report)
137 assert_equal "154.25", "%.2f" % assigns(:report).total_hours
138 assert_tag :form,
139 :attributes => {:action => "/projects/ecookbook/issues/1/time_entries/report", :id => 'query_form'}
140 end
141
142 def test_report_by_week_should_use_commercial_year
143 TimeEntry.delete_all
144 TimeEntry.generate!(:hours => '2', :spent_on => '2009-12-25') # 2009-52
145 TimeEntry.generate!(:hours => '4', :spent_on => '2009-12-31') # 2009-53
146 TimeEntry.generate!(:hours => '8', :spent_on => '2010-01-01') # 2009-53
147 TimeEntry.generate!(:hours => '16', :spent_on => '2010-01-05') # 2010-1
148
149 get :report, :columns => 'week', :from => "2009-12-25", :to => "2010-01-05", :criteria => ["project"]
150 assert_response :success
151
152 assert_select '#time-report thead tr' do
153 assert_select 'th:nth-child(1)', :text => 'Project'
154 assert_select 'th:nth-child(2)', :text => '2009-52'
155 assert_select 'th:nth-child(3)', :text => '2009-53'
156 assert_select 'th:nth-child(4)', :text => '2010-1'
157 assert_select 'th:nth-child(5)', :text => 'Total time'
158 end
159 assert_select '#time-report tbody tr' do
160 assert_select 'td:nth-child(1)', :text => 'eCookbook'
161 assert_select 'td:nth-child(2)', :text => '2.00'
162 assert_select 'td:nth-child(3)', :text => '12.00'
163 assert_select 'td:nth-child(4)', :text => '16.00'
164 assert_select 'td:nth-child(5)', :text => '30.00' # Total
165 end
166 end
167
168 def test_report_should_propose_association_custom_fields
169 get :report
170 assert_response :success
171 assert_template 'report'
172
173 assert_select 'select[name=?]', 'criteria[]' do
174 assert_select 'option[value=cf_1]', {:text => 'Database'}, 'Issue custom field not found'
175 assert_select 'option[value=cf_3]', {:text => 'Development status'}, 'Project custom field not found'
176 assert_select 'option[value=cf_7]', {:text => 'Billable'}, 'TimeEntryActivity custom field not found'
177 end
178 end
179
180 def test_report_with_association_custom_fields
181 get :report, :criteria => ['cf_1', 'cf_3', 'cf_7']
182 assert_response :success
183 assert_template 'report'
184 assert_not_nil assigns(:report)
185 assert_equal 3, assigns(:report).criteria.size
186 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
187
188 # Custom fields columns
189 assert_select 'th', :text => 'Database'
190 assert_select 'th', :text => 'Development status'
191 assert_select 'th', :text => 'Billable'
192
193 # Custom field row
194 assert_select 'tr' do
195 assert_select 'td', :text => 'MySQL'
196 assert_select 'td.hours', :text => '1.00'
197 end
198 end
199
200 def test_report_one_criteria_no_result
201 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criteria => ['project']
202 assert_response :success
203 assert_template 'report'
204 assert_not_nil assigns(:report)
205 assert_equal "0.00", "%.2f" % assigns(:report).total_hours
206 end
207
208 def test_report_status_criterion
209 get :report, :project_id => 1, :criteria => ['status']
210 assert_response :success
211 assert_template 'report'
212 assert_tag :tag => 'th', :content => 'Status'
213 assert_tag :tag => 'td', :content => 'New'
214 end
215
216 def test_report_all_projects_csv_export
217 get :report, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30",
218 :criteria => ["project", "user", "activity"], :format => "csv"
219 assert_response :success
220 assert_equal 'text/csv; header=present', @response.content_type
221 lines = @response.body.chomp.split("\n")
222 # Headers
223 assert_equal 'Project,User,Activity,2007-3,2007-4,Total time', lines.first
224 # Total row
225 assert_equal 'Total time,"","",154.25,8.65,162.90', lines.last
226 end
227
228 def test_report_csv_export
229 get :report, :project_id => 1, :columns => 'month',
230 :from => "2007-01-01", :to => "2007-06-30",
231 :criteria => ["project", "user", "activity"], :format => "csv"
232 assert_response :success
233 assert_equal 'text/csv; header=present', @response.content_type
234 lines = @response.body.chomp.split("\n")
235 # Headers
236 assert_equal 'Project,User,Activity,2007-3,2007-4,Total time', lines.first
237 # Total row
238 assert_equal 'Total time,"","",154.25,8.65,162.90', lines.last
239 end
240
241 def test_csv_big_5
242 Setting.default_language = "zh-TW"
243 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
244 str_big5 = "\xa4@\xa4\xeb"
245 if str_utf8.respond_to?(:force_encoding)
246 str_utf8.force_encoding('UTF-8')
247 str_big5.force_encoding('Big5')
248 end
249 user = User.find_by_id(3)
250 user.firstname = str_utf8
251 user.lastname = "test-lastname"
252 assert user.save
253 comments = "test_csv_big_5"
254 te1 = TimeEntry.create(:spent_on => '2011-11-11',
255 :hours => 7.3,
256 :project => Project.find(1),
257 :user => user,
258 :activity => TimeEntryActivity.find_by_name('Design'),
259 :comments => comments)
260
261 te2 = TimeEntry.find_by_comments(comments)
262 assert_not_nil te2
263 assert_equal 7.3, te2.hours
264 assert_equal 3, te2.user_id
265
266 get :report, :project_id => 1, :columns => 'day',
267 :from => "2011-11-11", :to => "2011-11-11",
268 :criteria => ["user"], :format => "csv"
269 assert_response :success
270 assert_equal 'text/csv; header=present', @response.content_type
271 lines = @response.body.chomp.split("\n")
272 # Headers
273 s1 = "\xa5\xce\xa4\xe1,2011-11-11,\xa4u\xae\xc9\xc1`\xadp"
274 s2 = "\xa4u\xae\xc9\xc1`\xadp"
275 if s1.respond_to?(:force_encoding)
276 s1.force_encoding('Big5')
277 s2.force_encoding('Big5')
278 end
279 assert_equal s1, lines.first
280 # Total row
281 assert_equal "#{str_big5} #{user.lastname},7.30,7.30", lines[1]
282 assert_equal "#{s2},7.30,7.30", lines[2]
283
284 str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
285 if str_tw.respond_to?(:force_encoding)
286 str_tw.force_encoding('UTF-8')
287 end
288 assert_equal str_tw, l(:general_lang_name)
289 assert_equal 'Big5', l(:general_csv_encoding)
290 assert_equal ',', l(:general_csv_separator)
291 assert_equal '.', l(:general_csv_decimal_separator)
292 end
293
294 def test_csv_cannot_convert_should_be_replaced_big_5
295 Setting.default_language = "zh-TW"
296 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85"
297 if str_utf8.respond_to?(:force_encoding)
298 str_utf8.force_encoding('UTF-8')
299 end
300 user = User.find_by_id(3)
301 user.firstname = str_utf8
302 user.lastname = "test-lastname"
303 assert user.save
304 comments = "test_replaced"
305 te1 = TimeEntry.create(:spent_on => '2011-11-11',
306 :hours => 7.3,
307 :project => Project.find(1),
308 :user => user,
309 :activity => TimeEntryActivity.find_by_name('Design'),
310 :comments => comments)
311
312 te2 = TimeEntry.find_by_comments(comments)
313 assert_not_nil te2
314 assert_equal 7.3, te2.hours
315 assert_equal 3, te2.user_id
316
317 get :report, :project_id => 1, :columns => 'day',
318 :from => "2011-11-11", :to => "2011-11-11",
319 :criteria => ["user"], :format => "csv"
320 assert_response :success
321 assert_equal 'text/csv; header=present', @response.content_type
322 lines = @response.body.chomp.split("\n")
323 # Headers
324 s1 = "\xa5\xce\xa4\xe1,2011-11-11,\xa4u\xae\xc9\xc1`\xadp"
325 if s1.respond_to?(:force_encoding)
326 s1.force_encoding('Big5')
327 end
328 assert_equal s1, lines.first
329 # Total row
330 s2 = ""
331 if s2.respond_to?(:force_encoding)
332 s2 = "\xa5H?"
333 s2.force_encoding('Big5')
334 elsif RUBY_PLATFORM == 'java'
335 s2 = "??"
336 else
337 s2 = "\xa5H???"
338 end
339 assert_equal "#{s2} #{user.lastname},7.30,7.30", lines[1]
340 end
341
342 def test_csv_fr
343 with_settings :default_language => "fr" do
344 str1 = "test_csv_fr"
345 user = User.find_by_id(3)
346 te1 = TimeEntry.create(:spent_on => '2011-11-11',
347 :hours => 7.3,
348 :project => Project.find(1),
349 :user => user,
350 :activity => TimeEntryActivity.find_by_name('Design'),
351 :comments => str1)
352
353 te2 = TimeEntry.find_by_comments(str1)
354 assert_not_nil te2
355 assert_equal 7.3, te2.hours
356 assert_equal 3, te2.user_id
357
358 get :report, :project_id => 1, :columns => 'day',
359 :from => "2011-11-11", :to => "2011-11-11",
360 :criteria => ["user"], :format => "csv"
361 assert_response :success
362 assert_equal 'text/csv; header=present', @response.content_type
363 lines = @response.body.chomp.split("\n")
364 # Headers
365 s1 = "Utilisateur;2011-11-11;Temps total"
366 s2 = "Temps total"
367 if s1.respond_to?(:force_encoding)
368 s1.force_encoding('ISO-8859-1')
369 s2.force_encoding('ISO-8859-1')
370 end
371 assert_equal s1, lines.first
372 # Total row
373 assert_equal "#{user.firstname} #{user.lastname};7,30;7,30", lines[1]
374 assert_equal "#{s2};7,30;7,30", lines[2]
375
376 str_fr = "Fran\xc3\xa7ais"
377 if str_fr.respond_to?(:force_encoding)
378 str_fr.force_encoding('UTF-8')
379 end
380 assert_equal str_fr, l(:general_lang_name)
381 assert_equal 'ISO-8859-1', l(:general_csv_encoding)
382 assert_equal ';', l(:general_csv_separator)
383 assert_equal ',', l(:general_csv_decimal_separator)
384 end
385 end
386 end