Skip to content

Commit

Permalink
Only check on description meta tag because Google uses only that tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Baspa committed Jan 26, 2024
1 parent 2f98cbf commit 855cbd4
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 38 deletions.
18 changes: 6 additions & 12 deletions src/Checks/Meta/DescriptionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,13 @@ public function check(Response $response, Crawler $crawler): bool

public function getDescriptionContent(Crawler $crawler): ?string
{
$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 instanceof \DOMElement && $node->hasAttribute('content')) {
return $node->getAttribute('content');
}
/** @var \DOMElement $node */
$node = $crawler->filterXPath("//meta[@name=\"description\"]")->getNode(0);

if ($node instanceof \DOMElement && $node->hasAttribute('content')) {
return $node->getAttribute('content');
}

return null;
}

Expand Down
26 changes: 0 additions & 26 deletions tests/Checks/Meta/DescriptionCheckTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,6 @@
use Symfony\Component\DomCrawler\Crawler;
use Vormkracht10\Seo\Checks\Meta\DescriptionCheck;

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 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 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();
Expand Down

0 comments on commit 855cbd4

Please sign in to comment.