cirandas.net

ref: master

test/unit/profile_list_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
231
232
233
234
235
236
require_relative "../test_helper"

class ProfileListBlockTest < ActiveSupport::TestCase

  include ActionView::Helpers::TagHelper

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

  should 'provide a default title' do
    assert_not_equal Block.new.default_title, ProfileListBlock.new.default_title
  end

  should 'accept a limit of people to be displayed (and default to 6)' do
    block = ProfileListBlock.new
    assert_equal 6, block.limit

    block.limit = 20
    assert_equal 20, block.limit
  end

  include BoxesHelper

  should 'list people' do
    env = fast_create(Environment)

    person1 = create_user('testperson1', :environment => env).person
    person2 = create_user('testperson2', :environment => env).person
    person3 = create_user('testperson3', :environment => env).person

    block = ProfileListBlock.new
    block.stubs(:owner).returns(env)

    ApplicationHelper.class_eval do
      alias_method :original_profile_image_link, :profile_image_link
      def profile_image_link( profile, size=:portrait, tag='li', extra_info = nil )
        "<#{profile.name}>"
      end
    end

    content = render_block_content(block)
    assert_match '<testperson1>', content
    assert_match '<testperson2>', content
    assert_match '<testperson3>', content
    ApplicationHelper.class_eval do
      alias_method :profile_image_link, :original_profile_image_link
    end
  end

  should 'list private profiles' do
    env = fast_create(Environment)
    env.boxes << Box.new
    profile1 = fast_create(Profile, :environment_id => env.id)
    profile2 = fast_create(Profile, :environment_id => env.id, :public_profile => false) # private profile
    block = ProfileListBlock.new
    env.boxes.first.blocks << block
    block.save!

    profiles = block.profiles
    assert_includes profiles, profile1
    assert_includes profiles, profile2
  end

  should 'not list invisible profiles' do
    env = fast_create(Environment)
    env.boxes << Box.new
    profile1 = fast_create(Profile, :environment_id => env.id)
    profile2 = fast_create(Profile, :environment_id => env.id, :visible => false) # not visible profile
    block = ProfileListBlock.new
    env.boxes.first.blocks << block
    block.save!

    profiles = block.profile_list
    assert_includes profiles, profile1
    assert_not_includes profiles, profile2
  end

  should 'provide view_title' do
    env = fast_create(Environment)
    env.boxes << Box.new
    block = ProfileListBlock.new(:title => 'Title from block')
    env.boxes.first.blocks << block
    block.save!
    assert_equal 'Title from block', block.view_title
  end

  should 'provide view title with variables' do
    env = fast_create(Environment)
    env.boxes << Box.new
    block = ProfileListBlock.new(:title => '{#} members')
    env.boxes.first.blocks << block
    block.save!
    assert_equal '0 members', block.view_title
  end

  should 'count number of only public profiles' do
    env = fast_create(Environment)
    env.boxes << Box.new
    block = ProfileListBlock.new
    env.boxes.first.blocks << block
    block.save!

    priv_p = fast_create(Person, :environment_id => env.id, :public_profile => false)
    pub_p = fast_create(Person, :environment_id => env.id, :public_profile => true)

    priv_c = fast_create(Community, :public_profile => false, :environment_id => env.id)
    pub_c = fast_create(Community, :public_profile => true , :environment_id => env.id)

    priv_e = fast_create(Enterprise, :public_profile => false , :environment_id => env.id)
    pub_e = fast_create(Enterprise, :public_profile => true , :environment_id => env.id)

    assert_equal 3, block.profile_count
  end

  should 'only count number of visible profiles' do
    env = fast_create(Environment)
    env.boxes << Box.new
    block = ProfileListBlock.new
    env.boxes.first.blocks << block
    block.save!

    priv_p = fast_create(Person, :environment_id => env.id, :visible => false)
    pub_p = fast_create(Person, :environment_id => env.id, :visible => true)

    priv_c = fast_create(Community, :visible => false, :environment_id => env.id)
    pub_c = fast_create(Community, :visible => true , :environment_id => env.id)

    priv_e = fast_create(Enterprise, :visible => false , :environment_id => env.id)
    pub_e = fast_create(Enterprise, :visible => true , :environment_id => env.id)

    assert_equal 3, block.profile_count
  end

  should 'respect limit when listing profiles' 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 = ProfileListBlock.new(:limit => 3)
    block.stubs(:owner).returns(env)

    assert_equal 3, block.profile_list.size
  end

  should 'list random profiles' do
    env = fast_create(Environment)
    6.times.each do
      fast_create(Person, :environment_id => env.id)
    end

    block = ProfileListBlock.new
    block.stubs(:owner).returns(env)

    assert_not_equal block.profile_list.map(&:id), block.profile_list.map(&:id)
  end

  should 'prioritize profiles with image if this option is turned on' do
    env = fast_create(Environment)
    img1 = create(Image, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
    p1 = fast_create(Person, :environment_id => env.id, :image_id => img1.id)
    img2 = create(Image, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
    p2 = fast_create(Person, :environment_id => env.id, :image_id => img2.id)

    p_without_image = fast_create(Person, :environment_id => env.id)

    block = ProfileListBlock.new(:limit => 2)
    block.stubs(:owner).returns(env)
    block.stubs(:prioritize_profiles_with_image).returns(true)

    assert_not_includes block.profile_list[0..1], p_without_image
  end

  should 'list profiles without image only if profiles with image arent enought' do
    env = fast_create(Environment)
    img1 = create(Image, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
    p1 = fast_create(Person, :environment_id => env.id, :image_id => img1.id)
    img2 = create(Image, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
    p2 = fast_create(Person, :environment_id => env.id, :image_id => img2.id)
    p_without_image = fast_create(Person, :environment_id => env.id)
    block = ProfileListBlock.new
    block.stubs(:owner).returns(env)
    block.stubs(:prioritize_profiles_with_image).returns(true)

    block.limit = 2
    assert_not_includes block.profile_list, p_without_image

    block.limit = 3
    assert_includes block.profile_list, p_without_image
  end

  should 'list profile with image among profiles without image' do
    env = fast_create(Environment)
    5.times do |n|
      fast_create(Person, :environment_id => env.id)
    end
    img = create(Image, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
    with_image = fast_create(Person, :environment_id => env.id, :image_id => img.id)
    block = ProfileListBlock.new(:limit => 3)
    block.stubs(:prioritize_profiles_with_image).returns(true)
    block.stubs(:owner).returns(env)
    assert_includes block.profile_list, with_image
  end

  should 'not prioritize profiles with image if this option is turned off' do
    env = fast_create(Environment)
    img = create(Image, :uploaded_data => fixture_file_upload('/files/rails.png', 'image/png'))
    with_image = fast_create(Person, :environment_id => env.id, :updated_at => DateTime.now, :image_id => img.id)
    5.times do |n|
      fast_create(Person, :environment_id => env.id, :updated_at => DateTime.now + 1.day)
    end

    block = ProfileListBlock.new(:limit => 3)
    block.stubs(:owner).returns(env)
    block.stubs(:prioritize_profiles_with_image).returns(false)

    assert_not_includes block.profile_list, with_image
  end

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

  should 'return the max value in the range between zero and limit' do
    block = ProfileListBlock.new
    assert_equal 6, block.get_limit
  end

  should 'return 0 if limit of the block is negative' do
    block = ProfileListBlock.new
    block.limit = -5
    assert_equal 0, block.get_limit
  end
end