require File.dirname(__FILE__) + '/../spec_helper' class PageSpecTestPage < Page description 'this is just a test page' def headers { 'cool' => 'beans', 'request' => @request.inspect[20..30], 'response' => @response.inspect[20..31] } end tag 'test1' do 'Hello world!' end tag 'test2' do 'Another test.' end end describe Page, 'validations' do dataset :pages test_helper :validations let(:page){ Page.new(page_params)} before :each do @page = @model = Page.new(page_params) end it 'should not be valid with a slug length greater than 100 characters' do page.valid?.should be_true page.slug = 'x'*101 page.valid?.should be_false end it 'should not be valid with a title length greater than 255 characters' do page.valid?.should be_true page.title = 'x'*256 page.valid?.should be_false end it 'should not be valid with a breadcrumb length greater than 160 characters' do page.valid?.should be_true page.breadcrumb = 'x'*161 page.valid?.should be_false end it 'should validate length of' do { :title => 255, :slug => 100, :breadcrumb => 160 }.each do |field, max| assert_invalid field, ('this must not be longer than %d characters' % max), 'x' * (max + 1) assert_valid field, 'x' * max end end it 'should validate presence of' do [:title, :slug, :breadcrumb].each do |field| assert_invalid field, 'this must not be blank', '', ' ', nil end end it 'should validate format of' do @page.parent = pages(:home) assert_valid :slug, 'abc', 'abcd-efg', 'abcd_efg', 'abc.html', '/', '123' assert_invalid :slug, 'this does not match the expected format', 'abcd efg', ' abcd', 'abcd/efg' end it 'should validate uniqueness of' do @page.parent = pages(:parent) assert_invalid :slug, 'this slug is already in use by a sibling of this page', 'child', 'child-2', 'child-3' assert_valid :slug, 'child-4' end it 'should allow mass assignment for class name' do @page.attributes = { :class_name => 'ArchivePage' } assert_valid @page @page.class_name.should == 'ArchivePage' end it 'should not be valid when class name is not a descendant of page' do @page.class_name = 'Object' @page.valid?.should == false assert_not_nil @page.errors.on(:class_name) @page.errors.on(:class_name).should == 'must be set to a valid descendant of Page' end it 'should not be valid when class name is not a descendant of page and it is set through mass assignment' do @page.attributes = {:class_name => 'Object' } @page.valid?.should == false assert_not_nil @page.errors.on(:class_name) @page.errors.on(:class_name).should == 'must be set to a valid descendant of Page' end it 'should be valid when class name is page or empty or nil' do [nil, '', 'Page'].each do |value| @page = ArchivePage.new(page_params) @page.class_name = value assert_valid @page @page.class_name.should == value end end end describe Page, "behaviors" do it 'should include' do Page.included_modules.should include(StandardTags) Page.included_modules.should include(Annotatable) end end describe Page, "layout" do dataset :pages_with_layouts it 'should be accessible' do @page = pages(:first) @page.layout_id.should == layout_id(:main) @page.layout.name.should == "Main" end it 'should be inherited' do @page = pages(:inherited_layout) @page.layout_id.should == nil @page.layout.should == @page.parent.layout end end describe Page do dataset :pages let(:page){ pages(:first ) } let(:home){ pages(:home) } let(:parent){ pages(:parent) } let(:child){ pages(:child) } let(:part){ page.parts(:body) } describe '#parts' do it 'should return PageParts with a page_id of the page id' do home.parts.sort_by{|p| p.name }.should == PagePart.find_all_by_page_id(home.id).sort_by{|p| p.name } end end it 'should destroy dependant parts' do page.parts.create(page_part_params(:name => 'test')) page.parts.find_by_name('test').should_not be_nil id = page.id page.destroy PagePart.find_by_page_id(id).should be_nil end describe '#part' do it 'should find the part with a name of the given string' do page.part('body').should == page.parts.find_by_name('body') end it 'should find the part with a name of the given symbol' do page.part(:body).should == page.parts.find_by_name('body') end it 'should access unsaved parts by name' do part = PagePart.new(:name => "test") page.parts << part page.part('test').should == part page.part(:test).should == part end it 'should return nil for an invalid part name' do page.part('not-real').should be_nil end end describe '#field' do it "should find a field" do page.fields.create(:name => 'keywords', :content => 'radiant') page.field(:keywords).should == page.fields.find_by_name('keywords') end it "should find an unsaved field" do field = PageField.new(:name => 'description', :content => 'radiant') page.fields << field page.field(:description).should == field end end describe '#has_part?' do it 'should return true for a valid part' do page.has_part?('body').should == true page.has_part?(:body).should == true end it 'should return false for a non-existant part' do page.has_part?('obviously_false_part_name').should == false page.has_part?(:obviously_false_part_name).should == false end end describe '#inherits_part?' do it 'should return true if any ancestor page has a part of the given name' do child.has_part?(:sidebar).should be_false child.inherits_part?(:sidebar).should be_true end it 'should return false if any ancestor page does not have a part of the given name' do home.has_part?(:sidebar).should be_true home.inherits_part?(:sidebar).should be_false end end describe '#has_or_inherits_part?' do it 'should return true if the current page or any ancestor has a part of the given name' do child.has_or_inherits_part?(:sidebar).should be_true end it 'should return false if the current part or any ancestor does not have a part of the given name' do child.has_or_inherits_part?(:obviously_false_part_name).should be_false end end it "should accept new page parts as an array of PageParts" do page.parts = [PagePart.new(:name => 'body', :content => 'Hello, world!')] page.parts.size.should == 1 page.parts.first.should be_kind_of(PagePart) page.parts.first.name.should == 'body' page.parts.first.content.should == 'Hello, world!' end it "should dirty the page object when only changing parts" do lambda do page.dirty?.should be_false page.parts = [PagePart.new(:name => 'body', :content => 'Hello, world!')] page.dirty?.should be_true end end describe '#published?' do it "should be true when the status is Status[:published]" do page.status = Status[:published] page.published?.should be_true end it "should be false when the status is not Status[:published]" do page.status = Status[:draft] page.published?.should be_false end end describe '#scheduled?' do it "should be true when the status is Status[:scheduled]" do page.status = Status[:scheduled] page.scheduled?.should be_true end it "should be false when the status is not Status[:scheduled]" do page.status = Status[:published] page.scheduled?.should be_false end end context 'when setting the published_at date' do it 'should change its status to scheduled with a date in the future' do new_page = Page.new(page_params(:status_id => '100', :published_at => '2020-1-1')) new_page.save new_page.status_id.should == 90 end it 'should set the status to published when the date is in the past' do scheduled_time = Time.zone.now - 1.year p = Page.new(page_params(:status_id => '90', :published_at => scheduled_time)) p.save p.status_id.should == 100 end it 'should interpret the input date correctly when the current language is not English' do I18n.locale = :nl page.update_attribute(:published_at, "17 mei 2011") #page.published_at.month.should == 5 I18n.locale = :en end end context 'when setting the status' do it 'should set published_at when given the published status id' do page = Page.new(page_params(:status_id => '100', :published_at => nil)) page.status_id = Status[:published].id page.save page.published_at.utc.day.should == Time.now.utc.day end it 'should change its status to draft when set to draft' do scheduled = pages(:scheduled) scheduled.status_id = '1' scheduled.save scheduled.status_id.should == 1 end it 'should not update published_at when already published' do new_page = Page.new(page_params(:status_id => Status[:published].id)) expected = new_page.published_at new_page.save new_page.published_at.should == expected end end describe '#path' do it "should start with a slash" do page.path.should match(/^\//) end it "should return a string with the current page's slug catenated with it's ancestor's slugs and delimited by slashes" do pages(:grandchild).path.should == '/parent/child/grandchild/' end it 'should end with a slash' do page.path.should match(/\/$/) end end describe '#child_path' do it 'should return the #path for the given child' do parent.child_path(child).should == '/parent/child/' end end describe '#status' do it 'should return the Status with the id of the page status_id' do home.status.should == Status.find(home.status_id) end end describe '#status=' do it 'should set the status_id to the id of the given Status' do home.status = Status[:draft] home.status_id.should == Status[:draft].id end end its(:cache?){ should be_true } its(:virtual?){ should be_false } it 'should support optimistic locking' do p1, p2 = Page.find(page_id(:first)), Page.find(page_id(:first)) p1.update_attributes!(:breadcrumb => "foo") lambda { p2.update_attributes!(:breadcrumb => "blah") }.should raise_error(ActiveRecord::StaleObjectError) end it "should list descendants" do children = page.allowed_children children.should include(FileNotFoundPage) end it "should reject pages that aren't in the menu" do FileNotFoundPage.in_menu false page.allowed_children.should_not include(FileNotFoundPage) end end describe Page, "before save filter" do dataset :home_page before :each do Page.create(page_params(:title =>"Month Index", :class_name => "ArchiveMonthIndexPage")) @page = Page.find_by_title("Month Index") end it 'should set the class name correctly' do @page.should be_kind_of(ArchiveMonthIndexPage) end it 'should set the virtual bit correctly' do @page.virtual?.should == true @page.virtual.should == true end it 'should update virtual based on new class name' do # turn a regular page into a virtual page @page.class_name = "ArchiveMonthIndexPage" @page.save.should == true @page.virtual?.should == true @page.send(:read_attribute, :virtual).should == true # turn a virtual page into a non-virtual one ["", nil, "Page", "PageSpecTestPage"].each do |value| @page = ArchiveYearIndexPage.create(page_params) @page.class_name = value @page.save.should == true @page = Page.find @page.id @page.should be_instance_of(Page.descendant_class(value)) @page.virtual?.should == false @page.send(:read_attribute, :virtual).should == false end end end describe Page, "rendering" do dataset :pages, :markup_pages, :snippets, :layouts test_helper :render before :each do @page = pages(:home) end it 'should render' do @page.render.should == 'Hello world!' end it 'should render with a filter' do pages(:textile).render.should == '
Some Textile content.
' end it 'should render with tags' do pages(:radius).render.should == "Radius body." end it 'should render with a layout' do @page.update_attribute(:layout_id, layout_id(:main)) @page.render.should == "\n \nmarkdown
}) end it 'should render a snippet with a tag' do @page.render_snippet(snippets(:radius)).should == 'Home' end it 'should render custom pages with tags' do create_page "Test Page", :body => "