Skip to content

Commit 0bb161c

Browse files
authored
Merge pull request #1969 from alphagov/show-custom-error-page-when-welsh-form-is-archived
Show custom error page when welsh form is archived
2 parents 656ef17 + 083ca68 commit 0bb161c

File tree

10 files changed

+123
-44
lines changed

10 files changed

+123
-44
lines changed

app/controllers/forms/base_controller.rb

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,15 +44,28 @@ def set_available_languages
4444
end
4545

4646
def set_form
47-
begin
48-
form_id = params.require(:form_id)
49-
@form = Api::V2::FormDocumentRepository.find_with_mode(form_id:, mode:, language: locale)
50-
rescue ActiveResource::ResourceNotFound
51-
archived_form = Api::V2::FormDocumentRepository.find(form_id:, tag: :archived)
52-
return render template: "forms/archived/show", locals: { form_name: archived_form.name }, status: :not_found if archived_form.present?
47+
form_id = params.require(:form_id)
48+
@form = Api::V2::FormDocumentRepository.find_with_mode(form_id:, mode:, language: locale)
49+
50+
if @form.blank?
51+
I18n.with_locale(locale) do
52+
if locale == "cy"
53+
archived_welsh_version = Api::V2::FormDocumentRepository.find(form_id:, tag: :archived, language: :cy)
54+
live_english_version = Api::V2::FormDocumentRepository.find(form_id:, tag: :live, language: :en)
55+
56+
if archived_welsh_version.present? && live_english_version.present?
57+
return render template: "forms/archived_welsh/show",
58+
locals: { form: live_english_version },
59+
status: :not_found
60+
end
61+
end
62+
63+
archived_form = Api::V2::FormDocumentRepository.find(form_id:, tag: :archived)
64+
return render template: "forms/archived/show", locals: { form_name: archived_form.name }, status: :not_found if archived_form.present?
65+
end
5366
end
5467

55-
raise ActiveResource::ResourceNotFound, "Not Found" unless @form.start_page
68+
raise ActiveResource::ResourceNotFound, "Not Found" unless @form.present? && @form.start_page
5669
end
5770
end
5871
end

app/services/api/v2/form_document_repository.rb

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
class Api::V2::FormDocumentRepository
22
class << self
33
def find(form_id:, tag:, language: :en)
4-
raise ActiveResource::ResourceNotFound.new(404, "Not Found") unless form_id.to_s =~ /^[[:alnum:]]+$/
4+
return nil unless form_id.to_s =~ /^[[:alnum:]]+$/
55

6-
form_document = Api::V2::FormDocumentResource.get(form_id, tag, **options_for_language(language))
7-
form = Form.new(form_document, true)
8-
form.document_json = form_document
9-
form.prefix_options = { form_id:, tag: }
10-
form
6+
begin
7+
form_document = Api::V2::FormDocumentResource.get(form_id, tag, **options_for_language(language))
8+
9+
form = Form.new(form_document, true)
10+
form.document_json = form_document
11+
form.prefix_options = { form_id:, tag: }
12+
form
13+
rescue ActiveResource::ResourceNotFound
14+
nil
15+
end
1116
end
1217

1318
def find_with_mode(form_id:, mode:, language: :en)

app/services/form_submission_service.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ def ensure_form_english
5252

5353
def fetch_english_language_form
5454
@form = Api::V2::FormDocumentRepository.find_with_mode(form_id: form.id, mode:)
55+
56+
raise ActiveResource::ResourceNotFound.new(404, "Not Found") if @form.nil?
5557
end
5658

5759
def validate_submission
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<% set_page_title(form_title(form_name: form.name, page_name: t(".heading"), mode: @mode)) %>
2+
3+
<div class="govuk-grid-row">
4+
<div class="govuk-grid-column-two-thirds">
5+
<h1 class="govuk-heading-l"><%= t(".heading") %></h1>
6+
7+
<p><%= t(".welsh_version_archived") %></p>
8+
9+
<p><%= t(".view_english_form") %></p>
10+
11+
<p><%= govuk_link_to(form.name, form_path(form_id: form.id, form_slug: form.form_slug, locale: nil), lang: "en", hreflang: "en", rel: "alternate") %></p>
12+
</div>
13+
</div>

