summaryrefslogtreecommitdiffstats
path: root/test/functional/imports_controller_test.rb
blob: 871a0834643f82fb3d94f4fe910ed20e044918c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# frozen_string_literal: true

# Redmine - project management software
# Copyright (C) 2006-2019  Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

require File.expand_path('../../test_helper', __FILE__)

class ImportsControllerTest < Redmine::ControllerTest
  fixtures :projects, :enabled_modules,
           :users, :email_addresses,
           :roles, :members, :member_roles,
           :issues, :issue_statuses,
           :trackers, :projects_trackers,
           :versions,
           :issue_categories,
           :enumerations,
           :workflows,
           :custom_fields,
           :custom_values,
           :custom_fields_projects,
           :custom_fields_trackers

  def setup
    User.current = nil
    @request.session[:user_id] = 2
  end

  def teardown
    Import.destroy_all
  end

  def test_new_should_display_the_upload_form
    get :new, :params => { :type => 'IssueImport' }
    assert_response :success
    assert_select 'input[name=?]', 'file'
  end

  def test_create_should_save_the_file
    import = new_record(Import) do
      post :create, :params => {
          :type => 'IssueImport',
          :file => uploaded_test_file('import_issues.csv', 'text/csv')
        }
      assert_response 302
    end
    assert_equal 2, import.user_id
    assert_match /\A[0-9a-f]+\z/, import.filename
    assert import.file_exists?
  end

  def test_get_settings_should_display_settings_form
    import = generate_import
    get :settings, :params => {
        :id => import.to_param
      }
    assert_response :success
    assert_select 'select[name=?]', 'import_settings[separator]'
    assert_select 'select[name=?]', 'import_settings[wrapper]'
    assert_select 'select[name=?]', 'import_settings[encoding]'
    assert_select 'select[name=?]', 'import_settings[date_format]'
  end

  def test_post_settings_should_update_settings
    import = generate_import

    post :settings, :params => {
        :id => import.to_param,
        :import_settings => {
          :separator => ":",
          :wrapper => "|",
          :encoding => "UTF-8",
          :date_format => '%m/%d/%Y'
        }
      }
    assert_redirected_to "/imports/#{import.to_param}/mapping"

    import.reload
    assert_equal ":", import.settings['separator']
    assert_equal "|", import.settings['wrapper']
    assert_equal "UTF-8", import.settings['encoding']
    assert_equal '%m/%d/%Y', import.settings['date_format']
  end

  def test_post_settings_should_update_total_items_count
    import = generate_import('import_iso8859-1.csv')

    post :settings, :params => {
        :id => import.to_param,
        :import_settings => {
          :separator => ";",
          :wrapper => '"',
          :encoding => "ISO-8859-1"
        }
      }
    assert_response 302
    import.reload
    assert_equal 2, import.total_items
  end

  def test_post_settings_with_wrong_encoding_should_display_error
    import = generate_import('import_iso8859-1.csv')

    post :settings, :params => {
        :id => import.to_param,
        :import_settings => {
          :separator => ";",
          :wrapper => '"',
          :encoding => "UTF-8"
        }
      }
    assert_response 200
    import.reload
    assert_nil import.total_items
    assert_select 'div#flash_error', /not a valid UTF-8 encoded file/
  end

  def test_post_settings_with_invalid_encoding_should_display_error
    import = generate_import('invalid-Shift_JIS.csv')

    post :settings, :params => {
        :id => import.to_param,
        :import_settings => {
          :separator => ";",
          :wrapper => '"',
          :encoding => "Shift_JIS"
        }
      }
    assert_response 200
    import.reload
    assert_nil import.total_items
    assert_select 'div#flash_error', /not a valid Shift_JIS encoded file/
  end

  def test_get_mapping_should_display_mapping_form
    import = generate_import('import_iso8859-1.csv')
    import.settings = {'separator' => ";", 'wrapper' => '"', 'encoding' => "ISO-8859-1"}
    import.save!

    get :mapping, :params => {
        :id => import.to_param
      }
    assert_response :success

    assert_select 'select[name=?]', 'import_settings[mapping][subject]' do
      assert_select 'option', 4
      assert_select 'option[value="0"]', :text => 'column A'
    end

    assert_select 'table.sample-data' do
      assert_select 'tr', 3
      assert_select 'td', 9
    end
  end

  def test_post_mapping_should_update_mapping
    import = generate_import('import_iso8859-1.csv')

    post :mapping, :params => {
        :id => import.to_param,
        :import_settings => {
          :mapping => {
            :project_id => '1',
            :tracker_id => '2',
          :subject => '0'}
        }
      }
    assert_redirected_to "/imports/#{import.to_param}/run"
    import.reload
    mapping = import.settings['mapping']
    assert mapping
    assert_equal '1', mapping['project_id']
    assert_equal '2', mapping['tracker_id']
    assert_equal '0', mapping['subject']
  end

  def test_get_run
    import = generate_import_with_mapping

    get :run, :params => {
        :id => import
      }
    assert_response :success
    assert_select '#import-progress'
  end

  def test_post_run_should_import_the_file
    import = generate_import_with_mapping

    assert_difference 'Issue.count', 3 do
      post :run, :params => {
          :id => import
        }
      assert_redirected_to "/imports/#{import.to_param}"
    end

    import.reload
    assert_equal true, import.finished
    assert_equal 3, import.items.count

    issues = Issue.order(:id => :desc).limit(3).to_a
    assert_equal ["Child of existing issue", "Child 1", "First"], issues.map(&:subject)
  end

  def test_post_run_should_import_max_items_and_resume
    ImportsController.any_instance.stubs(:max_items_per_request).returns(2)
    import = generate_import_with_mapping

    assert_difference 'Issue.count', 2 do
      post :run, :params => {
          :id => import
        }
      assert_redirected_to "/imports/#{import.to_param}/run"
    end

    assert_difference 'Issue.count', 1 do
      post :run, :params => {
          :id => import
        }
      assert_redirected_to "/imports/#{import.to_param}"
    end

    issues = Issue.order(:id => :desc).limit(3).to_a
    assert_equal ["Child of existing issue", "Child 1", "First"], issues.map(&:subject)
  end

  def test_post_run_with_notifications
    import = generate_import

    post :settings, :params => {
        :id => import,
        :import_settings => {
          :separator => ';',
          :wrapper => '"',
          :encoding => 'ISO-8859-1',
          :notifications => '1',
          :mapping => {
            :project_id => '1',
            :tracker => '13',
            :subject => '1',
            :assigned_to => '11',
          },
        },
      }

    ActionMailer::Base.deliveries.clear
    assert_difference 'Issue.count', 3 do
      post :run, :params => {
          :id => import,
        }
      assert_response :found
    end
    actual_email_count = ActionMailer::Base.deliveries.size
    assert_not_equal 0, actual_email_count

    import.reload
    issue_ids = import.items.collect(&:obj_id)
    expected_email_count =
      Issue.where(:id => issue_ids).inject(0) do |sum, issue|
        sum + (issue.notified_users | issue.notified_watchers).size
      end
    assert_equal expected_email_count, actual_email_count
  end

  def test_show_without_errors
    import = generate_import_with_mapping
    import.run
    assert_equal 0, import.unsaved_items.count

    get :show, :params => {
        :id => import.to_param
      }
    assert_response :success

    assert_select 'ul#saved-items'
    assert_select 'ul#saved-items li', import.saved_items.count
    assert_select 'table#unsaved-items', 0
  end

  def test_show_with_errors_should_show_unsaved_items
    import = generate_import_with_mapping
    import.mapping.merge! 'subject' => 20
    import.run
    assert_not_equal 0, import.unsaved_items.count

    get :show, :params => {
        :id => import.to_param
      }
    assert_response :success

    assert_select 'table#unsaved-items'
    assert_select 'table#unsaved-items tbody tr', import.unsaved_items.count
  end
end