allow PUT requests to create wiki pages implicitly

testing note:
  this change affects all wiki page endpoints: api, draft state,
  and non-draft state. basic functionality for all of these
  pages should be verified, specifically when dealing with
  non-existent or deleted pages.

test plan:
 * PUT to /api/v1/courses/:course_id/front_page
   - variations:
     * wiki_page[title] - provided/not provided
     * wiki_page[published] - true/false/not provided
     * wiki_page[hide_from_students] - true/false/not provided
     (hiding/unpublishing the front page is not allowed)
 * PUT to /api/v1/courses/:course_id/pages/non-existent-page
   - same variations as front_page (above)
 * navigating to a non-existent/deleted page (draft state/non-draft state)
   - should redirect teachers to the edit page
   - should redirect students to the index page
     (with an alert message indicating why)
 * navigating to the 'Pages' tab (non-draft state)
   - if the 'Front Page' exists
     - shows the page
   - if the 'Front Page' has been deleted (with draft state enabled)
     - shows the edit page
   - if the 'Front Page' has never existed
     - shows the edit page

 * all other pages functionality should remain unchanged
   - non-draft state UI
     - show page
     - edit page
   - draft state UI
     - index page
     - show page
     - edit page
   - api
     - .../pages
       - GET (index)
       - POST (create page)
     - .../front_page
       - GET (show front page)
       - PUT (create/update front page)
     - .../pages/page-url
       - GET (show page)
       - PUT (create/update page)
       - DELETE (destroy page)

fixes CNVS-8488

Change-Id: I563e6944e1602e0b21ab69c6fe2dcd643c06611d
Reviewed-on: https://gerrit.instructure.com/23590
Tested-by: Jenkins <jenkins@instructure.com>
QA-Review: Matt Fairbourn <mfairbourn@instructure.com>
Reviewed-by: Jeremy Stanley <jeremy@instructure.com>
Product-Review: Bracken Mosbacker <bracken@instructure.com>
This commit is contained in:
Mark Severson 2013-08-22 09:43:03 -06:00
parent 290ca75300
commit 074151402d
10 changed files with 216 additions and 324 deletions

View File

@ -1014,41 +1014,11 @@ class ApplicationController < ActionController::Base
return if @page || !page_name
if params[:action] != 'create'
@page = @wiki.wiki_pages.deleted_last.find_by_url(page_name.to_s) ||
@wiki.wiki_pages.deleted_last.find_by_url(page_name.to_s.to_url) ||
@wiki.wiki_pages.find_by_id(page_name.to_i)
end
@page ||= @wiki.wiki_pages.new(
:title => page_name.titleize,
:url => page_name.to_url
)
if @page.new_record?
@page.wiki = @wiki
initialize_wiki_page
end
end
# Initializes the state of @page, but only if it is a new page
def initialize_wiki_page
return unless @page.new_record? || @page.deleted?
unless @context.draft_state_enabled?
@page.set_as_front_page! if !@wiki.has_front_page? and @page.url == Wiki::DEFAULT_FRONT_PAGE_URL
end
is_privileged_user = is_authorized_action?(@page.wiki, @current_user, :manage)
if is_privileged_user && @context.draft_state_enabled? && !@context.is_a?(Group)
@page.workflow_state = 'unpublished'
else
@page.workflow_state = 'active'
end
@page.editing_roles = (@context.default_wiki_editing_roles rescue nil) || @page.default_roles
if @page.is_front_page?
@page.body = t "#application.wiki_front_page_default_content_course", "Welcome to your new course wiki!" if @context.is_a?(Course)
@page.body = t "#application.wiki_front_page_default_content_group", "Welcome to your new group wiki!" if @context.is_a?(Group)
@page = @wiki.wiki_pages.not_deleted.find_by_url(page_name.to_s) ||
@wiki.wiki_pages.not_deleted.find_by_url(page_name.to_s.to_url) ||
@wiki.wiki_pages.not_deleted.find_by_id(page_name.to_i)
end
@page ||= @wiki.build_wiki_page(@current_user, :url => page_name)
end
def context_wiki_page_url

View File

