annotate test/functional/time_entry_reports_controller_test.rb @ 1516:b450a9d58aed redmine-2.4

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