From 31203cf03a6caafa9739309184c8776e7e9556c7 Mon Sep 17 00:00:00 2001 From: Edward Kerry Date: Wed, 17 Apr 2019 15:06:49 +0100 Subject: [PATCH] Prevent withdrawal notices on translated editions displaying multiple languages Currently a withdrawn translated edition may display the majority of the withdrawal notice title in English, but with dates and publication type in the chosen locale. Until withdrawal notices are also available via I18n, this PR will ensure only English is displayed. --- app/presenters/content_item/withdrawable.rb | 8 +++-- .../content_item/withdrawable_test.rb | 30 ++++++++++++++++++- 2 files changed, 35 insertions(+), 3 deletions(-) diff --git a/app/presenters/content_item/withdrawable.rb b/app/presenters/content_item/withdrawable.rb index 1df4af1d7..86a0cda2f 100644 --- a/app/presenters/content_item/withdrawable.rb +++ b/app/presenters/content_item/withdrawable.rb @@ -31,11 +31,15 @@ def withdrawal_notice_title end def withdrawal_notice_context - I18n.t("content_item.schema_name.#{schema_name}", count: 1) + I18n.t("content_item.schema_name.#{schema_name}", count: 1, locale: :en) end def withdrawal_notice_time - content_tag(:time, display_date(withdrawal_notice["withdrawn_at"]), datetime: withdrawal_notice["withdrawn_at"]) + content_tag(:time, english_display_date(withdrawal_notice["withdrawn_at"]), datetime: withdrawal_notice["withdrawn_at"]) + end + + def english_display_date(timestamp, format = "%-d %B %Y") + I18n.l(Time.zone.parse(timestamp), format: format, locale: :en) if timestamp end end end diff --git a/test/presenters/content_item/withdrawable_test.rb b/test/presenters/content_item/withdrawable_test.rb index d321c7d6d..5862f60e5 100644 --- a/test/presenters/content_item/withdrawable_test.rb +++ b/test/presenters/content_item/withdrawable_test.rb @@ -4,6 +4,11 @@ class ContentItemWithdrawableTest < ActiveSupport::TestCase def setup @withdrawable = Object.new @withdrawable.extend(ContentItem::Withdrawable) + I18n.locale = :cy + end + + def teardown + I18n.locale = I18n.default_locale end test 'content item is withdrawn' do @@ -60,7 +65,30 @@ def content_item end end - assert_equal @withdrawable.withdrawal_notice_component[:title], "This news article was withdrawn on " + assert_equal @withdrawable.withdrawal_notice_component[:title], "This news article was withdrawn on " assert_equal @withdrawable.withdrawal_notice_component[:description_govspeak], "

It has been superseded by Local area walking and cycling in England: 2014 to 2015.

\\n
" end + + test 'notice title presents only in English, even if locale is set to another language' do + # This is to prevent the withdrawal notices on translated editions + # displaying a combination of languages in their titles. + class << @withdrawable + def schema_name + 'publication' + end + + def content_item + { + 'title' => 'Proportion of residents who do any walking or cycling (at local authority level) (CW010)', + 'withdrawn_notice' => { + 'explanation' => '

It has been superseded by Local area walking and cycling in England: 2014 to 2015.

\n
', + 'withdrawn_at' => '2016-07-12T09:47:15Z' + }, + 'locale': 'cy' + } + end + end + + assert_equal @withdrawable.withdrawal_notice_component[:title], "This publication was withdrawn on " + end end