cirandas.net

ref: master

test/unit/plugin_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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
require_relative "../test_helper"

class PluginTest < ActiveSupport::TestCase

  include Noosfero::Plugin::HotSpot

  def setup
    @environment = Environment.default
  end
  attr_reader :environment

  should 'keep the list of all available plugins' do
    assert File.directory?(File.join(Rails.root, 'plugins', 'foo'))
    assert_includes  Noosfero::Plugin.all, 'FooPlugin'
  end

  should 'returns url to plugin management if plugin has admin_controller' do
    class Plugin1 < Noosfero::Plugin
    end
    File.stubs(:exists?).with(anything).returns(true)

    assert_equal({:controller => 'plugin_test/plugin1_admin', :action => 'index'}, Plugin1.admin_url)
  end

  should 'returns empty hash for class method extra_blocks by default if no blocks are defined on plugin' do

    class SomePlugin1 < Noosfero::Plugin
    end

    assert_equal({}, SomePlugin1.new.extra_blocks)
  end

  should 'returns empty array for instance method extra_blocks by default if no blocks are defined on plugin' do
    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])
    assert_equal([], plugins_extra_blocks)
  end

  should 'returns empty array for instance method extra_blocks by default if nil is returned' do
    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
        nil
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])
    assert_equal([], plugins_extra_blocks)
  end

  should 'returns the blocks implemented by plugin' do
    class CustomBlock1 < Block; end;
    class CustomBlock2 < Block; end;

    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {},
          CustomBlock2 => {}
        }
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])
    assert_equal([], [CustomBlock1, CustomBlock2] - plugins_extra_blocks)
    assert_equal([], plugins_extra_blocks - [CustomBlock1, CustomBlock2])
  end

  should 'returns only person block and non defined types block if type person is specified' do
    class CustomBlock1 < Block; end;
    class CustomBlock2 < Block; end;
    class CustomBlock3 < Block; end;
    class CustomBlock4 < Block; end;
    class CustomBlock5 < Block; end;

    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:type => 'person'},
          CustomBlock2 => {},
          CustomBlock3 => {:type => 'enterprise'},
          CustomBlock4 => {:type => 'community'},
          CustomBlock5 => {:type => 'environment'},
        }
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])
    assert_equal([], [CustomBlock1, CustomBlock2] - plugins_extra_blocks(:type => Person))
    assert_equal([],  plugins_extra_blocks(:type => Person) - [CustomBlock1, CustomBlock2])
  end

  should 'returns only community block and non defined types block if type community is specified' do
    class CustomBlock1 < Block; end;
    class CustomBlock2 < Block; end;
    class CustomBlock3 < Block; end;
    class CustomBlock4 < Block; end;
    class CustomBlock5 < Block; end;

    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:type => 'community'},
          CustomBlock2 => {},
          CustomBlock3 => {:type => 'person'},
          CustomBlock4 => {:type => 'enterprise'},
          CustomBlock5 => {:type => 'environment'},
        }
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])
    assert_equal([], [CustomBlock1, CustomBlock2] - plugins_extra_blocks(:type => Community))
    assert_equal([],  plugins_extra_blocks(:type => Community) - [CustomBlock1, CustomBlock2])
  end

  should 'returns only enterprise block and non defined types block if type enterprise is specified' do
    class CustomBlock1 < Block; end;
    class CustomBlock2 < Block; end;
    class CustomBlock3 < Block; end;
    class CustomBlock4 < Block; end;
    class CustomBlock5 < Block; end;

    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:type => 'enterprise'},
          CustomBlock2 => {},
          CustomBlock3 => {:type => 'person'},
          CustomBlock4 => {:type => 'community'},
          CustomBlock5 => {:type => 'environment'},
        }
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])
    assert_equal([], [CustomBlock1, CustomBlock2] - plugins_extra_blocks(:type => Enterprise))
    assert_equal([],  plugins_extra_blocks(:type => Enterprise) - [CustomBlock1, CustomBlock2])
  end

  should 'returns only environment block and non defined types block if type environment is specified' do
    class CustomBlock1 < Block; end;
    class CustomBlock2 < Block; end;
    class CustomBlock3 < Block; end;
    class CustomBlock4 < Block; end;
    class CustomBlock5 < Block; end;

    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:type => 'environment'},
          CustomBlock2 => {},
          CustomBlock3 => {:type => 'enterprise'},
          CustomBlock4 => {:type => 'community'},
          CustomBlock5 => {:type => 'person'},
        }
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])
    assert_equal([], [CustomBlock1, CustomBlock2] - plugins_extra_blocks(:type => Environment))
    assert_equal([],  plugins_extra_blocks(:type => Environment) - [CustomBlock1, CustomBlock2])
  end

  should 'returns array of blocks of a specified type' do
    class CustomBlock1 < Block; end;
    class CustomBlock2 < Block; end;
    class CustomBlock3 < Block; end;
    class CustomBlock4 < Block; end;
    class CustomBlock5 < Block; end;

    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:type => 'person'},
          CustomBlock2 => {:type => 'person'},
          CustomBlock3 => {:type => 'environment'},
          CustomBlock4 => {:type => 'enterprise'},
          CustomBlock5 => {:type => 'community'},
        }
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])
    assert_equal([], [CustomBlock1, CustomBlock2] - plugins_extra_blocks(:type => Person))
    assert_equal([], plugins_extra_blocks(:type => Person) - [CustomBlock1, CustomBlock2])
  end

  should 'returns all blocks without type if no type is specified' do
    class CustomBlock1 < Block; end;
    class CustomBlock2 < Block; end;
    class CustomBlock3 < Block; end;
    class CustomBlock4 < Block; end;
    class CustomBlock5 < Block; end;
    class CustomBlock6 < Block; end;

    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:type => 'person'},
          CustomBlock2 => {:type => 'environment'},
          CustomBlock3 => {:type => 'enterprise'},
          CustomBlock4 => {:type => 'community'},
          CustomBlock5 => {},
          CustomBlock6 => {},
        }
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])
    assert_equal([], [CustomBlock5, CustomBlock6] - plugins_extra_blocks)
    assert_equal([], plugins_extra_blocks - [CustomBlock5, CustomBlock6])
  end

  should 'returns all blocks if type all is specified as parameter' do
    class CustomBlock1 < Block; end;
    class CustomBlock2 < Block; end;
    class CustomBlock3 < Block; end;
    class CustomBlock4 < Block; end;
    class CustomBlock5 < Block; end;
    class CustomBlock6 < Block; end;

    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:type => 'person'},
          CustomBlock2 => {:type => 'environment'},
          CustomBlock3 => {:type => 'enterprise'},
          CustomBlock4 => {:type => 'community'},
          CustomBlock5 => {},
          CustomBlock6 => {},
        }
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])
    assert_equal([], [CustomBlock1, CustomBlock2, CustomBlock3, CustomBlock4, CustomBlock5, CustomBlock6] - plugins_extra_blocks(:type => :all))
    assert_equal([], plugins_extra_blocks(:type => :all) - [CustomBlock1, CustomBlock2, CustomBlock3, CustomBlock4, CustomBlock5, CustomBlock6])
  end


  should 'returns blocks of specified types' do
    class CustomBlock1 < Block; end;
    class CustomBlock2 < Block; end;
    class CustomBlock3 < Block; end;
    class CustomBlock4 < Block; end;

    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:type => ['person', 'environment']},
          CustomBlock2 => {:type => 'environment'},
          CustomBlock3 => {:type => 'enterprise'},
          CustomBlock4 => {:type => 'community'},
        }
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])
    assert_equal([], [CustomBlock1, CustomBlock2] - plugins_extra_blocks(:type => Environment))
    assert_equal([], plugins_extra_blocks(:type => Environment) - [CustomBlock1, CustomBlock2])
  end

  should 'returns blocks of with types passed as string or constant' do
    class CustomBlock1 < Block; end;
    class CustomBlock2 < Block; end;
    class CustomBlock3 < Block; end;
    class CustomBlock4 < Block; end;
    class CustomBlock5 < Block; end;

    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:type => ['person', 'environment']},
          CustomBlock2 => {:type => 'environment'},
          CustomBlock3 => {:type => Environment},
          CustomBlock4 => {:type => [Environment]},
          CustomBlock5 => {:type => 'person'},
        }
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])
    assert_equal([], [CustomBlock1, CustomBlock2, CustomBlock3, CustomBlock4] - plugins_extra_blocks(:type => Environment))
    assert_equal([], plugins_extra_blocks(:type => Environment) - [CustomBlock1, CustomBlock2, CustomBlock3, CustomBlock4])
  end

  should 'through exception if undefined type is specified as parameter' do
    class CustomBlock1 < Block; end;

    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:type => 'undefined_type'},
        }
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])

    assert_raise NameError do
      plugins_extra_blocks
    end
  end

  should 'returns only position 1 block and non defined position block if position 1 is specified' do
    class CustomBlock1 < Block; end;
    class CustomBlock2 < Block; end;
    class CustomBlock3 < Block; end;
    class CustomBlock4 < Block; end;

    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:position => 1},
          CustomBlock2 => {},
          CustomBlock3 => {:position => 3},
          CustomBlock4 => {:position => 2},
        }
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])
    assert_equal([], [CustomBlock1, CustomBlock2] - plugins_extra_blocks(:position => 1))
    assert_equal([],  plugins_extra_blocks(:position => 1) - [CustomBlock1, CustomBlock2])
  end

  should 'returns only position 2 block and non defined position block if position 2 is specified' do
    class CustomBlock1 < Block; end;
    class CustomBlock2 < Block; end;
    class CustomBlock3 < Block; end;
    class CustomBlock4 < Block; end;

    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:position => 2},
          CustomBlock2 => {},
          CustomBlock3 => {:position => 3},
          CustomBlock4 => {:position => 1},
        }
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])
    assert_equal([], [CustomBlock1, CustomBlock2] - plugins_extra_blocks(:position => 2))
    assert_equal([],  plugins_extra_blocks(:position => 2) - [CustomBlock1, CustomBlock2])
  end

  should 'returns only position 3 block and non defined position block if position 3 is specified' do
    class CustomBlock1 < Block; end;
    class CustomBlock2 < Block; end;
    class CustomBlock3 < Block; end;
    class CustomBlock4 < Block; end;

    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:position => 3},
          CustomBlock2 => {},
          CustomBlock3 => {:position => 1},
          CustomBlock4 => {:position => 2},
        }
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])
    assert_equal([], [CustomBlock1, CustomBlock2] - plugins_extra_blocks(:position => 3))
    assert_equal([],  plugins_extra_blocks(:position => 3) - [CustomBlock1, CustomBlock2])
  end

  should 'returns array of blocks of a specified position' do
    class CustomBlock1 < Block; end;
    class CustomBlock2 < Block; end;
    class CustomBlock3 < Block; end;
    class CustomBlock4 < Block; end;

    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:position => 1 },
          CustomBlock2 => {:position => 1 },
          CustomBlock3 => {:position => 2},
          CustomBlock4 => {:position => 3},
        }
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])
    assert_equal([], [CustomBlock1, CustomBlock2] - plugins_extra_blocks(:position => 1))
    assert_equal([], plugins_extra_blocks(:position => 1) - [CustomBlock1, CustomBlock2])
  end

  should 'returns array of blocks of a specified position wihout any type' do
    class CustomBlock1 < Block; end;
    class CustomBlock2 < Block; end;
    class CustomBlock3 < Block; end;
    class CustomBlock4 < Block; end;
    class CustomBlock5 < Block; end;
    class CustomBlock6 < Block; end;
    class CustomBlock7 < Block; end;
    class CustomBlock8 < Block; end;
    class CustomBlock9 < Block; end;
    class CustomBlock10 < Block; end;

    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:type => Person, :position => 1 },
          CustomBlock2 => {:type => Community, :position => 1 },
          CustomBlock3 => {:type => Enterprise, :position => 1 },
          CustomBlock4 => {:type => Environment, :position => 1 },
          CustomBlock5 => {:position => 1 },
          CustomBlock6 => {:type => Person, :position => [1,2,3] },
          CustomBlock7 => {:type => Community, :position => [1,2,3] },
          CustomBlock8 => {:type => Enterprise, :position => [1,2,3] },
          CustomBlock9 => {:type => Environment, :position => [1,2,3] },
          CustomBlock10 => {:position => [1,2,3] },
        }
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])
    assert_equal([], [CustomBlock5, CustomBlock10] - plugins_extra_blocks(:position => 1))
    assert_equal([], plugins_extra_blocks(:position => 1) - [CustomBlock5, CustomBlock10])
  end

  should 'returns blocks of all position if no position is specified' do
    class CustomBlock1 < Block; end;
    class CustomBlock2 < Block; end;
    class CustomBlock3 < Block; end;
    class CustomBlock4 < Block; end;

    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:position => 1 },
          CustomBlock2 => {:position => 2},
          CustomBlock3 => {:position => 3},
          CustomBlock4 => {},
        }
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])
    assert_equal([], [CustomBlock1, CustomBlock2, CustomBlock3, CustomBlock4] - plugins_extra_blocks)
    assert_equal([], plugins_extra_blocks - [CustomBlock1, CustomBlock2, CustomBlock3, CustomBlock4])
  end

  should 'returns blocks of specified positions' do
    class CustomBlock1 < Block; end;
    class CustomBlock2 < Block; end;
    class CustomBlock3 < Block; end;

    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:position => [1, 2]},
          CustomBlock2 => {:position => 2},
          CustomBlock3 => {:position => 3},
        }
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])
    assert_equal([], [CustomBlock1, CustomBlock2] - plugins_extra_blocks(:position => 2))
    assert_equal([], plugins_extra_blocks(:position => 2) - [CustomBlock1, CustomBlock2])
  end

  should 'returns blocks of with positions passed as string or numbers' do
    class CustomBlock1 < Block; end;
    class CustomBlock2 < Block; end;
    class CustomBlock3 < Block; end;
    class CustomBlock4 < Block; end;
    class CustomBlock5 < Block; end;

    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:position => [1, '2']},
          CustomBlock2 => {:position => '2'},
          CustomBlock3 => {:position => 2},
          CustomBlock4 => {:position => [2]},
          CustomBlock5 => {:position => '1'},
        }
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])
    assert_equal([], [CustomBlock1, CustomBlock2, CustomBlock3, CustomBlock4] - plugins_extra_blocks(:position => 2))
    assert_equal([], plugins_extra_blocks(:position => 2) - [CustomBlock1, CustomBlock2, CustomBlock3, CustomBlock4])
  end

  should 'through exception if undefined position is specified as parameter' do
    class CustomBlock1 < Block; end;

    class Plugin1 < Noosfero::Plugin
      def self.extra_blocks
        {
          CustomBlock1 => {:type => 'undefined_type'},
        }
      end
    end
    p = Plugin1.new
    plugins.stubs(:enabled_plugins).returns([p])

    assert_raise NameError do
      plugins_extra_blocks
    end
  end

  should 'comment_actions be nil if the comment is nil' do
    class SomePlugin < Noosfero::Plugin; end
    plugin = SomePlugin.new
    assert_nil plugin.comment_actions(nil)
  end

  should 'comment_actions be nil by default' do
    class SomePlugin < Noosfero::Plugin; end
    plugin = SomePlugin.new
    assert_nil plugin.comment_actions(Comment.new)
  end

  should 'check_comment_actions be an empty array if the comment is nil' do
    class SomePlugin < Noosfero::Plugin; end
    plugin = SomePlugin.new
    assert_equal [], plugin.check_comment_actions(nil)
  end

  should 'check_comment_actions be  an empty array by default' do
    class SomePlugin < Noosfero::Plugin; end
    plugin = SomePlugin.new
    assert_equal [], plugin.check_comment_actions(Comment.new)
  end

  should 'have default search suggestions based on search terms' do
    context = Environment.default
    st1 = SearchTerm.new(:term => 'universe', :asset => 'science', :context => context)
    st1.score = 3
    st1.save!
    st2 = SearchTerm.new(:term => 'unisound', :asset => 'science', :context => context)
    st2.score = 5
    st2.save!
    st3 = SearchTerm.new(:term => 'unicrowd', :asset => 'science', :context => context)
    st3.score = 2
    st3.save!
    st4 = SearchTerm.new(:term => 'univault', :asset => 'science', :context => context)
    st4.score = 8
    st4.save!
    # Query not match
    st5 = SearchTerm.create!(:term => 'destroyer', :asset => 'science', :context => context)
    st5.score = 8
    st5.save!
    # Different asset
    st6 = SearchTerm.create!(:term => 'unilight', :asset => 'pseudo-science', :context => context)
    st6.score = 8
    st6.save!
    # Score not bigger than zero
    st7 = SearchTerm.create!(:term => 'unicloud', :asset => 'science', :context => context)
    st7.score = 0
    st7.save!

    class SomePlugin < Noosfero::Plugin; end
    plugin = SomePlugin.new

    suggestions = plugin.find_suggestions('uni', context, 'science')

    assert_includes suggestions, st1.term
    assert_includes suggestions, st2.term
    assert_includes suggestions, st3.term
    assert_includes suggestions, st4.term
    assert_not_includes suggestions, st5.term
    assert_not_includes suggestions, st6.term
    assert_not_includes suggestions, st7.term

    assert_order [st4.term, st2.term, st1.term, st3.term], suggestions

    limited_suggestions = plugin.find_suggestions('uni', context, 'science', {:limit => 2})
    assert_equivalent [st4.term, st2.term], limited_suggestions
  end

  should 'article_extra_toolbar_buttons return an empty array by default' do
    class Plugin1 < Noosfero::Plugin
    end
    p = Plugin1.new
    assert_equal [], p.article_extra_toolbar_buttons(nil)
  end

end