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 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. # frozen_string_literal: true
  2. # Redmine - project management software
  3. # Copyright (C) 2006- 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_relative '../test_helper'
  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['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['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['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. def test_imports_timelogs_for_issues_in_other_project
  137. import = generate_import
  138. import.settings = {
  139. 'separator' => ';', 'wrapper' => '"', 'encoding' => 'UTF-8',
  140. 'mapping' => {
  141. 'project_id' => '3',
  142. 'activity' => 'value:10',
  143. 'issue_id' => '1',
  144. 'spent_on' => '2',
  145. 'hours' => '3',
  146. 'comments' => '4',
  147. 'user' => '7'
  148. }
  149. }
  150. import.save!
  151. first, second, third, fourth = new_records(TimeEntry, 4) {import.run}
  152. assert_equal 3, first.project_id
  153. assert_equal 3, second.project_id
  154. assert_equal 1, third.project_id
  155. assert_equal 1, fourth.project_id
  156. end
  157. def test_imports_custom_field_with_user_format
  158. cf = TimeEntryCustomField.create! name: 'User Field', field_format: 'user'
  159. import = generate_import
  160. import.settings = {
  161. 'separator' => ';', 'wrapper' => '"', 'encoding' => 'UTF-8',
  162. 'mapping' => {
  163. 'project_id' => '1',
  164. 'activity' => 'value:10',
  165. 'issue_id' => '1',
  166. 'spent_on' => '2',
  167. 'hours' => '3',
  168. 'comments' => '4',
  169. 'user' => '7',
  170. "cf_#{cf.id}" => '7'
  171. }
  172. }
  173. import.save!
  174. first, second, third, fourth = new_records(TimeEntry, 4) {import.run}
  175. jsmith = User.find_by_login 'jsmith'
  176. dlopper = User.find_by_login 'dlopper'
  177. assert_equal dlopper.id, third.custom_values.where(custom_field: cf.id).first.value.to_i
  178. assert_equal jsmith.id, fourth.custom_values.where(custom_field: cf.id).first.value.to_i
  179. assert_equal jsmith.id, first.custom_values.where(custom_field: cf.id).first.value.to_i
  180. assert_equal jsmith.id, second.custom_values.where(custom_field: cf.id).first.value.to_i
  181. end
  182. protected
  183. def generate_import(fixture_name='import_time_entries.csv')
  184. import = TimeEntryImport.new
  185. import.user_id = 2
  186. import.file = uploaded_test_file(fixture_name, 'text/csv')
  187. import.save!
  188. import
  189. end
  190. def generate_import_with_mapping(fixture_name='import_time_entries.csv')
  191. import = generate_import(fixture_name)
  192. import.settings = {
  193. 'separator' => ';', 'wrapper' => '"', 'encoding' => 'UTF-8',
  194. 'mapping' => {
  195. 'project_id' => '1',
  196. 'activity' => 'value:10',
  197. 'issue_id' => '1',
  198. 'spent_on' => '2',
  199. 'hours' => '3',
  200. 'comments' => '4',
  201. 'user' => '7'
  202. }
  203. }
  204. import.save!
  205. import
  206. end
  207. end