article_follower_test.rb
5.97 KB
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
144
145
146
147
148
149
150
151
152
require_relative "../test_helper"
class ArticleFollowerTest < ActiveSupport::TestCase
def setup
@person = create_user('testuser').person
@environment = Environment.default
@community = fast_create(Community)
end
attr_accessor :person, :environment, :community
should 'add merit points to an article follower by user' do
create_point_rule_definition('followed_article')
article = create(TextArticle, :name => 'Test', :profile => community, :author => person)
process_delayed_job_queue
c = GamificationPlugin::PointsCategorization.for_type(:followed_article).first
assert_difference 'article.points(:category => c.id.to_s)', c.weight do
article.person_followers << person
process_delayed_job_queue
end
end
should 'add merit points to follower when it follows an article' do
create_point_rule_definition('follower')
article = create(TextArticle, :name => 'Test', :profile => community, :author => person)
process_delayed_job_queue
c = GamificationPlugin::PointsCategorization.for_type(:follower).first
assert_difference 'person.points(:category => c.id.to_s)', c.weight do
article.person_followers << person
process_delayed_job_queue
end
end
should "add merit points for article's author followed by an user" do
create_point_rule_definition('followed_article_author')
article = create(TextArticle, :name => 'Test', :profile => community, :author => person)
process_delayed_job_queue
c = GamificationPlugin::PointsCategorization.for_type(:followed_article_author).first
assert_difference 'article.author.points(:category => c.id.to_s)', c.weight do
article.person_followers << person
process_delayed_job_queue
end
end
should 'subtract merit points to follower when it unfollow an article' do
create_point_rule_definition('follower')
follower = create_user('someuser').person
article = create(TextArticle, :profile_id => community.id, :author => person)
process_delayed_job_queue
score_points = follower.score_points.count
points = follower.points
article.person_followers << follower
process_delayed_job_queue
assert_equal score_points + 1, follower.score_points.count
ArticleFollower.last.destroy
process_delayed_job_queue
assert_equal score_points + 2, follower.score_points.count
assert_equal points, follower.points
end
should 'subtract merit points to article author when a user unfollow an article' do
create_point_rule_definition('follower')
article = create(TextArticle, :profile_id => community.id, :author => person)
process_delayed_job_queue
assert_no_difference 'person.points' do
assert_difference 'person.score_points.count' do
article.person_followers << fast_create(Person)
process_delayed_job_queue
end
assert_difference 'person.score_points.count' do
ArticleFollower.last.destroy
end
end
end
should 'subtract merit points to article when a user unfollow an article' do
create_point_rule_definition('followed_article')
article = create(TextArticle, :profile_id => community.id, :author => person)
process_delayed_job_queue
score_points = article.score_points.count
points = article.points
article.person_followers << person
process_delayed_job_queue
assert_equal score_points + 1, article.score_points.count
ArticleFollower.last.destroy
process_delayed_job_queue
assert_equal score_points + 2, article.score_points.count
assert_equal points, article.points
end
#FIXME make tests for badges generete with article follower actions
#
# should 'add badge to author when users like his article' do
# GamificationPlugin::Badge.create!(:owner => environment, :name => 'positive_votes_received')
# GamificationPlugin.gamification_set_rules(environment)
#
# article = create(TextArticle, :name => 'Test', :profile => person, :author => person)
# 4.times { Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => 1) }
# Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => -1)
# assert_equal [], person.badges
# Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => 1)
# assert_equal 'positive_votes_received', person.reload.badges.first.name
# end
#
# should 'add badge to author when users dislike his article' do
# GamificationPlugin::Badge.create!(:owner => environment, :name => 'negative_votes_received')
# GamificationPlugin.gamification_set_rules(environment)
#
# article = create(TextArticle, :name => 'Test', :profile => person, :author => person)
# 4.times { Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => -1) }
# Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => 1)
# assert_equal [], person.badges
# Vote.create!(:voter => fast_create(Person), :voteable => article, :vote => -1)
# assert_equal 'negative_votes_received', person.reload.badges.first.name
# end
#
# should 'add merit badge to author when create 5 new articles' do
# GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 1)
# GamificationPlugin.gamification_set_rules(environment)
#
# 5.times { create(TextArticle, :profile_id => person.id, :author => person) }
# assert_equal 'article_author', person.badges.first.name
# assert_equal 1, person.badges.first.level
# end
#
# should 'add merit badge level 2 to author when create 10 new articles' do
# GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 1)
# GamificationPlugin::Badge.create!(:owner => environment, :name => 'article_author', :level => 2, :custom_fields => {:threshold => 10})
# GamificationPlugin.gamification_set_rules(environment)
#
# 10.times { create(TextArticle, :profile_id => person.id, :author => person) }
# assert_equal ['article_author'], person.badges.map(&:name).uniq
# assert_equal [1, 2], person.badges.map(&:level)
# end
end