Skip to content

Commit

Permalink
Check on different variants of meta description tags
Browse files Browse the repository at this point in the history
  • Loading branch information
Baspa committed Jan 25, 2024
1 parent d014ee0 commit 7534828
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 17 deletions.
39 changes: 26 additions & 13 deletions src/Checks/Meta/DescriptionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,33 @@ public function check(Response $response, Crawler $crawler): bool
return true;
}

public function validateContent(Crawler $crawler): bool
public function getDescriptionContent(Crawler $crawler): ?string
{
$node = $crawler->filterXPath('//meta[@name="description"]')->getNode(0);

if (! $node) {
return false;
}

$content = $crawler->filterXPath('//meta[@name="description"]')->attr('content');

if (! $content) {
return false;
$tags = ['description', 'og:description', 'twitter:description'];

foreach ($tags as $tag) {
$property = $tag === 'og:description' ? 'property' : 'name';

/** @var \DOMElement $node */
$node = $crawler->filterXPath("//meta[@{$property}=\"{$tag}\"]")->getNode(0);

if ($node) {

Check failure on line 51 in src/Checks/Meta/DescriptionCheck.php

View workflow job for this annotation

GitHub Actions / phpstan

If condition is always true.

Check failure on line 51 in src/Checks/Meta/DescriptionCheck.php

View workflow job for this annotation

GitHub Actions / phpstan

If condition is always true.
return $node->getAttribute('content');
}
}

return true;

return null;

Check failure on line 56 in src/Checks/Meta/DescriptionCheck.php

View workflow job for this annotation

GitHub Actions / phpstan

Unreachable statement - code above always terminates.

Check failure on line 56 in src/Checks/Meta/DescriptionCheck.php

View workflow job for this annotation

GitHub Actions / phpstan

Unreachable statement - code above always terminates.
}

public function validateContent(Crawler $crawler): bool
{
$content = $this->getDescriptionContent($crawler);

return !empty($content);
}

public function isDescriptionSet(Crawler $crawler): bool
{
return $this->getDescriptionContent($crawler) !== null;
}
}
34 changes: 30 additions & 4 deletions tests/Checks/Meta/DescriptionCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,46 @@
use Symfony\Component\DomCrawler\Crawler;
use Vormkracht10\Seo\Checks\Meta\DescriptionCheck;

it('can perform the description check on a page with a description', function () {
it('can perform the description check on a page with an og:description', function () {
$check = new DescriptionCheck();
$crawler = new Crawler();

Http::fake([
'vormkracht10.nl' => Http::response('<html><head><meta name="description" content="Vormkracht10 is a web development agency based in Amsterdam."></head><body></body></html>', 200),
'vormkracht10.nl' => Http::response('<html><head><meta property="og:description" content="Vormkracht10 is a web development agency based in Amsterdam."></head><body></body></html>', 200),
]);

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

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

it('can perform the description check on a page without a description', function () {
it('can perform the description check on a page with a twitter:description', function () {
$check = new DescriptionCheck();
$crawler = new Crawler();

Http::fake([
'vormkracht10.nl' => Http::response('<html><head><meta name="twitter:description" content="Vormkracht10 is a web development agency based in Amsterdam."></head><body></body></html>', 200),
]);

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

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

it('can perform the description check on a page with multiple description tags', function () {
$check = new DescriptionCheck();
$crawler = new Crawler();

Http::fake([
'vormkracht10.nl' => Http::response('<html><head><meta name="description" content="Vormkracht10 is a web development agency based in Amsterdam."><meta property="og:description" content="Vormkracht10 is a web development agency based in Amsterdam."><meta name="twitter:description" content="Vormkracht10 is a web development agency based in Amsterdam."></head><body></body></html>', 200),
]);

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

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

it('can perform the description check on a page without any description tags', function () {
$check = new DescriptionCheck();
$crawler = new Crawler();

Expand All @@ -28,4 +54,4 @@
$crawler->addHtmlContent(Http::get('vormkracht10.nl')->body());

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

0 comments on commit 7534828

Please sign in to comment.