Commit 044a4adf authored by catch's avatar catch

Issue #2084125 by pwolanin: Fixed Bug in active detection in LinkGenerator -...

Issue #2084125 by pwolanin: Fixed Bug in active detection in LinkGenerator - need to extract array from _raw_variables() ParameterBag.
parent b8016410
......@@ -74,10 +74,13 @@ public function __construct(UrlGeneratorInterface $url_generator, ModuleHandlerI
public function setRequest(Request $request) {
// Pre-calculate and store values based on the request that may be used
// repeatedly in generate().
$raw_variables = $request->attributes->get('_raw_variables');
// $raw_variables is a ParameterBag object or NULL.
$parameters = $raw_variables ? $raw_variables->all() : array();
$this->active = array(
'route_name' => $request->attributes->get(RouteObjectInterface::ROUTE_NAME),
'language' => $this->languageManager->getLanguage(Language::TYPE_URL)->id,
'parameters' => (array) $request->attributes->get('_raw_variables') + (array) $request->query->all(),
'parameters' => $parameters + (array) $request->query->all(),
);
}
......
......@@ -7,11 +7,12 @@
namespace Drupal\Tests\Core\Utility {
use Drupal\Core\Language\Language;
use Drupal\Core\Utility\LinkGenerator;
use Drupal\Tests\UnitTestCase;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
use Drupal\Core\Language\Language;
use Drupal\Core\Utility\LinkGenerator;
use Drupal\Tests\UnitTestCase;
use Symfony\Cmf\Component\Routing\RouteObjectInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
/**
* Tests the link generator.
......@@ -311,7 +312,7 @@ public function testGenerateWithHtml() {
* service.
*/
public function testGenerateActive() {
$this->urlGenerator->expects($this->exactly(6))
$this->urlGenerator->expects($this->exactly(7))
->method('generateFromRoute')
->will($this->returnValueMap(array(
array('test_route_1', array(), FALSE, '/test-route-1'),
......@@ -320,9 +321,10 @@ public function testGenerateActive() {
array('test_route_1', array(), FALSE, '/test-route-1'),
array('test_route_3', array(), FALSE, '/test-route-3'),
array('test_route_3', array(), FALSE, '/test-route-3'),
array('test_route_4', array('object' => '1'), FALSE, '/test-route-4/1'),
)));
$this->moduleHandler->expects($this->exactly(6))
$this->moduleHandler->expects($this->exactly(7))
->method('alter');
$this->setUpLanguageManager();
......@@ -338,6 +340,10 @@ public function testGenerateActive() {
// Render a link with the same path as the current path.
$request = new Request(array(), array(), array('system_path' => 'test-route-1', RouteObjectInterface::ROUTE_NAME => 'test_route_1'));
// This attribute is expected to be set in a Drupal request by
// \Drupal\Core\ParamConverter\ParamConverterManager
$raw_variables = new ParameterBag();
$request->attributes->set('_raw_variables', $raw_variables);
$this->linkGenerator->setRequest($request);
$result = $this->linkGenerator->generate('Test', 'test_route_1');
$this->assertTag(array(
......@@ -367,7 +373,8 @@ public function testGenerateActive() {
// Render a link with the same path and query parameter as the current path.
$request = new Request(array('value' => 'example_1'), array(), array('system_path' => 'test-route-3', RouteObjectInterface::ROUTE_NAME => 'test_route_3'));
$parameters = $request->query->all();
$raw_variables = new ParameterBag();
$request->attributes->set('_raw_variables', $raw_variables);
$this->linkGenerator->setRequest($request);
$result = $this->linkGenerator->generate(
'Test',
......@@ -392,6 +399,21 @@ public function testGenerateActive() {
'tag' => 'a',
'attributes' => array('class' => 'active'),
), $result);
// Render a link with the same path and query parameter as the current path.
$request = new Request(array('value' => 'example_1'), array(), array('system_path' => 'test-route-4/1', RouteObjectInterface::ROUTE_NAME => 'test_route_4'));
$raw_variables = new ParameterBag(array('object' => '1'));
$request->attributes->set('_raw_variables', $raw_variables);
$this->linkGenerator->setRequest($request);
$result = $this->linkGenerator->generate(
'Test',
'test_route_4',
array('object' => '1'),
array('query' => array('value' => 'example_1'))
);
$this->assertTag(array(
'tag' => 'a',
'attributes' => array('class' => 'active'),
), $result);
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment