From 11b905b46462f8b0a7df3de43553886b00632740 Mon Sep 17 00:00:00 2001 From: Blaize Kaye Date: Mon, 17 Aug 2020 14:09:41 +1200 Subject: [PATCH] Adds PSI audit --- Policy/pagespeedinsightsscore.policy.yml | 15 ++++++ Profiles/ALGMPerformance.profile.yml | 6 +++ phpunit.xml | 1 - src/Audit/PageSpeedInsightsScore.php | 61 ++++++++++++++++++++++++ tests/src/PageSpeedInsightsScore.php | 35 ++++++++++++++ 5 files changed, 117 insertions(+), 1 deletion(-) create mode 100644 Policy/pagespeedinsightsscore.policy.yml create mode 100644 Profiles/ALGMPerformance.profile.yml create mode 100644 src/Audit/PageSpeedInsightsScore.php create mode 100644 tests/src/PageSpeedInsightsScore.php diff --git a/Policy/pagespeedinsightsscore.policy.yml b/Policy/pagespeedinsightsscore.policy.yml new file mode 100644 index 0000000..82bee42 --- /dev/null +++ b/Policy/pagespeedinsightsscore.policy.yml @@ -0,0 +1,15 @@ +title: "Page Speed Insights policy" +class: \Drutiny\algm\Audit\PageSpeedInsightsScore +name: ALGMPerformance:PSI +description: | + Runs PSI against LAGOON_ROUTE +success: | + Success: All PageSpeed Insights metrics are nominal + Performance {{ report.performance }} + SEO {{ report.seo }} + Accessibility {{ report.accessibility }} +failure: | + Warning: PageSpeed Insights Minimums Not Met + Performance {{ report.performance }} + SEO {{ report.seo }} + Accessibility {{ report.accessibility }} diff --git a/Profiles/ALGMPerformance.profile.yml b/Profiles/ALGMPerformance.profile.yml new file mode 100644 index 0000000..9f9684e --- /dev/null +++ b/Profiles/ALGMPerformance.profile.yml @@ -0,0 +1,6 @@ +title: 'ALGM Performance' +description: 'Site Performance Audit for ALGM.' +policies: + 'ALGMPerformance:PSI': { } +format: + html: { template: page, content: [{ heading: Purpose, body: "This report is designed to provide some feedback on the overall health of\nthe web application by performing some deep dive analysis. The items\nsurfaced in the report can help improve performance and stability.\n" }, { heading: 'Reporting period', body: "Period | Date time\n------ | ---------\nStart | {{reporting_period_start}}\nEnd | {{reporting_period_end}}\n" }, { heading: Recommendations, body: "\n" }, { heading: Findings, body: "{{{ severity_stats }}}\n### Issue Summary\n{{{ summary_table }}}\n\n{{#failures}}\n ### Issues\n {{# output_failure }}\n {{{.}}}\n {{/ output_failure }}\n{{/failures}}\n\n{{#warnings}}\n ### Warnings\n {{# output_warning }}\n {{{.}}}\n {{/ output_warning }}\n{{/warnings}}\n" }, { heading: Appendix, body: "{{#notices}}\n ### Appendix - Analysis\n\n The various appendices provides more detailed data regarding the health of\n the site.\n\n {{# output_notice }}\n {{{.}}}\n {{/ output_notice }}\n{{/notices}}\n\n{{#errors}}\n ### Appendix - Errors\n\n During the production of this report, not all report policies were able to\n be carried out due to errors encounted.\n\n {{#output_error}}\n {{{.}}}\n {{/output_error}}\n{{/errors}}\n\n### Appendix - Summary\nThe below table describes all audit and analysis work completed for the\nproduction of this report and their associated outcomes.\n\n{{{ appendix_table }}}\n\n{{#passes}}\n### Appendix - Successful Assessments\n{{# output_success }}\n {{{.}}}\n{{/ output_success }}\n{{/passes}}\n" }] } diff --git a/phpunit.xml b/phpunit.xml index 6985881..ffe8735 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -12,7 +12,6 @@ -s tests diff --git a/src/Audit/PageSpeedInsightsScore.php b/src/Audit/PageSpeedInsightsScore.php new file mode 100644 index 0000000..62a078d --- /dev/null +++ b/src/Audit/PageSpeedInsightsScore.php @@ -0,0 +1,61 @@ + 'lighthouseResult/categories/*/score', + 'strategy' => 'desktop', + 'url' => $route, + ]; + + $categories = '&category=SEO&category=PERFORMANCE&category=ACCESSIBILITY'; + $params = http_build_query($queryParams) . $categories; + $response = file_get_contents(self::PSI_API_ENDPOINT . '?' . $params); + $responseObj = json_decode($response, TRUE); + + $performanceScore = $responseObj['lighthouseResult']['categories']['performance']['score']; + $accessibilityScore = $responseObj['lighthouseResult']['categories']['accessibility']['score']; + $seoScore = $responseObj['lighthouseResult']['categories']['seo']['score']; + + $sandbox->setParameter('report', [ + 'performance' => $performanceScore, + 'seo' => $seoScore, + 'accessibility' => $accessibilityScore, + ]); + + if ($performanceScore < self::THRESHOLD_PERFORMANCE || + $accessibilityScore < self::THRESHOLD_ACCESSIBILITY || + $seoScore < self::THRESHOLD_SEO + ) { + return Audit::FAIL; + } + + return Audit::PASS; + } +} \ No newline at end of file diff --git a/tests/src/PageSpeedInsightsScore.php b/tests/src/PageSpeedInsightsScore.php new file mode 100644 index 0000000..80a9047 --- /dev/null +++ b/tests/src/PageSpeedInsightsScore.php @@ -0,0 +1,35 @@ +target = TargetRegistry::getTarget('none', ''); + parent::__construct(); + } + + /** @test */ + public function it_should_run_a_psi_scan_against_a_site() { + $policy = Policy::load('ALGMPerformance:PSI'); + $sandbox = new Sandbox($this->target, $policy); + + $response = $sandbox->run(); + var_dump($response->isSuccessful()); + var_dump($response->getFailure()); + $this->assertTrue($response->isSuccessful()); + } + +}