##// END OF EJS Templates
Fixed that validating a Setting with invalid name triggers an error (#15551)....
Jean-Philippe Lang -
r12097:23974fb0cede
parent child
Show More
@@ -1,250 +1,252
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 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 Setting < ActiveRecord::Base
18 class Setting < ActiveRecord::Base
19
19
20 DATE_FORMATS = [
20 DATE_FORMATS = [
21 '%Y-%m-%d',
21 '%Y-%m-%d',
22 '%d/%m/%Y',
22 '%d/%m/%Y',
23 '%d.%m.%Y',
23 '%d.%m.%Y',
24 '%d-%m-%Y',
24 '%d-%m-%Y',
25 '%m/%d/%Y',
25 '%m/%d/%Y',
26 '%d %b %Y',
26 '%d %b %Y',
27 '%d %B %Y',
27 '%d %B %Y',
28 '%b %d, %Y',
28 '%b %d, %Y',
29 '%B %d, %Y'
29 '%B %d, %Y'
30 ]
30 ]
31
31
32 TIME_FORMATS = [
32 TIME_FORMATS = [
33 '%H:%M',
33 '%H:%M',
34 '%I:%M %p'
34 '%I:%M %p'
35 ]
35 ]
36
36
37 ENCODINGS = %w(US-ASCII
37 ENCODINGS = %w(US-ASCII
38 windows-1250
38 windows-1250
39 windows-1251
39 windows-1251
40 windows-1252
40 windows-1252
41 windows-1253
41 windows-1253
42 windows-1254
42 windows-1254
43 windows-1255
43 windows-1255
44 windows-1256
44 windows-1256
45 windows-1257
45 windows-1257
46 windows-1258
46 windows-1258
47 windows-31j
47 windows-31j
48 ISO-2022-JP
48 ISO-2022-JP
49 ISO-2022-KR
49 ISO-2022-KR
50 ISO-8859-1
50 ISO-8859-1
51 ISO-8859-2
51 ISO-8859-2
52 ISO-8859-3
52 ISO-8859-3
53 ISO-8859-4
53 ISO-8859-4
54 ISO-8859-5
54 ISO-8859-5
55 ISO-8859-6
55 ISO-8859-6
56 ISO-8859-7
56 ISO-8859-7
57 ISO-8859-8
57 ISO-8859-8
58 ISO-8859-9
58 ISO-8859-9
59 ISO-8859-13
59 ISO-8859-13
60 ISO-8859-15
60 ISO-8859-15
61 KOI8-R
61 KOI8-R
62 UTF-8
62 UTF-8
63 UTF-16
63 UTF-16
64 UTF-16BE
64 UTF-16BE
65 UTF-16LE
65 UTF-16LE
66 EUC-JP
66 EUC-JP
67 Shift_JIS
67 Shift_JIS
68 CP932
68 CP932
69 GB18030
69 GB18030
70 GBK
70 GBK
71 ISCII91
71 ISCII91
72 EUC-KR
72 EUC-KR
73 Big5
73 Big5
74 Big5-HKSCS
74 Big5-HKSCS
75 TIS-620)
75 TIS-620)
76
76
77 cattr_accessor :available_settings
77 cattr_accessor :available_settings
78 @@available_settings = YAML::load(File.open("#{Rails.root}/config/settings.yml"))
78 @@available_settings = YAML::load(File.open("#{Rails.root}/config/settings.yml"))
79 Redmine::Plugin.all.each do |plugin|
79 Redmine::Plugin.all.each do |plugin|
80 next unless plugin.settings
80 next unless plugin.settings
81 @@available_settings["plugin_#{plugin.id}"] = {'default' => plugin.settings[:default], 'serialized' => true}
81 @@available_settings["plugin_#{plugin.id}"] = {'default' => plugin.settings[:default], 'serialized' => true}
82 end
82 end
83
83
84 validates_uniqueness_of :name
84 validates_uniqueness_of :name
85 validates_inclusion_of :name, :in => @@available_settings.keys
85 validates_inclusion_of :name, :in => @@available_settings.keys
86 validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting| @@available_settings[setting.name]['format'] == 'int' }
86 validates_numericality_of :value, :only_integer => true, :if => Proc.new { |setting|
87 (s = @@available_settings[setting.name]) && s['format'] == 'int'
88 }
87
89
88 # Hash used to cache setting values
90 # Hash used to cache setting values
89 @cached_settings = {}
91 @cached_settings = {}
90 @cached_cleared_on = Time.now
92 @cached_cleared_on = Time.now
91
93
92 def value
94 def value
93 v = read_attribute(:value)
95 v = read_attribute(:value)
94 # Unserialize serialized settings
96 # Unserialize serialized settings
95 v = YAML::load(v) if @@available_settings[name]['serialized'] && v.is_a?(String)
97 v = YAML::load(v) if @@available_settings[name]['serialized'] && v.is_a?(String)
96 v = v.to_sym if @@available_settings[name]['format'] == 'symbol' && !v.blank?
98 v = v.to_sym if @@available_settings[name]['format'] == 'symbol' && !v.blank?
97 v
99 v
98 end
100 end
99
101
100 def value=(v)
102 def value=(v)
101 v = v.to_yaml if v && @@available_settings[name] && @@available_settings[name]['serialized']
103 v = v.to_yaml if v && @@available_settings[name] && @@available_settings[name]['serialized']
102 write_attribute(:value, v.to_s)
104 write_attribute(:value, v.to_s)
103 end
105 end
104
106
105 # Returns the value of the setting named name
107 # Returns the value of the setting named name
106 def self.[](name)
108 def self.[](name)
107 v = @cached_settings[name]
109 v = @cached_settings[name]
108 v ? v : (@cached_settings[name] = find_or_default(name).value)
110 v ? v : (@cached_settings[name] = find_or_default(name).value)
109 end
111 end
110
112
111 def self.[]=(name, v)
113 def self.[]=(name, v)
112 setting = find_or_default(name)
114 setting = find_or_default(name)
113 setting.value = (v ? v : "")
115 setting.value = (v ? v : "")
114 @cached_settings[name] = nil
116 @cached_settings[name] = nil
115 setting.save
117 setting.save
116 setting.value
118 setting.value
117 end
119 end
118
120
119 # Defines getter and setter for each setting
121 # Defines getter and setter for each setting
120 # Then setting values can be read using: Setting.some_setting_name
122 # Then setting values can be read using: Setting.some_setting_name
121 # or set using Setting.some_setting_name = "some value"
123 # or set using Setting.some_setting_name = "some value"
122 @@available_settings.each do |name, params|
124 @@available_settings.each do |name, params|
123 src = <<-END_SRC
125 src = <<-END_SRC
124 def self.#{name}
126 def self.#{name}
125 self[:#{name}]
127 self[:#{name}]
126 end
128 end
127
129
128 def self.#{name}?
130 def self.#{name}?
129 self[:#{name}].to_i > 0
131 self[:#{name}].to_i > 0
130 end
132 end
131
133
132 def self.#{name}=(value)
134 def self.#{name}=(value)
133 self[:#{name}] = value
135 self[:#{name}] = value
134 end
136 end
135 END_SRC
137 END_SRC
136 class_eval src, __FILE__, __LINE__
138 class_eval src, __FILE__, __LINE__
137 end
139 end
138
140
139 # Sets a setting value from params
141 # Sets a setting value from params
140 def self.set_from_params(name, params)
142 def self.set_from_params(name, params)
141 params = params.dup
143 params = params.dup
142 params.delete_if {|v| v.blank? } if params.is_a?(Array)
144 params.delete_if {|v| v.blank? } if params.is_a?(Array)
143
145
144 m = "#{name}_from_params"
146 m = "#{name}_from_params"
145 if respond_to? m
147 if respond_to? m
146 self[name.to_sym] = send m, params
148 self[name.to_sym] = send m, params
147 else
149 else
148 self[name.to_sym] = params
150 self[name.to_sym] = params
149 end
151 end
150 end
152 end
151
153
152 # Returns a hash suitable for commit_update_keywords setting
154 # Returns a hash suitable for commit_update_keywords setting
153 #
155 #
154 # Example:
156 # Example:
155 # params = {:keywords => ['fixes', 'closes'], :status_id => ["3", "5"], :done_ratio => ["", "100"]}
157 # params = {:keywords => ['fixes', 'closes'], :status_id => ["3", "5"], :done_ratio => ["", "100"]}
156 # Setting.commit_update_keywords_from_params(params)
158 # Setting.commit_update_keywords_from_params(params)
157 # # => [{'keywords => 'fixes', 'status_id' => "3"}, {'keywords => 'closes', 'status_id' => "5", 'done_ratio' => "100"}]
159 # # => [{'keywords => 'fixes', 'status_id' => "3"}, {'keywords => 'closes', 'status_id' => "5", 'done_ratio' => "100"}]
158 def self.commit_update_keywords_from_params(params)
160 def self.commit_update_keywords_from_params(params)
159 s = []
161 s = []
160 if params.is_a?(Hash) && params.key?(:keywords) && params.values.all? {|v| v.is_a? Array}
162 if params.is_a?(Hash) && params.key?(:keywords) && params.values.all? {|v| v.is_a? Array}
161 attributes = params.except(:keywords).keys
163 attributes = params.except(:keywords).keys
162 params[:keywords].each_with_index do |keywords, i|
164 params[:keywords].each_with_index do |keywords, i|
163 next if keywords.blank?
165 next if keywords.blank?
164 s << attributes.inject({}) {|h, a|
166 s << attributes.inject({}) {|h, a|
165 value = params[a][i].to_s
167 value = params[a][i].to_s
166 h[a.to_s] = value if value.present?
168 h[a.to_s] = value if value.present?
167 h
169 h
168 }.merge('keywords' => keywords)
170 }.merge('keywords' => keywords)
169 end
171 end
170 end
172 end
171 s
173 s
172 end
174 end
173
175
174 # Helper that returns an array based on per_page_options setting
176 # Helper that returns an array based on per_page_options setting
175 def self.per_page_options_array
177 def self.per_page_options_array
176 per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort
178 per_page_options.split(%r{[\s,]}).collect(&:to_i).select {|n| n > 0}.sort
177 end
179 end
178
180
179 # Helper that returns a Hash with single update keywords as keys
181 # Helper that returns a Hash with single update keywords as keys
180 def self.commit_update_keywords_array
182 def self.commit_update_keywords_array
181 a = []
183 a = []
182 if commit_update_keywords.is_a?(Array)
184 if commit_update_keywords.is_a?(Array)
183 commit_update_keywords.each do |rule|
185 commit_update_keywords.each do |rule|
184 next unless rule.is_a?(Hash)
186 next unless rule.is_a?(Hash)
185 rule = rule.dup
187 rule = rule.dup
186 rule.delete_if {|k, v| v.blank?}
188 rule.delete_if {|k, v| v.blank?}
187 keywords = rule['keywords'].to_s.downcase.split(",").map(&:strip).reject(&:blank?)
189 keywords = rule['keywords'].to_s.downcase.split(",").map(&:strip).reject(&:blank?)
188 next if keywords.empty?
190 next if keywords.empty?
189 a << rule.merge('keywords' => keywords)
191 a << rule.merge('keywords' => keywords)
190 end
192 end
191 end
193 end
192 a
194 a
193 end
195 end
194
196
195 def self.commit_fix_keywords
197 def self.commit_fix_keywords
196 ActiveSupport::Deprecation.warn "Setting.commit_fix_keywords is deprecated and will be removed in Redmine 3"
198 ActiveSupport::Deprecation.warn "Setting.commit_fix_keywords is deprecated and will be removed in Redmine 3"
197 if commit_update_keywords.is_a?(Array)
199 if commit_update_keywords.is_a?(Array)
198 commit_update_keywords.first && commit_update_keywords.first['keywords']
200 commit_update_keywords.first && commit_update_keywords.first['keywords']
199 end
201 end
200 end
202 end
201
203
202 def self.commit_fix_status_id
204 def self.commit_fix_status_id
203 ActiveSupport::Deprecation.warn "Setting.commit_fix_status_id is deprecated and will be removed in Redmine 3"
205 ActiveSupport::Deprecation.warn "Setting.commit_fix_status_id is deprecated and will be removed in Redmine 3"
204 if commit_update_keywords.is_a?(Array)
206 if commit_update_keywords.is_a?(Array)
205 commit_update_keywords.first && commit_update_keywords.first['status_id']
207 commit_update_keywords.first && commit_update_keywords.first['status_id']
206 end
208 end
207 end
209 end
208
210
209 def self.commit_fix_done_ratio
211 def self.commit_fix_done_ratio
210 ActiveSupport::Deprecation.warn "Setting.commit_fix_done_ratio is deprecated and will be removed in Redmine 3"
212 ActiveSupport::Deprecation.warn "Setting.commit_fix_done_ratio is deprecated and will be removed in Redmine 3"
211 if commit_update_keywords.is_a?(Array)
213 if commit_update_keywords.is_a?(Array)
212 commit_update_keywords.first && commit_update_keywords.first['done_ratio']
214 commit_update_keywords.first && commit_update_keywords.first['done_ratio']
213 end
215 end
214 end
216 end
215
217
216 def self.openid?
218 def self.openid?
217 Object.const_defined?(:OpenID) && self[:openid].to_i > 0
219 Object.const_defined?(:OpenID) && self[:openid].to_i > 0
218 end
220 end
219
221
220 # Checks if settings have changed since the values were read
222 # Checks if settings have changed since the values were read
221 # and clears the cache hash if it's the case
223 # and clears the cache hash if it's the case
222 # Called once per request
224 # Called once per request
223 def self.check_cache
225 def self.check_cache
224 settings_updated_on = Setting.maximum(:updated_on)
226 settings_updated_on = Setting.maximum(:updated_on)
225 if settings_updated_on && @cached_cleared_on <= settings_updated_on
227 if settings_updated_on && @cached_cleared_on <= settings_updated_on
226 clear_cache
228 clear_cache
227 end
229 end
228 end
230 end
229
231
230 # Clears the settings cache
232 # Clears the settings cache
231 def self.clear_cache
233 def self.clear_cache
232 @cached_settings.clear
234 @cached_settings.clear
233 @cached_cleared_on = Time.now
235 @cached_cleared_on = Time.now
234 logger.info "Settings cache cleared." if logger
236 logger.info "Settings cache cleared." if logger
235 end
237 end
236
238
237 private
239 private
238 # Returns the Setting instance for the setting named name
240 # Returns the Setting instance for the setting named name
239 # (record found in database or new record with default value)
241 # (record found in database or new record with default value)
240 def self.find_or_default(name)
242 def self.find_or_default(name)
241 name = name.to_s
243 name = name.to_s
242 raise "There's no setting named #{name}" unless @@available_settings.has_key?(name)
244 raise "There's no setting named #{name}" unless @@available_settings.has_key?(name)
243 setting = find_by_name(name)
245 setting = find_by_name(name)
244 unless setting
246 unless setting
245 setting = new(:name => name)
247 setting = new(:name => name)
246 setting.value = @@available_settings[name]['default']
248 setting.value = @@available_settings[name]['default']
247 end
249 end
248 setting
250 setting
249 end
251 end
250 end
252 end
@@ -1,90 +1,104
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 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 SettingTest < ActiveSupport::TestCase
20 class SettingTest < ActiveSupport::TestCase
21
21
22 def teardown
22 def teardown
23 Setting.clear_cache
23 Setting.clear_cache
24 end
24 end
25
25
26 def test_read_default
26 def test_read_default
27 assert_equal "Redmine", Setting.app_title
27 assert_equal "Redmine", Setting.app_title
28 assert Setting.self_registration?
28 assert Setting.self_registration?
29 assert !Setting.login_required?
29 assert !Setting.login_required?
30 end
30 end
31
31
32 def test_update
32 def test_update
33 Setting.app_title = "My title"
33 Setting.app_title = "My title"
34 assert_equal "My title", Setting.app_title
34 assert_equal "My title", Setting.app_title
35 # make sure db has been updated (INSERT)
35 # make sure db has been updated (INSERT)
36 assert_equal "My title", Setting.find_by_name('app_title').value
36 assert_equal "My title", Setting.find_by_name('app_title').value
37
37
38 Setting.app_title = "My other title"
38 Setting.app_title = "My other title"
39 assert_equal "My other title", Setting.app_title
39 assert_equal "My other title", Setting.app_title
40 # make sure db has been updated (UPDATE)
40 # make sure db has been updated (UPDATE)
41 assert_equal "My other title", Setting.find_by_name('app_title').value
41 assert_equal "My other title", Setting.find_by_name('app_title').value
42 end
42 end
43
43
44 def test_setting_with_int_format_should_accept_numeric_only
45 with_settings :session_timeout => 30 do
46 Setting.session_timeout = 'foo'
47 assert_equal "30", Setting.session_timeout
48 Setting.session_timeout = 40
49 assert_equal "40", Setting.session_timeout
50 end
51 end
52
53 def test_setting_with_invalid_name_should_be_valid
54 setting = Setting.new(:name => "does_not_exist", :value => "should_not_be_allowed")
55 assert !setting.save
56 end
57
44 def test_serialized_setting
58 def test_serialized_setting
45 Setting.notified_events = ['issue_added', 'issue_updated', 'news_added']
59 Setting.notified_events = ['issue_added', 'issue_updated', 'news_added']
46 assert_equal ['issue_added', 'issue_updated', 'news_added'], Setting.notified_events
60 assert_equal ['issue_added', 'issue_updated', 'news_added'], Setting.notified_events
47 assert_equal ['issue_added', 'issue_updated', 'news_added'], Setting.find_by_name('notified_events').value
61 assert_equal ['issue_added', 'issue_updated', 'news_added'], Setting.find_by_name('notified_events').value
48 end
62 end
49
63
50 def test_setting_should_be_reloaded_after_clear_cache
64 def test_setting_should_be_reloaded_after_clear_cache
51 Setting.app_title = "My title"
65 Setting.app_title = "My title"
52 assert_equal "My title", Setting.app_title
66 assert_equal "My title", Setting.app_title
53
67
54 s = Setting.find_by_name("app_title")
68 s = Setting.find_by_name("app_title")
55 s.value = 'New title'
69 s.value = 'New title'
56 s.save!
70 s.save!
57 assert_equal "My title", Setting.app_title
71 assert_equal "My title", Setting.app_title
58
72
59 Setting.clear_cache
73 Setting.clear_cache
60 assert_equal "New title", Setting.app_title
74 assert_equal "New title", Setting.app_title
61 end
75 end
62
76
63 def test_per_page_options_array_should_be_an_empty_array_when_setting_is_blank
77 def test_per_page_options_array_should_be_an_empty_array_when_setting_is_blank
64 with_settings :per_page_options => nil do
78 with_settings :per_page_options => nil do
65 assert_equal [], Setting.per_page_options_array
79 assert_equal [], Setting.per_page_options_array
66 end
80 end
67
81
68 with_settings :per_page_options => '' do
82 with_settings :per_page_options => '' do
69 assert_equal [], Setting.per_page_options_array
83 assert_equal [], Setting.per_page_options_array
70 end
84 end
71 end
85 end
72
86
73 def test_per_page_options_array_should_be_an_array_of_integers
87 def test_per_page_options_array_should_be_an_array_of_integers
74 with_settings :per_page_options => '10, 25, 50' do
88 with_settings :per_page_options => '10, 25, 50' do
75 assert_equal [10, 25, 50], Setting.per_page_options_array
89 assert_equal [10, 25, 50], Setting.per_page_options_array
76 end
90 end
77 end
91 end
78
92
79 def test_per_page_options_array_should_omit_non_numerial_values
93 def test_per_page_options_array_should_omit_non_numerial_values
80 with_settings :per_page_options => 'a, 25, 50' do
94 with_settings :per_page_options => 'a, 25, 50' do
81 assert_equal [25, 50], Setting.per_page_options_array
95 assert_equal [25, 50], Setting.per_page_options_array
82 end
96 end
83 end
97 end
84
98
85 def test_per_page_options_array_should_be_sorted
99 def test_per_page_options_array_should_be_sorted
86 with_settings :per_page_options => '25, 10, 50' do
100 with_settings :per_page_options => '25, 10, 50' do
87 assert_equal [10, 25, 50], Setting.per_page_options_array
101 assert_equal [10, 25, 50], Setting.per_page_options_array
88 end
102 end
89 end
103 end
90 end
104 end
General Comments 0
You need to be logged in to leave comments. Login now