annotate .svn/pristine/b6/b648e7261c6cf5a90c411f73fe039c9171cf83b0.svn-base @ 1295:622f24f53b42 redmine-2.3

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