Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Apply correct html lang to static pages also #1804

Merged
merged 6 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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,
jaucourt marked this conversation as resolved.
Show resolved Hide resolved
"uri": Object {
"analyticsFormAction": "/buy/process-analytics-preferences",
},
Expand Down Expand Up @@ -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",
},
Expand Down
Original file line number Diff line number Diff line change
@@ -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: {
Expand Down Expand Up @@ -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()

Expand All @@ -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
})
)
})
Expand All @@ -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: {
Expand All @@ -324,7 +295,6 @@ const getMockRequest = ({ setCurrentPermission = () => {}, path = '/buy/we/are/h
getLocale: () => ''
},
path,
query,
url: {
search: ''
}
Expand Down
8 changes: 2 additions & 6 deletions packages/gafl-webapp-service/src/handlers/page-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/gafl-webapp-service/src/pages/layout/layout.njk
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
{{ mssgs.analytics_banner_preamble_1 }}
</p>
<p class="govuk-body">
{{ mssgs.analytics_banner_preamble_2 }} <a class="govuk-link" href="/guidance/cookies">{{ mssgs.cookie_banner_link }}</a>
{{ mssgs.analytics_banner_preamble_2 }} <a class="govuk-link" href="{{ _uri.cookies }}">{{ mssgs.cookie_banner_link }}</a>
</p>
</div>
<div class="govuk-button-group">
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
})
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const welshEnabledAndApplied = request => {
const showWelshContent = process.env.SHOW_WELSH_CONTENT?.toLowerCase() === 'true'
irisfaraway marked this conversation as resolved.
Show resolved Hide resolved
return showWelshContent && request.query.lang === 'cy'
}
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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 = '') => {
Expand Down
7 changes: 7 additions & 0 deletions packages/gafl-webapp-service/src/routes/misc-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@ 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',
path: view.uri,
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)
}
Expand Down Expand Up @@ -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,
Expand All @@ -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)
Expand Down
Loading