@@ -1,176 +1,178 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2016 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2016 Jean-Philippe Lang | |
3 | # |
|
3 | # | |
4 | # This program is free software; you can redistribute it and/or |
|
4 | # This program is free software; you can redistribute it and/or | |
5 | # modify it under the terms of the GNU General Public License |
|
5 | # modify it under the terms of the GNU General Public License | |
6 | # as published by the Free Software Foundation; either version 2 |
|
6 | # as published by the Free Software Foundation; either version 2 | |
7 | # of the License, or (at your option) any later version. |
|
7 | # of the License, or (at your option) any later version. | |
8 | # |
|
8 | # | |
9 | # This program is distributed in the hope that it will be useful, |
|
9 | # This program is distributed in the hope that it will be useful, | |
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | # GNU General Public License for more details. |
|
12 | # GNU General Public License for more details. | |
13 | # |
|
13 | # | |
14 | # You should have received a copy of the GNU General Public License |
|
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 |
|
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. |
|
16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 |
|
17 | |||
18 | require File.expand_path('../../test_helper', __FILE__) |
|
18 | require File.expand_path('../../test_helper', __FILE__) | |
19 |
|
19 | |||
20 | class IssueImportTest < ActiveSupport::TestCase |
|
20 | class IssueImportTest < ActiveSupport::TestCase | |
21 | fixtures :projects, :enabled_modules, |
|
21 | fixtures :projects, :enabled_modules, | |
22 | :users, :email_addresses, |
|
22 | :users, :email_addresses, | |
23 | :roles, :members, :member_roles, |
|
23 | :roles, :members, :member_roles, | |
24 | :issues, :issue_statuses, |
|
24 | :issues, :issue_statuses, | |
25 | :trackers, :projects_trackers, |
|
25 | :trackers, :projects_trackers, | |
26 | :versions, |
|
26 | :versions, | |
27 | :issue_categories, |
|
27 | :issue_categories, | |
28 | :enumerations, |
|
28 | :enumerations, | |
29 | :workflows, |
|
29 | :workflows, | |
30 | :custom_fields, |
|
30 | :custom_fields, | |
31 | :custom_values, |
|
31 | :custom_values, | |
32 | :custom_fields_projects, |
|
32 | :custom_fields_projects, | |
33 | :custom_fields_trackers |
|
33 | :custom_fields_trackers | |
34 |
|
34 | |||
|
35 | include Redmine::I18n | |||
|
36 | ||||
35 | def setup |
|
37 | def setup | |
36 | set_language_if_valid 'en' |
|
38 | set_language_if_valid 'en' | |
37 | end |
|
39 | end | |
38 |
|
40 | |||
39 | def test_create_versions_should_create_missing_versions |
|
41 | def test_create_versions_should_create_missing_versions | |
40 | import = generate_import_with_mapping |
|
42 | import = generate_import_with_mapping | |
41 | import.mapping.merge!('fixed_version' => '9', 'create_versions' => '1') |
|
43 | import.mapping.merge!('fixed_version' => '9', 'create_versions' => '1') | |
42 | import.save! |
|
44 | import.save! | |
43 |
|
45 | |||
44 | version = new_record(Version) do |
|
46 | version = new_record(Version) do | |
45 | assert_difference 'Issue.count', 3 do |
|
47 | assert_difference 'Issue.count', 3 do | |
46 | import.run |
|
48 | import.run | |
47 | end |
|
49 | end | |
48 | end |
|
50 | end | |
49 | assert_equal '2.1', version.name |
|
51 | assert_equal '2.1', version.name | |
50 | end |
|
52 | end | |
51 |
|
53 | |||
52 | def test_create_categories_should_create_missing_categories |
|
54 | def test_create_categories_should_create_missing_categories | |
53 | import = generate_import_with_mapping |
|
55 | import = generate_import_with_mapping | |
54 | import.mapping.merge!('category' => '10', 'create_categories' => '1') |
|
56 | import.mapping.merge!('category' => '10', 'create_categories' => '1') | |
55 | import.save! |
|
57 | import.save! | |
56 |
|
58 | |||
57 | category = new_record(IssueCategory) do |
|
59 | category = new_record(IssueCategory) do | |
58 | assert_difference 'Issue.count', 3 do |
|
60 | assert_difference 'Issue.count', 3 do | |
59 | import.run |
|
61 | import.run | |
60 | end |
|
62 | end | |
61 | end |
|
63 | end | |
62 | assert_equal 'New category', category.name |
|
64 | assert_equal 'New category', category.name | |
63 | end |
|
65 | end | |
64 |
|
66 | |||
65 | def test_mapping_with_fixed_tracker |
|
67 | def test_mapping_with_fixed_tracker | |
66 | import = generate_import_with_mapping |
|
68 | import = generate_import_with_mapping | |
67 | import.mapping.merge!('tracker' => 'value:2') |
|
69 | import.mapping.merge!('tracker' => 'value:2') | |
68 | import.save! |
|
70 | import.save! | |
69 |
|
71 | |||
70 | issues = new_records(Issue, 3) { import.run } |
|
72 | issues = new_records(Issue, 3) { import.run } | |
71 | assert_equal [2], issues.map(&:tracker_id).uniq |
|
73 | assert_equal [2], issues.map(&:tracker_id).uniq | |
72 | end |
|
74 | end | |
73 |
|
75 | |||
74 | def test_mapping_with_mapped_tracker |
|
76 | def test_mapping_with_mapped_tracker | |
75 | import = generate_import_with_mapping |
|
77 | import = generate_import_with_mapping | |
76 | import.mapping.merge!('tracker' => '13') |
|
78 | import.mapping.merge!('tracker' => '13') | |
77 | import.save! |
|
79 | import.save! | |
78 |
|
80 | |||
79 | issues = new_records(Issue, 3) { import.run } |
|
81 | issues = new_records(Issue, 3) { import.run } | |
80 | assert_equal [1, 2, 1], issues.map(&:tracker_id) |
|
82 | assert_equal [1, 2, 1], issues.map(&:tracker_id) | |
81 | end |
|
83 | end | |
82 |
|
84 | |||
83 | def test_should_not_import_with_default_tracker_when_tracker_is_invalid |
|
85 | def test_should_not_import_with_default_tracker_when_tracker_is_invalid | |
84 | Tracker.find_by_name('Feature request').update!(:name => 'Feature') |
|
86 | Tracker.find_by_name('Feature request').update!(:name => 'Feature') | |
85 |
|
87 | |||
86 | import = generate_import_with_mapping |
|
88 | import = generate_import_with_mapping | |
87 | import.mapping.merge!('tracker' => '13') |
|
89 | import.mapping.merge!('tracker' => '13') | |
88 | import.save! |
|
90 | import.save! | |
89 | import.run |
|
91 | import.run | |
90 |
|
92 | |||
91 | assert_equal 1, import.unsaved_items.count |
|
93 | assert_equal 1, import.unsaved_items.count | |
92 | item = import.unsaved_items.first |
|
94 | item = import.unsaved_items.first | |
93 | assert_include "Tracker cannot be blank", item.message |
|
95 | assert_include "Tracker cannot be blank", item.message | |
94 | end |
|
96 | end | |
95 |
|
97 | |||
96 | def test_status_should_be_set |
|
98 | def test_status_should_be_set | |
97 | import = generate_import_with_mapping |
|
99 | import = generate_import_with_mapping | |
98 | import.mapping.merge!('status' => '14') |
|
100 | import.mapping.merge!('status' => '14') | |
99 | import.save! |
|
101 | import.save! | |
100 |
|
102 | |||
101 | issues = new_records(Issue, 3) { import.run } |
|
103 | issues = new_records(Issue, 3) { import.run } | |
102 | assert_equal ['New', 'New', 'Assigned'], issues.map(&:status).map(&:name) |
|
104 | assert_equal ['New', 'New', 'Assigned'], issues.map(&:status).map(&:name) | |
103 | end |
|
105 | end | |
104 |
|
106 | |||
105 | def test_parent_should_be_set |
|
107 | def test_parent_should_be_set | |
106 | import = generate_import_with_mapping |
|
108 | import = generate_import_with_mapping | |
107 | import.mapping.merge!('parent_issue_id' => '5') |
|
109 | import.mapping.merge!('parent_issue_id' => '5') | |
108 | import.save! |
|
110 | import.save! | |
109 |
|
111 | |||
110 | issues = new_records(Issue, 3) { import.run } |
|
112 | issues = new_records(Issue, 3) { import.run } | |
111 | assert_nil issues[0].parent |
|
113 | assert_nil issues[0].parent | |
112 | assert_equal issues[0].id, issues[1].parent_id |
|
114 | assert_equal issues[0].id, issues[1].parent_id | |
113 | assert_equal 2, issues[2].parent_id |
|
115 | assert_equal 2, issues[2].parent_id | |
114 | end |
|
116 | end | |
115 |
|
117 | |||
116 | def test_assignee_should_be_set |
|
118 | def test_assignee_should_be_set | |
117 | import = generate_import_with_mapping |
|
119 | import = generate_import_with_mapping | |
118 | import.mapping.merge!('assigned_to' => '11') |
|
120 | import.mapping.merge!('assigned_to' => '11') | |
119 | import.save! |
|
121 | import.save! | |
120 |
|
122 | |||
121 | issues = new_records(Issue, 3) { import.run } |
|
123 | issues = new_records(Issue, 3) { import.run } | |
122 | assert_equal [User.find(3), nil, nil], issues.map(&:assigned_to) |
|
124 | assert_equal [User.find(3), nil, nil], issues.map(&:assigned_to) | |
123 | end |
|
125 | end | |
124 |
|
126 | |||
125 | def test_user_custom_field_should_be_set |
|
127 | def test_user_custom_field_should_be_set | |
126 | field = IssueCustomField.generate!(:field_format => 'user', :is_for_all => true, :trackers => Tracker.all) |
|
128 | field = IssueCustomField.generate!(:field_format => 'user', :is_for_all => true, :trackers => Tracker.all) | |
127 | import = generate_import_with_mapping |
|
129 | import = generate_import_with_mapping | |
128 | import.mapping.merge!("cf_#{field.id}" => '11') |
|
130 | import.mapping.merge!("cf_#{field.id}" => '11') | |
129 | import.save! |
|
131 | import.save! | |
130 |
|
132 | |||
131 | issues = new_records(Issue, 3) { import.run } |
|
133 | issues = new_records(Issue, 3) { import.run } | |
132 | assert_equal '3', issues.first.custom_field_value(field) |
|
134 | assert_equal '3', issues.first.custom_field_value(field) | |
133 | end |
|
135 | end | |
134 |
|
136 | |||
135 | def test_is_private_should_be_set_based_on_user_locale |
|
137 | def test_is_private_should_be_set_based_on_user_locale | |
136 | import = generate_import_with_mapping |
|
138 | import = generate_import_with_mapping | |
137 | import.mapping.merge!('is_private' => '6') |
|
139 | import.mapping.merge!('is_private' => '6') | |
138 | import.save! |
|
140 | import.save! | |
139 |
|
141 | |||
140 | issues = new_records(Issue, 3) { import.run } |
|
142 | issues = new_records(Issue, 3) { import.run } | |
141 | assert_equal [false, true, false], issues.map(&:is_private) |
|
143 | assert_equal [false, true, false], issues.map(&:is_private) | |
142 | end |
|
144 | end | |
143 |
|
145 | |||
144 | def test_dates_should_be_parsed_using_date_format_setting |
|
146 | def test_dates_should_be_parsed_using_date_format_setting | |
145 | field = IssueCustomField.generate!(:field_format => 'date', :is_for_all => true, :trackers => Tracker.all) |
|
147 | field = IssueCustomField.generate!(:field_format => 'date', :is_for_all => true, :trackers => Tracker.all) | |
146 | import = generate_import_with_mapping('import_dates.csv') |
|
148 | import = generate_import_with_mapping('import_dates.csv') | |
147 | import.settings.merge!('date_format' => Import::DATE_FORMATS[1]) |
|
149 | import.settings.merge!('date_format' => Import::DATE_FORMATS[1]) | |
148 | import.mapping.merge!('tracker' => 'value:1', 'subject' => '0', 'start_date' => '1', 'due_date' => '2', "cf_#{field.id}" => '3') |
|
150 | import.mapping.merge!('tracker' => 'value:1', 'subject' => '0', 'start_date' => '1', 'due_date' => '2', "cf_#{field.id}" => '3') | |
149 | import.save! |
|
151 | import.save! | |
150 |
|
152 | |||
151 | issue = new_record(Issue) { import.run } # only 1 valid issue |
|
153 | issue = new_record(Issue) { import.run } # only 1 valid issue | |
152 | assert_equal "Valid dates", issue.subject |
|
154 | assert_equal "Valid dates", issue.subject | |
153 | assert_equal Date.parse('2015-07-10'), issue.start_date |
|
155 | assert_equal Date.parse('2015-07-10'), issue.start_date | |
154 | assert_equal Date.parse('2015-08-12'), issue.due_date |
|
156 | assert_equal Date.parse('2015-08-12'), issue.due_date | |
155 | assert_equal '2015-07-14', issue.custom_field_value(field) |
|
157 | assert_equal '2015-07-14', issue.custom_field_value(field) | |
156 | end |
|
158 | end | |
157 |
|
159 | |||
158 | def test_date_format_should_default_to_user_language |
|
160 | def test_date_format_should_default_to_user_language | |
159 | user = User.generate!(:language => 'fr') |
|
161 | user = User.generate!(:language => 'fr') | |
160 | import = Import.new |
|
162 | import = Import.new | |
161 | import.user = user |
|
163 | import.user = user | |
162 | assert_nil import.settings['date_format'] |
|
164 | assert_nil import.settings['date_format'] | |
163 |
|
165 | |||
164 | import.set_default_settings |
|
166 | import.set_default_settings | |
165 | assert_equal '%d/%m/%Y', import.settings['date_format'] |
|
167 | assert_equal '%d/%m/%Y', import.settings['date_format'] | |
166 | end |
|
168 | end | |
167 |
|
169 | |||
168 | def test_run_should_remove_the_file |
|
170 | def test_run_should_remove_the_file | |
169 | import = generate_import_with_mapping |
|
171 | import = generate_import_with_mapping | |
170 | file_path = import.filepath |
|
172 | file_path = import.filepath | |
171 | assert File.exists?(file_path) |
|
173 | assert File.exists?(file_path) | |
172 |
|
174 | |||
173 | import.run |
|
175 | import.run | |
174 | assert !File.exists?(file_path) |
|
176 | assert !File.exists?(file_path) | |
175 | end |
|
177 | end | |
176 | end |
|
178 | end |
General Comments 0
You need to be logged in to leave comments.
Login now