Skip to content

Commit

Permalink
Merge pull request #2830 from DFE-Digital/include-events-in-sitemap
Browse files Browse the repository at this point in the history
Include GiT/Online Q&A events in sitemap
  • Loading branch information
ethax-ross authored Sep 30, 2022
2 parents c6b86cb + 94a55e3 commit 2c89974
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
17 changes: 17 additions & 0 deletions app/controllers/sitemap_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class SitemapController < ApplicationController
DEFAULT_LASTMOD = "2021-03-01".freeze
FUTURE_EVENTS_LIMIT = 100

ALIASES = {
"/home" => "/",
Expand Down Expand Up @@ -45,6 +46,22 @@ def build
end

def published_pages
content_pages.merge(event_pages)
end

def event_pages
events.map { |e| event_path(e.readable_id) }.index_with({})
end

def events
GetIntoTeachingApiClient::TeachingEventsApi.new.search_teaching_events(
start_after: Time.zone.now,
quantity: FUTURE_EVENTS_LIMIT,
type_ids: [EventType.get_into_teaching_event_id, EventType.online_event_id],
)
end

def content_pages
::Pages::Frontmatter.list.reject { |_path, fm| fm[:draft] }
end

Expand Down
28 changes: 20 additions & 8 deletions spec/requests/sitemap_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,21 @@
},
}
end
let(:sitemap_pages) { content_pages.except("/test/a") }
let(:event_pages) do
events.map { |e| event_path(e.readable_id) }.index_with({})
end
let(:all_sitemap_pages) { content_sitemap_pages.merge(event_pages) }
let(:content_sitemap_pages) { content_pages.except("/test/a") }
let(:events) { build_list(:event_api, 3) }

before do
freeze_time
allow_any_instance_of(GetIntoTeachingApiClient::TeachingEventsApi).to \
receive(:search_teaching_events).with(
start_after: Time.zone.now,
quantity: 100,
type_ids: [EventType.get_into_teaching_event_id, EventType.online_event_id],
).and_return(events)
allow(Pages::Frontmatter).to receive(:list) { content_pages }
get("/sitemap.xml")
end
Expand All @@ -39,15 +51,15 @@
expect(subject.at_xpath("/xmlns:urlset").namespace.href).to eql(sitemap_namespace)
end

describe "content_pages" do
let(:expected) { SitemapController::OTHER_PATHS.count + sitemap_pages.count }
let(:paths) { sitemap_pages.keys.concat(SitemapController::OTHER_PATHS) }
describe "sitemap pages" do
let(:expected) { SitemapController::OTHER_PATHS.count + all_sitemap_pages.count }
let(:paths) { all_sitemap_pages.keys.concat(SitemapController::OTHER_PATHS) }

specify "should have the right number of content_pages" do
specify "should have the right number of pages" do
expect(expected).to eql(subject.xpath("/xmlns:urlset/xmlns:url").size)
end

specify "the content_pages should have the correct paths" do
specify "the pages should have the correct paths" do
expect(
subject
.xpath("xmlns:urlset/xmlns:url/xmlns:loc")
Expand All @@ -58,7 +70,7 @@

describe "priority" do
specify "priority should be assinged from frontmatter" do
sitemap_pages.each do |path, data|
content_sitemap_pages.each do |path, data|
importance = subject.at_xpath(
%(/xmlns:urlset/xmlns:url[xmlns:loc = 'http://www.example.com#{path}']/xmlns:priority),
).text
Expand All @@ -70,7 +82,7 @@

describe "lastmod" do
specify "last modified date should be assinged from frontmatter" do
sitemap_pages.each do |path, data|
content_sitemap_pages.each do |path, data|
last_modified = subject.at_xpath(
%(/xmlns:urlset/xmlns:url[xmlns:loc = 'http://www.example.com#{path}']/xmlns:lastmod),
).text
Expand Down

0 comments on commit 2c89974

Please sign in to comment.