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

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