Project

General

Profile

Feature #22913 » 22913_auto_mapping.patch

Yuichi HARADA, 2019-11-26 06:59

View differences:

app/controllers/imports_controller.rb
65 65

  
66 66
  def mapping
67 67
    @custom_fields = @import.mappable_custom_fields
68
    if @import.mapping.empty?
69
      @import.settings['mapping'] = {}
70
      headers = @import.headers
71
      # Core fields
72
      core_fields =
73
        case @import
74
        when IssueImport
75
          %w(tracker subject description status priority category assigned_to fixed_version is_private parent_issue_id start_date due_date estimated_hours done_ratio)
76
        when TimeEntryImport
77
          %w(activity user_id issue_id spent_on hours comments)
78
        else
79
          []
80
        end
81
      core_fields.each do |field|
82
        label = l(:"field_#{field.gsub(/\_id$/, '')}")
83
        @import.mapping[field] = headers.index(label)
84
      end
85
      # Custom fields
86
      @custom_fields.each do |field|
87
        @import.mapping["cf_#{field.id}"] = headers.index(field.name)
88
      end
89
    end
68 90

  
69 91
    if request.post?
70 92
      respond_to do |format|
app/views/imports/_issues_fields_mapping.html.erb
7 7
        :id => 'import_mapping_project_id' %>
8 8
</p>
9 9
<p>
10
  <label for="import_mapping_tracker"><%= l(:label_tracker) %></label>
10
  <label for="import_mapping_tracker"><%= l(:field_tracker) %></label>
11 11
  <%= mapping_select_tag @import, 'tracker', :required => true,
12 12
        :values => @import.allowed_target_trackers.sorted.map {|t| [t.name, t.id]} %>
13 13
</p>
......
99 99
</p>
100 100
</div>
101 101
</div>
102

  
test/fixtures/files/import_auto_mapping.csv
1
Column 0;Column 1;Column 2;Subject;Column 4;Database;Column 6
2
0;1;2;3;4;5;6
test/fixtures/files/import_time_entries_auto_mapping.csv
1
Column 0;Column 1;Activity;Column 3;Overtime;Column 5;Column 6
2
0;1;2;3;4;5;6
test/functional/imports_controller_test.rb
166 166
    end
167 167
  end
168 168

  
169
  def test_get_mapping_should_auto_select_by_column_name
170
    import = generate_import('import_auto_mapping.csv')
171
    import.settings = {'separator' => ';', 'wrapper'=> '"', 'encoding' => 'ISO-8859-1'}
172
    import.save!
173

  
174
    get :mapping, :params => {
175
        :id => import.to_param
176
      }
177
    assert_response :success
178

  
179
    assert_select 'select[name=?]', 'import_settings[mapping][subject]' do
180
      assert_select 'option[value="3"][selected="selected"]', :text => 'Subject'
181
    end
182
    assert_select 'select[name=?]', 'import_settings[mapping][cf_1]' do
183
      assert_select 'option[value="5"][selected="selected"]', :text => 'Database'
184
    end
185
  end
186

  
169 187
  def test_post_mapping_should_update_mapping
170 188
    import = generate_import('import_iso8859-1.csv')
171 189

  
......
222 240
    assert_select 'select[name=?]', 'import_settings[mapping][user_id]', 0
223 241
  end
224 242

  
243
  def test_get_mapping_time_entry_should_auto_select_by_column_name
244
    import = generate_time_entry_import('import_time_entries_auto_mapping.csv')
245
    import.settings = {'separator' => ";", 'wrapper' => '"', 'encoding' => "ISO-8859-1"}
246
    import.save!
247

  
248
    get :mapping, :params => {
249
        :id => import.to_param
250
      }
251
    assert_response :success
252

  
253
    assert_select 'select[name=?]', 'import_settings[mapping][activity]' do
254
      assert_select 'option[value="2"][selected="selected"]', :text => 'Activity'
255
    end
256
    assert_select 'select[name=?]', 'import_settings[mapping][cf_10]' do
257
      assert_select 'option[value="4"][selected="selected"]', :text => 'Overtime'
258
    end
259
  end
260

  
225 261
  def test_get_run
226 262
    import = generate_import_with_mapping
227 263

  
(3-3/7)