timelog_report_test.rb 14.3 KB
Newer Older
1
# frozen_string_literal: true
2

jplang's avatar
jplang committed
3
# Redmine - project management software
jplang's avatar
jplang committed
4
# Copyright (C) 2006-2017  Jean-Philippe Lang
jplang's avatar
jplang committed
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

20
require File.expand_path('../../test_helper', __FILE__)
21

22
class TimelogReportTest < Redmine::ControllerTest
23 24
  tests TimelogController

25
  fixtures :projects, :enabled_modules, :roles, :members, :member_roles,
26
           :email_addresses,
27
           :issues, :time_entries, :users, :trackers, :enumerations,
28 29 30
           :issue_statuses, :custom_fields, :custom_values,
           :projects_trackers, :custom_fields_trackers,
           :custom_fields_projects
31

32 33
  include Redmine::I18n

34 35 36 37
  def setup
    Setting.default_language = "en"
  end

38
  def test_report_at_project_level
39
    get :report, :params => {:project_id => 'ecookbook'}
40
    assert_response :success
41 42 43 44 45 46 47 48 49 50 51 52

    # query form
    assert_select 'form#query_form' do
      assert_select 'div#query_form_with_buttons.hide-when-print' do
        assert_select 'div#query_form_content' do
          assert_select 'fieldset#filters.collapsible'
          assert_select 'fieldset#options'
        end
        assert_select 'p.buttons'
      end
    end

53
    assert_select 'form#query_form[action=?]', '/projects/ecookbook/time_entries/report'
54
  end
55

56 57 58
  def test_report_all_projects
    get :report
    assert_response :success
59
    assert_select 'form#query_form[action=?]', '/time_entries/report'
60
  end
61

62 63 64 65 66 67 68 69
  def test_report_all_projects_denied
    r = Role.anonymous
    r.permissions.delete(:view_time_entries)
    r.permissions_will_change!
    r.save
    get :report
    assert_redirected_to '/login?back_url=http%3A%2F%2Ftest.host%2Ftime_entries%2Freport'
  end
70

71
  def test_report_all_projects_one_criteria
