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,7 +4,7 @@ class RelevantContentPlugin::RelevantContentBlock < Block
4 end 4 end
5 5
6 def default_title 6 def default_title
7 - _('Relevant content') 7 + _('Relevant content')
8 end 8 end
9 9
10 def help 10 def help
@@ -53,7 +53,7 @@ class RelevantContentPlugin::RelevantContentBlock < Block @@ -53,7 +53,7 @@ class RelevantContentPlugin::RelevantContentBlock < Block
53 env = owner.environment 53 env = owner.environment
54 end 54 end
55 55
56 - if env.plugin_enabled?(VotePlugin) 56 + if env.plugin_enabled?('VotePlugin')
57 if self.show_most_liked 57 if self.show_most_liked
58 docs = Article.more_positive_votes(owner, self.limit) 58 docs = Article.more_positive_votes(owner, self.limit)
59 if !docs.blank? 59 if !docs.blank?
plugins/relevant_content/test/unit/article.rb
@@ -20,10 +20,10 @@ class RelevantContentBlockTest < ActiveSupport::TestCase @@ -20,10 +20,10 @@ class RelevantContentBlockTest < ActiveSupport::TestCase
20 20
21 def enable_vote_plugin 21 def enable_vote_plugin
22 enabled = false 22 enabled = false
23 - environment=Environment.default 23 + environment = Environment.default
24 if Noosfero::Plugin.all.include?('VotePlugin') 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 environment.save! 27 environment.save!
28 end 28 end
29 enabled = true 29 enabled = true
@@ -145,4 +145,4 @@ class RelevantContentBlockTest < ActiveSupport::TestCase @@ -145,4 +145,4 @@ class RelevantContentBlockTest < ActiveSupport::TestCase
145 assert_equal '23 votes for 29 votes against', articles.first.name 145 assert_equal '23 votes for 29 votes against', articles.first.name
146 assert_equal '2 votes against', articles.last.name 146 assert_equal '2 votes against', articles.last.name
147 end 147 end
148 -end  
149 \ No newline at end of file 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,4 +44,19 @@ class RelevantContentBlockTest < ActiveSupport::TestCase
44 assert_equal RelevantContentPlugin::RelevantContentBlock.expire_on, {:environment=>[:article], :profile=>[:article]} 44 assert_equal RelevantContentPlugin::RelevantContentBlock.expire_on, {:environment=>[:article], :profile=>[:article]}
45 end 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 end 62 end