4
require 'models/person'
6
module ActiveRecordValidationsI18nTestHelper
7
def store_translations(*args)
8
data = args.extract_options!
9
locale = args.shift || 'en'
10
I18n.backend.send(:init_translations)
11
I18n.backend.store_translations(locale, :activerecord => data)
14
def delete_translation(key)
15
I18n.backend.instance_eval do
16
keys = I18n.send(:normalize_translation_keys, 'en', key, nil)
17
keys.inject(translations) { |result, k| keys.last == k ? result.delete(k.to_sym) : result[k.to_sym] }
21
def reset_callbacks(*models)
22
models.each do |model|
23
model.instance_variable_set("@validate_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
24
model.instance_variable_set("@validate_on_create_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
25
model.instance_variable_set("@validate_on_update_callbacks", ActiveSupport::Callbacks::CallbackChain.new)
32
class ActiveRecordValidationsI18nDeprecationsTests < ActiveSupport::TestCase
33
test "default_error_messages is deprecated and can be removed in Rails 3 / ActiveModel" do
34
assert_deprecated('ActiveRecord::Errors.default_error_messages') do
35
ActiveRecord::Errors.default_error_messages
39
test "%s interpolation syntax in error messages still works" do
40
ActiveSupport::Deprecation.silence do
41
result = I18n.t :does_not_exist, :default => "%s interpolation syntax is deprecated", :value => 'this'
42
assert_equal result, "this interpolation syntax is deprecated"
46
test "%s interpolation syntax in error messages is deprecated" do
47
assert_deprecated('using %s in messages') do
48
I18n.t :does_not_exist, :default => "%s interpolation syntax is deprected", :value => 'this'
52
test "%d interpolation syntax in error messages still works" do
53
ActiveSupport::Deprecation.silence do
54
result = I18n.t :does_not_exist, :default => "%d interpolation syntaxes are deprecated", :count => 2
55
assert_equal result, "2 interpolation syntaxes are deprecated"
59
test "%d interpolation syntax in error messages is deprecated" do
60
assert_deprecated('using %d in messages') do
61
I18n.t :does_not_exist, :default => "%d interpolation syntaxes are deprected", :count => 2
67
# ACTIVERECORD VALIDATIONS
69
# For each validation:
71
# * test expect that it adds an error with the appropriate arguments
72
# * test that it looks up the correct default message
74
class ActiveRecordValidationsI18nTests < ActiveSupport::TestCase
75
include ActiveRecordValidationsI18nTestHelper
78
reset_callbacks(Topic)
81
@old_load_path, @old_backend = I18n.load_path, I18n.backend
83
I18n.backend = I18n::Backend::Simple.new
84
I18n.backend.store_translations('en', :activerecord => {:errors => {:messages => {:custom => nil}}})
88
reset_callbacks(Topic)
89
I18n.load_path.replace(@old_load_path)
90
I18n.backend = @old_backend
93
def expect_error_added(model, attribute, type, options)
94
model.errors.expects(:add).with(attribute, type, options)
99
def assert_message_translations(model, attribute, type, &block)
100
assert_default_message_translation(model, attribute, type, &block)
101
reset_callbacks(model.class)
103
assert_custom_message_translation(model, attribute, type, &block)
106
def assert_custom_message_translation(model, attribute, type)
107
store_translations(:errors => { :models => { model.class.name.underscore => { :attributes => { attribute => { type => 'custom message' } } } } })
110
assert_equal 'custom message', model.errors.on(attribute)
113
def assert_default_message_translation(model, attribute, type)
114
store_translations(:errors => { :messages => { type => 'default message' } })
117
assert_equal 'default message', model.errors.on(attribute)
121
@unique ||= Topic.create(:title => 'unique!')
125
@replied_topic ||= begin
126
topic = Topic.create(:title => "topic")
127
topic.replies << Reply.new
132
# validates_confirmation_of
134
test "#validates_confirmation_of given no custom message" do
135
expect_error_added(@topic, :title, :confirmation, :default => nil) do
136
Topic.validates_confirmation_of :title
137
@topic.title = 'title'
138
@topic.title_confirmation = 'foo'
142
test "#validates_confirmation_of given a custom message" do
143
expect_error_added(@topic, :title, :confirmation, :default => 'custom') do
144
Topic.validates_confirmation_of :title, :message => 'custom'
145
@topic.title_confirmation = 'foo'
149
test "#validates_confirmation_of finds the correct message translations" do
150
assert_message_translations(@topic, :title, :confirmation) do
151
Topic.validates_confirmation_of :title
152
@topic.title_confirmation = 'foo'
156
# validates_acceptance_of
158
test "#validates_acceptance_of given no custom message" do
159
expect_error_added(@topic, :title, :accepted, :default => nil) do
160
Topic.validates_acceptance_of :title, :allow_nil => false
164
test "#validates_acceptance_of given a custom message" do
165
expect_error_added(@topic, :title, :accepted, :default => 'custom') do
166
Topic.validates_acceptance_of :title, :message => 'custom', :allow_nil => false
170
test "#validates_acceptance_of finds the correct message translations" do
171
assert_message_translations(@topic, :title, :accepted) do
172
Topic.validates_acceptance_of :title, :allow_nil => false
176
# validates_presence_of
178
test "#validates_presence_of given no custom message" do
179
expect_error_added(@topic, :title, :blank, :default => nil) do
180
Topic.validates_presence_of :title
184
test "#validates_presence_of given a custom message" do
185
expect_error_added(@topic, :title, :blank, :default => 'custom') do
186
Topic.validates_presence_of :title, :message => 'custom'
190
test "#validates_presence_of finds the correct message translations" do
191
assert_message_translations(@topic, :title, :blank) do
192
Topic.validates_presence_of :title
196
# validates_length_of :too_short
198
test "#validates_length_of (:too_short) and no custom message" do
199
expect_error_added(@topic, :title, :too_short, :default => nil, :count => 3) do
200
Topic.validates_length_of :title, :within => 3..5
204
test "#validates_length_of (:too_short) and a custom message" do
205
expect_error_added(@topic, :title, :too_short, :default => 'custom', :count => 3) do
206
Topic.validates_length_of :title, :within => 3..5, :too_short => 'custom'
210
test "#validates_length_of (:too_short) finds the correct message translations" do
211
assert_message_translations(@topic, :title, :too_short) do
212
Topic.validates_length_of :title, :within => 3..5
216
# validates_length_of :too_long
218
test "#validates_length_of (:too_long) and no custom message" do
219
expect_error_added(@topic, :title, :too_long, :default => nil, :count => 5) do
220
Topic.validates_length_of :title, :within => 3..5
221
@topic.title = 'this title is too long'
225
test "#validates_length_of (:too_long) and a custom message" do
226
expect_error_added(@topic, :title, :too_long, :default => 'custom', :count => 5) do
227
Topic.validates_length_of :title, :within => 3..5, :too_long => 'custom'
228
@topic.title = 'this title is too long'
232
test "#validates_length_of (:too_long) finds the correct message translations" do
233
assert_message_translations(@topic, :title, :too_long) do
234
Topic.validates_length_of :title, :within => 3..5
235
@topic.title = 'this title is too long'
239
# validates_length_of :is
241
test "#validates_length_of (:is) and no custom message" do
242
expect_error_added(@topic, :title, :wrong_length, :default => nil, :count => 5) do
243
Topic.validates_length_of :title, :is => 5
244
@topic.title = 'this title has the wrong length'
248
test "#validates_length_of (:is) and a custom message" do
249
expect_error_added(@topic, :title, :wrong_length, :default => 'custom', :count => 5) do
250
Topic.validates_length_of :title, :is => 5, :wrong_length => 'custom'
251
@topic.title = 'this title has the wrong length'
255
test "#validates_length_of (:is) finds the correct message translations" do
256
assert_message_translations(@topic, :title, :wrong_length) do
257
Topic.validates_length_of :title, :is => 5
258
@topic.title = 'this title has the wrong length'
262
# validates_uniqueness_of
264
test "#validates_uniqueness_of and no custom message" do
265
expect_error_added(@topic, :title, :taken, :default => nil, :value => 'unique!') do
266
Topic.validates_uniqueness_of :title
267
@topic.title = unique_topic.title
271
test "#validates_uniqueness_of and a custom message" do
272
expect_error_added(@topic, :title, :taken, :default => 'custom', :value => 'unique!') do
273
Topic.validates_uniqueness_of :title, :message => 'custom'
274
@topic.title = unique_topic.title
278
test "#validates_uniqueness_of finds the correct message translations" do
279
assert_message_translations(@topic, :title, :taken) do
280
Topic.validates_uniqueness_of :title
281
@topic.title = unique_topic.title
285
# validates_format_of
287
test "#validates_format_of and no custom message" do
288
expect_error_added(@topic, :title, :invalid, :default => nil, :value => '72x') do
289
Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/
294
test "#validates_format_of and a custom message" do
295
expect_error_added(@topic, :title, :invalid, :default => 'custom', :value => '72x') do
296
Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/, :message => 'custom'
301
test "#validates_format_of finds the correct message translations" do
302
assert_message_translations(@topic, :title, :invalid) do
303
Topic.validates_format_of :title, :with => /^[1-9][0-9]*$/
308
# validates_inclusion_of
310
test "#validates_inclusion_of and no custom message" do
312
expect_error_added(@topic, :title, :inclusion, :default => nil, :value => 'z') do
313
Topic.validates_inclusion_of :title, :in => list
318
test "#validates_inclusion_of and a custom message" do
320
expect_error_added(@topic, :title, :inclusion, :default => 'custom', :value => 'z') do
321
Topic.validates_inclusion_of :title, :in => list, :message => 'custom'
326
test "#validates_inclusion_of finds the correct message translations" do
328
assert_message_translations(@topic, :title, :inclusion) do
329
Topic.validates_inclusion_of :title, :in => list
334
# validates_exclusion_of
336
test "#validates_exclusion_of and no custom message" do
338
expect_error_added(@topic, :title, :exclusion, :default => nil, :value => 'a') do
339
Topic.validates_exclusion_of :title, :in => list
344
test "#validates_exclusion_of and a custom message" do
346
expect_error_added(@topic, :title, :exclusion, :default => 'custom', :value => 'a') do
347
Topic.validates_exclusion_of :title, :in => list, :message => 'custom'
352
test "#validates_exclusion_of finds the correct message translations" do
354
assert_message_translations(@topic, :title, :exclusion) do
355
Topic.validates_exclusion_of :title, :in => list
360
# validates_numericality_of :not_a_number, without :only_integer
362
test "#validates_numericality_of (:not_a_number, w/o :only_integer) no custom message" do
363
expect_error_added(@topic, :title, :not_a_number, :default => nil, :value => 'a') do
364
Topic.validates_numericality_of :title
369
test "#validates_numericality_of (:not_a_number, w/o :only_integer) and a custom message" do
370
expect_error_added(@topic, :title, :not_a_number, :default => 'custom', :value => 'a') do
371
Topic.validates_numericality_of :title, :message => 'custom'
376
test "#validates_numericality_of (:not_a_number, w/o :only_integer) finds the correct message translations" do
377
assert_message_translations(@topic, :title, :not_a_number) do
378
Topic.validates_numericality_of :title
383
# validates_numericality_of :not_a_number, with :only_integer
385
test "#validates_numericality_of (:not_a_number, with :only_integer) no custom message" do
386
expect_error_added(@topic, :title, :not_a_number, :default => nil, :value => 'a') do
387
Topic.validates_numericality_of :title, :only_integer => true
392
test "#validates_numericality_of (:not_a_number, with :only_integer) and a custom message" do
393
expect_error_added(@topic, :title, :not_a_number, :default => 'custom', :value => 'a') do
394
Topic.validates_numericality_of :title, :only_integer => true, :message => 'custom'
399
test "#validates_numericality_of (:not_a_number, with :only_integer) finds the correct message translations" do
400
assert_message_translations(@topic, :title, :not_a_number) do
401
Topic.validates_numericality_of :title, :only_integer => true
406
# validates_numericality_of :odd
408
test "#validates_numericality_of (:odd) no custom message" do
409
expect_error_added(@topic, :title, :odd, :default => nil, :value => 0) do
410
Topic.validates_numericality_of :title, :only_integer => true, :odd => true
415
test "#validates_numericality_of (:odd) and a custom message" do
416
expect_error_added(@topic, :title, :odd, :default => 'custom', :value => 0) do
417
Topic.validates_numericality_of :title, :only_integer => true, :odd => true, :message => 'custom'
422
test "#validates_numericality_of (:odd) finds the correct message translations" do
423
assert_message_translations(@topic, :title, :odd) do
424
Topic.validates_numericality_of :title, :only_integer => true, :odd => true
429
# validates_numericality_of :even
431
test "#validates_numericality_of (:even) no custom message" do
432
expect_error_added(@topic, :title, :even, :default => nil, :value => 1) do
433
Topic.validates_numericality_of :title, :only_integer => true, :even => true
438
test "#validates_numericality_of (:even) and a custom message" do
439
expect_error_added(@topic, :title, :even, :default => 'custom', :value => 1) do
440
Topic.validates_numericality_of :title, :only_integer => true, :even => true, :message => 'custom'
445
test "#validates_numericality_of (:even) finds the correct message translations" do
446
assert_message_translations(@topic, :title, :even) do
447
Topic.validates_numericality_of :title, :only_integer => true, :even => true
452
# validates_numericality_of :less_than
454
test "#validates_numericality_of (:less_than) no custom message" do
455
expect_error_added(@topic, :title, :less_than, :default => nil, :value => 1, :count => 0) do
456
Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0
461
test "#validates_numericality_of (:less_than) and a custom message" do
462
expect_error_added(@topic, :title, :less_than, :default => 'custom', :value => 1, :count => 0) do
463
Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0, :message => 'custom'
468
test "#validates_numericality_of (:less_than) finds the correct message translations" do
469
assert_message_translations(@topic, :title, :less_than) do
470
Topic.validates_numericality_of :title, :only_integer => true, :less_than => 0
475
# validates_associated
477
test "#validates_associated no custom message" do
478
expect_error_added(replied_topic, :replies, :invalid, :default => nil, :value => replied_topic.replies) do
479
Topic.validates_associated :replies
483
test "#validates_associated and a custom message" do
484
expect_error_added(replied_topic, :replies, :invalid, :default => 'custom', :value => replied_topic.replies) do
485
Topic.validates_associated :replies, :message => 'custom'
489
test "#validates_associated finds the correct message translations" do
490
assert_message_translations(replied_topic, :replies, :invalid) do
491
Topic.validates_associated :replies
499
# * test that it passes given interpolation arguments, the human model name and human attribute name
500
# * test that it looks messages up with the the correct keys
501
# * test that it looks up the correct default messages
503
class ActiveRecordErrorI18nTests < ActiveSupport::TestCase
504
include ActiveRecordValidationsI18nTestHelper
508
@old_backend, I18n.backend = I18n.backend, I18n::Backend::Simple.new
512
I18n.backend = @old_backend
516
def assert_error_message(message, *args)
517
assert_equal message, ActiveRecord::Error.new(@reply, *args).message
520
def assert_full_message(message, *args)
521
assert_equal message, ActiveRecord::Error.new(@reply, *args).full_message
524
test "#generate_message passes the model attribute value for interpolation" do
525
store_translations(:errors => { :messages => { :foo => "You fooed: {{value}}." } })
526
@reply.title = "da title"
527
assert_error_message 'You fooed: da title.', :title, :foo
530
test "#generate_message passes the human_name of the model for interpolation" do
532
:errors => { :messages => { :foo => "You fooed: {{model}}." } },
533
:models => { :topic => 'da topic' }
535
assert_error_message 'You fooed: da topic.', :title, :foo
538
test "#generate_message passes the human_name of the attribute for interpolation" do
540
:errors => { :messages => { :foo => "You fooed: {{attribute}}." } },
541
:attributes => { :topic => { :title => 'da topic title' } }
543
assert_error_message 'You fooed: da topic title.', :title, :foo
546
# generate_message will look up the key for the error message (e.g. :blank) in these namespaces:
548
# activerecord.errors.models.reply.attributes.title
549
# activerecord.errors.models.reply
550
# activerecord.errors.models.topic.attributes.title
551
# activerecord.errors.models.topic
552
# [default from class level :validates_foo statement if this is a String]
553
# activerecord.errors.messages
555
test "#generate_message key fallbacks (given a String as key)" do
560
:attributes => { :title => { :custom => 'activerecord.errors.models.reply.attributes.title.custom' } },
561
:custom => 'activerecord.errors.models.reply.custom'
564
:attributes => { :title => { :custom => 'activerecord.errors.models.topic.attributes.title.custom' } },
565
:custom => 'activerecord.errors.models.topic.custom'
569
:custom => 'activerecord.errors.messages.custom',
570
:kaputt => 'activerecord.errors.messages.kaputt'
575
assert_error_message 'activerecord.errors.models.reply.attributes.title.custom', :title, :kaputt, :message => 'custom'
576
delete_translation :'activerecord.errors.models.reply.attributes.title.custom'
578
assert_error_message 'activerecord.errors.models.reply.custom', :title, :kaputt, :message => 'custom'
579
delete_translation :'activerecord.errors.models.reply.custom'
581
assert_error_message 'activerecord.errors.models.topic.attributes.title.custom', :title, :kaputt, :message => 'custom'
582
delete_translation :'activerecord.errors.models.topic.attributes.title.custom'
584
assert_error_message 'activerecord.errors.models.topic.custom', :title, :kaputt, :message => 'custom'
585
delete_translation :'activerecord.errors.models.topic.custom'
587
assert_error_message 'activerecord.errors.messages.custom', :title, :kaputt, :message => 'custom'
588
delete_translation :'activerecord.errors.messages.custom'
590
# Implementing this would clash with the AR default behaviour of using validates_foo :message => 'foo'
591
# as an untranslated string. I.e. at this point we can either fall back to the given string from the
592
# class-level macro (validates_*) or fall back to the default message for this validation type.
593
# assert_error_message 'activerecord.errors.messages.kaputt', :title, :kaputt, :message => 'custom'
595
assert_error_message 'custom', :title, :kaputt, :message => 'custom'
598
test "#generate_message key fallbacks (given a Symbol as key)" do
603
:attributes => { :title => { :kaputt => 'activerecord.errors.models.reply.attributes.title.kaputt' } },
604
:kaputt => 'activerecord.errors.models.reply.kaputt'
607
:attributes => { :title => { :kaputt => 'activerecord.errors.models.topic.attributes.title.kaputt' } },
608
:kaputt => 'activerecord.errors.models.topic.kaputt'
612
:kaputt => 'activerecord.errors.messages.kaputt'
617
assert_error_message 'activerecord.errors.models.reply.attributes.title.kaputt', :title, :kaputt
618
delete_translation :'activerecord.errors.models.reply.attributes.title.kaputt'
620
assert_error_message 'activerecord.errors.models.reply.kaputt', :title, :kaputt
621
delete_translation :'activerecord.errors.models.reply.kaputt'
623
assert_error_message 'activerecord.errors.models.topic.attributes.title.kaputt', :title, :kaputt
624
delete_translation :'activerecord.errors.models.topic.attributes.title.kaputt'
626
assert_error_message 'activerecord.errors.models.topic.kaputt', :title, :kaputt
627
delete_translation :'activerecord.errors.models.topic.kaputt'
629
assert_error_message 'activerecord.errors.messages.kaputt', :title, :kaputt
634
test "#full_message with no format present" do
635
store_translations(:errors => { :messages => { :kaputt => 'is kaputt' } })
636
assert_full_message 'Title is kaputt', :title, :kaputt
639
test "#full_message with a format present" do
640
store_translations(:errors => { :messages => { :kaputt => 'is kaputt' }, :full_messages => { :format => '{{attribute}}: {{message}}' } })
641
assert_full_message 'Title: is kaputt', :title, :kaputt
644
test "#full_message with a type specific format present" do
645
store_translations(:errors => { :messages => { :kaputt => 'is kaputt' }, :full_messages => { :kaputt => '{{attribute}} {{message}}!' } })
646
assert_full_message 'Title is kaputt!', :title, :kaputt
649
test "#full_message with class-level specified custom message" do
650
store_translations(:errors => { :messages => { :broken => 'is kaputt' }, :full_messages => { :broken => '{{attribute}} {{message}}?!' } })
651
assert_full_message 'Title is kaputt?!', :title, :kaputt, :message => :broken
654
test "#full_message with different scope" do
655
store_translations(:my_errors => { :messages => { :kaputt => 'is kaputt' } })
656
assert_full_message 'Title is kaputt', :title, :kaputt, :scope => [:activerecord, :my_errors]
658
store_translations(:my_errors => { :full_messages => { :kaputt => '{{attribute}} {{message}}!' } })
659
assert_full_message 'Title is kaputt!', :title, :kaputt, :scope => [:activerecord, :my_errors]
664
test "#message allows to switch locales" do
665
store_translations(:en, :errors => { :messages => { :kaputt => 'is kaputt' } })
666
store_translations(:de, :errors => { :messages => { :kaputt => 'ist kaputt' } })
668
assert_error_message 'is kaputt', :title, :kaputt
670
assert_error_message 'ist kaputt', :title, :kaputt
672
assert_error_message 'is kaputt', :title, :kaputt
675
test "#full_message allows to switch locales" do
676
store_translations(:en, :errors => { :messages => { :kaputt => 'is kaputt' } }, :attributes => { :topic => { :title => 'The title' } })
677
store_translations(:de, :errors => { :messages => { :kaputt => 'ist kaputt' } }, :attributes => { :topic => { :title => 'Der Titel' } })
679
assert_full_message 'The title is kaputt', :title, :kaputt
681
assert_full_message 'Der Titel ist kaputt', :title, :kaputt
683
assert_full_message 'The title is kaputt', :title, :kaputt
687
# ACTIVERECORD DEFAULT ERROR MESSAGES
689
# * test that Error generates the default error messages
691
class ActiveRecordDefaultErrorMessagesI18nTests < ActiveSupport::TestCase
692
def assert_default_error_message(message, *args)
693
assert_equal message, error_message(*args)
696
def error_message(*args)
697
ActiveRecord::Error.new(Topic.new, :title, *args).message
700
# used by: validates_inclusion_of
701
test "default error message: inclusion" do
702
assert_default_error_message 'is not included in the list', :inclusion, :value => 'title'
705
# used by: validates_exclusion_of
706
test "default error message: exclusion" do
707
assert_default_error_message 'is reserved', :exclusion, :value => 'title'
710
# used by: validates_associated and validates_format_of
711
test "default error message: invalid" do
712
assert_default_error_message 'is invalid', :invalid, :value => 'title'
715
# used by: validates_confirmation_of
716
test "default error message: confirmation" do
717
assert_default_error_message "doesn't match confirmation", :confirmation, :default => nil
720
# used by: validates_acceptance_of
721
test "default error message: accepted" do
722
assert_default_error_message "must be accepted", :accepted
725
# used by: add_on_empty
726
test "default error message: empty" do
727
assert_default_error_message "can't be empty", :empty
730
# used by: add_on_blank
731
test "default error message: blank" do
732
assert_default_error_message "can't be blank", :blank
735
# used by: validates_length_of
736
test "default error message: too_long" do
737
assert_default_error_message "is too long (maximum is 10 characters)", :too_long, :count => 10
740
# used by: validates_length_of
741
test "default error message: too_short" do
742
assert_default_error_message "is too short (minimum is 10 characters)", :too_short, :count => 10
745
# used by: validates_length_of
746
test "default error message: wrong_length" do
747
assert_default_error_message "is the wrong length (should be 10 characters)", :wrong_length, :count => 10
750
# used by: validates_uniqueness_of
751
test "default error message: taken" do
752
assert_default_error_message "has already been taken", :taken, :value => 'title'
755
# used by: validates_numericality_of
756
test "default error message: not_a_number" do
757
assert_default_error_message "is not a number", :not_a_number, :value => 'title'
760
# used by: validates_numericality_of
761
test "default error message: greater_than" do
762
assert_default_error_message "must be greater than 10", :greater_than, :value => 'title', :count => 10
765
# used by: validates_numericality_of
766
test "default error message: greater_than_or_equal_to" do
767
assert_default_error_message "must be greater than or equal to 10", :greater_than_or_equal_to, :value => 'title', :count => 10
770
# used by: validates_numericality_of
771
test "default error message: equal_to" do
772
assert_default_error_message "must be equal to 10", :equal_to, :value => 'title', :count => 10
775
# used by: validates_numericality_of
776
test "default error message: less_than" do
777
assert_default_error_message "must be less than 10", :less_than, :value => 'title', :count => 10
780
# used by: validates_numericality_of
781
test "default error message: less_than_or_equal_to" do
782
assert_default_error_message "must be less than or equal to 10", :less_than_or_equal_to, :value => 'title', :count => 10
785
# used by: validates_numericality_of
786
test "default error message: odd" do
787
assert_default_error_message "must be odd", :odd, :value => 'title', :count => 10
790
# used by: validates_numericality_of
791
test "default error message: even" do
792
assert_default_error_message "must be even", :even, :value => 'title', :count => 10
795
test "custom message string interpolation" do
796
assert_equal 'custom message title', error_message(:invalid, :default => 'custom message {{value}}', :value => 'title')
800
# ACTIVERECORD VALIDATION ERROR MESSAGES - FULL STACK
802
# * test a few combinations full stack to ensure the tests above are correct
804
class I18nPerson < Person
807
class ActiveRecordValidationsI18nFullStackTests < ActiveSupport::TestCase
808
include ActiveRecordValidationsI18nTestHelper
811
reset_callbacks(I18nPerson)
812
@old_backend, I18n.backend = I18n.backend, I18n::Backend::Simple.new
813
@person = I18nPerson.new
817
reset_callbacks(I18nPerson)
818
I18n.backend = @old_backend
821
def assert_name_invalid(message)
824
assert_equal message, @person.errors.on(:name)
827
# Symbols as class-level validation messages
829
test "Symbol as class level validation message translated per attribute (translation on child class)" do
830
assert_name_invalid("is broken") do
831
store_translations :errors => {:models => {:i18n_person => {:attributes => {:name => {:broken => "is broken"}}}}}
832
I18nPerson.validates_presence_of :name, :message => :broken
836
test "Symbol as class level validation message translated per attribute (translation on base class)" do
837
assert_name_invalid("is broken") do
838
store_translations :errors => {:models => {:person => {:attributes => {:name => {:broken => "is broken"}}}}}
839
I18nPerson.validates_presence_of :name, :message => :broken
843
test "Symbol as class level validation message translated per model (translation on child class)" do
844
assert_name_invalid("is broken") do
845
store_translations :errors => {:models => {:i18n_person => {:broken => "is broken"}}}
846
I18nPerson.validates_presence_of :name, :message => :broken
850
test "Symbol as class level validation message translated per model (translation on base class)" do
851
assert_name_invalid("is broken") do
852
store_translations :errors => {:models => {:person => {:broken => "is broken"}}}
853
I18nPerson.validates_presence_of :name, :message => :broken
857
test "Symbol as class level validation message translated as error message" do
858
assert_name_invalid("is broken") do
859
store_translations :errors => {:messages => {:broken => "is broken"}}
860
I18nPerson.validates_presence_of :name, :message => :broken
864
# Strings as class-level validation messages
866
test "String as class level validation message translated per attribute (translation on child class)" do
867
assert_name_invalid("is broken") do
868
store_translations :errors => {:models => {:i18n_person => {:attributes => {:name => {"is broken" => "is broken"}}}}}
869
I18nPerson.validates_presence_of :name, :message => "is broken"
873
test "String as class level validation message translated per attribute (translation on base class)" do
874
assert_name_invalid("is broken") do
875
store_translations :errors => {:models => {:person => {:attributes => {:name => {"is broken" => "is broken"}}}}}
876
I18nPerson.validates_presence_of :name, :message => "is broken"
880
test "String as class level validation message translated per model (translation on child class)" do
881
assert_name_invalid("is broken") do
882
store_translations :errors => {:models => {:i18n_person => {"is broken" => "is broken"}}}
883
I18nPerson.validates_presence_of :name, :message => "is broken"
887
test "String as class level validation message translated per model (translation on base class)" do
888
assert_name_invalid("is broken") do
889
store_translations :errors => {:models => {:person => {"is broken" => "is broken"}}}
890
I18nPerson.validates_presence_of :name, :message => "is broken"
894
test "String as class level validation message translated as error message" do
895
assert_name_invalid("is broken") do
896
store_translations :errors => {:messages => {"is broken" => "is broken"}}
897
I18nPerson.validates_presence_of :name, :message => "is broken"
901
test "String as class level validation message not translated (uses message as default)" do
902
assert_name_invalid("is broken!") do
903
I18nPerson.validates_presence_of :name, :message => "is broken!"
908
class ActiveRecordValidationsI18nFullMessagesFullStackTests < ActiveSupport::TestCase
909
include ActiveRecordValidationsI18nTestHelper
912
reset_callbacks(I18nPerson)
913
@old_backend, I18n.backend = I18n.backend, I18n::Backend::Simple.new
914
@person = I18nPerson.new
918
reset_callbacks(I18nPerson)
919
I18n.backend = @old_backend
922
def assert_full_message(message)
925
assert_equal message, @person.errors.full_messages.join
928
test "full_message format stored per custom error message key" do
929
assert_full_message("Name is broken!") do
930
store_translations :errors => { :messages => { :broken => 'is broken' }, :full_messages => { :broken => '{{attribute}} {{message}}!' } }
931
I18nPerson.validates_presence_of :name, :message => :broken
935
test "full_message format stored per error type" do
936
assert_full_message("Name can't be blank!") do
937
store_translations :errors => { :full_messages => { :blank => '{{attribute}} {{message}}!' } }
938
I18nPerson.validates_presence_of :name
941
# ActiveRecord#RecordInvalid exception
943
test "full_message format stored as default" do
944
assert_full_message("Name: can't be blank") do
945
store_translations :errors => { :full_messages => { :format => '{{attribute}}: {{message}}' } }
946
I18nPerson.validates_presence_of :name
949
test "RecordInvalid exception can be localized" do
951
topic.errors.add(:title, :invalid)
952
topic.errors.add(:title, :blank)
953
assert_equal "Validation failed: Title is invalid, Title can't be blank", ActiveRecord::RecordInvalid.new(topic).message