From 2402b6ba2d835672f9af80a98bac3e27ccdbff62 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Mon, 11 Nov 2013 14:31:15 -0300 Subject: [PATCH] Tests added to breadcrumbs block --- plugins/breadcrumbs/lib/breadcrumbs_block.rb | 26 +++++++++++++++++++++----- plugins/breadcrumbs/lib/breadcrumbs_plugin.rb | 2 +- plugins/breadcrumbs/test/functional/profile_design_controller_test.rb | 45 +++++++++++++++++++++++++++++++++++++++++++++ plugins/breadcrumbs/test/test_helper.rb | 1 + plugins/breadcrumbs/test/unit/breadcrumbs_block_test.rb | 71 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/breadcrumbs/test/unit/breadcrumbs_plugin_test.rb | 21 +++++++++++++++++++++ plugins/breadcrumbs/views/box_organizer/_breadcrumbs_block.rhtml | 4 ++++ plugins/breadcrumbs/views/profile_design | 1 + 8 files changed, 165 insertions(+), 6 deletions(-) create mode 100644 plugins/breadcrumbs/test/functional/profile_design_controller_test.rb create mode 100644 plugins/breadcrumbs/test/test_helper.rb create mode 100644 plugins/breadcrumbs/test/unit/breadcrumbs_block_test.rb create mode 100644 plugins/breadcrumbs/test/unit/breadcrumbs_plugin_test.rb create mode 100644 plugins/breadcrumbs/views/box_organizer/_breadcrumbs_block.rhtml create mode 120000 plugins/breadcrumbs/views/profile_design diff --git a/plugins/breadcrumbs/lib/breadcrumbs_block.rb b/plugins/breadcrumbs/lib/breadcrumbs_block.rb index 8cbcada..91a0822 100644 --- a/plugins/breadcrumbs/lib/breadcrumbs_block.rb +++ b/plugins/breadcrumbs/lib/breadcrumbs_block.rb @@ -11,25 +11,33 @@ class BreadcrumbsBlock < Block _('This block displays breadcrumb trail.') end - def trail(page, params={}) + def page_trail(page, params={}) links = [] if page links = page.ancestors.reverse.map { |p| { :name => p.title, :url => p.url } } links << { :name => page.title, :url => page.url } elsif params[:controller] == 'cms' id = params[:id] || params[:parent_id] - links = trail(Article.find(id)) if id - links << { :name => params[:action], :url => params } if show_cms_action + links = page_trail(Article.find(id)) if id + links << { :name => cms_action(params[:action]), :url => params } if show_cms_action end links end + def trail(page, profile=nil, params={}) + links = page_trail(page, params) + if profile && !links.empty? && show_profile + [ {:name => profile.name, :url => profile.url} ] + links + else + links + end + end + def content(args={}) block = self lambda do - trail = block.trail(@page, params) + trail = block.trail(@page, @profile, params) if !trail.empty? - trail = [ {:name => @profile.name, :url => @profile.url} ] + trail if block.show_profile trail.map { |t| link_to(t[:name], t[:url], :class => 'item') }.join(content_tag('span', ' > ', :class => 'separator')) else '' @@ -37,4 +45,12 @@ class BreadcrumbsBlock < Block end end + protected + + CMS_ACTIONS = {:edit => _('Edit'), :upload_files => _('Upload Files'), :new => _('New')} + + def cms_action(action) + CMS_ACTIONS[action.to_sym] || action + end + end diff --git a/plugins/breadcrumbs/lib/breadcrumbs_plugin.rb b/plugins/breadcrumbs/lib/breadcrumbs_plugin.rb index 6774c6e..40fd097 100644 --- a/plugins/breadcrumbs/lib/breadcrumbs_plugin.rb +++ b/plugins/breadcrumbs/lib/breadcrumbs_plugin.rb @@ -9,7 +9,7 @@ class BreadcrumbsPlugin < Noosfero::Plugin end def self.extra_blocks - { BreadcrumbsBlock => {} } + { BreadcrumbsBlock => {:type => [Community, Person, Enterprise] } } end end diff --git a/plugins/breadcrumbs/test/functional/profile_design_controller_test.rb b/plugins/breadcrumbs/test/functional/profile_design_controller_test.rb new file mode 100644 index 0000000..36a9957 --- /dev/null +++ b/plugins/breadcrumbs/test/functional/profile_design_controller_test.rb @@ -0,0 +1,45 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class ProfileDesignController + append_view_path File.join(File.dirname(__FILE__) + '/../../views') + def rescue_action(e) + raise e + end +end + +class ProfileDesignControllerTest < ActionController::TestCase + + def setup + Environment.delete_all + @environment = Environment.create(:name => 'testenv', :is_default => true) + @environment.enabled_plugins = ['BreadcrumbsPlugin'] + @environment.save! + + @profile = fast_create(Community, :environment_id => @environment.id) + @page = fast_create(Folder, :profile_id => @profile.id) + + box = Box.create!(:owner => @profile) + @block = BreadcrumbsBlock.create!(:box => box) + + user = create_user('testinguser') + @profile.add_admin(user.person) + login_as(user.login) + end + + should 'be able to edit breadcrumbs block' do + get :edit, :id => @block.id, :profile => @profile.identifier + assert_tag :tag => 'input', :attributes => { :id => 'block_title' } + assert_tag :tag => 'input', :attributes => { :id => 'block_show_cms_action' } + assert_tag :tag => 'input', :attributes => { :id => 'block_show_profile' } + end + + should 'be able to save breadcrumbs block' do + get :edit, :id => @block.id, :profile => @profile.identifier + post :save, :id => @block.id, :profile => @profile.identifier, :block => {:title => 'breadcrumbs', :show_cms_action => false, :show_profile => false} + @block.reload + assert_equal 'breadcrumbs', @block.title + assert !@block.show_profile + assert !@block.show_cms_action + end + +end diff --git a/plugins/breadcrumbs/test/test_helper.rb b/plugins/breadcrumbs/test/test_helper.rb new file mode 100644 index 0000000..cca1fd3 --- /dev/null +++ b/plugins/breadcrumbs/test/test_helper.rb @@ -0,0 +1 @@ +require File.dirname(__FILE__) + '/../../../test/test_helper' diff --git a/plugins/breadcrumbs/test/unit/breadcrumbs_block_test.rb b/plugins/breadcrumbs/test/unit/breadcrumbs_block_test.rb new file mode 100644 index 0000000..b49068d --- /dev/null +++ b/plugins/breadcrumbs/test/unit/breadcrumbs_block_test.rb @@ -0,0 +1,71 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class BreadcrumbsBlockTest < ActiveSupport::TestCase + + include NoosferoTestHelper + + def setup + @block = BreadcrumbsBlock.new + @profile = fast_create(Community) + @folder = fast_create(Folder, :profile_id => @profile.id) + @article = fast_create(Folder, :profile_id => @profile.id, :parent_id => @folder.id) + @params = {} + end + + attr_reader :params + + should 'has a description' do + assert_not_equal Block.description, BreadcrumbsBlock.description + end + + should 'has a help' do + assert @block.help + end + + should 'return path of links to reach a page' do + links = [{:name => @folder.name, :url => @folder.url}, {:name => @article.name, :url => @article.url}] + assert_equal links, @block.page_trail(@article) + end + + should 'return path of links when current page is at cms controller' do + params = {:controller => 'cms', :action => 'edit', :id => @article.id} + links = [{:name => @folder.name, :url => @folder.url}, {:name => @article.name, :url => @article.url}, {:url=>{:controller=>"cms", :action=>"edit", :id=>@article.id}, :name=>"Edit"}] + assert_equal links, @block.page_trail(nil, params) + end + + should 'not return cms action link when show_cms_action is false' do + params = {:controller => 'cms', :action => 'edit', :id => @article.id} + links = [{:name => @folder.name, :url => @folder.url}, {:name => @article.name, :url => @article.url}] + @block.show_cms_action = false + assert_equal links, @block.page_trail(nil, params) + end + + should 'include profile link on path of links to reach a page' do + links = [{:name => @profile.name, :url => @profile.url}, {:name => @folder.name, :url => @folder.url}, {:name => @article.name, :url => @article.url}] + assert_equal links, @block.trail(@article, @profile) + end + + should 'not include profile link on path of links when show_profile is false' do + links = [{:name => @folder.name, :url => @folder.url}, {:name => @article.name, :url => @article.url}] + @block.show_profile = false + assert_equal links, @block.trail(@article, @profile) + end + + should 'not include profile link on path of links when trail is empty' do + assert_equal [], @block.trail(nil, @profile) + end + + should 'render trail if there is links to show' do + @page = @article + trail = instance_eval(&@block.content) + assert_match /#{@profile.name}/, trail + assert_match /#{@folder.name}/, trail + assert_match /#{@page.name}/, trail + end + + should 'render nothing if there is no links to show' do + @page = nil + assert_equal '', instance_eval(&@block.content) + end + +end diff --git a/plugins/breadcrumbs/test/unit/breadcrumbs_plugin_test.rb b/plugins/breadcrumbs/test/unit/breadcrumbs_plugin_test.rb new file mode 100644 index 0000000..e7fcee7 --- /dev/null +++ b/plugins/breadcrumbs/test/unit/breadcrumbs_plugin_test.rb @@ -0,0 +1,21 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class BreadcrumbsPluginTest < ActiveSupport::TestCase + + def setup + @plugin = BreadcrumbsPlugin.new + end + + should 'has a name' do + assert !BreadcrumbsPlugin.plugin_name.blank? + end + + should 'has a description' do + assert !BreadcrumbsPlugin.plugin_description.blank? + end + + should 'add a block' do + assert_equal [BreadcrumbsBlock], BreadcrumbsPlugin.extra_blocks.keys + end + +end diff --git a/plugins/breadcrumbs/views/box_organizer/_breadcrumbs_block.rhtml b/plugins/breadcrumbs/views/box_organizer/_breadcrumbs_block.rhtml new file mode 100644 index 0000000..8692a79 --- /dev/null +++ b/plugins/breadcrumbs/views/box_organizer/_breadcrumbs_block.rhtml @@ -0,0 +1,4 @@ +
+ <%= labelled_form_field check_box(:block, :show_cms_action) + _('Show cms action'), '' %> + <%= labelled_form_field check_box(:block, :show_profile) + _('Show profile'), '' %> +
diff --git a/plugins/breadcrumbs/views/profile_design b/plugins/breadcrumbs/views/profile_design new file mode 120000 index 0000000..a75d184 --- /dev/null +++ b/plugins/breadcrumbs/views/profile_design @@ -0,0 +1 @@ +box_organizer \ No newline at end of file -- libgit2 0.21.2