Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove canonical-rails gem and handroll our own #4713

Merged
merged 8 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ gem 'puma', '~> 6.5'
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false

# Canonical meta tag
gem 'canonical-rails'

# Decorate logic to keep it out of the views and helper methods
gem 'draper'

Expand Down
3 changes: 0 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,6 @@ GEM
activesupport (>= 3.0.0)
uniform_notifier (~> 1.11)
byebug (11.1.3)
canonical-rails (0.2.16)
actionview (>= 4.1, < 7.3)
capybara (3.40.0)
addressable
matrix
Expand Down Expand Up @@ -756,7 +754,6 @@ DEPENDENCIES
brakeman
bullet
byebug
canonical-rails
capybara (>= 2.15)
cloudfront-rails
colorize
Expand Down
6 changes: 5 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
<title><%= yield :page_title %> - Publish teacher training courses - GOV.UK</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= canonical_tag %>

<% canonical_url = "#{request.base_url}#{request.path.chomp('/')}/" %>

<%= tag.link(href: canonical_url, rel: "canonical") %>
<%= tag.meta(property: "og:url", content: canonical_url) %>

<%= tag.meta(name: "viewport", content: "width=device-width, initial-scale=1") %>
<%= tag.meta(property: "og:image", content: image_path("govuk-opengraph-image.png")) %>
Expand Down
6 changes: 5 additions & 1 deletion app/views/layouts/find_layout.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
<title><%= yield :page_title %> - <%= t("service_name.find") %> - GOV.UK</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
<%= canonical_tag %>

<% canonical_url = "#{request.base_url}#{request.path.chomp('/')}/" %>

<%= tag.link(href: canonical_url, rel: "canonical") %>
<%= tag.meta(property: "og:url", content: canonical_url) %>

<%= tag.meta(name: "viewport", content: "width=device-width, initial-scale=1") %>
<%= tag.meta(property: "og:image", content: image_path("govuk-opengraph-image.png")) %>
gms-gs marked this conversation as resolved.
Show resolved Hide resolved
Expand Down
30 changes: 0 additions & 30 deletions config/initializers/canonical_rails.rb

This file was deleted.

93 changes: 93 additions & 0 deletions spec/features/canonical_tags_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
# frozen_string_literal: true

require 'rails_helper'

feature 'Canonical tags', :with_find_constraint do
before do
given_i_have_courses
and_i_visit_the_home_page
end

describe 'Canonical tags on Find pages' do
scenario 'without query params' do
then_the_page_contains_canonical_tags_with_no_query_params
end

scenario 'with query parameters' do
and_i_select_the_across_england_radio_button
then_the_page_contains_canonical_tags_without_query_params
end

scenario 'when visiting a course' do
when_i_visit_the_course_page
then_the_page_contains_canonical_tags_for_a_course
end

scenario 'when visiting a course and selecting an anchor tag' do
when_i_visit_the_course_page
and_i_click_an_anchor_tag
then_the_page_contains_canonical_tags_for_a_course
end
end

describe 'Canonical tags on Publish pages', :with_publish_constraint do
scenario 'Publish page contains canonical tags' do
and_i_visit_the_publish_page
then_the_publish_page_contains_canonical_tags
end
end

def when_i_visit_the_course_page
visit find_course_path(
provider_code: @mathematics_course.provider.provider_code,
course_code: @mathematics_course.course_code
)
end

def given_i_have_courses
provider = create(:provider)
@mathematics_course = create(:course, :published_postgraduate, :secondary, provider:, name: 'Mathematics', subjects: [find_or_create(:secondary_subject, :mathematics)])
end

def and_i_visit_the_home_page
visit '/'
end

def and_i_visit_the_publish_page
visit '/sign-in/'
end

def and_i_select_the_across_england_radio_button
choose 'Across England'
click_link_or_button 'Continue'
end

def and_i_click_an_anchor_tag
click_link_or_button 'Where you will train'
end

def then_the_page_contains_canonical_tags_for_a_course
canonical_url = "http://www.example.com/course/#{@mathematics_course.provider.provider_code}/#{@mathematics_course.course_code}/"

link_tag = page.find("link[rel='canonical']", visible: :all)
expect(link_tag[:href]).to eq(canonical_url)

og_url_tag = page.find("meta[property='og:url']", visible: :all)
expect(og_url_tag[:content]).to eq(canonical_url)
end

def then_the_page_contains_canonical_tags_with_no_query_params
expect(page).to have_css("link[rel='canonical'][href='http://www.example.com/']", visible: :all)
expect(page).to have_css("meta[property='og:url'][content='http://www.example.com/']", visible: :all)
end

def then_the_page_contains_canonical_tags_without_query_params
expect(page).to have_css("link[rel='canonical'][href='http://www.example.com/age-groups/']", visible: :all)
expect(page).to have_css("meta[property='og:url'][content='http://www.example.com/age-groups/']", visible: :all)
end

def then_the_publish_page_contains_canonical_tags
expect(page).to have_css("link[rel='canonical'][href='http://www.example.com/sign-in/']", visible: :all)
expect(page).to have_css("meta[property='og:url'][content='http://www.example.com/sign-in/']", visible: :all)
end
end
Loading