config/locales/cy.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -342,9 +342,9 @@ cy:
342342
privacy_policy: Preifatrwydd
343343
form:
344344
archived:
345-
heading: Sorry, this service is no longer available
346-
service_unavailable: "“%{form_name}” has been archived and is no longer available."
347-
title: Form archived
345+
heading: Mae’n ddrwg gennym, nid yw’r gwasanaeth hwn ar gael mwyach
346+
service_unavailable: Mae “%{form_name}” wedi’i archifo ac nid yw bellach ar gael.
347+
title: Mae’n ddrwg gennym, nid yw’r gwasanaeth hwn ar gael mwyach
348348
check_your_answers:
349349
agree_and_submit: Cytuno a chyflwyno
350350
declaration: Datganiad
@@ -372,6 +372,11 @@ cy:
372372
max_answers: Ni allwch ychwanegu ateb arall i’r cwestiwn hwn gan eich bod wedi rhoi’r uchafswm o %{max_answers}
373373
radios_legend: Ydych chi angen ychwanegu ateb arall?
374374
title: Ychwanegu neu ddileu ateb i %{page_title}
375+
archived_welsh:
376+
show:
377+
heading: Mae’n ddrwg gennym, nid yw’r gwasanaeth hwn ar gael yn Gymraeg mwyach
378+
view_english_form: 'Gallwch lenwi’r ffurflen yn Saesneg o hyd:'
379+
welsh_version_archived: Mae’r fersiwn Gymraeg o’r ffurflen hon wedi’i harchifo ac nid yw ar gael mwyach.
375380
back: Yn ôl
376381
remove_answer:
377382
show:

config/locales/en.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ en:
344344
archived:
345345
heading: Sorry, this service is no longer available
346346
service_unavailable: "“%{form_name}” has been archived and is no longer available."
347-
title: Form archived
347+
title: Sorry, this service is no longer available
348348
check_your_answers:
349349
agree_and_submit: Agree and submit
350350
declaration: Declaration
@@ -372,6 +372,11 @@ en:
372372
max_answers: You cannot add another answer to this question as you’ve entered the maximum of %{max_answers}
373373
radios_legend: Do you need to add another answer?
374374
title: Add or remove answer to %{page_title}
375+
archived_welsh:
376+
show:
377+
heading: Sorry, this service is no longer available in Welsh
378+
view_english_form: 'You can still complete the form in English:'
379+
welsh_version_archived: The Welsh version of this form has been archived and is no longer available.
375380
back: Back
376381
remove_answer:
377382
show:

spec/requests/forms/base_controller_spec.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151
mock.get "/api/v2/forms/2#{api_url_suffix}?language=cy", req_headers, form_response_data.to_json, 200
5252
mock.get "/api/v2/forms/9999#{api_url_suffix}", req_headers, no_data_found_response, 404
5353
mock.get "/api/v2/forms/9999#{api_url_suffix}?language=cy", req_headers, no_data_found_response, 404
54+
mock.get "/api/v2/forms/9999/archived", req_headers, no_data_found_response, 404
55+
mock.get "/api/v2/forms/9999/archived?language=cy", req_headers, no_data_found_response, 404
5456
end
5557
end
5658

@@ -343,6 +345,22 @@
343345
expect(response.body).to include(I18n.t("form.archived.title"))
344346
end
345347
end
348+
349+
context "when the form is an archived Welsh form with a live English version" do
350+
before do
351+
ActiveResource::HttpMock.respond_to do |mock|
352+
mock.get "/api/v2/forms/2/live", req_headers, form_response_data.to_json, 200
353+
mock.get "/api/v2/forms/2/live?language=cy", req_headers, nil, 404
354+
mock.get "/api/v2/forms/2/archived?language=cy", req_headers, form_response_data.to_json, 200
355+
end
356+
357+
get form_path(mode: "form", form_id: 2, form_slug: form_response_data.form_slug, locale: :cy)
358+
end
359+
360+
it "Renders the form archived page" do
361+
expect(Capybara.string(response.body)).to have_text(I18n.t("forms.archived_welsh.show.heading", locale: :cy))
362+
end
363+
end
346364
end
347365
end
348366

spec/services/api/v2/form_document_repository_spec.rb

Lines changed: 14 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,25 @@
4949
let(:response_data) { nil }
5050
let(:status) { 404 }
5151

52-
it "raises ActiveResource::ResourceNotFound" do
53-
expect { described_class.find(tag: :archived, form_id: form_id) }.to raise_error(ActiveResource::ResourceNotFound)
52+
it "returns nil" do
53+
expect(described_class.find(tag: :archived, form_id: form_id)).to be_nil
5454
end
5555
end
5656
end
5757

5858
context "when the form id contains non-alpha-numeric chars" do
5959
let(:form_id) { "<id>" }
6060

