diff --git a/Gemfile b/Gemfile
index fda0d60af..37d135131 100644
--- a/Gemfile
+++ b/Gemfile
@@ -49,7 +49,7 @@ gem "govuk-components", "~> 6"
gem "govuk_design_system_formbuilder", "~> 6"
# Our own custom markdown renderer
-gem "govuk-forms-markdown", github: "govuk-forms/govuk-forms-markdown", tag: "0.9.0"
+gem "govuk-forms-markdown", github: "govuk-forms/govuk-forms-markdown", tag: "0.10.0"
# For compiling our frontend assets
gem "vite_rails"
diff --git a/Gemfile.lock b/Gemfile.lock
index dce276f53..12262911b 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -22,10 +22,10 @@ GIT
GIT
remote: https://github.com/govuk-forms/govuk-forms-markdown.git
- revision: 84c6b2296accca578e7b77e7d1730472d9c9e7af
- tag: 0.9.0
+ revision: 3ca93546da82c3d39fb087e0e94891bafb2f9142
+ tag: 0.10.0
specs:
- govuk-forms-markdown (0.9.0)
+ govuk-forms-markdown (0.10.0)
redcarpet (~> 3.6)
GEM
@@ -898,7 +898,7 @@ CHECKSUMS
google-protobuf (4.35.0-x86_64-linux-musl) sha256=be0218520d77b2aee898b363514b03819f6f63f9c041ae0d0d79b4ce5247bffd
googleapis-common-protos-types (1.23.0) sha256=992e740a523794d9fc5f29a504465d8fc737aaa16c930fe7228e3346860faf0a
govuk-components (6.3.0) sha256=9e10aba5f88f9bead1ac792814e3551aadf17813080a74a17d69c6e0446bd88d
- govuk-forms-markdown (0.9.0)
+ govuk-forms-markdown (0.10.0)
govuk_design_system_formbuilder (6.2.0) sha256=d8cd1543776b7b15ba3d1774f16e3b8458c59446b0cad56ccbbc1e517a575e93
govuk_notify_rails (3.0.0) sha256=e0e1b8b0a7c47f90963034f290fb2982652aa8051a733c25c178680d44f2d7aa
hana (1.3.7) sha256=5425db42d651fea08859811c29d20446f16af196308162894db208cac5ce9b0d
diff --git a/app/helpers/email_format_helper.rb b/app/helpers/email_format_helper.rb
index ae60ce4f5..ef910ac93 100644
--- a/app/helpers/email_format_helper.rb
+++ b/app/helpers/email_format_helper.rb
@@ -9,11 +9,20 @@ def convert_newlines_to_html(text)
text.gsub("\n", "
")
end
+ def convert_newlines_to_markdown(text)
+ text.gsub("\n", " \n")
+ end
+
def normalize_whitespace_and_convert_to_html(text)
output = normalize_whitespace(text)
convert_newlines_to_html(output)
end
+ def normalize_whitespace_and_convert_to_markdown(text)
+ output = normalize_whitespace(text)
+ convert_newlines_to_markdown(output)
+ end
+
def markdown_to_html(markdown)
HtmlMarkdownSanitizer.new.render_scrubbed_markdown(markdown, allow_headings: false, for_email: true)
end
diff --git a/app/lib/markdown_handler.rb b/app/lib/markdown_handler.rb
new file mode 100644
index 000000000..6a9fc1662
--- /dev/null
+++ b/app/lib/markdown_handler.rb
@@ -0,0 +1,11 @@
+module MarkdownHandler
+ def self.erb
+ @erb ||= ActionView::Template.registered_template_handler(:erb)
+ end
+
+ def self.call(template)
+ erb.call(template)
+ end
+end
+
+ActionView::Template.register_template_handler :md, MarkdownHandler
diff --git a/app/lib/ses_email_formatter.rb b/app/lib/ses_email_formatter.rb
index 53217bb25..95a42ee92 100644
--- a/app/lib/ses_email_formatter.rb
+++ b/app/lib/ses_email_formatter.rb
@@ -12,57 +12,42 @@ def initialize(submission_reference:, steps:, confirmation_email:)
@confirmation_email = confirmation_email
end
- def build_question_answers_section_html(heading_level: 3)
+ def build_question_answers_section_markdown(heading_level: 3)
@steps.map { |step|
- [prep_question_title_html(step, heading_level),
- prep_answer_text_html(step)].join
- }.join(H_RULE)
- end
-
- def build_question_answers_section_plain_text
- @steps.map { |step|
- [prep_question_title_plain_text(step),
- prep_answer_text_plain_text(step)].join("\n\n")
+ [prep_question_title_markdown(step, heading_level),
+ prep_answer_text_markdown(step)].join("\n\n")
}.join(H_RULE_PLAIN_TEXT)
end
private
- def prep_question_title_html(step, heading_level)
+ def prep_question_title_markdown(step, heading_level)
case heading_level
when 3
- "
#{convert_newlines_to_html(prep_answer_text(step))}
" + prep_answer_text(step) end rescue StandardError raise FormattingError, "could not format answer for question step #{step.id}" end - def prep_html_none_of_the_above_answer_text(step) + def prep_none_of_the_above_answer_text_markdown(step) [ - "#{convert_newlines_to_html(prep_answer_text(step))}
", - "#{convert_newlines_to_html(prep_none_of_the_above_answer_text(step))}
", - ] - end - - def prep_question_title_plain_text(step) - step.question.question_text - end - - def prep_none_of_the_above_question_text(step) - step.question.none_of_the_above_question_text + prep_answer_text(step), + "#### #{step.question.none_of_the_above_question_text}", + prep_none_of_the_above_answer_markdown(step), + ].join("\n\n") end def prep_answer_text(step) @@ -75,7 +60,7 @@ def prep_answer_text(step) raise FormattingError, "could not format answer for question step #{step.id}" end - def prep_none_of_the_above_answer_text(step) + def prep_none_of_the_above_answer_markdown(step) answer = step.question.none_of_the_above_answer return skipped_question_text if answer.blank? @@ -85,22 +70,6 @@ def prep_none_of_the_above_answer_text(step) raise FormattingError, "could not format none of the above answer for question step #{step.id}" end - def prep_answer_text_plain_text(step) - if step.is_selection_with_none_of_the_above_answer? - prep_plain_text_none_of_the_above_answer_text(step) - else - prep_answer_text(step) - end - end - - def prep_plain_text_none_of_the_above_answer_text(step) - [ - prep_answer_text(step), - prep_none_of_the_above_question_text(step), - prep_none_of_the_above_answer_text(step), - ].join("\n\n") - end - def sanitize(text) text .then { normalize_whitespace _1 } diff --git a/app/models/submission.rb b/app/models/submission.rb index b83e0ffdf..b343e2e2f 100644 --- a/app/models/submission.rb +++ b/app/models/submission.rb @@ -59,12 +59,8 @@ def mode_object Mode.new(mode) end - def answer_content_for_email_html(heading_level:, locale: :en, confirmation_email: false) - ses_email_formatter(locale, confirmation_email).build_question_answers_section_html(heading_level:) - end - - def answer_content_for_email_plain_text(locale: :en, confirmation_email: false) - ses_email_formatter(locale, confirmation_email).build_question_answers_section_plain_text + def answer_content_for_email_markdown(heading_level:, locale: :en, confirmation_email: false) + ses_email_formatter(locale, confirmation_email).build_question_answers_section_markdown(heading_level:) end private diff --git a/app/views/aws_ses_form_submission_mailer/_content.md.erb b/app/views/aws_ses_form_submission_mailer/_content.md.erb new file mode 100644 index 000000000..bc764184b --- /dev/null +++ b/app/views/aws_ses_form_submission_mailer/_content.md.erb @@ -0,0 +1,45 @@ +<% if @submission.preview? %> +<%= I18n.t("mailer.submission.preview") %> + +<% end %> +<%= I18n.t("mailer.form_name", form_name: @submission.form.name) %> + +<%= I18n.t("mailer.submission.time", time: format_time(@submission.submission_time), date: format_date(@submission.submission_time) ) %> + +<% if @welsh_submission %> +<%= I18n.t("mailer.submission.welsh_submission") %> + +<% end %> +<%= I18n.t("mailer.submission.reference", submission_reference: @submission.reference) %> + +<% if @submission.payment_url.present? %> +<%= I18n.t("mailer.submission.payment") %> + +<% end %> +^<%= I18n.t("mailer.check_before_using") %>^ + +--- + +## <%= I18n.t("mailer.submission.answers_submitted") %> + +<% if @csv_filename.present? %> +<%= I18n.t("mailer.submission.csv_file", filename: @csv_filename) %> + +<% end %> +<% if @json_filename.present? %> +<%= I18n.t("mailer.submission.json_file", filename: @json_filename) %> + +<% end %> +<%= @submission.answer_content_for_email_markdown(heading_level: 3) %> + +--- + +^^^ + +## <%= I18n.t("mailer.cannot_reply.heading") %> + +<%= I18n.t("mailer.cannot_reply.contact_form_filler_markdown") %> + +<%= I18n.t("mailer.cannot_reply.contact_forms_team_markdown") %> + +^^^ diff --git a/app/views/aws_ses_form_submission_mailer/submission_email.html.erb b/app/views/aws_ses_form_submission_mailer/submission_email.html.erb index 1de8f537f..9793cf368 100644 --- a/app/views/aws_ses_form_submission_mailer/submission_email.html.erb +++ b/app/views/aws_ses_form_submission_mailer/submission_email.html.erb @@ -1,54 +1 @@ -<% if @submission.preview? %> -- <%= I18n.t("mailer.submission.preview") %> -
-<% end %> - -<%= I18n.t("mailer.form_name", form_name: @submission.form.name) %>
- -- <%= I18n.t("mailer.submission.time", time: format_time(@submission.submission_time), date: format_date(@submission.submission_time) ) %> -
-- <% if @welsh_submission %> - <%= I18n.t("mailer.submission.welsh_submission") %> - <% end %> -
-- <%= I18n.t("mailer.submission.reference", submission_reference: @submission.reference) %> -
- -<% if @submission.payment_url.present? %> -- <%= I18n.t("mailer.submission.payment") %> -
-<% end %> - -- <%= I18n.t("mailer.check_before_using") %> -
- -<%= I18n.t("mailer.submission.csv_file", filename: @csv_filename) %>
-<% end %> - -<% if @json_filename.present? %> -<%= I18n.t("mailer.submission.json_file", filename: @json_filename) %>
-<% end %> - -<%= @submission.answer_content_for_email_html(heading_level: 3).html_safe %> - -- <%= I18n.t("mailer.submission_batch.preview.#{@mode.tag}") %> -
-<% end %> - -<%= I18n.t("mailer.form_name", form_name: @form_name) %>
- -<% if @filenames.size > 1 %> -<%= I18n.t("mailer.submission_batch.daily.multiple_files_explainer", date: @date) %>
-<% else %> -<%= I18n.t("mailer.submission_batch.daily.single_file_explainer", date: @date) %>
-<% end %> - -- <%= I18n.t("mailer.check_before_using") %> -
- -<%= I18n.t("mailer.submission_batch.submissions_are_split") %>
- -<%= I18n.t("mailer.submission_batch.file_upload_questions") %>
- -- <%= I18n.t("mailer.submission_batch.preview.#{@mode.tag}") %> -
-<% end %> - -<%= I18n.t("mailer.form_name", form_name: @form_name) %>
- -<% if @filenames.size > 1 %> -<%= I18n.t("mailer.submission_batch.weekly.multiple_files_explainer", begin_date: @begin_date, end_date: @end_date) %>
-<% else %> -<%= I18n.t("mailer.submission_batch.weekly.single_file_explainer", begin_date: @begin_date, end_date: @end_date) %>
-<% end %> - -- <%= I18n.t("mailer.check_before_using") %> -
- -<%= I18n.t("mailer.submission_batch.submissions_are_split") %>
- -<%= I18n.t("mailer.submission_batch.file_upload_questions") %>
- -<%= I18n.t("mailer.submission_confirmation.this_is_a_test") %>
-<% else %> -<%= I18n.t("mailer.submission_confirmation.submitted_at", form_name: form.name, - time: format_time(@submission.submission_time), - date: format_date(@submission.submission_time)) %>
- -<%= I18n.t("mailer.submission_confirmation.reference_number", reference: @submission.reference) %>
- -<%= I18n.t("mailer.submission_confirmation.you_cannot_reply") %>
-<%= I18n.t("mailer.submission_confirmation.payment_link_html", payment_link: form.payment_url_with_reference(@submission.reference)).html_safe %>
-<% end %> - -<%= I18n.t("mailer.submission_confirmation.default_what_happens_next") %>
-<% end %> - -<% if @include_copy_of_answers %> -<%= normalize_whitespace_and_convert_to_html(form.support_details.phone).html_safe %>
-<%= I18n.t('support_details.call_charges') %>
- <% end %> - - <% if form.support_details.email.present? %> -<%= form.support_details.email %>
- <% end %> - - <% if form.support_details.url.present? && form.support_details.url_text.present? %> -<%= form.support_details.url_text %>
- <% end %> -<% else %> -<%= I18n.t("mailer.submission_confirmation.default_support_contact_details") %>
-<% end %> diff --git a/app/views/aws_ses_submission_confirmation_mailer/_content.md.erb b/app/views/aws_ses_submission_confirmation_mailer/_content.md.erb new file mode 100644 index 000000000..7bc8fcec4 --- /dev/null +++ b/app/views/aws_ses_submission_confirmation_mailer/_content.md.erb @@ -0,0 +1,70 @@ +<% if @submission.preview? %> + +## <%= I18n.t("mailer.submission_confirmation.title_preview") %> + +<%= I18n.t("mailer.submission_confirmation.this_is_a_test") %> + +<% else %> + +## <%= I18n.t("mailer.submission_confirmation.title") %> + +<% end %> + +<%= I18n.t("mailer.submission_confirmation.submitted_at", form_name: form.name, +time: format_time(@submission.submission_time), +date: format_date(@submission.submission_time)) %> + +<%= I18n.t("mailer.submission_confirmation.reference_number", reference: @submission.reference) %> + +^<%= I18n.t("mailer.submission_confirmation.you_cannot_reply") %>^ + +<% if form.payment_url.present? %> + +### <%= I18n.t("mailer.submission_confirmation.payment_link_heading") %> + +<%= I18n.t("mailer.submission_confirmation.payment_link_markdown", payment_link: form.payment_url_with_reference(@submission.reference)) %> + +<% end %> + +### <%= I18n.t("mailer.submission_confirmation.what_happens_next_heading") %> + +<% if form.what_happens_next_markdown.present? %> +<%= form.what_happens_next_markdown %> +<% else %> + +<%= I18n.t("mailer.submission_confirmation.default_what_happens_next") %> +<% end %> + +<% if @include_copy_of_answers %> + +### <%= I18n.t("mailer.submission_confirmation.answers_submitted_heading") %> + +<%= @submission.answer_content_for_email_markdown(heading_level: 4, locale: locale, confirmation_email: true) %> + +--- + +<% end %> + +### <%= I18n.t("mailer.submission_confirmation.contact_details_heading") %> + +<% if form.support_details.phone || form.support_details.email || form.support_details.url %> +<% if form.support_details.phone.present? %> + +<%= normalize_whitespace_and_convert_to_markdown(form.support_details.phone) %> + +[<%= I18n.t('support_details.call_charges') %>](<%= form.support_details.call_charges_url %>) +<% end %> + +<% if form.support_details.email.present? %> + +[<%= form.support_details.email %>](mailto:<%= form.support_details.email %>) +<% end %> + +<% if form.support_details.url.present? && form.support_details.url_text.present? %> + +[<%= form.support_details.url_text %>](<%= form.support_details.url %>) +<% end %> +<% else %> + +<%= I18n.t("mailer.submission_confirmation.default_support_contact_details") %> +<% end %> diff --git a/app/views/aws_ses_submission_confirmation_mailer/_content.text.erb b/app/views/aws_ses_submission_confirmation_mailer/_content.text.erb deleted file mode 100644 index e8f831b76..000000000 --- a/app/views/aws_ses_submission_confirmation_mailer/_content.text.erb +++ /dev/null @@ -1,55 +0,0 @@ -<% if @submission.preview? %> -<%= I18n.t("mailer.submission_confirmation.title_preview") %> - -<%= I18n.t("mailer.submission_confirmation.this_is_a_test") %> -<% else %> -<%= I18n.t("mailer.submission_confirmation.title") %> -<% end %> - -<%= I18n.t("mailer.submission_confirmation.submitted_at", form_name: form.name, - time: format_time(@submission.submission_time), - date: format_date(@submission.submission_time)) %> - -<%= I18n.t("mailer.submission_confirmation.reference_number", reference: @submission.reference) %> - -<%= I18n.t("mailer.submission_confirmation.you_cannot_reply") %> - -<% if form.payment_url.present? %> -<%= I18n.t("mailer.submission_confirmation.payment_link_heading") %> - -<%= strip_tags(I18n.t("mailer.submission_confirmation.payment_link_html", payment_link: form.payment_url_with_reference(@submission.reference))) %> - -<% end %> -<%= I18n.t("mailer.submission_confirmation.what_happens_next_heading") %> - -<% if form.what_happens_next_markdown.present? %> -<%= markdown_to_plain_text(form.what_happens_next_markdown) %> -<% else %> -<%= I18n.t("mailer.submission_confirmation.default_what_happens_next") %> -<% end %> - -<% if @include_copy_of_answers %> -<%= I18n.t("mailer.submission_confirmation.answers_submitted_heading") %> - -<%= @submission.answer_content_for_email_plain_text(locale: locale, confirmation_email: true) %> - -<% end %> -<%= I18n.t("mailer.submission_confirmation.contact_details_heading") %> - -<% if form.support_details.phone || form.support_details.email || form.support_details.url %> - <% if form.support_details.phone.present? %> -<%= normalize_whitespace(form.support_details.phone).html_safe %> - -<%= I18n.t('support_details.call_charges') %>: <%= form.support_details.call_charges_url %> - <% end %> - - <% if form.support_details.email.present? %> -<%= form.support_details.email %> - <% end %> - - <% if form.support_details.url.present? && form.support_details.url_text.present? %> -<%= form.support_details.url_text %>: <%= form.support_details.url %> - <% end %> -<% else %> - <%= I18n.t("mailer.submission_confirmation.default_support_contact_details") %> -<% end %> diff --git a/app/views/aws_ses_submission_confirmation_mailer/submission_confirmation_email.html.erb b/app/views/aws_ses_submission_confirmation_mailer/submission_confirmation_email.html.erb index 708df345e..bab3a619f 100644 --- a/app/views/aws_ses_submission_confirmation_mailer/submission_confirmation_email.html.erb +++ b/app/views/aws_ses_submission_confirmation_mailer/submission_confirmation_email.html.erb @@ -1,8 +1,9 @@ -<%= render partial: "content", locals: { form: @form, locale: :en } %> +<%= GovukFormsMarkdown.render_for_email(render(partial: "content", formats: [:md], locals: { form: @form, locale: :en })).html_safe %> <% if @submission_locale == :cy %> <% I18n.with_locale(:cy) do %> -If you need to contact the person who completed this form, you’ll need to contact them directly.
" - contact_form_filler_plain: If you need to contact the person who completed this form, you’ll need to contact them directly. - contact_forms_team_html:If you’re experiencing a technical issue with this form, contact the GOV.UK Forms team with details of the issue and the form it relates to.
- contact_forms_team_plain: If you’re experiencing a technical issue with this form, contact the GOV.UK Forms team (https://www.forms.service.gov.uk/support) with details of the issue and the form it relates to. + contact_form_filler_markdown: If you need to contact the person who completed this form, you’ll need to contact them directly. + contact_forms_team_markdown: If you’re experiencing a technical issue with this form, [contact the GOV.UK Forms team](https://www.forms.service.gov.uk/support) with details of the issue and the form it relates to. heading: You cannot reply to this email check_before_using: Check that these answers look safe before you use them form_name: 'Form name: “%{form_name}”' @@ -526,7 +524,7 @@ cy: file_answer: Rydych wedi llwytho ffeil o’r enw %{filename} not_completed: Heb ei gwblhau payment_link_heading: Os ydych dal angen talu - payment_link_html: 'Os ydych angen gwneud taliad ac nad ydych wedi gwneud hynny eisoes, defnyddiwch y ddolen dalu hon i dalu nawr: %{payment_link}' + payment_link_markdown: 'Os ydych angen gwneud taliad ac nad ydych wedi gwneud hynny eisoes, defnyddiwch y ddolen dalu hon i dalu nawr: [%{payment_link}](%{payment_link})' reference_number: Rhif cyfeirnod eich ffurflen yw %{reference}. subject: 'Mae eich ffurflen wedi’i chyflwyno’n llwyddiannus – cyfeirnod: %{reference}' subject_preview: 'TEST EMAIL CONFIRMING SUBMISSION: Mae eich ffurflen wedi’i chyflwyno’n llwyddiannus – cyfeirnod: %{reference}' diff --git a/config/locales/en.yml b/config/locales/en.yml index 942603a06..8cf83b572 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -480,10 +480,8 @@ en: bounced_weekly_batch: Weekly batch sent at %{sent_at_time} on %{sent_at_date} contacted_group_admins_paragraph: The form is in the group “%{group_name}”, which has no group admins or whose group admins have not responded. cannot_reply: - contact_form_filler_html: "If you need to contact the person who completed this form, you’ll need to contact them directly.
" - contact_form_filler_plain: If you need to contact the person who completed this form, you’ll need to contact them directly. - contact_forms_team_html:If you’re experiencing a technical issue with this form, contact the GOV.UK Forms team with details of the issue and the form it relates to.
- contact_forms_team_plain: If you’re experiencing a technical issue with this form, contact the GOV.UK Forms team (https://www.forms.service.gov.uk/support) with details of the issue and the form it relates to. + contact_form_filler_markdown: If you need to contact the person who completed this form, you’ll need to contact them directly. + contact_forms_team_markdown: If you’re experiencing a technical issue with this form, [contact the GOV.UK Forms team](https://www.forms.service.gov.uk/support) with details of the issue and the form it relates to. heading: You cannot reply to this email check_before_using: Check that these answers look safe before you use them form_name: 'Form name: “%{form_name}”' @@ -512,7 +510,7 @@ en: archived: These are test submissions to a preview of an archived form. draft: These are test submissions to a preview of a draft form. live: These are test submissions to a preview of a live form. - submissions_are_split: Submissions are split into separate CSV files if the form's been changed in a way that affects the structure of the CSV. + submissions_are_split: Submissions are split into separate CSV files if the form’s been changed in a way that affects the structure of the CSV. weekly: multiple_files_explainer: 'All submissions to this form from %{begin_date} to %{end_date} are attached to this email in CSV files named:' single_file_explainer: 'All submissions to this form from %{begin_date} to %{end_date} are attached to this email in a CSV file named:' @@ -526,12 +524,12 @@ en: file_answer: You uploaded a file called %{filename} not_completed: Not completed payment_link_heading: If you still need to pay - payment_link_html: 'If you need to make a payment and have not done so already, use this payment link to pay now: %{payment_link}' + payment_link_markdown: 'If you need to make a payment and have not done so already, use this payment link to pay now: [%{payment_link}](%{payment_link})' reference_number: Your form reference number is %{reference}. subject: 'Your form has been successfully submitted – reference: %{reference}' subject_preview: 'TEST EMAIL CONFIRMING SUBMISSION: Your form has been successfully submitted – reference: %{reference}' submitted_at: "“%{form_name}” was successfully submitted at %{time} on %{date}." - this_is_a_test: This is a test email confirming that a form's been submitted. + this_is_a_test: This is a test email confirming that a form’s been submitted. title: Your form has been successfully submitted title_preview: 'TEST EMAIL CONFIRMING SUBMISSION: Your form has been successfully submitted' what_happens_next_heading: What happens next diff --git a/spec/lib/ses_email_formatter_spec.rb b/spec/lib/ses_email_formatter_spec.rb index b507c96c6..2361b9063 100644 --- a/spec/lib/ses_email_formatter_spec.rb +++ b/spec/lib/ses_email_formatter_spec.rb @@ -27,27 +27,19 @@ let(:none_of_the_above_step) { build :step, question: none_of_the_above_question } let(:steps) { [text_step] } - describe "#build_question_answers_section_html" do + describe "#build_question_answers_section_markdown" do context "when there is one step" do - it "returns question and and answer HTML" do - expected = <<~HTML.strip.gsub("\n", "") -42
- HTML - expect(ses_email_formatter.build_question_answers_section_html).to eq(expected) + it "returns question and and answer markdown" do + expect(ses_email_formatter.build_question_answers_section_markdown).to eq("### What is the meaning of life?\n\n42") end - it "formats with heading level 4" do - expected = <<~HTML.strip.gsub("\n", "") -42
- HTML - expect(ses_email_formatter.build_question_answers_section_html(heading_level: 4)).to eq(expected) + it "formats with the specified heading level" do + expect(ses_email_formatter.build_question_answers_section_markdown(heading_level: 4)).to eq("#### What is the meaning of life?\n\n42") end it "raises an error if the heading level is unsupported" do expect { - ses_email_formatter.build_question_answers_section_html(heading_level: 2) + ses_email_formatter.build_question_answers_section_markdown(heading_level: 2) }.to raise_error(SesEmailFormatter::FormattingError, "unsupported heading level: 2") end end @@ -56,11 +48,7 @@ let(:steps) { [name_step] } it "inserts line breaks between answer attributes" do - expected = <<~HTML.strip.gsub("\n", "") -First name: #{name_question.first_name}
Last name: #{name_question.last_name}
[This question was skipped]
- HTML - expect(ses_email_formatter.build_question_answers_section_html).to eq(expected) + expect(ses_email_formatter.build_question_answers_section_markdown).to eq("### What is the meaning of life?\n\n[This question was skipped]") end context "when formatting for a confirmation email" do let(:confirmation_email) { true } it "returns the blank answer text" do - expect(ses_email_formatter.build_question_answers_section_html).to include("Not completed
") + expect(ses_email_formatter.build_question_answers_section_markdown).to include("Not completed") end end end @@ -89,150 +73,7 @@ let(:steps) { [text_step, name_step] } it "returns all question an answers separated by a horizontal rule" do - expected = <<~HTML.strip.gsub("\n", "") -42
-First name: #{name_question.first_name}
Last name: #{name_question.last_name}
#{test_case[:output]}
- HTML - expect(ses_email_formatter.build_question_answers_section_html).to eq(expected) - end - end - end - - context "when none of the above is selected in a none of the above question" do - let(:steps) { [none_of_the_above_step] } - - it "returns the sanitized answer including the none of the above answer" do - expected = <<~HTML.strip.gsub("\n", "") -None of the above
-Cheese and pickle
- HTML - expect(ses_email_formatter.build_question_answers_section_html).to eq(expected) - end - - context "when the none of the above question has no answer is provided" do - let(:none_of_the_above_answer) { nil } - let(:none_of_the_above_question_is_optional) { "true" } - - it "returns the skipped none of the above answer text" do - expected = <<~HTML.strip.gsub("\n", "") -None of the above
-[This question was skipped]
- HTML - expect(ses_email_formatter.build_question_answers_section_html).to eq(expected) - end - - context "when formatting for a confirmation email" do - let(:confirmation_email) { true } - - it "returns the skipped none of the above answer text for a confirmation email" do - expect(ses_email_formatter.build_question_answers_section_html).to include("Not completed
") - end - end - end - end - - context "when there is a file question" do - let(:steps) { [file_step] } - - context "when formatting for a submission email" do - it "returns the content for a submission email" do - expected = <<~HTML.strip.gsub("\n", "") -a-file_SUB-12345.txt (attached to this email)
- HTML - expect(ses_email_formatter.build_question_answers_section_html).to eq(expected) - end - end - - context "when formatting for a confirmation email" do - let(:confirmation_email) { true } - - it "returns the content for a confirmation email" do - expected = <<~HTML.strip.gsub("\n", "") -You uploaded a file called a-file.txt
- HTML - expect(ses_email_formatter.build_question_answers_section_html).to eq(expected) - end - end - end - - context "when there is an error formatting an answer" do - before do - allow(text_step).to receive(:show_answer_in_email).and_raise(NoMethodError, "undefined method 'strip' for an instance of Array") - end - - it "raises an error with the page id" do - expect { - ses_email_formatter.build_question_answers_section_html - }.to raise_error(SesEmailFormatter::FormattingError, "could not format answer for question step #{text_step.id}") - end - end - end - - describe "#build_question_answers_section_plain_text" do - context "when there is one step" do - it "returns question and and answer HTML" do - expect(ses_email_formatter.build_question_answers_section_plain_text).to eq("What is the meaning of life?\n\n42") - end - end - - context "when the answer has multiple attributes" do - let(:steps) { [name_step] } - - it "inserts line breaks between answer attributes" do - expect(ses_email_formatter.build_question_answers_section_plain_text).to eq("What is your name?\n\nFirst name: #{name_question.first_name}\n\nLast name: #{name_question.last_name}") - end - end - - context "when the answer is blank i.e. skipped" do - let(:text_question) { build :text, question_text: "What is the meaning of life?", text: nil } - let(:steps) { [text_step] } - - it "returns the blank answer text" do - expect(ses_email_formatter.build_question_answers_section_plain_text).to eq("What is the meaning of life?\n\n[This question was skipped]") - end - - context "when formatting for a confirmation email" do - let(:confirmation_email) { true } - - it "returns the blank answer text" do - expect(ses_email_formatter.build_question_answers_section_plain_text).to include("Not completed") - end - end - end - - context "when there is more than one step" do - let(:steps) { [text_step, name_step] } - - it "returns all question an answers separated by a horizontal rule" do - expect(ses_email_formatter.build_question_answers_section_plain_text).to eq("What is the meaning of life?\n\n42\n\n---\n\nWhat is your name?\n\nFirst name: #{name_question.first_name}\n\nLast name: #{name_question.last_name}") + expect(ses_email_formatter.build_question_answers_section_markdown).to eq("### What is the meaning of life?\n\n42\n\n---\n\n### What is your name?\n\nFirst name: #{name_question.first_name}\n\nLast name: #{name_question.last_name}") end end @@ -247,7 +88,7 @@ ].each do |test_case| text_question.text = test_case[:input] - expect(ses_email_formatter.build_question_answers_section_plain_text).to eq("What is the meaning of life?\n\n#{test_case[:output]}") + expect(ses_email_formatter.build_question_answers_section_markdown).to eq("### What is the meaning of life?\n\n#{test_case[:output]}") end end end @@ -256,7 +97,7 @@ let(:steps) { [none_of_the_above_step] } it "returns the sanitized answer including the none of the above answer" do - expect(ses_email_formatter.build_question_answers_section_plain_text).to eq("What sandwich do you want?\n\nNone of the above\n\nSpecify your desired sandwich\n\nCheese and pickle") + expect(ses_email_formatter.build_question_answers_section_markdown).to eq("### What sandwich do you want?\n\nNone of the above\n\n#### Specify your desired sandwich\n\nCheese and pickle") end context "when the none of the above question is optional and no answer is provided" do @@ -264,14 +105,14 @@ let(:none_of_the_above_question_is_optional) { "true" } it "returns the skipped none of the above answer text" do - expect(ses_email_formatter.build_question_answers_section_plain_text).to eq("What sandwich do you want?\n\nNone of the above\n\nSpecify your desired sandwich (optional)\n\n[This question was skipped]") + expect(ses_email_formatter.build_question_answers_section_markdown).to eq("### What sandwich do you want?\n\nNone of the above\n\n#### Specify your desired sandwich (optional)\n\n[This question was skipped]") end context "when formatting for a confirmation email" do let(:confirmation_email) { true } it "returns the skipped none of the above answer text" do - expect(ses_email_formatter.build_question_answers_section_plain_text).to include("Not completed") + expect(ses_email_formatter.build_question_answers_section_markdown).to include("Not completed") end end end @@ -282,7 +123,7 @@ context "when formatting for a submission email" do it "returns the content for a submission email" do - expect(ses_email_formatter.build_question_answers_section_plain_text).to eq("Upload a file\n\na-file_SUB-12345.txt (attached to this email)") + expect(ses_email_formatter.build_question_answers_section_markdown).to eq("### Upload a file\n\na-file_SUB-12345.txt (attached to this email)") end end @@ -290,7 +131,7 @@ let(:confirmation_email) { true } it "returns the content for a confirmation email" do - expect(ses_email_formatter.build_question_answers_section_plain_text).to eq("Upload a file\n\nYou uploaded a file called a-file.txt") + expect(ses_email_formatter.build_question_answers_section_markdown).to eq("### Upload a file\n\nYou uploaded a file called a-file.txt") end end end @@ -302,7 +143,7 @@ it "raises an error with the page id" do expect { - ses_email_formatter.build_question_answers_section_plain_text + ses_email_formatter.build_question_answers_section_markdown }.to raise_error(SesEmailFormatter::FormattingError, "could not format answer for question step #{text_step.id}") end end diff --git a/spec/mailers/aws_ses_form_submission_mailer_spec.rb b/spec/mailers/aws_ses_form_submission_mailer_spec.rb index e0abaa607..7dcfc3e62 100644 --- a/spec/mailers/aws_ses_form_submission_mailer_spec.rb +++ b/spec/mailers/aws_ses_form_submission_mailer_spec.rb @@ -79,8 +79,8 @@ it "includes the warning about not replying" do expect(part.body).to have_css("h2", text: I18n.t("mailer.cannot_reply.heading")) - expect(part.body).to include(I18n.t("mailer.cannot_reply.contact_form_filler_html")) - expect(part.body).to include(I18n.t("mailer.cannot_reply.contact_forms_team_html")) + expect(part.body).to include(GovukFormsMarkdown.render_for_email(I18n.t("mailer.cannot_reply.contact_form_filler_markdown"))) + expect(part.body).to include(GovukFormsMarkdown.render_for_email(I18n.t("mailer.cannot_reply.contact_forms_team_markdown"))) end describe "submission date/time" do @@ -156,8 +156,8 @@ it "includes the warning about not replying" do expect(part.body).to have_text(I18n.t("mailer.cannot_reply.heading")) - expect(part.body).to include(I18n.t("mailer.cannot_reply.contact_form_filler_plain")) - expect(part.body).to include(I18n.t("mailer.cannot_reply.contact_forms_team_plain")) + expect(part.body).to include(GovukFormsMarkdown.render_plain_text(I18n.t("mailer.cannot_reply.contact_form_filler_markdown"))) + expect(part.body).to include(GovukFormsMarkdown.render_plain_text(I18n.t("mailer.cannot_reply.contact_forms_team_markdown"))) end describe "submission date/time" do diff --git a/spec/mailers/aws_ses_submission_batch_mailer_spec.rb b/spec/mailers/aws_ses_submission_batch_mailer_spec.rb index 0e4ac02ae..ffa0456dd 100644 --- a/spec/mailers/aws_ses_submission_batch_mailer_spec.rb +++ b/spec/mailers/aws_ses_submission_batch_mailer_spec.rb @@ -110,7 +110,7 @@ end it "lists the filename of the attachment" do - expect(part.body).to have_text(" • filename.csv") + expect(part.body).to have_text("• filename.csv") end end @@ -122,8 +122,8 @@ end it "lists the filenames of the attachments" do - expect(part.body).to have_text(" • filename.csv") - expect(part.body).to have_text(" • filename-2.csv") + expect(part.body).to have_text("• filename.csv") + expect(part.body).to have_text("• filename-2.csv") end end end diff --git a/spec/models/submission_spec.rb b/spec/models/submission_spec.rb index b5dc23652..a9f12d887 100644 --- a/spec/models/submission_spec.rb +++ b/spec/models/submission_spec.rb @@ -183,33 +183,19 @@ end let(:answers) { { "q1" => { text: "blue" }, "q2" => { first_name: "Jane", last_name: "Doe" } } } - describe "#answer_content_for_email_html" do - it "uses the English form document to construct the HTML by default" do - result = submission.answer_content_for_email_html(heading_level: 4) + describe "#answer_content_for_email_markdown" do + it "uses the English form document to construct the markdown by default" do + result = submission.answer_content_for_email_markdown(heading_level: 4) expect(result).to include("What is your favourite colour?") end - it "uses the Welsh form document to construct the HTML when the locale is :cy" do - result = submission.answer_content_for_email_html(heading_level: 4, locale: :cy) + it "uses the Welsh form document to construct the markdown when the locale is :cy" do + result = submission.answer_content_for_email_markdown(heading_level: 4, locale: :cy) expect(result).to include("Beth yw eich hoff liw?") end end - - describe "#answer_content_for_email_plain_text" do - it "uses the English form document to construct the answer content by default" do - result = submission.answer_content_for_email_plain_text - - expect(result).to start_with("What is your favourite colour?") - end - - it "uses the Welsh form document to construct the answer content when the locale is :cy" do - result = submission.answer_content_for_email_plain_text(locale: :cy) - - expect(result).to start_with("Beth yw eich hoff liw?") - end - end end describe "#sent?" do