core/core.services.yml | 3 +- core/includes/common.inc | 4 +- core/lib/Drupal/Core/Utility/LinkGenerator.php | 17 +- .../Tests/Core/Utility/LinkGeneratorTest.php | 275 +++++++------------- 4 files changed, 109 insertions(+), 190 deletions(-) diff --git a/core/core.services.yml b/core/core.services.yml index 395d994..d4a6e95 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -270,9 +270,10 @@ services: - { name: persist } link_generator: class: Drupal\Core\Utility\LinkGenerator - arguments: ['@url_generator', '@module_handler', '@language_manager', '@path.alias_manager.cached', '@current_user'] + arguments: ['@url_generator', '@module_handler', '@language_manager', '@path.alias_manager.cached'] calls: - [setRequest, ['@?request']] + - [setCurrentUser, ['@?current_user']] router.dynamic: class: Symfony\Cmf\Component\Routing\DynamicRouter arguments: ['@router.request_context', '@router.matcher', '@url_generator'] diff --git a/core/includes/common.inc b/core/includes/common.inc index dc4083e..3ba3000 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -2257,14 +2257,14 @@ function drupal_add_js($data = NULL, $options = NULL) { if (!(defined('MAINTENANCE_MODE') && MAINTENANCE_MODE === 'update')) { $current_path_is_admin = path_is_admin($current_path); } - $javascript['settings']['data'][] = array( + $path = array( 'basePath' => base_path(), 'scriptPath' => $scriptPath, 'pathPrefix' => $pathPrefix, 'currentPath' => $current_path, 'currentPathIsAdmin' => $current_path_is_admin, 'isFront' => drupal_is_front_page(), - 'currentLanguage' => Drupal::languageManager()->getLanguage(Language::TYPE_URL)->id, + 'currentLanguage' => \Drupal::languageManager()->getLanguage(Language::TYPE_URL)->id, ); if (!empty($current_query)) { ksort($current_query); diff --git a/core/lib/Drupal/Core/Utility/LinkGenerator.php b/core/lib/Drupal/Core/Utility/LinkGenerator.php index bc026c8..d5300bf 100644 --- a/core/lib/Drupal/Core/Utility/LinkGenerator.php +++ b/core/lib/Drupal/Core/Utility/LinkGenerator.php @@ -60,7 +60,7 @@ class LinkGenerator implements LinkGeneratorInterface { protected $aliasManager; /** - * The current user service. + * The current user. * * @var \Drupal\Core\Session\AccountInterface */ @@ -77,15 +77,12 @@ class LinkGenerator implements LinkGeneratorInterface { * The language manager. * @param \Drupal\Core\Path\AliasManagerInterface $alias_manager * The path alias manager. - * @param \Drupal\Core\Session\AccountInterface $current_user - * The current user service. */ - public function __construct(UrlGeneratorInterface $url_generator, ModuleHandlerInterface $module_handler, LanguageManager $language_manager, AliasManagerInterface $alias_manager, AccountInterface $current_user) { + public function __construct(UrlGeneratorInterface $url_generator, ModuleHandlerInterface $module_handler, LanguageManager $language_manager, AliasManagerInterface $alias_manager) { $this->urlGenerator = $url_generator; $this->moduleHandler = $module_handler; $this->languageManager = $language_manager; $this->aliasManager = $alias_manager; - $this->currentUser = $current_user; } /** @@ -108,6 +105,16 @@ public function setRequest(Request $request) { } /** + * Sets the current user. + * + * @param \Drupal\Core\Session\AccountInterface $current_user + * The current user service. + */ + public function setCurrentUser(AccountInterface $current_user) { + $this->currentUser = $current_user; + } + + /** * {@inheritdoc} */ public function getActive() { diff --git a/core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php b/core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php index 8cd8f56..1301fe6 100644 --- a/core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php +++ b/core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php @@ -319,159 +319,78 @@ public function testGenerateWithHtml() { } /** - * Tests the active class on the link method for anonymous users. + * Provides test data for testing the active class on the link method. * - * @see \Drupal\Core\Utility\LinkGenerator::generate() + * @see \Drupal\Tests\Core\Utility\LinkGeneratorTest::testGenerateActive() * - * @todo Test that the active class is added on the front page when generating - * links to the front page when drupal_is_front_page() is converted to a - * service. + * @return array + * Returns some test data. */ - public function testGenerateActiveAnonymous() { - $this->currentUser->expects($this->exactly(7)) - ->method('isAuthenticated') - ->will($this->returnValue(FALSE)); - - $this->urlGenerator->expects($this->exactly(8)) - ->method('generateFromRoute') - ->will($this->returnValueMap(array( - array('test_route_1', array(), FALSE, '/test-route-1'), - 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(8)) - ->method('alter'); - - $this->setUpLanguageManager(); - - // Render a link with a path different from the current path. - $request = new Request(array(), array(), array('system_path' => 'test-route-2')); - $this->linkGenerator->setRequest($request); - $result = $this->linkGenerator->generate('Test', 'test_route_1', array(), array('set_active_class' => TRUE)); - $this->assertNotTag(array( - 'tag' => 'a', - 'attributes' => array('class' => 'active'), - ), $result); - - // 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', array(), array('set_active_class' => TRUE)); - $this->assertTag(array( - 'tag' => 'a', - 'attributes' => array('class' => 'active'), - ), $result); - - // Render a link with the same path as the current path, but with the - // set_active_class option disabled. - $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', array(), array('set_active_class' => FALSE)); - $this->assertNotTag(array( - 'tag' => 'a', - 'attributes' => array('class' => 'active'), - ), $result); - - // Render a link with the same path and language as the current path. - $result = $this->linkGenerator->generate('Test', 'test_route_1', array(), array('set_active_class' => TRUE)); - $this->assertTag(array( - 'tag' => 'a', - 'attributes' => array('class' => 'active'), - ), $result); - - // Render a link with the same path but a different language than the current - // path. - $result = $this->linkGenerator->generate( - 'Test', - 'test_route_1', - array(), - array( - 'language' => new Language(array('id' => 'de')), - 'set_active_class' => TRUE, - ) - ); - $this->assertNotTag(array( - 'tag' => 'a', - 'attributes' => array('class' => 'active', 'hreflang' => 'de'), - ), $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-3', RouteObjectInterface::ROUTE_NAME => 'test_route_3')); - $raw_variables = new ParameterBag(); - $request->attributes->set('_raw_variables', $raw_variables); - $this->linkGenerator->setRequest($request); - $result = $this->linkGenerator->generate( - 'Test', - 'test_route_3', - array(), - array( - 'query' => array('value' => 'example_1'), - 'set_active_class' => TRUE, - ) - ); - $this->assertTag(array( - 'tag' => 'a', - 'attributes' => array('class' => 'active'), - ), $result); - - // Render a link with the same path but a different query parameter than the - // current path. - $result = $this->linkGenerator->generate( - 'Test', - 'test_route_3', - array(), - array( - 'query' => array('value' => 'example_2'), - 'set_active_class' => TRUE, - ) - ); - $this->assertNotTag(array( - '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'), - 'set_active_class' => TRUE, - ) + public function providerTestGenerateActive() { + return array( + // Anonymous: generate "active" class. + array(FALSE, array( + // Render a link with a path different from the current path. + 0 => array('method' => 'assertNotTag', 'attributes' => array('class' => 'active')), + // Render a link with the same path as the current path. + 1 => array('method' => 'assertTag', 'attributes' => array('class' => 'active')), + // Render a link with the same path as the current path, but with the + // set_active_class option disabled. + 2 => array('method' => 'assertNotTag', 'attributes' => array('class' => 'active')), + // Render a link with the same path and language as the current path. + 3 => array('method' => 'assertTag', 'attributes' => array('class' => 'active')), + // Render a link with the same path but a different language than the current + // path. + 4 => array('method' => 'assertNotTag', 'attributes' => array('class' => 'active', 'hreflang' => 'de')), + // Render a link with the same path and query parameter as the current path. + 5 => array('method' => 'assertTag', 'attributes' => array('class' => 'active')), + // Render a link with the same path but a different query parameter than the + // current path. + 6 => array('method' => 'assertNotTag', 'attributes' => array('class' => 'active')), + // Render a link with the same path and query parameter as the current path. + 7 => array('method' => 'assertTag', 'attributes' => array('class' => 'active')), + )), + // Authenticated: generate data attributes for drupal.active-link library. + array(TRUE, array( + // Render a link with a path different from the current path. + 0 => array('method' => 'assertTag', 'attributes' => array('data-drupal-link-system-path' => 'test-route-1')), + // Render a link with the same path as the current path. + 1 => array('method' => 'assertTag', 'attributes' => array('data-drupal-link-system-path' => 'test-route-1')), + // Render a link with the same path as the current path, but with the + // set_active_class option disabled. + 2 => array('method' => 'assertNotTag', 'attributes' => array('data-drupal-link-system-path' => 'test-route-1')), + // Render a link with the same path and language as the current path. + 3 => array('method' => 'assertTag', 'attributes' => array('data-drupal-link-system-path' => 'test-route-1')), + // Render a link with the same path but a different language than the current + // path. + 4 => array('method' => 'assertTag', 'attributes' => array('data-drupal-link-system-path' => 'test-route-1', 'hreflang' => 'de')), + // Render a link with the same path and query parameter as the current path. + 5 => array('method' => 'assertTag', 'attributes' => array('data-drupal-link-system-path' => 'test-route-3', 'data-drupal-link-query' => 'regexp:/.*value.*example_1.*/')), + // Render a link with the same path but a different query parameter than the + // current path. + 6 => array('method' => 'assertTag', 'attributes' => array('data-drupal-link-system-path' => 'test-route-3', 'data-drupal-link-query' => 'regexp:/.*value.*example_2.*/')), + // Render a link with the same path and query parameter as the current path. + 7 => array('method' => 'assertTag', 'attributes' => array('data-drupal-link-system-path' => 'test-route-4/1', 'data-drupal-link-query' => 'regexp:/.*value.*example_1.*/')), + )), ); - $this->assertTag(array( - 'tag' => 'a', - 'attributes' => array('class' => 'active'), - ), $result); } /** - * Tests the active class on the link method for authenticated users. + * Tests the active class on the link method. * * @see \Drupal\Core\Utility\LinkGenerator::generate() + * @see \Drupal\Tests\Core\Utility\LinkGeneratorTest::providerTestGenerateActive() + * + * @dataProvider providerTestGenerateActive * * @todo Test that the active class is added on the front page when generating * links to the front page when drupal_is_front_page() is converted to a * service. */ - public function testGenerateActiveAuthenticated() { + public function testGenerateActive($is_authenticated, $expected_results) { $this->currentUser->expects($this->exactly(7)) ->method('isAuthenticated') - ->will($this->returnValue(TRUE)); + ->will($this->returnValue($is_authenticated)); $this->urlGenerator->expects($this->exactly(8)) ->method('generateFromRoute') @@ -481,34 +400,38 @@ public function testGenerateActiveAuthenticated() { array('test_route_4', array('object' => '1'), FALSE, '/test-route-4/1'), ))); - $this->urlGenerator->expects($this->exactly(7)) - ->method('getPathFromRoute') - ->will($this->returnValueMap(array( - array('test_route_1', array(), 'test-route-1'), - array('test_route_3', array(), 'test-route-3'), - array('test_route_4', array('object' => '1'), 'test-route-4/1'), - ))); - - $this->aliasManager->expects($this->exactly(7)) - ->method('getSystemPath') - ->will($this->returnValueMap(array( - array('test-route-1', NULL, 'test-route-1'), - array('test-route-3', NULL, 'test-route-3'), - array('test-route-4/1', NULL, 'test-route-4/1'), - ))); + if ($is_authenticated) { + $this->urlGenerator->expects($this->exactly(7)) + ->method('getPathFromRoute') + ->will($this->returnValueMap(array( + array('test_route_1', array(), 'test-route-1'), + array('test_route_3', array(), 'test-route-3'), + array('test_route_4', array('object' => '1'), 'test-route-4/1'), + ))); + + $this->aliasManager->expects($this->exactly(7)) + ->method('getSystemPath') + ->will($this->returnValueMap(array( + array('test-route-1', NULL, 'test-route-1'), + array('test-route-3', NULL, 'test-route-3'), + array('test-route-4/1', NULL, 'test-route-4/1'), + ))); + } $this->moduleHandler->expects($this->exactly(8)) ->method('alter'); $this->setUpLanguageManager(); + $this->linkGenerator->setCurrentUser($this->currentUser); + // Render a link with a path different from the current path. $request = new Request(array(), array(), array('system_path' => 'test-route-2')); $this->linkGenerator->setRequest($request); $result = $this->linkGenerator->generate('Test', 'test_route_1', array(), array('set_active_class' => TRUE)); - $this->assertTag(array( + $this->$expected_results[0]['method'](array( 'tag' => 'a', - 'attributes' => array('data-drupal-link-system-path' => 'test-route-1'), + 'attributes' => $expected_results[0]['attributes'], ), $result); // Render a link with the same path as the current path. @@ -519,9 +442,9 @@ public function testGenerateActiveAuthenticated() { $request->attributes->set('_raw_variables', $raw_variables); $this->linkGenerator->setRequest($request); $result = $this->linkGenerator->generate('Test', 'test_route_1', array(), array('set_active_class' => TRUE)); - $this->assertTag(array( + $this->$expected_results[1]['method'](array( 'tag' => 'a', - 'attributes' => array('data-drupal-link-system-path' => 'test-route-1'), + 'attributes' => $expected_results[1]['attributes'], ), $result); // Render a link with the same path as the current path, but with the @@ -533,20 +456,20 @@ public function testGenerateActiveAuthenticated() { $request->attributes->set('_raw_variables', $raw_variables); $this->linkGenerator->setRequest($request); $result = $this->linkGenerator->generate('Test', 'test_route_1', array(), array('set_active_class' => FALSE)); - $this->assertNotTag(array( + $this->$expected_results[2]['method'](array( 'tag' => 'a', - 'attributes' => array('data-drupal-link-system-path' => 'test-route-1'), + 'attributes' => $expected_results[2]['attributes'], ), $result); // Render a link with the same path and language as the current path. $result = $this->linkGenerator->generate('Test', 'test_route_1', array(), array('set_active_class' => TRUE)); - $this->assertTag(array( + $this->$expected_results[3]['method'](array( 'tag' => 'a', - 'attributes' => array('data-drupal-link-system-path' => 'test-route-1'), + 'attributes' => $expected_results[3]['attributes'], ), $result); - // Render a link with the same path but a different language than the - // current path. + // Render a link with the same path but a different language than the current + // path. $result = $this->linkGenerator->generate( 'Test', 'test_route_1', @@ -556,12 +479,9 @@ public function testGenerateActiveAuthenticated() { 'set_active_class' => TRUE, ) ); - $this->assertTag(array( + $this->$expected_results[4]['method'](array( 'tag' => 'a', - 'attributes' => array( - 'data-drupal-link-system-path' => 'test-route-1', - 'hreflang' => 'de', - ), + 'attributes' => $expected_results[4]['attributes'], ), $result); // Render a link with the same path and query parameter as the current path. @@ -578,12 +498,9 @@ public function testGenerateActiveAuthenticated() { 'set_active_class' => TRUE, ) ); - $this->assertTag(array( + $this->$expected_results[5]['method'](array( 'tag' => 'a', - 'attributes' => array( - 'data-drupal-link-system-path' => 'test-route-3', - 'data-drupal-link-query' => 'regexp:/.*value.*example_1.*/', - ), + 'attributes' => $expected_results[5]['attributes'], ), $result); // Render a link with the same path but a different query parameter than the @@ -597,12 +514,9 @@ public function testGenerateActiveAuthenticated() { 'set_active_class' => TRUE, ) ); - $this->assertTag(array( + $this->$expected_results[6]['method'](array( 'tag' => 'a', - 'attributes' => array( - 'data-drupal-link-system-path' => 'test-route-3', - 'data-drupal-link-query' => 'regexp:/.*value.*example_2.*/', - ), + 'attributes' => $expected_results[6]['attributes'], ), $result); // Render a link with the same path and query parameter as the current path. @@ -619,12 +533,9 @@ public function testGenerateActiveAuthenticated() { 'set_active_class' => TRUE, ) ); - $this->assertTag(array( + $this->$expected_results[7]['method'](array( 'tag' => 'a', - 'attributes' => array( - 'data-drupal-link-system-path' => 'test-route-4/1', - 'data-drupal-link-query' => 'regexp:/.*value.*example_1.*/', - ), + 'attributes' => $expected_results[7]['attributes'], ), $result); }