Skip to content

Commit

Permalink
Fix Issue 612 (jeroennoten#651)
Browse files Browse the repository at this point in the history
* [src/Menu]: Make changes on the active checker to cover issues when url generator scheme is forced to https.

* [src/Menu]: Fix code style.
  • Loading branch information
dfsmania authored Jul 3, 2020
1 parent 5628daf commit 02a3029
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
9 changes: 5 additions & 4 deletions src/Menu/ActiveChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,11 @@ class ActiveChecker
/**
* Constructor.
*
* @param Request $request
* @param UrlGenerator $url
*/
public function __construct(Request $request, UrlGenerator $url)
public function __construct(UrlGenerator $url)
{
$this->request = $request;
$this->request = $url->getRequest();
$this->url = $url;

// Fill the map with tests. These tests will check if a menu item is
Expand Down Expand Up @@ -135,6 +134,8 @@ protected function checkPattern($pattern)
// If pattern is not a regex, check if the requested url matches the
// absolute path to the given pattern.

return Str::is($this->url->to($pattern), $this->request->url());
$pattern = preg_replace('@^https?:https://@', '*', $this->url->to($pattern));

return Str::is($pattern, $this->request->url());
}
}
14 changes: 14 additions & 0 deletions tests/Menu/ActiveCheckerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,18 @@ public function testActivefallbackToUrl()

$this->assertTrue($isActive);
}

public function testWithForcedScheme()
{
$checker = $this->makeActiveChecker('https://example.com/about', 'https');

$isActive = $checker->isActive(['url' => 'about']);
$this->assertTrue($isActive);

$isActive = $checker->isActive(['url' => 'https://example.com/about']);
$this->assertTrue($isActive);

$isActive = $checker->isActive(['url' => 'https://example.com/about']);
$this->assertTrue($isActive);
}
}
17 changes: 13 additions & 4 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ protected function makeTranslator($locale = 'en')
return $translator;
}

protected function makeActiveChecker($uri = 'https://example.com')
protected function makeActiveChecker($uri = 'https://example.com', $scheme = null)
{
return new ActiveChecker($this->makeRequest($uri), $this->makeUrlGenerator($uri));
return new ActiveChecker($this->makeUrlGenerator($uri, $scheme));
}

private function makeRequest($uri)
Expand All @@ -64,9 +64,18 @@ protected function makeAdminLte()
return new AdminLte($this->getFilters(), $this->getDispatcher(), $this->makeContainer());
}

protected function makeUrlGenerator($uri = 'https://example.com')
protected function makeUrlGenerator($uri = 'https://example.com', $scheme = null)
{
return new UrlGenerator($this->getRouteCollection(), $this->makeRequest($uri));
$UrlGenerator = new UrlGenerator(
$this->getRouteCollection(),
$this->makeRequest($uri)
);

if ($scheme) {
$UrlGenerator->forceScheme($scheme);
}

return $UrlGenerator;
}

protected function makeGate()
Expand Down

0 comments on commit 02a3029

Please sign in to comment.