@@ -1,318 +1,318 | |||
|
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 CustomFieldTest < ActiveSupport::TestCase |
|
21 | fixtures :custom_fields | |
|
21 | fixtures :custom_fields, :roles, :projects, :issues | |
|
22 | 22 | |
|
23 | 23 | def test_create |
|
24 | 24 | field = UserCustomField.new(:name => 'Money money money', :field_format => 'float') |
|
25 | 25 | assert field.save |
|
26 | 26 | end |
|
27 | 27 | |
|
28 | 28 | def test_before_validation |
|
29 | 29 | field = CustomField.new(:name => 'test_before_validation', :field_format => 'int') |
|
30 | 30 | field.searchable = true |
|
31 | 31 | assert field.save |
|
32 | 32 | assert_equal false, field.searchable |
|
33 | 33 | field.searchable = true |
|
34 | 34 | assert field.save |
|
35 | 35 | assert_equal false, field.searchable |
|
36 | 36 | end |
|
37 | 37 | |
|
38 | 38 | def test_regexp_validation |
|
39 | 39 | field = IssueCustomField.new(:name => 'regexp', :field_format => 'text', :regexp => '[a-z0-9') |
|
40 | 40 | assert !field.save |
|
41 | 41 | assert_include I18n.t('activerecord.errors.messages.invalid'), |
|
42 | 42 | field.errors[:regexp] |
|
43 | 43 | field.regexp = '[a-z0-9]' |
|
44 | 44 | assert field.save |
|
45 | 45 | end |
|
46 | 46 | |
|
47 | 47 | def test_default_value_should_be_validated |
|
48 | 48 | field = CustomField.new(:name => 'Test', :field_format => 'int') |
|
49 | 49 | field.default_value = 'abc' |
|
50 | 50 | assert !field.valid? |
|
51 | 51 | field.default_value = '6' |
|
52 | 52 | assert field.valid? |
|
53 | 53 | end |
|
54 | 54 | |
|
55 | 55 | def test_default_value_should_not_be_validated_when_blank |
|
56 | 56 | field = CustomField.new(:name => 'Test', :field_format => 'list', :possible_values => ['a', 'b'], :is_required => true, :default_value => '') |
|
57 | 57 | assert field.valid? |
|
58 | 58 | end |
|
59 | 59 | |
|
60 | 60 | def test_field_format_should_be_validated |
|
61 | 61 | field = CustomField.new(:name => 'Test', :field_format => 'foo') |
|
62 | 62 | assert !field.valid? |
|
63 | 63 | end |
|
64 | 64 | |
|
65 | 65 | def test_field_format_validation_should_accept_formats_added_at_runtime |
|
66 | 66 | Redmine::FieldFormat.add 'foobar', Class.new(Redmine::FieldFormat::Base) |
|
67 | 67 | |
|
68 | 68 | field = CustomField.new(:name => 'Some Custom Field', :field_format => 'foobar') |
|
69 | 69 | assert field.valid?, 'field should be valid' |
|
70 | 70 | ensure |
|
71 | 71 | Redmine::FieldFormat.delete 'foobar' |
|
72 | 72 | end |
|
73 | 73 | |
|
74 | 74 | def test_should_not_change_field_format_of_existing_custom_field |
|
75 | 75 | field = CustomField.find(1) |
|
76 | 76 | field.field_format = 'int' |
|
77 | 77 | assert_equal 'list', field.field_format |
|
78 | 78 | end |
|
79 | 79 | |
|
80 | 80 | def test_possible_values_should_accept_an_array |
|
81 | 81 | field = CustomField.new |
|
82 | 82 | field.possible_values = ["One value", ""] |
|
83 | 83 | assert_equal ["One value"], field.possible_values |
|
84 | 84 | end |
|
85 | 85 | |
|
86 | 86 | def test_possible_values_should_accept_a_string |
|
87 | 87 | field = CustomField.new |
|
88 | 88 | field.possible_values = "One value" |
|
89 | 89 | assert_equal ["One value"], field.possible_values |
|
90 | 90 | end |
|
91 | 91 | |
|
92 | 92 | def test_possible_values_should_accept_a_multiline_string |
|
93 | 93 | field = CustomField.new |
|
94 | 94 | field.possible_values = "One value\nAnd another one \r\n \n" |
|
95 | 95 | assert_equal ["One value", "And another one"], field.possible_values |
|
96 | 96 | end |
|
97 | 97 | |
|
98 | 98 | if "string".respond_to?(:encoding) |
|
99 | 99 | def test_possible_values_stored_as_binary_should_be_utf8_encoded |
|
100 | 100 | field = CustomField.find(11) |
|
101 | 101 | assert_kind_of Array, field.possible_values |
|
102 | 102 | assert field.possible_values.size > 0 |
|
103 | 103 | field.possible_values.each do |value| |
|
104 | 104 | assert_equal "UTF-8", value.encoding.name |
|
105 | 105 | end |
|
106 | 106 | end |
|
107 | 107 | end |
|
108 | 108 | |
|
109 | 109 | def test_destroy |
|
110 | 110 | field = CustomField.find(1) |
|
111 | 111 | assert field.destroy |
|
112 | 112 | end |
|
113 | 113 | |
|
114 | 114 | def test_new_subclass_instance_should_return_an_instance |
|
115 | 115 | f = CustomField.new_subclass_instance('IssueCustomField') |
|
116 | 116 | assert_kind_of IssueCustomField, f |
|
117 | 117 | end |
|
118 | 118 | |
|
119 | 119 | def test_new_subclass_instance_should_set_attributes |
|
120 | 120 | f = CustomField.new_subclass_instance('IssueCustomField', :name => 'Test') |
|
121 | 121 | assert_kind_of IssueCustomField, f |
|
122 | 122 | assert_equal 'Test', f.name |
|
123 | 123 | end |
|
124 | 124 | |
|
125 | 125 | def test_new_subclass_instance_with_invalid_class_name_should_return_nil |
|
126 | 126 | assert_nil CustomField.new_subclass_instance('WrongClassName') |
|
127 | 127 | end |
|
128 | 128 | |
|
129 | 129 | def test_new_subclass_instance_with_non_subclass_name_should_return_nil |
|
130 | 130 | assert_nil CustomField.new_subclass_instance('Project') |
|
131 | 131 | end |
|
132 | 132 | |
|
133 | 133 | def test_string_field_validation_with_blank_value |
|
134 | 134 | f = CustomField.new(:field_format => 'string') |
|
135 | 135 | |
|
136 | 136 | assert f.valid_field_value?(nil) |
|
137 | 137 | assert f.valid_field_value?('') |
|
138 | 138 | |
|
139 | 139 | f.is_required = true |
|
140 | 140 | assert !f.valid_field_value?(nil) |
|
141 | 141 | assert !f.valid_field_value?('') |
|
142 | 142 | end |
|
143 | 143 | |
|
144 | 144 | def test_string_field_validation_with_min_and_max_lengths |
|
145 | 145 | f = CustomField.new(:field_format => 'string', :min_length => 2, :max_length => 5) |
|
146 | 146 | |
|
147 | 147 | assert f.valid_field_value?(nil) |
|
148 | 148 | assert f.valid_field_value?('') |
|
149 | 149 | assert !f.valid_field_value?(' ') |
|
150 | 150 | assert f.valid_field_value?('a' * 2) |
|
151 | 151 | assert !f.valid_field_value?('a') |
|
152 | 152 | assert !f.valid_field_value?('a' * 6) |
|
153 | 153 | end |
|
154 | 154 | |
|
155 | 155 | def test_string_field_validation_with_regexp |
|
156 | 156 | f = CustomField.new(:field_format => 'string', :regexp => '^[A-Z0-9]*$') |
|
157 | 157 | |
|
158 | 158 | assert f.valid_field_value?(nil) |
|
159 | 159 | assert f.valid_field_value?('') |
|
160 | 160 | assert !f.valid_field_value?(' ') |
|
161 | 161 | assert f.valid_field_value?('ABC') |
|
162 | 162 | assert !f.valid_field_value?('abc') |
|
163 | 163 | end |
|
164 | 164 | |
|
165 | 165 | def test_date_field_validation |
|
166 | 166 | f = CustomField.new(:field_format => 'date') |
|
167 | 167 | |
|
168 | 168 | assert f.valid_field_value?(nil) |
|
169 | 169 | assert f.valid_field_value?('') |
|
170 | 170 | assert !f.valid_field_value?(' ') |
|
171 | 171 | assert f.valid_field_value?('1975-07-14') |
|
172 | 172 | assert !f.valid_field_value?('1975-07-33') |
|
173 | 173 | assert !f.valid_field_value?('abc') |
|
174 | 174 | end |
|
175 | 175 | |
|
176 | 176 | def test_list_field_validation |
|
177 | 177 | f = CustomField.new(:field_format => 'list', :possible_values => ['value1', 'value2']) |
|
178 | 178 | |
|
179 | 179 | assert f.valid_field_value?(nil) |
|
180 | 180 | assert f.valid_field_value?('') |
|
181 | 181 | assert !f.valid_field_value?(' ') |
|
182 | 182 | assert f.valid_field_value?('value2') |
|
183 | 183 | assert !f.valid_field_value?('abc') |
|
184 | 184 | end |
|
185 | 185 | |
|
186 | 186 | def test_int_field_validation |
|
187 | 187 | f = CustomField.new(:field_format => 'int') |
|
188 | 188 | |
|
189 | 189 | assert f.valid_field_value?(nil) |
|
190 | 190 | assert f.valid_field_value?('') |
|
191 | 191 | assert !f.valid_field_value?(' ') |
|
192 | 192 | assert f.valid_field_value?('123') |
|
193 | 193 | assert f.valid_field_value?('+123') |
|
194 | 194 | assert f.valid_field_value?('-123') |
|
195 | 195 | assert !f.valid_field_value?('6abc') |
|
196 | 196 | end |
|
197 | 197 | |
|
198 | 198 | def test_float_field_validation |
|
199 | 199 | f = CustomField.new(:field_format => 'float') |
|
200 | 200 | |
|
201 | 201 | assert f.valid_field_value?(nil) |
|
202 | 202 | assert f.valid_field_value?('') |
|
203 | 203 | assert !f.valid_field_value?(' ') |
|
204 | 204 | assert f.valid_field_value?('11.2') |
|
205 | 205 | assert f.valid_field_value?('-6.250') |
|
206 | 206 | assert f.valid_field_value?('5') |
|
207 | 207 | assert !f.valid_field_value?('6abc') |
|
208 | 208 | end |
|
209 | 209 | |
|
210 | 210 | def test_multi_field_validation |
|
211 | 211 | f = CustomField.new(:field_format => 'list', :multiple => 'true', :possible_values => ['value1', 'value2']) |
|
212 | 212 | |
|
213 | 213 | assert f.valid_field_value?(nil) |
|
214 | 214 | assert f.valid_field_value?('') |
|
215 | 215 | assert !f.valid_field_value?(' ') |
|
216 | 216 | assert f.valid_field_value?([]) |
|
217 | 217 | assert f.valid_field_value?([nil]) |
|
218 | 218 | assert f.valid_field_value?(['']) |
|
219 | 219 | assert !f.valid_field_value?([' ']) |
|
220 | 220 | |
|
221 | 221 | assert f.valid_field_value?('value2') |
|
222 | 222 | assert !f.valid_field_value?('abc') |
|
223 | 223 | |
|
224 | 224 | assert f.valid_field_value?(['value2']) |
|
225 | 225 | assert !f.valid_field_value?(['abc']) |
|
226 | 226 | |
|
227 | 227 | assert f.valid_field_value?(['', 'value2']) |
|
228 | 228 | assert !f.valid_field_value?(['', 'abc']) |
|
229 | 229 | |
|
230 | 230 | assert f.valid_field_value?(['value1', 'value2']) |
|
231 | 231 | assert !f.valid_field_value?(['value1', 'abc']) |
|
232 | 232 | end |
|
233 | 233 | |
|
234 | 234 | def test_changing_multiple_to_false_should_delete_multiple_values |
|
235 | 235 | field = ProjectCustomField.create!(:name => 'field', :field_format => 'list', :multiple => 'true', :possible_values => ['field1', 'field2']) |
|
236 | 236 | other = ProjectCustomField.create!(:name => 'other', :field_format => 'list', :multiple => 'true', :possible_values => ['other1', 'other2']) |
|
237 | 237 | |
|
238 | 238 | item_with_multiple_values = Project.generate!(:custom_field_values => {field.id => ['field1', 'field2'], other.id => ['other1', 'other2']}) |
|
239 | 239 | item_with_single_values = Project.generate!(:custom_field_values => {field.id => ['field1'], other.id => ['other2']}) |
|
240 | 240 | |
|
241 | 241 | assert_difference 'CustomValue.count', -1 do |
|
242 | 242 | field.multiple = false |
|
243 | 243 | field.save! |
|
244 | 244 | end |
|
245 | 245 | |
|
246 | 246 | item_with_multiple_values = Project.find(item_with_multiple_values.id) |
|
247 | 247 | assert_kind_of String, item_with_multiple_values.custom_field_value(field) |
|
248 | 248 | assert_kind_of Array, item_with_multiple_values.custom_field_value(other) |
|
249 | 249 | assert_equal 2, item_with_multiple_values.custom_field_value(other).size |
|
250 | 250 | end |
|
251 | 251 | |
|
252 | 252 | def test_value_class_should_return_the_class_used_for_fields_values |
|
253 | 253 | assert_equal User, CustomField.new(:field_format => 'user').value_class |
|
254 | 254 | assert_equal Version, CustomField.new(:field_format => 'version').value_class |
|
255 | 255 | end |
|
256 | 256 | |
|
257 | 257 | def test_value_class_should_return_nil_for_other_fields |
|
258 | 258 | assert_nil CustomField.new(:field_format => 'text').value_class |
|
259 | 259 | assert_nil CustomField.new.value_class |
|
260 | 260 | end |
|
261 | 261 | |
|
262 | 262 | def test_value_from_keyword_for_list_custom_field |
|
263 | 263 | field = CustomField.find(1) |
|
264 | 264 | assert_equal 'PostgreSQL', field.value_from_keyword('postgresql', Issue.find(1)) |
|
265 | 265 | end |
|
266 | 266 | |
|
267 | 267 | def test_visibile_scope_with_admin_should_return_all_custom_fields |
|
268 | 268 | CustomField.delete_all |
|
269 | 269 | fields = [ |
|
270 | 270 | CustomField.generate!(:visible => true), |
|
271 | 271 | CustomField.generate!(:visible => false), |
|
272 | 272 | CustomField.generate!(:visible => false, :role_ids => [1, 3]), |
|
273 | 273 | CustomField.generate!(:visible => false, :role_ids => [1, 2]), |
|
274 | 274 | ] |
|
275 | 275 | |
|
276 | 276 | assert_equal 4, CustomField.visible(User.find(1)).count |
|
277 | 277 | end |
|
278 | 278 | |
|
279 | 279 | def test_visibile_scope_with_non_admin_user_should_return_visible_custom_fields |
|
280 | 280 | CustomField.delete_all |
|
281 | 281 | fields = [ |
|
282 | 282 | CustomField.generate!(:visible => true), |
|
283 | 283 | CustomField.generate!(:visible => false), |
|
284 | 284 | CustomField.generate!(:visible => false, :role_ids => [1, 3]), |
|
285 | 285 | CustomField.generate!(:visible => false, :role_ids => [1, 2]), |
|
286 | 286 | ] |
|
287 | 287 | user = User.generate! |
|
288 | 288 | User.add_to_project(user, Project.first, Role.find(3)) |
|
289 | 289 | |
|
290 | 290 | assert_equal [fields[0], fields[2]], CustomField.visible(user).order("id").to_a |
|
291 | 291 | end |
|
292 | 292 | |
|
293 | 293 | def test_visibile_scope_with_anonymous_user_should_return_visible_custom_fields |
|
294 | 294 | CustomField.delete_all |
|
295 | 295 | fields = [ |
|
296 | 296 | CustomField.generate!(:visible => true), |
|
297 | 297 | CustomField.generate!(:visible => false), |
|
298 | 298 | CustomField.generate!(:visible => false, :role_ids => [1, 3]), |
|
299 | 299 | CustomField.generate!(:visible => false, :role_ids => [1, 2]), |
|
300 | 300 | ] |
|
301 | 301 | |
|
302 | 302 | assert_equal [fields[0]], CustomField.visible(User.anonymous).order("id").to_a |
|
303 | 303 | end |
|
304 | 304 | |
|
305 | 305 | def test_float_cast_blank_value_should_return_nil |
|
306 | 306 | field = CustomField.new(:field_format => 'float') |
|
307 | 307 | assert_equal nil, field.cast_value(nil) |
|
308 | 308 | assert_equal nil, field.cast_value('') |
|
309 | 309 | end |
|
310 | 310 | |
|
311 | 311 | def test_float_cast_valid_value_should_return_float |
|
312 | 312 | field = CustomField.new(:field_format => 'float') |
|
313 | 313 | assert_equal 12.0, field.cast_value('12') |
|
314 | 314 | assert_equal 12.5, field.cast_value('12.5') |
|
315 | 315 | assert_equal 12.5, field.cast_value('+12.5') |
|
316 | 316 | assert_equal -12.5, field.cast_value('-12.5') |
|
317 | 317 | end |
|
318 | 318 | end |
General Comments 0
You need to be logged in to leave comments.
Login now