@ -40,7 +40,7 @@
# "hide_from_students": false,
#
# // roles allowed to edit the page; comma-separated list comprising a combination of
# // 'teachers', 'students', and/or 'public'
# // 'teachers', 'students', 'members', and/or 'public'
# // if not supplied, course defaults are used
# "editing_roles": "teachers,students",
#
@ -57,7 +57,7 @@
# // (present when requesting a single page; omitted when listing pages)
# "body": "<p>Page Content</p>",
#
# // whether the page is published
# // whether the page is published (true) or draft state (false).
# "published": true,
#
# // whether this page is the front page for the wiki
@ -111,10 +111,69 @@
class WikiPagesApiController < ApplicationController
before_filter :require_context
before_filter :get_wiki_page, :except => [:create, :index]
before_filter :require_wiki_page, :except => [:create, :update, :update_front_page, :index]
before_filter :was_front_page, :except => [:index]
include Api::V1::WikiPage
# @API Show front page
#
# Retrieve the content of the front page
#
# @example_request
# curl -H 'Authorization: Bearer <token>' \
# https://<canvas>/api/v1/courses/123/front_page
#
# @returns Page
def show_front_page
show
end
# @API Update/create front page
#
# Update the title or contents of the front page
#
# @argument wiki_page[title] [Optional, String]
# The title for the new page. NOTE: changing a page's title will change its
# url. The updated url will be returned in the result.
#
# @argument wiki_page[body] [String]
# The content for the new page.
#
# @argument wiki_page[hide_from_students] [Optional, Boolean]
# Whether the page should be hidden from students.
#
# *Note:* when draft state is enabled, attempts to set +hide_from_students+
# will be ignored and the value returned will always be the inverse of the
# +published+ value.
#
# @argument wiki_page[editing_roles] [Optional, String, "teachers"|"students"|"members"|"public"]
# Which user roles are allowed to edit this page. Any combination
# of these roles is allowed (separated by commas).
#
# "teachers":: Allows editing by teachers in the course.
# "students":: Allows editing by students in the course.
# "members":: For group wikis, allows editing by members of the group.
# "public":: Allows editing by any user.
#
# @argument wiki_page[notify_of_update] [Optional, Boolean]
# Whether participants should be notified when this page changes.
#
# @argument wiki_page[published] [Optional, Boolean]
# Whether the page is published (true) or draft state (false).
#
# *Note:* when draft state is disabled, attempts to set +published+
# will be ignored and the value returned will always be true.
#
# @example_request
# curl -X PUT -H 'Authorization: Bearer <token>' \
# https://<canvas>/api/v1/courses/123/front_page?wiki_page[body]=Updated+body+text
#
# @returns Page
def update_front_page
update
end
# @API List pages
#
# List the wiki pages associated with a course or group
@ -161,30 +220,6 @@ class WikiPagesApiController < ApplicationController
end
end
# @API Show page
#
# Retrieve the content of a wiki page
#
# @argument url [String]
# The unique identifier for a page.
#
# @example_request
# curl -H 'Authorization: Bearer <token>' \
# https://<canvas>/api/v1/courses/123/pages/my-page-url
#
# @example_request
# curl -H 'Authorization: Bearer <token>' \
# https://<canvas>/api/v1/groups/456/front_page
#
# @returns Page
def show
if authorized_action(@page, @current_user, :read)
@page.increment_view_count(@current_user, @context)
log_asset_access(@page, "wiki", @wiki)
render :json => wiki_page_json(@page, @current_user, session)
end
end
# @API Create page
#
# Create a new wiki page
@ -229,8 +264,11 @@ class WikiPagesApiController < ApplicationController
#
# @returns Page
def create
@page = @context.wiki.wiki_pages.build
initial_params = params.slice(:url)
initial_params.merge! (params[:wiki_page] || {}).slice(:url, :title)
@wiki = @context.wiki
@page = @wiki.build_wiki_page(@current_user, initial_params)
if authorized_action(@page, @current_user, :create)
update_params = get_update_params(Set[:title, :body])
if !update_params.is_a?(Symbol) && @page.update_attributes(update_params) && process_front_page
@ -241,14 +279,28 @@ class WikiPagesApiController < ApplicationController
end
end
end
# @API Show page
#
# Retrieve the content of a wiki page
#
# @example_request
# curl -H 'Authorization: Bearer <token>' \
# https://<canvas>/api/v1/courses/123/pages/my-page-url
#
# @returns Page
def show
if authorized_action(@page, @current_user, :read)
@page.increment_view_count(@current_user, @context)
log_asset_access(@page, "wiki", @wiki)
render :json => wiki_page_json(@page, @current_user, session)
end
end
# @API Update page
# @API Update/create page
#
# Update the title or contents of a wiki page
#
# @argument url [String]
# The unique identifier for a page.
#
# @argument wiki_page[title] [String]
# The title for the new page. NOTE: changing a page's title will change its
# url. The updated url will be returned in the result.
@ -288,13 +340,16 @@ class WikiPagesApiController < ApplicationController
# curl -X PUT -H 'Authorization: Bearer <token>' \
# https://<canvas>/api/v1/courses/123/pages/the-page-url?wiki_page[body]=Updated+body+text
#
# @example_request
# curl -X PUT -H 'Authorization: Bearer <token>' \
# https://<canvas>/api/v1/courses/123/front_page?wiki_page[body]=Updated+body+text
#
# @returns Page
def update
if authorized_action(@page, @current_user, [:update, :update_content])
perform_update = false
if @page.new_record?
perform_update = true if authorized_action(@page, @current_user, [:create])
elsif authorized_action(@page, @current_user, [:update, :update_content])
perform_update = true
end
if perform_update
update_params = get_update_params
if !update_params.is_a?(Symbol) && @page.update_attributes(update_params) && process_front_page
log_asset_access(@page, "wiki", @wiki, 'participate')
@ -310,17 +365,10 @@ class WikiPagesApiController < ApplicationController
#
# Delete a wiki page
#
# @argument url [String]
# the unique identifier for a page.
#
# @example_request
# curl -X DELETE -H 'Authorization: Bearer <token>' \
# https://<canvas>/api/v1/courses/123/pages/the-page-url
#
# @example_request
# curl -X DELETE -H 'Authorization: Bearer <token>' \
# https://<canvas>/api/v1/courses/123/front_page
#
# @returns Page
def destroy
if authorized_action(@page, @current_user, :delete)
@ -340,9 +388,6 @@ class WikiPagesApiController < ApplicationController
#
# List the revisions of a page. Callers must have update rights on the page in order to see page history.
#
# @argument url [String]
# The unique identifier for a page
#
# @example_request
# curl -H 'Authorization: Bearer <token>' \
# https://<canvas>/api/v1/courses/123/pages/the-page-url/revisions
@ -362,9 +407,6 @@ class WikiPagesApiController < ApplicationController
# Retrieve the metadata and optionally content of a revision of the page.
# Note that retrieving historic versions of pages requires edit rights.
#
# @argument url [String]
# The unique identifier for a page
#
# @argument summary [Optional, Boolean]
# If set, exclude page content from results
#
@ -399,9 +441,6 @@ class WikiPagesApiController < ApplicationController
#
# Revert a page to a prior revision.
#
# @argument url [String]
# The unique identifier for the page
#
# @argument revision_id [Integer]
# The revision to revert to (use the
# {api:WikiPagesApiController#revisions List Revisions API} to see
@ -429,21 +468,45 @@ class WikiPagesApiController < ApplicationController
end
protected
def is_front_page_action?
!!action_name.match(/_front_page$/)
end
def get_wiki_page
@wiki = @context.wiki
@wiki.check_has_front_page
url = params[:url]
if url.blank?
if @wiki.has_front_page?
url = @wiki.get_front_page_url
# attempt to find an existing page
is_front_page_action = is_front_page_action?
url = is_front_page_action ? Wiki::DEFAULT_FRONT_PAGE_URL : params[:url]
@page = if is_front_page_action
@wiki.front_page
else
@wiki.wiki_pages.not_deleted.find_by_url(url)
end
# create a new page if the page was not found
unless @page
@page = @wiki.build_wiki_page(@current_user, :url => url)
if is_front_page_action
@page.workflow_state = 'active'
@set_front_page = true
@set_as_front_page = true
else
render :status => 404, :json => { :message => t(:no_wiki_front_page, "No front page has been set") }
return false
@page.workflow_state = 'unpublished'
end
end
end
def require_wiki_page
if !@page || @page.new_record?
if is_front_page_action?
render :status => :not_found, :json => { :message => 'No front page has been set' }
else
render :status => :not_found, :json => { :message => 'page not found' }
end
end
@page = @wiki.wiki_pages.not_deleted.find_by_url!(url)
end
def was_front_page
@ -452,8 +515,6 @@ class WikiPagesApiController < ApplicationController
end
def get_update_params(allowed_fields=Set[])
initialize_wiki_page
# normalize parameters
page_params = params[:wiki_page] || {}
if @context.draft_state_enabled?

