Commit 55909011c4634cea329d6fbd117e8f003c3c38f2

Authored by Leandro Santos
1 parent 781e93a4

adding more unit test for artice follower

lib/merit/badge_rules.rb
... ... @@ -65,6 +65,8 @@ module Merit
65 65 value: lambda { |friendship, person| person.friends.count }
66 66 }
67 67 ],
  68 +
  69 +#FIXME review the name of the badges and see a way to make it generic
68 70 creative: [
69 71 {
70 72 action: 'comment#create',
... ...
lib/merit/point_rules.rb
... ... @@ -114,12 +114,22 @@ module Merit
114 114 model: 'ArticleFollower',
115 115 condition: lambda {|follow, profile| profile.nil? or follow.article.profile == profile },
116 116 },
  117 + followed_article: {
  118 + action: 'articlefollower#create',
  119 + undo_action: 'articlefollower#destroy',
  120 + to: lambda {|follow| follow.article },
  121 + value: 1,
  122 + description: _('Article followed'),
  123 + default_weight: 30,
  124 + model: 'ArticleFollower',
  125 + condition: lambda {|follow, profile| profile.nil? or follow.article.profile == profile },
  126 + },
117 127 followed_article_author: {
118 128 action: 'articlefollower#create',
119 129 undo_action: 'articlefollower#destroy',
120 130 to: lambda {|follow| follow.article.author },
121 131 value: 1,
122   - description: _('Followed'),
  132 + description: _('Author of a followed content'),
123 133 default_weight: 20,
124 134 model: 'ArticleFollower',
125 135 condition: lambda {|follow, profile| profile.nil? or follow.article.profile == profile },
... ...
test/unit/article_follower.rb
... ... @@ -10,116 +10,76 @@ class ArticleTest < ActiveSupport::TestCase
10 10  
11 11 attr_accessor :person, :environment, :community
12 12  
  13 + should 'add merit points to an article follower by user' do
  14 + create_point_rule_definition('followed_article')
  15 + article = create(TextArticle, :name => 'Test', :profile => community, :author => person)
  16 +
  17 + c = GamificationPlugin::PointsCategorization.for_type(:followed_article).first
  18 + assert_difference 'article.points(:category => c.id.to_s)', c.weight do
  19 + article.person_followers << person
  20 + end
  21 + end
  22 +
13 23 should 'add merit points to follower when it follows an article' do
14 24 create_point_rule_definition('follower')
15   - article = create(TextArticle, :profile_id => community.id, :author => create_user('someuser').person)
16   - amount = person.score_points.count
17   - article.person_followers << person
18   - article.reload
19   - assert_equal amount + 1, person.score_points.count
20   - last_point = person.score_points.last
21   - assert_equal article.article_followers.last.id, last_point.action.target_id
  25 +
  26 + article = create(TextArticle, :name => 'Test', :profile => community, :author => person)
  27 +
  28 + c = GamificationPlugin::PointsCategorization.for_type(:follower).first
  29 + assert_difference 'person.points(:category => c.id.to_s)', c.weight do
  30 + article.person_followers << person
  31 + end
22 32 end
23 33  
24 34 should "add merit points for article's author followed by an user" do
25 35 create_point_rule_definition('followed_article_author')
26   - author = create_user('someuser').person
27   - article = create(TextArticle, :profile_id => community.id, :author => author)
28   - amount = author.score_points.count
29   - article.person_followers << person
30   - assert_equal amount + 1, author.score_points.count
31   - last_point = author.score_points.last
32   - assert_equal article.article_followers.last.id, last_point.action.target_id
  36 +
  37 + article = create(TextArticle, :name => 'Test', :profile => community, :author => person)
  38 +
  39 + c = GamificationPlugin::PointsCategorization.for_type(:followed_article_author).first
  40 + assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do
  41 + article.person_followers << person
  42 + end
33 43 end
34 44  
35   - should "add merit points for article's author and person who follow an article at the same time" do
  45 + should 'subtract merit points to follower when it unfollow an article' do
36 46 create_point_rule_definition('follower')
37   - create_point_rule_definition('followed_article_author')
38   - author = create_user('someuser').person
39   - article = create(TextArticle, :profile_id => community.id, :author => author)
  47 + follower = create_user('someuser').person
  48 + article = create(TextArticle, :profile_id => community.id, :author => person)
  49 + score_points = follower.score_points.count
  50 + points = follower.points
  51 + article.person_followers << follower
  52 + assert_equal score_points + 1, follower.score_points.count
  53 + ArticleFollower.last.destroy
  54 + assert_equal score_points + 2, follower.score_points.count
  55 + assert_equal points, follower.points
  56 + end
40 57  
41   - author_amount = author.score_points.count
42   - person_amount = person.score_points.count
  58 + should 'subtract merit points to article author when a user unfollow an article' do
  59 + create_point_rule_definition('follower')
  60 + article = create(TextArticle, :profile_id => community.id, :author => person)
  61 + score_points = person.score_points.count
  62 + points = person.points
43 63 article.person_followers << person
  64 + assert_equal score_points + 1, person.score_points.count
  65 + ArticleFollower.last.destroy
  66 + assert_equal score_points + 2, person.score_points.count
  67 + assert_equal points, person.points
  68 + end
44 69  
45   - assert_equal author_amount + 1, author.score_points.count
46   - last_point = author.score_points.last
47   - assert_equal article.article_followers.last.id, last_point.action.target_id
48   -
49   - assert_equal person_amount + 1, person.score_points.count
50   - last_point = person.score_points.last
51   - assert_equal article.article_followers.last.id, last_point.action.target_id
  70 + should 'subtract merit points to article when a user unfollow an article' do
  71 + create_point_rule_definition('followed_article')
  72 + article = create(TextArticle, :profile_id => community.id, :author => person)
  73 + score_points = article.score_points.count
  74 + points = article.points
  75 + article.person_followers << person
  76 + assert_equal score_points + 1, article.score_points.count
  77 + ArticleFollower.last.destroy
  78 + assert_equal score_points + 2, article.score_points.count
  79 + assert_equal points, article.points
52 80 end
53 81  
54   -# should 'subtract merit points to author when destroy an article' do
55   -# article = create(TextArticle, :profile_id => @community.id, :author => person)
56   -# assert_equal 1, person.score_points.count
57   -# article.destroy
58   -# assert_equal 2, person.score_points.count
59   -# assert_equal 0, person.points
60   -# end
61   -#
62   -# should 'add merit badge to author when create 5 new articles' do
63   -# GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 1)
64   -# GamificationPlugin.gamification_set_rules(environment)
65   -#
66   -# 5.times { create(TextArticle, :profile_id => person.id, :author => person) }
67   -# assert_equal 'article_author', person.badges.first.name
68   -# assert_equal 1, person.badges.first.level
69   -# end
70   -#
71   -# should 'add merit badge level 2 to author when create 10 new articles' do
72   -# GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 1)
73   -# GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 2, :custom_fields => {:threshold => 10})
74   -# GamificationPlugin.gamification_set_rules(environment)
75   -#
76   -# 10.times { create(TextArticle, :profile_id => person.id, :author => person) }
77   -# assert_equal ['article_author'], person.badges.map(&:name).uniq
78   -# assert_equal [1, 2], person.badges.map(&:level)
79   -# end
80   -#
81   -# should 'add merit points to community article owner when an user like it' do
82   -# article = create(TextArticle, :name => 'Test', :profile => @community, :author => person)
83   -#
84   -# c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable_author).where(profile_id: @community.id).first
85   -# assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do
86   -# Vote.create!(:voter => person, :voteable => article, :vote => 1)
87   -# end
88   -# end
89   -#
90   -# should 'add merit points to article when an user like it' do
91   -# article = create(TextArticle, :name => 'Test', :profile => @community, :author => person)
92   -# article = article.reload
93   -#
94   -# c = GamificationPlugin::PointsCategorization.for_type(:vote_voteable).where(profile_id: @community.id).first
95   -# assert_difference 'article.points(:category => c.id.to_s)', c.weight do
96   -# Vote.create!(:voter => person, :voteable => article, :vote => 1)
97   -# end
98   -# end
99   -#
100   -# should 'add merit points to community when create a new article' do
101   -# assert_difference 'community.score_points.count' do
102   -# create(TextArticle, :profile_id => @community.id, :author => person)
103   -# end
104   -# end
105   -#
106   -# should 'add merit points to voter when he likes an article' do
107   -# article = create(TextArticle, :name => 'Test', :profile => @community, :author => person)
108   -#
109   -# c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).where(profile_id: @community.id).first
110   -# assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do
111   -# Vote.create!(:voter => person, :voteable => article, :vote => 1)
112   -# end
113   -# end
114   -#
115   -# should 'add merit points to voter when he dislikes an article' do
116   -# article = create(TextArticle, :name => 'Test', :profile => @community, :author => person)
117   -#
118   -# c = GamificationPlugin::PointsCategorization.for_type(:vote_voter).where(profile_id: @community.id).first
119   -# assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do
120   -# Vote.create!(:voter => person, :voteable => article, :vote => -1)
121   -# end
122   -# end
  82 +#FIXME make tests for badges generete with article follower actions
123 83 #
124 84 # should 'add badge to author when users like his article' do
125 85 # GamificationPlugin::Badge.create!(:owner => environment, :name => 'positive_votes_received')
... ... @@ -145,4 +105,22 @@ class ArticleTest &lt; ActiveSupport::TestCase
145 105 # assert_equal 'negative_votes_received', person.reload.badges.first.name
146 106 # end
147 107 #
  108 +# should 'add merit badge to author when create 5 new articles' do
  109 +# GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 1)
  110 +# GamificationPlugin.gamification_set_rules(environment)
  111 +#
  112 +# 5.times { create(TextArticle, :profile_id => person.id, :author => person) }
  113 +# assert_equal 'article_author', person.badges.first.name
  114 +# assert_equal 1, person.badges.first.level
  115 +# end
  116 +#
  117 +# should 'add merit badge level 2 to author when create 10 new articles' do
  118 +# GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 1)
  119 +# GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 2, :custom_fields => {:threshold => 10})
  120 +# GamificationPlugin.gamification_set_rules(environment)
  121 +#
  122 +# 10.times { create(TextArticle, :profile_id => person.id, :author => person) }
  123 +# assert_equal ['article_author'], person.badges.map(&:name).uniq
  124 +# assert_equal [1, 2], person.badges.map(&:level)
  125 +# end
148 126 end
... ...