communities_block_test.rb
6 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
require File.dirname(__FILE__) + '/../test_helper'
class CommunitiesBlockTest < Test::Unit::TestCase
  should 'inherit from ProfileListBlock' do
    assert_kind_of ProfileListBlock, CommunitiesBlock.new
  end
  should 'declare its default title' do
    CommunitiesBlock.any_instance.stubs(:profile_count).returns(0)
    assert_not_equal ProfileListBlock.new.default_title, CommunitiesBlock.new.default_title
  end
  should 'describe itself' do
    assert_not_equal ProfileListBlock.description, CommunitiesBlock.description
  end
  should 'use its own finder' do
    assert_not_equal CommunitiesBlock::Finder, ProfileListBlock::Finder
    assert_kind_of CommunitiesBlock::Finder, CommunitiesBlock.new.profile_finder
  end
  should 'list owner communities' do
    block = CommunitiesBlock.new
    block.limit = 2
    owner = mock
    block.expects(:owner).at_least_once.returns(owner)
    community1 = mock; community1.stubs(:id).returns(1); community1.stubs(:visible).returns(true)
    community2 = mock; community2.stubs(:id).returns(2); community2.stubs(:visible).returns(true)
    community3 = mock; community3.stubs(:id).returns(3); community3.stubs(:visible).returns(true)
    owner.expects(:communities).returns([community1, community2, community3])
    
    block.profile_finder.expects(:pick_random).with(3).returns(2)
    block.profile_finder.expects(:pick_random).with(2).returns(0)
    Profile.expects(:find).with(3).returns(community3)
    Profile.expects(:find).with(1).returns(community1)
    assert_equal [community3, community1], block.profiles
  end
  should 'link to all communities of profile' do
    profile = Profile.new
    profile.expects(:identifier).returns("theprofile")
    block = CommunitiesBlock.new
    block.expects(:owner).returns(profile)
    expects(:link_to).with('View all', :controller => 'profile', :profile => 'theprofile', :action => 'communities')
    instance_eval(&block.footer)
  end
  should 'support environment as owner' do
    env = Environment.default
    block = CommunitiesBlock.new
    block.expects(:owner).returns(env)
    expects(:link_to).with('View all', :controller => 'search', :action => 'assets', :asset => 'communities')
    instance_eval(&block.footer)
  end
  should 'give empty footer on unsupported owner type' do
    block = CommunitiesBlock.new
    block.expects(:owner).returns(1)
    assert_equal '', block.footer
  end
  should 'list non-public communities' do
    user = create_user('testuser').person
    public_community = fast_create(Community, :environment_id => Environment.default.id)
    public_community.add_member(user)
    private_community = fast_create(Community, :environment_id => Environment.default.id, :public_profile => false)
    private_community.add_member(user)
    block = CommunitiesBlock.new
    block.expects(:owner).at_least_once.returns(user)
    assert_equivalent [public_community, private_community], block.profiles
  end
  should 'not list non-visible communities' do
    user = create_user('testuser').person
    visible_community = fast_create(Community, :environment_id => Environment.default.id)
    visible_community.add_member(user)
    not_visible_community = fast_create(Community, :environment_id => Environment.default.id, :visible => false)
    not_visible_community.add_member(user)
    block = CommunitiesBlock.new
    block.expects(:owner).at_least_once.returns(user)
    assert_equal [visible_community], block.profiles
  end
  should 'count number of owner communities' do
    user = create_user('testuser').person
    community1 = fast_create(Community, :environment_id => Environment.default.id, :visible => true)
    community1.add_member(user)
    community2 = fast_create(Community, :environment_id => Environment.default.id, :visible => true)
    community2.add_member(user)
    block = CommunitiesBlock.new
    block.expects(:owner).at_least_once.returns(user)
    assert_equal 2, block.profile_count
  end
  should 'count non-public profile communities' do
    user = create_user('testuser').person
    community_public = fast_create(Community, :environment_id => Environment.default.id, :public_profile => true)
    community_public.add_member(user)
    community_private = fast_create(Community, :environment_id => Environment.default.id, :public_profile => false)
    community_private.add_member(user)
    block = CommunitiesBlock.new
    block.expects(:owner).at_least_once.returns(user)
    assert_equal 2, block.profile_count
  end
  should 'not count non-visible profile communities' do
    user = create_user('testuser').person
    visible_community = Community.create!(:name => 'tcommunity 1', :identifier => 'comm1', :environment => Environment.default, :visible => true)
    visible_community.add_member(user)
    not_visible_community = Community.create!(:name => ' community 2', :identifier => 'comm2', :environment => Environment.default, :visible => false)
    not_visible_community.add_member(user)
    block = CommunitiesBlock.new
    block.expects(:owner).at_least_once.returns(user)
    assert_equal 1, block.profile_count
  end
  should 'count non-public environment communities' do
    community_public = Community.create!(:name => 'tcommunity 1', :identifier => 'comm1', :environment => Environment.default, :public_profile => true)
    community_private = Community.create!(:name => ' community 2', :identifier => 'comm2', :environment => Environment.default, :public_profile => false)
    block = CommunitiesBlock.new
    block.expects(:owner).at_least_once.returns(Environment.default)
    assert_equal 2, block.profile_count
  end
  should 'not count non-visible environment communities' do
    visible_community = Community.create!(:name => 'tcommunity 1', :identifier => 'comm1', :environment => Environment.default, :visible => true)
    not_visible_community = Community.create!(:name => ' community 2', :identifier => 'comm2', :environment => Environment.default, :visible => false)
    block = CommunitiesBlock.new
    block.expects(:owner).at_least_once.returns(Environment.default)
    assert_equal 1, block.profile_count
  end
end