From e8468b51cc7c0725e1f59d4f2f2c828ffcd080ce Mon Sep 17 00:00:00 2001 From: Eric Davis Date: Wed, 13 Oct 2010 17:13:50 +0000 Subject: [PATCH] Refactor: extract method in WikiController#special to create a new #export method git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@4251 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/wiki_controller.rb | 22 +++++++++++-------- app/views/wiki/special_date_index.rhtml | 2 +- app/views/wiki/special_page_index.rhtml | 2 +- config/routes.rb | 3 ++- lib/redmine.rb | 2 +- test/functional/wiki_controller_test.rb | 28 +++++++++++++++++++++++++ test/integration/routing_test.rb | 2 +- 7 files changed, 47 insertions(+), 14 deletions(-) diff --git a/app/controllers/wiki_controller.rb b/app/controllers/wiki_controller.rb index 55194e048..10890495f 100644 --- a/app/controllers/wiki_controller.rb +++ b/app/controllers/wiki_controller.rb @@ -181,16 +181,9 @@ class WikiController < ApplicationController :order => 'title' @pages_by_date = @pages.group_by {|p| p.updated_on.to_date} @pages_by_parent_id = @pages.group_by(&:parent_id) - # export wiki to a single html file when 'export' - if User.current.allowed_to?(:export_wiki_pages, @project) - @pages = @wiki.pages.find :all, :order => 'title' - export = render_to_string :action => 'export_multiple', :layout => false - send_data(export, :type => 'text/html', :filename => "wiki.html") - else - redirect_to :action => 'index', :id => @project, :page => nil - end - return + redirect_to :action => 'export', :id => @project # Compatibility stub while refactoring + return else # requested special page doesn't exist, redirect to default page redirect_to :action => 'index', :id => @project, :page => nil @@ -198,6 +191,17 @@ class WikiController < ApplicationController end render :action => "special_#{page_title}" end + + # Export wiki to a single html file + def export + if User.current.allowed_to?(:export_wiki_pages, @project) + @pages = @wiki.pages.find :all, :order => 'title' + export = render_to_string :action => 'export_multiple', :layout => false + send_data(export, :type => 'text/html', :filename => "wiki.html") + else + redirect_to :action => 'index', :id => @project, :page => nil + end + end def preview page = @wiki.find_page(params[:page]) diff --git a/app/views/wiki/special_date_index.rhtml b/app/views/wiki/special_date_index.rhtml index b34fb8464..957c15967 100644 --- a/app/views/wiki/special_date_index.rhtml +++ b/app/views/wiki/special_date_index.rhtml @@ -24,7 +24,7 @@ <% unless @pages.empty? %> <% other_formats_links do |f| %> <%= f.link_to 'Atom', :url => {:controller => 'activities', :action => 'index', :id => @project, :show_wiki_edits => 1, :key => User.current.rss_key} %> - <%= f.link_to('HTML', :url => {:action => 'special', :page => 'export'}) if User.current.allowed_to?(:export_wiki_pages, @project) %> + <%= f.link_to('HTML', :url => {:action => 'export'}) if User.current.allowed_to?(:export_wiki_pages, @project) %> <% end %> <% end %> diff --git a/app/views/wiki/special_page_index.rhtml b/app/views/wiki/special_page_index.rhtml index e2d1e44ad..e85f83b91 100644 --- a/app/views/wiki/special_page_index.rhtml +++ b/app/views/wiki/special_page_index.rhtml @@ -17,7 +17,7 @@ <% unless @pages.empty? %> <% other_formats_links do |f| %> <%= f.link_to 'Atom', :url => {:controller => 'activities', :action => 'index', :id => @project, :show_wiki_edits => 1, :key => User.current.rss_key} %> - <%= f.link_to('HTML', :url => {:action => 'special', :page => 'export'}) if User.current.allowed_to?(:export_wiki_pages, @project) %> + <%= f.link_to('HTML', :url => {:action => 'export'}) if User.current.allowed_to?(:export_wiki_pages, @project) %> <% end %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 0d52476d0..4bb893dae 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -29,7 +29,8 @@ ActionController::Routing::Routes.draw do |map| map.connect 'projects/:id/wiki/destroy', :controller => 'wikis', :action => 'destroy', :conditions => {:method => :post} map.with_options :controller => 'wiki' do |wiki_routes| wiki_routes.with_options :conditions => {:method => :get} do |wiki_views| - wiki_views.connect 'projects/:id/wiki/:page', :action => 'special', :page => /page_index|date_index|export/i + wiki_views.connect 'projects/:id/wiki/export', :action => 'export' + wiki_views.connect 'projects/:id/wiki/:page', :action => 'special', :page => /page_index|date_index/i wiki_views.connect 'projects/:id/wiki/:page', :action => 'index', :page => nil wiki_views.connect 'projects/:id/wiki/:page/edit', :action => 'edit' wiki_views.connect 'projects/:id/wiki/:page/rename', :action => 'rename' diff --git a/lib/redmine.rb b/lib/redmine.rb index e5694a705..953707781 100644 --- a/lib/redmine.rb +++ b/lib/redmine.rb @@ -112,7 +112,7 @@ Redmine::AccessControl.map do |map| map.permission :rename_wiki_pages, {:wiki => :rename}, :require => :member map.permission :delete_wiki_pages, {:wiki => :destroy}, :require => :member map.permission :view_wiki_pages, :wiki => [:index, :special] - map.permission :export_wiki_pages, {} + map.permission :export_wiki_pages, :wiki => [:export] map.permission :view_wiki_edits, :wiki => [:history, :diff, :annotate] map.permission :edit_wiki_pages, :wiki => [:edit, :preview, :add_attachment] map.permission :delete_wiki_pages_attachments, {} diff --git a/test/functional/wiki_controller_test.rb b/test/functional/wiki_controller_test.rb index 76aa02467..6b0a6cc5c 100644 --- a/test/functional/wiki_controller_test.rb +++ b/test/functional/wiki_controller_test.rb @@ -256,6 +256,34 @@ class WikiControllerTest < ActionController::TestCase :child => { :tag => 'li', :child => { :tag => 'a', :attributes => { :href => '/projects/ecookbook/wiki/Another_page' }, :content => 'Another page' } } end + + context "GET :export" do + context "with an authorized user to export the wiki" do + setup do + @request.session[:user_id] = 2 + get :export, :id => 'ecookbook' + end + + should_respond_with :success + should_assign_to :pages + should_respond_with_content_type "text/html" + should "export all of the wiki pages to a single html file" do + assert_select "a[name=?]", "CookBook_documentation" + assert_select "a[name=?]", "Another_page" + assert_select "a[name=?]", "Page_with_an_inline_image" + end + + end + + context "with an unauthorized user" do + setup do + get :export, :id => 'ecookbook' + + should_respond_with :redirect + should_redirect_to('wiki index') { {:action => 'index', :id => @project, :page => nil} } + end + end + end def test_not_found get :index, :id => 999 diff --git a/test/integration/routing_test.rb b/test/integration/routing_test.rb index 25755dc36..e20d8ad19 100644 --- a/test/integration/routing_test.rb +++ b/test/integration/routing_test.rb @@ -321,7 +321,7 @@ class RoutingTest < ActionController::IntegrationTest should_route :get, "/projects/567/wiki/page_index", :controller => 'wiki', :action => 'special', :id => '567', :page => 'page_index' should_route :get, "/projects/567/wiki/Page_Index", :controller => 'wiki', :action => 'special', :id => '567', :page => 'Page_Index' should_route :get, "/projects/567/wiki/date_index", :controller => 'wiki', :action => 'special', :id => '567', :page => 'date_index' - should_route :get, "/projects/567/wiki/export", :controller => 'wiki', :action => 'special', :id => '567', :page => 'export' + should_route :get, "/projects/567/wiki/export", :controller => 'wiki', :action => 'export', :id => '567' should_route :post, "/projects/567/wiki/my_page/edit", :controller => 'wiki', :action => 'edit', :id => '567', :page => 'my_page' should_route :post, "/projects/567/wiki/CookBook_documentation/preview", :controller => 'wiki', :action => 'preview', :id => '567', :page => 'CookBook_documentation' -- 2.39.5