cirandas.net

ref: master

test/unit/plugin_manager_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
require_relative "../test_helper"

class PluginManagerTest < ActiveSupport::TestCase

  include Noosfero::Plugin::HotSpot

  def setup
    @environment = Environment.default
    @controller = mock()
    @controller.stubs(:profile).returns()
    @controller.stubs(:request).returns()
    @controller.stubs(:response).returns()
    @controller.stubs(:params).returns()
    @manager = Noosfero::Plugin::Manager.new(@environment, @controller)
  end
  attr_reader :environment
  attr_reader :manager

  should 'give access to environment and context' do
    assert_same @environment, @manager.environment
    assert_same @controller, @manager.context
  end

  should 'return the intersection between environment\'s enabled plugins and system available plugins' do
    class Plugin1 < Noosfero::Plugin; end;
    class Plugin2 < Noosfero::Plugin; end;
    class Plugin3 < Noosfero::Plugin; end;
    class Plugin4 < Noosfero::Plugin; end;
    Noosfero::Plugin.stubs(:all).returns(['PluginManagerTest::Plugin1', 'PluginManagerTest::Plugin2', 'PluginManagerTest::Plugin3', 'PluginManagerTest::Plugin4'])
    environment.stubs(:enabled_plugins).returns([Plugin1.to_s, Plugin2.to_s, Plugin4.to_s])
    Noosfero::Plugin.stubs(:all).returns([Plugin1.to_s, Plugin3.to_s, Plugin4.to_s])
    results = plugins.enabled_plugins.map { |instance| instance.class.to_s }
    assert_equal [Plugin1.to_s, Plugin4.to_s], results
  end

  should 'map events to registered plugins' do

    class Noosfero::Plugin
      def random_event
        nil
      end
    end

    class Plugin1 < Noosfero::Plugin
      def random_event
        'Plugin 1 action.'.html_safe
      end
    end

    class Plugin2 < Noosfero::Plugin
      def random_event
        'Plugin 2 action.'.html_safe
      end
    end
    Noosfero::Plugin.stubs(:all).returns(['PluginManagerTest::Plugin1', 'PluginManagerTest::Plugin2'])

    environment.stubs(:enabled_plugins).returns([Plugin1.to_s, Plugin2.to_s])

    p1 = Plugin1.new
    p2 = Plugin2.new

    assert_equal [p1.random_event, p2.random_event], plugins.dispatch(:random_event)
  end

  should 'dispatch_first method returns the first plugin response if there is many plugins to responde the event' do
    class Noosfero::Plugin
      def random_event
      end
    end

    class Plugin1 < Noosfero::Plugin
      def random_event
        'Plugin 1 action.'.html_safe
      end
    end

    class Plugin2 < Noosfero::Plugin
      def random_event
        'Plugin 2 action.'.html_safe
      end
    end

    class Plugin3 < Noosfero::Plugin
      def random_event
        'Plugin 3 action.'.html_safe
      end
    end
    Noosfero::Plugin.stubs(:all).returns(['PluginManagerTest::Plugin1', 'PluginManagerTest::Plugin2', 'PluginManagerTest::Plugin3'])

    environment.stubs(:enabled_plugins).returns([Plugin1.to_s, Plugin2.to_s, Plugin3.to_s])
    p1 = Plugin1.new

    assert_equal p1.random_event, plugins.dispatch_first(:random_event)
  end

  should 'return the first non-blank result' do
    class Plugin1 < Noosfero::Plugin
      def random_event
      end
    end

    class Plugin2 < Noosfero::Plugin
      def random_event
        'Plugin2'
      end
    end

    class Plugin3 < Noosfero::Plugin
      def random_event
        'Plugin3'
      end
    end
    Noosfero::Plugin.stubs(:all).returns(['PluginManagerTest::Plugin1', 'PluginManagerTest::Plugin2', 'PluginManagerTest::Plugin3'])

    environment.enable_plugin(Plugin1.name)
    environment.enable_plugin(Plugin2.name)
    environment.enable_plugin(Plugin3.name)

    Plugin3.any_instance.expects(:random_event).never

    assert 'Plugin2', manager.dispatch_first(:random_event)
  end

  should 'returns plugins that returns true to the event' do
    class Plugin1 < Noosfero::Plugin
      def random_event
      end
    end

    class Plugin2 < Noosfero::Plugin
      def random_event
        true
      end
    end

    class Plugin3 < Noosfero::Plugin
      def random_event
        true
      end
    end
    Noosfero::Plugin.stubs(:all).returns(['PluginManagerTest::Plugin1', 'PluginManagerTest::Plugin2', 'PluginManagerTest::Plugin3'])

    environment.enable_plugin(Plugin1.name)
    environment.enable_plugin(Plugin2.name)
    environment.enable_plugin(Plugin3.name)

    results = manager.fetch_plugins(:random_event)

    assert_includes results, Plugin2
    assert_includes results, Plugin3
  end

  should 'return the first plugin that returns true' do
    class Plugin1 < Noosfero::Plugin
      def random_event
      end
    end

    class Plugin2 < Noosfero::Plugin
      def random_event
        true
      end
    end

    class Plugin3 < Noosfero::Plugin
      def random_event
        true
      end
    end
    Noosfero::Plugin.stubs(:all).returns(['PluginManagerTest::Plugin1', 'PluginManagerTest::Plugin2', 'PluginManagerTest::Plugin3'])

    environment.enable_plugin(Plugin1.name)
    environment.enable_plugin(Plugin2.name)
    environment.enable_plugin(Plugin3.name)

    Plugin3.any_instance.expects(:random_event).never

    assert_equal Plugin2, manager.fetch_first_plugin(:random_event)
  end

  should 'return nil if missing method is called' do
    class Plugin1 < Noosfero::Plugin
    end
    Noosfero::Plugin.stubs(:all).returns(['PluginManagerTest::Plugin1'])
    environment.enable_plugin(Plugin1)

    assert_equal nil, @manager.result_for(Plugin1.new, :content_remove_new)
  end

  should 'parse macro' do
    class Plugin1 < Noosfero::Plugin
      def macros
        [Macro1, Macro2]
      end
    end
    Noosfero::Plugin.stubs(:all).returns(['PluginManagerTest::Plugin1'])

    class Plugin1::Macro1 < Noosfero::Plugin::Macro
      def convert(macro, source)
        macro.gsub('%{name}', 'Macro1')
      end
    end

    class Plugin1::Macro2 < Noosfero::Plugin::Macro
      def convert(macro, source)
        macro.gsub('%{name}', 'Macro2')
      end
    end

    environment.enable_plugin(Plugin1)
    macro = 'My name is %{name}!'

    assert_equal 'My name is Macro1!', manager.parse_macro(Plugin1::Macro1.identifier, macro)
    assert_equal 'My name is Macro2!', manager.parse_macro(Plugin1::Macro2.identifier, macro)
  end

  should 'dispatch event in a pipeline sequence' do
    class Plugin1 < Noosfero::Plugin
      def transform(v1, v2)
        v = 2
        [v1 * v, v2 * v]
      end
    end

    class Plugin2 < Noosfero::Plugin
      def transform(v1, v2)
        v = 5
        [v1 * v, v2 * v]
      end
    end
    Noosfero::Plugin.stubs(:all).returns(['PluginManagerTest::Plugin1', 'PluginManagerTest::Plugin2'])

    environment.enable_plugin(Plugin1)
    environment.enable_plugin(Plugin2)

    assert_equal [10, 20], manager.pipeline(:transform, 1, 2)
  end

  should 'be able to pipeline with single arguments' do
    class Plugin1 < Noosfero::Plugin
      def transform(value)
        value * 2
      end
    end

    class Plugin2 < Noosfero::Plugin
      def transform(value)
        value * 5
      end
    end
    Noosfero::Plugin.stubs(:all).returns(['PluginManagerTest::Plugin1', 'PluginManagerTest::Plugin2'])

    environment.enable_plugin(Plugin1)
    environment.enable_plugin(Plugin2)

    assert_equal 10, manager.pipeline(:transform, 1)
  end

  should 'raise if pipeline is broken' do
    class Plugin1 < Noosfero::Plugin
      def transform(v1, v2)
        v = 2
        [v1 * v, v2 * v]
      end
    end

    class Plugin2 < Noosfero::Plugin
      def transform(v1, v2)
        v = 5
        [v1 * v, v2 * v, 666]
      end
    end
    Noosfero::Plugin.stubs(:all).returns(['PluginManagerTest::Plugin1', 'PluginManagerTest::Plugin2'])

    environment.enable_plugin(Plugin1)
    environment.enable_plugin(Plugin2)

    assert_raise ArgumentError do
      manager.pipeline(:transform, 1, 2)
    end
  end

  should 'filter a property' do
    class Plugin1 < Noosfero::Plugin
      def invalid_numbers(numbers)
        numbers.reject {|n| n%2==0}
      end
    end

    class Plugin2 < Noosfero::Plugin
      def invalid_numbers(numbers)
        numbers.reject {|n| n<=5}
      end
    end
    Noosfero::Plugin.stubs(:all).returns(['PluginManagerTest::Plugin1', 'PluginManagerTest::Plugin2'])

    environment.enable_plugin(Plugin1)
    environment.enable_plugin(Plugin2)

    assert_equal [7,9], manager.filter(:invalid_numbers, [1,2,3,4,5,6,7,8,9,10])
  end

  should 'only call default if value is blank' do
    class Plugin1 < Noosfero::Plugin
      def find_by_contents asset, scope, query, paginate_options={}, options={}
        {results: [1,2,3]}
      end
    end
    Noosfero::Plugin.stubs(:all).returns(['PluginManagerTest::Plugin1'])
    environment.enable_plugin(Plugin1)

    Noosfero::Plugin.any_instance.expects(:find_by_contents).never
    @manager.dispatch_first :find_by_contents, :products, environment.products, 'product'
  end

  should 'not event if it is not defined by plugin' do
    class Noosfero::Plugin
      def never_call
        nil
      end
    end
    class Plugin1 < Noosfero::Plugin
      def never_call
        'defined'
      end
    end
    class Plugin2 < Noosfero::Plugin
    end
    Noosfero::Plugin.stubs(:all).returns(['PluginManagerTest::Plugin1', 'PluginManagerTest::Plugin2'])
    environment.enable_plugin(Plugin1)
    environment.enable_plugin(Plugin2)
    plugin1 = @manager.enabled_plugins.detect{ |p| p.is_a? Plugin1 }
    plugin2 = @manager.enabled_plugins.detect{ |p| p.is_a? Plugin2 }

    assert_equal Plugin1, Plugin1.new.method(:never_call).owner
    assert_equal Noosfero::Plugin, Plugin2.new.method(:never_call).owner
    # expects never can't be used as it defines the method
    @manager.expects(:result_for).with(plugin1, :never_call).returns(Plugin1.new.never_call)
    @manager.expects(:result_for).with(plugin2, :never_call).returns(nil)
    @manager.dispatch :never_call
  end

end