Skip to content

Commit 2bae426

Browse files
authored
Merge pull request #2600 from alphagov/support-multilingual-markdown-fields
Support multilingual markdown fields
2 parents 25b635a + a59f84a commit 2bae426

13 files changed

+120
-292
lines changed

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ gem "govuk_design_system_formbuilder"
4949
gem "dfe-autocomplete", require: "dfe/autocomplete", github: "DFE-Digital/dfe-autocomplete", ref: "1d4cc65039e11cc3ba9e7217a719b8128d0e4d53"
5050

5151
# Our own custom markdown renderer
52-
gem "govuk-forms-markdown", require: "govuk_forms_markdown", github: "alphagov/govuk-forms-markdown", tag: "0.7.0"
52+
gem "govuk-forms-markdown", require: "govuk_forms_markdown", github: "alphagov/govuk-forms-markdown", tag: "0.8.0"
5353

5454
# For structured logging
5555
gem "lograge"

Gemfile.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ GIT
1212

1313
GIT
1414
remote: https://github.com/alphagov/govuk-forms-markdown.git
15-
revision: bfc72c960427b6393ba2eacac95111b5122fc0aa
16-
tag: 0.7.0
15+
revision: 62bcbf4390aa4998390c1390debcbf250aa90ec0
16+
tag: 0.8.0
1717
specs:
18-
govuk-forms-markdown (0.7.0)
18+
govuk-forms-markdown (0.8.0)
1919
redcarpet (~> 3.6)
2020

2121
GEM
@@ -720,7 +720,7 @@ CHECKSUMS
720720
gds-sso (22.1.1) sha256=87da38284fe2eb98cca4f02d5f23bc01bbad7eeabe10a3c60d915628968d9122
721721
globalid (1.3.0) sha256=05c639ad6eb4594522a0b07983022f04aa7254626ab69445a0e493aa3786ff11
722722
govuk-components (5.13.1) sha256=74808d8188a7de2c5e83e5a829c5f8ac3851df27f82410a4a2444cf53c24cfe5
723-
govuk-forms-markdown (0.7.0)
723+
govuk-forms-markdown (0.8.0)
724724
govuk_design_system_formbuilder (5.13.0) sha256=126ff4af70b36f06395e8f4f09d399944bcc10e9a41c2ff7d7773e81e92ca885
725725
govuk_notify_rails (3.0.0) sha256=e0e1b8b0a7c47f90963034f290fb2982652aa8051a733c25c178680d44f2d7aa
726726
hashdiff (1.2.1) sha256=9c079dbc513dfc8833ab59c0c2d8f230fa28499cc5efb4b8dd276cf931457cd1

app/controllers/forms/welsh_translation_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def render_preview
6565
def preview_html
6666
return t("guidance.no_guidance_added_html") if params[:markdown].blank?
6767

68-
GovukFormsMarkdown.render(params[:markdown])
68+
GovukFormsMarkdown.render(params[:markdown], locale: "cy")
6969
end
7070

7171
def welsh_enabled?

app/controllers/forms/what_happens_next_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ def what_happens_next_input_params
4646
def preview_html(what_happens_next_input)
4747
return t("guidance.no_guidance_added_html") if what_happens_next_input.what_happens_next_markdown.blank?
4848

49-
GovukFormsMarkdown.render(what_happens_next_input.what_happens_next_markdown, allow_headings: false)
49+
GovukFormsMarkdown.render(what_happens_next_input.what_happens_next_markdown, allow_headings: false, locale: "en")
5050
end
5151
end
5252
end

app/controllers/pages/exit_page_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,6 @@ def ensure_answer_value_present
100100
def preview_html(exit_page_input_object)
101101
return t("exit_page.no_content_added_html") if exit_page_input_object.exit_page_markdown.blank?
102102

103-
GovukFormsMarkdown.render(exit_page_input_object.exit_page_markdown)
103+
GovukFormsMarkdown.render(exit_page_input_object.exit_page_markdown, locale: "en")
104104
end
105105
end

app/controllers/pages/guidance_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def route_to
6868
def preview_html(guidance_input)
6969
return t("guidance.no_guidance_added_html") if guidance_input.guidance_markdown.blank?
7070

71-
GovukFormsMarkdown.render(guidance_input.guidance_markdown)
71+
GovukFormsMarkdown.render(guidance_input.guidance_markdown, locale: "en")
7272
end
7373

7474
def view_locals(current_page, guidance_input, back_link)

