comparison test/functional/time_entry_reports_controller_test.rb @ 1526:404aa68d4227

Merge from live branch
author Chris Cannam
date Thu, 11 Sep 2014 12:46:20 +0100
parents dffacf8a6908
children
comparison
equal deleted inserted replaced
1493:a5f2bdf3b486 1526:404aa68d4227
1 # -*- coding: utf-8 -*- 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
2 require File.expand_path('../../test_helper', __FILE__) 19 require File.expand_path('../../test_helper', __FILE__)
3 20
4 class TimeEntryReportsControllerTest < ActionController::TestCase 21 class TimeEntryReportsControllerTest < ActionController::TestCase
5 tests TimelogController 22 tests TimelogController
6 23
7 fixtures :projects, :enabled_modules, :roles, :members, :member_roles, 24 fixtures :projects, :enabled_modules, :roles, :members, :member_roles,
8 :issues, :time_entries, :users, :trackers, :enumerations, 25 :issues, :time_entries, :users, :trackers, :enumerations,
9 :issue_statuses, :custom_fields, :custom_values 26 :issue_statuses, :custom_fields, :custom_values,
27 :projects_trackers, :custom_fields_trackers,
28 :custom_fields_projects
10 29
11 include Redmine::I18n 30 include Redmine::I18n
12 31
13 def setup 32 def setup
14 Setting.default_language = "en" 33 Setting.default_language = "en"
71 assert_not_nil assigns(:report) 90 assert_not_nil assigns(:report)
72 assert_equal "8.65", "%.2f" % assigns(:report).total_hours 91 assert_equal "8.65", "%.2f" % assigns(:report).total_hours
73 end 92 end
74 93
75 def test_report_two_criteria 94 def test_report_two_criteria
76 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["member", "activity"] 95 get :report, :project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["user", "activity"]
77 assert_response :success 96 assert_response :success
78 assert_template 'report' 97 assert_template 'report'
79 assert_not_nil assigns(:report) 98 assert_not_nil assigns(:report)
80 assert_equal "162.90", "%.2f" % assigns(:report).total_hours 99 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
81 end 100 end
82 101
83 def test_report_custom_field_criteria_with_multiple_values 102 def test_report_custom_field_criteria_with_multiple_values_on_single_value_custom_field_should_not_fail
84 field = TimeEntryCustomField.create!(:name => 'multi', :field_format => 'list', :possible_values => ['value1', 'value2']) 103 field = TimeEntryCustomField.create!(:name => 'multi', :field_format => 'list', :possible_values => ['value1', 'value2'])
85 entry = TimeEntry.create!(:project => Project.find(1), :hours => 1, :activity_id => 10, :user => User.find(2), :spent_on => Date.today) 104 entry = TimeEntry.create!(:project => Project.find(1), :hours => 1, :activity_id => 10, :user => User.find(2), :spent_on => Date.today)
86 CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value1') 105 CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value1')
87 CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value2') 106 CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value2')
88 107
89 get :report, :project_id => 1, :columns => 'day', :criteria => ["cf_#{field.id}"] 108 get :report, :project_id => 1, :columns => 'day', :criteria => ["cf_#{field.id}"]
90 assert_response :success 109 assert_response :success
91 end 110 end
92 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
93 def test_report_one_day 124 def test_report_one_day
94 get :report, :project_id => 1, :columns => 'day', :from => "2007-03-23", :to => "2007-03-23", :criteria => ["member", "activity"] 125 get :report, :project_id => 1, :columns => 'day', :from => "2007-03-23", :to => "2007-03-23", :criteria => ["user", "activity"]
95 assert_response :success 126 assert_response :success
96 assert_template 'report' 127 assert_template 'report'
97 assert_not_nil assigns(:report) 128 assert_not_nil assigns(:report)
98 assert_equal "4.25", "%.2f" % assigns(:report).total_hours 129 assert_equal "4.25", "%.2f" % assigns(:report).total_hours
99 end 130 end
100 131
101 def test_report_at_issue_level 132 def test_report_at_issue_level
102 get :report, :project_id => 1, :issue_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["member", "activity"] 133 get :report, :project_id => 1, :issue_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["user", "activity"]
103 assert_response :success 134 assert_response :success
104 assert_template 'report' 135 assert_template 'report'
105 assert_not_nil assigns(:report) 136 assert_not_nil assigns(:report)
106 assert_equal "154.25", "%.2f" % assigns(:report).total_hours 137 assert_equal "154.25", "%.2f" % assigns(:report).total_hours
107 assert_tag :form, 138 assert_tag :form,
108 :attributes => {:action => "/projects/ecookbook/issues/1/time_entries/report", :id => 'query_form'} 139 :attributes => {:action => "/projects/ecookbook/issues/1/time_entries/report", :id => 'query_form'}
109 end 140 end
110 141
111 def test_report_custom_field_criteria 142 def test_report_by_week_should_use_commercial_year
112 get :report, :project_id => 1, :criteria => ['project', 'cf_1', 'cf_7'] 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']
113 assert_response :success 182 assert_response :success
114 assert_template 'report' 183 assert_template 'report'
115 assert_not_nil assigns(:report) 184 assert_not_nil assigns(:report)
116 assert_equal 3, assigns(:report).criteria.size 185 assert_equal 3, assigns(:report).criteria.size
117 assert_equal "162.90", "%.2f" % assigns(:report).total_hours 186 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
118 # Custom field column 187
119 assert_tag :tag => 'th', :content => 'Database' 188 # Custom fields columns
189 assert_select 'th', :text => 'Database'
190 assert_select 'th', :text => 'Development status'
191 assert_select 'th', :text => 'Billable'
192
120 # Custom field row 193 # Custom field row
121 assert_tag :tag => 'td', :content => 'MySQL', 194 assert_select 'tr' do
122 :sibling => { :tag => 'td', :attributes => { :class => 'hours' }, 195 assert_select 'td', :text => 'MySQL'
123 :child => { :tag => 'span', :attributes => { :class => 'hours hours-int' }, 196 assert_select 'td.hours', :text => '1.00'
124 :content => '1' }} 197 end
125 # Second custom field column
126 assert_tag :tag => 'th', :content => 'Billable'
127 end 198 end
128 199
129 def test_report_one_criteria_no_result 200 def test_report_one_criteria_no_result
130 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criteria => ['project'] 201 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criteria => ['project']
131 assert_response :success 202 assert_response :success
142 assert_tag :tag => 'td', :content => 'New' 213 assert_tag :tag => 'td', :content => 'New'
143 end 214 end
144 215
145 def test_report_all_projects_csv_export 216 def test_report_all_projects_csv_export
146 get :report, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30", 217 get :report, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30",
147 :criteria => ["project", "member", "activity"], :format => "csv" 218 :criteria => ["project", "user", "activity"], :format => "csv"
148 assert_response :success 219 assert_response :success
149 assert_equal 'text/csv; header=present', @response.content_type 220 assert_equal 'text/csv; header=present', @response.content_type
150 lines = @response.body.chomp.split("\n") 221 lines = @response.body.chomp.split("\n")
151 # Headers 222 # Headers
152 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total', 223 assert_equal 'Project,User,Activity,2007-3,2007-4,Total time', lines.first
153 lines.first
154 # Total row 224 # Total row
155 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last 225 assert_equal 'Total time,"","",154.25,8.65,162.90', lines.last
156 end 226 end
157 227
158 def test_report_csv_export 228 def test_report_csv_export
159 get :report, :project_id => 1, :columns => 'month', 229 get :report, :project_id => 1, :columns => 'month',
160 :from => "2007-01-01", :to => "2007-06-30", 230 :from => "2007-01-01", :to => "2007-06-30",
161 :criteria => ["project", "member", "activity"], :format => "csv" 231 :criteria => ["project", "user", "activity"], :format => "csv"
162 assert_response :success 232 assert_response :success
163 assert_equal 'text/csv; header=present', @response.content_type 233 assert_equal 'text/csv; header=present', @response.content_type
164 lines = @response.body.chomp.split("\n") 234 lines = @response.body.chomp.split("\n")
165 # Headers 235 # Headers
166 assert_equal 'Project,Member,Activity,2007-1,2007-2,2007-3,2007-4,2007-5,2007-6,Total', 236 assert_equal 'Project,User,Activity,2007-3,2007-4,Total time', lines.first
167 lines.first
168 # Total row 237 # Total row
169 assert_equal 'Total,"","","","",154.25,8.65,"","",162.90', lines.last 238 assert_equal 'Total time,"","",154.25,8.65,162.90', lines.last
170 end 239 end
171 240
172 def test_csv_big_5 241 def test_csv_big_5
173 Setting.default_language = "zh-TW" 242 Setting.default_language = "zh-TW"
174 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88" 243 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
194 assert_equal 7.3, te2.hours 263 assert_equal 7.3, te2.hours
195 assert_equal 3, te2.user_id 264 assert_equal 3, te2.user_id
196 265
197 get :report, :project_id => 1, :columns => 'day', 266 get :report, :project_id => 1, :columns => 'day',
198 :from => "2011-11-11", :to => "2011-11-11", 267 :from => "2011-11-11", :to => "2011-11-11",
199 :criteria => ["member"], :format => "csv" 268 :criteria => ["user"], :format => "csv"
200 assert_response :success 269 assert_response :success
201 assert_equal 'text/csv; header=present', @response.content_type 270 assert_equal 'text/csv; header=present', @response.content_type
202 lines = @response.body.chomp.split("\n") 271 lines = @response.body.chomp.split("\n")
203 # Headers 272 # Headers
204 s1 = "\xa6\xa8\xad\xfb,2011-11-11,\xc1`\xadp" 273 s1 = "\xa5\xce\xa4\xe1,2011-11-11,\xa4u\xae\xc9\xc1`\xadp"
205 s2 = "\xc1`\xadp" 274 s2 = "\xa4u\xae\xc9\xc1`\xadp"
206 if s1.respond_to?(:force_encoding) 275 if s1.respond_to?(:force_encoding)
207 s1.force_encoding('Big5') 276 s1.force_encoding('Big5')
208 s2.force_encoding('Big5') 277 s2.force_encoding('Big5')
209 end 278 end
210 assert_equal s1, lines.first 279 assert_equal s1, lines.first
245 assert_equal 7.3, te2.hours 314 assert_equal 7.3, te2.hours
246 assert_equal 3, te2.user_id 315 assert_equal 3, te2.user_id
247 316
248 get :report, :project_id => 1, :columns => 'day', 317 get :report, :project_id => 1, :columns => 'day',
249 :from => "2011-11-11", :to => "2011-11-11", 318 :from => "2011-11-11", :to => "2011-11-11",
250 :criteria => ["member"], :format => "csv" 319 :criteria => ["user"], :format => "csv"
251 assert_response :success 320 assert_response :success
252 assert_equal 'text/csv; header=present', @response.content_type 321 assert_equal 'text/csv; header=present', @response.content_type
253 lines = @response.body.chomp.split("\n") 322 lines = @response.body.chomp.split("\n")
254 # Headers 323 # Headers
255 s1 = "\xa6\xa8\xad\xfb,2011-11-11,\xc1`\xadp" 324 s1 = "\xa5\xce\xa4\xe1,2011-11-11,\xa4u\xae\xc9\xc1`\xadp"
256 if s1.respond_to?(:force_encoding) 325 if s1.respond_to?(:force_encoding)
257 s1.force_encoding('Big5') 326 s1.force_encoding('Big5')
258 end 327 end
259 assert_equal s1, lines.first 328 assert_equal s1, lines.first
260 # Total row 329 # Total row
286 assert_equal 7.3, te2.hours 355 assert_equal 7.3, te2.hours
287 assert_equal 3, te2.user_id 356 assert_equal 3, te2.user_id
288 357
289 get :report, :project_id => 1, :columns => 'day', 358 get :report, :project_id => 1, :columns => 'day',
290 :from => "2011-11-11", :to => "2011-11-11", 359 :from => "2011-11-11", :to => "2011-11-11",
291 :criteria => ["member"], :format => "csv" 360 :criteria => ["user"], :format => "csv"
292 assert_response :success 361 assert_response :success
293 assert_equal 'text/csv; header=present', @response.content_type 362 assert_equal 'text/csv; header=present', @response.content_type
294 lines = @response.body.chomp.split("\n") 363 lines = @response.body.chomp.split("\n")
295 # Headers 364 # Headers
296 s1 = "Membre;2011-11-11;Total" 365 s1 = "Utilisateur;2011-11-11;Temps total"
297 s2 = "Total" 366 s2 = "Temps total"
298 if s1.respond_to?(:force_encoding) 367 if s1.respond_to?(:force_encoding)
299 s1.force_encoding('ISO-8859-1') 368 s1.force_encoding('ISO-8859-1')
300 s2.force_encoding('ISO-8859-1') 369 s2.force_encoding('ISO-8859-1')
301 end 370 end
302 assert_equal s1, lines.first 371 assert_equal s1, lines.first