Commit 0f48036de78b00c85367bdfb023b95110d274857
1 parent
446eea30
Exists in
master
and in
28 other branches
ActionItem22: making tests pass
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1112 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
3 changed files
with
42 additions
and
1 deletions
Show diff stats
app/models/comment.rb
| @@ -2,4 +2,17 @@ class Comment < ActiveRecord::Base | @@ -2,4 +2,17 @@ class Comment < ActiveRecord::Base | ||
| 2 | validates_presence_of :title, :body | 2 | validates_presence_of :title, :body |
| 3 | belongs_to :article | 3 | belongs_to :article |
| 4 | belongs_to :author, :class_name => 'Person', :foreign_key => 'author_id' | 4 | belongs_to :author, :class_name => 'Person', :foreign_key => 'author_id' |
| 5 | + | ||
| 6 | + # unauthenticated authors: | ||
| 7 | + validates_presence_of :name, :if => (lambda { |record| !record.email.blank? }) | ||
| 8 | + validates_presence_of :email, :if => (lambda { |record| !record.name.blank? }) | ||
| 9 | + | ||
| 10 | + # require either a recognized author or an external person | ||
| 11 | + validates_presence_of :author_id, :if => (lambda { |rec| rec.name.blank? && rec.email.blank? }) | ||
| 12 | + validates_each :name do |rec,attribute,value| | ||
| 13 | + if rec.author_id && (!rec.name.blank? || !rec.email.blank?) | ||
| 14 | + rec.errors.add(:name, _('%{fn} can only be informed for unauthenticated authors')) | ||
| 15 | + end | ||
| 16 | + end | ||
| 17 | + | ||
| 5 | end | 18 | end |
test/test_helper.rb
| @@ -98,6 +98,12 @@ class Test::Unit::TestCase | @@ -98,6 +98,12 @@ class Test::Unit::TestCase | ||
| 98 | assert !object.errors.invalid?(attribute), "Attribute \"#{attribute.to_s}\" expected to accept value #{test_value.inspect}" | 98 | assert !object.errors.invalid?(attribute), "Attribute \"#{attribute.to_s}\" expected to accept value #{test_value.inspect}" |
| 99 | end | 99 | end |
| 100 | 100 | ||
| 101 | + def assert_optional(object, attribute) | ||
| 102 | + object.send("#{attribute}=", nil) | ||
| 103 | + object.valid? | ||
| 104 | + assert !object.errors.invalid?(attribute) | ||
| 105 | + end | ||
| 106 | + | ||
| 101 | private | 107 | private |
| 102 | 108 | ||
| 103 | def uses_host(name) | 109 | def uses_host(name) |
test/unit/comment_test.rb
| @@ -34,7 +34,29 @@ class CommentTest < Test::Unit::TestCase | @@ -34,7 +34,29 @@ class CommentTest < Test::Unit::TestCase | ||
| 34 | end | 34 | end |
| 35 | 35 | ||
| 36 | should 'record unauthenticated author' do | 36 | should 'record unauthenticated author' do |
| 37 | - flunk 'not yet' | 37 | + |
| 38 | + assert_optional Comment.new, :name | ||
| 39 | + assert_optional Comment.new, :email | ||
| 40 | + | ||
| 41 | + # if given name, require email | ||
| 42 | + c1 = Comment.new | ||
| 43 | + c1.name = 'My Name' | ||
| 44 | + assert_mandatory c1, :email | ||
| 45 | + | ||
| 46 | + # if given email, require name | ||
| 47 | + c2 = Comment.new | ||
| 48 | + c2.email = 'my@email.com' | ||
| 49 | + assert_mandatory c2, :name | ||
| 50 | + end | ||
| 51 | + | ||
| 52 | + should 'accept either an authenticated or unauthenticated author' do | ||
| 53 | + assert_mandatory Comment.new, :author_id | ||
| 54 | + | ||
| 55 | + c1 = Comment.new | ||
| 56 | + c1.author = create_user('someperson').person | ||
| 57 | + c1.name = 'my name' | ||
| 58 | + c1.valid? | ||
| 59 | + assert c1.errors.invalid?(:name) | ||
| 38 | end | 60 | end |
| 39 | 61 | ||
| 40 | end | 62 | end |