View File

@ -51,15 +51,15 @@ class WikiPagesController < ApplicationController
append_sis_data(hash)
js_env(hash)
@editing = true if Canvas::Plugin.value_to_boolean(params[:edit])
if @page.deleted?
flash[:notice] = t('notices.page_deleted', 'The page "%{title}" has been deleted.', :title => @page.title)
if @wiki.has_front_page? && !@page.is_front_page?
redirect_to named_context_url(@context, :context_wiki_page_url, @wiki.get_front_page_url)
else
redirect_to named_context_url(@context, :context_url)
unless is_authorized_action?(@page, @current_user, [:update, :update_content]) || @page.is_front_page?
wiki_page = @wiki.wiki_pages.deleted_last.find_by_url(@page.url) if @page.new_record?
if wiki_page && wiki_page.deleted?
flash[:warning] = t('notices.page_deleted', 'The page "%{title}" has been deleted.', :title => @page.title)
return redirect_to named_context_url(@context, :context_wiki_page_url, @wiki.get_front_page_url)
end
return
end
if is_authorized_action?(@page, @current_user, :read)
add_crumb(@page.title)
@page.increment_view_count(@current_user, @context)
@ -104,8 +104,6 @@ class WikiPagesController < ApplicationController
end
def perform_update
initialize_wiki_page
if @page.update_attributes(params[:wiki_page].merge(:user_id => @current_user.id))
unless @page.context.draft_state_enabled?
@page.set_as_front_page! if !@page.wiki.has_front_page? and @page.url == Wiki::DEFAULT_FRONT_PAGE_URL
@ -178,9 +176,20 @@ class WikiPagesController < ApplicationController
return
end
if @page.deleted?
flash[:notice] = t('notices.page_deleted', 'The page "%{title}" has been deleted.', :title => @page.title)
return front_page # delegate to front_page logic
if @page.new_record?
if is_authorized_action?(@page, @current_user, [:update, :update_content])
flash[:info] = t('notices.create_non_existent_page', 'The page "%{title}" does not exist, but you can create it below', :title => @page.title)
redirect_to polymorphic_url([@context, :edit_named_page], :wiki_page_id => @page)
return
else
wiki_page = @wiki.wiki_pages.deleted_last.find_by_url(@page.url)
if wiki_page && wiki_page.deleted?
flash[:warning] = t('notices.page_deleted', 'The page "%{title}" has been deleted.', :title => @page.title)
else
flash[:warning] = t('notices.page_does_not_exist', 'The page "%{title}" does not exist.', :title => @page.title)
end
return front_page # delegate to front_page logic
end
end
if authorized_action(@page, @current_user, :read)
@ -199,11 +208,6 @@ class WikiPagesController < ApplicationController
return
end
if @page.deleted?
flash[:notice] = t('notices.page_deleted', 'The page "%{title}" has been deleted.', :title => @page.title)
return front_page # delegate to front_page logic
end
if is_authorized_action?(@page, @current_user, [:update, :update_content])
add_crumb(@page.title)
@ -211,7 +215,7 @@ class WikiPagesController < ApplicationController
render
else
if authorized_action(@page, @current_user, :read)
flash[:error] = t('notices.cannot_edit', 'You are not allowed to edit the page "%{title}".', :title => @page.title)
flash[:warning] = t('notices.cannot_edit', 'You are not allowed to edit the page "%{title}".', :title => @page.title)
redirect_to polymorphic_url([@context, :named_page], :wiki_page_id => @page)
end
end

