Mercurial > hg > soundsoftware-site
comparison .svn/pristine/ec/ecd4b60dc00a13b08ce25da84aad8d2582d6ea63.svn-base @ 1295:622f24f53b42 redmine-2.3
Update to Redmine SVN revision 11972 on 2.3-stable branch
author | Chris Cannam |
---|---|
date | Fri, 14 Jun 2013 09:02:21 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1294:3e4c3460b6ca | 1295:622f24f53b42 |
---|---|
1 # Redmine - project management software | |
2 # Copyright (C) 2006-2012 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_should_not_change_field_format_of_existing_custom_field | |
61 field = CustomField.find(1) | |
62 field.field_format = 'int' | |
63 assert_equal 'list', field.field_format | |
64 end | |
65 | |
66 def test_possible_values_should_accept_an_array | |
67 field = CustomField.new | |
68 field.possible_values = ["One value", ""] | |
69 assert_equal ["One value"], field.possible_values | |
70 end | |
71 | |
72 def test_possible_values_should_accept_a_string | |
73 field = CustomField.new | |
74 field.possible_values = "One value" | |
75 assert_equal ["One value"], field.possible_values | |
76 end | |
77 | |
78 def test_possible_values_should_accept_a_multiline_string | |
79 field = CustomField.new | |
80 field.possible_values = "One value\nAnd another one \r\n \n" | |
81 assert_equal ["One value", "And another one"], field.possible_values | |
82 end | |
83 | |
84 if "string".respond_to?(:encoding) | |
85 def test_possible_values_stored_as_binary_should_be_utf8_encoded | |
86 field = CustomField.find(11) | |
87 assert_kind_of Array, field.possible_values | |
88 assert field.possible_values.size > 0 | |
89 field.possible_values.each do |value| | |
90 assert_equal "UTF-8", value.encoding.name | |
91 end | |
92 end | |
93 end | |
94 | |
95 def test_destroy | |
96 field = CustomField.find(1) | |
97 assert field.destroy | |
98 end | |
99 | |
100 def test_new_subclass_instance_should_return_an_instance | |
101 f = CustomField.new_subclass_instance('IssueCustomField') | |
102 assert_kind_of IssueCustomField, f | |
103 end | |
104 | |
105 def test_new_subclass_instance_should_set_attributes | |
106 f = CustomField.new_subclass_instance('IssueCustomField', :name => 'Test') | |
107 assert_kind_of IssueCustomField, f | |
108 assert_equal 'Test', f.name | |
109 end | |
110 | |
111 def test_new_subclass_instance_with_invalid_class_name_should_return_nil | |
112 assert_nil CustomField.new_subclass_instance('WrongClassName') | |
113 end | |
114 | |
115 def test_new_subclass_instance_with_non_subclass_name_should_return_nil | |
116 assert_nil CustomField.new_subclass_instance('Project') | |
117 end | |
118 | |
119 def test_string_field_validation_with_blank_value | |
120 f = CustomField.new(:field_format => 'string') | |
121 | |
122 assert f.valid_field_value?(nil) | |
123 assert f.valid_field_value?('') | |
124 | |
125 f.is_required = true | |
126 assert !f.valid_field_value?(nil) | |
127 assert !f.valid_field_value?('') | |
128 end | |
129 | |
130 def test_string_field_validation_with_min_and_max_lengths | |
131 f = CustomField.new(:field_format => 'string', :min_length => 2, :max_length => 5) | |
132 | |
133 assert f.valid_field_value?(nil) | |
134 assert f.valid_field_value?('') | |
135 assert f.valid_field_value?('a' * 2) | |
136 assert !f.valid_field_value?('a') | |
137 assert !f.valid_field_value?('a' * 6) | |
138 end | |
139 | |
140 def test_string_field_validation_with_regexp | |
141 f = CustomField.new(:field_format => 'string', :regexp => '^[A-Z0-9]*$') | |
142 | |
143 assert f.valid_field_value?(nil) | |
144 assert f.valid_field_value?('') | |
145 assert f.valid_field_value?('ABC') | |
146 assert !f.valid_field_value?('abc') | |
147 end | |
148 | |
149 def test_date_field_validation | |
150 f = CustomField.new(:field_format => 'date') | |
151 | |
152 assert f.valid_field_value?(nil) | |
153 assert f.valid_field_value?('') | |
154 assert f.valid_field_value?('1975-07-14') | |
155 assert !f.valid_field_value?('1975-07-33') | |
156 assert !f.valid_field_value?('abc') | |
157 end | |
158 | |
159 def test_list_field_validation | |
160 f = CustomField.new(:field_format => 'list', :possible_values => ['value1', 'value2']) | |
161 | |
162 assert f.valid_field_value?(nil) | |
163 assert f.valid_field_value?('') | |
164 assert f.valid_field_value?('value2') | |
165 assert !f.valid_field_value?('abc') | |
166 end | |
167 | |
168 def test_int_field_validation | |
169 f = CustomField.new(:field_format => 'int') | |
170 | |
171 assert f.valid_field_value?(nil) | |
172 assert f.valid_field_value?('') | |
173 assert f.valid_field_value?('123') | |
174 assert f.valid_field_value?('+123') | |
175 assert f.valid_field_value?('-123') | |
176 assert !f.valid_field_value?('6abc') | |
177 end | |
178 | |
179 def test_float_field_validation | |
180 f = CustomField.new(:field_format => 'float') | |
181 | |
182 assert f.valid_field_value?(nil) | |
183 assert f.valid_field_value?('') | |
184 assert f.valid_field_value?('11.2') | |
185 assert f.valid_field_value?('-6.250') | |
186 assert f.valid_field_value?('5') | |
187 assert !f.valid_field_value?('6abc') | |
188 end | |
189 | |
190 def test_multi_field_validation | |
191 f = CustomField.new(:field_format => 'list', :multiple => 'true', :possible_values => ['value1', 'value2']) | |
192 | |
193 assert f.valid_field_value?(nil) | |
194 assert f.valid_field_value?('') | |
195 assert f.valid_field_value?([]) | |
196 assert f.valid_field_value?([nil]) | |
197 assert f.valid_field_value?(['']) | |
198 | |
199 assert f.valid_field_value?('value2') | |
200 assert !f.valid_field_value?('abc') | |
201 | |
202 assert f.valid_field_value?(['value2']) | |
203 assert !f.valid_field_value?(['abc']) | |
204 | |
205 assert f.valid_field_value?(['', 'value2']) | |
206 assert !f.valid_field_value?(['', 'abc']) | |
207 | |
208 assert f.valid_field_value?(['value1', 'value2']) | |
209 assert !f.valid_field_value?(['value1', 'abc']) | |
210 end | |
211 | |
212 def test_value_class_should_return_the_class_used_for_fields_values | |
213 assert_equal User, CustomField.new(:field_format => 'user').value_class | |
214 assert_equal Version, CustomField.new(:field_format => 'version').value_class | |
215 end | |
216 | |
217 def test_value_class_should_return_nil_for_other_fields | |
218 assert_nil CustomField.new(:field_format => 'text').value_class | |
219 assert_nil CustomField.new.value_class | |
220 end | |
221 | |
222 def test_value_from_keyword_for_list_custom_field | |
223 field = CustomField.find(1) | |
224 assert_equal 'PostgreSQL', field.value_from_keyword('postgresql', Issue.find(1)) | |
225 end | |
226 end |