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