72
    get :report, :params => {:columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']}
73
    assert_response :success
74
    assert_select 'tr.total td:last', :text => '8.65'
75
    assert_select 'tr td.name a[href=?]', '/projects/ecookbook', :text => 'eCookbook'
76 77 78
  end

  def test_report_all_time
79
    get :report, :params => {:project_id => 1, :criteria => ['project', 'issue']}
80
    assert_response :success
81
    assert_select 'tr.total td:last', :text => '162.90'
82 83 84
  end

  def test_report_all_time_by_day
85
    get :report, :params => {:project_id => 1, :criteria => ['project', 'issue'], :columns => 'day'}
86
    assert_response :success
87
    assert_select 'tr.total td:last', :text => '162.90'
88
    assert_select 'th', :text => '2007-03-12'
89
  end
90

91
  def test_report_one_criteria
92
    get :report, :params => {:project_id => 1, :columns => 'week', :from => "2007-04-01", :to => "2007-04-30", :criteria => ['project']}
93
    assert_response :success
94
    assert_select 'tr.total td:last', :text => '8.65'
95
  end
96

97
  def test_report_two_criteria
98
    get :report, :params => {:project_id => 1, :columns => 'month', :from => "2007-01-01", :to => "2007-12-31", :criteria => ["user", "activity"]}
99
    assert_response :success
100
    assert_select 'tr.total td:last', :text => '162.90'
101
  end
102

103 104 105 106 107 108 109 110 111 112 113 114 115 116
  def test_report_should_show_locked_users
    @request.session[:user_id] = 1

    user = User.find(2)
    user.status = User::STATUS_LOCKED
    user.save

    get :report, :params => {:project_id => 1, :columns => 'month', :criteria => ["user", "activity"]}
    assert_response :success

    assert_select 'td.name a.user.active[href=?]', '/users/1', 1, :text => 'Redmine Admin'
    assert_select 'td.name a.user.locked[href=?]', '/users/2', 1, :text => 'John Smith'
  end

117
  def test_report_custom_field_criteria_with_multiple_values_on_single_value_custom_field_should_not_fail
118 119 120 121 122
    field = TimeEntryCustomField.create!(:name => 'multi', :field_format => 'list', :possible_values => ['value1', 'value2'])
    entry = TimeEntry.create!(:project => Project.find(1), :hours => 1, :activity_id => 10, :user => User.find(2), :spent_on => Date.today)
    CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value1')
    CustomValue.create!(:customized => entry, :custom_field => field, :value => 'value2')

123
    get :report, :params => {:project_id => 1, :columns => 'day', :criteria => ["cf_#{field.id}"]}
124 125 126
    assert_response :success
  end

127 128 129 130
  def test_report_multiple_values_custom_fields_should_not_be_proposed
    TimeEntryCustomField.create!(:name => 'Single', :field_format => 'list', :possible_values => ['value1', 'value2'])
    TimeEntryCustomField.create!(:name => 'Multi', :field_format => 'list', :multiple => true, :possible_values => ['value1', 'value2'])

131
    get :report, :params => {:project_id => 1}
132 133 134 135 136 137 138
    assert_response :success
    assert_select 'select[name=?]', 'criteria[]' do
      assert_select 'option', :text => 'Single'
      assert_select 'option', :text => 'Multi', :count => 0
    end
  end

139 140 141 142 143 144 145 146 147 148 149 150
  def test_hidden_custom_fields_should_not_be_proposed
    TimeEntryCustomField.create!(name: 'shown', field_format: 'list', possible_values: ['value1', 'value2'], visible: true)
    TimeEntryCustomField.create!(name: 'Hidden', field_format: 'list', possible_values: ['value1', 'value2'], visible: false)

    get :report, :params => {:project_id => 1}
    assert_response :success
    assert_select 'select[name=?]', 'criteria[]' do
      assert_select 'option', :text => 'Shown'
      assert_select 'option', :text => 'Hidden', :count => 0
    end
  end

151
  def test_report_one_day
152
    get :report, :params => {:project_id => 1, :columns => 'day', :from => "2007-03-23", :to => "2007-03-23", :criteria => ["user", "activity"]}
153
    assert_response :success
154
    assert_select 'tr.total td:last', :text => '4.25'
155
  end
156

157 158 159 160 161 162 163
  def test_report_by_week_should_use_commercial_year
    TimeEntry.delete_all
    TimeEntry.generate!(:hours => '2', :spent_on => '2009-12-25') # 2009-52
    TimeEntry.generate!(:hours => '4', :spent_on => '2009-12-31') # 2009-53
    TimeEntry.generate!(:hours => '8', :spent_on => '2010-01-01') # 2009-53
    TimeEntry.generate!(:hours => '16', :spent_on => '2010-01-05') # 2010-1

164
    get :report, :params => {:columns => 'week', :from => "2009-12-25", :to => "2010-01-05", :criteria => ["project"]}
165 166 167 168 169 170 171
    assert_response :success

    assert_select '#time-report thead tr' do
      assert_select 'th:nth-child(1)', :text => 'Project'
      assert_select 'th:nth-child(2)', :text => '2009-52'
      assert_select 'th:nth-child(3)', :text => '2009-53'
      assert_select 'th:nth-child(4)', :text => '2010-1'
172
      assert_select 'th:nth-child(5)', :text => 'Total time'
173 174 175 176 177 178 179 180 181 182
    end
    assert_select '#time-report tbody tr' do
      assert_select 'td:nth-child(1)', :text => 'eCookbook'
      assert_select 'td:nth-child(2)', :text => '2.00'
      assert_select 'td:nth-child(3)', :text => '12.00'
      assert_select 'td:nth-child(4)', :text => '16.00'
      assert_select 'td:nth-child(5)', :text => '30.00' # Total
    end
  end

183 184 185 186 187 188 189 190 191 192 193 194
  def test_report_should_propose_association_custom_fields
    get :report
    assert_response :success

    assert_select 'select[name=?]', 'criteria[]' do
      assert_select 'option[value=cf_1]', {:text => 'Database'}, 'Issue custom field not found'
      assert_select 'option[value=cf_3]', {:text => 'Development status'}, 'Project custom field not found'
      assert_select 'option[value=cf_7]', {:text => 'Billable'}, 'TimeEntryActivity custom field not found'
    end
  end

  def test_report_with_association_custom_fields
195
    get :report, :params => {:criteria => ['cf_1', 'cf_3', 'cf_7']}
196
    assert_response :success
197 198

    assert_select 'tr.total td:last', :text => '162.90'
199 200 201 202 203 204

    # Custom fields columns
    assert_select 'th', :text => 'Database'
    assert_select 'th', :text => 'Development status'
    assert_select 'th', :text => 'Billable'

205
    # Custom field row
206 207 208 209
    assert_select 'tr' do
      assert_select 'td', :text => 'MySQL'
      assert_select 'td.hours', :text => '1.00'
    end
210
  end
211

212
  def test_report_one_criteria_no_result
213
    get :report, :params => {:project_id => 1, :columns => 'week', :from => "1998-04-01", :to => "1998-04-30", :criteria => ['project']}
214
    assert_response :success
215 216

    assert_select '.nodata'
217
  end
218

219
  def test_report_status_criterion
220
    get :report, :params => {:project_id => 1, :criteria => ['status']}
221
    assert_response :success
222

223 224
    assert_select 'th', :text => 'Status'
    assert_select 'td', :text => 'New'
225 226
  end

227
  def test_report_all_projects_csv_export
228 229 230 231 232 233 234
    get :report, :params => {
      :columns => 'month',
      :from => "2007-01-01",
      :to => "2007-06-30",
      :criteria => ["project", "user", "activity"],
      :format => "csv"
    }
235
    assert_response :success
jplang's avatar
jplang committed
236
    assert_equal 'text/csv; header=present', @response.content_type
237 238
    lines = @response.body.chomp.split("\n")
    # Headers
239
    assert_equal 'Project,User,Activity,2007-3,2007-4,Total time', lines.first
240
    # Total row
241
    assert_equal 'Total time,"","",154.25,8.65,162.90', lines.last
242
  end
243

244
  def test_report_csv_export
245 246 247 248 249 250 251 252
    get :report, :params => {
      :project_id => 1,
      :columns => 'month',
      :from => "2007-01-01",
      :to => "2007-06-30",
      :criteria => ["project", "user", "activity"],
      :format => "csv"
    }
253
    assert_response :success
jplang's avatar
jplang committed
254
    assert_equal 'text/csv; header=present', @response.content_type
255 256
    lines = @response.body.chomp.split("\n")
    # Headers
257
    assert_equal 'Project,User,Activity,2007-3,2007-4,Total time', lines.first
258
    # Total row
259
    assert_equal 'Total time,"","",154.25,8.65,162.90', lines.last
260
  end
261 262

  def test_csv_big_5
263
    str_big5  = (+"\xa4@\xa4\xeb").force_encoding('Big5')
264
    user = User.find_by_id(3)
265
    user.firstname = "一月"
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
    user.lastname  = "test-lastname"
    assert user.save
    comments = "test_csv_big_5"
    te1 = TimeEntry.create(:spent_on => '2011-11-11',
                           :hours    => 7.3,
                           :project  => Project.find(1),
                           :user     => user,
                           :activity => TimeEntryActivity.find_by_name('Design'),
                           :comments => comments)

    te2 = TimeEntry.find_by_comments(comments)
    assert_not_nil te2
    assert_equal 7.3, te2.hours
    assert_equal 3, te2.user_id

281
    with_settings :default_language => "zh-TW" do
282 283 284 285 286 287 288 289
      get :report, :params => {
        :project_id => 1,
        :columns => 'day',
        :from => "2011-11-11",
        :to => "2011-11-11",
        :criteria => ["user"],
        :format => "csv"
      }
290
    end
291
    assert_response :success
jplang's avatar
jplang committed
292
    assert_equal 'text/csv; header=present', @response.content_type
293
    lines = @response.body.chomp.split("\n")
294
    # Headers
295 296
    s1 = (+"\xa5\xce\xa4\xe1,2011-11-11,\xa4u\xae\xc9\xc1`\xadp").force_encoding('Big5')
    s2 = (+"\xa4u\xae\xc9\xc1`\xadp").force_encoding('Big5')
297 298 299
    assert_equal s1, lines.first
    # Total row
    assert_equal "#{str_big5} #{user.lastname},7.30,7.30", lines[1]
300
    assert_equal "#{s2},7.30,7.30", lines[2]
301

302
    assert_equal 'Chinese/Traditional (繁體中文)', l(:general_lang_name)
303 304 305
    assert_equal 'Big5', l(:general_csv_encoding)
    assert_equal ',', l(:general_csv_separator)
    assert_equal '.', l(:general_csv_decimal_separator)
306
  end
307 308 309

  def test_csv_cannot_convert_should_be_replaced_big_5
    user = User.find_by_id(3)
310
    user.firstname = "以内"
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
    user.lastname  = "test-lastname"
    assert user.save
    comments = "test_replaced"
    te1 = TimeEntry.create(:spent_on => '2011-11-11',
                           :hours    => 7.3,
                           :project  => Project.find(1),
                           :user     => user,
                           :activity => TimeEntryActivity.find_by_name('Design'),
                           :comments => comments)

    te2 = TimeEntry.find_by_comments(comments)
    assert_not_nil te2
    assert_equal 7.3, te2.hours
    assert_equal 3, te2.user_id

326
    with_settings :default_language => "zh-TW" do
327 328 329 330 331 332 333 334
      get :report, :params => {
        :project_id => 1,
        :columns => 'day',
        :from => "2011-11-11",
        :to => "2011-11-11",
        :criteria => ["user"],
        :format => "csv"
      }
335
    end
336
    assert_response :success
jplang's avatar
jplang committed
337
    assert_equal 'text/csv; header=present', @response.content_type
338
    lines = @response.body.chomp.split("\n")
339
    # Headers
340
    s1 = (+"\xa5\xce\xa4\xe1,2011-11-11,\xa4u\xae\xc9\xc1`\xadp").force_encoding('Big5')
341 342
    assert_equal s1, lines.first
    # Total row
343
    s2 = (+"\xa5H?").force_encoding('Big5')
344 345
    assert_equal "#{s2} #{user.lastname},7.30,7.30", lines[1]
  end
346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362

  def test_csv_fr
    with_settings :default_language => "fr" do
      str1  = "test_csv_fr"
      user = User.find_by_id(3)
      te1 = TimeEntry.create(:spent_on => '2011-11-11',
                             :hours    => 7.3,
                             :project  => Project.find(1),
                             :user     => user,
                             :activity => TimeEntryActivity.find_by_name('Design'),
                             :comments => str1)

      te2 = TimeEntry.find_by_comments(str1)
      assert_not_nil te2
      assert_equal 7.3, te2.hours
      assert_equal 3, te2.user_id

363 364 365 366 367 368 369 370
      get :report, :params => {
        :project_id => 1,
        :columns => 'day',
        :from => "2011-11-11",
        :to => "2011-11-11",
        :criteria => ["user"],
        :format => "csv"
      }
371
      assert_response :success
jplang's avatar
jplang committed
372
      assert_equal 'text/csv; header=present', @response.content_type
373
      lines = @response.body.chomp.split("\n")
374
      # Headers
375 376
      s1 = (+"Utilisateur;2011-11-11;Temps total").force_encoding('ISO-8859-1')
      s2 = (+"Temps total").force_encoding('ISO-8859-1')
377 378 379 380 381
      assert_equal s1, lines.first
      # Total row
      assert_equal "#{user.firstname} #{user.lastname};7,30;7,30", lines[1]
      assert_equal "#{s2};7,30;7,30", lines[2]

382
      assert_equal 'French (Français)', l(:general_lang_name)
383 384 385 386 387
      assert_equal 'ISO-8859-1', l(:general_csv_encoding)
      assert_equal ';', l(:general_csv_separator)
      assert_equal ',', l(:general_csv_decimal_separator)
    end
  end
388
end