fix legacy wiki front page behavior

test plan:
 - in a course with no pages (new course is best)
 * manually update the course object:
   * set default_view to 'wiki'
 * manually update the course's wiki object:
   * set front_page_url to nil
   * set has_no_front_page to true
 * navigate to the course home page
 - there should not be an error on the page

fixes CNVS-7744

Change-Id: I047db32d2fa7a376be994a1314c0e55a0cadffde
Reviewed-on: https://gerrit.instructure.com/23751
QA-Review: Hannah Bottalla <hannah@instructure.com>
Tested-by: Jenkins <jenkins@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-26 16:05:50 -06:00
parent ddda935387
commit 932bd3dc0e
11 changed files with 100 additions and 24 deletions

View File

@ -1108,6 +1108,12 @@ class CoursesController < ApplicationController
@course_home_view = (params[:view] == "feed" && 'feed') || @context.default_view || 'feed'
# make sure the wiki front page exists
if @course_home_view == 'wiki'
@context.wiki.check_has_front_page
@course_home_view = 'feed' if @context.wiki.front_page.nil?
end
@contexts = [@context]
case @course_home_view
when "wiki"

View File

@ -102,6 +102,10 @@ class WikiPagesController < ApplicationController
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
end
log_asset_access(@page, "wiki", @wiki, 'participate')
generate_new_page_view
@page.context_module_action(@current_user, @context, :contributed)

View File

@ -1958,7 +1958,7 @@ class Course < ActiveRecord::Base
end
# be very explicit about draft state courses, but be liberal toward legacy courses
if self.root_account.enable_draft?
if self.draft_state_enabled?
if migration.for_course_copy? && (source = migration.source_course || Course.find_by_id(migration.migration_settings[:source_course_id]))
self.wiki.has_no_front_page = !!source.wiki.has_no_front_page
self.wiki.front_page_url = source.wiki.front_page_url
@ -1966,7 +1966,7 @@ class Course < ActiveRecord::Base
end
end
front_page = self.wiki.front_page
self.wiki.unset_front_page! if front_page.nil? || (self.root_account.enable_draft? && front_page.new_record?)
self.wiki.unset_front_page! if front_page.nil? || (self.draft_state_enabled? && front_page.new_record?)
syllabus_should_be_added = everything_selected || migration.copy_options[:syllabus_body] || migration.copy_options[:all_syllabus_body]
if syllabus_should_be_added

View File

@ -30,15 +30,17 @@ class Wiki < ActiveRecord::Base
attr_accessible :title
has_many :wiki_pages, :dependent => :destroy
before_create :default_front_page
before_save :set_has_no_front_page_default
after_save :update_contexts
DEFAULT_FRONT_PAGE_URL = 'front-page'
def default_front_page
has_no_front_page = front_page_url.nil? if has_no_front_page.nil?
def set_has_no_front_page_default
if self.has_no_front_page.nil? && self.id && context
self.has_no_front_page = true if context.draft_state_enabled?
end
end
private :default_front_page
private :set_has_no_front_page_default
def update_contexts
self.context.try(:touch)
@ -66,18 +68,26 @@ class Wiki < ActiveRecord::Base
def check_has_front_page
return unless self.has_no_front_page.nil?
self.has_no_front_page = !self.wiki_pages.where(:url => self.front_page_url || DEFAULT_FRONT_PAGE_URL).exists?
url = DEFAULT_FRONT_PAGE_URL
self.has_no_front_page = !self.wiki_pages.not_deleted.where(:url => url).exists?
self.front_page_url = url unless self.has_no_front_page
self.save
end
def front_page
return nil unless has_front_page?
url = self.get_front_page_url
return nil if url.nil?
# TODO i18n
t :front_page_name, "Front Page"
url = self.get_front_page_url
page = self.wiki_pages.find_by_url(url)
# attempt to find the page and store it's url (if it is found)
page = self.wiki_pages.not_deleted.find_by_url(url)
self.set_front_page_url!(url) if self.has_no_front_page && page
# return an implicitly created page if a page could not be found
unless page
page = self.wiki_pages.new(:title => "Front Page", :url => url)
page = self.wiki_pages.new(:title => url.titleize, :url => url)
page.wiki = self
end
page
@ -88,7 +98,7 @@ class Wiki < ActiveRecord::Base
end
def get_front_page_url
return nil unless self.has_front_page?
return nil unless self.has_front_page? || !context.draft_state_enabled?
self.front_page_url || DEFAULT_FRONT_PAGE_URL
end

View File

@ -209,7 +209,8 @@ class WikiPage < ActiveRecord::Base
end
def is_front_page?
!self.deleted? && self.wiki.has_front_page? && self.url == self.wiki.get_front_page_url
return false if self.deleted?
self.url == self.wiki.get_front_page_url # wiki.get_front_page_url checks has_front_page? and context.draft_state_enabled?
end
def set_as_front_page!

View File

