cirandas.net

ref: master

plugins/statistics/test/unit/statistics_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
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
379
380
require_relative '../test_helper'

class StatisticsBlockTest < ActiveSupport::TestCase

  ['user_counter', 'tag_counter', 'comment_counter'].map do |counter|
    should "#{counter} be true by default" do
      b = StatisticsBlock.new
      assert b.is_visible?(counter)
    end
  end

  ['community_counter', 'enterprise_counter', 'product_counter', 'category_counter', 'hit_counter'].map do |counter|
    should "#{counter} be false by default" do
      b = StatisticsBlock.new
      refute b.is_visible?(counter)
    end
  end

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

  should 'provide a default title' do
    block = StatisticsBlock.new

    owner = mock
    owner.expects(:name).returns('my environment')
    block.expects(:owner).returns(owner)
    assert_equal 'Statistics for my environment', block.title
  end

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

  should 'is_visible? return true if setting is true' do
    b = StatisticsBlock.new
    b.community_counter = true
    assert b.is_visible?('community_counter')
  end

  should 'is_visible? return false if setting is false' do
    b = StatisticsBlock.new
    b.community_counter = false
    refute b.is_visible?('community_counter')
  end

  should 'templates return the Community templates of the Environment' do
    b = StatisticsBlock.new
    e = fast_create(Environment)

    t1 = fast_create(Community, :is_template => true, :environment_id => e.id)
    t2 = fast_create(Community, :is_template => true, :environment_id => e.id)
    fast_create(Community, :is_template => false)

    b.expects(:owner).at_least_once.returns(e)

    t = b.templates
    assert_equal [], [t1,t2] - t
    assert_equal [], t - [t1,t2]
  end

  should 'users return the amount of users of the Environment' do
    b = StatisticsBlock.new
    e = fast_create(Environment)

    fast_create(Person, :environment_id => e.id)
    fast_create(Person, :environment_id => e.id)
    fast_create(Person, :visible => false, :environment_id => e.id)

    b.expects(:owner).at_least_once.returns(e)

    assert_equal 2, b.users
  end

  should 'users return the amount of members of the community' do
    b = StatisticsBlock.new

    c1 = fast_create(Community)
    c1.add_member(fast_create(Person))
    c1.add_member(fast_create(Person))
    c1.add_member(fast_create(Person))
    c1.add_member(fast_create(Person, :visible => false))
    c1.add_member(fast_create(Person, :visible => false))

    b.expects(:owner).at_least_once.returns(c1)
    assert_equal 3, b.users
  end

  should 'users return the amount of friends of the person' do
    b = StatisticsBlock.new

    p1 = fast_create(Person)
    p1.add_friend(fast_create(Person))
    p1.add_friend(fast_create(Person))
    p1.add_friend(fast_create(Person))
    p1.add_friend(fast_create(Person, :visible => false))
    p1.add_friend(fast_create(Person, :visible => false))

    b.expects(:owner).at_least_once.returns(p1)
    assert_equal 3, b.users
  end

  should 'communities return the amount of communities of the Environment' do
    b = StatisticsBlock.new
    e = fast_create(Environment)

    fast_create(Community, :environment_id => e.id)
    fast_create(Community, :environment_id => e.id)
    fast_create(Community, :visible => false, :environment_id => e.id)

    b.expects(:owner).at_least_once.returns(e)

    assert_equal 2, b.communities
  end

  should 'enterprises return the amount of enterprises of the Environment' do
    b = StatisticsBlock.new
    e = fast_create(Environment)

    fast_create(Enterprise, :environment_id => e.id)
    fast_create(Enterprise, :environment_id => e.id)
    fast_create(Enterprise, :visible => false, :environment_id => e.id)

    b.expects(:owner).at_least_once.returns(e)

    assert_equal 2, b.enterprises
  end

  should 'return the amount of enabled enterprises' do
    b = StatisticsBlock.new
    e = fast_create(Environment)

    fast_create(Enterprise, :environment_id => e.id)
    fast_create(Enterprise, :environment_id => e.id)
    fast_create(Enterprise, :enabled => false, :environment_id => e.id)

    b.expects(:owner).at_least_once.returns(e)

    assert_equal 2, b.enterprises
  end

  should 'return the amount of visible environment products' do
    return unless defined? ProductsPlugin

    b = StatisticsBlock.new
    e = fast_create(Environment)
    e.enable_plugin('ProductsPlugin')

    e1 = fast_create(Enterprise, :visible => true, :enabled => true, :environment_id => e.id)
    e2 = fast_create(Enterprise, :visible => true, :enabled => false, :environment_id => e.id)
    e3 = fast_create(Enterprise, :visible => false, :enabled => true, :environment_id => e.id)

    fast_create(Product, :profile_id => e1.id)
    fast_create(Product, :profile_id => e1.id)
    fast_create(Product, :profile_id => e2.id)
    fast_create(Product, :profile_id => e2.id)
    fast_create(Product, :profile_id => e3.id)
    fast_create(Product, :profile_id => e3.id)

    b.expects(:owner).at_least_once.returns(e)

    assert_equal 2, b.products
  end

  should 'return the amount of visible enterprise products' do
    return unless defined? ProductsPlugin

    b = StatisticsBlock.new

    e = fast_create(Enterprise)
    environment = e.environment
    environment.enable_plugin('ProductsPlugin')

    fast_create(Product, :profile_id => e.id)
    fast_create(Product, :profile_id => e.id)
    fast_create(Product, :profile_id => nil)

    b.expects(:owner).at_least_once.returns(e)

    assert_equal 2, b.products
  end

  should 'categories return the amount of categories of the Environment' do
    b = StatisticsBlock.new
    e = fast_create(Environment)

    fast_create(Category, :environment_id => e.id)
    fast_create(Category, :environment_id => e.id)

    b.expects(:owner).at_least_once.returns(e)

    assert_equal 2, b.categories
  end

  should 'tags return the amount of tags of the Environment' do
    b = StatisticsBlock.new
    e = fast_create(Environment)

    p1 = fast_create(Person, :environment_id => e.id)
    a1 = fast_create(Article, :profile_id => p1.id)
    a1.tag_list.add('T1', 'T2')
    a1.save!
    a2 = fast_create(Article, :profile_id => p1.id)
    a2.tag_list.add('T3', 'T4')
    a2.save!

    b.expects(:owner).at_least_once.returns(e)

    assert_equal 4, b.tags
  end

  should 'tags return the amount of tags of the community' do
    b = StatisticsBlock.new
    e = fast_create(Environment)

    c1 = fast_create(Community, :environment_id => e.id)
    a1 = fast_create(Article, :profile_id => c1.id)
    a1.tag_list.add('T1', 'T2')
    a1.save!
    a2 = fast_create(Article, :profile_id => c1.id)
    a2.tag_list.add('T3', 'T4')
    a2.save!

    b.expects(:owner).at_least_once.returns(c1)

    assert_equal 4, b.tags
  end

  should 'tags return the amount of tags of the profile (person)' do
    b = StatisticsBlock.new
    e = fast_create(Environment)

    p1 = fast_create(Person, :environment_id => e.id)
    a1 = fast_create(Article, :profile_id => p1.id)
    a1.tag_list.add('T1', 'T2')
    a1.save!
    a2 = fast_create(Article, :profile_id => p1.id)
    a2.tag_list.add('T3', 'T4')
    a2.save!

    b.expects(:owner).at_least_once.returns(p1)

    assert_equal 4, b.tags
  end

  should 'comments return the amount of comments of the Environment' do
    b = StatisticsBlock.new
    e = fast_create(Environment)

    p1 = fast_create(Person, :environment_id => e.id)
    a1 = fast_create(Article, :profile_id => p1.id)

    Comment.create!(:source => a1, :body => 'C1', :author => p1)
    Comment.create!(:source => a1, :body => 'C2', :author => p1)

    a2 = fast_create(Article, :profile_id => p1.id)
    Comment.create!(:source => a2, :body => 'C3', :author => p1)
    Comment.create!(:source => a2, :body => 'C4', :author => p1)

    b.expects(:owner).at_least_once.returns(e)

    assert_equal 4, b.comments
  end

  should 'comments return the amount of comments of the community' do
    b = StatisticsBlock.new
    e = Environment.default

    p1 = fast_create(Person, :environment_id => e.id)
    c1 = fast_create(Community, :environment_id => e.id)
    a1 = fast_create(Article, :profile_id => c1.id)
    Comment.create!(:source => a1, :body => 'C1', :author => p1)
    Comment.create!(:source => a1, :body => 'C2', :author => p1)

    a2 = fast_create(Article, :profile_id => c1.id)
    Comment.create!(:source => a2, :body => 'C3', :author => p1)
    Comment.create!(:source => a2, :body => 'C4', :author => p1)

    b.expects(:owner).at_least_once.returns(c1)

    assert_equal 4, b.comments
  end

  should 'comments return the amount of comments of the profile (person)' do
    b = StatisticsBlock.new
    e = fast_create(Environment)

    p1 = fast_create(Person, :environment_id => e.id)
    a1 = fast_create(Article, :profile_id => p1.id)
    Comment.create!(:source => a1, :body => 'C1', :author => p1)
    Comment.create!(:source => a1, :body => 'C2', :author => p1)

    a2 = fast_create(Article, :profile_id => p1.id)
    Comment.create!(:source => a1, :body => 'C3', :author => p1)
    Comment.create!(:source => a1, :body => 'C4', :author => p1)

    b.expects(:owner).at_least_once.returns(p1)

    assert_equal 4, b.comments
  end

  should 'hits return the amount of hits of the Environment' do
    b = StatisticsBlock.new
    e = fast_create(Environment)

    p1 = fast_create(Person, :environment_id => e.id)
    a1 = fast_create(Article, :profile_id => p1.id, :hits => 2)
    a2 = fast_create(Article, :profile_id => p1.id, :hits => 5)

    b.expects(:owner).at_least_once.returns(e)

    assert_equal 7, b.hits
  end

  should 'hits return the amount of hits of the community' do
    b = StatisticsBlock.new
    e = fast_create(Environment)

    c1 = fast_create(Community, :environment_id => e.id)
    a1 = fast_create(Article, :profile_id => c1.id, :hits => 2)
    a2 = fast_create(Article, :profile_id => c1.id, :hits => 5)

    b.expects(:owner).at_least_once.returns(c1)

    assert_equal 7, b.hits
  end

  should 'hits return the amount of hits of the profile (person)' do
    b = StatisticsBlock.new
    e = fast_create(Environment)

    p1 = fast_create(Person, :environment_id => e.id)
    a1 = fast_create(Article, :profile_id => p1.id, :hits => 2)
    a2 = fast_create(Article, :profile_id => p1.id, :hits => 5)

    b.expects(:owner).at_least_once.returns(p1)

    assert_equal 7, b.hits
  end

  should 'is_counter_available? return true for all counters if owner is environment' do
    b = StatisticsBlock.new
    e = fast_create(Environment)

    b.expects(:owner).at_least_once.returns(e)

    assert b.is_counter_available?(:user_counter)
  end

  should 'is_template_counter_active? return true if setting is true' do
    b = StatisticsBlock.new
    b.templates_ids_counter = {'1' => 'true'}
    assert b.is_template_counter_active?(1)
  end

  should 'is_template_counter_active? return false if setting is false' do
    b = StatisticsBlock.new
    b.templates_ids_counter = {'1' => 'false'}
    refute b.is_template_counter_active?(1)
  end

  should 'template_counter_count return the amount of communities of the Environment using a template' do
    b = StatisticsBlock.new
    e = fast_create(Environment)

    t1 = fast_create(Community, :is_template => true, :environment_id => e.id)
    t2 = fast_create(Community, :is_template => true, :environment_id => e.id)
    fast_create(Community, :is_template => false, :environment_id => e.id, :template_id => t1.id, :visible => true)
    fast_create(Community, :is_template => false, :environment_id => e.id, :template_id => t1.id, :visible => true)
    fast_create(Community, :is_template => false, :environment_id => e.id, :template_id => t1.id, :visible => false)

    fast_create(Community, :is_template => false, :environment_id => e.id, :template_id => t2.id, :visible => true)
    fast_create(Community, :is_template => false, :environment_id => e.id, :template_id => t2.id, :visible => false)

    b.expects(:owner).at_least_once.returns(e)

    assert_equal 2, b.template_counter_count(t1.id)
  end
end