66982346e9f09bdc10a5634274b0a3a117ab7bc0
[feedcatcher.git] / vendor / rails / activerecord / test / cases / validations_i18n_test.rb
1 require "cases/helper"
2 require 'models/topic'
3 require 'models/reply'
4
5 class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase
6 def setup
7 reset_callbacks Topic
8 @topic = Topic.new
9 @old_load_path, @old_backend = I18n.load_path, I18n.backend
10 I18n.load_path.clear
11 I18n.backend = I18n::Backend::Simple.new
12 I18n.backend.store_translations('en', :activerecord => {:errors => {:messages => {:custom => nil}}})
13 end
14
15 def teardown
16 reset_callbacks Topic
17 I18n.load_path.replace @old_load_path
18 I18n.backend = @old_backend
19 end
20
21 def unique_topic
22 @unique ||= Topic.create :title => 'unique!'
23 end
24
25 def replied_topic
26 @replied_topic ||= begin
27 topic = Topic.create(:title => "topic")
28 topic.replies << Reply.new
29 topic
30 end
31 end
32
33 def reset_callbacks(*models)
34 models.each do |model|
35 model.instance_variable_set("@validate_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
36 model.instance_variable_set("@validate_on_create_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
37 model.instance_variable_set("@validate_on_update_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
38 end
39 end
40
41 def test_default_error_messages_is_deprecated
42 assert_deprecated('ActiveRecord::Errors.default_error_messages') do
43 ActiveRecord::Errors.default_error_messages
44 end
45 end
46
47 def test_percent_s_interpolation_syntax_in_error_messages_still_works
48 ActiveSupport::Deprecation.silence do
49 result = I18n.t :does_not_exist, :default => "%s interpolation syntax is deprecated", :value => 'this'
50 assert_equal result, "this interpolation syntax is deprecated"
51 end
52 end
53
54 def test_percent_s_interpolation_syntax_in_error_messages_is_deprecated
55 assert_deprecated('using %s in messages') do
56 I18n.t :does_not_exist, :default => "%s interpolation syntax is deprected", :value => 'this'
57 end
58 end
59
60 def test_percent_d_interpolation_syntax_in_error_messages_still_works
61 ActiveSupport::Deprecation.silence do
62 result = I18n.t :does_not_exist, :default => "%d interpolation syntaxes are deprecated", :count => 2
63 assert_equal result, "2 interpolation syntaxes are deprecated"
64 end
65 end
66
67 def test_percent_d_interpolation_syntax_in_error_messages_is_deprecated
68 assert_deprecated('using %d in messages') do
69 I18n.t :does_not_exist, :default => "%d interpolation syntaxes are deprected", :count => 2
70 end
71 end
72
73 # ActiveRecord::Errors
74 def test_errors_generate_message_translates_custom_model_attribute_key
75
76 I18n.expects(:translate).with(
77 :topic,
78 { :count => 1,
79 :default => ['Topic'],
80 :scope => [:activerecord, :models]
81 }
82 ).returns('Topic')
83
84 I18n.expects(:translate).with(
85 :"topic.title",
86 { :count => 1,
87 :default => ['Title'],
88 :scope => [:activerecord, :attributes]
89 }
90 ).returns('Title')
91
92 I18n.expects(:translate).with(
93 :"models.topic.attributes.title.invalid",
94 :value => nil,
95 :scope => [:activerecord, :errors],
96 :default => [
97 :"models.topic.invalid",
98 'default from class def error 1',
99 :"messages.invalid"],
100 :attribute => "Title",
101 :model => "Topic"
102 ).returns('default from class def error 1')
103
104 @topic.errors.generate_message :title, :invalid, :default => 'default from class def error 1'
105 end
106
107 def test_errors_generate_message_translates_custom_model_attribute_keys_with_sti
108
109 I18n.expects(:translate).with(
110 :reply,
111 { :count => 1,
112 :default => [:topic, 'Reply'],
113 :scope => [:activerecord, :models]
114 }
115 ).returns('Reply')
116
117 I18n.expects(:translate).with(
118 :"reply.title",
119 { :count => 1,
120 :default => [:'topic.title', 'Title'],
121 :scope => [:activerecord, :attributes]
122 }
123 ).returns('Title')
124
125 I18n.expects(:translate).with(
126 :"models.reply.attributes.title.invalid",
127 :value => nil,
128 :scope => [:activerecord, :errors],
129 :default => [
130 :"models.reply.invalid",
131 :"models.topic.attributes.title.invalid",
132 :"models.topic.invalid",
133 'default from class def',
134 :"messages.invalid"],
135 :model => 'Reply',
136 :attribute => 'Title'
137 ).returns("default from class def")
138
139 Reply.new.errors.generate_message :title, :invalid, :default => 'default from class def'
140
141 end
142
143 def test_errors_add_on_empty_generates_message
144 @topic.errors.expects(:generate_message).with(:title, :empty, {:default => nil})
145 @topic.errors.add_on_empty :title
146 end
147
148 def test_errors_add_on_empty_generates_message_with_custom_default_message
149 @topic.errors.expects(:generate_message).with(:title, :empty, {:default => 'custom'})
150 @topic.errors.add_on_empty :title, 'custom'
151 end
152
153 def test_errors_add_on_blank_generates_message
154 @topic.errors.expects(:generate_message).with(:title, :blank, {:default => nil})
155 @topic.errors.add_on_blank :title
156 end
157
158 def test_errors_add_on_blank_generates_message_with_custom_default_message
159 @topic.errors.expects(:generate_message).with(:title, :blank, {:default => 'custom'})
160 @topic.errors.add_on_blank :title, 'custom'
161 end
162
163 def test_errors_full_messages_translates_human_attribute_name_for_model_attributes
164 @topic.errors.instance_variable_set :@errors, { 'title' => ['empty'] }
165 I18n.expects(:translate).with(:"topic.title", :default => ['Title'], :scope => [:activerecord, :attributes], :count => 1).returns('Title')
166 @topic.errors.full_messages :locale => 'en'
167 end
168
169 # ActiveRecord::Validations
170 # validates_confirmation_of w/ mocha
171 def test_validates_confirmation_of_generates_message
172 Topic.validates_confirmation_of :title
173 @topic.title_confirmation = 'foo'
174 @topic.errors.expects(:generate_message).with(:title, :confirmation, {:default => nil})
175 @topic.valid?
176 end
177
178 def test_validates_confirmation_of_generates_message_with_custom_default_message
179 Topic.validates_confirmation_of :title, :message => 'custom'
180 @topic.title_confirmation = 'foo'
181 @topic.errors.expects(:generate_message).with(:title, :confirmation, {:default => 'custom'})
182 @topic.valid?
183 end
184
185 # validates_acceptance_of w/ mocha
186
187 def test_validates_acceptance_of_generates_message
188 Topic.validates_acceptance_of :title, :allow_nil => false
189 @topic.errors.expects(:generate_message).with(:title, :accepted, {:default => nil})
190 @topic.valid?
191 end
192
193 def test_validates_acceptance_of_generates_message_with_custom_default_message
194 Topic.validates_acceptance_of :title, :message => 'custom', :allow_nil => false
195 @topic.errors.expects(:generate_message).with(:title, :accepted, {:default => 'custom'})
196 @topic.valid?
197 end
198
199 # validates_presence_of w/ mocha
200
201 def test_validates_presence_of_generates_message
202 Topic.validates_presence_of :title
203 @topic.errors.expects(:generate_message).with(:title, :blank, {:default => nil})
204 @topic.valid?
205 end
206
207 def test_validates_presence_of_generates_message_with_custom_default_message
208 Topic.validates_presence_of :title, :message => 'custom'
209 @topic.errors.expects(:generate_message).with(:title, :blank, {:default => 'custom'})
210 @topic.valid?
211 end
212
213 def test_validates_length_of_within_generates_message_with_title_too_short
214 Topic.validates_length_of :title, :within => 3..5
215 @topic.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => nil})
216 @topic.valid?
217 end
218
219 def test_validates_length_of_within_generates_message_with_title_too_short_and_custom_default_message
220 Topic.validates_length_of :title, :within => 3..5, :too_short => 'custom'
221 @topic.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => 'custom'})
222 @topic.valid?
223 end
224
225 def test_validates_length_of_within_generates_message_with_title_too_long
226 Topic.validates_length_of :title, :within => 3..5
227 @topic.title = 'this title is too long'
228 @topic.errors.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => nil})
229 @topic.valid?
230 end
231
232 def test_validates_length_of_within_generates_message_with_title_too_long_and_custom_default_message
233 Topic.validates_length_of :title, :within => 3..5, :too_long => 'custom'
234 @topic.title = 'this title is too long'
235 @topic.errors.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => 'custom'})
236 @topic.valid?
237 end
238
239 # validates_length_of :within w/ mocha
240
241 def test_validates_length_of_within_generates_message_with_title_too_short
242 Topic.validates_length_of :title, :within => 3..5
243 @topic.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => nil})
244 @topic.valid?
245 end
246
247 def test_validates_length_of_within_generates_message_with_title_too_short_and_custom_default_message
248 Topic.validates_length_of :title, :within => 3..5, :too_short => 'custom'
249 @topic.errors.expects(:generate_message).with(:title, :too_short, {:count => 3, :default => 'custom'})
250 @topic.valid?
251 end
252
253 def test_validates_length_of_within_generates_message_with_title_too_long
254 Topic.validates_length_of :title, :within => 3..5
255 @topic.title = 'this title is too long'
256 @topic.errors.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => nil})
257 @topic.valid?
258 end
259
260 def test_validates_length_of_within_generates_message_with_title_too_long_and_custom_default_message
261 Topic.validates_length_of :title, :within => 3..5, :too_long => 'custom'
262 @topic.title = 'this title is too long'
263 @topic.errors.expects(:generate_message).with(:title, :too_long, {:count => 5, :default => 'custom'})
264 @topic.valid?
265 end
266
267 # validates_length_of :is w/ mocha
268
269 def test_validates_length_of_is_generates_message
270 Topic.validates_length_of :title, :is => 5
271 @topic.errors.expects(:generate_message).with(:title, :wrong_length, {:count => 5, :default => nil})
272 @topic.valid?
273 end
274
275 def test_validates_length_of_is_generates_message_with_custom_default_message
276 Topic.validates_length_of :title, :is => 5, :message => 'custom'
277 @topic.errors.expects(:generate_message).with(:title, :wrong_length, {:count => 5, :default => 'custom'})
278 @topic.valid?
279 end
280
281 # validates_uniqueness_of w/ mocha
282
283 def test_validates_uniqueness_of_generates_message
284 Topic.validates_uniqueness_of :title
285 @topic.title = unique_topic.title
286 @topic.errors.expects(:generate_message).with(:title, :taken, {:default => nil, :value => 'unique!'})
287 @topic.valid?
288 end
289
290 def test_validates_uniqueness_of_generates_message_with_custom_default_message
291 Topic.validates_uniqueness_of :title, :message => 'custom'
292 @topic.title = unique_topic.title
293 @topic.errors.expects(:generate_message).with(:title, :taken, {:default => 'custom', :value => 'unique!'})
294 @topic.valid?
295 end
296
297 # validates_format_of w/ mocha
298
299 def test_validates_format_of_generates_message
300 Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/
301 @topic.title = '72x'
302 @topic.errors.expects(:generate_message).with(:title, :invalid, {:value => '72x', :default => nil})
303 @topic.valid?
304 end
305
306 def test_validates_format_of_generates_message_with_custom_default_message
307 Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/, :message => 'custom'
308 @topic.title = '72x'
309 @topic.errors.expects(:generate_message).with(:title, :invalid, {:value => '72x', :default => 'custom'})
310 @topic.valid?
311 end
312
313 # validates_inclusion_of w/ mocha
314
315 def test_validates_inclusion_of_generates_message
316 Topic.validates_inclusion_of :title, :in => %w(a b c)
317 @topic.title = 'z'
318 @topic.errors.expects(:generate_message).with(:title, :inclusion, {:value => 'z', :default => nil})
319 @topic.valid?
320 end
321
322 def test_validates_inclusion_of_generates_message_with_custom_default_message
323 Topic.validates_inclusion_of :title, :in => %w(a b c), :message => 'custom'
324 @topic.title = 'z'
325 @topic.errors.expects(:generate_message).with(:title, :inclusion, {:value => 'z', :default => 'custom'})
326 @topic.valid?
327 end
328
329 # validates_exclusion_of w/ mocha
330
331 def test_validates_exclusion_of_generates_message
332 Topic.validates_exclusion_of :title, :in => %w(a b c)
333 @topic.title = 'a'
334 @topic.errors.expects(:generate_message).with(:title, :exclusion, {:value => 'a', :default => nil})
335 @topic.valid?
336 end
337
338 def test_validates_exclusion_of_generates_message_with_custom_default_message
339 Topic.validates_exclusion_of :title, :in => %w(a b c), :message => 'custom'
340 @topic.title = 'a'
341 @topic.errors.expects(:generate_message).with(:title, :exclusion, {:value => 'a', :default => 'custom'})
342 @topic.valid?
343 end
344
345 # validates_numericality_of without :only_integer w/ mocha
346
347 def test_validates_numericality_of_generates_message
348 Topic.validates_numericality_of :title
349 @topic.title = 'a'
350 @topic.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => nil})
351 @topic.valid?
352 end
353
354 def test_validates_numericality_of_generates_message_with_custom_default_message
355 Topic.validates_numericality_of :title, :message => 'custom'
356 @topic.title = 'a'
357 @topic.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => 'custom'})
358 @topic.valid?
359 end
360
361 # validates_numericality_of with :only_integer w/ mocha
362
363 def test_validates_numericality_of_only_integer_generates_message
364 Topic.validates_numericality_of :title, :only_integer => true
365 @topic.title = 'a'
366 @topic.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => nil})
367 @topic.valid?
368 end
369
370 def test_validates_numericality_of_only_integer_generates_message_with_custom_default_message
371 Topic.validates_numericality_of :title, :only_integer => true, :message => 'custom'
372 @topic.title = 'a'
373 @topic.errors.expects(:generate_message).with(:title, :not_a_number, {:value => 'a', :default => 'custom'})
374 @topic.valid?
375 end
376
377 # validates_numericality_of :odd w/ mocha
378
379 def test_validates_numericality_of_odd_generates_message
380 Topic.validates_numericality_of :title, :only_integer => true, :odd => true
381 @topic.title = 0
382 @topic.errors.expects(:generate_message).with(:title, :odd, {:value => 0, :default => nil})
383 @topic.valid?
384 end
385
386 def test_validates_numericality_of_odd_generates_message_with_custom_default_message
387 Topic.validates_numericality_of :title, :only_integer => true, :odd => true, :message => 'custom'
388 @topic.title = 0
389 @topic.errors.expects(:generate_message).with(:title, :odd, {:value => 0, :default => 'custom'})
390 @topic.valid?
391 end
392
393 # validates_numericality_of :less_than w/ mocha
394
395 def test_validates_numericality_of_less_than_generates_message
396 Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0
397 @topic.title = 1
398 @topic.errors.expects(:generate_message).with(:title, :less_than, {:value => 1, :count => 0, :default => nil})
399 @topic.valid?
400 end
401
402 def test_validates_numericality_of_odd_generates_message_with_custom_default_message
403 Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0, :message => 'custom'
404 @topic.title = 1
405 @topic.errors.expects(:generate_message).with(:title, :less_than, {:value => 1, :count => 0, :default => 'custom'})
406 @topic.valid?
407 end
408
409 # validates_associated w/ mocha
410
411 def test_validates_associated_generates_message
412 Topic.validates_associated :replies
413 replied_topic.errors.expects(:generate_message).with(:replies, :invalid, {:value => replied_topic.replies, :default => nil})
414 replied_topic.valid?
415 end
416
417 def test_validates_associated_generates_message_with_custom_default_message
418 Topic.validates_associated :replies
419 replied_topic.errors.expects(:generate_message).with(:replies, :invalid, {:value => replied_topic.replies, :default => nil})
420 replied_topic.valid?
421 end
422
423 # validates_confirmation_of w/o mocha
424
425 def test_validates_confirmation_of_finds_custom_model_key_translation
426 I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:confirmation => 'custom message'}}}}}}
427 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:confirmation => 'global message'}}}
428
429 Topic.validates_confirmation_of :title
430 @topic.title_confirmation = 'foo'
431 @topic.valid?
432 assert_equal 'custom message', @topic.errors.on(:title)
433 end
434
435 def test_validates_confirmation_of_finds_global_default_translation
436 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:confirmation => 'global message'}}}
437
438 Topic.validates_confirmation_of :title
439 @topic.title_confirmation = 'foo'
440 @topic.valid?
441 assert_equal 'global message', @topic.errors.on(:title)
442 end
443
444 # validates_acceptance_of w/o mocha
445
446 def test_validates_acceptance_of_finds_custom_model_key_translation
447 I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:accepted => 'custom message'}}}}}}
448 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:accepted => 'global message'}}}
449
450 Topic.validates_acceptance_of :title, :allow_nil => false
451 @topic.valid?
452 assert_equal 'custom message', @topic.errors.on(:title)
453 end
454
455 def test_validates_acceptance_of_finds_global_default_translation
456 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:accepted => 'global message'}}}
457
458 Topic.validates_acceptance_of :title, :allow_nil => false
459 @topic.valid?
460 assert_equal 'global message', @topic.errors.on(:title)
461 end
462
463 # validates_presence_of w/o mocha
464
465 def test_validates_presence_of_finds_custom_model_key_translation
466 I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:blank => 'custom message'}}}}}}
467 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:blank => 'global message'}}}
468
469 Topic.validates_presence_of :title
470 @topic.valid?
471 assert_equal 'custom message', @topic.errors.on(:title)
472 end
473
474 def test_validates_presence_of_finds_global_default_translation
475 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:blank => 'global message'}}}
476
477 Topic.validates_presence_of :title
478 @topic.valid?
479 assert_equal 'global message', @topic.errors.on(:title)
480 end
481
482 # validates_length_of :within w/o mocha
483
484 def test_validates_length_of_within_finds_custom_model_key_translation
485 I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:too_short => 'custom message'}}}}}}
486 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:too_short => 'global message'}}}
487
488 Topic.validates_length_of :title, :within => 3..5
489 @topic.valid?
490 assert_equal 'custom message', @topic.errors.on(:title)
491 end
492
493 def test_validates_length_of_within_finds_global_default_translation
494 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:too_short => 'global message'}}}
495
496 Topic.validates_length_of :title, :within => 3..5
497 @topic.valid?
498 assert_equal 'global message', @topic.errors.on(:title)
499 end
500
501 # validates_length_of :is w/o mocha
502
503 def test_validates_length_of_is_finds_custom_model_key_translation
504 I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}}
505 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}}
506
507 Topic.validates_length_of :title, :is => 5
508 @topic.valid?
509 assert_equal 'custom message', @topic.errors.on(:title)
510 end
511
512 def test_validates_length_of_is_finds_global_default_translation
513 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}}
514
515 Topic.validates_length_of :title, :is => 5
516 @topic.valid?
517 assert_equal 'global message', @topic.errors.on(:title)
518 end
519
520 # validates_uniqueness_of w/o mocha
521
522 def test_validates_length_of_is_finds_custom_model_key_translation
523 I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:wrong_length => 'custom message'}}}}}}
524 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}}
525
526 Topic.validates_length_of :title, :is => 5
527 @topic.valid?
528 assert_equal 'custom message', @topic.errors.on(:title)
529 end
530
531 def test_validates_length_of_is_finds_global_default_translation
532 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:wrong_length => 'global message'}}}
533
534 Topic.validates_length_of :title, :is => 5
535 @topic.valid?
536 assert_equal 'global message', @topic.errors.on(:title)
537 end
538
539
540 # validates_format_of w/o mocha
541
542 def test_validates_format_of_finds_custom_model_key_translation
543 I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:invalid => 'custom message'}}}}}}
544 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}}
545
546 Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/
547 @topic.valid?
548 assert_equal 'custom message', @topic.errors.on(:title)
549 end
550
551 def test_validates_format_of_finds_global_default_translation
552 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}}
553
554 Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/
555 @topic.valid?
556 assert_equal 'global message', @topic.errors.on(:title)
557 end
558
559 # validates_inclusion_of w/o mocha
560
561 def test_validates_inclusion_of_finds_custom_model_key_translation
562 I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:inclusion => 'custom message'}}}}}}
563 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:inclusion => 'global message'}}}
564
565 Topic.validates_inclusion_of :title, :in => %w(a b c)
566 @topic.valid?
567 assert_equal 'custom message', @topic.errors.on(:title)
568 end
569
570 def test_validates_inclusion_of_finds_global_default_translation
571 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:inclusion => 'global message'}}}
572
573 Topic.validates_inclusion_of :title, :in => %w(a b c)
574 @topic.valid?
575 assert_equal 'global message', @topic.errors.on(:title)
576 end
577
578 # validates_exclusion_of w/o mocha
579
580 def test_validates_exclusion_of_finds_custom_model_key_translation
581 I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:exclusion => 'custom message'}}}}}}
582 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:exclusion => 'global message'}}}
583
584 Topic.validates_exclusion_of :title, :in => %w(a b c)
585 @topic.title = 'a'
586 @topic.valid?
587 assert_equal 'custom message', @topic.errors.on(:title)
588 end
589
590 def test_validates_exclusion_of_finds_global_default_translation
591 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:exclusion => 'global message'}}}
592
593 Topic.validates_exclusion_of :title, :in => %w(a b c)
594 @topic.title = 'a'
595 @topic.valid?
596 assert_equal 'global message', @topic.errors.on(:title)
597 end
598
599 # validates_numericality_of without :only_integer w/o mocha
600
601 def test_validates_numericality_of_finds_custom_model_key_translation
602 I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}}
603 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}}
604
605 Topic.validates_numericality_of :title
606 @topic.title = 'a'
607 @topic.valid?
608 assert_equal 'custom message', @topic.errors.on(:title)
609 end
610
611 def test_validates_numericality_of_finds_global_default_translation
612 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}}
613
614 Topic.validates_numericality_of :title, :only_integer => true
615 @topic.title = 'a'
616 @topic.valid?
617 assert_equal 'global message', @topic.errors.on(:title)
618 end
619
620 # validates_numericality_of with :only_integer w/o mocha
621
622 def test_validates_numericality_of_only_integer_finds_custom_model_key_translation
623 I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:not_a_number => 'custom message'}}}}}}
624 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}}
625
626 Topic.validates_numericality_of :title, :only_integer => true
627 @topic.title = 'a'
628 @topic.valid?
629 assert_equal 'custom message', @topic.errors.on(:title)
630 end
631
632 def test_validates_numericality_of_only_integer_finds_global_default_translation
633 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:not_a_number => 'global message'}}}
634
635 Topic.validates_numericality_of :title, :only_integer => true
636 @topic.title = 'a'
637 @topic.valid?
638 assert_equal 'global message', @topic.errors.on(:title)
639 end
640
641 # validates_numericality_of :odd w/o mocha
642
643 def test_validates_numericality_of_odd_finds_custom_model_key_translation
644 I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:odd => 'custom message'}}}}}}
645 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:odd => 'global message'}}}
646
647 Topic.validates_numericality_of :title, :only_integer => true, :odd => true
648 @topic.title = 0
649 @topic.valid?
650 assert_equal 'custom message', @topic.errors.on(:title)
651 end
652
653 def test_validates_numericality_of_odd_finds_global_default_translation
654 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:odd => 'global message'}}}
655
656 Topic.validates_numericality_of :title, :only_integer => true, :odd => true
657 @topic.title = 0
658 @topic.valid?
659 assert_equal 'global message', @topic.errors.on(:title)
660 end
661
662 # validates_numericality_of :less_than w/o mocha
663
664 def test_validates_numericality_of_less_than_finds_custom_model_key_translation
665 I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:less_than => 'custom message'}}}}}}
666 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:less_than => 'global message'}}}
667
668 Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0
669 @topic.title = 1
670 @topic.valid?
671 assert_equal 'custom message', @topic.errors.on(:title)
672 end
673
674 def test_validates_numericality_of_less_than_finds_global_default_translation
675 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:less_than => 'global message'}}}
676
677 Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0
678 @topic.title = 1
679 @topic.valid?
680 assert_equal 'global message', @topic.errors.on(:title)
681 end
682
683
684 # validates_associated w/o mocha
685
686 def test_validates_associated_finds_custom_model_key_translation
687 I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:replies => {:invalid => 'custom message'}}}}}}
688 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}}
689
690 Topic.validates_associated :replies
691 replied_topic.valid?
692 assert_equal 'custom message', replied_topic.errors.on(:replies)
693 end
694
695 def test_validates_associated_finds_global_default_translation
696 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:invalid => 'global message'}}}
697
698 Topic.validates_associated :replies
699 replied_topic.valid?
700 assert_equal 'global message', replied_topic.errors.on(:replies)
701 end
702
703 def test_validations_with_message_symbol_must_translate
704 I18n.backend.store_translations 'en', :activerecord => {:errors => {:messages => {:custom_error => "I am a custom error"}}}
705 Topic.validates_presence_of :title, :message => :custom_error
706 @topic.title = nil
707 @topic.valid?
708 assert_equal "I am a custom error", @topic.errors.on(:title)
709 end
710
711 def test_validates_with_message_symbol_must_translate_per_attribute
712 I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:attributes => {:title => {:custom_error => "I am a custom error"}}}}}}
713 Topic.validates_presence_of :title, :message => :custom_error
714 @topic.title = nil
715 @topic.valid?
716 assert_equal "I am a custom error", @topic.errors.on(:title)
717 end
718
719 def test_validates_with_message_symbol_must_translate_per_model
720 I18n.backend.store_translations 'en', :activerecord => {:errors => {:models => {:topic => {:custom_error => "I am a custom error"}}}}
721 Topic.validates_presence_of :title, :message => :custom_error
722 @topic.title = nil
723 @topic.valid?
724 assert_equal "I am a custom error", @topic.errors.on(:title)
725 end
726
727 def test_validates_with_message_string
728 Topic.validates_presence_of :title, :message => "I am a custom error"
729 @topic.title = nil
730 @topic.valid?
731 assert_equal "I am a custom error", @topic.errors.on(:title)
732 end
733
734 end
735
736 class ActiveRecordValidationsGenerateMessageI18nTests < Test::Unit::TestCase
737 def setup
738 reset_callbacks Topic
739 @topic = Topic.new
740 I18n.backend.store_translations :'en', {
741 :activerecord => {
742 :errors => {
743 :messages => {
744 :inclusion => "is not included in the list",
745 :exclusion => "is reserved",
746 :invalid => "is invalid",
747 :confirmation => "doesn't match confirmation",
748 :accepted => "must be accepted",
749 :empty => "can't be empty",
750 :blank => "can't be blank",
751 :too_long => "is too long (maximum is {{count}} characters)",
752 :too_short => "is too short (minimum is {{count}} characters)",
753 :wrong_length => "is the wrong length (should be {{count}} characters)",
754 :taken => "has already been taken",
755 :not_a_number => "is not a number",
756 :greater_than => "must be greater than {{count}}",
757 :greater_than_or_equal_to => "must be greater than or equal to {{count}}",
758 :equal_to => "must be equal to {{count}}",
759 :less_than => "must be less than {{count}}",
760 :less_than_or_equal_to => "must be less than or equal to {{count}}",
761 :odd => "must be odd",
762 :even => "must be even"
763 }
764 }
765 }
766 }
767 end
768
769 def reset_callbacks(*models)
770 models.each do |model|
771 model.instance_variable_set("@validate_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
772 model.instance_variable_set("@validate_on_create_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
773 model.instance_variable_set("@validate_on_update_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
774 end
775 end
776
777 # validates_inclusion_of: generate_message(attr_name, :inclusion, :default => configuration[:message], :value => value)
778 def test_generate_message_inclusion_with_default_message
779 assert_equal 'is not included in the list', @topic.errors.generate_message(:title, :inclusion, :default => nil, :value => 'title')
780 end
781
782 def test_generate_message_inclusion_with_custom_message
783 assert_equal 'custom message title', @topic.errors.generate_message(:title, :inclusion, :default => 'custom message {{value}}', :value => 'title')
784 end
785
786 # validates_exclusion_of: generate_message(attr_name, :exclusion, :default => configuration[:message], :value => value)
787 def test_generate_message_exclusion_with_default_message
788 assert_equal 'is reserved', @topic.errors.generate_message(:title, :exclusion, :default => nil, :value => 'title')
789 end
790
791 def test_generate_message_exclusion_with_custom_message
792 assert_equal 'custom message title', @topic.errors.generate_message(:title, :exclusion, :default => 'custom message {{value}}', :value => 'title')
793 end
794
795 # validates_associated: generate_message(attr_name, :invalid, :default => configuration[:message], :value => value)
796 # validates_format_of: generate_message(attr_name, :invalid, :default => configuration[:message], :value => value)
797 def test_generate_message_invalid_with_default_message
798 assert_equal 'is invalid', @topic.errors.generate_message(:title, :invalid, :default => nil, :value => 'title')
799 end
800
801 def test_generate_message_invalid_with_custom_message
802 assert_equal 'custom message title', @topic.errors.generate_message(:title, :invalid, :default => 'custom message {{value}}', :value => 'title')
803 end
804
805 # validates_confirmation_of: generate_message(attr_name, :confirmation, :default => configuration[:message])
806 def test_generate_message_confirmation_with_default_message
807 assert_equal "doesn't match confirmation", @topic.errors.generate_message(:title, :confirmation, :default => nil)
808 end
809
810 def test_generate_message_confirmation_with_custom_message
811 assert_equal 'custom message', @topic.errors.generate_message(:title, :confirmation, :default => 'custom message')
812 end
813
814 # validates_acceptance_of: generate_message(attr_name, :accepted, :default => configuration[:message])
815 def test_generate_message_accepted_with_default_message
816 assert_equal "must be accepted", @topic.errors.generate_message(:title, :accepted, :default => nil)
817 end
818
819 def test_generate_message_accepted_with_custom_message
820 assert_equal 'custom message', @topic.errors.generate_message(:title, :accepted, :default => 'custom message')
821 end
822
823 # add_on_empty: generate_message(attr, :empty, :default => custom_message)
824 def test_generate_message_empty_with_default_message
825 assert_equal "can't be empty", @topic.errors.generate_message(:title, :empty, :default => nil)
826 end
827
828 def test_generate_message_empty_with_custom_message
829 assert_equal 'custom message', @topic.errors.generate_message(:title, :empty, :default => 'custom message')
830 end
831
832 # add_on_blank: generate_message(attr, :blank, :default => custom_message)
833 def test_generate_message_blank_with_default_message
834 assert_equal "can't be blank", @topic.errors.generate_message(:title, :blank, :default => nil)
835 end
836
837 def test_generate_message_blank_with_custom_message
838 assert_equal 'custom message', @topic.errors.generate_message(:title, :blank, :default => 'custom message')
839 end
840
841 # validates_length_of: generate_message(attr, :too_long, :default => options[:too_long], :count => option_value.end)
842 def test_generate_message_too_long_with_default_message
843 assert_equal "is too long (maximum is 10 characters)", @topic.errors.generate_message(:title, :too_long, :default => nil, :count => 10)
844 end
845
846 def test_generate_message_too_long_with_custom_message
847 assert_equal 'custom message 10', @topic.errors.generate_message(:title, :too_long, :default => 'custom message {{count}}', :count => 10)
848 end
849
850 # validates_length_of: generate_message(attr, :too_short, :default => options[:too_short], :count => option_value.begin)
851 def test_generate_message_too_short_with_default_message
852 assert_equal "is too short (minimum is 10 characters)", @topic.errors.generate_message(:title, :too_short, :default => nil, :count => 10)
853 end
854
855 def test_generate_message_too_short_with_custom_message
856 assert_equal 'custom message 10', @topic.errors.generate_message(:title, :too_short, :default => 'custom message {{count}}', :count => 10)
857 end
858
859 # validates_length_of: generate_message(attr, key, :default => custom_message, :count => option_value)
860 def test_generate_message_wrong_length_with_default_message
861 assert_equal "is the wrong length (should be 10 characters)", @topic.errors.generate_message(:title, :wrong_length, :default => nil, :count => 10)
862 end
863
864 def test_generate_message_wrong_length_with_custom_message
865 assert_equal 'custom message 10', @topic.errors.generate_message(:title, :wrong_length, :default => 'custom message {{count}}', :count => 10)
866 end
867
868 # validates_uniqueness_of: generate_message(attr_name, :taken, :default => configuration[:message])
869 def test_generate_message_taken_with_default_message
870 assert_equal "has already been taken", @topic.errors.generate_message(:title, :taken, :default => nil, :value => 'title')
871 end
872
873 def test_generate_message_taken_with_custom_message
874 assert_equal 'custom message title', @topic.errors.generate_message(:title, :taken, :default => 'custom message {{value}}', :value => 'title')
875 end
876
877 # validates_numericality_of: generate_message(attr_name, :not_a_number, :value => raw_value, :default => configuration[:message])
878 def test_generate_message_not_a_number_with_default_message
879 assert_equal "is not a number", @topic.errors.generate_message(:title, :not_a_number, :default => nil, :value => 'title')
880 end
881
882 def test_generate_message_not_a_number_with_custom_message
883 assert_equal 'custom message title', @topic.errors.generate_message(:title, :not_a_number, :default => 'custom message {{value}}', :value => 'title')
884 end
885
886 # validates_numericality_of: generate_message(attr_name, option, :value => raw_value, :default => configuration[:message])
887 def test_generate_message_greater_than_with_default_message
888 assert_equal "must be greater than 10", @topic.errors.generate_message(:title, :greater_than, :default => nil, :value => 'title', :count => 10)
889 end
890
891 def test_generate_message_greater_than_or_equal_to_with_default_message
892 assert_equal "must be greater than or equal to 10", @topic.errors.generate_message(:title, :greater_than_or_equal_to, :default => nil, :value => 'title', :count => 10)
893 end
894
895 def test_generate_message_equal_to_with_default_message
896 assert_equal "must be equal to 10", @topic.errors.generate_message(:title, :equal_to, :default => nil, :value => 'title', :count => 10)
897 end
898
899 def test_generate_message_less_than_with_default_message
900 assert_equal "must be less than 10", @topic.errors.generate_message(:title, :less_than, :default => nil, :value => 'title', :count => 10)
901 end
902
903 def test_generate_message_less_than_or_equal_to_with_default_message
904 assert_equal "must be less than or equal to 10", @topic.errors.generate_message(:title, :less_than_or_equal_to, :default => nil, :value => 'title', :count => 10)
905 end
906
907 def test_generate_message_odd_with_default_message
908 assert_equal "must be odd", @topic.errors.generate_message(:title, :odd, :default => nil, :value => 'title', :count => 10)
909 end
910
911 def test_generate_message_even_with_default_message
912 assert_equal "must be even", @topic.errors.generate_message(:title, :even, :default => nil, :value => 'title', :count => 10)
913 end
914
915 end