annotate .svn/pristine/2d/2dfab9f6389ac98654c616cc002d4ad37cf273c9.svn-base @ 1464:261b3d9a4903 redmine-2.4

Update to Redmine 2.4 branch rev 12663
author Chris Cannam
date Tue, 14 Jan 2014 14:37:42 +0000
parents
children
rev   line source
Chris@1464 1 # -*- coding: utf-8 -*-
Chris@1464 2 # Redmine - project management software
Chris@1464 3 # Copyright (C) 2006-2013 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@1464 19 require File.expand_path('../../test_helper', __FILE__)
Chris@1464 20
Chris@1464 21 class TimeEntryReportsControllerTest < ActionController::TestCase
Chris@1464 22 tests TimelogController
Chris@1464 23
Chris@1464 24 fixtures :projects, :enabled_modules, :roles, :members, :member_roles,
Chris@1464 25 :issues, :time_entries, :users, :trackers, :enumerations,
Chris@1464 26 :issue_statuses, :custom_fields, :custom_values
Chris@1464 27
Chris@1464 28 include Redmine::I18n
Chris@1464 29
Chris@1464 30 def setup
Chris@1464 31 Setting.default_language = "en"
Chris@1464 32 end
Chris@1464 33
Chris@1464 34 def test_report_at_project_level
Chris@1464 35 get :report, :project_id => 'ecookbook'
Chris@1464 36 assert_response :success
Chris@1464 37 assert_template 'report'
Chris@1464 38 assert_tag :form,
Chris@1464 39 :attributes => {:action => "/projects/ecookbook/time_entries/report", :id => 'query_form'}
Chris@1464 40 end
Chris@1464 41
Chris@1464 42 def test_report_all_projects
Chris@1464 43 get :report
Chris@1464 44 assert_response :success
Chris@1464 45 assert_template 'report'
Chris@1464 46 assert_tag :form,
Chris@1464 47 :attributes => {:action => "/time_entries/report", :id => 'query_form'}
Chris@1464 48 end
Chris@1464 49
Chris@1464 50 def test_report_all_projects_denied
Chris@1464 51 r = Role.anonymous
Chris@1464 52 r.permissions.delete(:view_time_entries)
Chris@1464 53 r.permissions_will_change!
Chris@1464 54 r.save
Chris@1464 55 get :report
Chris@1464 56 assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Ftime_entries%2Freport'
Chris@1464 57 end
Chris@1464 58
Chris@1464 59 def test_report_all_projects_one_criteria
Chris@1464 60 get :report, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']
Chris@1464 61 assert_response :success
Chris@1464 62 assert_template 'report'
Chris@1464 63 assert_not_nil assigns(:report)
Chris@1464 64 assert_equal "8.65", "%.2f" % assigns(:report).total_hours
Chris@1464 65 end
Chris@1464 66
Chris@1464 67 def test_report_all_time
Chris@1464 68 get :report, :project_id => 1, :criteria => ['project', 'issue']
Chris@1464 69 assert_response :success
Chris@1464 70 assert_template 'report'
Chris@1464 71 assert_not_nil assigns(:report)
Chris@1464 72 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
Chris@1464 73 end
Chris@1464 74
Chris@1464 75 def test_report_all_time_by_day
Chris@1464 76 get :report, :project_id => 1, :criteria => ['project', 'issue'], :columns => 'day'
Chris@1464 77 assert_response :success
Chris@1464 78 assert_template 'report'
Chris@1464 79 assert_not_nil assigns(:report)
Chris@1464 80 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
Chris@1464 81 assert_tag :tag => 'th', :content => '2007-03-12'
Chris@1464 82 end
Chris@1464 83
Chris@1464 84 def test_report_one_criteria
Chris@1464 85 get :report, :project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']
Chris@1464 86 assert_response :success
Chris@1464 87 assert_template 'report'
Chris@1464 88 assert_not_nil assigns(:report)
Chris@1464 89 assert_equal "8.65", "%.2f" % assigns(:report).total_hours
Chris@1464 90 end
Chris@1464 91
Chris@1464 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@1464 94 assert_response :success
Chris@1464 95 assert_template 'report'
Chris@1464 96 assert_not_nil assigns(:report)
Chris@1464 97 assert_equal "162.90", "%.2f" % assigns(:report).total_hours
Chris@1464 98 end
Chris@1464 99
Chris@1464 100 def test_report_custom_field_criteria_with_multiple_values
Chris@1464 101 field = TimeEntryCustomField.create!(:name => 'multi', :field_format => 'list', :possible_values => ['value1', 'value2'])
Chris@1464 102 entry = TimeEntry.create!(:project => Project.find(1), :hours => 1, :activity_id => 10, :user => User.find(2), :spent_on => Date.today)
Chris@1464 103 CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value1')
Chris@1464 104 CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value2')
Chris@1464 105
Chris@1464 106 get :report, :project_id => 1, :columns => 'day', :criteria => ["cf_#{field.id}"]
Chris@1464 107 assert_response :success
Chris@1464 108 end
Chris@1464 109
Chris@1464 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@1464 112 assert_response :success
Chris@1464 113 assert_template 'report'
Chris@1464 114 assert_not_nil assigns(:report)
Chris@1464 115 assert_equal "4.25", "%.2f" % assigns(:report).total_hours
Chris@1464 116 end
Chris@1464 117
Chris@1464 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@1464 120 assert_response :success
Chris@1464 121 assert_template 'report'
Chris@1464 122 assert_not_nil assigns(:report)
Chris@1464 123 assert_equal "154.25", "%.2f" % assigns(:report).total_hours
Chris@1464 124 assert_tag :form,
Chris@1464 125 :attributes => {:action => "/projects/ecookbook/issues/1/time_entries/report", :id => 'query_form'}
Chris@1464 126 end
Chris@1464 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@1464 168 assert_response :success
Chris@1464 169 assert_template 'report'
Chris@1464 170 assert_not_nil assigns(:report)
Chris@1464 171 assert_equal 3, assigns(:report).criteria.size
Chris@1464 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@1464 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@1464 184 end
Chris@1464 185
Chris@1464 186 def test_report_one_criteria_no_result
Chris@1464 187 get :report, :project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criteria => ['project']
Chris@1464 188 assert_response :success
Chris@1464 189 assert_template 'report'
Chris@1464 190 assert_not_nil assigns(:report)
Chris@1464 191 assert_equal "0.00", "%.2f" % assigns(:report).total_hours
Chris@1464 192 end
Chris@1464 193
Chris@1464 194 def test_report_status_criterion
Chris@1464 195 get :report, :project_id => 1, :criteria => ['status']
Chris@1464 196 assert_response :success
Chris@1464 197 assert_template 'report'
Chris@1464 198 assert_tag :tag => 'th', :content => 'Status'
Chris@1464 199 assert_tag :tag => 'td', :content => 'New'
Chris@1464 200 end
Chris@1464 201
Chris@1464 202 def test_report_all_projects_csv_export
Chris@1464 203 get :report, :columns => 'month', :from => "2007-01-01", :to => "2007-06-30",
Chris@1464 204 :criteria => ["project", "user", "activity"], :format => "csv"
Chris@1464 205 assert_response :success
Chris@1464 206 assert_equal 'text/csv; header=present', @response.content_type
Chris@1464 207 lines = @response.body.chomp.split("\n")
Chris@1464 208 # Headers
Chris@1464 209 assert_equal 'Project,User,Activity,2007-3,2007-4,Total time', lines.first
Chris@1464 210 # Total row
Chris@1464 211 assert_equal 'Total time,"","",154.25,8.65,162.90', lines.last
Chris@1464 212 end
Chris@1464 213
Chris@1464 214 def test_report_csv_export
Chris@1464 215 get :report, :project_id => 1, :columns => 'month',
Chris@1464 216 :from => "2007-01-01", :to => "2007-06-30",
Chris@1464 217 :criteria => ["project", "user", "activity"], :format => "csv"
Chris@1464 218 assert_response :success
Chris@1464 219 assert_equal 'text/csv; header=present', @response.content_type
Chris@1464 220 lines = @response.body.chomp.split("\n")
Chris@1464 221 # Headers
Chris@1464 222 assert_equal 'Project,User,Activity,2007-3,2007-4,Total time', lines.first
Chris@1464 223 # Total row
Chris@1464 224 assert_equal 'Total time,"","",154.25,8.65,162.90', lines.last
Chris@1464 225 end
Chris@1464 226
Chris@1464 227 def test_csv_big_5
Chris@1464 228 Setting.default_language = "zh-TW"
Chris@1464 229 str_utf8 = "\xe4\xb8\x80\xe6\x9c\x88"
Chris@1464 230 str_big5 = "\xa4@\xa4\xeb"
Chris@1464 231 if str_utf8.respond_to?(:force_encoding)
Chris@1464 232 str_utf8.force_encoding('UTF-8')
Chris@1464 233 str_big5.force_encoding('Big5')
Chris@1464 234 end
Chris@1464 235 user = User.find_by_id(3)
Chris@1464 236 user.firstname = str_utf8
Chris@1464 237 user.lastname = "test-lastname"
Chris@1464 238 assert user.save
Chris@1464 239 comments = "test_csv_big_5"
Chris@1464 240 te1 = TimeEntry.create(:spent_on => '2011-11-11',
Chris@1464 241 :hours => 7.3,
Chris@1464 242 :project => Project.find(1),
Chris@1464 243 :user => user,
Chris@1464 244 :activity => TimeEntryActivity.find_by_name('Design'),
Chris@1464 245 :comments => comments)
Chris@1464 246
Chris@1464 247 te2 = TimeEntry.find_by_comments(comments)
Chris@1464 248 assert_not_nil te2
Chris@1464 249 assert_equal 7.3, te2.hours
Chris@1464 250 assert_equal 3, te2.user_id
Chris@1464 251
Chris@1464 252 get :report, :project_id => 1, :columns => 'day',
Chris@1464 253 :from => "2011-11-11", :to => "2011-11-11",
Chris@1464 254 :criteria => ["user"], :format => "csv"
Chris@1464 255 assert_response :success
Chris@1464 256 assert_equal 'text/csv; header=present', @response.content_type
Chris@1464 257 lines = @response.body.chomp.split("\n")
Chris@1464 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@1464 261 if s1.respond_to?(:force_encoding)
Chris@1464 262 s1.force_encoding('Big5')
Chris@1464 263 s2.force_encoding('Big5')
Chris@1464 264 end
Chris@1464 265 assert_equal s1, lines.first
Chris@1464 266 # Total row
Chris@1464 267 assert_equal "#{str_big5} #{user.lastname},7.30,7.30", lines[1]
Chris@1464 268 assert_equal "#{s2},7.30,7.30", lines[2]
Chris@1464 269
Chris@1464 270 str_tw = "Traditional Chinese (\xe7\xb9\x81\xe9\xab\x94\xe4\xb8\xad\xe6\x96\x87)"
Chris@1464 271 if str_tw.respond_to?(:force_encoding)
Chris@1464 272 str_tw.force_encoding('UTF-8')
Chris@1464 273 end
Chris@1464 274 assert_equal str_tw, l(:general_lang_name)
Chris@1464 275 assert_equal 'Big5', l(:general_csv_encoding)
Chris@1464 276 assert_equal ',', l(:general_csv_separator)
Chris@1464 277 assert_equal '.', l(:general_csv_decimal_separator)
Chris@1464 278 end
Chris@1464 279
Chris@1464 280 def test_csv_cannot_convert_should_be_replaced_big_5
Chris@1464 281 Setting.default_language = "zh-TW"
Chris@1464 282 str_utf8 = "\xe4\xbb\xa5\xe5\x86\x85"
Chris@1464 283 if str_utf8.respond_to?(:force_encoding)
Chris@1464 284 str_utf8.force_encoding('UTF-8')
Chris@1464 285 end
Chris@1464 286 user = User.find_by_id(3)
Chris@1464 287 user.firstname = str_utf8
Chris@1464 288 user.lastname = "test-lastname"
Chris@1464 289 assert user.save
Chris@1464 290 comments = "test_replaced"
Chris@1464 291 te1 = TimeEntry.create(:spent_on => '2011-11-11',
Chris@1464 292 :hours => 7.3,
Chris@1464 293 :project => Project.find(1),
Chris@1464 294 :user => user,
Chris@1464 295 :activity => TimeEntryActivity.find_by_name('Design'),
Chris@1464 296 :comments => comments)
Chris@1464 297
Chris@1464 298 te2 = TimeEntry.find_by_comments(comments)
Chris@1464 299 assert_not_nil te2
Chris@1464 300 assert_equal 7.3, te2.hours
Chris@1464 301 assert_equal 3, te2.user_id
Chris@1464 302
Chris@1464 303 get :report, :project_id => 1, :columns => 'day',
Chris@1464 304 :from => "2011-11-11", :to => "2011-11-11",
Chris@1464 305 :criteria => ["user"], :format => "csv"
Chris@1464 306 assert_response :success
Chris@1464 307 assert_equal 'text/csv; header=present', @response.content_type
Chris@1464 308 lines = @response.body.chomp.split("\n")
Chris@1464 309 # Headers
Chris@1464 310 s1 = "\xa5\xce\xa4\xe1,2011-11-11,\xa4u\xae\xc9\xc1`\xadp"
Chris@1464 311 if s1.respond_to?(:force_encoding)
Chris@1464 312 s1.force_encoding('Big5')
Chris@1464 313 end
Chris@1464 314 assert_equal s1, lines.first
Chris@1464 315 # Total row
Chris@1464 316 s2 = ""
Chris@1464 317 if s2.respond_to?(:force_encoding)
Chris@1464 318 s2 = "\xa5H?"
Chris@1464 319 s2.force_encoding('Big5')
Chris@1464 320 elsif RUBY_PLATFORM == 'java'
Chris@1464 321 s2 = "??"
Chris@1464 322 else
Chris@1464 323 s2 = "\xa5H???"
Chris@1464 324 end
Chris@1464 325 assert_equal "#{s2} #{user.lastname},7.30,7.30", lines[1]
Chris@1464 326 end
Chris@1464 327
Chris@1464 328 def test_csv_fr
Chris@1464 329 with_settings :default_language => "fr" do
Chris@1464 330 str1 = "test_csv_fr"
Chris@1464 331 user = User.find_by_id(3)
Chris@1464 332 te1 = TimeEntry.create(:spent_on => '2011-11-11',
Chris@1464 333 :hours => 7.3,
Chris@1464 334 :project => Project.find(1),
Chris@1464 335 :user => user,
Chris@1464 336 :activity => TimeEntryActivity.find_by_name('Design'),
Chris@1464 337 :comments => str1)
Chris@1464 338
Chris@1464 339 te2 = TimeEntry.find_by_comments(str1)
Chris@1464 340 assert_not_nil te2
Chris@1464 341 assert_equal 7.3, te2.hours
Chris@1464 342 assert_equal 3, te2.user_id
Chris@1464 343
Chris@1464 344 get :report, :project_id => 1, :columns => 'day',
Chris@1464 345 :from => "2011-11-11", :to => "2011-11-11",
Chris@1464 346 :criteria => ["user"], :format => "csv"
Chris@1464 347 assert_response :success
Chris@1464 348 assert_equal 'text/csv; header=present', @response.content_type
Chris@1464 349 lines = @response.body.chomp.split("\n")
Chris@1464 350 # Headers
Chris@1464 351 s1 = "Utilisateur;2011-11-11;Temps total"
Chris@1464 352 s2 = "Temps total"
Chris@1464 353 if s1.respond_to?(:force_encoding)
Chris@1464 354 s1.force_encoding('ISO-8859-1')
Chris@1464 355 s2.force_encoding('ISO-8859-1')
Chris@1464 356 end
Chris@1464 357 assert_equal s1, lines.first
Chris@1464 358 # Total row
Chris@1464 359 assert_equal "#{user.firstname} #{user.lastname};7,30;7,30", lines[1]
Chris@1464 360 assert_equal "#{s2};7,30;7,30", lines[2]
Chris@1464 361
Chris@1464 362 str_fr = "Fran\xc3\xa7ais"
Chris@1464 363 if str_fr.respond_to?(:force_encoding)
Chris@1464 364 str_fr.force_encoding('UTF-8')
Chris@1464 365 end
Chris@1464 366 assert_equal str_fr, l(:general_lang_name)
Chris@1464 367 assert_equal 'ISO-8859-1', l(:general_csv_encoding)
Chris@1464 368 assert_equal ';', l(:general_csv_separator)
Chris@1464 369 assert_equal ',', l(:general_csv_decimal_separator)
Chris@1464 370 end
Chris@1464 371 end
Chris@1464 372 end