app/views/forms/_made_live_form.html.erb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,10 @@
9595
<%= table.with_body do |body|
9696
body.with_row do |row|
9797
row.with_cell do
98-
GovukFormsMarkdown.render(form_document.what_happens_next_markdown).html_safe
98+
GovukFormsMarkdown.render(form_document.what_happens_next_markdown, locale: "en").html_safe
9999
end
100100
row.with_cell do
101-
GovukFormsMarkdown.render(welsh_form_document.what_happens_next_markdown).html_safe
101+
GovukFormsMarkdown.render(welsh_form_document.what_happens_next_markdown, locale: "cy").html_safe
102102
end
103103
end
104104
end %>
@@ -108,7 +108,7 @@
108108
<% else %>
109109
<h3 class="govuk-heading-m"><%= t('.what_happens_next') %></h3>
110110
<div class="app-preview-area">
111-
<%= GovukFormsMarkdown.render(form_document.what_happens_next_markdown).html_safe %>
111+
<%= GovukFormsMarkdown.render(form_document.what_happens_next_markdown, locale: "en").html_safe %>
112112
</div>
113113
<%= govuk_details(summary_text: t('.what_is_what_happens_next'), text: t('.what_happens_next_description')) %>
114114
<% end %>

db/seeds.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,8 @@
454454
support_email_cy: "welsh-your.email+fakedata84701@gmail.com.gov.uk",
455455
support_phone: "08000800",
456456
support_phone_cy: "welsh 08000800",
457-
what_happens_next_markdown: "Test",
458-
what_happens_next_markdown_cy: "Prawf",
457+
what_happens_next_markdown: "If you have not received a response within 5 working days, [contact our user support team](https://example.com).",
458+
what_happens_next_markdown_cy: "Os nad ydych wedi derbyn ymateb o fewn 5 diwrnod gwaith, [cysylltwch â'n tîm cymorth defnyddwyr](https://example.com).",
459459
share_preview_completed: true,
460460
available_languages: %w[en cy],
461461
welsh_completed: true,

spec/requests/forms/welsh_translation_controller_spec.rb

Lines changed: 46 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@
2222
get welsh_translation_path(id)
2323
end
2424

25-
it "returns 200" do
26-
expect(response).to have_http_status(:ok)
27-
end
28-
2925
it "renders the template" do
26+
expect(response).to have_http_status(:ok)
3027
expect(response).to render_template(:new)
3128
end
3229

@@ -54,31 +51,17 @@
5451
let(:params) { { forms_welsh_translation_input: { form:, mark_complete:, name_cy: "Gwneud cais am drwydded jyglo", privacy_policy_url_cy: "https://juggling.gov.uk/privacy_policy/cy", page_translations_attributes: } } }
5552

5653
context "when 'Yes' is selected" do
57-
it "updates the form" do
54+
it "updates the form, pages and conditions" do
5855
expect {
5956
post(welsh_translation_create_path(id), params:)
6057
}.to change { form.reload.welsh_completed }.to(true)
58+
.and change { form.pages.first.reload.question_text_cy }.to("Ydych chi'n adnewyddu trwydded?")
59+
.and change { condition.reload.exit_page_heading_cy }.to("Nid ydych yn gymwys")
6160
end
6261

63-
it "updates the form's pages" do
64-
expect {
65-
post(welsh_translation_create_path(id), params:)
66-
}.to change { form.pages.first.reload.question_text_cy }.to("Ydych chi'n adnewyddu trwydded?")
67-
end
68-
69-
it "updates the form's conditions" do
70-
expect {
71-
post(welsh_translation_create_path(id), params:)
72-
}.to change { condition.reload.exit_page_heading_cy }.to("Nid ydych yn gymwys")
73-
end
74-
75-
it "redirects to the form" do
62+
it "redirects to the form task list and displays a success banner including text about being marked complete" do
7663
post(welsh_translation_create_path(id), params:)
7764
expect(response).to redirect_to(form_path(id))
78-
end
79-
80-
it "displays a success banner including text about being marked complete" do
81-
post(welsh_translation_create_path(id), params:)
8265
expect(flash[:success]).to eq(I18n.t("banner.success.form.welsh_translation_saved_and_completed"))
8366
end
8467
end
@@ -87,80 +70,49 @@
8770
let(:mark_complete) { "false" }
8871
let(:form) { create(:form, :ready_for_routing, welsh_completed: true) }
8972

90-
it "updates the form" do
73+
it "updates the form and redirects to the form task list" do
9174
expect {
9275
post(welsh_translation_create_path(id), params:)
9376
}.to change { form.reload.welsh_completed }.to(false)
9477
end
9578

96-
it "redirects to the form" do
79+
it "redirects to the form and displays a success banner without text about being marked complete" do
9780
post(welsh_translation_create_path(id), params:)
9881
expect(response).to redirect_to(form_path(id))
99-
end
100-
101-
it "displays a success banner without text about being marked complete" do
102-
post(welsh_translation_create_path(id), params:)
10382
expect(flash[:success]).to eq(I18n.t("banner.success.form.welsh_translation_saved"))
10483
end
10584
end
10685

10786
context "when no value is selected" do
10887
let(:mark_complete) { "" }
10988

