Skip to content

Commit

Permalink
Check on width and height attribute when running tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Baspa committed Aug 17, 2023
1 parent 082aa08 commit 62afb7c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
28 changes: 24 additions & 4 deletions src/Checks/Content/AltTagCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ public function validateContent(Crawler $crawler): bool
{
$imagesWithoutAlt = $crawler->filterXPath('//img[not(@alt)]')->each(function (Crawler $node, $i) {
$src = $node->attr('src');
$dimensions = getimagesize($src);

if ($dimensions[0] < 5 || $dimensions[1] < 5) {
$dimensions = $this->getImageDimensions($src, $node);

if ($dimensions['width'] < 5 || $dimensions['height'] < 5) {
return null;
}

Expand All @@ -51,9 +52,11 @@ public function validateContent(Crawler $crawler): bool

$imagesWithEmptyAlt = $crawler->filterXPath('//img[@alt=""]')->each(function (Crawler $node, $i) {
$src = $node->attr('src');
$dimensions = getimagesize($src);

$dimensions = $this->getImageDimensions($src, $node);


if ($dimensions[0] < 5 || $dimensions[1] < 5) {
if ($dimensions['width'] < 5 || $dimensions['height'] < 5) {
return null;
}

Expand All @@ -78,4 +81,21 @@ public function validateContent(Crawler $crawler): bool

return true;
}

public function getImageDimensions(string $src, Crawler $node): array
{
if (app()->runningUnitTests()) {
return [
'width' => $node->attr('width'),
'height' => $node->attr('height'),
];
}

$dimensions = getimagesize($src);

return [
'width' => $dimensions[0],
'height' => $dimensions[1],
];
}
}
8 changes: 4 additions & 4 deletions tests/Checks/Content/AltTagCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
$crawler = new Crawler();

Http::fake([
'vormkracht10.nl' => Http::response('<html><head></head><body><img src="https://vormkracht10.nl/images/logo.png" alt="Vormkracht10 logo"></body></html>', 200),
'vormkracht10.nl' => Http::response('<html><head></head><body><img src="https://vormkracht10.nl/images/logo.png" width="5" height="5" alt="Vormkracht10 logo"></body></html>', 200),
]);

$crawler->addHtmlContent(Http::get('vormkracht10.nl')->body());
Expand All @@ -22,7 +22,7 @@
$crawler = new Crawler();

Http::fake([
'vormkracht10.nl' => Http::response('<html><head></head><body><img src="https://vormkracht10.nl/images/logo.png"></body></html>', 200),
'vormkracht10.nl' => Http::response('<html><head></head><body><img src="https://vormkracht10.nl/images/logo.png" width="5" height="5"></body></html>', 200),
]);

$crawler->addHtmlContent(Http::get('vormkracht10.nl')->body());
Expand All @@ -35,10 +35,10 @@
$crawler = new Crawler();

Http::fake([
'vormkracht10.nl' => Http::response('<html><head></head><body><img src="https://vormkracht10.nl/images/logo.png" alt=""></body></html>', 200),
'vormkracht10.nl' => Http::response('<html><head></head><body><img src="https://vormkracht10.nl/images/logo.png" width="5" height="5" alt=""></body></html>', 200),
]);

$crawler->addHtmlContent(Http::get('vormkracht10.nl')->body());

$this->assertFalse($check->check(Http::get('vormkracht10.nl'), $crawler));
});
});

0 comments on commit 62afb7c

Please sign in to comment.