annotate .svn/pristine/20/20cf4d4753525274da375856e4def6acfb536bf9.svn-base @ 1519:afce8026aaeb redmine-2.4-integration

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