confirmation_validation_test.rb 2.6 KB
Newer Older
1 2 3 4
# encoding: utf-8
require 'cases/helper'

require 'models/topic'
5
require 'models/person'
6 7 8

class ConfirmationValidationTest < ActiveModel::TestCase

9
  def teardown
10
    Topic.clear_validators!
11
  end
12 13 14 15

  def test_no_title_confirmation
    Topic.validates_confirmation_of(:title)

16
    t = Topic.new(author_name: "Plutarch")
17 18 19
    assert t.valid?

    t.title_confirmation = "Parallel Lives"
20
    assert t.invalid?
21 22 23 24 25 26 27 28 29 30 31 32

    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)

33 34
    t = Topic.new("title" => "We should be confirmed","title_confirmation" => "")
    assert t.invalid?
35 36

    t.title_confirmation = "We should be confirmed"
37
    assert t.valid?
38 39
  end

40
  def test_validates_confirmation_of_for_ruby_class
41
    Person.validates_confirmation_of :karma
42

43 44 45
    p = Person.new
    p.karma_confirmation = "None"
    assert p.invalid?
46

47
    assert_equal ["doesn't match Karma"], p.errors[:karma_confirmation]
48

49 50 51
    p.karma = "None"
    assert p.valid?
  ensure
52
    Person.clear_validators!
53 54
  end

55 56 57 58 59
  def test_title_confirmation_with_i18n_attribute
    @old_load_path, @old_backend = I18n.load_path.dup, I18n.backend
    I18n.load_path.clear
    I18n.backend = I18n::Backend::Simple.new
    I18n.backend.store_translations('en', {
60 61
      errors: { messages: { confirmation: "doesn't match %{attribute}" } },
      activemodel: { attributes: { topic: { title: 'Test Title'} } }
62 63 64 65 66 67 68 69 70 71 72 73
    })

    Topic.validates_confirmation_of(:title)

    t = Topic.new("title" => "We should be confirmed","title_confirmation" => "")
    assert t.invalid?
    assert_equal ["doesn't match Test Title"], t.errors[:title_confirmation]

    I18n.load_path.replace @old_load_path
    I18n.backend = @old_backend
  end

74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
  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
105
end