spec/models/refinery/page_spec.rb in refinerycms-pages-2.0.10 vs spec/models/refinery/page_spec.rb in refinerycms-pages-2.1.0
- old
+ new
@@ -1,11 +1,10 @@
# encoding: utf-8
require 'spec_helper'
module Refinery
describe Page do
-
let(:page_title) { 'RSpec is great for testing too' }
let(:child_title) { 'The child page' }
# For when we do not need the page persisted.
let(:page) { subject.class.new(:title => page_title, :deletable => true)}
@@ -14,21 +13,30 @@
# For when we need the page persisted.
let(:created_page) { subject.class.create!(:title => page_title, :deletable => true) }
let(:created_child) { created_page.children.create!(:title => child_title) }
def page_cannot_be_destroyed
+ page.should_receive(:puts_destroy_help)
page.destroy.should == false
end
def turn_off_marketable_urls
- Refinery::Pages.stub(:marketable_urls).and_return(false)
+ Pages.stub(:marketable_urls).and_return(false)
end
def turn_on_marketable_urls
- Refinery::Pages.stub(:marketable_urls).and_return(true)
+ Pages.stub(:marketable_urls).and_return(true)
end
+ def turn_off_slug_scoping
+ Pages.stub(:scope_slug_by_parent).and_return(false)
+ end
+
+ def turn_on_slug_scoping
+ Pages.stub(:scope_slug_by_parent).and_return(true)
+ end
+
context 'cannot be deleted under certain rules' do
it 'if link_url is present' do
page.link_url = '/plugin-name'
page_cannot_be_destroyed
end
@@ -104,23 +112,23 @@
end
end
context 'canonicals' do
before do
- ::Refinery::I18n.stub(:default_frontend_locale).and_return(:en)
- ::Refinery::I18n.stub(:frontend_locales).and_return([Refinery::I18n.default_frontend_locale, :ru])
- ::Refinery::I18n.stub(:current_frontend_locale).and_return(Refinery::I18n.default_frontend_locale)
+ Refinery::I18n.stub(:default_frontend_locale).and_return(:en)
+ Refinery::I18n.stub(:frontend_locales).and_return([I18n.default_frontend_locale, :ru])
+ Refinery::I18n.stub(:current_frontend_locale).and_return(I18n.default_frontend_locale)
page.save
end
let(:page_title) { 'team' }
let(:child_title) { 'about' }
let(:ru_page_title) { 'Новости' }
describe '#canonical' do
let!(:default_canonical) {
- Globalize.with_locale(::Refinery::I18n.default_frontend_locale) {
+ Globalize.with_locale(Refinery::I18n.default_frontend_locale) {
page.canonical
}
}
specify 'page returns itself' do
@@ -141,11 +149,11 @@
end
end
describe '#canonical_slug' do
let!(:default_canonical_slug) {
- Globalize.with_locale(::Refinery::I18n.default_frontend_locale) {
+ Globalize.with_locale(Refinery::I18n.default_frontend_locale) {
page.canonical_slug
}
}
specify 'page returns its own slug' do
page.canonical_slug.should == page.slug
@@ -167,68 +175,122 @@
end
context 'custom slugs' do
let(:custom_page_slug) { 'custom-page-slug' }
let(:custom_child_slug) { 'custom-child-slug' }
+ let(:custom_route) { '/products/my-product' }
let(:page_with_custom_slug) {
subject.class.new(:title => page_title, :custom_slug => custom_page_slug)
}
let(:child_with_custom_slug) {
page.children.new(:title => child_title, :custom_slug => custom_child_slug)
}
+ let(:page_with_custom_route) {
+ subject.class.new(:title => page_title, :custom_slug => custom_route)
+ }
after(:each) do
- ::Refinery::I18n.stub(:current_frontend_locale).and_return(Refinery::I18n.default_frontend_locale)
- ::Refinery::I18n.current_locale = Refinery::I18n.default_locale
+ Refinery::I18n.stub(:current_frontend_locale).and_return(I18n.default_frontend_locale)
+ Refinery::I18n.stub(:current_locale).and_return(I18n.default_locale)
end
it 'returns its path with custom slug' do
page_with_custom_slug.save
page_with_custom_slug.url[:id].should be_nil
page_with_custom_slug.url[:path].should == [custom_page_slug]
end
+ it 'allows a custom route when slug scoping is off' do
+ turn_off_slug_scoping
+ page_with_custom_route.save
+ page_with_custom_route.url[:id].should be_nil
+ page_with_custom_route.url[:path].should == [custom_route]
+ turn_on_slug_scoping
+ end
+
+ it 'allows slashes in custom routes but slugs everything in between' do
+ turn_off_slug_scoping
+ page_needing_a_slugging = subject.class.new(:title => page_title, :custom_slug => 'products/category/sub category/my product is cool!')
+ page_needing_a_slugging.save
+ page_needing_a_slugging.url[:id].should be_nil
+ page_needing_a_slugging.url[:path].should == ['products/category/sub-category/my-product-is-cool']
+ turn_on_slug_scoping
+ end
+
it 'returns its path underneath its parent with custom urls' do
child_with_custom_slug.save
page.save
child_with_custom_slug.url[:id].should be_nil
child_with_custom_slug.url[:path].should == [page.url[:path].first, custom_child_slug]
end
+ it 'does not return a path underneath its parent when scoping is off' do
+ turn_off_slug_scoping
+ child_with_custom_slug.save
+ page.save
+
+ child_with_custom_slug.url[:id].should be_nil
+ child_with_custom_slug.url[:path].should == [custom_child_slug]
+ turn_on_slug_scoping
+ end
+
+ it "doesn't allow slashes in slug" do
+ page_with_slashes_in_slug = subject.class.new(:title => page_title, :custom_slug => '/products/category')
+ page_with_slashes_in_slug.save
+ page_with_slashes_in_slug.url[:path].should == ['productscategory']
+ end
+
+ it "allow slashes in slug when slug scoping is off" do
+ turn_off_slug_scoping
+ page_with_slashes_in_slug = subject.class.new(:title => page_title, :custom_slug => 'products/category/subcategory')
+ page_with_slashes_in_slug.save
+ page_with_slashes_in_slug.url[:path].should == ['products/category/subcategory']
+ turn_on_slug_scoping
+ end
+
+ it "strips leading and trailing slashes in slug when slug scoping is off" do
+ turn_off_slug_scoping
+ page_with_slashes_in_slug = subject.class.new(:title => page_title, :custom_slug => '/products/category/subcategory/')
+ page_with_slashes_in_slug.save
+ page_with_slashes_in_slug.url[:path].should == ['products/category/subcategory']
+ turn_on_slug_scoping
+ end
+
it 'returns its path with custom slug when using different locale' do
- ::Refinery::I18n.stub(:current_frontend_locale).and_return(:ru)
- ::Refinery::I18n.current_locale = :ru
+ Refinery::I18n.stub(:current_frontend_locale).and_return(:ru)
+ Refinery::I18n.stub(:current_locale).and_return(:ru)
page_with_custom_slug.custom_slug = "#{custom_page_slug}-ru"
page_with_custom_slug.save
page_with_custom_slug.reload
page_with_custom_slug.url[:id].should be_nil
page_with_custom_slug.url[:path].should == ["#{custom_page_slug}-ru"]
end
it 'returns path underneath its parent with custom urls when using different locale' do
- ::Refinery::I18n.stub(:current_frontend_locale).and_return(:ru)
- ::Refinery::I18n.current_locale = :ru
+ Refinery::I18n.stub(:current_frontend_locale).and_return(:ru)
+ Refinery::I18n.stub(:current_locale).and_return(:ru)
child_with_custom_slug.custom_slug = "#{custom_child_slug}-ru"
child_with_custom_slug.save
child_with_custom_slug.reload
child_with_custom_slug.url[:id].should be_nil
child_with_custom_slug.url[:path].should == [page.url[:path].first, "#{custom_child_slug}-ru"]
end
- end
+ context "given a page with a custom_slug exists" do
+ before do
+ FactoryGirl.create(:page, :custom_slug => custom_page_slug)
+ end
- context 'home page' do
- it 'responds as the home page' do
- page.link_url = '/'
- page.home?.should == true
- end
+ it "fails validation when a new record uses that custom_slug" do
+ new_page = Page.new :custom_slug => custom_page_slug
+ new_page.valid?
- it 'responds as a normal page when not set to home page' do
- page.home?.should == false
+ new_page.errors[:custom_slug].should_not be_empty
+ end
end
end
context 'content sections (page parts)' do
before do
@@ -239,14 +301,43 @@
it 'return the content when using content_for' do
page.content_for(:body).should == "<p>I'm the first page part for this page.</p>"
page.content_for('BoDY').should == "<p>I'm the first page part for this page.</p>"
end
- it 'return all page part content' do
- page.all_page_part_content.should == "<p>I'm the first page part for this page.</p> <p>Closely followed by the second page part.</p>"
+ it 'requires a unique title' do
+ page.save
+ page.parts.create(:title => 'body')
+ duplicate_title_part = page.parts.create(:title => 'body')
+
+ duplicate_title_part.errors[:title].should be_present
end
+ it 'only requires a unique title on the same page' do
+ part_one = Page.create(:title => 'first page').parts.create(:title => 'body')
+ part_two = Page.create(:title => 'second page').parts.create(:title => 'body')
+
+ part_two.errors[:title].should be_empty
+ end
+
+ context 'when using content_for?' do
+
+ it 'return true when page part has content' do
+ page.content_for?(:body).should be_true
+ end
+
+ it 'return false when page part does not exist' do
+ page.parts = []
+ page.content_for?(:body).should be_false
+ end
+
+ it 'return false when page part does not have any content' do
+ page.parts.first.content = ''
+ page.content_for?(:body).should be_false
+ end
+
+ end
+
it 'reposition correctly' do
page.save
page.parts.first.update_attributes :position => 6
page.parts.last.update_attributes :position => 4
@@ -293,29 +384,20 @@
end
end
context 'meta data' do
context 'responds to' do
- it 'meta_keywords' do
- page.respond_to?(:meta_keywords)
- end
-
it 'meta_description' do
page.respond_to?(:meta_description)
end
it 'browser_title' do
page.respond_to?(:browser_title)
end
end
context 'allows us to assign to' do
- it 'meta_keywords' do
- page.meta_keywords = 'Some, great, keywords'
- page.meta_keywords.should == 'Some, great, keywords'
- end
-
it 'meta_description' do
page.meta_description = 'This is my description of the page for search results.'
page.meta_description.should == 'This is my description of the page for search results.'
end
@@ -324,18 +406,10 @@
page.browser_title.should == 'An awesome browser title for SEO'
end
end
context 'allows us to update' do
- it 'meta_keywords' do
- page.meta_keywords = 'Some, great, keywords'
- page.save
-
- page.reload
- page.meta_keywords.should == 'Some, great, keywords'
- end
-
it 'meta_description' do
page.meta_description = 'This is my description of the page for search results.'
page.save
page.reload
@@ -353,16 +427,16 @@
end
describe "#to_refinery_menu_item" do
let(:page) do
- Refinery::Page.new(
+ Page.new(
:id => 5,
:parent_id => 8,
:menu_match => "^/foo$"
- # Refinery::Page does not allow setting lft and rgt, so stub them.
+ # Page does not allow setting lft and rgt, so stub them.
).tap do |p|
p[:lft] = 6
p[:rgt] = 7
end
end
@@ -385,34 +459,10 @@
subject[:url].should be_a(Hash) # guard against nil
subject[:url].should eq(page.url)
end
end
- context "with #page_menu_title" do
- before do
- page.page_menu_title = "Page Menu Title"
- end
-
- it_should_behave_like "Refinery menu item hash"
-
- it "returns the page_menu_title for :title" do
- subject[:title].should eq("Page Menu Title")
- end
- end
-
- context "with #page_title" do
- before do
- page.page_title = "Page Title"
- end
-
- it_should_behave_like "Refinery menu item hash"
-
- it "returns the page_title for :title" do
- subject[:title].should eq("Page Title")
- end
- end
-
context "with #menu_title" do
before do
page[:menu_title] = "Menu Title"
end
@@ -434,11 +484,11 @@
subject[:title].should eq("Title")
end
end
end
- describe ".in_menu?" do
+ describe "#in_menu?" do
context "when live? and show_in_menu? returns true" do
it "returns true" do
page.stub(:live?).and_return(true)
page.stub(:show_in_menu?).and_return(true)
page.in_menu?.should be_true
@@ -456,11 +506,11 @@
page.in_menu?.should be_false
end
end
end
- describe ".not_in_menu?" do
+ describe "#not_in_menu?" do
context "when in_menu? returns true" do
it "returns false" do
page.stub(:in_menu?).and_return(true)
page.not_in_menu?.should be_false
end
@@ -484,14 +534,89 @@
created_child
created_root_about
end
it "should return (root) about page when looking for '/about'" do
- Refinery::Page.find_by_path('/about').should == created_root_about
+ Page.find_by_path('/about').should == created_root_about
end
it "should return child about page when looking for '/team/about'" do
- Refinery::Page.find_by_path('/team/about').should == created_child
+ Page.find_by_path('/team/about').should == created_child
+ end
+ end
+
+ describe ".find_by_path_or_id" do
+ let!(:market) { FactoryGirl.create(:page, :title => "market") }
+ let(:path) { "market" }
+ let(:id) { market.id }
+
+ context "when path param is present" do
+ context "when path is friendly_id" do
+ it "finds page using path" do
+ Page.find_by_path_or_id(path, "").should eq(market)
+ end
+ end
+
+ context "when path is not friendly_id" do
+ it "finds page using id" do
+ Page.find_by_path_or_id(id, "").should eq(market)
+ end
+ end
+ end
+
+ context "when id param is present" do
+ it "finds page using id" do
+ Page.find_by_path_or_id("", id).should eq(market)
+ end
+ end
+ end
+
+ describe "#deletable?" do
+ let(:deletable_page) do
+ page.deletable = true
+ page.link_url = ""
+ page.menu_match = ""
+ page.stub(:puts_destroy_help).and_return('')
+ page
+ end
+
+ context "when deletable is true and link_url, and menu_match is blank" do
+ it "returns true" do
+ deletable_page.deletable?.should be_true
+ end
+ end
+
+ context "when deletable is false and link_url, and menu_match is blank" do
+ it "returns false" do
+ deletable_page.deletable = false
+ deletable_page.deletable?.should be_false
+ end
+ end
+
+ context "when deletable is false and link_url or menu_match isn't blank" do
+ it "returns false" do
+ deletable_page.deletable = false
+ deletable_page.link_url = "text"
+ deletable_page.deletable?.should be_false
+
+ deletable_page.menu_match = "text"
+ deletable_page.deletable?.should be_false
+ end
+ end
+ end
+
+ describe "#destroy" do
+ before do
+ page.deletable = false
+ page.link_url = "link_url"
+ page.menu_match = "menu_match"
+ page.save!
+ end
+
+ it "shows message" do
+ page.should_receive(:puts_destroy_help)
+
+ page.destroy
end
end
end
end