Mercurial > hg > soundsoftware-site
comparison .svn/pristine/c5/c5d7758e547d0bfbbce3e5c93eb387772d220785.svn-base @ 1517:dffacf8a6908 redmine-2.5
Update to Redmine SVN revision 13367 on 2.5-stable branch
author | Chris Cannam |
---|---|
date | Tue, 09 Sep 2014 09:29:00 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1516:b450a9d58aed | 1517:dffacf8a6908 |
---|---|
1 # Redmine - project management software | |
2 # Copyright (C) 2006-2014 Jean-Philippe Lang | |
3 # | |
4 # This program is free software; you can redistribute it and/or | |
5 # modify it under the terms of the GNU General Public License | |
6 # as published by the Free Software Foundation; either version 2 | |
7 # of the License, or (at your option) any later version. | |
8 # | |
9 # This program is distributed in the hope that it will be useful, | |
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 # GNU General Public License for more details. | |
13 # | |
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 | |
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
17 | |
18 require File.expand_path('../../test_helper', __FILE__) | |
19 | |
20 class CustomFieldTest < ActiveSupport::TestCase | |
21 fixtures :custom_fields | |
22 | |
23 def test_create | |
24 field = UserCustomField.new(:name => 'Money money money', :field_format => 'float') | |
25 assert field.save | |
26 end | |
27 | |
28 def test_before_validation | |
29 field = CustomField.new(:name => 'test_before_validation', :field_format => 'int') | |
30 field.searchable = true | |
31 assert field.save | |
32 assert_equal false, field.searchable | |
33 field.searchable = true | |
34 assert field.save | |
35 assert_equal false, field.searchable | |
36 end | |
37 | |
38 def test_regexp_validation | |
39 field = IssueCustomField.new(:name => 'regexp', :field_format => 'text', :regexp => '[a-z0-9') | |
40 assert !field.save | |
41 assert_include I18n.t('activerecord.errors.messages.invalid'), | |
42 field.errors[:regexp] | |
43 field.regexp = '[a-z0-9]' | |
44 assert field.save | |
45 end | |
46 | |
47 def test_default_value_should_be_validated | |
48 field = CustomField.new(:name => 'Test', :field_format => 'int') | |
49 field.default_value = 'abc' | |
50 assert !field.valid? | |
51 field.default_value = '6' | |
52 assert field.valid? | |
53 end | |
54 | |
55 def test_default_value_should_not_be_validated_when_blank | |
56 field = CustomField.new(:name => 'Test', :field_format => 'list', :possible_values => ['a', 'b'], :is_required => true, :default_value => '') | |
57 assert field.valid? | |
58 end | |
59 | |
60 def test_field_format_should_be_validated | |
61 field = CustomField.new(:name => 'Test', :field_format => 'foo') | |
62 assert !field.valid? | |
63 end | |
64 | |
65 def test_field_format_validation_should_accept_formats_added_at_runtime | |
66 Redmine::FieldFormat.add 'foobar', Class.new(Redmine::FieldFormat::Base) | |
67 | |
68 field = CustomField.new(:name => 'Some Custom Field', :field_format => 'foobar') | |
69 assert field.valid?, 'field should be valid' | |
70 ensure | |
71 Redmine::FieldFormat.delete 'foobar' | |
72 end | |
73 | |
74 def test_should_not_change_field_format_of_existing_custom_field | |
75 field = CustomField.find(1) | |
76 field.field_format = 'int' | |
77 assert_equal 'list', field.field_format | |
78 end | |
79 | |
80 def test_possible_values_should_accept_an_array | |
81 field = CustomField.new | |
82 field.possible_values = ["One value", ""] | |
83 assert_equal ["One value"], field.possible_values | |
84 end | |
85 | |
86 def test_possible_values_should_accept_a_string | |
87 field = CustomField.new | |
88 field.possible_values = "One value" | |
89 assert_equal ["One value"], field.possible_values | |
90 end | |
91 | |
92 def test_possible_values_should_accept_a_multiline_string | |
93 field = CustomField.new | |
94 field.possible_values = "One value\nAnd another one \r\n \n" | |
95 assert_equal ["One value", "And another one"], field.possible_values | |
96 end | |
97 | |
98 if "string".respond_to?(:encoding) | |
99 def test_possible_values_stored_as_binary_should_be_utf8_encoded | |
100 field = CustomField.find(11) | |
101 assert_kind_of Array, field.possible_values | |
102 assert field.possible_values.size > 0 | |
103 field.possible_values.each do |value| | |
104 assert_equal "UTF-8", value.encoding.name | |
105 end | |
106 end | |
107 end | |
108 | |
109 def test_destroy | |
110 field = CustomField.find(1) | |
111 assert field.destroy | |
112 end | |
113 | |
114 def test_new_subclass_instance_should_return_an_instance | |
115 f = CustomField.new_subclass_instance('IssueCustomField') | |
116 assert_kind_of IssueCustomField, f | |
117 end | |
118 | |
119 def test_new_subclass_instance_should_set_attributes | |
120 f = CustomField.new_subclass_instance('IssueCustomField', :name => 'Test') | |
121 assert_kind_of IssueCustomField, f | |
122 assert_equal 'Test', f.name | |
123 end | |
124 | |
125 def test_new_subclass_instance_with_invalid_class_name_should_return_nil | |
126 assert_nil CustomField.new_subclass_instance('WrongClassName') | |
127 end | |
128 | |
129 def test_new_subclass_instance_with_non_subclass_name_should_return_nil | |
130 assert_nil CustomField.new_subclass_instance('Project') | |
131 end | |
132 | |
133 def test_string_field_validation_with_blank_value | |
134 f = CustomField.new(:field_format => 'string') | |
135 | |
136 assert f.valid_field_value?(nil) | |
137 assert f.valid_field_value?('') | |
138 | |
139 f.is_required = true | |
140 assert !f.valid_field_value?(nil) | |
141 assert !f.valid_field_value?('') | |
142 end | |
143 | |
144 def test_string_field_validation_with_min_and_max_lengths | |
145 f = CustomField.new(:field_format => 'string', :min_length => 2, :max_length => 5) | |
146 | |
147 assert f.valid_field_value?(nil) | |
148 assert f.valid_field_value?('') | |
149 assert !f.valid_field_value?(' ') | |
150 assert f.valid_field_value?('a' * 2) | |
151 assert !f.valid_field_value?('a') | |
152 assert !f.valid_field_value?('a' * 6) | |
153 end | |
154 | |
155 def test_string_field_validation_with_regexp | |
156 f = CustomField.new(:field_format => 'string', :regexp => '^[A-Z0-9]*$') | |
157 | |
158 assert f.valid_field_value?(nil) | |
159 assert f.valid_field_value?('') | |
160 assert !f.valid_field_value?(' ') | |
161 assert f.valid_field_value?('ABC') | |
162 assert !f.valid_field_value?('abc') | |
163 end | |
164 | |
165 def test_date_field_validation | |
166 f = CustomField.new(:field_format => 'date') | |
167 | |
168 assert f.valid_field_value?(nil) | |
169 assert f.valid_field_value?('') | |
170 assert !f.valid_field_value?(' ') | |
171 assert f.valid_field_value?('1975-07-14') | |
172 assert !f.valid_field_value?('1975-07-33') | |
173 assert !f.valid_field_value?('abc') | |
174 end | |
175 | |
176 def test_list_field_validation | |
177 f = CustomField.new(:field_format => 'list', :possible_values => ['value1', 'value2']) | |
178 | |
179 assert f.valid_field_value?(nil) | |
180 assert f.valid_field_value?('') | |
181 assert !f.valid_field_value?(' ') | |
182 assert f.valid_field_value?('value2') | |
183 assert !f.valid_field_value?('abc') | |
184 end | |
185 | |
186 def test_int_field_validation | |
187 f = CustomField.new(:field_format => 'int') | |
188 | |
189 assert f.valid_field_value?(nil) | |
190 assert f.valid_field_value?('') | |
191 assert !f.valid_field_value?(' ') | |
192 assert f.valid_field_value?('123') | |
193 assert f.valid_field_value?('+123') | |
194 assert f.valid_field_value?('-123') | |
195 assert !f.valid_field_value?('6abc') | |
196 end | |
197 | |
198 def test_float_field_validation | |
199 f = CustomField.new(:field_format => 'float') | |
200 | |
201 assert f.valid_field_value?(nil) | |
202 assert f.valid_field_value?('') | |
203 assert !f.valid_field_value?(' ') | |
204 assert f.valid_field_value?('11.2') | |
205 assert f.valid_field_value?('-6.250') | |
206 assert f.valid_field_value?('5') | |
207 assert !f.valid_field_value?('6abc') | |
208 end | |
209 | |
210 def test_multi_field_validation | |
211 f = CustomField.new(:field_format => 'list', :multiple => 'true', :possible_values => ['value1', 'value2']) | |
212 | |
213 assert f.valid_field_value?(nil) | |
214 assert f.valid_field_value?('') | |
215 assert !f.valid_field_value?(' ') | |
216 assert f.valid_field_value?([]) | |
217 assert f.valid_field_value?([nil]) | |
218 assert f.valid_field_value?(['']) | |
219 assert !f.valid_field_value?([' ']) | |
220 | |
221 assert f.valid_field_value?('value2') | |
222 assert !f.valid_field_value?('abc') | |
223 | |
224 assert f.valid_field_value?(['value2']) | |
225 assert !f.valid_field_value?(['abc']) | |
226 | |
227 assert f.valid_field_value?(['', 'value2']) | |
228 assert !f.valid_field_value?(['', 'abc']) | |
229 | |
230 assert f.valid_field_value?(['value1', 'value2']) | |
231 assert !f.valid_field_value?(['value1', 'abc']) | |
232 end | |
233 | |
234 def test_changing_multiple_to_false_should_delete_multiple_values | |
235 field = ProjectCustomField.create!(:name => 'field', :field_format => 'list', :multiple => 'true', :possible_values => ['field1', 'field2']) | |
236 other = ProjectCustomField.create!(:name => 'other', :field_format => 'list', :multiple => 'true', :possible_values => ['other1', 'other2']) | |
237 | |
238 item_with_multiple_values = Project.generate!(:custom_field_values => {field.id => ['field1', 'field2'], other.id => ['other1', 'other2']}) | |
239 item_with_single_values = Project.generate!(:custom_field_values => {field.id => ['field1'], other.id => ['other2']}) | |
240 | |
241 assert_difference 'CustomValue.count', -1 do | |
242 field.multiple = false | |
243 field.save! | |
244 end | |
245 | |
246 item_with_multiple_values = Project.find(item_with_multiple_values.id) | |
247 assert_kind_of String, item_with_multiple_values.custom_field_value(field) | |
248 assert_kind_of Array, item_with_multiple_values.custom_field_value(other) | |
249 assert_equal 2, item_with_multiple_values.custom_field_value(other).size | |
250 end | |
251 | |
252 def test_value_class_should_return_the_class_used_for_fields_values | |
253 assert_equal User, CustomField.new(:field_format => 'user').value_class | |
254 assert_equal Version, CustomField.new(:field_format => 'version').value_class | |
255 end | |
256 | |
257 def test_value_class_should_return_nil_for_other_fields | |
258 assert_nil CustomField.new(:field_format => 'text').value_class | |
259 assert_nil CustomField.new.value_class | |
260 end | |
261 | |
262 def test_value_from_keyword_for_list_custom_field | |
263 field = CustomField.find(1) | |
264 assert_equal 'PostgreSQL', field.value_from_keyword('postgresql', Issue.find(1)) | |
265 end | |
266 | |
267 def test_visibile_scope_with_admin_should_return_all_custom_fields | |
268 CustomField.delete_all | |
269 fields = [ | |
270 CustomField.generate!(:visible => true), | |
271 CustomField.generate!(:visible => false), | |
272 CustomField.generate!(:visible => false, :role_ids => [1, 3]), | |
273 CustomField.generate!(:visible => false, :role_ids => [1, 2]), | |
274 ] | |
275 | |
276 assert_equal 4, CustomField.visible(User.find(1)).count | |
277 end | |
278 | |
279 def test_visibile_scope_with_non_admin_user_should_return_visible_custom_fields | |
280 CustomField.delete_all | |
281 fields = [ | |
282 CustomField.generate!(:visible => true), | |
283 CustomField.generate!(:visible => false), | |
284 CustomField.generate!(:visible => false, :role_ids => [1, 3]), | |
285 CustomField.generate!(:visible => false, :role_ids => [1, 2]), | |
286 ] | |
287 user = User.generate! | |
288 User.add_to_project(user, Project.first, Role.find(3)) | |
289 | |
290 assert_equal [fields[0], fields[2]], CustomField.visible(user).order("id").to_a | |
291 end | |
292 | |
293 def test_visibile_scope_with_anonymous_user_should_return_visible_custom_fields | |
294 CustomField.delete_all | |
295 fields = [ | |
296 CustomField.generate!(:visible => true), | |
297 CustomField.generate!(:visible => false), | |
298 CustomField.generate!(:visible => false, :role_ids => [1, 3]), | |
299 CustomField.generate!(:visible => false, :role_ids => [1, 2]), | |
300 ] | |
301 | |
302 assert_equal [fields[0]], CustomField.visible(User.anonymous).order("id").to_a | |
303 end | |
304 | |
305 def test_float_cast_blank_value_should_return_nil | |
306 field = CustomField.new(:field_format => 'float') | |
307 assert_equal nil, field.cast_value(nil) | |
308 assert_equal nil, field.cast_value('') | |
309 end | |
310 | |
311 def test_float_cast_valid_value_should_return_float | |
312 field = CustomField.new(:field_format => 'float') | |
313 assert_equal 12.0, field.cast_value('12') | |
314 assert_equal 12.5, field.cast_value('12.5') | |
315 assert_equal 12.5, field.cast_value('+12.5') | |
316 assert_equal -12.5, field.cast_value('-12.5') | |
317 end | |
318 end |