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