Skip to content

Commit

Permalink
Merge pull request #70 from atiksoftware/3.x
Browse files Browse the repository at this point in the history
fix(pagination): ensure canonical URLs exclude `?page=1`
  • Loading branch information
butschster authored Jan 16, 2025
2 parents 4481137 + 6b21fe0 commit 5608a65
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/MetaTags/Concerns/ManageLinksTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
52 changes: 45 additions & 7 deletions tests/MetaTags/PaginatorMetaTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
'<link rel="next" href="http://site.com/next">',
'<link rel="next" href="http://site.com?page=2">',
$meta->getNextHref()
);

$this->assertHtmlableContains(
'<link rel="prev" href="http://site.com/prev">',
'<link rel="canonical" href="http://site.com">',
$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(
'<link rel="next" href="http://site.com?page=11">',
$meta->getNextHref()
);

$this->assertHtmlableContains(
'<link rel="prev" href="http://site.com?page=9">',
$meta->getPrevHref()
);

$this->assertHtmlableContains(
'<link rel="canonical" href="http://site.com/1">',
'<link rel="canonical" href="http://site.com?page=10">',
$meta->getCanonical()
);
}
Expand Down Expand Up @@ -160,4 +198,4 @@ function test_next_href_string_should_be_cleaned()
$meta->getNextHref()
);
}
}
}

0 comments on commit 5608a65

Please sign in to comment.