Project

General

Profile

Defect #21766 » 0002-Fix-CSV-import-does-not-keep-the-project-it-was-clic.patch

Marius BĂLTEANU, 2019-11-01 23:06

View differences:

app/controllers/imports_controller.rb
36 36
    @import = import_type.new
37 37
    @import.user = User.current
38 38
    @import.file = params[:file]
39
    @import.set_default_settings
39
    @import.set_default_settings(:project_id => params[:project_id])
40 40

  
41 41
    if @import.save
42 42
      redirect_to import_settings_path(@import)
app/models/import.rb
62 62
    Redmine::Utils.save_upload(arg, filepath)
63 63
  end
64 64

  
65
  def set_default_settings
65
  def set_default_settings(options={})
66 66
    separator = lu(user, :general_csv_separator)
67 67
    if file_exists?
68 68
      begin
......
84 84
      'date_format' => date_format,
85 85
      'notifications' => '0'
86 86
    )
87

  
88
    if options.key?(:project_id) && !options[:project_id].blank?
89
      # Do not fail if project doesn't exist
90
      begin
91
        project = Project.find(options[:project_id])
92
        self.settings.merge!('mapping' => {'project_id' => project.id})
93
      rescue; end
94
    end
87 95
  end
88 96

  
89 97
  def to_param
app/views/imports/new.html.erb
2 2

  
3 3
<%= form_tag(imports_path, :multipart => true) do %>
4 4
  <%= hidden_field_tag 'type', @import.type %>
5
  <%= hidden_field_tag 'project_id', params[:project_id] %>
5 6
  <fieldset class="box">
6 7
    <legend><%= l(:label_select_file_to_import) %> (CSV)</legend>
7 8
    <p>
app/views/issues/index.html.erb
11 11
    <% end %>
12 12

  
13 13
    <% if User.current.allowed_to?(:import_issues, @project, :global => true) %>
14
      <%= link_to l(:button_import), new_issues_import_path %>
14
      <%= link_to l(:button_import), new_issues_import_path(:project_id => @project) %>
15 15
    <% end %>
16 16
  <% end %>
17 17
</div>
app/views/timelog/index.html.erb
7 7
            :class => 'icon icon-settings' if User.current.allowed_to?(:manage_project_activities, @project) %>
8 8
<%= actions_dropdown do %>
9 9
  <% if User.current.allowed_to?(:import_time_entries, @project, :global => true) %>
10
    <%= link_to l(:button_import), new_time_entries_import_path %>
10
    <%= link_to l(:button_import), new_time_entries_import_path(:project_id => @project) %>
11 11
  <% end %>
12 12
<% end %>
13 13
</div>
test/functional/imports_controller_test.rb
44 44
  end
45 45

  
46 46
  def test_new_should_display_the_upload_form
47
    get :new, :params => { :type => 'IssueImport' }
47
    get :new, :params => { :type => 'IssueImport', :project_id => 'subproject1' }
48 48
    assert_response :success
49 49
    assert_select 'input[name=?]', 'file'
50
    assert_select 'input[name=?][type=?][value=?]', 'project_id', 'hidden', 'subproject1'
50 51
  end
51 52

  
52 53
  def test_create_should_save_the_file
test/unit/issue_import_test.rb
266 266
    issues = new_records(Issue, 3) { import.run }
267 267
    assert [nil, 3, system_version.id], issues.map(&:fixed_version_id)
268 268
  end
269

  
270
  def test_set_default_settings_with_project_id
271
    import = Import.new
272
    import.set_default_settings(:project_id => 3)
273

  
274
    assert_equal 3, import.mapping['project_id']
275
  end
276

  
277
  def test_set_default_settings_with_project_identifier
278
    import = Import.new
279
    import.set_default_settings(:project_id => 'ecookbook')
280

  
281
    assert_equal 1, import.mapping['project_id']
282
  end
283

  
284
  def test_set_default_settings_without_project_id
285
    import = Import.new
286
    import.set_default_settings
287

  
288
    assert_empty import.mapping
289
  end
290

  
291
  def test_set_default_settings_with_invalid_project_should_not_fail
292
    import = Import.new
293
    import.set_default_settings(:project_id => 'abc')
294

  
295
    assert_empty import.mapping
296
  end
269 297
end
    (1-1/1)