From ae70f4dbde94de6513c4e84c20c46586e6a304e3 Mon Sep 17 00:00:00 2001 From: irisfaraway Date: Fri, 29 Sep 2023 12:32:09 +0100 Subject: [PATCH 1/6] Apply correct html lang to static pages also https://eaflood.atlassian.net/browse/IWTF-3674 This change makes sure we also apply the correct html lang tag to pages that don't use the pageHandler, like the privacy policy. --- .../__snapshots__/page-handler.spec.js.snap | 4 +- .../handlers/__tests__/page-handler.spec.js | 44 +++---------------- .../src/handlers/page-handler.js | 8 +--- .../__tests__/page-language-helper.spec.js | 29 ++++++++++++ .../src/processors/page-language-helper.js | 4 ++ .../__tests__/misc-routes-handlers.spec.js | 21 ++++++++- .../src/routes/misc-routes.js | 7 +++ 7 files changed, 71 insertions(+), 46 deletions(-) create mode 100644 packages/gafl-webapp-service/src/processors/__tests__/page-language-helper.spec.js create mode 100644 packages/gafl-webapp-service/src/processors/page-language-helper.js diff --git a/packages/gafl-webapp-service/src/handlers/__tests__/__snapshots__/page-handler.spec.js.snap b/packages/gafl-webapp-service/src/handlers/__tests__/__snapshots__/page-handler.spec.js.snap index 6b58fda164..a0a8b3fac6 100644 --- a/packages/gafl-webapp-service/src/handlers/__tests__/__snapshots__/page-handler.spec.js.snap +++ b/packages/gafl-webapp-service/src/handlers/__tests__/__snapshots__/page-handler.spec.js.snap @@ -13,7 +13,7 @@ exports[`The page handler function sets the value of pageData with displayAnalyt "backRef": null, "displayAnalytics": false, "mssgs": undefined, - "pageLanguageSetToWelsh": false, + "pageLanguageSetToWelsh": undefined, "uri": Object { "analyticsFormAction": "/buy/process-analytics-preferences", }, @@ -42,7 +42,7 @@ exports[`The page handler function sets the value of pageData with displayAnalyt "backRef": null, "displayAnalytics": true, "mssgs": undefined, - "pageLanguageSetToWelsh": false, + "pageLanguageSetToWelsh": undefined, "uri": Object { "analyticsFormAction": "/buy/process-analytics-preferences", }, diff --git a/packages/gafl-webapp-service/src/handlers/__tests__/page-handler.spec.js b/packages/gafl-webapp-service/src/handlers/__tests__/page-handler.spec.js index 49a73d74b7..262ead51b1 100644 --- a/packages/gafl-webapp-service/src/handlers/__tests__/page-handler.spec.js +++ b/packages/gafl-webapp-service/src/handlers/__tests__/page-handler.spec.js @@ -1,12 +1,14 @@ import pageHandler from '../page-handler.js' import journeyDefinition from '../../routes/journey-definition.js' import { addLanguageCodeToUri } from '../../processors/uri-helper.js' +import { welshEnabledAndApplied } from '../../processors/page-language-helper.js' import GetDataRedirect from '../get-data-redirect.js' import { ANALYTICS } from '../../constants.js' import { AGREED, IDENTIFY, LICENCE_DETAILS, LICENCE_FOR, ORDER_COMPLETE, PAYMENT_CANCELLED, PAYMENT_FAILED } from '../../uri.js' jest.mock('../../routes/journey-definition.js', () => []) jest.mock('../../processors/uri-helper.js') +jest.mock('../../processors/page-language-helper.js') jest.mock('../../constants', () => ({ ANALYTICS: { @@ -243,8 +245,9 @@ describe('The page handler function', () => { }) describe('pageLanguageSetToWelsh', () => { - it('returns false when SHOW_WELSH_CONTENT is not true', async () => { - process.env.SHOW_WELSH_CONTENT = false + it('returns the value of welshEnabledAndApplied', async () => { + const expectedValue = Symbol('expected') + welshEnabledAndApplied.mockReturnValueOnce(expectedValue) const { get } = pageHandler('', 'view', '/next/page') const toolkit = getMockToolkit() @@ -253,39 +256,7 @@ describe('The page handler function', () => { expect(toolkit.view).toHaveBeenCalledWith( expect.any(String), expect.objectContaining({ - pageLanguageSetToWelsh: false - }) - ) - }) - - it('returns false when SHOW_WELSH_CONTENT is true but the lang is not set to cy', async () => { - process.env.SHOW_WELSH_CONTENT = true - const { get } = pageHandler('', 'view', '/next/page') - const toolkit = getMockToolkit() - - await get(getMockRequest(), toolkit) - - expect(toolkit.view).toHaveBeenCalledWith( - expect.any(String), - expect.objectContaining({ - pageLanguageSetToWelsh: false - }) - ) - }) - - it('returns true when SHOW_WELSH_CONTENT is true and the lang is set to cy', async () => { - process.env.SHOW_WELSH_CONTENT = true - const { get } = pageHandler('', 'view', '/next/page') - const query = { lang: 'cy' } - const request = getMockRequest({ query }) - const toolkit = getMockToolkit() - - await get(request, toolkit) - - expect(toolkit.view).toHaveBeenCalledWith( - expect.any(String), - expect.objectContaining({ - pageLanguageSetToWelsh: true + pageLanguageSetToWelsh: expectedValue }) ) }) @@ -298,7 +269,7 @@ const getAnalytics = overides => ({ ...overides }) -const getMockRequest = ({ setCurrentPermission = () => {}, path = '/buy/we/are/here', query = {}, analytics, set = () => {} } = {}) => ({ +const getMockRequest = ({ setCurrentPermission = () => {}, path = '/buy/we/are/here', analytics, set = () => {} } = {}) => ({ cache: () => ({ helpers: { page: { @@ -324,7 +295,6 @@ const getMockRequest = ({ setCurrentPermission = () => {}, path = '/buy/we/are/h getLocale: () => '' }, path, - query, url: { search: '' } diff --git a/packages/gafl-webapp-service/src/handlers/page-handler.js b/packages/gafl-webapp-service/src/handlers/page-handler.js index 5bf8b70e11..a0f86ce43b 100644 --- a/packages/gafl-webapp-service/src/handlers/page-handler.js +++ b/packages/gafl-webapp-service/src/handlers/page-handler.js @@ -14,6 +14,7 @@ import { import GetDataRedirect from './get-data-redirect.js' import journeyDefinition from '../routes/journey-definition.js' import { addLanguageCodeToUri } from '../processors/uri-helper.js' +import { welshEnabledAndApplied } from '../processors/page-language-helper.js' const pagesToOmitAnalyticsBanner = [AGREED.uri, LICENCE_DETAILS.uri, ORDER_COMPLETE.uri, PAYMENT_CANCELLED.uri, PAYMENT_FAILED.uri] const pagesJourneyBeginning = [LICENCE_FOR.uri, IDENTIFY.uri] @@ -40,11 +41,6 @@ const omitPageFromAnalytics = async request => { } } -const pageLanguageSetToWelsh = request => { - const showWelshContent = process.env.SHOW_WELSH_CONTENT?.toLowerCase() === 'true' - return showWelshContent && request.query.lang === 'cy' -} - /** * Flattens the error structure from joi for use in the templates * @param e @@ -128,7 +124,7 @@ export default (path, view, completion, getData) => ({ pageData.altLang = request.i18n.getLocales().filter(locale => locale !== request.i18n.getLocale()) pageData.backRef = await getBackReference(request, view) pageData.uri = { ...(pageData.uri || {}), analyticsFormAction: addLanguageCodeToUri(request, PROCESS_ANALYTICS_PREFERENCES.uri) } - pageData.pageLanguageSetToWelsh = pageLanguageSetToWelsh(request) + pageData.pageLanguageSetToWelsh = welshEnabledAndApplied(request) const analytics = await request.cache().helpers.analytics.get() pageData.analyticsMessageDisplayed = analytics ? analytics[ANALYTICS.seenMessage] : false diff --git a/packages/gafl-webapp-service/src/processors/__tests__/page-language-helper.spec.js b/packages/gafl-webapp-service/src/processors/__tests__/page-language-helper.spec.js new file mode 100644 index 0000000000..b6407686e3 --- /dev/null +++ b/packages/gafl-webapp-service/src/processors/__tests__/page-language-helper.spec.js @@ -0,0 +1,29 @@ +import { welshEnabledAndApplied } from '../page-language-helper.js' + +describe('welshEnabledAndApplied', () => { + describe('welshEnabledAndApplied', () => { + it('returns false when SHOW_WELSH_CONTENT is not true', async () => { + process.env.SHOW_WELSH_CONTENT = false + const request = { query: { lang: 'cy' } } + + const result = welshEnabledAndApplied(request) + expect(result).toEqual(false) + }) + + it('returns false when SHOW_WELSH_CONTENT is true but the lang is not set to cy', async () => { + process.env.SHOW_WELSH_CONTENT = true + const request = { query: { lang: 'en' } } + + const result = welshEnabledAndApplied(request) + expect(result).toEqual(false) + }) + + it('returns true when SHOW_WELSH_CONTENT is true and the lang is set to cy', async () => { + process.env.SHOW_WELSH_CONTENT = true + const request = { query: { lang: 'cy' } } + + const result = welshEnabledAndApplied(request) + expect(result).toEqual(true) + }) + }) +}) diff --git a/packages/gafl-webapp-service/src/processors/page-language-helper.js b/packages/gafl-webapp-service/src/processors/page-language-helper.js new file mode 100644 index 0000000000..b031c4ac39 --- /dev/null +++ b/packages/gafl-webapp-service/src/processors/page-language-helper.js @@ -0,0 +1,4 @@ +export const welshEnabledAndApplied = request => { + const showWelshContent = process.env.SHOW_WELSH_CONTENT?.toLowerCase() === 'true' + return showWelshContent && request.query.lang === 'cy' +} diff --git a/packages/gafl-webapp-service/src/routes/__tests__/misc-routes-handlers.spec.js b/packages/gafl-webapp-service/src/routes/__tests__/misc-routes-handlers.spec.js index aef9db7358..8ac694baf3 100644 --- a/packages/gafl-webapp-service/src/routes/__tests__/misc-routes-handlers.spec.js +++ b/packages/gafl-webapp-service/src/routes/__tests__/misc-routes-handlers.spec.js @@ -2,6 +2,7 @@ import uri from '../../uri.js' import miscRoutes from '../misc-routes.js' import constants from '../../constants.js' import { addLanguageCodeToUri } from '../../processors/uri-helper.js' +import { welshEnabledAndApplied } from '../../processors/page-language-helper.js' jest.mock('../../uri.js', () => ({ ...jest.requireActual('../../uri.js'), @@ -20,6 +21,7 @@ jest.mock('../../constants.js', () => ({ })) jest.mock('../../processors/uri-helper.js') +jest.mock('../../processors/page-language-helper.js') describe('guidance page handlers', () => { const cookiesPageHandler = miscRoutes.find(r => r.path === uri.COOKIES.uri).handler @@ -215,7 +217,8 @@ describe('guidance page handlers', () => { { pageHandler: accessibilityPageHandler, handlerName: 'Accessibility' }, { pageHandler: privacyPolicyPageHandler, handlerName: 'Privacy policy' }, { pageHandler: refundPolicyPageHandler, handlerName: 'Refund policy' }, - { pageHandler: osTermsPageHandler, handlerName: 'OS Terms' } + { pageHandler: osTermsPageHandler, handlerName: 'OS Terms' }, + { pageHandler: newPricesPageHandler, handlerName: 'New Prices Page Handler' } ])('language code tests for $handlerName page', ({ pageHandler }) => { it('uses addLanguageCodeToUri to get back url', async () => { const toolkit = getMockToolkit() @@ -243,6 +246,22 @@ describe('guidance page handlers', () => { }) ) }) + + it('returns the value of welshEnabledAndApplied for pageLanguageSetToWelsh', async () => { + const toolkit = getMockToolkit() + const request = getMockRequest() + const expectedValue = Symbol('expected') + welshEnabledAndApplied.mockReturnValueOnce(expectedValue) + + await pageHandler(request, toolkit) + + expect(toolkit.view).toHaveBeenCalledWith( + expect.any(String), + expect.objectContaining({ + pageLanguageSetToWelsh: expectedValue + }) + ) + }) }) const getMockRequest = (i18nValues, search = '') => { diff --git a/packages/gafl-webapp-service/src/routes/misc-routes.js b/packages/gafl-webapp-service/src/routes/misc-routes.js index 4f0e12c7bd..03967260d4 100644 --- a/packages/gafl-webapp-service/src/routes/misc-routes.js +++ b/packages/gafl-webapp-service/src/routes/misc-routes.js @@ -24,6 +24,7 @@ import controllerHandler from '../handlers/controller-handler.js' import authenticationHandler from '../handlers/authentication-handler.js' import { addLanguageCodeToUri } from '../processors/uri-helper.js' import analytics from '../handlers/analytics-handler.js' +import { welshEnabledAndApplied } from '../processors/page-language-helper.js' const simpleView = view => ({ method: 'GET', @@ -31,9 +32,11 @@ const simpleView = view => ({ handler: async (request, h) => { const mssgs = request.i18n.getCatalog() const altLang = request.i18n.getLocales().filter(locale => locale !== request.i18n.getLocale()) + const pageLanguageSetToWelsh = welshEnabledAndApplied(request) return h.view(view.page, { mssgs, altLang, + pageLanguageSetToWelsh, uri: { back: addLanguageCodeToUri(request, CONTROLLER.uri) } @@ -92,10 +95,12 @@ export default [ path: COOKIES.uri, handler: async (request, h) => { const altLang = request.i18n.getLocales().filter(locale => locale !== request.i18n.getLocale()) + const pageLanguageSetToWelsh = welshEnabledAndApplied(request) return h.view(COOKIES.page, { altLang, mssgs: request.i18n.getCatalog(), + pageLanguageSetToWelsh, cookie: { csrf: process.env.CSRF_TOKEN_COOKIE_NAME || CSRF_TOKEN_COOKIE_NAME_DEFAULT, sess: process.env.SESSION_COOKIE_NAME || SESSION_COOKIE_NAME_DEFAULT, @@ -113,9 +118,11 @@ export default [ path: NEW_PRICES.uri, handler: async (request, h) => { const altLang = request.i18n.getLocales().filter(locale => locale !== request.i18n.getLocale()) + const pageLanguageSetToWelsh = welshEnabledAndApplied(request) return h.view(NEW_PRICES.page, { altLang, + pageLanguageSetToWelsh, mssgs: request.i18n.getCatalog(), uri: { back: addLanguageCodeToUri(request, CONTROLLER.uri) From f818df3657e35d3ba86d3a71bdf571980170ad0a Mon Sep 17 00:00:00 2001 From: irisfaraway Date: Fri, 29 Sep 2023 12:37:16 +0100 Subject: [PATCH 2/6] Make sure language persists through link in cookie banner --- packages/gafl-webapp-service/src/pages/layout/layout.njk | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gafl-webapp-service/src/pages/layout/layout.njk b/packages/gafl-webapp-service/src/pages/layout/layout.njk index 515e1bab90..8ab18eb601 100644 --- a/packages/gafl-webapp-service/src/pages/layout/layout.njk +++ b/packages/gafl-webapp-service/src/pages/layout/layout.njk @@ -42,7 +42,7 @@ {{ mssgs.analytics_banner_preamble_1 }}

