Skip to content

Commit

Permalink
Merge pull request #255 from brentmullen/fix/canonical
Browse files Browse the repository at this point in the history
fix: add ability to use Url::current() as default canonical via config
  • Loading branch information
vinicius73 committed Sep 29, 2021
2 parents b894d9e + 10711d2 commit 94fbb3b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
12 changes: 11 additions & 1 deletion src/SEOTools/SEOMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,19 @@ public function getDescription()
*/
public function getCanonical()
{
if ($this->canonical) {
return $this->canonical;
}

$canonical_config = $this->config->get('defaults.canonical', false);

return $this->canonical ?: (($canonical_config === null) ? htmlspecialchars(app('url')->full()) : $canonical_config);
if ($canonical_config === null || $canonical_config === 'full') {
return htmlspecialchars(app('url')->full());
} elseif ($canonical_config === 'current') {
return htmlspecialchars(app('url')->current());
}

return $canonical_config;
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/resources/config/seotools.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
'description' => 'For those who helped create the Genki Dama', // set false to total remove
'separator' => ' - ',
'keywords' => [],
'canonical' => false, // Set null for using Url::current(), set false to total remove
'canonical' => false, // Set to null or 'full' to use Url::full(), set to 'current' to use Url::current(), set false to total remove
'robots' => false, // Set to 'all', 'none' or any combination of index/noindex and follow/nofollow
],
/*
Expand Down
44 changes: 44 additions & 0 deletions tests/SEOTools/SEOMetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,50 @@ public function test_set_canonical()
$this->assertEquals($canonical, $this->seoMeta->getCanonical());
}

public function dataTestUrls()
{
return [
['http://localhost/hello/world', 'http://localhost/hello/world'],
['http://localhost/hello/world?param=1', 'http://localhost/hello/world'],
];
}

/**
* @dataProvider dataTestUrls
*/
public function test_get_canonical_null($fullUrl)
{
config()->set('defaults.canonical', null);
$this->seoMeta = new SEOMeta(config());

$this->get($fullUrl);
$this->assertEquals($fullUrl, $this->seoMeta->getCanonical());
}

/**
* @dataProvider dataTestUrls
*/
public function test_get_canonical_full($fullUrl)
{
config()->set('defaults.canonical', 'full');
$this->seoMeta = new SEOMeta(config());

$this->get($fullUrl);
$this->assertEquals($fullUrl, $this->seoMeta->getCanonical());
}

/**
* @dataProvider dataTestUrls
*/
public function test_get_canonical_current($fullUrl, $currentUrl)
{
config()->set('defaults.canonical', 'current');
$this->seoMeta = new SEOMeta(config());

$this->get($fullUrl);
$this->assertEquals($currentUrl, $this->seoMeta->getCanonical());
}

public function test_set_amp()
{
$fullHeader = "<title>It's Over 9000!</title>";
Expand Down

0 comments on commit 94fbb3b

Please sign in to comment.