110-
it "does not update the form" do
111-
expect {
112-
post(welsh_translation_create_path(id), params:)
113-
}.not_to(change { form.reload.welsh_completed })
114-
end
115-
116-
it "does not update the form's pages" do
89+
it "does not update the form, pages or conditions" do
11790
expect {
11891
post(welsh_translation_create_path(id), params:)
119-
}.not_to(change { form.pages.first.reload.question_text_cy })
92+
}.to not_change { form.reload.welsh_completed }
93+
.and not_change { form.pages.first.reload.question_text_cy }
94+
.and(not_change { condition.reload.exit_page_markdown_cy })
12095
end
12196

122-
it "does not update the form's conditions" do
123-
expect {
124-
post(welsh_translation_create_path(id), params:)
125-
}.not_to(change { condition.reload.exit_page_markdown_cy })
126-
end
127-
128-
it "returns a 422" do
97+
it "returns a 422, re-renders the page with an error, and does not display a success banner" do
12998
post(welsh_translation_create_path(id), params:)
130-
expect(response).to have_http_status(:unprocessable_content)
131-
end
13299

133-
it "re-renders the page with an error" do
134-
post(welsh_translation_create_path(id), params:)
100+
expect(response).to have_http_status(:unprocessable_content)
135101
expect(response).to render_template(:new)
136102
expect(response.body).to include(I18n.t("activemodel.errors.models.forms/welsh_translation_input.attributes.mark_complete.blank"))
137-
end
138-
139-
it "does not display a success banner" do
140-
post(welsh_translation_create_path(id), params:)
141103
expect(flash).to be_empty
142104
end
143105
end
144106

145107
context "when the user is not authorized" do
146108
let(:current_user) { build :user }
147109

148-
it "does not update the form" do
149-
expect {
150-
post(welsh_translation_create_path(id), params:)
151-
}.not_to(change { form.reload.welsh_completed })
152-
end
153-
154-
it "does not update the form's pages" do
155-
expect {
156-
post(welsh_translation_create_path(id), params:)
157-
}.not_to(change { form.pages.first.reload.question_text_cy })
158-
end
159-
160-
it "does not update the form's conditions" do
110+
it "does not update the form, pages or conditions" do
161111
expect {
162112
post(welsh_translation_create_path(id), params:)
163-
}.not_to(change { condition.reload.exit_page_heading_cy })
113+
}.to not_change { form.reload.welsh_completed }
114+
.and not_change { form.pages.first.reload.question_text_cy }
115+
.and(not_change { condition.reload.exit_page_markdown_cy })
164116
end
165117

166118
it "returns 403" do
@@ -172,22 +124,12 @@
172124
context "when the welsh feature is not enabled for the group" do
173125
let(:welsh_enabled) { false }
174126

175-
it "does not update the form" do
176-
expect {
177-
post(welsh_translation_create_path(id), params:)
178-
}.not_to(change { form.reload.welsh_completed })
179-
end
180-
181-
it "does not update the form's pages" do
127+
it "does not update the form, pages or conditions" do
182128
expect {
183129
post(welsh_translation_create_path(id), params:)
184-
}.not_to(change { form.pages.first.reload.question_text_cy })
185-
end
186-
187-
it "does not update the form's conditions" do
188-
expect {
189-
post(welsh_translation_create_path(id), params:)
190-
}.not_to(change { condition.reload.exit_page_heading_cy })
130+
}.to not_change { form.reload.welsh_completed }
131+
.and not_change { form.pages.first.reload.question_text_cy }
132+
.and(not_change { condition.reload.exit_page_markdown_cy })
191133
end
192134

193135
it "redirects to the form" do
@@ -306,4 +248,29 @@
306248
end
307249
end
308250
end
251+
252+
describe "#render_preview" do
253+
let(:markdown) { "[Markdown](https://example.com)" }
254+
255+
before do
256+
post welsh_translation_render_preview_path(form_id: form.id), params: { markdown: }
257+
end
258+
259+
it "returns a JSON object containing the converted HTML" do
260+
expect(response).to have_http_status(:ok)
261+
expect(response.body).to eq({
262+
preview_html: "<p class=\"govuk-body\"><a href=\"https://example.com\" class=\"govuk-link\" rel=\"noreferrer noopener\" target=\"_blank\">Markdown (agor mewn tab newydd)</a></p>",
263+
errors: [],
264+
}.to_json)
265+
end
266+
267+
context "when markdown is blank" do
268+
let(:markdown) { "" }
269+
270+
it "returns a JSON object containing the converted HTML" do
271+
expect(response).to have_http_status(:ok)
272+
expect(response.body).to eq({ preview_html: I18n.t("guidance.no_guidance_added_html"), errors: [] }.to_json)
273+
end
274+
end
275+
end
309276
end

0 commit comments

Comments
 (0)