View File

@ -167,4 +167,16 @@ class Wiki < ActiveRecord::Base
wiki
end
end
def build_wiki_page(user, opts={})
if (opts.include?(:url) || opts.include?(:title)) && (!opts.include?(:url) || !opts.include?(:title))
opts[:title] = opts[:url].to_s.titleize if opts.include?(:url)
opts[:url] = opts[:title].to_s.to_url if opts.include?(:title)
end
page = WikiPage.new(opts)
page.wiki = self
page.initialize_wiki_page(user)
page
end
end

View File

@ -580,4 +580,24 @@ class WikiPage < ActiveRecord::Base
end
end
end
def initialize_wiki_page(user)
unless context.draft_state_enabled?
set_as_front_page! if !wiki.has_front_page? and url == Wiki::DEFAULT_FRONT_PAGE_URL
end
is_privileged_user = wiki.grants_right?(user, :manage)
if is_privileged_user && context.draft_state_enabled? && !context.is_a?(Group)
self.workflow_state = 'unpublished'
else
self.workflow_state = 'active'
end
self.editing_roles = (context.default_wiki_editing_roles rescue nil) || default_roles
if is_front_page?
self.body = t "#application.wiki_front_page_default_content_course", "Welcome to your new course wiki!" if context.is_a?(Course)
self.body = t "#application.wiki_front_page_default_content_group", "Welcome to your new group wiki!" if context.is_a?(Group)
end
end
end

