@@ -1,217 +1,217 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2012 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 | class CustomField < ActiveRecord::Base |
|
18 | class CustomField < ActiveRecord::Base | |
19 | include Redmine::SubclassFactory |
|
19 | include Redmine::SubclassFactory | |
20 |
|
20 | |||
21 | has_many :custom_values, :dependent => :delete_all |
|
21 | has_many :custom_values, :dependent => :delete_all | |
22 | acts_as_list :scope => 'type = \'#{self.class}\'' |
|
22 | acts_as_list :scope => 'type = \'#{self.class}\'' | |
23 | serialize :possible_values |
|
23 | serialize :possible_values | |
24 |
|
24 | |||
25 | validates_presence_of :name, :field_format |
|
25 | validates_presence_of :name, :field_format | |
26 | validates_uniqueness_of :name, :scope => :type |
|
26 | validates_uniqueness_of :name, :scope => :type | |
27 | validates_length_of :name, :maximum => 30 |
|
27 | validates_length_of :name, :maximum => 30 | |
28 | validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats |
|
28 | validates_inclusion_of :field_format, :in => Redmine::CustomFieldFormat.available_formats | |
29 |
|
29 | |||
30 | validate :validate_custom_field |
|
30 | validate :validate_custom_field | |
31 | before_validation :set_searchable |
|
31 | before_validation :set_searchable | |
32 |
|
32 | |||
33 | def initialize(attributes=nil, *args) |
|
33 | def initialize(attributes=nil, *args) | |
34 | super |
|
34 | super | |
35 | self.possible_values ||= [] |
|
35 | self.possible_values ||= [] | |
36 | end |
|
36 | end | |
37 |
|
37 | |||
38 | def set_searchable |
|
38 | def set_searchable | |
39 | # make sure these fields are not searchable |
|
39 | # make sure these fields are not searchable | |
40 | self.searchable = false if %w(int float date bool).include?(field_format) |
|
40 | self.searchable = false if %w(int float date bool).include?(field_format) | |
41 | # make sure only these fields can have multiple values |
|
41 | # make sure only these fields can have multiple values | |
42 | self.multiple = false unless %w(list user version).include?(field_format) |
|
42 | self.multiple = false unless %w(list user version).include?(field_format) | |
43 | true |
|
43 | true | |
44 | end |
|
44 | end | |
45 |
|
45 | |||
46 | def validate_custom_field |
|
46 | def validate_custom_field | |
47 | if self.field_format == "list" |
|
47 | if self.field_format == "list" | |
48 | errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty? |
|
48 | errors.add(:possible_values, :blank) if self.possible_values.nil? || self.possible_values.empty? | |
49 | errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array |
|
49 | errors.add(:possible_values, :invalid) unless self.possible_values.is_a? Array | |
50 | end |
|
50 | end | |
51 |
|
51 | |||
52 | if regexp.present? |
|
52 | if regexp.present? | |
53 | begin |
|
53 | begin | |
54 | Regexp.new(regexp) |
|
54 | Regexp.new(regexp) | |
55 | rescue |
|
55 | rescue | |
56 | errors.add(:regexp, :invalid) |
|
56 | errors.add(:regexp, :invalid) | |
57 | end |
|
57 | end | |
58 | end |
|
58 | end | |
59 |
|
59 | |||
60 |
|
|
60 | if default_value.present? && !valid_field_value?(default_value) | |
61 | errors.add(:default_value, :invalid) |
|
61 | errors.add(:default_value, :invalid) | |
62 | end |
|
62 | end | |
63 | end |
|
63 | end | |
64 |
|
64 | |||
65 | def possible_values_options(obj=nil) |
|
65 | def possible_values_options(obj=nil) | |
66 | case field_format |
|
66 | case field_format | |
67 | when 'user', 'version' |
|
67 | when 'user', 'version' | |
68 | if obj.respond_to?(:project) && obj.project |
|
68 | if obj.respond_to?(:project) && obj.project | |
69 | case field_format |
|
69 | case field_format | |
70 | when 'user' |
|
70 | when 'user' | |
71 | obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]} |
|
71 | obj.project.users.sort.collect {|u| [u.to_s, u.id.to_s]} | |
72 | when 'version' |
|
72 | when 'version' | |
73 | obj.project.shared_versions.sort.collect {|u| [u.to_s, u.id.to_s]} |
|
73 | obj.project.shared_versions.sort.collect {|u| [u.to_s, u.id.to_s]} | |
74 | end |
|
74 | end | |
75 | elsif obj.is_a?(Array) |
|
75 | elsif obj.is_a?(Array) | |
76 | obj.collect {|o| possible_values_options(o)}.inject {|memo, v| memo & v} |
|
76 | obj.collect {|o| possible_values_options(o)}.inject {|memo, v| memo & v} | |
77 | else |
|
77 | else | |
78 | [] |
|
78 | [] | |
79 | end |
|
79 | end | |
80 | else |
|
80 | else | |
81 | read_attribute :possible_values |
|
81 | read_attribute :possible_values | |
82 | end |
|
82 | end | |
83 | end |
|
83 | end | |
84 |
|
84 | |||
85 | def possible_values(obj=nil) |
|
85 | def possible_values(obj=nil) | |
86 | case field_format |
|
86 | case field_format | |
87 | when 'user', 'version' |
|
87 | when 'user', 'version' | |
88 | possible_values_options(obj).collect(&:last) |
|
88 | possible_values_options(obj).collect(&:last) | |
89 | else |
|
89 | else | |
90 | read_attribute :possible_values |
|
90 | read_attribute :possible_values | |
91 | end |
|
91 | end | |
92 | end |
|
92 | end | |
93 |
|
93 | |||
94 | # Makes possible_values accept a multiline string |
|
94 | # Makes possible_values accept a multiline string | |
95 | def possible_values=(arg) |
|
95 | def possible_values=(arg) | |
96 | if arg.is_a?(Array) |
|
96 | if arg.is_a?(Array) | |
97 | write_attribute(:possible_values, arg.compact.collect(&:strip).select {|v| !v.blank?}) |
|
97 | write_attribute(:possible_values, arg.compact.collect(&:strip).select {|v| !v.blank?}) | |
98 | else |
|
98 | else | |
99 | self.possible_values = arg.to_s.split(/[\n\r]+/) |
|
99 | self.possible_values = arg.to_s.split(/[\n\r]+/) | |
100 | end |
|
100 | end | |
101 | end |
|
101 | end | |
102 |
|
102 | |||
103 | def cast_value(value) |
|
103 | def cast_value(value) | |
104 | casted = nil |
|
104 | casted = nil | |
105 | unless value.blank? |
|
105 | unless value.blank? | |
106 | case field_format |
|
106 | case field_format | |
107 | when 'string', 'text', 'list' |
|
107 | when 'string', 'text', 'list' | |
108 | casted = value |
|
108 | casted = value | |
109 | when 'date' |
|
109 | when 'date' | |
110 | casted = begin; value.to_date; rescue; nil end |
|
110 | casted = begin; value.to_date; rescue; nil end | |
111 | when 'bool' |
|
111 | when 'bool' | |
112 | casted = (value == '1' ? true : false) |
|
112 | casted = (value == '1' ? true : false) | |
113 | when 'int' |
|
113 | when 'int' | |
114 | casted = value.to_i |
|
114 | casted = value.to_i | |
115 | when 'float' |
|
115 | when 'float' | |
116 | casted = value.to_f |
|
116 | casted = value.to_f | |
117 | when 'user', 'version' |
|
117 | when 'user', 'version' | |
118 | casted = (value.blank? ? nil : field_format.classify.constantize.find_by_id(value.to_i)) |
|
118 | casted = (value.blank? ? nil : field_format.classify.constantize.find_by_id(value.to_i)) | |
119 | end |
|
119 | end | |
120 | end |
|
120 | end | |
121 | casted |
|
121 | casted | |
122 | end |
|
122 | end | |
123 |
|
123 | |||
124 | # Returns a ORDER BY clause that can used to sort customized |
|
124 | # Returns a ORDER BY clause that can used to sort customized | |
125 | # objects by their value of the custom field. |
|
125 | # objects by their value of the custom field. | |
126 | # Returns false, if the custom field can not be used for sorting. |
|
126 | # Returns false, if the custom field can not be used for sorting. | |
127 | def order_statement |
|
127 | def order_statement | |
128 | return nil if multiple? |
|
128 | return nil if multiple? | |
129 | case field_format |
|
129 | case field_format | |
130 | when 'string', 'text', 'list', 'date', 'bool' |
|
130 | when 'string', 'text', 'list', 'date', 'bool' | |
131 | # COALESCE is here to make sure that blank and NULL values are sorted equally |
|
131 | # COALESCE is here to make sure that blank and NULL values are sorted equally | |
132 | "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" + |
|
132 | "COALESCE((SELECT cv_sort.value FROM #{CustomValue.table_name} cv_sort" + | |
133 | " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" + |
|
133 | " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" + | |
134 | " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" + |
|
134 | " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" + | |
135 | " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')" |
|
135 | " AND cv_sort.custom_field_id=#{id} LIMIT 1), '')" | |
136 | when 'int', 'float' |
|
136 | when 'int', 'float' | |
137 | # Make the database cast values into numeric |
|
137 | # Make the database cast values into numeric | |
138 | # Postgresql will raise an error if a value can not be casted! |
|
138 | # Postgresql will raise an error if a value can not be casted! | |
139 | # CustomValue validations should ensure that it doesn't occur |
|
139 | # CustomValue validations should ensure that it doesn't occur | |
140 | "(SELECT CAST(cv_sort.value AS decimal(60,3)) FROM #{CustomValue.table_name} cv_sort" + |
|
140 | "(SELECT CAST(cv_sort.value AS decimal(60,3)) FROM #{CustomValue.table_name} cv_sort" + | |
141 | " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" + |
|
141 | " WHERE cv_sort.customized_type='#{self.class.customized_class.name}'" + | |
142 | " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" + |
|
142 | " AND cv_sort.customized_id=#{self.class.customized_class.table_name}.id" + | |
143 | " AND cv_sort.custom_field_id=#{id} AND cv_sort.value <> '' AND cv_sort.value IS NOT NULL LIMIT 1)" |
|
143 | " AND cv_sort.custom_field_id=#{id} AND cv_sort.value <> '' AND cv_sort.value IS NOT NULL LIMIT 1)" | |
144 | else |
|
144 | else | |
145 | nil |
|
145 | nil | |
146 | end |
|
146 | end | |
147 | end |
|
147 | end | |
148 |
|
148 | |||
149 | def <=>(field) |
|
149 | def <=>(field) | |
150 | position <=> field.position |
|
150 | position <=> field.position | |
151 | end |
|
151 | end | |
152 |
|
152 | |||
153 | def self.customized_class |
|
153 | def self.customized_class | |
154 | self.name =~ /^(.+)CustomField$/ |
|
154 | self.name =~ /^(.+)CustomField$/ | |
155 | begin; $1.constantize; rescue nil; end |
|
155 | begin; $1.constantize; rescue nil; end | |
156 | end |
|
156 | end | |
157 |
|
157 | |||
158 | # to move in project_custom_field |
|
158 | # to move in project_custom_field | |
159 | def self.for_all |
|
159 | def self.for_all | |
160 | find(:all, :conditions => ["is_for_all=?", true], :order => 'position') |
|
160 | find(:all, :conditions => ["is_for_all=?", true], :order => 'position') | |
161 | end |
|
161 | end | |
162 |
|
162 | |||
163 | def type_name |
|
163 | def type_name | |
164 | nil |
|
164 | nil | |
165 | end |
|
165 | end | |
166 |
|
166 | |||
167 | # Returns the error messages for the given value |
|
167 | # Returns the error messages for the given value | |
168 | # or an empty array if value is a valid value for the custom field |
|
168 | # or an empty array if value is a valid value for the custom field | |
169 | def validate_field_value(value) |
|
169 | def validate_field_value(value) | |
170 | errs = [] |
|
170 | errs = [] | |
171 | if value.is_a?(Array) |
|
171 | if value.is_a?(Array) | |
172 | if !multiple? |
|
172 | if !multiple? | |
173 | errs << ::I18n.t('activerecord.errors.messages.invalid') |
|
173 | errs << ::I18n.t('activerecord.errors.messages.invalid') | |
174 | end |
|
174 | end | |
175 | if is_required? && value.detect(&:present?).nil? |
|
175 | if is_required? && value.detect(&:present?).nil? | |
176 | errs << ::I18n.t('activerecord.errors.messages.blank') |
|
176 | errs << ::I18n.t('activerecord.errors.messages.blank') | |
177 | end |
|
177 | end | |
178 | value.each {|v| errs += validate_field_value_format(v)} |
|
178 | value.each {|v| errs += validate_field_value_format(v)} | |
179 | else |
|
179 | else | |
180 | if is_required? && value.blank? |
|
180 | if is_required? && value.blank? | |
181 | errs << ::I18n.t('activerecord.errors.messages.blank') |
|
181 | errs << ::I18n.t('activerecord.errors.messages.blank') | |
182 | end |
|
182 | end | |
183 | errs += validate_field_value_format(value) |
|
183 | errs += validate_field_value_format(value) | |
184 | end |
|
184 | end | |
185 | errs |
|
185 | errs | |
186 | end |
|
186 | end | |
187 |
|
187 | |||
188 | # Returns true if value is a valid value for the custom field |
|
188 | # Returns true if value is a valid value for the custom field | |
189 | def valid_field_value?(value) |
|
189 | def valid_field_value?(value) | |
190 | validate_field_value(value).empty? |
|
190 | validate_field_value(value).empty? | |
191 | end |
|
191 | end | |
192 |
|
192 | |||
193 | protected |
|
193 | protected | |
194 |
|
194 | |||
195 | # Returns the error message for the given value regarding its format |
|
195 | # Returns the error message for the given value regarding its format | |
196 | def validate_field_value_format(value) |
|
196 | def validate_field_value_format(value) | |
197 | errs = [] |
|
197 | errs = [] | |
198 | if value.present? |
|
198 | if value.present? | |
199 | errs << ::I18n.t('activerecord.errors.messages.invalid') unless regexp.blank? or value =~ Regexp.new(regexp) |
|
199 | errs << ::I18n.t('activerecord.errors.messages.invalid') unless regexp.blank? or value =~ Regexp.new(regexp) | |
200 | errs << ::I18n.t('activerecord.errors.messages.too_short', :count => min_length) if min_length > 0 and value.length < min_length |
|
200 | errs << ::I18n.t('activerecord.errors.messages.too_short', :count => min_length) if min_length > 0 and value.length < min_length | |
201 | errs << ::I18n.t('activerecord.errors.messages.too_long', :count => max_length) if max_length > 0 and value.length > max_length |
|
201 | errs << ::I18n.t('activerecord.errors.messages.too_long', :count => max_length) if max_length > 0 and value.length > max_length | |
202 |
|
202 | |||
203 | # Format specific validations |
|
203 | # Format specific validations | |
204 | case field_format |
|
204 | case field_format | |
205 | when 'int' |
|
205 | when 'int' | |
206 | errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value =~ /^[+-]?\d+$/ |
|
206 | errs << ::I18n.t('activerecord.errors.messages.not_a_number') unless value =~ /^[+-]?\d+$/ | |
207 | when 'float' |
|
207 | when 'float' | |
208 | begin; Kernel.Float(value); rescue; errs << ::I18n.t('activerecord.errors.messages.invalid') end |
|
208 | begin; Kernel.Float(value); rescue; errs << ::I18n.t('activerecord.errors.messages.invalid') end | |
209 | when 'date' |
|
209 | when 'date' | |
210 | errs << ::I18n.t('activerecord.errors.messages.not_a_date') unless value =~ /^\d{4}-\d{2}-\d{2}$/ && begin; value.to_date; rescue; false end |
|
210 | errs << ::I18n.t('activerecord.errors.messages.not_a_date') unless value =~ /^\d{4}-\d{2}-\d{2}$/ && begin; value.to_date; rescue; false end | |
211 | when 'list' |
|
211 | when 'list' | |
212 | errs << ::I18n.t('activerecord.errors.messages.inclusion') unless possible_values.include?(value) |
|
212 | errs << ::I18n.t('activerecord.errors.messages.inclusion') unless possible_values.include?(value) | |
213 | end |
|
213 | end | |
214 | end |
|
214 | end | |
215 | errs |
|
215 | errs | |
216 | end |
|
216 | end | |
217 | end |
|
217 | end |
@@ -1,189 +1,194 | |||||
1 | # Redmine - project management software |
|
1 | # Redmine - project management software | |
2 | # Copyright (C) 2006-2012 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006-2012 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 | |
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_equal I18n.t('activerecord.errors.messages.invalid'), |
|
41 | assert_equal I18n.t('activerecord.errors.messages.invalid'), | |
42 | field.errors[:regexp].to_s |
|
42 | field.errors[:regexp].to_s | |
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 | |||
|
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 | ||||
55 | def test_possible_values_should_accept_an_array |
|
60 | def test_possible_values_should_accept_an_array | |
56 | field = CustomField.new |
|
61 | field = CustomField.new | |
57 | field.possible_values = ["One value", ""] |
|
62 | field.possible_values = ["One value", ""] | |
58 | assert_equal ["One value"], field.possible_values |
|
63 | assert_equal ["One value"], field.possible_values | |
59 | end |
|
64 | end | |
60 |
|
65 | |||
61 | def test_possible_values_should_accept_a_string |
|
66 | def test_possible_values_should_accept_a_string | |
62 | field = CustomField.new |
|
67 | field = CustomField.new | |
63 | field.possible_values = "One value" |
|
68 | field.possible_values = "One value" | |
64 | assert_equal ["One value"], field.possible_values |
|
69 | assert_equal ["One value"], field.possible_values | |
65 | end |
|
70 | end | |
66 |
|
71 | |||
67 | def test_possible_values_should_accept_a_multiline_string |
|
72 | def test_possible_values_should_accept_a_multiline_string | |
68 | field = CustomField.new |
|
73 | field = CustomField.new | |
69 | field.possible_values = "One value\nAnd another one \r\n \n" |
|
74 | field.possible_values = "One value\nAnd another one \r\n \n" | |
70 | assert_equal ["One value", "And another one"], field.possible_values |
|
75 | assert_equal ["One value", "And another one"], field.possible_values | |
71 | end |
|
76 | end | |
72 |
|
77 | |||
73 | def test_destroy |
|
78 | def test_destroy | |
74 | field = CustomField.find(1) |
|
79 | field = CustomField.find(1) | |
75 | assert field.destroy |
|
80 | assert field.destroy | |
76 | end |
|
81 | end | |
77 |
|
82 | |||
78 | def test_new_subclass_instance_should_return_an_instance |
|
83 | def test_new_subclass_instance_should_return_an_instance | |
79 | f = CustomField.new_subclass_instance('IssueCustomField') |
|
84 | f = CustomField.new_subclass_instance('IssueCustomField') | |
80 | assert_kind_of IssueCustomField, f |
|
85 | assert_kind_of IssueCustomField, f | |
81 | end |
|
86 | end | |
82 |
|
87 | |||
83 | def test_new_subclass_instance_should_set_attributes |
|
88 | def test_new_subclass_instance_should_set_attributes | |
84 | f = CustomField.new_subclass_instance('IssueCustomField', :name => 'Test') |
|
89 | f = CustomField.new_subclass_instance('IssueCustomField', :name => 'Test') | |
85 | assert_kind_of IssueCustomField, f |
|
90 | assert_kind_of IssueCustomField, f | |
86 | assert_equal 'Test', f.name |
|
91 | assert_equal 'Test', f.name | |
87 | end |
|
92 | end | |
88 |
|
93 | |||
89 | def test_new_subclass_instance_with_invalid_class_name_should_return_nil |
|
94 | def test_new_subclass_instance_with_invalid_class_name_should_return_nil | |
90 | assert_nil CustomField.new_subclass_instance('WrongClassName') |
|
95 | assert_nil CustomField.new_subclass_instance('WrongClassName') | |
91 | end |
|
96 | end | |
92 |
|
97 | |||
93 | def test_new_subclass_instance_with_non_subclass_name_should_return_nil |
|
98 | def test_new_subclass_instance_with_non_subclass_name_should_return_nil | |
94 | assert_nil CustomField.new_subclass_instance('Project') |
|
99 | assert_nil CustomField.new_subclass_instance('Project') | |
95 | end |
|
100 | end | |
96 |
|
101 | |||
97 | def test_string_field_validation_with_blank_value |
|
102 | def test_string_field_validation_with_blank_value | |
98 | f = CustomField.new(:field_format => 'string') |
|
103 | f = CustomField.new(:field_format => 'string') | |
99 |
|
104 | |||
100 | assert f.valid_field_value?(nil) |
|
105 | assert f.valid_field_value?(nil) | |
101 | assert f.valid_field_value?('') |
|
106 | assert f.valid_field_value?('') | |
102 |
|
107 | |||
103 | f.is_required = true |
|
108 | f.is_required = true | |
104 | assert !f.valid_field_value?(nil) |
|
109 | assert !f.valid_field_value?(nil) | |
105 | assert !f.valid_field_value?('') |
|
110 | assert !f.valid_field_value?('') | |
106 | end |
|
111 | end | |
107 |
|
112 | |||
108 | def test_string_field_validation_with_min_and_max_lengths |
|
113 | def test_string_field_validation_with_min_and_max_lengths | |
109 | f = CustomField.new(:field_format => 'string', :min_length => 2, :max_length => 5) |
|
114 | f = CustomField.new(:field_format => 'string', :min_length => 2, :max_length => 5) | |
110 |
|
115 | |||
111 | assert f.valid_field_value?(nil) |
|
116 | assert f.valid_field_value?(nil) | |
112 | assert f.valid_field_value?('') |
|
117 | assert f.valid_field_value?('') | |
113 | assert f.valid_field_value?('a' * 2) |
|
118 | assert f.valid_field_value?('a' * 2) | |
114 | assert !f.valid_field_value?('a') |
|
119 | assert !f.valid_field_value?('a') | |
115 | assert !f.valid_field_value?('a' * 6) |
|
120 | assert !f.valid_field_value?('a' * 6) | |
116 | end |
|
121 | end | |
117 |
|
122 | |||
118 | def test_string_field_validation_with_regexp |
|
123 | def test_string_field_validation_with_regexp | |
119 | f = CustomField.new(:field_format => 'string', :regexp => '^[A-Z0-9]*$') |
|
124 | f = CustomField.new(:field_format => 'string', :regexp => '^[A-Z0-9]*$') | |
120 |
|
125 | |||
121 | assert f.valid_field_value?(nil) |
|
126 | assert f.valid_field_value?(nil) | |
122 | assert f.valid_field_value?('') |
|
127 | assert f.valid_field_value?('') | |
123 | assert f.valid_field_value?('ABC') |
|
128 | assert f.valid_field_value?('ABC') | |
124 | assert !f.valid_field_value?('abc') |
|
129 | assert !f.valid_field_value?('abc') | |
125 | end |
|
130 | end | |
126 |
|
131 | |||
127 | def test_date_field_validation |
|
132 | def test_date_field_validation | |
128 | f = CustomField.new(:field_format => 'date') |
|
133 | f = CustomField.new(:field_format => 'date') | |
129 |
|
134 | |||
130 | assert f.valid_field_value?(nil) |
|
135 | assert f.valid_field_value?(nil) | |
131 | assert f.valid_field_value?('') |
|
136 | assert f.valid_field_value?('') | |
132 | assert f.valid_field_value?('1975-07-14') |
|
137 | assert f.valid_field_value?('1975-07-14') | |
133 | assert !f.valid_field_value?('1975-07-33') |
|
138 | assert !f.valid_field_value?('1975-07-33') | |
134 | assert !f.valid_field_value?('abc') |
|
139 | assert !f.valid_field_value?('abc') | |
135 | end |
|
140 | end | |
136 |
|
141 | |||
137 | def test_list_field_validation |
|
142 | def test_list_field_validation | |
138 | f = CustomField.new(:field_format => 'list', :possible_values => ['value1', 'value2']) |
|
143 | f = CustomField.new(:field_format => 'list', :possible_values => ['value1', 'value2']) | |
139 |
|
144 | |||
140 | assert f.valid_field_value?(nil) |
|
145 | assert f.valid_field_value?(nil) | |
141 | assert f.valid_field_value?('') |
|
146 | assert f.valid_field_value?('') | |
142 | assert f.valid_field_value?('value2') |
|
147 | assert f.valid_field_value?('value2') | |
143 | assert !f.valid_field_value?('abc') |
|
148 | assert !f.valid_field_value?('abc') | |
144 | end |
|
149 | end | |
145 |
|
150 | |||
146 | def test_int_field_validation |
|
151 | def test_int_field_validation | |
147 | f = CustomField.new(:field_format => 'int') |
|
152 | f = CustomField.new(:field_format => 'int') | |
148 |
|
153 | |||
149 | assert f.valid_field_value?(nil) |
|
154 | assert f.valid_field_value?(nil) | |
150 | assert f.valid_field_value?('') |
|
155 | assert f.valid_field_value?('') | |
151 | assert f.valid_field_value?('123') |
|
156 | assert f.valid_field_value?('123') | |
152 | assert f.valid_field_value?('+123') |
|
157 | assert f.valid_field_value?('+123') | |
153 | assert f.valid_field_value?('-123') |
|
158 | assert f.valid_field_value?('-123') | |
154 | assert !f.valid_field_value?('6abc') |
|
159 | assert !f.valid_field_value?('6abc') | |
155 | end |
|
160 | end | |
156 |
|
161 | |||
157 | def test_float_field_validation |
|
162 | def test_float_field_validation | |
158 | f = CustomField.new(:field_format => 'float') |
|
163 | f = CustomField.new(:field_format => 'float') | |
159 |
|
164 | |||
160 | assert f.valid_field_value?(nil) |
|
165 | assert f.valid_field_value?(nil) | |
161 | assert f.valid_field_value?('') |
|
166 | assert f.valid_field_value?('') | |
162 | assert f.valid_field_value?('11.2') |
|
167 | assert f.valid_field_value?('11.2') | |
163 | assert f.valid_field_value?('-6.250') |
|
168 | assert f.valid_field_value?('-6.250') | |
164 | assert f.valid_field_value?('5') |
|
169 | assert f.valid_field_value?('5') | |
165 | assert !f.valid_field_value?('6abc') |
|
170 | assert !f.valid_field_value?('6abc') | |
166 | end |
|
171 | end | |
167 |
|
172 | |||
168 | def test_multi_field_validation |
|
173 | def test_multi_field_validation | |
169 | f = CustomField.new(:field_format => 'list', :multiple => 'true', :possible_values => ['value1', 'value2']) |
|
174 | f = CustomField.new(:field_format => 'list', :multiple => 'true', :possible_values => ['value1', 'value2']) | |
170 |
|
175 | |||
171 | assert f.valid_field_value?(nil) |
|
176 | assert f.valid_field_value?(nil) | |
172 | assert f.valid_field_value?('') |
|
177 | assert f.valid_field_value?('') | |
173 | assert f.valid_field_value?([]) |
|
178 | assert f.valid_field_value?([]) | |
174 | assert f.valid_field_value?([nil]) |
|
179 | assert f.valid_field_value?([nil]) | |
175 | assert f.valid_field_value?(['']) |
|
180 | assert f.valid_field_value?(['']) | |
176 |
|
181 | |||
177 | assert f.valid_field_value?('value2') |
|
182 | assert f.valid_field_value?('value2') | |
178 | assert !f.valid_field_value?('abc') |
|
183 | assert !f.valid_field_value?('abc') | |
179 |
|
184 | |||
180 | assert f.valid_field_value?(['value2']) |
|
185 | assert f.valid_field_value?(['value2']) | |
181 | assert !f.valid_field_value?(['abc']) |
|
186 | assert !f.valid_field_value?(['abc']) | |
182 |
|
187 | |||
183 | assert f.valid_field_value?(['', 'value2']) |
|
188 | assert f.valid_field_value?(['', 'value2']) | |
184 | assert !f.valid_field_value?(['', 'abc']) |
|
189 | assert !f.valid_field_value?(['', 'abc']) | |
185 |
|
190 | |||
186 | assert f.valid_field_value?(['value1', 'value2']) |
|
191 | assert f.valid_field_value?(['value1', 'value2']) | |
187 | assert !f.valid_field_value?(['value1', 'abc']) |
|
192 | assert !f.valid_field_value?(['value1', 'abc']) | |
188 | end |
|
193 | end | |
189 | end |
|
194 | end |
General Comments 0
You need to be logged in to leave comments.
Login now