From d07031fe9e22764559f563d67be1b3fae300d14e Mon Sep 17 00:00:00 2001 From: Leandro Nunes dos Santos Date: Tue, 7 Apr 2015 10:56:04 -0300 Subject: [PATCH] Refactoring article extra toolbar buttons --- app/helpers/plugins_helper.rb | 7 +++++++ app/views/content_viewer/_article_toolbar.html.erb | 3 +-- lib/noosfero/plugin.rb | 2 ++ test/functional/content_viewer_controller_test.rb | 31 +++++++++++++++++++++++++++---- test/unit/plugin_test.rb | 2 -- test/unit/plugins_helper_test.rb | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 84 insertions(+), 8 deletions(-) create mode 100644 test/unit/plugins_helper_test.rb diff --git a/app/helpers/plugins_helper.rb b/app/helpers/plugins_helper.rb index 503c791..4b3074a 100644 --- a/app/helpers/plugins_helper.rb +++ b/app/helpers/plugins_helper.rb @@ -6,4 +6,11 @@ module PluginsHelper end end + def plugins_toolbar_actions_for_article(article) + toolbar_actions = Array.wrap(@plugins.dispatch(:article_extra_toolbar_buttons, article)) + toolbar_actions.each do |action| + [:title, :url, :icon].each { |param| raise "No #{param} was passed as parameter for #{action}" unless action.has_key?(param) } + end + end + end diff --git a/app/views/content_viewer/_article_toolbar.html.erb b/app/views/content_viewer/_article_toolbar.html.erb index e157fd4..be22603 100644 --- a/app/views/content_viewer/_article_toolbar.html.erb +++ b/app/views/content_viewer/_article_toolbar.html.erb @@ -46,11 +46,10 @@ <%= button(:clock, _('All versions'), {:controller => 'content_viewer', :profile => profile.identifier, :action => 'article_versions'}, :id => 'article-versions-link') %> <% end %> - <% @plugins.dispatch(:article_extra_toolbar_buttons, @page).each do |plugin_button| %> + <% plugins_toolbar_actions_for_article(@page).each do |plugin_button| %> <%= button plugin_button[:icon], plugin_button[:title], plugin_button[:url], plugin_button[:html_options] %> <% end %> - <%= report_abuse(profile, :link, @page) %> diff --git a/lib/noosfero/plugin.rb b/lib/noosfero/plugin.rb index d8fa600..1426652 100644 --- a/lib/noosfero/plugin.rb +++ b/lib/noosfero/plugin.rb @@ -687,6 +687,8 @@ class Noosfero::Plugin # returns = string with reason of expiration elsif method.to_s =~ /^content_expire_(#{content_actions.join('|')})$/ nil + elsif context.respond_to?(method) + context.send(method) else super end diff --git a/test/functional/content_viewer_controller_test.rb b/test/functional/content_viewer_controller_test.rb index d291c90..bf8fb9d 100644 --- a/test/functional/content_viewer_controller_test.rb +++ b/test/functional/content_viewer_controller_test.rb @@ -1479,7 +1479,7 @@ class ContentViewerControllerTest < ActionController::TestCase should 'add icon attribute in extra toolbar actions on article from plugins' do class Plugin1 < Noosfero::Plugin def article_extra_toolbar_buttons(article) - {:title => 'some_title1', :icon => 'some_icon1', :url => {}} + {:title => 'some_title', :icon => 'some_icon', :url => {}} end end Noosfero::Plugin.stubs(:all).returns([Plugin1.name]) @@ -1489,13 +1489,33 @@ class ContentViewerControllerTest < ActionController::TestCase page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text') get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ] - assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :class => /some_icon1/ }} + assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :class => /some_icon/ }} end should 'add url attribute in extra toolbar actions on article from plugins' do class Plugin1 < Noosfero::Plugin def article_extra_toolbar_buttons(article) - {:title => 'some_title1', :icon => 'some_icon1', :url => '/bli'} + {:title => 'some_title', :icon => 'some_icon', :url => '/someurl'} + end + end + Noosfero::Plugin.stubs(:all).returns([Plugin1.name]) + + Environment.default.enable_plugin(Plugin1.name) + + page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text') + + get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ] + assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/someurl" }} + end + + should 'use context method in extra toolbar actions on article from plugins' do + class Plugin1 < Noosfero::Plugin + def article_extra_toolbar_buttons(article) + if current_person.public? + {:title => 'some_title', :icon => 'some_icon', :url => '/someurl'} + else + {:title => 'another_title', :icon => 'another_icon', :url => '/anotherurl'} + end end end Noosfero::Plugin.stubs(:all).returns([Plugin1.name]) @@ -1504,8 +1524,11 @@ class ContentViewerControllerTest < ActionController::TestCase page = profile.articles.create!(:name => 'myarticle', :body => 'the body of the text') + profile.public_profile = false + profile.save + login_as(profile.identifier) get :view_page, :profile => profile.identifier, :page => [ 'myarticle' ] - assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/bli" }} + assert_tag :tag => 'div', :attributes => { :id => 'article-actions' }, :descendant => { :tag => 'a', :attributes => { :href => "/anotherurl" }} end end diff --git a/test/unit/plugin_test.rb b/test/unit/plugin_test.rb index 460ca26..ecf08ea 100644 --- a/test/unit/plugin_test.rb +++ b/test/unit/plugin_test.rb @@ -561,8 +561,6 @@ class PluginTest < ActiveSupport::TestCase end should 'article_extra_toolbar_buttons return an empty array by default' do - class CustomBlock1 < Block; end; - class Plugin1 < Noosfero::Plugin end p = Plugin1.new diff --git a/test/unit/plugins_helper_test.rb b/test/unit/plugins_helper_test.rb new file mode 100644 index 0000000..fcf7dce --- /dev/null +++ b/test/unit/plugins_helper_test.rb @@ -0,0 +1,47 @@ +require_relative "../test_helper" + +class PluginsHelperTest < ActionView::TestCase + + def setup + @environment = Environment.default + @plugins = mock + end + + attr_accessor :environment, :plugins + + should 'plugins_toolbar_actions_for_article return an array if the plugin return a single hash' do + hash = {:title => 'some title', :url => 'some_url', :icon => 'some icon'} + plugins.expects(:dispatch).with(:article_extra_toolbar_buttons, nil).returns(hash) + assert_equal [hash], plugins_toolbar_actions_for_article(nil) + end + + should 'plugins_toolbar_actions_for_article return an empty array if an array is passed as parameter' do + plugins.expects(:dispatch).with(:article_extra_toolbar_buttons, nil).returns([]) + assert_equal [], plugins_toolbar_actions_for_article(nil) + end + + should 'plugins_toolbar_actions_for_article throw raise if no title is passed as parameter' do + plugins.expects(:dispatch).with(:article_extra_toolbar_buttons, nil).returns({:url => 'some_url', :icon => 'some icon'}) + + assert_raise(RuntimeError) do + plugins_toolbar_actions_for_article(nil) + end + end + + should 'plugins_toolbar_actions_for_article throw raise if no icon is passed as parameter' do + plugins.expects(:dispatch).with(:article_extra_toolbar_buttons, nil).returns({:title => 'some title', :url => 'some_url'}) + + assert_raise(RuntimeError) do + plugins_toolbar_actions_for_article(nil) + end + end + + should 'plugins_toolbar_actions_for_article throw raise if no url is passed as parameter' do + plugins.expects(:dispatch).with(:article_extra_toolbar_buttons, nil).returns({:title => 'some title', :icon => 'some icon'}) + + assert_raise(RuntimeError) do + plugins_toolbar_actions_for_article(nil) + end + end + +end -- libgit2 0.21.2