2011-02-13 13:40:55 +00:00
require 'spec_helper'
2011-06-23 12:57:52 +00:00
describe Locomotive :: Liquid :: Tags :: SEO do
2011-06-23 08:14:36 +00:00
2011-04-29 20:10:13 +00:00
let ( :site ) do
2011-08-25 21:28:56 +00:00
FactoryGirl . build ( :site , :seo_title = > 'Site title (SEO)' , :meta_description = > 'A short site description' , :meta_keywords = > 'test only cat dog' )
2011-02-13 13:40:55 +00:00
end
2011-04-29 20:10:13 +00:00
describe 'rendering' do
2011-06-23 08:14:36 +00:00
2011-06-23 12:57:52 +00:00
it 'renders everything' do
html = render_seo
html . should include '<title>Site title (SEO)</title>'
html . should include '<meta name="description" content="A short site description" />'
html . should include '<meta name="keywords" content="test only cat dog" />'
end
it 'renders a seo title' do
render_seo_title . should include '<title>Site title (SEO)</title>'
end
it 'renders the site title if no seo title is provided' do
site . seo_title = nil
render_seo_title . should include '<title>Acme Website</title>'
end
it 'renders a meta description tag' do
2011-02-13 13:40:55 +00:00
render_seo_metadata . should include '<meta name="description" content="A short site description" />'
end
it 'strips and removes quote characters from the description' do
2011-04-29 20:10:13 +00:00
site . meta_description = ' String with " " quotes '
2011-02-13 13:40:55 +00:00
render_seo_metadata . should include '<meta name="description" content="String with quotes" />'
end
it 'renders a meta keywords tag' do
render_seo_metadata . should include '<meta name="keywords" content="test only cat dog" />'
end
it 'strips and removes quote characters from the keywords' do
2011-04-29 20:10:13 +00:00
site . meta_keywords = ' one " two " three '
2011-02-13 13:40:55 +00:00
render_seo_metadata . should include '<meta name="keywords" content="one two three" />'
end
2011-06-23 08:14:36 +00:00
it 'renders an empty string if no meta' do
site . meta_keywords = nil
render_seo_metadata . should include '<meta name="keywords" content="" />'
end
2011-04-29 20:10:13 +00:00
context " when page " do
2011-06-23 08:14:36 +00:00
2011-06-23 12:57:52 +00:00
context " has seo title " do
let ( :page ) { site . pages . build ( :seo_title = > 'Page title (SEO)' , :meta_keywords = > 'hulk,gamma' , :meta_description = > " Bruce Banner " ) }
subject { render_seo_title ( 'page' = > page ) }
it { should include ( %Q[ <title>Page title (SEO)</title> ] ) }
end
context " does not have seo title " do
let ( :page ) { site . pages . build }
subject { render_seo_title ( 'page' = > page ) }
it { should include ( %Q[ <title>Site title (SEO)</title> ] ) }
end
2011-04-29 20:10:13 +00:00
context " has metadata " do
let ( :page ) { site . pages . build ( :meta_keywords = > 'hulk,gamma' , :meta_description = > " Bruce Banner " ) }
subject { render_seo_metadata ( 'page' = > page ) }
it { should include ( %Q[ <meta name="keywords" content=" #{ page . meta_keywords } " /> ] ) }
it { should include ( %Q[ <meta name="description" content=" #{ page . meta_description } " /> ] ) }
end
context " does not have metadata " do
2011-06-23 08:14:36 +00:00
let ( :page ) { site . pages . build }
2011-04-29 20:10:13 +00:00
subject { render_seo_metadata ( 'page' = > page ) }
it { should include ( %Q[ <meta name="keywords" content=" #{ site . meta_keywords } " /> ] ) }
it { should include ( %Q[ <meta name="description" content=" #{ site . meta_description } " /> ] ) }
end
2011-06-23 12:57:52 +00:00
2011-04-29 20:10:13 +00:00
end
2011-06-23 08:14:36 +00:00
2011-04-29 20:10:13 +00:00
context " when content instance " do
2011-06-23 08:14:36 +00:00
2011-04-29 20:10:13 +00:00
let ( :content_type ) do
2011-08-25 21:28:56 +00:00
FactoryGirl . build ( :content_type , :site = > site ) . tap do | ct |
2011-12-22 23:45:32 +00:00
ct . entries_custom_fields . build :label = > 'anything' , :type = > 'String'
2011-04-29 20:10:13 +00:00
end
end
2011-02-13 13:40:55 +00:00
2011-06-23 12:57:52 +00:00
context " has seo title " do
2011-12-22 23:45:32 +00:00
let ( :content_entry ) { content_type . entries . build ( :seo_title = > 'Content title (SEO)' , :meta_keywords = > 'Libidinous, Angsty' , :meta_description = > " Quite the combination. " ) }
subject { render_seo_title ( 'content_entry' = > content_entry ) }
2011-06-23 12:57:52 +00:00
it { should include ( %Q[ <title>Content title (SEO)</title> ] ) }
end
context " does not have seo title " do
2011-12-22 23:45:32 +00:00
let ( :content_entry ) { content_type . entries . build }
subject { render_seo_title ( 'content_entry' = > content_entry ) }
2011-06-23 12:57:52 +00:00
it { should include ( %Q[ <title>Site title (SEO)</title> ] ) }
end
2011-04-29 20:10:13 +00:00
context " has metadata " do
2011-12-22 23:45:32 +00:00
let ( :content_entry ) { content_type . entries . build ( :meta_keywords = > 'Libidinous, Angsty' , :meta_description = > " Quite the combination. " ) }
subject { render_seo_metadata ( 'content_entry' = > content_entry ) }
it { should include ( %Q[ <meta name="keywords" content=" #{ content_entry . meta_keywords } " /> ] ) }
it { should include ( %Q[ <meta name="description" content=" #{ content_entry . meta_description } " /> ] ) }
2011-04-29 20:10:13 +00:00
end
context " does not have metadata " do
2011-12-22 23:45:32 +00:00
let ( :content_entry ) { content_type . entries . build }
subject { render_seo_metadata ( 'content_entry' = > content_entry ) }
2011-04-29 20:10:13 +00:00
it { should include ( %Q[ <meta name="keywords" content=" #{ site . meta_keywords } " /> ] ) }
it { should include ( %Q[ <meta name="description" content=" #{ site . meta_description } " /> ] ) }
end
2011-06-23 12:57:52 +00:00
2011-04-29 20:10:13 +00:00
end
2011-06-23 12:57:52 +00:00
end
def render_seo ( assigns = { } )
render_seo_tag ( 'seo' , assigns )
2011-02-13 13:40:55 +00:00
end
2011-06-23 12:57:52 +00:00
def render_seo_title ( assigns = { } )
render_seo_tag ( 'seo_title' , assigns )
end
2011-06-23 08:14:36 +00:00
def render_seo_metadata ( assigns = { } )
2011-06-23 12:57:52 +00:00
render_seo_tag ( 'seo_metadata' , assigns )
end
def render_seo_tag ( tag_name , assigns = { } )
2011-04-29 20:10:13 +00:00
registers = { :site = > site }
liquid_context = :: Liquid :: Context . new ( { } , assigns , registers )
2011-06-23 12:57:52 +00:00
output = Liquid :: Template . parse ( " {% #{ tag_name } %} " ) . render ( liquid_context )
2011-02-13 13:40:55 +00:00
end
2011-12-05 14:29:32 +00:00
end