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

Siteimprove new gem #1374

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ gem "pg"
gem "plek"
gem "sassc-rails"
gem "sentry-sidekiq"
gem "siteimprove_api_client"
gem "siteimprove_api_client", git: "https://github.com/alphagov/siteimprove_api_client.git", branch: "a11y-next-gen-update"
gem "uglifier"

group :development do
Expand Down
14 changes: 10 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
GIT
remote: https://github.com/alphagov/siteimprove_api_client.git
revision: 01d8a2d7b5880fd86b3983afb2759e8b4f35fe45
branch: a11y-next-gen-update
specs:
siteimprove_api_client (1.0.0)
typhoeus (~> 1.0, >= 1.0.1)

GEM
remote: https://rubygems.org/
specs:
Expand Down Expand Up @@ -662,8 +670,6 @@ GEM
simplecov_json_formatter (~> 0.1)
simplecov-html (0.12.3)
simplecov_json_formatter (0.1.4)
siteimprove_api_client (1.0.0)
typhoeus (~> 1.0, >= 1.0.1)
snaky_hash (2.0.1)
hashie
version_gem (~> 1.1, >= 1.1.1)
Expand All @@ -683,7 +689,7 @@ GEM
tilt (2.0.11)
timecop (0.9.8)
timeout (0.4.1)
typhoeus (1.4.0)
typhoeus (1.4.1)
ethon (>= 0.9.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
Expand Down Expand Up @@ -739,7 +745,7 @@ DEPENDENCIES
sassc-rails
sentry-sidekiq
simplecov
siteimprove_api_client
siteimprove_api_client!
spring-commands-rspec
timecop
uglifier
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/metrics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def setup_siteimprove
raw_policy_issues = Siteimprove::FetchPolicyIssues.call(url: "https://www.gov.uk/#{base_path}")
@policy_issues = Siteimprove::PolicyIssuesPresenter.new(raw_policy_issues, @summary_info)
raw_quality_assurance_issues = Siteimprove::FetchQualityAssuranceIssues.call(url: "https://www.gov.uk/#{base_path}")
@quality_assurance_issues = Siteimprove::QualityAssuranceIssuesPresenter.new(raw_quality_assurance_issues, @summary_info)
@quality_assurance_issues = Siteimprove::QualityAssuranceIssuesPresenter.new(raw_quality_assurance_issues, @summary_info, LinkTitleResolver.new("/#{base_path}"))
@has_accessibility_info = @policy_issues.any? || @quality_assurance_issues.any?
rescue Siteimprove::BaseError
@has_accessibility_info = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ module Siteimprove
class QualityAssuranceIssuesPresenter
attr_reader :summary_info

def initialize(quality_assurance_issues, summary_info)
def initialize(quality_assurance_issues, summary_info, link_title_resolver)
@quality_assurance_issues = quality_assurance_issues
@summary_info = summary_info
@link_title_resolver = link_title_resolver
end

def any?
Expand Down Expand Up @@ -35,6 +36,7 @@ def misspellings
def broken_links
@quality_assurance_issues[:broken_links].map do |link|
{
title: @link_title_resolver.title_for_link(link.url),
url: link.url,
message: link.message,
quality_assurance_direct_link: quality_assurance_broken_links_direct_link(link.id),
Expand Down
26 changes: 26 additions & 0 deletions app/services/link_title_resolver.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class LinkTitleResolver
def initialize(base_path)
@base_path = base_path
end

def title_for_link(link)
regexp = Regexp.new("<a.*href=\"#{link}\"[^>]*>([^<]*)<\/a>")
matches = regexp.match(content_body)
return "(Unable to find link text)" unless matches.captures.any?

matches.captures[0]
end

def content_body
@content_body ||= extract_content_body_from_item
end

def extract_content_body_from_item
content_item = GdsApi.content_store.content_item(@base_path)
details = content_item.to_hash["details"]
return details["body"] if details.key?("body")
return details["parts"].map { |p| p["body"] }.join(" - ") if details.key?("parts")

""
end
end
2 changes: 1 addition & 1 deletion app/services/siteimprove/fetch_policies.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def find(policy_id)

def policies
Rails.cache.fetch("siteimprove-policies", expires_in: 1.hour) do
policy_api.sites_site_id_policy_policies_get(site_id, page_size: 500).items
policy_api.sites_site_id_policy_policies_get(site_id, page_size: 1000).items
end
rescue SiteimproveAPIClient::ApiError => e
GovukError.notify(e)
Expand Down
6 changes: 4 additions & 2 deletions app/views/metrics/_siteimprove_issues.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,15 @@
<caption class="govuk-visually-hidden"><%= t("metrics.siteimprove.quality_assurance.broken_links.caption") %></caption>
<thead class="govuk-table__head">
<tr class="govuk-table__row">
<th scope="col" class="govuk-table__header"><%= t("metrics.siteimprove.quality_assurance.broken_links.url") %></th>
<th scope="col" class="govuk-table__header"><%= t("metrics.siteimprove.quality_assurance.broken_links.link_text") %></th>
<th scope="col" class="govuk-table__header"><%= t("metrics.siteimprove.quality_assurance.broken_links.broken_url") %></th>
</tr>
</thead>
<tbody class="govuk-table__body">
<% @quality_assurance_issues.issue_list[:broken_links].each do |link| %>
<tr class="govuk-table__row">
<td class="govuk-table__cell content-metrics__url-cell"><a href=<%= link[:quality_assurance_direct_link] %>><%= link[:url] %></a></td>
<td class="govuk-table__cell content-metrics__url-cell"><a href=<%= link[:quality_assurance_direct_link] %>><%= link[:title] %></a></td>
<td class="govuk-table__cell content-metrics__url-cell"><%= link[:url] %></td>
</tr>
<% end %>
</tr>
Expand Down
3 changes: 2 additions & 1 deletion config/locales/defaults/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ en:
title: 'Broken links'
description: 'This section highlights potential broken links on the page.'
caption: 'Table of broken links on the page'
url: 'URL'
link_text: 'Link text'
broken_url: 'Broken URL'
misspellings:
title: 'Misspellings'
description: 'This section highlights potential misspellings on the page.'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
siteimprove_has_summary
siteimprove_has_policies
Rails.application.config.siteimprove_site_id = 1
@link_title_resolver = double
allow(@link_title_resolver).to receive(:title_for_link).and_return("Link Text")
end

after do
Expand All @@ -25,31 +27,31 @@
siteimprove_has_misspellings
siteimprove_has_no_broken_links

presenter = described_class.new(quality_assurance_issues, summary_info)
presenter = described_class.new(quality_assurance_issues, summary_info, @link_title_resolver)
expect(presenter.any?).to be true
end

it "returns true if there are broken links" do
siteimprove_has_no_misspellings
siteimprove_has_broken_links

presenter = described_class.new(quality_assurance_issues, summary_info)
presenter = described_class.new(quality_assurance_issues, summary_info, @link_title_resolver)
expect(presenter.any?).to be true
end

it "returns true if there are misspellings and broken links" do
siteimprove_has_misspellings
siteimprove_has_broken_links

presenter = described_class.new(quality_assurance_issues, summary_info)
presenter = described_class.new(quality_assurance_issues, summary_info, @link_title_resolver)
expect(presenter.any?).to be true
end

it "returns false if there are no misspellings or broken links" do
siteimprove_has_no_misspellings
siteimprove_has_no_broken_links

presenter = described_class.new(quality_assurance_issues, summary_info)
presenter = described_class.new(quality_assurance_issues, summary_info, @link_title_resolver)
expect(presenter.any?).to be false
end
end
Expand All @@ -61,12 +63,12 @@
end

it "returns spelling suggestions as a string" do
misspellings = described_class.new(quality_assurance_issues, summary_info).misspellings
misspellings = described_class.new(quality_assurance_issues, summary_info, @link_title_resolver).misspellings
expect(misspellings.first[:suggestions]).to eq("fish, phish")
end

it "gets the direct link to the misspelling in the page report" do
misspellings = described_class.new(quality_assurance_issues, summary_info).misspellings
misspellings = described_class.new(quality_assurance_issues, summary_info, @link_title_resolver).misspellings
expected_link = "https://my2.siteimprove.com/Inspector/1054012/QualityAssurance/Page?impmd=0&PageId=2#/Issue/Misspellings/0"

expect(misspellings.first[:quality_assurance_direct_link]).to eq(expected_link)
Expand All @@ -80,7 +82,7 @@
end

it "gets the direct link to the broken link in the page report" do
broken_links = described_class.new(quality_assurance_issues, summary_info).broken_links
broken_links = described_class.new(quality_assurance_issues, summary_info, @link_title_resolver).broken_links
expected_link = "https://my2.siteimprove.com/Inspector/1054012/QualityAssurance/Page?impmd=0&PageId=2#/Issue/BrokenLinks/0"

expect(broken_links.first[:quality_assurance_direct_link]).to eq(expected_link)
Expand Down
4 changes: 2 additions & 2 deletions spec/support/siteimprove_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ def siteimprove_has_no_policies
},
},
}.to_json
stub_request(:get, "#{SI_BASE_URI}/sites/1/policy/policies?page_size=500").to_return(status: 200, headers:, body:)
stub_request(:get, "#{SI_BASE_URI}/sites/1/policy/policies?page_size=1000").to_return(status: 200, headers:, body:)
end

def siteimprove_has_policies
Expand Down Expand Up @@ -365,7 +365,7 @@ def siteimprove_has_policies
},
},
}.to_json
stub_request(:get, "#{SI_BASE_URI}/sites/1/policy/policies?page_size=500").to_return(status: 200, headers:, body:)
stub_request(:get, "#{SI_BASE_URI}/sites/1/policy/policies?page_size=1000").to_return(status: 200, headers:, body:)
end

def siteimprove_has_misspellings
Expand Down
Loading