Skip to content

Commit

Permalink
Adds PSI audit
Browse files Browse the repository at this point in the history
  • Loading branch information
bomoko committed Aug 17, 2020
1 parent 532ab38 commit 11b905b
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 1 deletion.
15 changes: 15 additions & 0 deletions Policy/pagespeedinsightsscore.policy.yml
Original file line number Diff line number Diff line change
@@ -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 }}
6 changes: 6 additions & 0 deletions Profiles/ALGMPerformance.profile.yml
Original file line number Diff line number Diff line change
@@ -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: "<ul>\n{{# remediations }}\n <li>{{{ . }}}</li>\n{{/ remediations }}\n</ul>\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" }] }
1 change: 0 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<!-- Do not limit the amount of memory tests take to run. -->
<ini name="memory_limit" value="-1"/>
</php>
s
<testsuites>
<testsuite name="checks">
<directory>tests</directory>
Expand Down
61 changes: 61 additions & 0 deletions src/Audit/PageSpeedInsightsScore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Drutiny\algm\Audit;

use Drutiny\Audit;
use Drutiny\Sandbox\Sandbox;

/**
*
*/
class PageSpeedInsightsScore extends Audit {

const THRESHOLD_PERFORMANCE = 0.5;

const THRESHOLD_ACCESSIBILITY = 0.5;

const THRESHOLD_SEO = 0.5;

const PSI_API_ENDPOINT = 'https://www.googleapis.com/pagespeedonline/v5/runPagespeed';

/**
* @inheritdoc
*/
public function audit(Sandbox $sandbox) {

$route = getenv("LAGOON_ROUTE");
if (empty($route)) {
return AUDIT::ERROR;
}

$queryParams = [
'fields' => '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;
}
}
35 changes: 35 additions & 0 deletions tests/src/PageSpeedInsightsScore.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace DrutinyTests\Audit;

use Drutiny\algm\Audit\ClamAVScan;
use Drutiny\Container;
use Drutiny\Policy;
use Drutiny\Sandbox\Sandbox;
use Drutiny\Target\Registry as TargetRegistry;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;

class PageSpeedInsightsScore extends TestCase {

protected $target;

public function __construct()
{
Container::setLogger(new NullLogger());
$this->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());
}

}

0 comments on commit 11b905b

Please sign in to comment.