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.

imports_controller_test.rb 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 ImportsControllerTest < Redmine::ControllerTest
  20. fixtures :projects, :enabled_modules,
  21. :users, :email_addresses, :user_preferences,
  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. :custom_fields_projects,
  32. :custom_fields_trackers
  33. def setup
  34. User.current = nil
  35. @request.session[:user_id] = 2
  36. end
  37. def teardown
  38. Import.destroy_all
  39. end
  40. def test_new_should_display_the_upload_form
  41. get(:new, :params => {:type => 'IssueImport', :project_id => 'subproject1'})
  42. assert_response :success
  43. assert_select 'input[name=?]', 'file'
  44. assert_select 'input[name=?][type=?][value=?]', 'project_id', 'hidden', 'subproject1'
  45. end
  46. def test_create_should_save_the_file
  47. import = new_record(Import) do
  48. post(
  49. :create,
  50. :params => {
  51. :type => 'IssueImport',
  52. :file => uploaded_test_file('import_issues.csv', 'text/csv')
  53. }
  54. )
  55. assert_response 302
  56. end
  57. assert_equal 2, import.user_id
  58. assert_match /\A[0-9a-f]+\z/, import.filename
  59. assert import.file_exists?
  60. end
  61. def test_get_settings_should_display_settings_form
  62. import = generate_import
  63. get(:settings, :params => {:id => import.to_param})
  64. assert_response :success
  65. assert_select 'select[name=?]', 'import_settings[separator]'
  66. assert_select 'select[name=?]', 'import_settings[wrapper]'
  67. assert_select 'select[name=?]', 'import_settings[encoding]'
  68. assert_select 'select[name=?]', 'import_settings[date_format]'
  69. end
  70. def test_post_settings_should_update_settings
  71. import = generate_import
  72. post(
  73. :settings,
  74. :params => {
  75. :id => import.to_param,
  76. :import_settings => {
  77. :separator => ":",
  78. :wrapper => "|",
  79. :encoding => "UTF-8",
  80. :date_format => '%m/%d/%Y'
  81. }
  82. }
  83. )
  84. assert_redirected_to "/imports/#{import.to_param}/mapping"
  85. import.reload
  86. assert_equal ":", import.settings['separator']
  87. assert_equal "|", import.settings['wrapper']
  88. assert_equal "UTF-8", import.settings['encoding']
  89. assert_equal '%m/%d/%Y', import.settings['date_format']
  90. end
  91. def test_post_settings_should_update_total_items_count
  92. import = generate_import('import_iso8859-1.csv')
  93. post(
  94. :settings,
  95. :params => {
  96. :id => import.to_param,
  97. :import_settings => {
  98. :separator => ";",
  99. :wrapper => '"',
  100. :encoding => "ISO-8859-1"
  101. }
  102. }
  103. )
  104. assert_response 302
  105. import.reload
  106. assert_equal 2, import.total_items
  107. end
  108. def test_post_settings_with_wrong_encoding_should_display_error
  109. import = generate_import('import_iso8859-1.csv')
  110. post(
  111. :settings,
  112. :params => {
  113. :id => import.to_param,
  114. :import_settings => {
  115. :separator => ";",
  116. :wrapper => '"',
  117. :encoding => "UTF-8"
  118. }
  119. }
  120. )
  121. assert_response 200
  122. import.reload
  123. assert_nil import.total_items
  124. assert_select 'div#flash_error', /not a valid UTF-8 encoded file/
  125. end
  126. def test_post_settings_with_invalid_encoding_should_display_error
  127. import = generate_import('invalid-Shift_JIS.csv')
  128. post(
  129. :settings,
  130. :params => {
  131. :id => import.to_param,
  132. :import_settings => {
  133. :separator => ";",
  134. :wrapper => '"',
  135. :encoding => "Shift_JIS"
  136. }
  137. }
  138. )
  139. assert_response 200
  140. import.reload
  141. assert_nil import.total_items
  142. assert_select 'div#flash_error', /not a valid Shift_JIS encoded file/
  143. end
  144. def test_post_settings_with_mailformed_csv_should_display_error
  145. import = generate_import('unclosed_quoted_field.csv')
  146. post(
  147. :settings,
  148. :params => {
  149. :id => import.to_param,
  150. :import_settings => {
  151. :separator => ';',
  152. :wrapper => '"',
  153. :encoding => 'US-ASCII'
  154. }
  155. }
  156. )
  157. assert_response 200
  158. import.reload
  159. assert_nil import.total_items
  160. assert_select 'div#flash_error', /The file is not a CSV file or does not match the settings below \([[:print:]]+\)/
  161. end
  162. def test_get_mapping_should_display_mapping_form
  163. import = generate_import('import_iso8859-1.csv')
  164. import.settings = {'separator' => ";", 'wrapper' => '"', 'encoding' => "ISO-8859-1"}
  165. import.save!
  166. get(:mapping, :params => {:id => import.to_param})
  167. assert_response :success
  168. assert_select 'select[name=?]', 'import_settings[mapping][subject]' do
  169. assert_select 'option', 4
  170. assert_select 'option[value="0"]', :text => 'column A'
  171. end
  172. assert_select 'table.sample-data' do
  173. assert_select 'tr', 3
  174. assert_select 'td', 9
  175. end
  176. end
  177. def test_get_mapping_should_auto_map_fields_by_internal_field_name_or_by_label
  178. import = generate_import('import_issues_auto_mapping.csv')
  179. import.settings = {'separator' => ';', 'wrapper'=> '"', 'encoding' => 'ISO-8859-1'}
  180. import.save!
  181. get(:mapping, :params => {:id => import.to_param})
  182. assert_response :success
  183. # 'subject' should be auto selected because
  184. # - 'Subject' exists in the import file
  185. # - mapping is case insensitive
  186. assert_select 'select[name=?]', 'import_settings[mapping][subject]' do
  187. assert_select 'option[value="1"][selected="selected"]', :text => 'Subject'
  188. end
  189. # 'estimated_hours' should be auto selected because
  190. # - 'estimated_hours' exists in the import file
  191. assert_select 'select[name=?]', 'import_settings[mapping][estimated_hours]' do
  192. assert_select 'option[value="10"][selected="selected"]', :text => 'estimated_hours'
  193. end
  194. # 'fixed_version' should be auto selected because
  195. # - the translation 'Target version' exists in the import file
  196. assert_select 'select[name=?]', 'import_settings[mapping][fixed_version]' do
  197. assert_select 'option[value="7"][selected="selected"]', :text => 'target version'
  198. end
  199. # 'assigned_to' should not be auto selected because
  200. # - 'assigned_to' does not exist in the import file
  201. assert_select 'select[name=?]', 'import_settings[mapping][assigned_to]' do
  202. assert_select 'option[selected="selected"]', 0
  203. end
  204. # Custom field 'Float field' should be auto selected because
  205. # - the internal field name ('cf_6') exists in the import file
  206. assert_select 'select[name=?]', 'import_settings[mapping][cf_6]' do
  207. assert_select 'option[value="14"][selected="selected"]', :text => 'cf_6'
  208. end
  209. # Custom field 'Database' should be auto selected because
  210. # - field name 'database' exists in the import file
  211. # - mapping is case insensitive
  212. assert_select 'select[name=?]', 'import_settings[mapping][cf_1]' do
  213. assert_select 'option[value="13"][selected="selected"]', :text => 'database'
  214. end
  215. end
  216. def test_post_mapping_should_update_mapping
  217. import = generate_import('import_iso8859-1.csv')
  218. post(
  219. :mapping,
  220. :params => {
  221. :id => import.to_param,
  222. :import_settings => {
  223. :mapping => {
  224. :project_id => '1',
  225. :tracker_id => '2',
  226. :subject => '0'
  227. }
  228. }
  229. }
  230. )
  231. assert_redirected_to "/imports/#{import.to_param}/run"
  232. import.reload
  233. mapping = import.settings['mapping']
  234. assert mapping
  235. assert_equal '1', mapping['project_id']
  236. assert_equal '2', mapping['tracker_id']
  237. assert_equal '0', mapping['subject']
  238. end
  239. def test_get_mapping_time_entry
  240. Role.find(1).add_permission! :log_time_for_other_users
  241. import = generate_time_entry_import
  242. import.settings = {'separator' => ";", 'wrapper' => '"', 'encoding' => "ISO-8859-1"}
  243. import.save!
  244. get(:mapping, :params => {:id => import.to_param})
  245. assert_response :success
  246. # Assert auto mapped fields
  247. assert_select 'select[name=?]', 'import_settings[mapping][activity]' do
  248. assert_select 'option[value="5"][selected="selected"]', :text => 'activity'
  249. end
  250. # 'user' should be mapped to column 'user' from import file
  251. # and not to current user because the auto map has priority
  252. assert_select 'select[name=?]', 'import_settings[mapping][user]' do
  253. assert_select 'option[value="7"][selected="selected"]', :text => 'user'
  254. end
  255. assert_select 'select[name=?]', 'import_settings[mapping][cf_10]' do
  256. assert_select 'option[value="6"][selected="selected"]', :text => 'overtime'
  257. end
  258. end
  259. def test_get_mapping_time_entry_for_user_with_log_time_for_other_users_permission
  260. Role.find(1).add_permission! :log_time_for_other_users
  261. import = generate_time_entry_import
  262. import.settings = {
  263. 'separator' => ";", 'wrapper' => '"', 'encoding' => "ISO-8859-1",
  264. # Do not auto map user in order to allow current user to be auto selected
  265. 'mapping' => {'user' => nil}
  266. }
  267. import.save!
  268. get(:mapping, :params => {:id => import.to_param})
  269. # 'user' field should be available because User#2 has both
  270. # 'import_time_entries' and 'log_time_for_other_users' permissions
  271. assert_select 'select[name=?]', 'import_settings[mapping][user]' do
  272. # Current user should be the default value if there is not auto map present
  273. assert_select 'option[value="value:2"][selected]', :text => User.find(2).name
  274. assert_select 'option[value="value:3"]', :text => User.find(3).name
  275. end
  276. end
  277. def test_get_mapping_time_entry_for_user_without_log_time_for_other_users_permission
  278. import = generate_time_entry_import
  279. import.settings = {'separator' => ";", 'wrapper' => '"', 'encoding' => "ISO-8859-1"}
  280. import.save!
  281. get(:mapping, :params => {:id => import.to_param})
  282. assert_response :success
  283. assert_select 'select[name=?]', 'import_settings[mapping][user_id]', 0
  284. end
  285. def test_get_run
  286. import = generate_import_with_mapping
  287. get(:run, :params => {:id => import})
  288. assert_response :success
  289. assert_select '#import-progress'
  290. end
  291. def test_post_run_should_import_the_file
  292. import = generate_import_with_mapping
  293. assert_difference 'Issue.count', 3 do
  294. post(:run, :params => {:id => import})
  295. assert_redirected_to "/imports/#{import.to_param}"
  296. end
  297. import.reload
  298. assert_equal true, import.finished
  299. assert_equal 3, import.items.count
  300. issues = Issue.order(:id => :desc).limit(3).to_a
  301. assert_equal ["Child of existing issue", "Child 1", "First"], issues.map(&:subject)
  302. end
  303. def test_post_run_should_import_max_items_and_resume
  304. ImportsController.any_instance.stubs(:max_items_per_request).returns(2)
  305. import = generate_import_with_mapping
  306. assert_difference 'Issue.count', 2 do
  307. post(:run, :params => {:id => import})
  308. assert_redirected_to "/imports/#{import.to_param}/run"
  309. end
  310. assert_difference 'Issue.count', 1 do
  311. post(:run, :params => {:id => import})
  312. assert_redirected_to "/imports/#{import.to_param}"
  313. end
  314. issues = Issue.order(:id => :desc).limit(3).to_a
  315. assert_equal ["Child of existing issue", "Child 1", "First"], issues.map(&:subject)
  316. end
  317. def test_post_run_with_notifications
  318. import = generate_import
  319. post(
  320. :settings,
  321. :params => {
  322. :id => import,
  323. :import_settings => {
  324. :separator => ';',
  325. :wrapper => '"',
  326. :encoding => 'ISO-8859-1',
  327. :notifications => '1',
  328. :mapping => {
  329. :project_id => '1',
  330. :tracker => '13',
  331. :subject => '1',
  332. :assigned_to => '11',
  333. }
  334. }
  335. }
  336. )
  337. ActionMailer::Base.deliveries.clear
  338. assert_difference 'Issue.count', 3 do
  339. post(:run, :params => {:id => import,})
  340. assert_response :found
  341. end
  342. actual_email_count = ActionMailer::Base.deliveries.size
  343. assert_not_equal 0, actual_email_count
  344. import.reload
  345. issue_ids = import.items.collect(&:obj_id)
  346. expected_email_count =
  347. Issue.where(:id => issue_ids).inject(0) do |sum, issue|
  348. sum + (issue.notified_users | issue.notified_watchers).size
  349. end
  350. assert_equal expected_email_count, actual_email_count
  351. end
  352. def test_show_without_errors
  353. import = generate_import_with_mapping
  354. import.run
  355. assert_equal 0, import.unsaved_items.count
  356. get(:show, :params => {:id => import.to_param})
  357. assert_response :success
  358. assert_select 'ul#saved-items'
  359. assert_select 'ul#saved-items li', import.saved_items.count
  360. assert_select 'table#unsaved-items', 0
  361. end
  362. def test_show_with_errors_should_show_unsaved_items
  363. import = generate_import_with_mapping
  364. import.mapping['subject'] = 20
  365. import.run
  366. assert_not_equal 0, import.unsaved_items.count
  367. get(:show, :params => {:id => import.to_param})
  368. assert_response :success
  369. assert_select 'table#unsaved-items'
  370. assert_select 'table#unsaved-items tbody tr', import.unsaved_items.count
  371. end
  372. end