2016-08-06 12:38:23 -04:00
|
|
|
require "cases/helper"
|
2009-03-20 11:07:49 -04:00
|
|
|
|
2016-08-06 12:38:23 -04:00
|
|
|
require "models/topic"
|
|
|
|
require "models/person"
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
class ConfirmationValidationTest < ActiveModel::TestCase
|
2009-12-23 07:30:58 -05:00
|
|
|
def teardown
|
2014-01-27 05:22:26 -05:00
|
|
|
Topic.clear_validators!
|
2009-12-23 07:30:58 -05:00
|
|
|
end
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
def test_no_title_confirmation
|
|
|
|
Topic.validates_confirmation_of(:title)
|
|
|
|
|
2013-05-01 20:10:06 -04:00
|
|
|
t = Topic.new(author_name: "Plutarch")
|
2009-03-20 11:07:49 -04:00
|
|
|
assert t.valid?
|
|
|
|
|
|
|
|
t.title_confirmation = "Parallel Lives"
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.invalid?
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
t.title_confirmation = nil
|
|
|
|
t.title = "Parallel Lives"
|
|
|
|
assert t.valid?
|
|
|
|
|
|
|
|
t.title_confirmation = "Parallel Lives"
|
|
|
|
assert t.valid?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_title_confirmation
|
|
|
|
Topic.validates_confirmation_of(:title)
|
|
|
|
|
2016-10-28 23:05:58 -04:00
|
|
|
t = Topic.new("title" => "We should be confirmed", "title_confirmation" => "")
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.invalid?
|
2009-03-20 11:07:49 -04:00
|
|
|
|
|
|
|
t.title_confirmation = "We should be confirmed"
|
2010-05-08 16:27:49 -04:00
|
|
|
assert t.valid?
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|
|
|
|
|
2009-03-20 18:21:27 -04:00
|
|
|
def test_validates_confirmation_of_for_ruby_class
|
2009-12-23 07:30:58 -05:00
|
|
|
Person.validates_confirmation_of :karma
|
2009-03-20 18:21:27 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
p = Person.new
|
|
|
|
p.karma_confirmation = "None"
|
|
|
|
assert p.invalid?
|
2009-03-20 18:21:27 -04:00
|
|
|
|
2012-04-24 01:05:41 -04:00
|
|
|
assert_equal ["doesn't match Karma"], p.errors[:karma_confirmation]
|
2009-03-20 18:21:27 -04:00
|
|
|
|
2009-12-23 07:30:58 -05:00
|
|
|
p.karma = "None"
|
|
|
|
assert p.valid?
|
|
|
|
ensure
|
2014-01-27 05:22:26 -05:00
|
|
|
Person.clear_validators!
|
2009-03-20 18:21:27 -04:00
|
|
|
end
|
|
|
|
|
2012-04-24 01:05:41 -04:00
|
|
|
def test_title_confirmation_with_i18n_attribute
|
2014-03-10 09:22:22 -04:00
|
|
|
begin
|
|
|
|
@old_load_path, @old_backend = I18n.load_path.dup, I18n.backend
|
|
|
|
I18n.load_path.clear
|
|
|
|
I18n.backend = I18n::Backend::Simple.new
|
2016-08-09 17:36:39 -04:00
|
|
|
I18n.backend.store_translations("en",
|
|
|
|
errors: { messages: { confirmation: "doesn't match %{attribute}" } },
|
2016-08-16 03:30:11 -04:00
|
|
|
activemodel: { attributes: { topic: { title: "Test Title" } } })
|
2012-04-24 01:05:41 -04:00
|
|
|
|
2014-03-10 09:22:22 -04:00
|
|
|
Topic.validates_confirmation_of(:title)
|
2012-04-24 01:05:41 -04:00
|
|
|
|
2016-10-28 23:05:58 -04:00
|
|
|
t = Topic.new("title" => "We should be confirmed", "title_confirmation" => "")
|
2014-03-07 12:59:57 -05:00
|
|
|
assert t.invalid?
|
|
|
|
assert_equal ["doesn't match Test Title"], t.errors[:title_confirmation]
|
|
|
|
ensure
|
|
|
|
I18n.load_path.replace @old_load_path
|
|
|
|
I18n.backend = @old_backend
|
|
|
|
I18n.backend.reload!
|
|
|
|
end
|
2012-04-24 01:05:41 -04:00
|
|
|
end
|
|
|
|
|
2013-03-04 12:50:56 -05:00
|
|
|
test "does not override confirmation reader if present" do
|
|
|
|
klass = Class.new do
|
|
|
|
include ActiveModel::Validations
|
|
|
|
|
|
|
|
def title_confirmation
|
|
|
|
"expected title"
|
|
|
|
end
|
|
|
|
|
|
|
|
validates_confirmation_of :title
|
|
|
|
end
|
|
|
|
|
|
|
|
assert_equal "expected title", klass.new.title_confirmation,
|
|
|
|
"confirmation validation should not override the reader"
|
|
|
|
end
|
|
|
|
|
|
|
|
test "does not override confirmation writer if present" do
|
|
|
|
klass = Class.new do
|
|
|
|
include ActiveModel::Validations
|
|
|
|
|
|
|
|
def title_confirmation=(value)
|
|
|
|
@title_confirmation = "expected title"
|
|
|
|
end
|
|
|
|
|
|
|
|
validates_confirmation_of :title
|
|
|
|
end
|
|
|
|
|
|
|
|
model = klass.new
|
|
|
|
model.title_confirmation = "new title"
|
|
|
|
assert_equal "expected title", model.title_confirmation,
|
|
|
|
"confirmation validation should not override the writer"
|
|
|
|
end
|
2015-09-01 08:05:50 -04:00
|
|
|
|
|
|
|
def test_title_confirmation_with_case_sensitive_option_true
|
|
|
|
Topic.validates_confirmation_of(:title, case_sensitive: true)
|
|
|
|
|
|
|
|
t = Topic.new(title: "title", title_confirmation: "Title")
|
|
|
|
assert t.invalid?
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_title_confirmation_with_case_sensitive_option_false
|
|
|
|
Topic.validates_confirmation_of(:title, case_sensitive: false)
|
|
|
|
|
|
|
|
t = Topic.new(title: "title", title_confirmation: "Title")
|
|
|
|
assert t.valid?
|
|
|
|
end
|
2009-03-20 11:07:49 -04:00
|
|
|
end
|