View File

@ -1227,12 +1227,15 @@ FakeRails3Routes.draw do
end
scope(:controller => :wiki_pages_api) do
get "courses/:course_id/front_page", :action => :show_front_page
get "groups/:group_id/front_page", :action => :show_front_page
put "courses/:course_id/front_page", :action => :update_front_page
put "groups/:group_id/front_page", :action => :update_front_page
get "courses/:course_id/pages", :action => :index, :path_name => 'course_wiki_pages'
get "groups/:group_id/pages", :action => :index, :path_name => 'group_wiki_pages'
get "courses/:course_id/pages/:url", :action => :show, :path_name => 'course_wiki_page'
get "groups/:group_id/pages/:url", :action => :show, :path_name => 'group_wiki_page'
get "courses/:course_id/front_page", :action => :show
get "groups/:group_id/front_page", :action => :show
get "courses/:course_id/pages/:url/revisions", :action => :revisions, :path_name => 'course_wiki_page_revisions'
get "groups/:group_id/pages/:url/revisions", :action => :revisions, :path_name => 'group_wiki_page_revisions'
get "courses/:course_id/pages/:url/revisions/latest", :action => :show_revision
@ -1245,12 +1248,8 @@ FakeRails3Routes.draw do
post "groups/:group_id/pages", :action => :create
put "courses/:course_id/pages/:url", :action => :update
put "groups/:group_id/pages/:url", :action => :update
put "courses/:course_id/front_page", :action => :update
put "groups/:group_id/front_page", :action => :update
delete "courses/:course_id/pages/:url", :action => :destroy
delete "groups/:group_id/pages/:url", :action => :destroy
delete "courses/:course_id/front_page", :action => :destroy
delete "groups/:group_id/front_page", :action => :destroy
end
scope(:controller => :context_modules_api) do

View File

