diff --git a/app/controllers/admin/environment_design_controller.rb b/app/controllers/admin/environment_design_controller.rb index ab139e2..81bfb03 100644 --- a/app/controllers/admin/environment_design_controller.rb +++ b/app/controllers/admin/environment_design_controller.rb @@ -4,6 +4,7 @@ class EnvironmentDesignController < BoxOrganizerController def available_blocks @available_blocks ||= [ ArticleBlock, LoginBlock, EnvironmentStatisticsBlock, RecentDocumentsBlock, EnterprisesBlock, CommunitiesBlock, PeopleBlock, SellersSearchBlock, LinkListBlock, FeedReaderBlock, SlideshowBlock, HighlightsBlock, FeaturedProductsBlock, CategoriesBlock, RawHTMLBlock ] + @available_blocks = @available_blocks + plugins.dispatch(:extra_blocks, :type => Environment) end end diff --git a/app/controllers/box_organizer_controller.rb b/app/controllers/box_organizer_controller.rb index 254582f..20d1d86 100644 --- a/app/controllers/box_organizer_controller.rb +++ b/app/controllers/box_organizer_controller.rb @@ -68,8 +68,8 @@ class BoxOrganizerController < ApplicationController raise ArgumentError.new("Type %s is not allowed. Go away." % type) end else - @center_block_types = Box.acceptable_center_blocks & available_blocks - @side_block_types = Box.acceptable_side_blocks & available_blocks + @center_block_types = (Box.acceptable_center_blocks & available_blocks) + plugins.dispatch(:extra_blocks, :type => :all, :position => 1) + @side_block_types = (Box.acceptable_side_blocks & available_blocks) + plugins.dispatch(:extra_blocks, :type => :all, :position => [2,3]) @boxes = boxes_holder.boxes render :action => 'add_block', :layout => false end diff --git a/app/controllers/my_profile/profile_design_controller.rb b/app/controllers/my_profile/profile_design_controller.rb index 84cb852..06d96bd 100644 --- a/app/controllers/my_profile/profile_design_controller.rb +++ b/app/controllers/my_profile/profile_design_controller.rb @@ -7,6 +7,8 @@ class ProfileDesignController < BoxOrganizerController def available_blocks blocks = [ ArticleBlock, TagsBlock, RecentDocumentsBlock, ProfileInfoBlock, LinkListBlock, MyNetworkBlock, FeedReaderBlock, ProfileImageBlock, LocationBlock, SlideshowBlock, ProfileSearchBlock, HighlightsBlock ] + blocks = blocks + plugins.dispatch(:extra_blocks) + # blocks exclusive for organizations if profile.has_members? blocks << MembersBlock @@ -18,6 +20,12 @@ class ProfileDesignController < BoxOrganizerController blocks << FavoriteEnterprisesBlock blocks << CommunitiesBlock blocks << EnterprisesBlock + blocks = blocks + plugins.dispatch(:extra_blocks, :type => Person) + end + + # blocks exclusive to person + if profile.community? + blocks = blocks + plugins.dispatch(:extra_blocks, :type => Community) end # blocks exclusive for enterprises @@ -26,6 +34,7 @@ class ProfileDesignController < BoxOrganizerController blocks << HighlightsBlock blocks << FeaturedProductsBlock blocks << FansBlock + blocks = blocks + plugins.dispatch(:extra_blocks, :type => Enterprise) end # product block exclusive for enterprises in environments that permits it diff --git a/lib/noosfero/plugin.rb b/lib/noosfero/plugin.rb index e374a5d..b7cfc76 100644 --- a/lib/noosfero/plugin.rb +++ b/lib/noosfero/plugin.rb @@ -97,6 +97,35 @@ class Noosfero::Plugin ERB.new(File.read("#{views_path}/#{file_path}")).result(binding) end + def extra_blocks(params = {}) + return [] if self.class.extra_blocks.nil? + blocks = self.class.extra_blocks.map do |block, options| + type = options[:type] + type = type.is_a?(Array) ? type : [type].compact + type = type.map do |x| + x.is_a?(String) ? x.capitalize.constantize : x + end + raise "This is not a valid type" if !type.empty? && ![Person, Community, Enterprise, Environment].detect{|m| type.include?(m)} + + position = options[:position] + position = position.is_a?(Array) ? position : [position].compact + position = position.map{|p| p.to_i} + raise "This is not a valid position" if !position.empty? && ![1,2,3].detect{|m| position.include?(m)} + + if !type.empty? && (params[:type] != :all) + block = type.include?(params[:type]) ? block : nil + end + + if !position.empty? && !params[:position].nil? + block = position.detect{ |p| [params[:position]].flatten.include?(p)} ? block : nil + end + + block + end + blocks.compact! + blocks || [] + end + # Here the developer may specify the events to which the plugins can # register and must return true or false. The default value must be false. @@ -351,6 +380,60 @@ class Noosfero::Plugin nil end + # -> Adds additional blocks to profiles and environments. + # Your plugin must implements a class method called 'extra_blocks' + # that returns a hash with the following syntax. + # { + # 'block_name' => + # { + # :type => 'for which holder the block will be available', + # :position => 'where the block could be displayed' + # } + # } + # + # Where: + # + # - block_name: Name of the new block added to the blocks list + # - type: Could have some of the values + # - 'environment' or Environment: If the block is available only for Environment models + # - 'community' or Community: If the block is available only for Community models + # - 'enterprise' or Enterprise: If the block is available only for Enterprise models + # - 'person' or Person: If the block is available only for Person models + # - nil: If no type parameter is passed the block will be available for all types + # - position: Is the layout position of the block. It should be: + # - '1' or 1: Area 1 of layout + # - '2' or 2: Area 2 of layout + # - '3' or 3: Area 3 of layout + # - nil: If no position parameter is passed the block will be available for all positions + # + # OBS: Area 1 is where stay the main content of layout. Areas 2 and 3 are the sides of layout. + # + # examples: + # + # def self.extra_blocks(params) + # { + # #Display 'CustomBlock1' only for 'Person' on position '1' + # CustomBlock1 => {:type => 'person', :position => '1' }, + # + # #Display 'CustomBlock2' only for 'Community' on position '2' + # CustomBlock2 => {:type => Community, :position => '2' }, + # + # #Display 'CustomBlock3' only for 'Enterprise' on position '3' + # CustomBlock3 => {:type => 'enterprise', :position => 3 }, + # + # #Display 'CustomBlock2' for 'Environment' and 'Person' on positions '1' and '3' + # CustomBlock4 => {:type => ['environment', Person], :position => ['1','3'] }, + # + # #Display 'CustomBlock5' for all types and all positions + # CustomBlock5 => {}, + # } + # end + # + # OBS: The default value is a empty hash. + def self.extra_blocks + {} + end + def method_missing(method, *args, &block) # This is a generic hotspot for all controllers on Noosfero. # If any plugin wants to define filters to run on any controller, the name of diff --git a/test/functional/environment_design_controller_test.rb b/test/functional/environment_design_controller_test.rb index 58c3395..d1bdf14 100644 --- a/test/functional/environment_design_controller_test.rb +++ b/test/functional/environment_design_controller_test.rb @@ -12,6 +12,7 @@ class EnvironmentDesignControllerTest < ActionController::TestCase @controller = EnvironmentDesignController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([]) end def test_local_files_reference @@ -192,4 +193,131 @@ class EnvironmentDesignControllerTest < ActionController::TestCase assert_response :success assert_template 'add_block' end + + should 'a environment block plugin add new blocks for environments' do + class CustomBlock1 < Block; end; + + class TestBlockPlugin < Noosfero::Plugin + def self.extra_blocks + { + CustomBlock1 => {:type => Environment}, + } + end + end + + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestBlockPlugin.new]) + assert @controller.available_blocks.include?(CustomBlock1) + end + + should 'a person, enterprise and community blocks plugins do not add new blocks for environments' do + class CustomBlock1 < Block; end; + class CustomBlock2 < Block; end; + class CustomBlock3 < Block; end; + class CustomBlock4 < Block; end; + + class TestBlockPlugin < Noosfero::Plugin + def self.extra_blocks + { + CustomBlock1 => {:type => Environment}, + CustomBlock2 => {:type => Enterprise}, + CustomBlock3 => {:type => Community}, + CustomBlock4 => {:type => Person}, + } + end + end + + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestBlockPlugin.new]) + assert @controller.available_blocks.include?(CustomBlock1) + assert !@controller.available_blocks.include?(CustomBlock2) + assert !@controller.available_blocks.include?(CustomBlock3) + assert !@controller.available_blocks.include?(CustomBlock4) + end + + should 'a block plugin with center position add new blocks only in this position' 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 TestBlockPlugin < Noosfero::Plugin + def self.extra_blocks + { + CustomBlock1 => {:type => Person, :position => [1]}, + CustomBlock2 => {:type => Enterprise, :position => 1}, + CustomBlock3 => {:type => Community, :position => '1'}, + CustomBlock4 => {:type => Person, :position => [2]}, + CustomBlock5 => {:type => Enterprise, :position => 2}, + CustomBlock6 => {:type => Community, :position => '2'}, + CustomBlock7 => {:type => Person, :position => [3]}, + CustomBlock8 => {:type => Enterprise, :position => 3}, + CustomBlock9 => {:type => Community, :position => '3'}, + } + end + end + + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestBlockPlugin.new]) + login_as(create_admin_user(Environment.default)) + get :add_block + + assert_response :success + + assert @controller.instance_variable_get('@center_block_types').include?(CustomBlock1) + assert @controller.instance_variable_get('@center_block_types').include?(CustomBlock2) + assert @controller.instance_variable_get('@center_block_types').include?(CustomBlock3) + assert !@controller.instance_variable_get('@center_block_types').include?(CustomBlock4) + assert !@controller.instance_variable_get('@center_block_types').include?(CustomBlock5) + assert !@controller.instance_variable_get('@center_block_types').include?(CustomBlock6) + assert !@controller.instance_variable_get('@center_block_types').include?(CustomBlock7) + assert !@controller.instance_variable_get('@center_block_types').include?(CustomBlock8) + assert !@controller.instance_variable_get('@center_block_types').include?(CustomBlock9) + end + + should 'a block plugin with side position add new blocks only in this position' 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 TestBlockPlugin < Noosfero::Plugin + def self.extra_blocks + { + CustomBlock1 => {:type => Person, :position => [1]}, + CustomBlock2 => {:type => Enterprise, :position => 1}, + CustomBlock3 => {:type => Community, :position => '1'}, + CustomBlock4 => {:type => Person, :position => [2]}, + CustomBlock5 => {:type => Enterprise, :position => 2}, + CustomBlock6 => {:type => Community, :position => '2'}, + CustomBlock7 => {:type => Person, :position => [3]}, + CustomBlock8 => {:type => Enterprise, :position => 3}, + CustomBlock9 => {:type => Community, :position => '3'}, + } + end + end + + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestBlockPlugin.new]) + login_as(create_admin_user(Environment.default)) + get :add_block + assert_response :success + + assert !@controller.instance_variable_get('@side_block_types').include?(CustomBlock1) + assert !@controller.instance_variable_get('@side_block_types').include?(CustomBlock2) + assert !@controller.instance_variable_get('@side_block_types').include?(CustomBlock3) + assert @controller.instance_variable_get('@side_block_types').include?(CustomBlock4) + assert @controller.instance_variable_get('@side_block_types').include?(CustomBlock5) + assert @controller.instance_variable_get('@side_block_types').include?(CustomBlock6) + assert @controller.instance_variable_get('@side_block_types').include?(CustomBlock7) + assert @controller.instance_variable_get('@side_block_types').include?(CustomBlock8) + assert @controller.instance_variable_get('@side_block_types').include?(CustomBlock9) + end + end diff --git a/test/functional/profile_design_controller_test.rb b/test/functional/profile_design_controller_test.rb index 97866d4..e7b5227 100644 --- a/test/functional/profile_design_controller_test.rb +++ b/test/functional/profile_design_controller_test.rb @@ -177,6 +177,91 @@ class ProfileDesignControllerTest < ActionController::TestCase end end + should 'a block plugin with center position add new blocks only in this position' 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 TestBlockPlugin < Noosfero::Plugin + def self.extra_blocks + { + CustomBlock1 => {:type => Person, :position => [1]}, + CustomBlock2 => {:type => Enterprise, :position => 1}, + CustomBlock3 => {:type => Community, :position => '1'}, + CustomBlock4 => {:type => Person, :position => [2]}, + CustomBlock5 => {:type => Enterprise, :position => 2}, + CustomBlock6 => {:type => Community, :position => '2'}, + CustomBlock7 => {:type => Person, :position => [3]}, + CustomBlock8 => {:type => Enterprise, :position => 3}, + CustomBlock9 => {:type => Community, :position => '3'}, + } + end + end + + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestBlockPlugin.new]) + get :add_block, :profile => 'designtestuser' + assert_response :success + + assert @controller.instance_variable_get('@center_block_types').include?(CustomBlock1) + assert @controller.instance_variable_get('@center_block_types').include?(CustomBlock2) + assert @controller.instance_variable_get('@center_block_types').include?(CustomBlock3) + assert !@controller.instance_variable_get('@center_block_types').include?(CustomBlock4) + assert !@controller.instance_variable_get('@center_block_types').include?(CustomBlock5) + assert !@controller.instance_variable_get('@center_block_types').include?(CustomBlock6) + assert !@controller.instance_variable_get('@center_block_types').include?(CustomBlock7) + assert !@controller.instance_variable_get('@center_block_types').include?(CustomBlock8) + assert !@controller.instance_variable_get('@center_block_types').include?(CustomBlock9) + end + + should 'a block plugin with side position add new blocks only in this position' 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 TestBlockPlugin < Noosfero::Plugin + def self.extra_blocks + { + CustomBlock1 => {:type => Person, :position => [1]}, + CustomBlock2 => {:type => Enterprise, :position => 1}, + CustomBlock3 => {:type => Community, :position => '1'}, + CustomBlock4 => {:type => Person, :position => [2]}, + CustomBlock5 => {:type => Enterprise, :position => 2}, + CustomBlock6 => {:type => Community, :position => '2'}, + CustomBlock7 => {:type => Person, :position => [3]}, + CustomBlock8 => {:type => Enterprise, :position => 3}, + CustomBlock9 => {:type => Community, :position => '3'}, + } + end + end + + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestBlockPlugin.new]) + get :add_block, :profile => 'designtestuser' + assert_response :success + + assert !@controller.instance_variable_get('@side_block_types').include?(CustomBlock1) + assert !@controller.instance_variable_get('@side_block_types').include?(CustomBlock2) + assert !@controller.instance_variable_get('@side_block_types').include?(CustomBlock3) + assert @controller.instance_variable_get('@side_block_types').include?(CustomBlock4) + assert @controller.instance_variable_get('@side_block_types').include?(CustomBlock5) + assert @controller.instance_variable_get('@side_block_types').include?(CustomBlock6) + assert @controller.instance_variable_get('@side_block_types').include?(CustomBlock7) + assert @controller.instance_variable_get('@side_block_types').include?(CustomBlock8) + assert @controller.instance_variable_get('@side_block_types').include?(CustomBlock9) + end + + ###################################################### # END - tests for BoxOrganizerController features ###################################################### @@ -349,6 +434,7 @@ class ProfileDesignControllerTest < ActionController::TestCase profile = mock profile.stubs(:has_members?).returns(false) profile.stubs(:person?).returns(true) + profile.stubs(:community?).returns(true) profile.stubs(:enterprise?).returns(false) profile.stubs(:has_blog?).returns(false) profile.stubs(:is_admin?).with(anything).returns(false) @@ -357,6 +443,7 @@ class ProfileDesignControllerTest < ActionController::TestCase environment.stubs(:enabled?).returns(false) @controller.stubs(:profile).returns(profile) @controller.stubs(:user).returns(profile) + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([]) assert_equal PERSON_BLOCKS, @controller.available_blocks end @@ -364,6 +451,7 @@ class ProfileDesignControllerTest < ActionController::TestCase profile = mock profile.stubs(:has_members?).returns(true) profile.stubs(:person?).returns(true) + profile.stubs(:community?).returns(true) profile.stubs(:enterprise?).returns(false) profile.stubs(:has_blog?).returns(false) profile.stubs(:is_admin?).with(anything).returns(false) @@ -372,6 +460,7 @@ class ProfileDesignControllerTest < ActionController::TestCase environment.stubs(:enabled?).returns(false) @controller.stubs(:profile).returns(profile) @controller.stubs(:user).returns(profile) + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([]) assert_equal [], @controller.available_blocks - PERSON_BLOCKS_WITH_MEMBERS end @@ -379,6 +468,7 @@ class ProfileDesignControllerTest < ActionController::TestCase profile = mock profile.stubs(:has_members?).returns(false) profile.stubs(:person?).returns(true) + profile.stubs(:community?).returns(true) profile.stubs(:enterprise?).returns(false) profile.stubs(:has_blog?).returns(true) profile.stubs(:is_admin?).with(anything).returns(false) @@ -387,6 +477,7 @@ class ProfileDesignControllerTest < ActionController::TestCase environment.stubs(:enabled?).returns(false) @controller.stubs(:profile).returns(profile) @controller.stubs(:user).returns(profile) + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([]) assert_equal [], @controller.available_blocks - PERSON_BLOCKS_WITH_BLOG end @@ -394,6 +485,7 @@ class ProfileDesignControllerTest < ActionController::TestCase profile = mock profile.stubs(:has_members?).returns(false) profile.stubs(:person?).returns(false) + profile.stubs(:community?).returns(true) profile.stubs(:enterprise?).returns(true) profile.stubs(:has_blog?).returns(false) profile.stubs(:is_admin?).with(anything).returns(false) @@ -402,6 +494,7 @@ class ProfileDesignControllerTest < ActionController::TestCase environment.stubs(:enabled?).returns(true) @controller.stubs(:profile).returns(profile) @controller.stubs(:user).returns(profile) + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([]) assert_equal [], @controller.available_blocks - ENTERPRISE_BLOCKS end @@ -409,6 +502,7 @@ class ProfileDesignControllerTest < ActionController::TestCase profile = mock profile.stubs(:has_members?).returns(false) profile.stubs(:person?).returns(false) + profile.stubs(:community?).returns(true) profile.stubs(:enterprise?).returns(true) profile.stubs(:has_blog?).returns(false) profile.stubs(:is_admin?).with(anything).returns(false) @@ -417,6 +511,7 @@ class ProfileDesignControllerTest < ActionController::TestCase environment.stubs(:enabled?).returns(false) @controller.stubs(:profile).returns(profile) @controller.stubs(:user).returns(profile) + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([]) assert_equal [], @controller.available_blocks - ENTERPRISE_BLOCKS_WITH_PRODUCTS_ENABLE end @@ -441,4 +536,144 @@ class ProfileDesignControllerTest < ActionController::TestCase assert_tag :tag => 'option', :attributes => {:value => selected_article.id, :selected => 'selected'} end + should 'the block plugin add a new block' do + profile = mock + profile.stubs(:has_members?).returns(false) + profile.stubs(:person?).returns(true) + profile.stubs(:community?).returns(true) + profile.stubs(:enterprise?).returns(false) + profile.stubs(:has_blog?).returns(false) + profile.stubs(:is_admin?).with(anything).returns(false) + environment = mock + profile.stubs(:environment).returns(environment) + environment.stubs(:enabled?).returns(false) + @controller.stubs(:profile).returns(profile) + @controller.stubs(:user).returns(profile) + + class CustomBlock1 < Block; end; + + class TestBlockPlugin < Noosfero::Plugin + def self.extra_blocks + { + CustomBlock1 => {}, + } + end + end + + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestBlockPlugin.new]) + assert @controller.available_blocks.include?(CustomBlock1) + end + + should 'a person block plugin add new blocks for person profile' do + profile = mock + profile.stubs(:has_members?).returns(false) + profile.stubs(:person?).returns(true) + profile.stubs(:community?).returns(false) + profile.stubs(:enterprise?).returns(false) + profile.stubs(:has_blog?).returns(false) + profile.stubs(:is_admin?).with(anything).returns(false) + environment = mock + profile.stubs(:environment).returns(environment) + environment.stubs(:enabled?).returns(false) + @controller.stubs(:profile).returns(profile) + @controller.stubs(:user).returns(profile) + + class CustomBlock1 < Block; end; + + class TestBlockPlugin < Noosfero::Plugin + def self.extra_blocks + { + CustomBlock1 => {:type => Person}, + } + end + end + + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestBlockPlugin.new]) + assert @controller.available_blocks.include?(CustomBlock1) + end + + should 'a community block plugin add new blocks for community profile' do + profile = mock + profile.stubs(:has_members?).returns(false) + profile.stubs(:person?).returns(false) + profile.stubs(:community?).returns(true) + profile.stubs(:enterprise?).returns(false) + profile.stubs(:has_blog?).returns(false) + profile.stubs(:is_admin?).with(anything).returns(false) + environment = mock + profile.stubs(:environment).returns(environment) + environment.stubs(:enabled?).returns(false) + @controller.stubs(:profile).returns(profile) + @controller.stubs(:user).returns(profile) + + class CustomBlock1 < Block; end; + + class TestBlockPlugin < Noosfero::Plugin + def self.extra_blocks + { + CustomBlock1 => {:type => Community}, + } + end + end + + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestBlockPlugin.new]) + assert @controller.available_blocks.include?(CustomBlock1) + end + + should 'a enterprise block plugin add new blocks for enterprise profile' do + profile = mock + profile.stubs(:has_members?).returns(false) + profile.stubs(:person?).returns(false) + profile.stubs(:community?).returns(false) + profile.stubs(:enterprise?).returns(true) + profile.stubs(:has_blog?).returns(false) + profile.stubs(:is_admin?).with(anything).returns(false) + environment = mock + profile.stubs(:environment).returns(environment) + environment.stubs(:enabled?).returns(false) + @controller.stubs(:profile).returns(profile) + @controller.stubs(:user).returns(profile) + + class CustomBlock1 < Block; end; + + class TestBlockPlugin < Noosfero::Plugin + def self.extra_blocks + { + CustomBlock1 => {:type => Enterprise}, + } + end + end + + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestBlockPlugin.new]) + assert @controller.available_blocks.include?(CustomBlock1) + end + + should 'an environment block plugin not add new blocks for enterprise, person or community profiles' do + profile = mock + profile.stubs(:has_members?).returns(false) + profile.stubs(:person?).returns(true) + profile.stubs(:community?).returns(true) + profile.stubs(:enterprise?).returns(true) + profile.stubs(:has_blog?).returns(false) + profile.stubs(:is_admin?).with(anything).returns(false) + environment = mock + profile.stubs(:environment).returns(environment) + environment.stubs(:enabled?).returns(false) + @controller.stubs(:profile).returns(profile) + @controller.stubs(:user).returns(profile) + + class CustomBlock1 < Block; end; + + class TestBlockPlugin < Noosfero::Plugin + def self.extra_blocks + { + CustomBlock1 => {:type => Environment}, + } + end + end + + Noosfero::Plugin::Manager.any_instance.stubs(:enabled_plugins).returns([TestBlockPlugin.new]) + assert !@controller.available_blocks.include?(CustomBlock1) + end + end diff --git a/test/unit/plugin_test.rb b/test/unit/plugin_test.rb index b16e4cc..dc2441b 100644 --- a/test/unit/plugin_test.rb +++ b/test/unit/plugin_test.rb @@ -26,4 +26,476 @@ class PluginTest < ActiveSupport::TestCase 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.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 + assert_equal([], p.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 + assert_equal([], p.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 + assert_equal([], [CustomBlock1, CustomBlock2] - p.extra_blocks) + assert_equal([], p.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 + assert_equal([], [CustomBlock1, CustomBlock2] - p.extra_blocks(:type => Person)) + assert_equal([], p.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 + assert_equal([], [CustomBlock1, CustomBlock2] - p.extra_blocks(:type => Community)) + assert_equal([], p.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 + assert_equal([], [CustomBlock1, CustomBlock2] - p.extra_blocks(:type => Enterprise)) + assert_equal([], p.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 + assert_equal([], [CustomBlock1, CustomBlock2] - p.extra_blocks(:type => Environment)) + assert_equal([], p.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 + assert_equal([], [CustomBlock1, CustomBlock2] - p.extra_blocks(:type => Person)) + assert_equal([], p.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 + assert_equal([], [CustomBlock5, CustomBlock6] - p.extra_blocks) + assert_equal([], p.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 + assert_equal([], [CustomBlock1, CustomBlock2, CustomBlock3, CustomBlock4, CustomBlock5, CustomBlock6] - p.extra_blocks(:type => :all)) + assert_equal([], p.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 + assert_equal([], [CustomBlock1, CustomBlock2] - p.extra_blocks(:type => Environment)) + assert_equal([], p.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 + assert_equal([], [CustomBlock1, CustomBlock2, CustomBlock3, CustomBlock4] - p.extra_blocks(:type => Environment)) + assert_equal([], p.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 + + assert_raise NameError do + p.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 + assert_equal([], [CustomBlock1, CustomBlock2] - p.extra_blocks(:position => 1)) + assert_equal([], p.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 + assert_equal([], [CustomBlock1, CustomBlock2] - p.extra_blocks(:position => 2)) + assert_equal([], p.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 + assert_equal([], [CustomBlock1, CustomBlock2] - p.extra_blocks(:position => 3)) + assert_equal([], p.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 + assert_equal([], [CustomBlock1, CustomBlock2] - p.extra_blocks(:position => 1)) + assert_equal([], p.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 + assert_equal([], [CustomBlock5, CustomBlock10] - p.extra_blocks(:position => 1)) + assert_equal([], p.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 + assert_equal([], [CustomBlock1, CustomBlock2, CustomBlock3, CustomBlock4] - p.extra_blocks) + assert_equal([], p.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 + assert_equal([], [CustomBlock1, CustomBlock2] - p.extra_blocks(:position => 2)) + assert_equal([], p.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 + assert_equal([], [CustomBlock1, CustomBlock2, CustomBlock3, CustomBlock4] - p.extra_blocks(:position => 2)) + assert_equal([], p.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 + + assert_raise NameError do + p.extra_blocks + end + end + + + + + end -- libgit2 0.21.2