cirandas.net

ref: master

plugins/people_block/test/unit/friends_block_test.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
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
require_relative '../test_helper'

class FriendsBlockTest < ActionView::TestCase

  should 'inherit from Block' do
    assert_kind_of Block, FriendsBlock.new
  end

  should 'declare its default title' do
    FriendsBlock.any_instance.expects(:profile_count).returns(0)
    assert_not_equal Block.new.default_title, FriendsBlock.new.default_title
  end

  should 'describe itself' do
    assert_not_equal Block.description, FriendsBlock.description
  end

  should 'is editable' do
    block = FriendsBlock.new
    assert block.editable?
  end

  should 'have field limit' do
    block = FriendsBlock.new
    assert_respond_to block, :limit
  end

  should 'default value of limit' do
    block = FriendsBlock.new
    assert_equal 6, block.limit
  end

  should 'have field name' do
    block = FriendsBlock.new
    assert_respond_to block, :name
  end

  should 'default value of name' do
    block = FriendsBlock.new
    assert_equal "", block.name
  end

  should 'have field address' do
    block = FriendsBlock.new
    assert_respond_to block, :address
  end

  should 'default value of address' do
    block = FriendsBlock.new
    assert_equal "", block.address
  end

  should 'prioritize profiles with image by default' do
    assert FriendsBlock.new.prioritize_profiles_with_image
  end

  should 'accept a limit of people to be displayed' do
    block = FriendsBlock.new
    block.limit = 20
    assert_equal 20, block.limit
  end

  should 'count number of owner friends' do
    owner = fast_create(Person)
    friend1 = fast_create(Person)
    friend2 = fast_create(Person)
    friend3 = fast_create(Person)
    owner.add_friend(friend1)
    owner.add_friend(friend2)
    owner.add_friend(friend3)

    block = FriendsBlock.new
    block.expects(:owner).returns(owner).at_least_once

    assert_equal 3, block.profile_count
  end

  should 'count number of public and private friends' do
    owner = fast_create(Person)
    private_p = fast_create(Person, {:public_profile => false})
    public_p = fast_create(Person, {:public_profile => true})

    owner.add_friend(private_p)
    owner.add_friend(public_p)

    block = FriendsBlock.new
    block.expects(:owner).returns(owner).at_least_once

    assert_equal 2, block.profile_count
  end

  should 'not count number of invisible friends' do
    owner = fast_create(Person)
    private_p = fast_create(Person, {:visible => false})
    public_p = fast_create(Person, {:visible => true})

    owner.add_friend(private_p)
    owner.add_friend(public_p)

    block = FriendsBlock.new
    block.expects(:owner).returns(owner).at_least_once

    assert_equal 1, block.profile_count
  end

  should 'list owner\'s friends suggestions' do
    owner = fast_create(Person)
    suggestion1 = ProfileSuggestion.create!(:suggestion => fast_create(Person), :person => owner)
    suggestion2 = ProfileSuggestion.create!(:suggestion => fast_create(Person), :person => owner)

    block = FriendsBlock.new
    block.stubs(:owner).returns(owner)

    assert_equivalent block.suggestions, [suggestion1,suggestion2]
  end

  should 'not list templates as friends' do
    owner = fast_create(Person)
    friend1 = fast_create(Person)
    template = fast_create(Person, is_template: true)
    owner.add_friend(friend1)
    owner.add_friend(template)
    block = FriendsBlock.new
    block.expects(:owner).returns(owner).at_least_once
    assert_equal 1, block.profile_count
  end

  protected
  include NoosferoTestHelper

end

require 'boxes_helper'

class FriendsBlockViewTest < ActionView::TestCase
  include BoxesHelper

  should 'list friends from person' do
    owner = fast_create(Person)
    friend1 = fast_create(Person)
    friend2 = fast_create(Person)
    owner.add_friend(friend1)
    owner.add_friend(friend2)

    block = FriendsBlock.new

    block.expects(:owner).returns(owner).at_least_once
    ActionView::Base.any_instance.expects(:profile_image_link).with(friend1, :minor).returns(friend1.name)
    ActionView::Base.any_instance.expects(:profile_image_link).with(friend2, :minor).returns(friend2.name)
    ActionView::Base.any_instance.expects(:block_title).with(anything, anything).returns('')

    content = render_block_content(block)

    assert_match(/#{friend1.name}/, content)
    assert_match(/#{friend2.name}/, content)
  end

  should 'link to "all friends"' do
    person1 = create_user('mytestperson').person

    block = FriendsBlock.new
    block.stubs(:suggestions).returns([])
    block.expects(:owner).returns(person1).at_least_once

    assert_tag_in_string render_block_footer(block), tag: 'a', attributes: {class: 'view-all', href: '/profile/mytestperson/friends' }
  end

  should 'not have a linear increase in time to display friends block' do
    owner = fast_create(Person)
    owner.boxes<< Box.new
    block = FriendsBlock.create!(:box => owner.boxes.first)

    ActionView::Base.any_instance.stubs(:profile_image_link).returns('some name')
    ActionView::Base.any_instance.stubs(:block_title).returns("")

    # no people
    block.reload
    time0 = (Benchmark.measure { 10.times { render_block_content(block) } })

    # first 50
    1.upto(50).map do |n|
      p = create_user("user #{n}").person
      owner.add_friend(p)
    end
    block.reload
    time1 = (Benchmark.measure { 10.times { render_block_content(block) } })

    # another 50
    1.upto(50).map do |n|
      p = create_user("user #{n}").person
      owner.add_friend(p)
    end
    block.reload
    time2 = (Benchmark.measure { 10.times { render_block_content(block) } })

    # should not scale linearly, i.e. the inclination of the first segment must
    # be a lot higher than the one of the segment segment. To compensate for
    # small variations due to hardware and/or execution environment, we are
    # satisfied if the the inclination of the first segment is at least twice
    # the inclination of the second segment.
    a1 = (time1.total - time0.total)/50.0
    a2 = (time2.total - time1.total)/50.0
    assert a1 > a2*NON_LINEAR_FACTOR, "#{a1} should be larger than #{a2} by at least a factor of #{NON_LINEAR_FACTOR}"
  end

  should 'list friends in api content' do
    owner = fast_create(Person)
    friend1 = fast_create(Person)
    friend2 = fast_create(Person)
    owner.add_friend(friend1)
    owner.add_friend(friend2)
    block = FriendsBlock.new
    block.expects(:owner).returns(owner).at_least_once
    json = block.api_content
    assert_equivalent [friend1.identifier, friend2.identifier], json["people"].map {|p| p[:identifier]}
  end

  should 'limit friends list in api content' do
    owner = fast_create(Person)
    5.times do
      friend = fast_create(Person)
      owner.add_friend(friend)
    end
    block = FriendsBlock.new(limit: 3)
    block.expects(:owner).returns(owner.reload).at_least_once
    json = block.api_content
    assert_equal 3, json["people"].size
    assert_equal 5, json["#"]
  end
end