@ -254,7 +254,7 @@ describe "Pages API", :type => :integration do
page.set_as_front_page!
json = api_call(:get, "/api/v1/courses/#{@course.id}/front_page",
:controller=>"wiki_pages_api", :action=>"show", :format=>"json", :course_id=>"#{@course.id}")
:controller=>"wiki_pages_api", :action=>"show_front_page", :format=>"json", :course_id=>"#{@course.id}")
expected = { "hide_from_students" => false,
"editing_roles" => "teachers",
@ -278,7 +278,7 @@ describe "Pages API", :type => :integration do
@wiki.save!
json = api_call(:get, "/api/v1/courses/#{@course.id}/front_page",
:controller=>"wiki_pages_api", :action=>"show", :format=>"json", :course_id=>"#{@course.id}")
:controller=>"wiki_pages_api", :action=>"show_front_page", :format=>"json", :course_id=>"#{@course.id}")
expected = { "hide_from_students" => false,
"editing_roles" => "teachers",
@ -302,7 +302,7 @@ describe "Pages API", :type => :integration do
wiki.unset_front_page!
json = api_call(:get, "/api/v1/courses/#{@course.id}/front_page",
{:controller=>"wiki_pages_api", :action=>"show", :format=>"json", :course_id=>"#{@course.id}"},
{:controller=>"wiki_pages_api", :action=>"show_front_page", :format=>"json", :course_id=>"#{@course.id}"},
{}, {}, {:expected_status => 404})
json['message'].should == "No front page has been set"
@ -552,7 +552,7 @@ describe "Pages API", :type => :integration do
new_title = 'blah blah blah'
api_call(:put, "/api/v1/courses/#{@course.id}/front_page",
{ :controller => 'wiki_pages_api', :action => 'update', :format => 'json', :course_id => @course.to_param},
{ :controller => 'wiki_pages_api', :action => 'update_front_page', :format => 'json', :course_id => @course.to_param},
{ :wiki_page => { :title => new_title}})
page.reload
@ -753,10 +753,14 @@ describe "Pages API", :type => :integration do
{}, {:expected_status => 400})
end
it "should 404 if the page doesn't exist" do
api_call(:put, "/api/v1/courses/#{@course.id}/pages/nonexistent-url?title=renamed",
it "should create a page if the page doesn't exist" do
api_call(:put, "/api/v1/courses/#{@course.id}/pages/nonexistent-url",
{ :controller => 'wiki_pages_api', :action => 'update', :format => 'json', :course_id => @course.to_param,
:url => 'nonexistent-url', :title => 'renamed' }, {}, {}, { :expected_status => 404 })
:url => 'nonexistent-url' },
{ :wiki_page => { :body => 'Nonexistent page content' } })
page = @wiki.wiki_pages.find_by_url!('nonexistent-url')
page.should_not be_nil
page.body.should == 'Nonexistent page content'
end
describe "notify_of_update" do
@ -794,8 +798,8 @@ describe "Pages API", :type => :integration do
page = @course.wiki.wiki_pages.create!(:title => "hrup", :body => "blooop")
page.set_as_front_page!
api_call(:delete, "/api/v1/courses/#{@course.id}/front_page",
{ :controller => 'wiki_pages_api', :action => 'destroy', :format => 'json', :course_id => @course.to_param},
api_call(:delete, "/api/v1/courses/#{@course.id}/pages/#{page.url}",
{ :controller => 'wiki_pages_api', :action => 'destroy', :format => 'json', :course_id => @course.to_param, :url => page.url},
{}, {}, {:expected_status => 400})
page.reload

View File

@ -194,189 +194,4 @@ describe ApplicationController do
@controller.send(:complete_request_uri).should == "https://example.com/api/v1/courses?password=[FILTERED]&test=5&Xaccess_token=13&access_token=[FILTERED]"
end
end
describe 'initialize_wiki_page' do
context 'course' do
before(:each) do
course_with_teacher_logged_in
@page = @course.wiki.wiki_pages.build(:title => 'Front Page', :url => 'front-page')
@stub_draft_state_enabled = Course.any_instance.stubs(:draft_state_enabled?)
@controller.instance_variable_set(:@domain_root_account, @domain_root_account)
@controller.instance_variable_set(:@current_user, @teacher)
@controller.instance_variable_set(:@context, @course)
@controller.instance_variable_set(:@wiki, @course.wiki)
@controller.instance_variable_set(:@page, @page)
end
it 'should set the front page body' do
@page.body.should be_nil
@controller.send :initialize_wiki_page
@page.body.should_not be_empty
end
context 'draft not enabled' do
before(:each) do
@stub_draft_state_enabled.returns(false)
end
it 'should initialize a new page' do
@page.should be_new_record
@page.expects(:workflow_state=).with('active')
@controller.send :initialize_wiki_page
end
it 'should initialize a deleted page' do
@page.workflow_state = 'deleted'
@page.save!
@page.should be_deleted
@page.expects(:workflow_state=).with('active')
@controller.send :initialize_wiki_page
end
it 'should not initialize an active page' do
@page.workflow_state = 'active'
@page.save!
@page.should be_active
@page.expects(:workflow_state=).never
@controller.send :initialize_wiki_page
end
end
context 'draft enabled' do
before(:each) do
@stub_draft_state_enabled.returns(true)
end
it 'should initialize a new page' do
@page.should be_new_record
@page.expects(:workflow_state=).with('unpublished')
@controller.send :initialize_wiki_page
end
it 'should initialize a deleted page' do
@page.workflow_state = 'deleted'
@page.save!
@page.should be_deleted
@page.expects(:workflow_state=).with('unpublished')
@controller.send :initialize_wiki_page
end
it 'should not initialize an unpublished page' do
@page.workflow_state = 'unpublished'
@page.save!
@page.should be_unpublished
@page.expects(:workflow_state=).never
@controller.send :initialize_wiki_page
end
it 'should not initialize an active page' do
@page.workflow_state = 'active'
@page.save!
@page.should be_active
@page.expects(:workflow_state=).never
@controller.send :initialize_wiki_page
end
end
end
context 'group' do
before(:each) do
group_with_user_logged_in
@page = @group.wiki.wiki_pages.build(:title => 'Front Page', :url => 'front-page')
@stub_draft_state_enabled = Group.any_instance.stubs(:draft_state_enabled?)
@controller.instance_variable_set(:@current_user, @user)
@controller.instance_variable_set(:@context, @group)
@controller.instance_variable_set(:@wiki, @group.wiki)
@controller.instance_variable_set(:@page, @page)
end
it 'should set the front page body' do
@page.body.should be_nil
@controller.send :initialize_wiki_page
@page.body.should_not be_empty
end
context 'draft not enabled' do
before(:each) do
@stub_draft_state_enabled.returns(false)
end
it 'should initialize a new page' do
@page.should be_new_record
@page.expects(:workflow_state=).with('active')
@controller.send :initialize_wiki_page
end
it 'should initialize a deleted page' do
@page.workflow_state = 'deleted'
@page.save!
@page.should be_deleted
@page.expects(:workflow_state=).with('active')
@controller.send :initialize_wiki_page
end
it 'should not initialize an active page' do
@page.workflow_state = 'active'
@page.save!
@page.should be_active
@page.expects(:workflow_state=).never
@controller.send :initialize_wiki_page
end
end
context 'draft enabled' do
before(:each) do
@stub_draft_state_enabled.returns(true)
end
it 'should initialize a new page' do
@page.should be_new_record
@page.expects(:workflow_state=).with('active')
@controller.send :initialize_wiki_page
end
it 'should initialize a deleted page' do
@page.workflow_state = 'deleted'
@page.save!
@page.should be_deleted
@page.expects(:workflow_state=).with('active')
@controller.send :initialize_wiki_page
end
it 'should not initialize an unpublished page' do
@page.workflow_state = 'unpublished'
@page.save!
@page.should be_unpublished
@page.expects(:workflow_state=).never
@controller.send :initialize_wiki_page
end
it 'should not initialize an active page' do
@page.workflow_state = 'active'
@page.save!
@page.should be_active
@page.expects(:workflow_state=).never
@controller.send :initialize_wiki_page
end
end
end
end
end

View File

@ -128,17 +128,6 @@ describe WikiPagesController do
get 'show', :course_id => @course.id, :id => page.wiki_id
assert_unauthorized
end
it "should not resurrect a deleted page" do
course_with_teacher_logged_in :active_all => true
page = @course.wiki.wiki_pages.create! :title => 'deleted page'
page.workflow_state = 'deleted'
page.save!
get 'show', :course_id => @course.id, :id => page.url
response.should be_redirect
flash[:notice].should be_include 'deleted'
assigns[:page].should be_deleted
end
end
# describe "GET 'revisions'" do

View File

@ -258,6 +258,24 @@ describe WikiPage do
end
end
context 'initialize_wiki_page' do
it 'should set the course front page body' do
course_with_teacher_logged_in
front_page = @course.wiki.wiki_pages.new(:title => 'Front Page', :url => 'front-page')
front_page.body.should be_nil
front_page.initialize_wiki_page(@teacher)
front_page.body.should_not be_empty
end
it 'should set the group front page body' do
group_with_user_logged_in
front_page = @group.wiki.wiki_pages.new(:title => 'Front Page', :url => 'front-page')
front_page.body.should be_nil
front_page.initialize_wiki_page(@user)
front_page.body.should_not be_empty
end
end
context 'set policy' do
before :each do
course :active_all => true