@ -200,9 +200,7 @@ $(document).ready(function() {
<label for="edit_course_home_content_select"><strong><%= t('labels.set_layout', %{When people visit the course page, show them}) %></strong></label>
<select name="course[default_view]" id="edit_course_home_content_select">
<option value="feed" <%= 'selected' if @context.default_view == 'feed' %>><%= t('options.recent_activity_dashboard', %{the Recent Activity Dashboard}) %></option>
<% if @context.wiki.has_front_page? %>
<option value="wiki" <%= 'selected' if @context.default_view == 'wiki' %>><%= t('options.custom_page', %{a Page I'll Design Myself}) %></option>
<% end %>
<option value="wiki" <%= 'selected' if @context.default_view == 'wiki' %>><%= t('options.custom_page', %{a Page I'll Design Myself}) %></option>
<option value="modules" <%= 'selected' if @context.default_view == 'modules' %>><%= t('options.modules', %{the Course Modules/Sections}) %></option>
<option value="assignments" <%= 'selected' if @context.default_view == 'assignments' %>><%= t('options.assignments', %{the Assignment List}) %></option>
<option value="syllabus" <%= 'selected' if @context.default_view == 'syllabus' %>><%= t('options.syllabus', %{the Assignments with Syllabus}) %></option>

View File

@ -69,7 +69,7 @@
<% if can_do(@page, @current_user, :update_content)%>
<%= link_to ('<i class="icon-edit"></i>').html_safe + " " + t('links.edit_page', "Edit this Page"), "#edit", :class => "edit_link btn button-sidebar-wide" %>
<% end %>
<% if can_do(@page, @current_user, :delete) && !@page.is_front_page? && !@page.deleted? %>
<% if can_do(@page, @current_user, :delete) && !@page.new_record? && !@page.is_front_page? && !@page.deleted? %>
<%= link_to ('<i class="icon-trash"></i>').html_safe + " " + t('links.delete_page', "Delete this Page"), context_url(@context, :context_wiki_page_url, @page.url), :method => :delete, :confirm => t('delete_page_confirmation', 'Are you sure you want to delete this page? This cannot be undone!'), :class => "btn button-sidebar-wide" %>
<% end %>
<a href="<%= context_url(@context, :context_wiki_page_url, @page) %>" class="wiki_page_url" style="display: none;">&nbsp;</a>

View File

@ -0,0 +1,14 @@
class FixImportedWikiFrontPages < ActiveRecord::Migration
tag :postdeploy
def self.up
DataFixup::FixImportedWikiFrontPages.send_later_if_production_enqueue_args(
:run,
:priority => Delayed::LOW_PRIORITY,
:max_attempts => 1
)
end
def self.down
end
end

View File

@ -0,0 +1,12 @@
module DataFixup::FixImportedWikiFrontPages
# some Wiki objects are getting has_no_front_page set to true, even when there should be a front page
def self.potentially_broken_wikis
Wiki.where(:has_no_front_page => true)
end
def self.run
self.potentially_broken_wikis.find_in_batches do |wikis|
Wiki.where(:id => wikis).update_all(:has_no_front_page => nil)
end
end
end

View File

@ -56,6 +56,7 @@ describe "Pages API", :type => :integration do
@front_page = @wiki.front_page
@front_page.workflow_state = 'active'
@front_page.save!
@front_page.set_as_front_page!
@hidden_page = @wiki.wiki_pages.create!(:title => "Hidden Page", :hide_from_students => true, :body => "Body of hidden page")
end
@ -270,12 +271,39 @@ describe "Pages API", :type => :integration do
json.should == expected
end
it "give a meaningful error if there is no front page" do
@wiki.unset_front_page!
it "should implicitly find the 'front-page' if no front page is set" do
@wiki.reload
@wiki.front_page_url = nil
@wiki.has_no_front_page = nil
@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}")
expected = { "hide_from_students" => false,
"editing_roles" => "teachers",
"url" => @front_page.url,
"html_url" => "http://www.example.com/courses/#{@course.id}/wiki/#{@front_page.url}",
"created_at" => @front_page.created_at.as_json,
"updated_at" => @front_page.updated_at.as_json,
"title" => @front_page.title,
"body" => @front_page.body,
"published" => true,
"front_page" => true,
"locked_for_user" => false,
}
json.should == expected
end
it "should give a meaningful error if there is no front page" do
@front_page.workflow_state = 'deleted'
@front_page.save!
wiki = @front_page.wiki
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}"},
{}, {}, {:expected_status => 404})
{}, {}, {:expected_status => 404})
json['message'].should == "No front page has been set"
end
@ -549,6 +577,9 @@ describe "Pages API", :type => :integration do
it "should un-set as front page" do
wiki = @course.wiki
wiki.reload
wiki.has_front_page?.should be_true
front_page = wiki.front_page
json = api_call(:put, "/api/v1/courses/#{@course.id}/pages/#{front_page.url}",
@ -561,7 +592,8 @@ describe "Pages API", :type => :integration do
front_page.is_front_page?.should be_false
wiki.reload
wiki.front_page.should be_nil
wiki.has_front_page?.should be_false
wiki.front_page.should be_new_record
json['front_page'].should == false
end

View File

@ -34,18 +34,17 @@ describe Wiki do
it "should unset front page" do
@wiki.unset_front_page!
@wiki.front_page_url.should == nil
@wiki.front_page.should == nil
@wiki.has_front_page?.should == false
@wiki.front_page_url.should == nil
end
end
context "set_front_page_url!" do
it "should set front_page_url" do
@wiki.unset_front_page!
new_url = "ponies4ever"
@wiki.set_front_page_url!(new_url).should == true
@wiki.has_front_page?.should == true
@wiki.front_page_url.should == new_url
end