61-
it "returns ResourceNotFound when the id contains non-alpha-numeric chars" do
62-
expect {
63-
described_class.find(tag:, form_id:)
64-
}.to raise_error(ActiveResource::ResourceNotFound)
61+
it "returns nil when the id contains non-alpha-numeric chars" do
62+
expect(described_class.find(tag:, form_id:)).to be_nil
6563
end
6664
end
6765

6866
context "when the form id is blank" do
6967
let(:form_id) { "" }
7068

71-
it "returns ResourceNotFound when the id is blank" do
72-
expect {
73-
described_class.find(tag:, form_id:)
74-
}.to raise_error(ActiveResource::ResourceNotFound)
69+
it "returns nil when the id is blank" do
70+
expect(described_class.find(tag:, form_id:)).to be_nil
7571
end
7672
end
7773

@@ -80,10 +76,8 @@
8076
let(:response_data) { nil }
8177
let(:status) { 404 }
8278

83-
it "raises an exception" do
84-
expect {
85-
described_class.find(tag:, form_id:)
86-
}.to raise_error(ActiveResource::ResourceNotFound)
79+
it "returns nil" do
80+
expect(described_class.find(tag:, form_id:)).to be_nil
8781
end
8882
end
8983

@@ -132,10 +126,8 @@
132126
expect(form_snapshot.pages).to all be_a Page
133127
end
134128

135-
it "raises an exception if the form does not exist" do
136-
expect {
137-
described_class.find_with_mode(form_id: "99", mode: Mode.new("preview-draft"))
138-
}.to raise_error(ActiveResource::ResourceNotFound)
129+
it "returns nil if the form does not exist" do
130+
expect(described_class.find_with_mode(form_id: "99", mode: Mode.new("preview-draft"))).to be_nil
139131
end
140132

141133
context "when mode is live" do
@@ -171,16 +163,12 @@
171163
end
172164

173165
context "when validating the provided form id" do
174-
it "returns ResourceNotFound when the id contains non-alpha-numeric chars" do
175-
expect {
176-
described_class.find_with_mode(form_id: "<id>", mode: Mode.new("preview-draft"))
177-
}.to raise_error(ActiveResource::ResourceNotFound)
166+
it "returns nil when the id contains non-alpha-numeric chars" do
167+
expect(described_class.find_with_mode(form_id: "<id>", mode: Mode.new("preview-draft"))).to be_nil
178168
end
179169

180-
it "returns ResourceNotFound when the id is blank" do
181-
expect {
182-
described_class.find_with_mode(form_id: "", mode: Mode.new("preview-draft"))
183-
}.to raise_error(ActiveResource::ResourceNotFound)
170+
it "returns nil when the id is blank" do
171+
expect(described_class.find_with_mode(form_id: "", mode: Mode.new("preview-draft"))).to be_nil
184172
end
185173

186174
it "returns the form when the id is alphanumeric" do

spec/views/forms/archived/show.html.erb_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
end
1313

1414
it "has the correct title" do
15-
expect(view.content_for(:title)).to eq "Form archived - Archived form"
15+
expect(view.content_for(:title)).to eq "Sorry, this service is no longer available - Archived form"
1616
end
1717

1818
it "has the correct heading" do
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
require "rails_helper"
2+
3+
describe "forms/archived_welsh/show.html.erb" do
4+
let(:form) { build :form, name: "Archived Welsh form" }
5+
let(:mode) { OpenStruct.new(preview_draft?: false, preview_archived?: false, preview_live?: false) }
6+
7+
before do
8+
assign(:mode, mode)
9+
allow(view).to receive(:form_path).and_return("/form/1/english_form")
10+
11+
render template: "forms/archived_welsh/show", locals: { form: }, status: :not_found
12+
end
13+
14+
it "has the correct title" do
15+
expect(view.content_for(:title)).to eq "#{t('forms.archived_welsh.show.heading')} - #{form.name}"
16+
end
17+
18+
it "has the correct heading" do
19+
expect(rendered).to have_css("h1", text: t("forms.archived_welsh.show.heading"))
20+
end
21+
22+
it "displays the body text" do
23+
expect(rendered).to have_text(I18n.t("forms.archived_welsh.show.welsh_version_archived"))
24+
end
25+
26+
it "displays a link to the English form" do
27+
expect(rendered).to have_text(I18n.t("forms.archived_welsh.show.view_english_form"))
28+
expect(rendered).to have_link("Archived Welsh form", href: "/form/1/english_form")
29+
end
30+
end

0 commit comments

Comments
 (0)