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