@@ -1,215 +1,215 | |||
|
1 | 1 | # Redmine - project management software |
|
2 | 2 | # Copyright (C) 2006-2012 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 | 21 | fixtures :custom_fields |
|
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_possible_values_should_accept_an_array |
|
61 | 61 | field = CustomField.new |
|
62 | 62 | field.possible_values = ["One value", ""] |
|
63 | 63 | assert_equal ["One value"], field.possible_values |
|
64 | 64 | end |
|
65 | 65 | |
|
66 | 66 | def test_possible_values_should_accept_a_string |
|
67 | 67 | field = CustomField.new |
|
68 | 68 | field.possible_values = "One value" |
|
69 | 69 | assert_equal ["One value"], field.possible_values |
|
70 | 70 | end |
|
71 | 71 | |
|
72 | 72 | def test_possible_values_should_accept_a_multiline_string |
|
73 | 73 | field = CustomField.new |
|
74 | 74 | field.possible_values = "One value\nAnd another one \r\n \n" |
|
75 | 75 | assert_equal ["One value", "And another one"], field.possible_values |
|
76 | 76 | end |
|
77 | 77 | |
|
78 | 78 | if "string".respond_to?(:encoding) |
|
79 | 79 | def test_possible_values_stored_as_binary_should_be_utf8_encoded |
|
80 | 80 | field = CustomField.find(11) |
|
81 | 81 | assert_kind_of Array, field.possible_values |
|
82 | 82 | assert field.possible_values.size > 0 |
|
83 | 83 | field.possible_values.each do |value| |
|
84 | 84 | assert_equal "UTF-8", value.encoding.name |
|
85 | 85 | end |
|
86 | 86 | end |
|
87 | 87 | end |
|
88 | 88 | |
|
89 | 89 | def test_destroy |
|
90 | 90 | field = CustomField.find(1) |
|
91 | 91 | assert field.destroy |
|
92 | 92 | end |
|
93 | 93 | |
|
94 | 94 | def test_new_subclass_instance_should_return_an_instance |
|
95 | 95 | f = CustomField.new_subclass_instance('IssueCustomField') |
|
96 | 96 | assert_kind_of IssueCustomField, f |
|
97 | 97 | end |
|
98 | 98 | |
|
99 | 99 | def test_new_subclass_instance_should_set_attributes |
|
100 | 100 | f = CustomField.new_subclass_instance('IssueCustomField', :name => 'Test') |
|
101 | 101 | assert_kind_of IssueCustomField, f |
|
102 | 102 | assert_equal 'Test', f.name |
|
103 | 103 | end |
|
104 | 104 | |
|
105 | 105 | def test_new_subclass_instance_with_invalid_class_name_should_return_nil |
|
106 | 106 | assert_nil CustomField.new_subclass_instance('WrongClassName') |
|
107 | 107 | end |
|
108 | 108 | |
|
109 | 109 | def test_new_subclass_instance_with_non_subclass_name_should_return_nil |
|
110 | 110 | assert_nil CustomField.new_subclass_instance('Project') |
|
111 | 111 | end |
|
112 | 112 | |
|
113 | 113 | def test_string_field_validation_with_blank_value |
|
114 | 114 | f = CustomField.new(:field_format => 'string') |
|
115 | 115 | |
|
116 | 116 | assert f.valid_field_value?(nil) |
|
117 | 117 | assert f.valid_field_value?('') |
|
118 | 118 | |
|
119 | 119 | f.is_required = true |
|
120 | 120 | assert !f.valid_field_value?(nil) |
|
121 | 121 | assert !f.valid_field_value?('') |
|
122 | 122 | end |
|
123 | 123 | |
|
124 | 124 | def test_string_field_validation_with_min_and_max_lengths |
|
125 | 125 | f = CustomField.new(:field_format => 'string', :min_length => 2, :max_length => 5) |
|
126 | 126 | |
|
127 | 127 | assert f.valid_field_value?(nil) |
|
128 | 128 | assert f.valid_field_value?('') |
|
129 | 129 | assert f.valid_field_value?('a' * 2) |
|
130 | 130 | assert !f.valid_field_value?('a') |
|
131 | 131 | assert !f.valid_field_value?('a' * 6) |
|
132 | 132 | end |
|
133 | 133 | |
|
134 | 134 | def test_string_field_validation_with_regexp |
|
135 | 135 | f = CustomField.new(:field_format => 'string', :regexp => '^[A-Z0-9]*$') |
|
136 | 136 | |
|
137 | 137 | assert f.valid_field_value?(nil) |
|
138 | 138 | assert f.valid_field_value?('') |
|
139 | 139 | assert f.valid_field_value?('ABC') |
|
140 | 140 | assert !f.valid_field_value?('abc') |
|
141 | 141 | end |
|
142 | 142 | |
|
143 | 143 | def test_date_field_validation |
|
144 | 144 | f = CustomField.new(:field_format => 'date') |
|
145 | 145 | |
|
146 | 146 | assert f.valid_field_value?(nil) |
|
147 | 147 | assert f.valid_field_value?('') |
|
148 | 148 | assert f.valid_field_value?('1975-07-14') |
|
149 | 149 | assert !f.valid_field_value?('1975-07-33') |
|
150 | 150 | assert !f.valid_field_value?('abc') |
|
151 | 151 | end |
|
152 | 152 | |
|
153 | 153 | def test_list_field_validation |
|
154 | 154 | f = CustomField.new(:field_format => 'list', :possible_values => ['value1', 'value2']) |
|
155 | 155 | |
|
156 | 156 | assert f.valid_field_value?(nil) |
|
157 | 157 | assert f.valid_field_value?('') |
|
158 | 158 | assert f.valid_field_value?('value2') |
|
159 | 159 | assert !f.valid_field_value?('abc') |
|
160 | 160 | end |
|
161 | 161 | |
|
162 | 162 | def test_int_field_validation |
|
163 | 163 | f = CustomField.new(:field_format => 'int') |
|
164 | 164 | |
|
165 | 165 | assert f.valid_field_value?(nil) |
|
166 | 166 | assert f.valid_field_value?('') |
|
167 | 167 | assert f.valid_field_value?('123') |
|
168 | 168 | assert f.valid_field_value?('+123') |
|
169 | 169 | assert f.valid_field_value?('-123') |
|
170 | 170 | assert !f.valid_field_value?('6abc') |
|
171 | 171 | end |
|
172 | 172 | |
|
173 | 173 | def test_float_field_validation |
|
174 | 174 | f = CustomField.new(:field_format => 'float') |
|
175 | 175 | |
|
176 | 176 | assert f.valid_field_value?(nil) |
|
177 | 177 | assert f.valid_field_value?('') |
|
178 | 178 | assert f.valid_field_value?('11.2') |
|
179 | 179 | assert f.valid_field_value?('-6.250') |
|
180 | 180 | assert f.valid_field_value?('5') |
|
181 | 181 | assert !f.valid_field_value?('6abc') |
|
182 | 182 | end |
|
183 | 183 | |
|
184 | 184 | def test_multi_field_validation |
|
185 | 185 | f = CustomField.new(:field_format => 'list', :multiple => 'true', :possible_values => ['value1', 'value2']) |
|
186 | 186 | |
|
187 | 187 | assert f.valid_field_value?(nil) |
|
188 | 188 | assert f.valid_field_value?('') |
|
189 | 189 | assert f.valid_field_value?([]) |
|
190 | 190 | assert f.valid_field_value?([nil]) |
|
191 | 191 | assert f.valid_field_value?(['']) |
|
192 | 192 | |
|
193 | 193 | assert f.valid_field_value?('value2') |
|
194 | 194 | assert !f.valid_field_value?('abc') |
|
195 | 195 | |
|
196 | 196 | assert f.valid_field_value?(['value2']) |
|
197 | 197 | assert !f.valid_field_value?(['abc']) |
|
198 | 198 | |
|
199 | 199 | assert f.valid_field_value?(['', 'value2']) |
|
200 | 200 | assert !f.valid_field_value?(['', 'abc']) |
|
201 | 201 | |
|
202 | 202 | assert f.valid_field_value?(['value1', 'value2']) |
|
203 | 203 | assert !f.valid_field_value?(['value1', 'abc']) |
|
204 | 204 | end |
|
205 | 205 | |
|
206 | 206 | def test_value_class_should_return_the_class_used_for_fields_values |
|
207 | assert_equal User, CustomField.new(:field_format => 'user') | |
|
208 | assert_equal Version, CustomField.new(:field_format => 'version') | |
|
207 | assert_equal User, CustomField.new(:field_format => 'user').value_class | |
|
208 | assert_equal Version, CustomField.new(:field_format => 'version').value_class | |
|
209 | 209 | end |
|
210 | 210 | |
|
211 | 211 | def test_value_class_should_return_nil_for_other_fields |
|
212 | assert_nil CustomField.new(:field_format => 'text') | |
|
213 | assert_nil CustomField.new | |
|
212 | assert_nil CustomField.new(:field_format => 'text').value_class | |
|
213 | assert_nil CustomField.new.value_class | |
|
214 | 214 | end |
|
215 | 215 | end |
General Comments 0
You need to be logged in to leave comments.
Login now