do not include layouts within the sitemap.xml page (fix issue locomotivecms/engine#1148)

did committed May 18, 2016
commit bd913c99095ced0bed62669da6fe62f54642b28a
Showing 5 changed files with 78 additions and 5 deletions
locomotive/steam.rb b/lib/locomotive/steam.rb +5 -3
@@ @@ -13,13 +13,15 @@ require_relative 'steam/services'
module Locomotive
module Steam
- FRONTMATTER_REGEXP = /^(?<yaml>(---\s*\n.*?\n?)^(---\s*$\n?))?(?<template>.*)/mo
+ FRONTMATTER_REGEXP = /^(?<yaml>(---\s*\n.*?\n?)^(---\s*$\n?))?(?<template>.*)/mo.freeze
WILDCARD = 'content_type_template'.freeze
- CONTENT_ENTRY_ENGINE_CLASS_NAME = /^Locomotive::ContentEntry(.*)$/o
+ CONTENT_ENTRY_ENGINE_CLASS_NAME = /^Locomotive::ContentEntry(.*)$/o.freeze
- IsHTTP = /\Ahttps?:\/\//o
+ IsHTTP = /\Ahttps?:\/\//o.freeze
+
+ IsLAYOUT = /\Alayouts(\/|\z)/o.freeze
class << self
attr_writer :configuration
locomotive/steam/entities/page.rb b/lib/locomotive/steam/entities/page.rb +4 -0
@@ @@ -48,6 +48,10 @@ module Locomotive::Steam
attributes[:fullpath].values.first == '404'
end
+ def layout?
+ (attributes[:fullpath].values.first =~ Locomotive::Steam::IsLAYOUT) == 0
+ end
+
def source
self[:raw_template]
end
locomotive/steam/middlewares/sitemap.rb b/lib/locomotive/steam/middlewares/sitemap.rb +6 -2
@@ @@ -27,8 +27,8 @@ module Locomotive::Steam
end
def build_pages_to_xml
- repositories.page.published.map do |page|
- next if page.index? || page.not_found?
+ page_repository.published.map do |page|
+ next if page.index? || page.not_found? || page.layout?
build_page_xml(page)
end.flatten.join.strip
@@ @@ -80,6 +80,10 @@ module Locomotive::Steam
services.repositories
end
+ def page_repository
+ repositories.page
+ end
+
def url_for(page, locale = nil)
services.url_builder.url_for(page, locale)
end
spec/unit/entities/page_spec.rb +19 -0
@@ @@ -33,6 +33,25 @@ describe Locomotive::Steam::Page do
end
+ describe '#layout?' do
+
+ let(:attributes) { { fullpath: { en: 'foo/layouts' } } }
+
+ subject { page.layout? }
+ it { is_expected.to eq false }
+
+ context 'true if starting by layouts' do
+ let(:attributes) { { fullpath: { en: 'layouts/base' } } }
+ it { is_expected.to eq true }
+ end
+
+ context 'true if the root layouts page' do
+ let(:attributes) { { fullpath: { en: 'layouts' } } }
+ it { is_expected.to eq true }
+ end
+
+ end
+
describe '#valid?' do
subject { page.valid? }
spec/unit/middlewares/sitemap_spec.rb +44 -0
@@ @@ -0,0 +1,44 @@
+ require 'spec_helper'
+
+ require_relative '../../../lib/locomotive/steam/middlewares/thread_safe'
+ require_relative '../../../lib/locomotive/steam/middlewares/helpers'
+ require_relative '../../../lib/locomotive/steam/middlewares/sitemap'
+
+ describe Locomotive::Steam::Middlewares::Sitemap do
+
+ let(:pages) { [] }
+ let(:page_repository) { instance_double('PageRepository', published: pages) }
+ let(:app) { ->(env) { [200, env, 'app'] }}
+ let(:middleware) { described_class.new(app) }
+
+ before do
+ allow_any_instance_of(described_class).to receive(:base_url).and_return('http://localhost/')
+ allow_any_instance_of(described_class).to receive(:page_repository).and_return(page_repository)
+ end
+
+ describe '#call' do
+
+ let(:env) { { 'PATH_INFO' => '/sitemap.xml', 'steam.page' => nil } }
+ subject { middleware.call(env) }
+
+ describe 'no pages' do
+
+ it 'renders a blank sitemap' do
+ is_expected.to eq [200, { "Content-Type"=>"text/plain" }, ["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n <url>\n <loc>http://localhost/</loc>\n <priority>1.0</priority>\n </url>\n\n</urlset>\n"]]
+ end
+
+ end
+
+ describe 'only layouts' do
+
+ let(:pages) { [instance_double('Page', index?: false, not_found?: false, layout?: true)] }
+
+ it 'renders a blank sitemap' do
+ is_expected.to eq [200, { "Content-Type"=>"text/plain" }, ["<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n <url>\n <loc>http://localhost/</loc>\n <priority>1.0</priority>\n </url>\n\n</urlset>\n"]]
+ end
+
+ end
+
+ end
+
+ end