2010-11-08 17:42:14 +00:00
|
|
|
# coding: utf-8
|
|
|
|
|
2010-04-25 00:33:38 +00:00
|
|
|
require 'spec_helper'
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-25 00:33:38 +00:00
|
|
|
describe Page do
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
before(:each) do
|
|
|
|
Site.any_instance.stubs(:create_default_pages!).returns(true)
|
2011-03-05 00:51:44 +00:00
|
|
|
Page.any_instance.stubs(:set_default_raw_template).returns(true)
|
2010-05-10 22:39:52 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-25 00:33:38 +00:00
|
|
|
it 'should have a valid factory' do
|
|
|
|
Factory.build(:page).should be_valid
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
# Validations ##
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
%w{site title}.each do |field|
|
2010-04-25 00:33:38 +00:00
|
|
|
it "should validate presence of #{field}" do
|
|
|
|
page = Factory.build(:page, field.to_sym => nil)
|
|
|
|
page.should_not be_valid
|
|
|
|
page.errors[field.to_sym].should == ["can't be blank"]
|
|
|
|
end
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
it 'should validate presence of slug' do
|
|
|
|
page = Factory.build(:page, :title => nil, :slug => nil)
|
|
|
|
page.should_not be_valid
|
|
|
|
page.errors[:slug].should == ["can't be blank"]
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-30 14:05:53 +00:00
|
|
|
it 'should validate uniqueness of slug' do
|
2010-04-25 00:33:38 +00:00
|
|
|
page = Factory(:page)
|
|
|
|
(page = Factory.build(:page, :site => page.site)).should_not be_valid
|
2010-04-30 14:05:53 +00:00
|
|
|
page.errors[:slug].should == ["is already taken"]
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
it 'should validate uniqueness of slug within a "folder"' do
|
|
|
|
site = Factory(:site)
|
|
|
|
root = Factory(:page, :slug => 'index', :site => site)
|
|
|
|
child_1 = Factory(:page, :slug => 'first_child', :parent => root, :site => site)
|
|
|
|
(page = Factory.build(:page, :slug => 'first_child', :parent => root, :site => site)).should_not be_valid
|
|
|
|
page.errors[:slug].should == ["is already taken"]
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
page.slug = 'index'
|
|
|
|
page.valid?.should be_true
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-30 14:05:53 +00:00
|
|
|
%w{admin stylesheets images javascripts}.each do |slug|
|
|
|
|
it "should consider '#{slug}' as invalid" do
|
|
|
|
page = Factory.build(:page, :slug => slug)
|
|
|
|
page.should_not be_valid
|
|
|
|
page.errors[:slug].should == ["is reserved"]
|
|
|
|
end
|
2010-04-25 00:33:38 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
# Named scopes ##
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
# Associations ##
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
# Methods ##
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-25 00:33:38 +00:00
|
|
|
describe 'once created' do
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
it 'should tell if the page is the index one' do
|
|
|
|
Factory.build(:page, :slug => 'index', :site => nil).index?.should be_true
|
|
|
|
Factory.build(:page, :slug => 'index', :depth => 1, :site => nil).index?.should be_false
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-30 14:05:53 +00:00
|
|
|
it 'should have normalized slug' do
|
|
|
|
page = Factory.build(:page, :slug => ' Valid ité.html ')
|
|
|
|
page.valid?
|
|
|
|
page.slug.should == 'Valid_ite'
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
page = Factory.build(:page, :title => ' Valid ité.html ', :slug => nil, :site => page.site)
|
|
|
|
page.should be_valid
|
|
|
|
page.slug.should == 'Valid_ite'
|
2010-04-30 14:05:53 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-09 10:38:50 +00:00
|
|
|
it 'has no cache strategy' do
|
|
|
|
page = Factory.build(:page, :site => nil)
|
|
|
|
page.with_cache?.should == false
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-25 00:33:38 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-13 00:46:17 +00:00
|
|
|
describe '#deleting' do
|
2010-07-23 20:09:54 +00:00
|
|
|
|
|
|
|
before(:each) do
|
2010-05-10 22:39:52 +00:00
|
|
|
@page = Factory.build(:page)
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-13 00:46:17 +00:00
|
|
|
it 'does not delete the index page' do
|
2010-05-10 22:39:52 +00:00
|
|
|
@page.stubs(:index?).returns(true)
|
|
|
|
lambda {
|
2010-07-13 00:46:17 +00:00
|
|
|
@page.destroy.should be_false
|
|
|
|
@page.errors.first == 'You can not remove index or 404 pages'
|
|
|
|
}.should_not change(Page, :count)
|
2010-05-10 22:39:52 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-13 00:46:17 +00:00
|
|
|
it 'does not delete the 404 page' do
|
2010-05-10 22:39:52 +00:00
|
|
|
@page.stubs(:not_found?).returns(true)
|
|
|
|
lambda {
|
2010-07-13 00:46:17 +00:00
|
|
|
@page.destroy.should be_false
|
|
|
|
@page.errors.first == 'You can not remove index or 404 pages'
|
|
|
|
}.should_not change(Page, :count)
|
2010-05-10 22:39:52 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-10 22:39:52 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-30 14:05:53 +00:00
|
|
|
describe 'acts as tree' do
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-30 14:05:53 +00:00
|
|
|
before(:each) do
|
2010-07-23 20:09:54 +00:00
|
|
|
@home = Factory(:page)
|
2010-05-02 23:33:17 +00:00
|
|
|
@child_1 = Factory(:page, :title => 'Subpage 1', :slug => 'foo', :parent_id => @home._id, :site => @home.site)
|
2010-04-30 14:05:53 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-30 14:05:53 +00:00
|
|
|
it 'should add root elements' do
|
|
|
|
page_404 = Factory(:page, :title => 'Page not found', :slug => '404', :site => @home.site)
|
|
|
|
Page.roots.count.should == 2
|
|
|
|
Page.roots.should == [@home, page_404]
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-30 14:05:53 +00:00
|
|
|
it 'should add sub pages' do
|
2010-07-23 20:09:54 +00:00
|
|
|
child_2 = Factory(:page, :title => 'Subpage 2', :slug => 'bar', :parent => @home, :site => @home.site)
|
2010-10-14 14:01:18 +00:00
|
|
|
@home = Page.find(@home.id)
|
|
|
|
@home.children.count.should == 2
|
|
|
|
@home.children.should == [@child_1, child_2]
|
2010-04-30 14:05:53 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
it 'should move its children accordingly' do
|
|
|
|
sub_child_1 = Factory(:page, :title => 'Sub Subpage 1', :slug => 'bar', :parent => @child_1, :site => @home.site)
|
|
|
|
archives = Factory(:page, :title => 'archives', :slug => 'archives', :parent => @home, :site => @home.site)
|
|
|
|
posts = Factory(:page, :title => 'posts', :slug => 'posts', :parent => archives, :site => @home.site)
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
@child_1.parent_id = archives._id
|
|
|
|
@child_1.save
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
@child_1.position.should == 2
|
|
|
|
@home.reload.children.count.should == 1
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
archives.reload.children.count.should == 2
|
|
|
|
archives.children.last.depth.should == 2
|
|
|
|
archives.children.last.position.should == 2
|
|
|
|
archives.children.last.children.first.depth.should == 3
|
2010-04-30 14:05:53 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
it 'should destroy descendants as well' do
|
|
|
|
Factory(:page, :title => 'Sub Subpage 1', :slug => 'bar', :parent_id => @child_1._id, :site => @home.site)
|
|
|
|
@child_1.destroy
|
|
|
|
Page.where(:slug => 'bar').first.should be_nil
|
2010-04-30 14:05:53 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-04-30 14:05:53 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-02 23:33:17 +00:00
|
|
|
describe 'acts as list' do
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-09 12:44:53 +00:00
|
|
|
before(:each) do
|
|
|
|
@home = Factory(:page)
|
|
|
|
@child_1 = Factory(:page, :title => 'Subpage 1', :slug => 'foo', :parent => @home, :site => @home.site)
|
2010-07-23 20:09:54 +00:00
|
|
|
@child_2 = Factory(:page, :title => 'Subpage 2', :slug => 'bar', :parent => @home, :site => @home.site)
|
2010-05-09 12:44:53 +00:00
|
|
|
@child_3 = Factory(:page, :title => 'Subpage 3', :slug => 'acme', :parent => @home, :site => @home.site)
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-09 12:44:53 +00:00
|
|
|
it 'should be at the bottom of the folder once created' do
|
|
|
|
[@child_1, @child_2, @child_3].each_with_index { |c, i| c.position.should == i + 1 }
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-09 12:44:53 +00:00
|
|
|
it 'should have its position updated if a sibling is removed' do
|
|
|
|
@child_2.destroy
|
|
|
|
[@child_1, @child_3.reload].each_with_index { |c, i| c.position.should == i + 1 }
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-05-30 23:57:33 +00:00
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-16 20:36:07 +00:00
|
|
|
describe 'templatized extension' do
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-16 20:36:07 +00:00
|
|
|
before(:each) do
|
|
|
|
@page = Factory.build(:page, :site => nil, :templatized => true, :content_type_id => 42)
|
|
|
|
ContentType.stubs(:find).returns(Factory.build(:content_type, :site => nil))
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-16 20:36:07 +00:00
|
|
|
it 'is considered as a templatized page' do
|
|
|
|
@page.templatized?.should be_true
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-16 20:36:07 +00:00
|
|
|
it 'fills in the slug field' do
|
|
|
|
@page.valid?
|
|
|
|
@page.slug.should == 'content_type_template'
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
|
2010-07-16 20:36:07 +00:00
|
|
|
it 'does forget to set the content type id' do
|
2010-07-26 02:49:35 +00:00
|
|
|
@page.content_type_id.should == 42
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|
|
|
|
|
2010-07-16 20:36:07 +00:00
|
|
|
end
|
2011-03-05 00:51:44 +00:00
|
|
|
|
2011-02-01 08:07:18 +00:00
|
|
|
describe 'listed extension' do
|
2011-03-05 00:51:44 +00:00
|
|
|
|
2011-02-01 08:07:18 +00:00
|
|
|
it 'is considered as a visible page' do
|
|
|
|
@page = Factory.build(:page, :site => nil, :content_type_id => 42)
|
|
|
|
@page.listed?.should be_true
|
|
|
|
end
|
2011-03-05 00:51:44 +00:00
|
|
|
|
2011-02-01 08:07:18 +00:00
|
|
|
it 'is not considered as a visible page' do
|
|
|
|
@page = Factory.build(:page, :site => nil, :listed => false, :content_type_id => 42)
|
|
|
|
@page.listed?.should be_false
|
2011-02-01 06:39:20 +00:00
|
|
|
end
|
2011-03-05 00:51:44 +00:00
|
|
|
|
2011-02-01 06:39:20 +00:00
|
|
|
end
|
2011-01-06 14:48:41 +00:00
|
|
|
|
|
|
|
describe 'redirect extension' do
|
|
|
|
|
|
|
|
before(:each) do
|
|
|
|
@page = Factory.build(:page, :site => nil, :redirect=> true, :redirect_url => 'http://www.google.com/')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'is considered as a redirect page' do
|
|
|
|
@page.redirect?.should be_true
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'validates the redirect_url if redirect is set' do
|
|
|
|
@page.redirect_url = nil
|
|
|
|
@page.should_not be_valid
|
|
|
|
@page.errors[:redirect_url].should == ["can't be blank"]
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'should validate format of redirect_url' do
|
|
|
|
@page.redirect_url = "invalid url with spaces"
|
|
|
|
@page.should_not be_valid
|
|
|
|
@page.errors[:redirect_url].should == ["is invalid"]
|
|
|
|
end
|
|
|
|
end
|
2010-07-23 20:09:54 +00:00
|
|
|
end
|