Skip to content

Commit

Permalink
[Fix] Only check on description meta tag because Google uses only tha…
Browse files Browse the repository at this point in the history
…t tag (#57)

* Only check on description meta tag because Google uses only that tag

* Fix styling

---------

Co-authored-by: Baspa <[email protected]>
  • Loading branch information
Baspa and Baspa authored Jan 26, 2024
1 parent 2f98cbf commit de4f83a
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 36 deletions.
14 changes: 4 additions & 10 deletions src/Checks/Meta/DescriptionCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,11 @@ public function check(Response $response, Crawler $crawler): bool

public function getDescriptionContent(Crawler $crawler): ?string
{
$tags = ['description', 'og:description', 'twitter:description'];
/** @var \DOMElement $node */
$node = $crawler->filterXPath('//meta[@name="description"]')->getNode(0);

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');
}
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 de4f83a

Please sign in to comment.