cirandas.net

ref: master

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

class PeopleBlockTest < ActionView::TestCase

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


  should 'declare its default title' do
    assert_not_equal Block.new.default_title, PeopleBlock.new.default_title
  end


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


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


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


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


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


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


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


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


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


  should 'respect limit when listing people' do
    env = fast_create(Environment)
    p1 = fast_create(Person, :environment_id => env.id)
    p2 = fast_create(Person, :environment_id => env.id)
    p3 = fast_create(Person, :environment_id => env.id)
    p4 = fast_create(Person, :environment_id => env.id)

    block = PeopleBlock.new(:limit => 3)
    block.stubs(:owner).returns(env)

    assert_equal 3, block.profile_list.size
  end


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


  should 'count number of public and private people' do
    owner = fast_create(Environment)
    private_p = fast_create(Person, :public_profile => false, :environment_id => owner.id)
    public_p = fast_create(Person, :public_profile => true, :environment_id => owner.id)

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

    assert_equal 2, block.profile_count
  end


  should 'not count number of invisible people' do
    owner = fast_create(Environment)
    private_p = fast_create(Person, :visible => false, :environment_id => owner.id)
    public_p = fast_create(Person, :visible => true, :environment_id => owner.id)

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

    assert_equal 1, block.profile_count
  end

  protected
  include NoosferoTestHelper

end

require 'boxes_helper'

class PeopleBlockViewTest < ActionView::TestCase
  include BoxesHelper

  should 'list people from environment' do
    owner = fast_create(Environment)
    person1 = fast_create(Person, :environment_id => owner.id)
    person2 = fast_create(Person, :environment_id => owner.id)

    block = PeopleBlock.new

    block.expects(:owner).returns(owner).at_least_once
    ActionView::Base.any_instance.expects(:profile_image_link).with(person1, :minor).returns(person1.name)
    ActionView::Base.any_instance.expects(:profile_image_link).with(person2, :minor).returns(person2.name)
    ActionView::Base.any_instance.stubs(:block_title).returns("")

    content = render_block_content(block)

    assert_match(/#{person1.name}/, content)
    assert_match(/#{person2.name}/, content)
  end

  should 'link to "all people"' do
    env = fast_create(Environment)
    block = PeopleBlock.new

    render_block_footer(block)
    assert_select 'a.view-all' do |elements|
      assert_select '[href=/search/people]'
    end
  end

  should 'not have a linear increase in time to display people block' do
    owner = fast_create(Environment)
    owner.boxes<< Box.new
    block = PeopleBlock.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 500
    1.upto(50).map do
      fast_create(Person, :environment_id => owner.id)
    end
    block.reload
    time1 = (Benchmark.measure { 10.times { render_block_content(block) } })

    # another 50
    1.upto(50).map do
      fast_create(Person, :environment_id => owner.id)
    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 people api content' do
    owner = fast_create(Environment)
    person1 = fast_create(Person, :environment_id => owner.id)
    person2 = fast_create(Person, :environment_id => owner.id)

    block = PeopleBlock.new
    block.expects(:owner).returns(owner).at_least_once
    json = block.api_content
    assert_equivalent [person1.identifier, person2.identifier], json["people"].map {|p| p[:identifier]}
  end

  should 'limit people list in api content' do
    owner = fast_create(Environment)
    5.times do
      fast_create(Person, :environment_id => owner.id)
    end
    block = PeopleBlock.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

  should 'not list person template from environment' do
    owner = fast_create(Environment)
    person1 = fast_create(Person, :environment_id => owner.id)
    person2 = fast_create(Person, :environment_id => owner.id)
    template = fast_create(Person, :environment_id => owner.id, is_template: true)
    block = PeopleBlock.new
    block.expects(:owner).returns(owner).at_least_once
    assert_equal 2, block.profile_list.count
  end

end