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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2016 Jean-Philippe Lang
  3. #
  4. # This program is free software; you can redistribute it and/or
  5. # modify it under the terms of the GNU General Public License
  6. # as published by the Free Software Foundation; either version 2
  7. # of the License, or (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. require File.expand_path('../../test_helper', __FILE__)
  18. class ImportsControllerTest < Redmine::ControllerTest
  19. fixtures :projects, :enabled_modules,
  20. :users, :email_addresses,
  21. :roles, :members, :member_roles,
  22. :issues, :issue_statuses,
  23. :trackers, :projects_trackers,
  24. :versions,
  25. :issue_categories,
  26. :enumerations,
  27. :workflows,
  28. :custom_fields,
  29. :custom_values,
  30. :custom_fields_projects,
  31. :custom_fields_trackers
  32. def setup
  33. User.current = nil
  34. @request.session[:user_id] = 2
  35. end
  36. def teardown
  37. Import.destroy_all
  38. end
  39. def test_new_should_display_the_upload_form
  40. get :new
  41. assert_response :success
  42. assert_select 'input[name=?]', 'file'
  43. end
  44. def test_create_should_save_the_file
  45. import = new_record(Import) do
  46. post :create, :file => uploaded_test_file('import_issues.csv', 'text/csv')
  47. assert_response 302
  48. end
  49. assert_equal 2, import.user_id
  50. assert_match /\A[0-9a-f]+\z/, import.filename
  51. assert import.file_exists?
  52. end
  53. def test_get_settings_should_display_settings_form
  54. import = generate_import
  55. get :settings, :id => import.to_param
  56. assert_response :success
  57. assert_select 'select[name=?]', 'import_settings[separator]'
  58. assert_select 'select[name=?]', 'import_settings[wrapper]'
  59. assert_select 'select[name=?]', 'import_settings[encoding]'
  60. assert_select 'select[name=?]', 'import_settings[date_format]'
  61. end
  62. def test_post_settings_should_update_settings
  63. import = generate_import
  64. post :settings, :id => import.to_param,
  65. :import_settings => {:separator => ":", :wrapper => "|", :encoding => "UTF-8", :date_format => '%m/%d/%Y'}
  66. assert_redirected_to "/imports/#{import.to_param}/mapping"
  67. import.reload
  68. assert_equal ":", import.settings['separator']
  69. assert_equal "|", import.settings['wrapper']
  70. assert_equal "UTF-8", import.settings['encoding']
  71. assert_equal '%m/%d/%Y', import.settings['date_format']
  72. end
  73. def test_post_settings_should_update_total_items_count
  74. import = generate_import('import_iso8859-1.csv')
  75. post :settings, :id => import.to_param,
  76. :import_settings => {:separator => ";", :wrapper => '"', :encoding => "ISO-8859-1"}
  77. assert_response 302
  78. import.reload
  79. assert_equal 2, import.total_items
  80. end
  81. def test_post_settings_with_wrong_encoding_should_display_error
  82. import = generate_import('import_iso8859-1.csv')
  83. post :settings, :id => import.to_param,
  84. :import_settings => {:separator => ";", :wrapper => '"', :encoding => "UTF-8"}
  85. assert_response 200
  86. import.reload
  87. assert_nil import.total_items
  88. assert_select 'div#flash_error', /not a valid UTF-8 encoded file/
  89. end
  90. def test_get_mapping_should_display_mapping_form
  91. import = generate_import('import_iso8859-1.csv')
  92. import.settings = {'separator' => ";", 'wrapper' => '"', 'encoding' => "ISO-8859-1"}
  93. import.save!
  94. get :mapping, :id => import.to_param
  95. assert_response :success
  96. assert_select 'select[name=?]', 'import_settings[mapping][subject]' do
  97. assert_select 'option', 4
  98. assert_select 'option[value="0"]', :text => 'column A'
  99. end
  100. assert_select 'table.sample-data' do
  101. assert_select 'tr', 3
  102. assert_select 'td', 9
  103. end
  104. end
  105. def test_post_mapping_should_update_mapping
  106. import = generate_import('import_iso8859-1.csv')
  107. post :mapping, :id => import.to_param,
  108. :import_settings => {:mapping => {:project_id => '1', :tracker_id => '2', :subject => '0'}}
  109. assert_redirected_to "/imports/#{import.to_param}/run"
  110. import.reload
  111. mapping = import.settings['mapping']
  112. assert mapping
  113. assert_equal '1', mapping['project_id']
  114. assert_equal '2', mapping['tracker_id']
  115. assert_equal '0', mapping['subject']
  116. end
  117. def test_get_run
  118. import = generate_import_with_mapping
  119. get :run, :id => import
  120. assert_response :success
  121. assert_select '#import-progress'
  122. end
  123. def test_post_run_should_import_the_file
  124. import = generate_import_with_mapping
  125. assert_difference 'Issue.count', 3 do
  126. post :run, :id => import
  127. assert_redirected_to "/imports/#{import.to_param}"
  128. end
  129. import.reload
  130. assert_equal true, import.finished
  131. assert_equal 3, import.items.count
  132. issues = Issue.order(:id => :desc).limit(3).to_a
  133. assert_equal ["Child of existing issue", "Child 1", "First"], issues.map(&:subject)
  134. end
  135. def test_post_run_should_import_max_items_and_resume
  136. ImportsController.any_instance.stubs(:max_items_per_request).returns(2)
  137. import = generate_import_with_mapping
  138. assert_difference 'Issue.count', 2 do
  139. post :run, :id => import
  140. assert_redirected_to "/imports/#{import.to_param}/run"
  141. end
  142. assert_difference 'Issue.count', 1 do
  143. post :run, :id => import
  144. assert_redirected_to "/imports/#{import.to_param}"
  145. end
  146. issues = Issue.order(:id => :desc).limit(3).to_a
  147. assert_equal ["Child of existing issue", "Child 1", "First"], issues.map(&:subject)
  148. end
  149. def test_show_without_errors
  150. import = generate_import_with_mapping
  151. import.run
  152. assert_equal 0, import.unsaved_items.count
  153. get :show, :id => import.to_param
  154. assert_response :success
  155. assert_select 'ul#saved-items'
  156. assert_select 'ul#saved-items li', import.saved_items.count
  157. assert_select 'table#unsaved-items', 0
  158. end
  159. def test_show_with_errors_should_show_unsaved_items
  160. import = generate_import_with_mapping
  161. import.mapping.merge! 'subject' => 20
  162. import.run
  163. assert_not_equal 0, import.unsaved_items.count
  164. get :show, :id => import.to_param
  165. assert_response :success
  166. assert_select 'table#unsaved-items'
  167. assert_select 'table#unsaved-items tbody tr', import.unsaved_items.count
  168. end
  169. end