- {{ mssgs.analytics_banner_preamble_2 }} {{ mssgs.cookie_banner_link }} + {{ mssgs.analytics_banner_preamble_2 }} {{ mssgs.cookie_banner_link }}

From 4eb09477377bc96e6b7234140e157ae42a2ea7dd Mon Sep 17 00:00:00 2001 From: irisfaraway Date: Fri, 6 Oct 2023 15:21:06 +0100 Subject: [PATCH 3/6] Remove check on Welsh language feature flag --- .../__tests__/page-language-helper.spec.js | 14 ++------------ .../src/processors/page-language-helper.js | 3 +-- 2 files changed, 3 insertions(+), 14 deletions(-) diff --git a/packages/gafl-webapp-service/src/processors/__tests__/page-language-helper.spec.js b/packages/gafl-webapp-service/src/processors/__tests__/page-language-helper.spec.js index b6407686e3..1a3f820994 100644 --- a/packages/gafl-webapp-service/src/processors/__tests__/page-language-helper.spec.js +++ b/packages/gafl-webapp-service/src/processors/__tests__/page-language-helper.spec.js @@ -2,24 +2,14 @@ import { welshEnabledAndApplied } from '../page-language-helper.js' describe('welshEnabledAndApplied', () => { describe('welshEnabledAndApplied', () => { - it('returns false when SHOW_WELSH_CONTENT is not true', async () => { - process.env.SHOW_WELSH_CONTENT = false - const request = { query: { lang: 'cy' } } - - const result = welshEnabledAndApplied(request) - expect(result).toEqual(false) - }) - - it('returns false when SHOW_WELSH_CONTENT is true but the lang is not set to cy', async () => { - process.env.SHOW_WELSH_CONTENT = true + it('returns false when the lang is not set to cy', async () => { const request = { query: { lang: 'en' } } const result = welshEnabledAndApplied(request) expect(result).toEqual(false) }) - it('returns true when SHOW_WELSH_CONTENT is true and the lang is set to cy', async () => { - process.env.SHOW_WELSH_CONTENT = true + it('returns true when the lang is set to cy', async () => { const request = { query: { lang: 'cy' } } const result = welshEnabledAndApplied(request) diff --git a/packages/gafl-webapp-service/src/processors/page-language-helper.js b/packages/gafl-webapp-service/src/processors/page-language-helper.js index b031c4ac39..6db49cdf2d 100644 --- a/packages/gafl-webapp-service/src/processors/page-language-helper.js +++ b/packages/gafl-webapp-service/src/processors/page-language-helper.js @@ -1,4 +1,3 @@ export const welshEnabledAndApplied = request => { - const showWelshContent = process.env.SHOW_WELSH_CONTENT?.toLowerCase() === 'true' - return showWelshContent && request.query.lang === 'cy' + return request.query.lang === 'cy' } From ad8285903a713db2999b65af460b1378cd6d9ad5 Mon Sep 17 00:00:00 2001 From: irisfaraway Date: Fri, 6 Oct 2023 15:22:53 +0100 Subject: [PATCH 4/6] Fix code smell --- packages/gafl-webapp-service/src/routes/misc-routes.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/gafl-webapp-service/src/routes/misc-routes.js b/packages/gafl-webapp-service/src/routes/misc-routes.js index 03967260d4..3fa37ae8e4 100644 --- a/packages/gafl-webapp-service/src/routes/misc-routes.js +++ b/packages/gafl-webapp-service/src/routes/misc-routes.js @@ -99,8 +99,8 @@ export default [ return h.view(COOKIES.page, { altLang, - mssgs: request.i18n.getCatalog(), pageLanguageSetToWelsh, + mssgs: request.i18n.getCatalog(), cookie: { csrf: process.env.CSRF_TOKEN_COOKIE_NAME || CSRF_TOKEN_COOKIE_NAME_DEFAULT, sess: process.env.SESSION_COOKIE_NAME || SESSION_COOKIE_NAME_DEFAULT, From c9f1862e7f0b559e81d8990f290c7e3d5f2dddb4 Mon Sep 17 00:00:00 2001 From: irisfaraway Date: Fri, 6 Oct 2023 16:07:04 +0100 Subject: [PATCH 5/6] Make sure snapshot has no undefined values Had to mock the return in the individual tests here due to mocks being reset before each test in this file. --- .../__tests__/__snapshots__/page-handler.spec.js.snap | 4 ++-- .../src/handlers/__tests__/page-handler.spec.js | 2 ++ 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/gafl-webapp-service/src/handlers/__tests__/__snapshots__/page-handler.spec.js.snap b/packages/gafl-webapp-service/src/handlers/__tests__/__snapshots__/page-handler.spec.js.snap index a0a8b3fac6..6b58fda164 100644 --- a/packages/gafl-webapp-service/src/handlers/__tests__/__snapshots__/page-handler.spec.js.snap +++ b/packages/gafl-webapp-service/src/handlers/__tests__/__snapshots__/page-handler.spec.js.snap @@ -13,7 +13,7 @@ exports[`The page handler function sets the value of pageData with displayAnalyt "backRef": null, "displayAnalytics": false, "mssgs": undefined, - "pageLanguageSetToWelsh": undefined, + "pageLanguageSetToWelsh": false, "uri": Object { "analyticsFormAction": "/buy/process-analytics-preferences", }, @@ -42,7 +42,7 @@ exports[`The page handler function sets the value of pageData with displayAnalyt "backRef": null, "displayAnalytics": true, "mssgs": undefined, - "pageLanguageSetToWelsh": undefined, + "pageLanguageSetToWelsh": false, "uri": Object { "analyticsFormAction": "/buy/process-analytics-preferences", }, diff --git a/packages/gafl-webapp-service/src/handlers/__tests__/page-handler.spec.js b/packages/gafl-webapp-service/src/handlers/__tests__/page-handler.spec.js index 262ead51b1..7411943aae 100644 --- a/packages/gafl-webapp-service/src/handlers/__tests__/page-handler.spec.js +++ b/packages/gafl-webapp-service/src/handlers/__tests__/page-handler.spec.js @@ -140,6 +140,7 @@ describe('The page handler function', () => { it('sets the value of pageData with displayAnalytics true', async () => { addLanguageCodeToUri.mockReturnValueOnce('/buy/process-analytics-preferences') + welshEnabledAndApplied.mockReturnValueOnce(false) const { get } = pageHandler('', 'view', '/next/page') const toolkit = getMockToolkit() await get(getMockRequest(), toolkit) @@ -148,6 +149,7 @@ describe('The page handler function', () => { it('sets the value of pageData with displayAnalytics false', async () => { addLanguageCodeToUri.mockReturnValueOnce('/buy/process-analytics-preferences') + welshEnabledAndApplied.mockReturnValueOnce(false) const { get } = pageHandler('', 'view', '/next/page') const toolkit = getMockToolkit() await get(getMockRequest({ path: '/we/are/here' }), toolkit) From 87d076d3e6a6ccf47a3a43530c4e147861cf67d4 Mon Sep 17 00:00:00 2001 From: irisfaraway Date: Fri, 6 Oct 2023 16:17:53 +0100 Subject: [PATCH 6/6] Condense one-line function --- .../src/processors/page-language-helper.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/packages/gafl-webapp-service/src/processors/page-language-helper.js b/packages/gafl-webapp-service/src/processors/page-language-helper.js index 6db49cdf2d..2b7830d05d 100644 --- a/packages/gafl-webapp-service/src/processors/page-language-helper.js +++ b/packages/gafl-webapp-service/src/processors/page-language-helper.js @@ -1,3 +1 @@ -export const welshEnabledAndApplied = request => { - return request.query.lang === 'cy' -} +export const welshEnabledAndApplied = request => request.query.lang === 'cy'