community_test.rb
13.1 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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
require File.dirname(__FILE__) + '/../test_helper'
class CommunityTest < ActiveSupport::TestCase
def setup
@person = fast_create(Person)
end
attr_reader :person
should 'inherit from Profile' do
assert_kind_of Profile, Community.new
end
should 'convert name into identifier' do
c = Community.new(:environment => Environment.default, :name =>'My shiny new Community')
assert_equal 'My shiny new Community', c.name
assert_equal 'my-shiny-new-community', c.identifier
end
should 'have a description attribute' do
c = Community.new(:environment => Environment.default)
c.description = 'the description of the community'
assert_equal 'the description of the community', c.description
end
should 'create default set of blocks' do
c = Community.create!(:environment => Environment.default, :name => 'my new community')
assert !c.boxes[0].blocks.empty?, 'person must have blocks in area 1'
assert !c.boxes[1].blocks.empty?, 'person must have blocks in area 2'
assert !c.boxes[2].blocks.empty?, 'person must have blocks in area 3'
end
should 'create a default set of articles' do
Community.any_instance.stubs(:default_set_of_articles).returns([Blog.new(:name => 'blog')])
community = Community.create!(:environment => Environment.default, :name => 'my new community')
assert_kind_of Blog, community.articles.find_by_path('blog')
assert_kind_of RssFeed, community.articles.find_by_path('blog/feed')
end
should 'have contact_person' do
community = Community.new(:environment => Environment.default, :name => 'my new community')
assert_respond_to community, :contact_person
end
should 'allow to add new members' do
c = fast_create(Community, :name => 'my test profile', :identifier => 'mytestprofile')
p = create_user('mytestuser').person
c.add_member(p)
assert c.members.include?(p), "Community should add the new member"
end
should 'allow to remove members' do
c = fast_create(Community, :name => 'my other test profile', :identifier => 'myothertestprofile')
p = create_user('myothertestuser').person
c.add_member(p)
assert_includes c.members, p
c.remove_member(p)
c.reload
assert_not_includes c.members, p
end
should 'clear relationships after destroy' do
c = fast_create(Community, :name => 'my test profile', :identifier => 'mytestprofile')
member = create_user('memberuser').person
admin = create_user('adminuser').person
moderator = create_user('moderatoruser').person
c.add_member(member)
c.add_admin(admin)
c.add_moderator(moderator)
relationships = c.role_assignments
assert_not_nil relationships
c.destroy
relationships.each do |i|
assert !RoleAssignment.exists?(i.id)
end
end
should 'have a community template' do
template = fast_create(Community, :is_template => true)
p = Community.create!(:name => 'test_com', :identifier => 'test_com', :template => template)
assert_equal template, p.template
end
should 'have a default community template' do
env = Environment.create!(:name => 'test env')
p = Community.create!(:name => 'test_com', :identifier => 'test_com', :environment => env)
assert_kind_of Community, p.template
end
should 'return active_community_fields' do
e = Environment.default
e.expects(:active_community_fields).returns(['contact_phone', 'contact_email']).at_least_once
ent = Community.new(:environment => e)
assert_equal e.active_community_fields, ent.active_fields
end
should 'return required_community_fields' do
e = Environment.default
e.expects(:required_community_fields).returns(['contact_phone', 'contact_email']).at_least_once
community = Community.new(:environment => e)
assert_equal e.required_community_fields, community.required_fields
end
should 'require fields if community needs' do
e = Environment.default
e.expects(:required_community_fields).returns(['contact_phone']).at_least_once
community = Community.new(:name => 'My community', :environment => e)
assert ! community.valid?
assert community.errors.invalid?(:contact_phone)
community.contact_phone = '99999'
community.valid?
assert ! community.errors.invalid?(:contact_phone)
end
should 'return newest text articles as news' do
c = fast_create(Community, :name => 'test_com')
f = fast_create(Folder, :name => 'folder', :profile_id => c.id)
u = UploadedFile.create!(:profile => c, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
older_t = fast_create(TinyMceArticle, :name => 'old news', :profile_id => c.id)
t = fast_create(TinyMceArticle, :name => 'news', :profile_id => c.id)
t_in_f = fast_create(TinyMceArticle, :name => 'news', :profile_id => c.id, :parent_id => f.id)
assert_equal [t_in_f, t], c.news(2)
end
should 'not return highlighted news when not asked' do
c = fast_create(Community, :name => 'test_com')
highlighted_t = fast_create(TinyMceArticle, :name => 'high news', :profile_id => c.id, :highlighted => true)
t = fast_create(TinyMceArticle, :name => 'news', :profile_id => c.id)
assert_equal [t].map(&:slug), c.news(2).map(&:slug)
end
should 'return highlighted news when asked' do
c = fast_create(Community, :name => 'test_com')
highlighted_t = fast_create(TinyMceArticle, :name => 'high news', :profile_id => c.id, :highlighted => true)
t = fast_create(TinyMceArticle, :name => 'news', :profile_id => c.id)
assert_equal [highlighted_t].map(&:slug), c.news(2, true).map(&:slug)
end
should 'sanitize description' do
c = Community.create!(:name => 'test_com', :description => '<b>new</b> community')
assert_sanitized c.description
end
should 'sanitize name' do
c = Community.create!(:name => '<b>test_com</b>')
assert_sanitized c.name
end
should 'create a task when creating a community if feature is enabled' do
env = Environment.default
env.enable('admin_must_approve_new_communities')
assert_difference CreateCommunity, :count do
Community.create_after_moderation(person, {:environment => env, :name => 'Example'})
end
assert_no_difference Community, :count do
Community.create_after_moderation(person, {:environment => env, :name => 'Example'})
end
end
should 'create a community if feature is disabled' do
env = Environment.default
env.disable('admin_must_approve_new_communities')
assert_difference Community, :count do
Community.create_after_moderation(person, {:environment => env, :name => 'Example'})
end
assert_no_difference CreateCommunity, :count do
Community.create_after_moderation(person, {:environment => env, :name => 'Example'})
end
end
should 'set as member without task if organization is closed and has no members' do
community = fast_create(Community)
community.closed = true
community.save
assert_no_difference AddMember, :count do
community.add_member(person)
end
assert person.is_member_of?(community)
end
should 'set as member without task if organization is not closed and has no members' do
community = fast_create(Community)
assert_no_difference AddMember, :count do
community.add_member(person)
end
assert person.is_member_of?(community)
end
should 'not create new request membership if it already exists' do
community = fast_create(Community)
community.closed = true
community.save
community.add_member(fast_create(Person))
community.reload
assert_difference AddMember, :count do
community.add_member(person)
end
assert_no_difference AddMember, :count do
community.add_member(person)
end
end
should 'escape malformed html tags' do
community = Community.new
community.name = "<h1 Malformed >> html >< tag"
community.address = "<h1 Malformed >,<<<asfdf> html >< tag"
community.contact_phone = "<h1 Malformed<<> >> html >><>< tag"
community.description = "<h1 Malformed /h1>>><<> html ><>h1< tag"
community.valid?
assert_no_match /[<>]/, community.name
assert_no_match /[<>]/, community.address
assert_no_match /[<>]/, community.contact_phone
assert_no_match /[<>]/, community.description
end
should "the followed_by method be protected and true to the community members by default" do
c = fast_create(Community)
p1 = fast_create(Person)
p2 = fast_create(Person)
p3 = fast_create(Person)
assert !p1.is_member_of?(c)
c.add_member(p1)
assert p1.is_member_of?(c)
assert !p3.is_member_of?(c)
c.add_member(p3)
assert p3.is_member_of?(c)
assert_equal true, c.send(:followed_by?,p1)
assert_equal true, c.send(:followed_by?,p3)
assert_equal false, c.send(:followed_by?,p2)
end
should "be created an tracked action when the user is join to the community" do
p1 = Person.first
community = fast_create(Community)
p2 = fast_create(Person)
p3 = fast_create(Person)
RoleAssignment.delete_all
ActionTrackerNotification.delete_all
assert_difference(ActionTrackerNotification, :count, 5) do
community.add_member(p1)
process_delayed_job_queue
community.add_member(p3)
assert p1.is_member_of?(community)
assert !p2.is_member_of?(community)
assert p3.is_member_of?(community)
process_delayed_job_queue
end
ActionTrackerNotification.all.map{|a|a.profile}.map do |profile|
assert [community,p1,p3].include?(profile)
end
end
should "update the action of article creation when an community's article is commented" do
ActionTrackerNotification.delete_all
p1 = Person.first
community = fast_create(Community)
p2 = fast_create(Person)
p3 = fast_create(Person)
community.add_member(p3)
article = create(TextileArticle, :profile_id => community.id)
time = article.activity.updated_at
Time.stubs(:now).returns(time + 1.day)
Comment.create!(:source_id => article.id, :title => 'some', :body => 'some', :author_id => p2.id)
process_delayed_job_queue
assert_equal time, article.activity.updated_at
end
should "see get all received scraps" do
c = fast_create(Community)
assert_equal [], c.scraps_received
fast_create(Scrap, :receiver_id => c.id)
fast_create(Scrap, :receiver_id => c.id)
assert_equal 2, c.scraps_received.count
c2 = fast_create(Community)
fast_create(Scrap, :receiver_id => c2.id)
assert_equal 2, c.scraps_received.count
fast_create(Scrap, :receiver_id => c.id)
assert_equal 3, c.scraps_received.count
end
should "see get all received scraps that are not replies" do
c = fast_create(Community)
s1 = fast_create(Scrap, :receiver_id => c.id)
s2 = fast_create(Scrap, :receiver_id => c.id)
s3 = fast_create(Scrap, :receiver_id => c.id, :scrap_id => s1.id)
assert_equal 3, c.scraps_received.count
assert_equal [s1,s2], c.scraps_received.not_replies
c2 = fast_create(Community)
s4 = fast_create(Scrap, :receiver_id => c2.id)
s5 = fast_create(Scrap, :receiver_id => c2.id, :scrap_id => s4.id)
assert_equal 2, c2.scraps_received.count
assert_equal [s4], c2.scraps_received.not_replies
end
should "the community browse for a scrap with a Scrap object" do
c = fast_create(Community)
s1 = fast_create(Scrap, :receiver_id => c.id)
s2 = fast_create(Scrap, :receiver_id => c.id)
s3 = fast_create(Scrap, :receiver_id => c.id)
assert_equal s2, c.scraps(s2)
end
should "the person browse for a scrap with an integer and string id" do
c = fast_create(Community)
s1 = fast_create(Scrap, :receiver_id => c.id)
s2 = fast_create(Scrap, :receiver_id => c.id)
s3 = fast_create(Scrap, :receiver_id => c.id)
assert_equal s2, c.scraps(s2.id)
assert_equal s2, c.scraps(s2.id.to_s)
end
should 'receive scrap notification' do
community = fast_create(Community)
assert_equal false, community.receives_scrap_notification?
end
should 'return scraps as activities' do
person = fast_create(Person)
community = fast_create(Community)
scrap = Scrap.create!(defaults_for_scrap(:sender => person, :receiver => community, :content => 'A scrap'))
activity = ActionTracker::Record.last
assert_equal [scrap], community.activities.map { |a| a.klass.constantize.find(a.id) }
end
should 'return tracked_actions of community as activities' do
person = fast_create(Person)
community = fast_create(Community)
UserStampSweeper.any_instance.expects(:current_user).returns(person).at_least_once
article = create(TinyMceArticle, :profile => community, :name => 'An article about free software')
assert_equal [article.activity], community.activities.map { |a| a.klass.constantize.find(a.id) }
end
should 'not return tracked_actions of other community as activities' do
person = fast_create(Person)
community = fast_create(Community)
community2 = fast_create(Community)
UserStampSweeper.any_instance.expects(:current_user).returns(person).at_least_once
article = create(TinyMceArticle, :profile => community2, :name => 'Another article about free software')
assert_not_includes community.activities.map { |a| a.klass.constantize.find(a.id) }, article.activity
end
end