require_relative "../test_helper" class CommentTest < ActiveSupport::TestCase def setup end should 'have a name but not require it' do assert_optional(Comment.new, :title) end should 'have a body and require it' do assert_mandatory(Comment.new, :body) end should 'have a polymorphic relationship with source' do c = Comment.new assert_nothing_raised do c.source = Article.new end assert_nothing_raised do c.source = ActionTracker::Record.new end end should 'record authenticated author' do c = Comment.new assert_raise ActiveRecord::AssociationTypeMismatch do c.author = 1 end assert_raise ActiveRecord::AssociationTypeMismatch do c.author = Profile end assert_nothing_raised do c.author = Person.new end end should 'record unauthenticated author' do assert_optional Comment.new, :name assert_optional Comment.new, :email # if given name, require email c1 = Comment.new c1.name = 'My Name' assert_mandatory c1, :email, 'my@email.com' # if given email, require name c2 = Comment.new c2.email = 'my@email.com' assert_mandatory c2, :name end should 'accept either an authenticated or unauthenticated author' do assert_mandatory Comment.new, :author_id c1 = Comment.new c1.author = create_user('someperson').person c1.name = 'my name' c1.valid? assert c1.errors[:name.to_s].present? assert_no_match /\{fn\}/, c1.errors[:name].first end should 'update counter cache in article' do owner = create_user('testuser').person art = create(TextileArticle, :profile_id => owner.id) cc = art.comments_count comment = create(Comment, :source => art, :author_id => owner.id) assert_equal cc + 1, Article.find(art.id).comments_count end should 'update counter cache in article activity' do owner = create_user('testuser').person article = create(TextileArticle, :profile_id => owner.id) action = article.activity cc = action.comments_count comment = create(Comment, :source => action, :author_id => owner.id) assert_equal cc + 1, ActionTracker::Record.find(action.id).comments_count end should 'update counter cache in general activity when add a comment' do person = fast_create(Person) community = fast_create(Community) activity = create ActionTracker::Record, :user => person, :target => community, :verb => 'add_member_in_community' cc = activity.comments_count comment = create(Comment, :source => activity, :author_id => person.id) assert_equal cc + 1, ActionTracker::Record.find(activity.id).comments_count end should 'provide author name for authenticated authors' do owner = create_user('testuser').person assert_equal 'testuser', build(Comment, :author => owner).author_name end should 'provide author name for unauthenticated author' do assert_equal 'anonymous coward', build(Comment, :name => 'anonymous coward').author_name end should 'provide empty text for author name if user was removed ' do assert_equal '', build(Comment, :author_id => 9999).author_name end should "provide author e-mail for athenticated authors" do owner = create_user('testuser').person assert_equal owner.email, build(Comment, :author => owner).author_email end should "provide author e-mail for unauthenticated author" do assert_equal 'my@email.com', build(Comment, :email => 'my@email.com').author_email end should 'provide author link for authenticated author' do author = Person.new author.expects(:url).returns('http://blabla.net/author') assert_equal 'http://blabla.net/author', build(Comment, :author => author).author_link end should 'provide author e-mail as author link for unauthenticated author' do assert_equal 'my@email.com', build(Comment, :email => 'my@email.com').author_link end should 'provide url to comment' do art = Article.new art.expects(:url).returns({ :controller => 'lala', :action => 'something' }) comment = build(Comment, :article => art) comment.expects(:id).returns(9876) assert_equal({ :controller => 'lala', :action => 'something', :anchor => 'comment-9876'}, comment.url) end should 'provide anchor' do comment = Comment.new comment.expects(:id).returns(4321) assert_equal 'comment-4321', comment.anchor end should 'be able to find recent comments' do Comment.delete_all owner = create_user('testuser').person art = owner.articles.build(:name => 'ytest'); art.save! comments = [] 3.times do comments.unshift create(Comment, :article => art, :title => 'a test comment', :body => 'bla', :author => owner) end assert_equal comments, Comment.recent end should 'be able to find recent comments with limit' do Comment.delete_all owner = create_user('testuser').person art = owner.articles.build(:name => 'ytest'); art.save! comments = [] 3.times do comments.unshift create(Comment, :article => art, :title => 'a test comment', :body => 'bla', :author => owner) end comments.pop assert_equal comments, Comment.recent(2) end should 'not accept invalid email' do c = build(Comment, :name => 'My Name', :email => 'my@invalid') c.valid? assert c.errors[:email.to_s].present? end should 'generate links to comments on images with view set to true' do owner = create_user('testuser').person image = create(UploadedFile, :profile => owner, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png')) comment = create(Comment, :article => image, :author => owner, :title => 'a random comment', :body => 'just another comment') assert comment.url[:view] end should 'not fill fields with javascript' do owner = create_user('testuser').person article = owner.articles.create!(:name => 'test', :body => '...') javascript = "" comment = create(Comment, :article => article, :name => javascript, :title => javascript, :body => javascript, :email => 'cracker@test.org') assert_no_match(/