diff --git a/src/MetaTags/Concerns/ManageLinksTags.php b/src/MetaTags/Concerns/ManageLinksTags.php index e5e0da5..494c5b3 100644 --- a/src/MetaTags/Concerns/ManageLinksTags.php +++ b/src/MetaTags/Concerns/ManageLinksTags.php @@ -57,7 +57,7 @@ public function getCanonical(): ?TagInterface public function setPaginationLinks(Paginator $paginator): self { - $this->setCanonical($paginator->url($paginator->currentPage())); + $this->setCanonical($paginator->currentPage() > 1 ? $paginator->url($paginator->currentPage()) : $paginator->url(1)); $this->setNextHref($paginator->nextPageUrl()); $this->setPrevHref($paginator->previousPageUrl()); diff --git a/tests/MetaTags/PaginatorMetaTagsTest.php b/tests/MetaTags/PaginatorMetaTagsTest.php index af65a04..5a87b40 100644 --- a/tests/MetaTags/PaginatorMetaTagsTest.php +++ b/tests/MetaTags/PaginatorMetaTagsTest.php @@ -8,31 +8,69 @@ class PaginatorMetaTagsTest extends TestCase { + /** + * Test pagination meta tags for the first page. + * Ensures the canonical URL does not include ?page=1 for SEO reasons. + */ function test_its_can_be_set_from_paginator() { $meta = $this->makeMetaTags(); $paginator = m::mock(Paginator::class); - $paginator->shouldReceive('nextPageUrl')->once()->andReturn('http://site.com/next'); - $paginator->shouldReceive('previousPageUrl')->once()->andReturn('http://site.com/prev'); + // Mock methods to simulate paginator behavior for the first page. + $paginator->shouldReceive('nextPageUrl')->once()->andReturn('http://site.com?page=2'); + $paginator->shouldReceive('previousPageUrl')->once()->andReturn(null); // No previous page for the first page $paginator->shouldReceive('currentPage')->once()->andReturn(1); - $paginator->shouldReceive('url')->once()->andReturn('http://site.com/1'); + $paginator->shouldReceive('url')->with(1)->andReturn('http://site.com'); // Canonical URL without ?page=1 + $paginator->shouldReceive('url')->with(2)->andReturn('http://site.com?page=2'); $meta->setPaginationLinks($paginator); $this->assertHtmlableContains( - '', + '', $meta->getNextHref() ); $this->assertHtmlableContains( - '', + '', + $meta->getCanonical() + ); + } + + /** + * Test pagination meta tags for page 10. + * Ensures canonical and next/prev links are generated correctly. + */ + function test_its_can_be_set_from_paginator_page_10() + { + $meta = $this->makeMetaTags(); + + $paginator = m::mock(Paginator::class); + + // Allow multiple calls to currentPage() + $paginator->shouldReceive('currentPage')->atLeast()->once()->andReturn(10); + + // Other mock setups remain unchanged + $paginator->shouldReceive('nextPageUrl')->once()->andReturn('http://site.com?page=11'); + $paginator->shouldReceive('previousPageUrl')->once()->andReturn('http://site.com?page=9'); + $paginator->shouldReceive('url')->with(10)->andReturn('http://site.com?page=10'); + $paginator->shouldReceive('url')->with(11)->andReturn('http://site.com?page=11'); + + $meta->setPaginationLinks($paginator); + + $this->assertHtmlableContains( + '', + $meta->getNextHref() + ); + + $this->assertHtmlableContains( + '', $meta->getPrevHref() ); $this->assertHtmlableContains( - '', + '', $meta->getCanonical() ); } @@ -160,4 +198,4 @@ function test_next_href_string_should_be_cleaned() $meta->getNextHref() ); } -} \ No newline at end of file +}