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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. # Redmine - project management software
  2. # Copyright (C) 2006-2017 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, :params => {
  47. :file => uploaded_test_file('import_issues.csv', 'text/csv')
  48. }
  49. assert_response 302
  50. end
  51. assert_equal 2, import.user_id
  52. assert_match /\A[0-9a-f]+\z/, import.filename
  53. assert import.file_exists?
  54. end
  55. def test_get_settings_should_display_settings_form
  56. import = generate_import
  57. get :settings, :params => {
  58. :id => import.to_param
  59. }
  60. assert_response :success
  61. assert_select 'select[name=?]', 'import_settings[separator]'
  62. assert_select 'select[name=?]', 'import_settings[wrapper]'
  63. assert_select 'select[name=?]', 'import_settings[encoding]'
  64. assert_select 'select[name=?]', 'import_settings[date_format]'
  65. end
  66. def test_post_settings_should_update_settings
  67. import = generate_import
  68. post :settings, :params => {
  69. :id => import.to_param,
  70. :import_settings => {
  71. :separator => ":",
  72. :wrapper => "|",
  73. :encoding => "UTF-8",
  74. :date_format => '%m/%d/%Y'
  75. }
  76. }
  77. assert_redirected_to "/imports/#{import.to_param}/mapping"
  78. import.reload
  79. assert_equal ":", import.settings['separator']
  80. assert_equal "|", import.settings['wrapper']
  81. assert_equal "UTF-8", import.settings['encoding']
  82. assert_equal '%m/%d/%Y', import.settings['date_format']
  83. end
  84. def test_post_settings_should_update_total_items_count
  85. import = generate_import('import_iso8859-1.csv')
  86. post :settings, :params => {
  87. :id => import.to_param,
  88. :import_settings => {
  89. :separator => ";",
  90. :wrapper => '"',
  91. :encoding => "ISO-8859-1"
  92. }
  93. }
  94. assert_response 302
  95. import.reload
  96. assert_equal 2, import.total_items
  97. end
  98. def test_post_settings_with_wrong_encoding_should_display_error
  99. import = generate_import('import_iso8859-1.csv')
  100. post :settings, :params => {
  101. :id => import.to_param,
  102. :import_settings => {
  103. :separator => ";",
  104. :wrapper => '"',
  105. :encoding => "UTF-8"
  106. }
  107. }
  108. assert_response 200
  109. import.reload
  110. assert_nil import.total_items
  111. assert_select 'div#flash_error', /not a valid UTF-8 encoded file/
  112. end
  113. def test_post_settings_with_invalid_encoding_should_display_error
  114. import = generate_import('invalid-Shift_JIS.csv')
  115. post :settings, :params => {
  116. :id => import.to_param,
  117. :import_settings => {
  118. :separator => ";",
  119. :wrapper => '"',
  120. :encoding => "Shift_JIS"
  121. }
  122. }
  123. assert_response 200
  124. import.reload
  125. assert_nil import.total_items
  126. assert_select 'div#flash_error', /not a valid Shift_JIS encoded file/
  127. end
  128. def test_get_mapping_should_display_mapping_form
  129. import = generate_import('import_iso8859-1.csv')
  130. import.settings = {'separator' => ";", 'wrapper' => '"', 'encoding' => "ISO-8859-1"}
  131. import.save!
  132. get :mapping, :params => {
  133. :id => import.to_param
  134. }
  135. assert_response :success
  136. assert_select 'select[name=?]', 'import_settings[mapping][subject]' do
  137. assert_select 'option', 4
  138. assert_select 'option[value="0"]', :text => 'column A'
  139. end
  140. assert_select 'table.sample-data' do
  141. assert_select 'tr', 3
  142. assert_select 'td', 9
  143. end
  144. end
  145. def test_post_mapping_should_update_mapping
  146. import = generate_import('import_iso8859-1.csv')
  147. post :mapping, :params => {
  148. :id => import.to_param,
  149. :import_settings => {
  150. :mapping => {
  151. :project_id => '1',
  152. :tracker_id => '2',
  153. :subject => '0'}
  154. }
  155. }
  156. assert_redirected_to "/imports/#{import.to_param}/run"
  157. import.reload
  158. mapping = import.settings['mapping']
  159. assert mapping
  160. assert_equal '1', mapping['project_id']
  161. assert_equal '2', mapping['tracker_id']
  162. assert_equal '0', mapping['subject']
  163. end
  164. def test_get_run
  165. import = generate_import_with_mapping
  166. get :run, :params => {
  167. :id => import
  168. }
  169. assert_response :success
  170. assert_select '#import-progress'
  171. end
  172. def test_post_run_should_import_the_file
  173. import = generate_import_with_mapping
  174. assert_difference 'Issue.count', 3 do
  175. post :run, :params => {
  176. :id => import
  177. }
  178. assert_redirected_to "/imports/#{import.to_param}"
  179. end
  180. import.reload
  181. assert_equal true, import.finished
  182. assert_equal 3, import.items.count
  183. issues = Issue.order(:id => :desc).limit(3).to_a
  184. assert_equal ["Child of existing issue", "Child 1", "First"], issues.map(&:subject)
  185. end
  186. def test_post_run_should_import_max_items_and_resume
  187. ImportsController.any_instance.stubs(:max_items_per_request).returns(2)
  188. import = generate_import_with_mapping
  189. assert_difference 'Issue.count', 2 do
  190. post :run, :params => {
  191. :id => import
  192. }
  193. assert_redirected_to "/imports/#{import.to_param}/run"
  194. end
  195. assert_difference 'Issue.count', 1 do
  196. post :run, :params => {
  197. :id => import
  198. }
  199. assert_redirected_to "/imports/#{import.to_param}"
  200. end
  201. issues = Issue.order(:id => :desc).limit(3).to_a
  202. assert_equal ["Child of existing issue", "Child 1", "First"], issues.map(&:subject)
  203. end
  204. def test_show_without_errors
  205. import = generate_import_with_mapping
  206. import.run
  207. assert_equal 0, import.unsaved_items.count
  208. get :show, :params => {
  209. :id => import.to_param
  210. }
  211. assert_response :success
  212. assert_select 'ul#saved-items'
  213. assert_select 'ul#saved-items li', import.saved_items.count
  214. assert_select 'table#unsaved-items', 0
  215. end
  216. def test_show_with_errors_should_show_unsaved_items
  217. import = generate_import_with_mapping
  218. import.mapping.merge! 'subject' => 20
  219. import.run
  220. assert_not_equal 0, import.unsaved_items.count
  221. get :show, :params => {
  222. :id => import.to_param
  223. }
  224. assert_response :success
  225. assert_select 'table#unsaved-items'
  226. assert_select 'table#unsaved-items tbody tr', import.unsaved_items.count
  227. end
  228. end