require File.dirname(__FILE__) + '/../test_helper'
class ComatosePageTest < Test::Unit::TestCase
fixtures :comatose_pages
should "create page" do
assert_difference ComatosePage, :count do
page = create_page :title=>'New Page Name'
assert !page.new_record?, "#{page.errors.full_messages.to_sentence}"
end
end
should "create a new version of an updated page" do
page = create_page
assert_difference page, :version do
page.update_attribute :body, "I'm the new content!"
assert_equal "
I’m the new content!
", page.to_html
end
end
should "render content through textile and liquid processors" do
page = create_page :title=>'Title Here', :body=>'h1. {{page.title}}'
assert_equal "Title Here
", page.to_html
end
should "not allow creation of page when missing a title" do
assert_no_difference ComatosePage, :count do
p = create_page(:title => nil)
assert p.errors.on(:title)
end
end
should "have good fixtures for this to work out" do
assert_equal 'Home Page', root_page.title
assert_equal 'home-page', root_page.slug
assert_equal 'Comatose', root_page.author
assert_equal "", root_page.full_path
assert_equal 'faq', faq_page.full_path
end
should "generate slugs correctly" do
assert_equal 'hello-how-are-you', new_page_slug( "Hello, How Are You?" )
assert_equal 'i-have-too-much-space', new_page_slug( "I have too much space" )
assert_equal 'i-have-leading-and-trailing-space', new_page_slug( " I have leading and trailing space " )
assert_equal 'what-about-dashes', new_page_slug( "What about - dashes?" )
assert_equal 'a-bizarre-title', new_page_slug( 'A !@!@#$%^<>&*()_+{} Bizarre TiTle!' )
assert_equal '001-numbers-too', new_page_slug( "001 Numbers too" )
end
should "generate page paths correctly" do
products = root_page.children.create( :title=>'Products' )
assert_equal 'products', products.full_path
books = products.children.create( :title=>'Books' )
assert_equal 'products/books', books.full_path
novels = books.children.create( :title=>'Novels' )
assert_equal 'products/books/novels', novels.full_path
comics = books.children.create( :title=>'Comics' )
assert_equal 'products/books/comics', comics.full_path
end
should "update page paths when pages are moved" do
page = comatose_page :params
assert_equal 'params', page.full_path
q1pg = comatose_page :question_one
page.parent_id = q1pg.id
assert page.save, "Page.save"
page.reload
assert_equal 'faq/question-one/params', page.full_path
q1pg.reload
q1pg.slug = "q-1"
assert q1pg.save, "Page.save"
assert_equal "faq/q-1", q1pg.full_path
page.reload
assert_equal 'faq/q-1/params', page.full_path
end
should "set an AR error with processor syntax error info" do
page = create_page :title=>'Title Here', :body=>'h1. {% crap %}'
assert !page.save, page.errors.full_messages.to_sentence
end
should "render body text accurately" do
assert_equal "Home Page
\nThis is your home page.
", root_page.to_html
assert_equal "Frequently Asked Questions
\n\nContent for question one.
\n\nContent for question two.
", faq_page.to_html
end
should "render data from parameterized calls too" do
assert_equal "I’m
", param_driven_page.to_html
assert_equal "I’m From the Params Hash
", param_driven_page.to_html(:extra=>'From the Params Hash')
end
should "render data from a Drop" do
Comatose.define_drop "app" do
def test
"From Drop"
end
end
p = create_page(:title=>'Test Drop', :body=>'{{ app.test }}')
assert_equal "From Drop
", p.to_html
end
# Test illustrates broken partial, sort of
# <%= render :comatose => 'faq/question-one' %> should work, but does not
should "render inline partial" do
Comatose.config.default_processor = :erb
page = create_page :body => "Included: <%= ComatosePage.find_by_path('faq/question-one').to_html %>", :filter_type => :none
assert_equal "Included: Content for question one.
", page.to_html
end
protected
def new_page_slug(title)
create_page( :title=>title ).slug
end
def root_page
ComatosePage.root
end
def faq_page
comatose_page :faq
end
def param_driven_page
comatose_page :params
end
end