You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

time_entry_import_test.rb 6.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006-2020 Jean-Philippe Lang
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. require File.expand_path('../../test_helper', __FILE__)
  19. class TimeEntryImportTest < ActiveSupport::TestCase
  20. fixtures :projects, :enabled_modules,
  21. :users, :email_addresses,
  22. :roles, :members, :member_roles,
  23. :issues, :issue_statuses,
  24. :trackers, :projects_trackers,
  25. :versions,
  26. :issue_categories,
  27. :enumerations,
  28. :workflows,
  29. :custom_fields,
  30. :custom_values
  31. include Redmine::I18n
  32. def setup
  33. set_language_if_valid 'en'
  34. User.current = nil
  35. end
  36. def test_authorized
  37. assert TimeEntryImport.authorized?(User.find(1)) # admins
  38. assert TimeEntryImport.authorized?(User.find(2)) # has log_time permission
  39. assert !TimeEntryImport.authorized?(User.find(6)) # anonymous does not have log_time permission
  40. end
  41. def test_maps_issue_id
  42. import = generate_import_with_mapping
  43. first, second, third, fourth = new_records(TimeEntry, 4) { import.run }
  44. assert_nil first.issue_id
  45. assert_nil second.issue_id
  46. assert_equal 1, third.issue_id
  47. assert_equal 2, fourth.issue_id
  48. end
  49. def test_maps_date
  50. import = generate_import_with_mapping
  51. first, second, third, fourth = new_records(TimeEntry, 4) { import.run }
  52. assert_equal Date.new(2020, 1, 1), first.spent_on
  53. assert_equal Date.new(2020, 1, 2), second.spent_on
  54. assert_equal Date.new(2020, 1, 3), third.spent_on
  55. assert_equal Date.new(2020, 1, 4), fourth.spent_on
  56. end
  57. def test_maps_hours
  58. import = generate_import_with_mapping
  59. first, second, third, fourth = new_records(TimeEntry, 4) { import.run }
  60. assert_equal 1, first.hours
  61. assert_equal 2, second.hours
  62. assert_equal 3, third.hours
  63. assert_equal 4, fourth.hours
  64. end
  65. def test_maps_comments
  66. import = generate_import_with_mapping
  67. first, second, third, fourth = new_records(TimeEntry, 4) { import.run }
  68. assert_equal 'Some Design', first.comments
  69. assert_equal 'Some Development', second.comments
  70. assert_equal 'Some QA', third.comments
  71. assert_equal 'Some Inactivity', fourth.comments
  72. end
  73. def test_maps_activity_to_column_value
  74. import = generate_import_with_mapping
  75. import.mapping.merge!('activity' => '5')
  76. import.save!
  77. # N.B. last row is not imported due to the usage of a disabled activity
  78. first, second, third = new_records(TimeEntry, 3) { import.run }
  79. assert_equal 9, first.activity_id
  80. assert_equal 10, second.activity_id
  81. assert_equal 11, third.activity_id
  82. last = import.items.last
  83. assert_equal 'Activity cannot be blank', last.message
  84. assert_nil last.obj_id
  85. end
  86. def test_maps_activity_to_fixed_value
  87. import = generate_import_with_mapping
  88. first, second, third, fourth = new_records(TimeEntry, 4) { import.run }
  89. assert_equal 10, first.activity_id
  90. assert_equal 10, second.activity_id
  91. assert_equal 10, third.activity_id
  92. assert_equal 10, fourth.activity_id
  93. end
  94. def test_maps_custom_fields
  95. overtime_cf = CustomField.find(10)
  96. import = generate_import_with_mapping
  97. import.mapping.merge!('cf_10' => '6')
  98. import.save!
  99. first, second, third, fourth = new_records(TimeEntry, 4) { import.run }
  100. assert_equal '1', first.custom_field_value(overtime_cf)
  101. assert_equal '1', second.custom_field_value(overtime_cf)
  102. assert_equal '0', third.custom_field_value(overtime_cf)
  103. assert_equal '0', fourth.custom_field_value(overtime_cf)
  104. end
  105. def test_maps_user_id_for_user_with_permissions
  106. Role.find_by_name('Manager').add_permission! :log_time_for_other_users
  107. import = generate_import_with_mapping
  108. first, second, third, fourth = new_records(TimeEntry, 4) { import.run }
  109. assert_equal 2, first.user_id
  110. assert_equal 2, second.user_id
  111. assert_equal 3, third.user_id
  112. assert_equal 2, fourth.user_id
  113. end
  114. def test_maps_user_to_column_value
  115. Role.find_by_name('Manager').add_permission! :log_time_for_other_users
  116. import = generate_import_with_mapping
  117. import.mapping.merge!('user' => 'value:3')
  118. import.save!
  119. first, second, third, fourth = new_records(TimeEntry, 4) { import.run }
  120. assert_equal 3, first.user_id
  121. assert_equal 3, second.user_id
  122. assert_equal 3, third.user_id
  123. assert_equal 3, fourth.user_id
  124. end
  125. def test_maps_user_id_for_user_without_permissions
  126. # User 2 doesn't have log_time_for_other_users permission
  127. User.current = User.find(2)
  128. import = generate_import_with_mapping
  129. first, second, third, fourth = new_records(TimeEntry, 4) { import.run }
  130. assert_equal 2, first.user_id
  131. assert_equal 2, second.user_id
  132. # user_id value from CSV should be ignored
  133. assert_equal 2, third.user_id
  134. assert_equal 2, fourth.user_id
  135. end
  136. protected
  137. def generate_import(fixture_name='import_time_entries.csv')
  138. import = TimeEntryImport.new
  139. import.user_id = 2
  140. import.file = uploaded_test_file(fixture_name, 'text/csv')
  141. import.save!
  142. import
  143. end
  144. def generate_import_with_mapping(fixture_name='import_time_entries.csv')
  145. import = generate_import(fixture_name)
  146. import.settings = {
  147. 'separator' => ';', 'wrapper' => '"', 'encoding' => 'UTF-8',
  148. 'mapping' => {
  149. 'project_id' => '1',
  150. 'activity' => 'value:10',
  151. 'issue_id' => '1',
  152. 'spent_on' => '2',
  153. 'hours' => '3',
  154. 'comments' => '4',
  155. 'user' => '7'
  156. }
  157. }
  158. import.save!
  159. import
  160. end
  161. end