From 3a4726cbdc0d4c2337e368f1c2e6db0940fc6879 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Fri, 8 Nov 2013 12:05:02 -0300 Subject: [PATCH] Functional tests for context content block --- plugins/context_content/controllers/profile/context_content_plugin_profile_controller.rb | 20 ++++++++++++-------- plugins/context_content/lib/context_content_block.rb | 4 ++-- plugins/context_content/test/functional/content_viewer_controller_test.rb | 46 ++++++++++++++++++++++++++++++++++++++++++++++ plugins/context_content/test/functional/context_content_plugin_profile_controller_test.rb | 43 +++++++++++++++++++++++++++++++++++++++++++ plugins/context_content/test/functional/profile_design_controller_test.rb | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/context_content/views/blocks/_more.rhtml | 4 ++-- 6 files changed, 159 insertions(+), 12 deletions(-) create mode 100644 plugins/context_content/test/functional/content_viewer_controller_test.rb create mode 100644 plugins/context_content/test/functional/context_content_plugin_profile_controller_test.rb create mode 100644 plugins/context_content/test/functional/profile_design_controller_test.rb diff --git a/plugins/context_content/controllers/profile/context_content_plugin_profile_controller.rb b/plugins/context_content/controllers/profile/context_content_plugin_profile_controller.rb index f0cd8ec..b7bedd5 100644 --- a/plugins/context_content/controllers/profile/context_content_plugin_profile_controller.rb +++ b/plugins/context_content/controllers/profile/context_content_plugin_profile_controller.rb @@ -2,14 +2,18 @@ class ContextContentPluginProfileController < ProfileController append_view_path File.join(File.dirname(__FILE__) + '/../../views') def view_content - block = Block.find(params[:id]) - p = params[:page].to_i - contents = block.contents(profile.articles.find(params[:article_id]), p) - - render :update do |page| - page.replace_html "context_content_#{block.id}", :file => "blocks/context_content", :locals => {:block => block, :contents => contents} - page.replace_html "context_content_more_#{block.id}", :partial => 'blocks/more', :locals => {:block => block, :contents => contents, :article_id => params[:article_id] } - end + block = Block.find(params[:id]) + p = params[:page].to_i + contents = block.contents(profile.articles.find(params[:article_id]), p) + + if contents + render :update do |page| + page.replace_html "context_content_#{block.id}", :file => "blocks/context_content", :locals => {:block => block, :contents => contents} + page.replace_html "context_content_more_#{block.id}", :partial => 'blocks/more', :locals => {:block => block, :contents => contents, :article_id => params[:article_id] } + end + else + render :text => "invalid page", :status => 500 + end end end diff --git a/plugins/context_content/lib/context_content_block.rb b/plugins/context_content/lib/context_content_block.rb index 9c80279..87068b4 100644 --- a/plugins/context_content/lib/context_content_block.rb +++ b/plugins/context_content/lib/context_content_block.rb @@ -50,8 +50,8 @@ class ContextContentBlock < Block def footer block = self lambda do - if @page - contents = block.contents(@page) + contents = block.contents(@page) + if contents content_tag('div', render(:partial => 'blocks/more', :locals => {:block => block, :contents => contents, :article_id => @page.id}), :id => "context_content_more_#{block.id}", :class => "more_button") else diff --git a/plugins/context_content/test/functional/content_viewer_controller_test.rb b/plugins/context_content/test/functional/content_viewer_controller_test.rb new file mode 100644 index 0000000..5487a86 --- /dev/null +++ b/plugins/context_content/test/functional/content_viewer_controller_test.rb @@ -0,0 +1,46 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class ContentViewerController + append_view_path File.join(File.dirname(__FILE__) + '/../../views') + def rescue_action(e) + raise e + end +end + +class ContentViewerControllerTest < ActionController::TestCase + + def setup + @profile = fast_create(Community) + @page = fast_create(Folder, :profile_id => @profile.id) + + box = Box.create!(:owner => @profile) + @block = ContextContentBlock.new(:box => box) + @block.types = ['TinyMceArticle'] + @block.limit = 1 + @block.save! + end + + should 'do not display context content block if it has no contents' do + get :view_page, @page.url + assert_no_tag 'div', :attributes => {:id => "context_content_#{@block.id}", :class => 'contents'} + assert_no_tag 'div', :attributes => {:id => "context_content_more_#{@block.id}", :class => 'more_button'} + end + + should 'display context content block if it has contents' do + article = fast_create(TinyMceArticle, :parent_id => @page.id, :profile_id => @profile.id, :name => 'article1') + get :view_page, @page.url + assert_tag 'div', :attributes => {:id => "context_content_#{@block.id}", :class => 'contents'} + assert_no_tag 'div', :attributes => {:id => "context_content_more_#{@block.id}", :class => 'more_button'}, :descendant => {:tag => 'a'} + assert_match /article1/, @response.body + end + + should 'display context content block with pagination' do + article1 = fast_create(TinyMceArticle, :parent_id => @page.id, :profile_id => @profile.id) + article2 = fast_create(TinyMceArticle, :parent_id => @page.id, :profile_id => @profile.id) + get :view_page, @page.url + assert_tag 'div', :attributes => {:id => "context_content_#{@block.id}", :class => 'contents'} + assert_tag 'div', :attributes => {:id => "context_content_more_#{@block.id}", :class => 'more_button'}, :descendant => {:tag => 'a', :attributes => {:class => 'button icon-button icon-left disabled'} } + assert_tag 'div', :attributes => {:id => "context_content_more_#{@block.id}", :class => 'more_button'}, :descendant => {:tag => 'a', :attributes => {:class => 'button icon-button icon-right'} } + end + +end diff --git a/plugins/context_content/test/functional/context_content_plugin_profile_controller_test.rb b/plugins/context_content/test/functional/context_content_plugin_profile_controller_test.rb new file mode 100644 index 0000000..43509be --- /dev/null +++ b/plugins/context_content/test/functional/context_content_plugin_profile_controller_test.rb @@ -0,0 +1,43 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class ContextContentPluginProfileControllerTest < ActionController::TestCase + + class ContextContentPluginProfileController; def rescue_action(e) raise e end; end + + def setup + @profile = fast_create(Community) + @block = ContextContentBlock.new + @block.types = ['TinyMceArticle'] + @block.limit = 1 + @block.save! + @page = fast_create(Folder, :profile_id => @profile.id) + end + + should 'render response error if contents is nil' do + xhr :get, :view_content, :id => @block.id, :article_id => @page.id, :page => 1, :profile => @profile.identifier + assert_response 500 + end + + should 'render error if page do not exists' do + article = fast_create(TinyMceArticle, :parent_id => @page.id, :profile_id => @profile.id) + xhr :get, :view_content, :id => @block.id, :article_id => @page.id, :page => 2, :profile => @profile.identifier + assert_response 500 + end + + should 'replace div with content for page passed as parameter' do + article1 = fast_create(TinyMceArticle, :parent_id => @page.id, :profile_id => @profile.id, :name => 'article1') + article2 = fast_create(TinyMceArticle, :parent_id => @page.id, :profile_id => @profile.id, :name => 'article2') + xhr :get, :view_content, :id => @block.id, :article_id => @page.id, :page => 2, :profile => @profile.identifier + assert_response :success + assert_match /context_content_#{@block.id}/, @response.body + assert_match /context_content_more_#{@block.id}/, @response.body + assert_match /article2/, @response.body + end + + should 'do not render pagination buttons if it has only one page' do + article1 = fast_create(TinyMceArticle, :parent_id => @page.id, :profile_id => @profile.id, :name => 'article1') + xhr :get, :view_content, :id => @block.id, :article_id => @page.id, :page => 2, :profile => @profile.identifier + assert_no_match /context_content_more_#{@block.id}/, @response.body + end + +end diff --git a/plugins/context_content/test/functional/profile_design_controller_test.rb b/plugins/context_content/test/functional/profile_design_controller_test.rb new file mode 100644 index 0000000..15225d1 --- /dev/null +++ b/plugins/context_content/test/functional/profile_design_controller_test.rb @@ -0,0 +1,54 @@ +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 = ['ContextContentPlugin'] + @environment.save! + + @profile = fast_create(Community, :environment_id => @environment.id) + @page = fast_create(Folder, :profile_id => @profile.id) + + box = Box.create!(:owner => @profile) + @block = ContextContentBlock.new(:box => box) + @block.types = ['TinyMceArticle'] + @block.limit = 1 + @block.save! + + user = create_user('testinguser') + @profile.add_admin(user.person) + login_as(user.login) + end + + should 'be able to edit context content 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_image' } + assert_tag :tag => 'input', :attributes => { :id => 'block_show_name' } + assert_tag :tag => 'input', :attributes => { :id => 'block_show_parent_content' } + assert_tag :tag => 'input', :attributes => { :id => 'block_types' } + end + + should 'be able to save TrackListBlock' do + @block.show_image = false + @block.show_name = false + @block.show_parent_content = false + @block.save! + get :edit, :id => @block.id, :profile => @profile.identifier + post :save, :id => @block.id, :block => {:title => 'context', :show_image => '0', :show_name => '0', :show_parent_content => '0', :types => ['TinyMceArticle', '', nil, 'Folder'] }, :profile => @profile.identifier + @block.reload + assert_equal 'context', @block.title + assert !@block.show_image && !@block.show_name && !@block.show_parent_content + assert_equal ['TinyMceArticle', 'Folder'], @block.types + end + +end diff --git a/plugins/context_content/views/blocks/_more.rhtml b/plugins/context_content/views/blocks/_more.rhtml index 21abedf..971556b 100644 --- a/plugins/context_content/views/blocks/_more.rhtml +++ b/plugins/context_content/views/blocks/_more.rhtml @@ -1,4 +1,4 @@ <% if contents.total_pages > 1 %> - <%= link_to_remote(nil, :url => {:id => block.id, :controller => 'context_content_plugin_profile', :action => 'view_content', :page => contents.previous_page, :article_id => article_id }, :html => {:class => "button icon-button icon-left #{contents.previous_page ? '':'disabled'}"}, :condition => "#{!contents.previous_page.nil?}", :success => "jQuery('#context_content_#{block.id}').effect('slide', {direction: 'left'});" )%> - <%= link_to_remote(nil, :url => {:id => block.id, :controller => 'context_content_plugin_profile', :action => 'view_content', :page => contents.next_page, :article_id => article_id }, :html => {:class => "button icon-button icon-right #{contents.next_page ? '':'disabled'}"}, :condition => "#{!contents.next_page.nil?}", :success => "jQuery('#context_content_#{block.id}').effect('slide', {direction: 'right'});" )%> + <%= link_to_remote(nil, :url => {:id => block.id, :controller => 'context_content_plugin_profile', :action => 'view_content', :page => contents.previous_page, :article_id => article_id }, :html => {:class => "button icon-button icon-left #{contents.previous_page ? '':'disabled'}".strip}, :condition => "#{!contents.previous_page.nil?}", :success => "jQuery('#context_content_#{block.id}').effect('slide', {direction: 'left'});" )%> + <%= link_to_remote(nil, :url => {:id => block.id, :controller => 'context_content_plugin_profile', :action => 'view_content', :page => contents.next_page, :article_id => article_id }, :html => {:class => "button icon-button icon-right #{contents.next_page ? '':'disabled'}".strip}, :condition => "#{!contents.next_page.nil?}", :success => "jQuery('#context_content_#{block.id}').effect('slide', {direction: 'right'});" )%> <% end %> -- libgit2 0.21.2