Commit 052b0480b3b66187166b3454b76218c0361f8417

Authored by Larissa Reis
1 parent afbebeee

[relevant-content-plugin] Fixes crash when VotePlugin is not found

  When vote plugin is not enabled in a noosfero instance, the constant
  VotePlugin is not defined, which was causing a crash in relevant
  content plugin. So I check if the plugin is enabled using a string and
  not a constant.
plugins/relevant_content/lib/relevant_content_plugin/relevant_content_block.rb
... ... @@ -4,7 +4,7 @@ class RelevantContentPlugin::RelevantContentBlock < Block
4 4 end
5 5  
6 6 def default_title
7   - _('Relevant content')
  7 + _('Relevant content')
8 8 end
9 9  
10 10 def help
... ... @@ -53,7 +53,7 @@ class RelevantContentPlugin::RelevantContentBlock < Block
53 53 env = owner.environment
54 54 end
55 55  
56   - if env.plugin_enabled?(VotePlugin)
  56 + if env.plugin_enabled?('VotePlugin')
57 57 if self.show_most_liked
58 58 docs = Article.more_positive_votes(owner, self.limit)
59 59 if !docs.blank?
... ...
plugins/relevant_content/test/unit/article.rb
... ... @@ -20,10 +20,10 @@ class RelevantContentBlockTest < ActiveSupport::TestCase
20 20  
21 21 def enable_vote_plugin
22 22 enabled = false
23   - environment=Environment.default
  23 + environment = Environment.default
24 24 if Noosfero::Plugin.all.include?('VotePlugin')
25   - if not environment.enabled_plugins.include?(:vote)
26   - environment.enable_plugin(Vote)
  25 + if not environment.enabled_plugins.include?('VotePlugin')
  26 + environment.enable_plugin(VotePlugin)
27 27 environment.save!
28 28 end
29 29 enabled = true
... ... @@ -145,4 +145,4 @@ class RelevantContentBlockTest < ActiveSupport::TestCase
145 145 assert_equal '23 votes for 29 votes against', articles.first.name
146 146 assert_equal '2 votes against', articles.last.name
147 147 end
148   -end
149 148 \ No newline at end of file
  149 +end
... ...
plugins/relevant_content/test/unit/relevant_content_block_test.rb
... ... @@ -44,4 +44,19 @@ class RelevantContentBlockTest < ActiveSupport::TestCase
44 44 assert_equal RelevantContentPlugin::RelevantContentBlock.expire_on, {:environment=>[:article], :profile=>[:article]}
45 45 end
46 46  
  47 + should 'not crash if vote plugin is not found' do
  48 + box = fast_create(Box, :owner_id => @profile.id, :owner_type => 'Profile')
  49 + block = RelevantContentPlugin::RelevantContentBlock.new(:box => box)
  50 +
  51 + Environment.any_instance.stubs(:enabled_plugins).returns(['RelevantContent'])
  52 + # When the plugin is disabled from noosfero instance, its constant name is
  53 + # undefined. To test this case, I have to manually undefine the constant
  54 + # if necessary.
  55 + Object.send(:remove_const, VotePlugin.to_s) if defined? VotePlugin
  56 +
  57 + assert_nothing_raised do
  58 + block.content
  59 + end
  60 + end
  61 +
47 62 end
... ...