cirandas.net

ref: master

plugins/relevant_content/test/unit/article.rb


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
require_relative '../test_helper'

class RelevantContentBlockTest < ActiveSupport::TestCase

  include AuthenticatedTestHelper
  fixtures :users, :environments

  def setup
    @controller = CommentController.new

    @profile = create_user('testinguser').person
    @environment = @profile.environment
  end
  attr_reader :profile, :environment

  def enable_vote_plugin
    enabled = false
    environment = Environment.default
    if Noosfero::Plugin.all.include?('VotePlugin')
      if not environment.enabled_plugins.include?('VotePlugin')
        environment.enable_plugin(VotePlugin)
        environment.save!
      end
      enabled = true
    end
    enabled
  end

 should 'list most commented articles' do
    Article.delete_all
    a1 = create(TextArticle, :name => "art 1", :profile_id => profile.id)
    a2 = create(TextArticle, :name => "art 2", :profile_id => profile.id)
    a3 = create(TextArticle, :name => "art 3", :profile_id => profile.id)

    2.times { Comment.create(:title => 'test', :body => 'asdsad', :author => profile, :source => a2).save! }
    4.times { Comment.create(:title => 'test', :body => 'asdsad', :author => profile, :source => a3).save! }

    # should respect the order (more commented comes first)
    assert_equal a3.name, profile.articles.most_commented_relevant_content(Environment.default, 3).first.name
    # It is a2 instead of a1 since it does not list articles without comments
    assert_equal a2.name, profile.articles.most_commented_relevant_content(Environment.default, 3).last.name
  end


  should 'find the most voted' do
    if not enable_vote_plugin
      return
    end
    article = fast_create(Article, {:name=>'2 votes'})
    2.times{
      person = fast_create(Person)
      person.vote_for(article)
    }
    article = fast_create(Article, {:name=>'10 votes'})
    10.times{
        person = fast_create(Person)
        person.vote_for(article)
    }
    article = fast_create(Article, {:name=>'5 votes'})
    5.times{
        person = fast_create(Person)
        person.vote_for(article)
    }
    articles = Article.most_voted(Environment.default, 5)
    assert_equal '10 votes', articles.first.name
    assert_equal '2 votes', articles.last.name
  end

  should 'list the most postive' do
    if not enable_vote_plugin
      return
    end
    article = fast_create(Article, {:name=>'23 votes for 20 votes against'})
    20.times{
        person = fast_create(Person)
        person.vote_against(article)
    }
    23.times{
        person = fast_create(Person)
        person.vote_for(article)
    }
    article = fast_create(Article, {:name=>'10 votes for 5 votes against'})
    10.times{
        person = fast_create(Person)
        person.vote_for(article)
    }
    5.times{
        person = fast_create(Person)
        person.vote_against(article)
    }
    article = fast_create(Article, {:name=>'2 votes against'})
    2.times{
        person = fast_create(Person)
        person.vote_against(article)
    }

    article = fast_create(Article, {:name=>'7 votes for'})
    7.times{
        person = fast_create(Person)
        person.vote_for(article)
    }
    articles = Article.more_positive_votes(Environment.default, 5)
    assert_equal '7 votes for', articles.first.name
    assert_equal '23 votes for 20 votes against', articles.last.name
  end

  should 'list the most negative' do
    if not enable_vote_plugin
      return
    end
    article = fast_create(Article, {:name=>'23 votes for 29 votes against'})
    29.times{
        person = fast_create(Person)
        person.vote_against(article)
    }
    23.times{
        person = fast_create(Person)
        person.vote_for(article)
    }
    article = fast_create(Article, {:name=>'10 votes for 15 votes against'})
    10.times{
        person = fast_create(Person)
        person.vote_for(article)
    }
    15.times{
        person = fast_create(Person)
        person.vote_against(article)
    }
    article = fast_create(Article, {:name=>'2 votes against'})
    2.times{
        person = fast_create(Person)
        person.vote_against(article)
    }
    article = fast_create(Article, {:name=>'7 votes for'})
    7.times{
        person = fast_create(Person)
        person.vote_for(article)
    }
    articles = Article.more_negative_votes(Environment.default, 5)
    assert_equal '23 votes for 29 votes against', articles.first.name
    assert_equal '2 votes against', articles.last.name
  end
end