diff --git a/src/Checks/Meta/DescriptionCheck.php b/src/Checks/Meta/DescriptionCheck.php index ba8fd354..18023b8c 100644 --- a/src/Checks/Meta/DescriptionCheck.php +++ b/src/Checks/Meta/DescriptionCheck.php @@ -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) { + return $node->getAttribute('content'); + } } - - return true; + + return null; + } + + public function validateContent(Crawler $crawler): bool + { + $content = $this->getDescriptionContent($crawler); + + return !empty($content); + } + + public function isDescriptionSet(Crawler $crawler): bool + { + return $this->getDescriptionContent($crawler) !== null; } } diff --git a/tests/Checks/Meta/DescriptionCheckTest.php b/tests/Checks/Meta/DescriptionCheckTest.php index bd2fdbd0..abd61e65 100644 --- a/tests/Checks/Meta/DescriptionCheckTest.php +++ b/tests/Checks/Meta/DescriptionCheckTest.php @@ -4,12 +4,12 @@ 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('', 200), + 'vormkracht10.nl' => Http::response('', 200), ]); $crawler->addHtmlContent(Http::get('vormkracht10.nl')->body()); @@ -17,7 +17,33 @@ $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('', 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('', 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(); @@ -28,4 +54,4 @@ $crawler->addHtmlContent(Http::get('vormkracht10.nl')->body()); $this->assertFalse($check->check(Http::get('vormkracht10.nl'), $crawler)); -}); +}); \ No newline at end of file