@@ -1,238 +1,238 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2014 Jean-Philippe Lang |
|
3 | 3 | # |
|
4 | 4 | # This program is free software; you can redistribute it and/or |
|
5 | 5 | # modify it under the terms of the GNU General Public License |
|
6 | 6 | # as published by the Free Software Foundation; either version 2 |
|
7 | 7 | # of the License, or (at your option) any later version. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU General Public License |
|
15 | 15 | # along with this program; if not, write to the Free Software |
|
16 | 16 | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
|
17 | 17 | |
|
18 | 18 | require File.expand_path('../../test_helper', __FILE__) |
|
19 | 19 | |
|
20 | 20 | class CustomFieldsControllerTest < ActionController::TestCase |
|
21 | 21 | fixtures :custom_fields, :custom_values, |
|
22 | 22 | :custom_fields_projects, :custom_fields_trackers, |
|
23 | 23 | :roles, :users, |
|
24 | 24 | :members, :member_roles, |
|
25 | 25 | :groups_users, |
|
26 | 26 | :trackers, :projects_trackers, |
|
27 | 27 | :enabled_modules, |
|
28 | 28 | :projects, :issues, |
|
29 | 29 | :issue_statuses, |
|
30 | 30 | :issue_categories, |
|
31 | 31 | :enumerations, |
|
32 | 32 | :workflows |
|
33 | 33 | |
|
34 | 34 | def setup |
|
35 | 35 | @request.session[:user_id] = 1 |
|
36 | 36 | end |
|
37 | 37 | |
|
38 | 38 | def test_index |
|
39 | 39 | get :index |
|
40 | 40 | assert_response :success |
|
41 | 41 | assert_template 'index' |
|
42 | 42 | end |
|
43 | 43 | |
|
44 | 44 | def test_new_without_type_should_render_select_type |
|
45 | 45 | get :new |
|
46 | 46 | assert_response :success |
|
47 | 47 | assert_template 'select_type' |
|
48 |
assert_select 'input[name=type]', CustomField |
|
|
48 | assert_select 'input[name=type]', CustomFieldsHelper::CUSTOM_FIELDS_TABS.size | |
|
49 | 49 | assert_select 'input[name=type][checked=checked]', 1 |
|
50 | 50 | end |
|
51 | 51 | |
|
52 | 52 | def test_new_should_work_for_each_customized_class_and_format |
|
53 | 53 | custom_field_classes.each do |klass| |
|
54 | 54 | Redmine::FieldFormat.available_formats.each do |format_name| |
|
55 | 55 | get :new, :type => klass.name, :custom_field => {:field_format => format_name} |
|
56 | 56 | assert_response :success |
|
57 | 57 | assert_template 'new' |
|
58 | 58 | assert_kind_of klass, assigns(:custom_field) |
|
59 | 59 | assert_equal format_name, assigns(:custom_field).format.name |
|
60 | 60 | assert_select 'form#custom_field_form' do |
|
61 | 61 | assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' |
|
62 | 62 | assert_select 'input[type=hidden][name=type][value=?]', klass.name |
|
63 | 63 | end |
|
64 | 64 | end |
|
65 | 65 | end |
|
66 | 66 | end |
|
67 | 67 | |
|
68 | 68 | def test_new_should_have_string_default_format |
|
69 | 69 | get :new, :type => 'IssueCustomField' |
|
70 | 70 | assert_response :success |
|
71 | 71 | assert_equal 'string', assigns(:custom_field).format.name |
|
72 | 72 | end |
|
73 | 73 | |
|
74 | 74 | def test_new_issue_custom_field |
|
75 | 75 | get :new, :type => 'IssueCustomField' |
|
76 | 76 | assert_response :success |
|
77 | 77 | assert_template 'new' |
|
78 | 78 | assert_select 'form#custom_field_form' do |
|
79 | 79 | assert_select 'select#custom_field_field_format[name=?]', 'custom_field[field_format]' do |
|
80 | 80 | assert_select 'option[value=user]', :text => 'User' |
|
81 | 81 | assert_select 'option[value=version]', :text => 'Version' |
|
82 | 82 | end |
|
83 | 83 | assert_select 'input[type=checkbox][name=?]', 'custom_field[project_ids][]', Project.count |
|
84 | 84 | assert_select 'input[type=hidden][name=?]', 'custom_field[project_ids][]', 1 |
|
85 | 85 | assert_select 'input[type=hidden][name=type][value=IssueCustomField]' |
|
86 | 86 | end |
|
87 | 87 | end |
|
88 | 88 | |
|
89 | 89 | def test_new_time_entry_custom_field_should_not_show_trackers_and_projects |
|
90 | 90 | get :new, :type => 'TimeEntryCustomField' |
|
91 | 91 | assert_response :success |
|
92 | 92 | assert_template 'new' |
|
93 | 93 | assert_select 'form#custom_field_form' do |
|
94 | 94 | assert_select 'input[name=?]', 'custom_field[tracker_ids][]', 0 |
|
95 | 95 | assert_select 'input[name=?]', 'custom_field[project_ids][]', 0 |
|
96 | 96 | end |
|
97 | 97 | end |
|
98 | 98 | |
|
99 | 99 | def test_default_value_should_be_an_input_for_string_custom_field |
|
100 | 100 | get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'string'} |
|
101 | 101 | assert_response :success |
|
102 | 102 | assert_select 'input[name=?]', 'custom_field[default_value]' |
|
103 | 103 | end |
|
104 | 104 | |
|
105 | 105 | def test_default_value_should_be_a_textarea_for_text_custom_field |
|
106 | 106 | get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'text'} |
|
107 | 107 | assert_response :success |
|
108 | 108 | assert_select 'textarea[name=?]', 'custom_field[default_value]' |
|
109 | 109 | end |
|
110 | 110 | |
|
111 | 111 | def test_default_value_should_be_a_checkbox_for_bool_custom_field |
|
112 | 112 | get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'bool'} |
|
113 | 113 | assert_response :success |
|
114 | 114 | assert_select 'select[name=?]', 'custom_field[default_value]' do |
|
115 | 115 | assert_select 'option', 3 |
|
116 | 116 | end |
|
117 | 117 | end |
|
118 | 118 | |
|
119 | 119 | def test_default_value_should_not_be_present_for_user_custom_field |
|
120 | 120 | get :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'user'} |
|
121 | 121 | assert_response :success |
|
122 | 122 | assert_select '[name=?]', 'custom_field[default_value]', 0 |
|
123 | 123 | end |
|
124 | 124 | |
|
125 | 125 | def test_new_js |
|
126 | 126 | xhr :get, :new, :type => 'IssueCustomField', :custom_field => {:field_format => 'list'}, :format => 'js' |
|
127 | 127 | assert_response :success |
|
128 | 128 | assert_template 'new' |
|
129 | 129 | assert_equal 'text/javascript', response.content_type |
|
130 | 130 | |
|
131 | 131 | field = assigns(:custom_field) |
|
132 | 132 | assert_equal 'list', field.field_format |
|
133 | 133 | end |
|
134 | 134 | |
|
135 | 135 | def test_new_with_invalid_custom_field_class_should_render_select_type |
|
136 | 136 | get :new, :type => 'UnknownCustomField' |
|
137 | 137 | assert_response :success |
|
138 | 138 | assert_template 'select_type' |
|
139 | 139 | end |
|
140 | 140 | |
|
141 | 141 | def test_create_list_custom_field |
|
142 | 142 | assert_difference 'CustomField.count' do |
|
143 | 143 | post :create, :type => "IssueCustomField", |
|
144 | 144 | :custom_field => {:name => "test_post_new_list", |
|
145 | 145 | :default_value => "", |
|
146 | 146 | :min_length => "0", |
|
147 | 147 | :searchable => "0", |
|
148 | 148 | :regexp => "", |
|
149 | 149 | :is_for_all => "1", |
|
150 | 150 | :possible_values => "0.1\n0.2\n", |
|
151 | 151 | :max_length => "0", |
|
152 | 152 | :is_filter => "0", |
|
153 | 153 | :is_required =>"0", |
|
154 | 154 | :field_format => "list", |
|
155 | 155 | :tracker_ids => ["1", ""]} |
|
156 | 156 | end |
|
157 | 157 | assert_redirected_to '/custom_fields?tab=IssueCustomField' |
|
158 | 158 | field = IssueCustomField.find_by_name('test_post_new_list') |
|
159 | 159 | assert_not_nil field |
|
160 | 160 | assert_equal ["0.1", "0.2"], field.possible_values |
|
161 | 161 | assert_equal 1, field.trackers.size |
|
162 | 162 | end |
|
163 | 163 | |
|
164 | 164 | def test_create_with_project_ids |
|
165 | 165 | assert_difference 'CustomField.count' do |
|
166 | 166 | post :create, :type => "IssueCustomField", :custom_field => { |
|
167 | 167 | :name => "foo", :field_format => "string", :is_for_all => "0", :project_ids => ["1", "3", ""] |
|
168 | 168 | } |
|
169 | 169 | assert_response 302 |
|
170 | 170 | end |
|
171 | 171 | field = IssueCustomField.order("id desc").first |
|
172 | 172 | assert_equal [1, 3], field.projects.map(&:id).sort |
|
173 | 173 | end |
|
174 | 174 | |
|
175 | 175 | def test_create_with_failure |
|
176 | 176 | assert_no_difference 'CustomField.count' do |
|
177 | 177 | post :create, :type => "IssueCustomField", :custom_field => {:name => ''} |
|
178 | 178 | end |
|
179 | 179 | assert_response :success |
|
180 | 180 | assert_template 'new' |
|
181 | 181 | end |
|
182 | 182 | |
|
183 | 183 | def test_create_without_type_should_render_select_type |
|
184 | 184 | assert_no_difference 'CustomField.count' do |
|
185 | 185 | post :create, :custom_field => {:name => ''} |
|
186 | 186 | end |
|
187 | 187 | assert_response :success |
|
188 | 188 | assert_template 'select_type' |
|
189 | 189 | end |
|
190 | 190 | |
|
191 | 191 | def test_edit |
|
192 | 192 | get :edit, :id => 1 |
|
193 | 193 | assert_response :success |
|
194 | 194 | assert_template 'edit' |
|
195 | 195 | assert_select 'input[name=?][value=?]', 'custom_field[name]', 'Database' |
|
196 | 196 | end |
|
197 | 197 | |
|
198 | 198 | def test_edit_invalid_custom_field_should_render_404 |
|
199 | 199 | get :edit, :id => 99 |
|
200 | 200 | assert_response 404 |
|
201 | 201 | end |
|
202 | 202 | |
|
203 | 203 | def test_update |
|
204 | 204 | put :update, :id => 1, :custom_field => {:name => 'New name'} |
|
205 | 205 | assert_redirected_to '/custom_fields?tab=IssueCustomField' |
|
206 | 206 | |
|
207 | 207 | field = CustomField.find(1) |
|
208 | 208 | assert_equal 'New name', field.name |
|
209 | 209 | end |
|
210 | 210 | |
|
211 | 211 | def test_update_with_failure |
|
212 | 212 | put :update, :id => 1, :custom_field => {:name => ''} |
|
213 | 213 | assert_response :success |
|
214 | 214 | assert_template 'edit' |
|
215 | 215 | end |
|
216 | 216 | |
|
217 | 217 | def test_destroy |
|
218 | 218 | custom_values_count = CustomValue.where(:custom_field_id => 1).count |
|
219 | 219 | assert custom_values_count > 0 |
|
220 | 220 | |
|
221 | 221 | assert_difference 'CustomField.count', -1 do |
|
222 | 222 | assert_difference 'CustomValue.count', - custom_values_count do |
|
223 | 223 | delete :destroy, :id => 1 |
|
224 | 224 | end |
|
225 | 225 | end |
|
226 | 226 | |
|
227 | 227 | assert_redirected_to '/custom_fields?tab=IssueCustomField' |
|
228 | 228 | assert_nil CustomField.find_by_id(1) |
|
229 | 229 | assert_nil CustomValue.find_by_custom_field_id(1) |
|
230 | 230 | end |
|
231 | 231 | |
|
232 | 232 | def custom_field_classes |
|
233 | 233 | files = Dir.glob(File.join(Rails.root, 'app/models/*_custom_field.rb')).map {|f| File.basename(f).sub(/\.rb$/, '') } |
|
234 | 234 | classes = files.map(&:classify).map(&:constantize) |
|
235 | 235 | assert classes.size > 0 |
|
236 | 236 | classes |
|
237 | 237 | end |
|
238 | 238 | end |
General Comments 0
You need to be logged in to leave comments.
Login now