From 7c401c49e2ab816a20002a7e2dd8ed7331272760 Mon Sep 17 00:00:00 2001 From: "simone.amadio" Date: Mon, 6 Feb 2023 17:10:06 +0100 Subject: [PATCH] fix: rinominazione metodi di utils --- .../municipality/bookingAppointmentAudit.ts | 4 +- .../bootstrapItaliaDoubleCheckAudit.ts | 12 +- .../municipality/contactsAssistencyAudit.ts | 4 +- .../municipality/cookieDomainCheckAudit.ts | 14 +- src/audits/municipality/fontsCheckAudit.ts | 4 +- src/audits/municipality/metatagAudit.ts | 4 +- src/audits/municipality/serviziAudit.ts | 4 +- .../school/bootstrapItaliaDoubleCheckAudit.ts | 12 +- src/audits/school/cookieDomainCheckAudit.ts | 16 +-- src/audits/school/fontsCheckAudit.ts | 4 +- src/audits/school/serviziAudit.ts | 4 +- src/utils/municipality/utils.ts | 12 +- src/utils/school/utils.ts | 128 +++++++++--------- 13 files changed, 111 insertions(+), 111 deletions(-) diff --git a/src/audits/municipality/bookingAppointmentAudit.ts b/src/audits/municipality/bookingAppointmentAudit.ts index ee097b83..c1db51bc 100644 --- a/src/audits/municipality/bookingAppointmentAudit.ts +++ b/src/audits/municipality/bookingAppointmentAudit.ts @@ -4,7 +4,7 @@ import lighthouse from "lighthouse"; import { loadPageData } from "../../utils/utils"; import { - getRandomMunicipalityThirdLevelPagesUrl, + getRandomThirdLevelPagesUrl, getServicePageUrl, } from "../../utils/municipality/utils"; import { auditDictionary } from "../../storage/auditDictionary"; @@ -49,7 +49,7 @@ class LoadAudit extends Audit { ]; const randomServices: string[] = - await getRandomMunicipalityThirdLevelPagesUrl( + await getRandomThirdLevelPagesUrl( url, await getServicePageUrl(url), '[data-element="service-link"]' diff --git a/src/audits/municipality/bootstrapItaliaDoubleCheckAudit.ts b/src/audits/municipality/bootstrapItaliaDoubleCheckAudit.ts index 20891e72..db51e356 100644 --- a/src/audits/municipality/bootstrapItaliaDoubleCheckAudit.ts +++ b/src/audits/municipality/bootstrapItaliaDoubleCheckAudit.ts @@ -12,9 +12,9 @@ import { loadPageData, } from "../../utils/utils"; import { - getRandomMunicipalityFirstLevelPagesUrl, - getRandomMunicipalitySecondLevelPagesUrl, - getRandomMunicipalityThirdLevelPagesUrl, + getRandomFirstLevelPagesUrl, + getRandomSecondLevelPagesUrl, + getRandomThirdLevelPagesUrl, getServicePageUrl, } from "../../utils/municipality/utils"; import { auditScanVariables } from "../../storage/municipality/auditScanVariables"; @@ -93,15 +93,15 @@ class LoadAudit extends Audit { const pagesToBeAnalyzed = [ url, - ...(await getRandomMunicipalityFirstLevelPagesUrl( + ...(await getRandomFirstLevelPagesUrl( url, auditVariables.numberOfFirstLevelPageToBeScanned )), - ...(await getRandomMunicipalitySecondLevelPagesUrl( + ...(await getRandomSecondLevelPagesUrl( url, auditVariables.numberOfSecondLevelPageToBeScanned )), - ...(await getRandomMunicipalityThirdLevelPagesUrl( + ...(await getRandomThirdLevelPagesUrl( url, await getServicePageUrl(url), '[data-element="service-link"]', diff --git a/src/audits/municipality/contactsAssistencyAudit.ts b/src/audits/municipality/contactsAssistencyAudit.ts index 693c5c7d..38ad16f6 100644 --- a/src/audits/municipality/contactsAssistencyAudit.ts +++ b/src/audits/municipality/contactsAssistencyAudit.ts @@ -4,7 +4,7 @@ import lighthouse from "lighthouse"; import { getPageElementDataAttribute, loadPageData } from "../../utils/utils"; import { - getRandomMunicipalityThirdLevelPagesUrl, + getRandomThirdLevelPagesUrl, getServicePageUrl, } from "../../utils/municipality/utils"; import { CheerioAPI } from "cheerio"; @@ -60,7 +60,7 @@ class LoadAudit extends Audit { ]; const randomServices: string[] = - await getRandomMunicipalityThirdLevelPagesUrl( + await getRandomThirdLevelPagesUrl( url, await getServicePageUrl(url), '[data-element="service-link"]' diff --git a/src/audits/municipality/cookieDomainCheckAudit.ts b/src/audits/municipality/cookieDomainCheckAudit.ts index 84f0f602..b4b78596 100644 --- a/src/audits/municipality/cookieDomainCheckAudit.ts +++ b/src/audits/municipality/cookieDomainCheckAudit.ts @@ -8,9 +8,9 @@ import { run as cookieAudit } from "../../utils/cookieAuditLogic"; import crawlerTypes from "../../types/crawler-types"; import cookie = crawlerTypes.cookie; import { - getRandomMunicipalityFirstLevelPagesUrl, - getRandomMunicipalitySecondLevelPagesUrl, - getRandomMunicipalityThirdLevelPagesUrl, + getRandomFirstLevelPagesUrl, + getRandomSecondLevelPagesUrl, + getRandomThirdLevelPagesUrl, getServicePageUrl, } from "../../utils/municipality/utils"; import { auditScanVariables } from "../../storage/municipality/auditScanVariables"; @@ -76,21 +76,21 @@ class LoadAudit extends Audit { const pagesToBeAnalyzed = [ url, - ...(await getRandomMunicipalityFirstLevelPagesUrl( + ...(await getRandomFirstLevelPagesUrl( url, auditVariables.numberOfFirstLevelPageToBeScanned )), - ...(await getRandomMunicipalitySecondLevelPagesUrl( + ...(await getRandomSecondLevelPagesUrl( url, auditVariables.numberOfSecondLevelPageToBeScanned )), - ...(await getRandomMunicipalityThirdLevelPagesUrl( + ...(await getRandomThirdLevelPagesUrl( url, await getServicePageUrl(url), '[data-element="service-link"]', auditVariables.numberOfServicesToBeScanned )), - ...(await getRandomMunicipalityThirdLevelPagesUrl( + ...(await getRandomThirdLevelPagesUrl( url, await getButtonUrl( await loadPageData(url), diff --git a/src/audits/municipality/fontsCheckAudit.ts b/src/audits/municipality/fontsCheckAudit.ts index 846531ef..327d98c5 100644 --- a/src/audits/municipality/fontsCheckAudit.ts +++ b/src/audits/municipality/fontsCheckAudit.ts @@ -4,7 +4,7 @@ import lighthouse from "lighthouse"; import { allowedFonts } from "../../storage/municipality/allowedFonts"; import { - getRandomMunicipalityThirdLevelPagesUrl, + getRandomThirdLevelPagesUrl, getServicePageUrl, } from "../../utils/municipality/utils"; import puppeteer from "puppeteer"; @@ -53,7 +53,7 @@ class LoadAudit extends Audit { const url = artifacts.origin; const randomServices: string[] = - await getRandomMunicipalityThirdLevelPagesUrl( + await getRandomThirdLevelPagesUrl( url, await getServicePageUrl(url), '[data-element="service-link"]' diff --git a/src/audits/municipality/metatagAudit.ts b/src/audits/municipality/metatagAudit.ts index b80e52e5..2923ba81 100644 --- a/src/audits/municipality/metatagAudit.ts +++ b/src/audits/municipality/metatagAudit.ts @@ -4,7 +4,7 @@ import lighthouse from "lighthouse"; import { loadPageData } from "../../utils/utils"; import { - getRandomMunicipalityThirdLevelPagesUrl, + getRandomThirdLevelPagesUrl, getServicePageUrl, } from "../../utils/municipality/utils"; import { CheerioAPI } from "cheerio"; @@ -65,7 +65,7 @@ class LoadAudit extends Audit { ]; const randomServices: string[] = - await getRandomMunicipalityThirdLevelPagesUrl( + await getRandomThirdLevelPagesUrl( url, await getServicePageUrl(url), '[data-element="service-link"]' diff --git a/src/audits/municipality/serviziAudit.ts b/src/audits/municipality/serviziAudit.ts index ebdffc3c..61185f45 100644 --- a/src/audits/municipality/serviziAudit.ts +++ b/src/audits/municipality/serviziAudit.ts @@ -13,7 +13,7 @@ import { toMenuItem, } from "../../utils/utils"; import { - getRandomMunicipalityThirdLevelPagesUrl, + getRandomThirdLevelPagesUrl, getServicePageUrl, } from "../../utils/municipality/utils"; import { @@ -89,7 +89,7 @@ class LoadAudit extends Audit { const mandatoryBodyVoices = contentTypeItemsBody; const randomServices: string[] = - await getRandomMunicipalityThirdLevelPagesUrl( + await getRandomThirdLevelPagesUrl( url, await getServicePageUrl(url), '[data-element="service-link"]', diff --git a/src/audits/school/bootstrapItaliaDoubleCheckAudit.ts b/src/audits/school/bootstrapItaliaDoubleCheckAudit.ts index bef1664c..4d285623 100644 --- a/src/audits/school/bootstrapItaliaDoubleCheckAudit.ts +++ b/src/audits/school/bootstrapItaliaDoubleCheckAudit.ts @@ -10,9 +10,9 @@ const Audit = lighthouse.Audit; import { checkCSSClassesOnPage } from "../../utils/utils"; import { - getRandomSchoolFirstLevelPagesUrl, - getRandomSchoolSecondLevelPagesUrl, - getRandomSchoolServicesUrl, + getRandomFirstLevelPagesUrl, + getRandomSecondLevelPagesUrl, + getRandomServicesUrl, } from "../../utils/school/utils"; import { auditScanVariables } from "../../storage/school/auditScanVariables"; import { cssClasses } from "../../storage/school/cssClasses"; @@ -87,15 +87,15 @@ class LoadAudit extends Audit { const pagesToBeAnalyzed = [ url, - ...(await getRandomSchoolFirstLevelPagesUrl( + ...(await getRandomFirstLevelPagesUrl( url, auditVariables.numberOfFirstLevelPageToBeScanned )), - ...(await getRandomSchoolSecondLevelPagesUrl( + ...(await getRandomSecondLevelPagesUrl( url, auditVariables.numberOfSecondLevelPageToBeScanned )), - ...(await getRandomSchoolServicesUrl( + ...(await getRandomServicesUrl( url, auditVariables.numberOfServicesToBeScanned )), diff --git a/src/audits/school/cookieDomainCheckAudit.ts b/src/audits/school/cookieDomainCheckAudit.ts index 9e381118..52bf554a 100644 --- a/src/audits/school/cookieDomainCheckAudit.ts +++ b/src/audits/school/cookieDomainCheckAudit.ts @@ -4,10 +4,10 @@ // @ts-ignore import lighthouse from "lighthouse"; import { - getRandomSchoolFirstLevelPagesUrl, - getRandomSchoolSecondLevelPagesUrl, - getRandomSchoolServicesUrl, - getRandomSchoolLocationsUrl, + getRandomFirstLevelPagesUrl, + getRandomSecondLevelPagesUrl, + getRandomServicesUrl, + getRandomLocationsUrl, } from "../../utils/school/utils"; import crawlerTypes from "../../types/crawler-types"; import cookie = crawlerTypes.cookie; @@ -73,19 +73,19 @@ class LoadAudit extends Audit { const pagesToBeAnalyzed = [ url, - ...(await getRandomSchoolFirstLevelPagesUrl( + ...(await getRandomFirstLevelPagesUrl( url, auditVariables.numberOfFirstLevelPageToBeScanned )), - ...(await getRandomSchoolSecondLevelPagesUrl( + ...(await getRandomSecondLevelPagesUrl( url, auditVariables.numberOfSecondLevelPageToBeScanned )), - ...(await getRandomSchoolServicesUrl( + ...(await getRandomServicesUrl( url, auditVariables.numberOfServicesToBeScanned )), - ...(await getRandomSchoolLocationsUrl( + ...(await getRandomLocationsUrl( url, auditVariables.numberOfLocationsToBeScanned )), diff --git a/src/audits/school/fontsCheckAudit.ts b/src/audits/school/fontsCheckAudit.ts index c839670a..0c24bd1f 100644 --- a/src/audits/school/fontsCheckAudit.ts +++ b/src/audits/school/fontsCheckAudit.ts @@ -3,7 +3,7 @@ // @ts-ignore import lighthouse from "lighthouse"; import { allowedFonts } from "../../storage/school/allowedFonts"; -import { getRandomSchoolServicesUrl } from "../../utils/school/utils"; +import { getRandomServicesUrl } from "../../utils/school/utils"; import puppeteer from "puppeteer"; import { auditDictionary } from "../../storage/auditDictionary"; @@ -49,7 +49,7 @@ class LoadAudit extends Audit { ): Promise { const url = artifacts.origin; - const randomService: string[] = await getRandomSchoolServicesUrl(url); + const randomService: string[] = await getRandomServicesUrl(url); if (randomService.length === 0) { return { diff --git a/src/audits/school/serviziAudit.ts b/src/audits/school/serviziAudit.ts index 81411114..78df4632 100644 --- a/src/audits/school/serviziAudit.ts +++ b/src/audits/school/serviziAudit.ts @@ -12,7 +12,7 @@ import { missingMenuItems, toMenuItem, } from "../../utils/utils"; -import { getRandomSchoolServicesUrl } from "../../utils/school/utils"; +import { getRandomServicesUrl } from "../../utils/school/utils"; import { contentTypeItemsBody, contentTypeItemsHeaders, @@ -96,7 +96,7 @@ class LoadAudit extends Audit { const mandatoryMetadata = contentTypeItemsMetadata; - const randomServices: string[] = await getRandomSchoolServicesUrl( + const randomServices: string[] = await getRandomServicesUrl( url, auditVariables.numberOfServicesToBeScanned ); diff --git a/src/utils/municipality/utils.ts b/src/utils/municipality/utils.ts index 0bb03a47..8d489a77 100644 --- a/src/utils/municipality/utils.ts +++ b/src/utils/municipality/utils.ts @@ -9,7 +9,7 @@ import { loadPageData, } from "../utils"; -const getRandomMunicipalityFirstLevelPagesUrl = async ( +const getRandomFirstLevelPagesUrl = async ( url: string, numberOfPages = 1 ): Promise => { @@ -38,7 +38,7 @@ const getRandomMunicipalityFirstLevelPagesUrl = async ( return getRandomNString(pagesUrls, numberOfPages); }; -const getRandomMunicipalitySecondLevelPagesUrl = async ( +const getRandomSecondLevelPagesUrl = async ( url: string, numberOfPages = 1 ): Promise => { @@ -80,7 +80,7 @@ const getRandomMunicipalitySecondLevelPagesUrl = async ( return getRandomNString(pagesUrls, numberOfPages); }; -const getRandomMunicipalityThirdLevelPagesUrl = async ( +const getRandomThirdLevelPagesUrl = async ( url: string, pageUrl: string, linkDataElement: string, @@ -156,8 +156,8 @@ const getServicePageUrl = async (url: string) => { }; export { - getRandomMunicipalityFirstLevelPagesUrl, - getRandomMunicipalitySecondLevelPagesUrl, - getRandomMunicipalityThirdLevelPagesUrl, + getRandomFirstLevelPagesUrl, + getRandomSecondLevelPagesUrl, + getRandomThirdLevelPagesUrl, getServicePageUrl, }; diff --git a/src/utils/school/utils.ts b/src/utils/school/utils.ts index 77f5f788..3bc3c143 100644 --- a/src/utils/school/utils.ts +++ b/src/utils/school/utils.ts @@ -7,15 +7,67 @@ import { loadPageData, } from "../utils"; -const getRandomSchoolServicesUrl = async ( +const getRandomFirstLevelPagesUrl = async ( url: string, - numberOfServices = 1 + numberOfPages = 1 +): Promise => { + const $ = await loadPageData(url); + + const pagesUrls = [ + ...new Set( + await getHREFValuesDataAttribute($, '[data-element="overview"]') + ), + ]; + + for (let i = 0; i < pagesUrls.length; i++) { + if (!pagesUrls[i].includes(url)) { + pagesUrls[i] = await buildUrl(url, pagesUrls[i]); + } + } + + return getRandomNString(pagesUrls, numberOfPages); +}; + +const getRandomSecondLevelPagesUrl = async ( + url: string, + numberOfPages = 1 +): Promise => { + const pagesUrls = []; + const $ = await loadPageData(url); + + for (const [, value] of Object.entries(menuItems)) { + const dataElement = `[data-element="${value.data_element}"]`; + + let elements = $(dataElement); + if (Object.keys(elements).length > 0) { + elements = elements.find("li > a"); + for (const element of elements) { + let secondLevelPageUrl = $(element).attr()?.href; + if ( + secondLevelPageUrl && + secondLevelPageUrl !== "#" && + secondLevelPageUrl !== "" + ) { + if (!secondLevelPageUrl.includes(url)) { + secondLevelPageUrl = await buildUrl(url, secondLevelPageUrl); + } + pagesUrls.push(secondLevelPageUrl); + } + } + } + } + return getRandomNString(pagesUrls, numberOfPages); +}; + +const getRandomServicesUrl = async ( + url: string, + numberOfServices = 1 ): Promise => { let $ = await loadPageData(url); let serviceTypeUrls = await getHREFValuesDataAttribute( - $, - '[data-element="service-type"]' + $, + '[data-element="service-type"]' ); if (serviceTypeUrls.length <= 0) { return []; @@ -37,7 +89,7 @@ const getRandomSchoolServicesUrl = async ( const pagerPagesUrls = [ ...new Set( - await getHREFValuesDataAttribute($, '[data-element="pager-link"]') + await getHREFValuesDataAttribute($, '[data-element="pager-link"]') ), ]; for (let pagerPageUrl of pagerPagesUrls) { @@ -50,8 +102,8 @@ const getRandomSchoolServicesUrl = async ( servicesUrls = [ ...servicesUrls, ...(await getHREFValuesDataAttribute( - $, - '[data-element="service-link"]' + $, + '[data-element="service-link"]' )), ]; } @@ -67,59 +119,7 @@ const getRandomSchoolServicesUrl = async ( return getRandomNString(servicesUrls, numberOfServices); }; -const getRandomSchoolFirstLevelPagesUrl = async ( - url: string, - numberOfPages = 1 -): Promise => { - const $ = await loadPageData(url); - - const pagesUrls = [ - ...new Set( - await getHREFValuesDataAttribute($, '[data-element="overview"]') - ), - ]; - - for (let i = 0; i < pagesUrls.length; i++) { - if (!pagesUrls[i].includes(url)) { - pagesUrls[i] = await buildUrl(url, pagesUrls[i]); - } - } - - return getRandomNString(pagesUrls, numberOfPages); -}; - -const getRandomSchoolSecondLevelPagesUrl = async ( - url: string, - numberOfPages = 1 -): Promise => { - const pagesUrls = []; - const $ = await loadPageData(url); - - for (const [, value] of Object.entries(menuItems)) { - const dataElement = `[data-element="${value.data_element}"]`; - - let elements = $(dataElement); - if (Object.keys(elements).length > 0) { - elements = elements.find("li > a"); - for (const element of elements) { - let secondLevelPageUrl = $(element).attr()?.href; - if ( - secondLevelPageUrl && - secondLevelPageUrl !== "#" && - secondLevelPageUrl !== "" - ) { - if (!secondLevelPageUrl.includes(url)) { - secondLevelPageUrl = await buildUrl(url, secondLevelPageUrl); - } - pagesUrls.push(secondLevelPageUrl); - } - } - } - } - return getRandomNString(pagesUrls, numberOfPages); -}; - -const getRandomSchoolLocationsUrl = async ( +const getRandomLocationsUrl = async ( url: string, numberOfPages = 1 ): Promise => { @@ -157,8 +157,8 @@ const getRandomSchoolLocationsUrl = async ( }; export { - getRandomSchoolServicesUrl, - getRandomSchoolFirstLevelPagesUrl, - getRandomSchoolSecondLevelPagesUrl, - getRandomSchoolLocationsUrl, + getRandomServicesUrl, + getRandomFirstLevelPagesUrl, + getRandomSecondLevelPagesUrl, + getRandomLocationsUrl, };