Commit 1299c3af authored by alexpott's avatar alexpott

Issue #2233619 by slashrsm, Jalandhar: Merge lookup functions in AliasManager.

parent 85c8f324
......@@ -1058,7 +1058,7 @@ function l($text, $path, array $options = array()) {
// Add a "data-drupal-link-system-path" attribute to let the
// drupal.active-link library know the path in a standardized manner.
if (!isset($variables['options']['attributes']['data-drupal-link-system-path'])) {
$variables['options']['attributes']['data-drupal-link-system-path'] = \Drupal::service('path.alias_manager.cached')->getSystemPath($path);
$variables['options']['attributes']['data-drupal-link-system-path'] = \Drupal::service('path.alias_manager.cached')->getPathByAlias($path);
}
}
......
......@@ -1230,7 +1230,7 @@ function template_preprocess_links(&$variables) {
// Add a "data-drupal-link-system-path" attribute to let the
// drupal.active-link library know the path in a standardized manner.
$li_attributes['data-drupal-link-system-path'] = \Drupal::service('path.alias_manager.cached')->getSystemPath($path);
$li_attributes['data-drupal-link-system-path'] = \Drupal::service('path.alias_manager.cached')->getPathByAlias($path);
}
$item['link'] = $link_element;
......
......@@ -83,25 +83,25 @@ public function writeCache() {
/**
* {@inheritdoc}
*/
public function getSystemPath($path, $path_language = NULL) {
$system_path = $this->aliasManager->getSystemPath($path, $path_language);
public function getPathByAlias($alias, $langcode = NULL) {
$path = $this->aliasManager->getPathByAlias($alias, $langcode);
// We need to pass on the list of previously cached system paths for this
// key to the alias manager for use in subsequent lookups.
$cached = $this->cache->get($system_path);
$cached = $this->cache->get($path);
$cached_paths = array();
if ($cached) {
$cached_paths = $cached->data;
$this->cacheNeedsWriting = FALSE;
}
$this->preloadPathLookups($cached_paths);
return $system_path;
return $path;
}
/**
* {@inheritdoc}
*/
public function getPathAlias($path, $path_language = NULL) {
return $this->aliasManager->getPathAlias($path, $path_language);
public function getAliasByPath($path, $langcode = NULL) {
return $this->aliasManager->getAliasByPath($path, $langcode);
}
/**
......
......@@ -141,7 +141,7 @@ public function on403Html(FlattenException $exception, Request $request) {
watchdog('access denied', $system_path, NULL, WATCHDOG_WARNING);
$system_config = $this->container->get('config.factory')->get('system.site');
$path = $this->container->get('path.alias_manager')->getSystemPath($system_config->get('page.403'));
$path = $this->container->get('path.alias_manager')->getPathByAlias($system_config->get('page.403'));
if ($path && $path != $system_path) {
if ($request->getMethod() === 'POST') {
$subrequest = Request::create($request->getBaseUrl() . '/' . $path, 'POST', array('destination' => $system_path, '_exception_statuscode' => 403) + $request->request->all(), $request->cookies->all(), array(), $request->server->all());
......@@ -197,7 +197,7 @@ public function on404Html(FlattenException $exception, Request $request) {
$system_path = $request->attributes->get('_system_path');
$path = $this->container->get('path.alias_manager')->getSystemPath(\Drupal::config('system.site')->get('page.404'));
$path = $this->container->get('path.alias_manager')->getPathByAlias(\Drupal::config('system.site')->get('page.404'));
if ($path && $path != $system_path) {
// @todo Um, how do I specify an override URL again? Totally not clear. Do
// that and sub-call the kernel rather than using meah().
......
......@@ -34,11 +34,11 @@ class AliasManager implements AliasManagerInterface {
protected $lookupMap = array();
/**
* Holds an array of path alias for which no source was found.
* Holds an array of aliases for which no path was found.
*
* @var array
*/
protected $noSource = array();
protected $noPath = array();
/**
* Holds the array of whitelisted path aliases.
......@@ -48,18 +48,18 @@ class AliasManager implements AliasManagerInterface {
protected $whitelist;
/**
* Holds an array of system paths that have no aliases.
* Holds an array of paths that have no alias.
*
* @var array
*/
protected $noAliases = array();
protected $noAlias = array();
/**
* Whether lookupPath() has not yet been called.
* Whether preloaded path lookups has already been loaded.
*
* @var boolean
* @var array
*/
protected $firstLookup = TRUE;
protected $langcodePreloaded = array();
/**
* Holds an array of previously looked up paths for the current request path.
......@@ -88,36 +88,87 @@ public function __construct(AliasStorageInterface $storage, AliasWhitelistInterf
}
/**
* Implements \Drupal\Core\Path\AliasManagerInterface::getSystemPath().
* {@inheritdoc}
*/
public function getSystemPath($path, $path_language = NULL) {
public function getPathByAlias($alias, $langcode = NULL) {
// If no language is explicitly specified we default to the current URL
// language. If we used a language different from the one conveyed by the
// requested URL, we might end up being unable to check if there is a path
// alias matching the URL path.
$path_language = $path_language ?: $this->languageManager->getCurrentLanguage(Language::TYPE_URL)->id;
// Lookup the path alias first.
if (!empty($path) && $source = $this->lookupPathSource($path, $path_language)) {
$path = $source;
$langcode = $langcode ?: $this->languageManager->getCurrentLanguage(Language::TYPE_URL)->id;
// If we already know that there are no paths for this alias simply return.
if (empty($alias) || !empty($this->noPath[$langcode][$alias])) {
return $alias;
}
return $path;
// Look for the alias within the cached map.
if (isset($this->lookupMap[$langcode]) && ($path = array_search($alias, $this->lookupMap[$langcode]))) {
return $path;
}
// Look for path in storage.
if ($path = $this->storage->lookupPathSource($alias, $langcode)) {
$this->lookupMap[$langcode][$path] = $alias;
return $path;
}
// We can't record anything into $this->lookupMap because we didn't find any
// paths for this alias. Thus cache to $this->noPath.
$this->noPath[$langcode][$alias] = TRUE;
return $alias;
}
/**
* Implements \Drupal\Core\Path\AliasManagerInterface::getPathAlias().
* {@inheritdoc}
*/
public function getPathAlias($path, $path_language = NULL) {
public function getAliasByPath($path, $langcode = NULL) {
// If no language is explicitly specified we default to the current URL
// language. If we used a language different from the one conveyed by the
// requested URL, we might end up being unable to check if there is a path
// alias matching the URL path.
$path_language = $path_language ?: $this->languageManager->getCurrentLanguage(Language::TYPE_URL)->id;
$result = $path;
if (!empty($path) && $alias = $this->lookupPathAlias($path, $path_language)) {
$result = $alias;
$langcode = $langcode ?: $this->languageManager->getCurrentLanguage(Language::TYPE_URL)->id;
// Check the path whitelist, if the top-level part before the first /
// is not in the list, then there is no need to do anything further,
// it is not in the database.
if (empty($path) || !$this->whitelist->get(strtok($path, '/'))) {
return $path;
}
return $result;
// During the first call to this method per language, load the expected
// paths for the page from cache.
if (empty($this->langcodePreloaded[$langcode])) {
$this->langcodePreloaded[$langcode] = TRUE;
$this->lookupMap[$langcode] = array();
// Load paths from cache.
if (!empty($this->preloadedPathLookups)) {
$this->lookupMap[$langcode] = $this->storage->preloadPathAlias($this->preloadedPathLookups, $langcode);
// Keep a record of paths with no alias to avoid querying twice.
$this->noAlias[$langcode] = array_flip(array_diff_key($this->preloadedPathLookups, array_keys($this->lookupMap[$langcode])));
}
}
// If we already know that there are no aliases for this path simply return.
if (!empty($this->noAlias[$langcode][$path])) {
return $path;
}
// If the alias has already been loaded, return it from static cache.
if (isset($this->lookupMap[$langcode][$path])) {
return $this->lookupMap[$langcode][$path];
}
// Try to load alias from storage.
if ($alias = $this->storage->lookupPathAlias($path, $langcode)) {
$this->lookupMap[$langcode][$path] = $alias;
return $alias;
}
// We can't record anything into $this->lookupMap because we didn't find any
// aliases for this path. Thus cache to $this->noAlias.
$this->noAlias[$langcode][$path] = TRUE;
return $path;
}
/**
......@@ -132,9 +183,9 @@ public function cacheClear($source = NULL) {
else {
$this->lookupMap = array();
}
$this->noSource = array();
$this->no_aliases = array();
$this->firstCall = TRUE;
$this->noPath = array();
$this->noAlias = array();
$this->langcodePreloaded = array();
$this->preloadedPathLookups = array();
$this->pathAliasWhitelistRebuild($source);
}
......@@ -157,86 +208,6 @@ public function preloadPathLookups(array $path_list) {
$this->preloadedPathLookups = $path_list;
}
/**
* Given a Drupal system URL return one of its aliases if such a one exists.
* Otherwise, return FALSE.
*
* @param $path
* The path to investigate for corresponding aliases.
* @param $langcode
* Optional language code to search the path with. Defaults to the page language.
* If there's no path defined for that language it will search paths without
* language.
*
* @return
* An aliased path, or FALSE if no path was found.
*/
protected function lookupPathAlias($path, $langcode) {
// During the first call to this method per language, load the expected
// system paths for the page from cache.
if (!empty($this->firstLookup)) {
$this->firstLookup = FALSE;
$this->lookupMap[$langcode] = array();
// Load system paths from cache.
if (!empty($this->preloadedPathLookups)) {
// Now fetch the aliases corresponding to these system paths.
$this->lookupMap[$langcode] = $this->storage->preloadPathAlias($this->preloadedPathLookups, $langcode);
// Keep a record of paths with no alias to avoid querying twice.
$this->noAliases[$langcode] = array_flip(array_diff_key($this->preloadedPathLookups, array_keys($this->lookupMap[$langcode])));
}
}
// If the alias has already been loaded, return it.
if (isset($this->lookupMap[$langcode][$path])) {
return $this->lookupMap[$langcode][$path];
}
// Check the path whitelist, if the top-level part before the first /
// is not in the list, then there is no need to do anything further,
// it is not in the database.
elseif (!$this->whitelist->get(strtok($path, '/'))) {
return FALSE;
}
// For system paths which were not cached, query aliases individually.
elseif (!isset($this->noAliases[$langcode][$path])) {
$this->lookupMap[$langcode][$path] = $this->storage->lookupPathAlias($path, $langcode);
return $this->lookupMap[$langcode][$path];
}
return FALSE;
}
/**
* Given an alias, return its Drupal system URL if one exists. Otherwise,
* return FALSE.
*
* @param $path
* The path to investigate for corresponding system URLs.
* @param $langcode
* Optional language code to search the path with. Defaults to the page language.
* If there's no path defined for that language it will search paths without
* language.
*
* @return
* A Drupal system path, or FALSE if no path was found.
*/
protected function lookupPathSource($path, $langcode) {
if ($this->whitelist && !isset($this->noSource[$langcode][$path])) {
// Look for the value $path within the cached $map
$source = isset($this->lookupMap[$langcode]) ? array_search($path, $this->lookupMap[$langcode]) : FALSE;
if (!$source) {
if ($source = $this->storage->lookupPathSource($path, $langcode)) {
$this->lookupMap[$langcode][$source] = $path;
}
else {
// We can't record anything into $map because we do not have a valid
// index and there is no need because we have not learned anything
// about any Drupal path. Thus cache to $no_source.
$this->noSource[$langcode][$path] = TRUE;
}
}
return $source;
}
return FALSE;
}
/**
* Rebuild the path alias white list.
*
......
......@@ -10,33 +10,30 @@
interface AliasManagerInterface {
/**
* Given a path alias, return the internal path it represents.
* Given the alias, return the path it represents.
*
* @param $path
* A Drupal path alias.
* @param $path_language
* @param string $alias
* An alias.
* @param string $langcode
* An optional language code to look up the path in.
*
* @return
* The internal path represented by the alias, or the original alias if no
* internal path was found.
* @return string
* The path represented by alias, or the alias if no path was found.
*/
public function getSystemPath($path, $path_language = NULL);
public function getPathByAlias($alias, $langcode = NULL);
/**
* Given an internal Drupal path, return the alias set by the administrator.
* Given a path, return the alias.
*
* @param $path
* An internal Drupal path.
*
* @param $path_language
* @param string $path
* A path.
* @param string $langcode
* An optional language code to look up the path in.
*
* @return
* An aliased path if one was found, or the original path if no alias was
* found.
* @return string
* An alias that represents the path, or path if no alias was found.
*/
public function getPathAlias($path, $path_language = NULL);
public function getAliasByPath($path, $langcode = NULL);
/**
* Returns an array of system paths that have been looked up.
......
......@@ -36,7 +36,7 @@ public function __construct(AliasManagerInterface $alias_manager) {
* Implements Drupal\Core\PathProcessor\InboundPathProcessorInterface::processInbound().
*/
public function processInbound($path, Request $request) {
$path = $this->aliasManager->getSystemPath($path);
$path = $this->aliasManager->getPathByAlias($path);
return $path;
}
......@@ -46,7 +46,7 @@ public function processInbound($path, Request $request) {
public function processOutbound($path, &$options = array(), Request $request = NULL) {
if (empty($options['alias'])) {
$langcode = isset($options['language']) ? $options['language']->id : NULL;
$path = $this->aliasManager->getPathAlias($path, $langcode);
$path = $this->aliasManager->getAliasByPath($path, $langcode);
}
return $path;
}
......
......@@ -98,7 +98,7 @@ protected function checkAccess(EntityInterface $entity, $operation, $langcode, A
if ($visibility['path']['visibility'] < BLOCK_VISIBILITY_PHP) {
// Compare the lowercase path alias (if any) and internal path.
$path = current_path();
$path_alias = Unicode::strtolower($this->aliasManager->getPathAlias($path));
$path_alias = Unicode::strtolower($this->aliasManager->getAliasByPath($path));
$page_match = drupal_match_path($path_alias, $pages) || (($path != $path_alias) && drupal_match_path($path, $pages));
// When $block->visibility has a value of 0
// (BLOCK_VISIBILITY_NOTLISTED), the block is displayed on all pages
......
......@@ -210,7 +210,7 @@ public function massageFormValues(array $values, array $form, array &$form_state
// If internal links are supported, look up whether the given value is
// a path alias and store the system path instead.
if ($this->supportsInternalLinks() && !UrlHelper::isExternal($value['url'])) {
$parsed_url['path'] = \Drupal::service('path.alias_manager.cached')->getSystemPath($parsed_url['path']);
$parsed_url['path'] = \Drupal::service('path.alias_manager.cached')->getPathByAlias($parsed_url['path']);
}
$url = Url::createFromPath($parsed_url['path']);
......
......@@ -109,10 +109,10 @@ function testPathLanguageConfiguration() {
'langcode' => Language::LANGCODE_NOT_SPECIFIED,
);
$this->container->get('path.alias_storage')->save($edit['source'], $edit['alias'], $edit['langcode']);
$lookup_path = $this->container->get('path.alias_manager')->getPathAlias('node/' . $node->id(), 'en');
$lookup_path = $this->container->get('path.alias_manager')->getAliasByPath('node/' . $node->id(), 'en');
$this->assertEqual($english_path, $lookup_path, 'English language alias has priority.');
// Same check for language 'xx'.
$lookup_path = $this->container->get('path.alias_manager')->getPathAlias('node/' . $node->id(), $prefix);
$lookup_path = $this->container->get('path.alias_manager')->getAliasByPath('node/' . $node->id(), $prefix);
$this->assertEqual($custom_language_path, $lookup_path, 'Custom language alias has priority.');
$this->container->get('path.alias_storage')->delete($edit);
......
......@@ -204,7 +204,7 @@ protected function actions(array $form, array &$form_state) {
public function validate(array $form, array &$form_state) {
$menu_link = $this->buildEntity($form, $form_state);
$normal_path = $this->pathAliasManager->getSystemPath($menu_link->link_path);
$normal_path = $this->pathAliasManager->getPathByAlias($menu_link->link_path);
if ($menu_link->link_path != $normal_path) {
drupal_set_message(t('The menu system stores system paths only, but will use the URL alias for display. %link_path has been stored as %normal_path', array('%link_path' => $menu_link->link_path, '%normal_path' => $normal_path)));
$menu_link->link_path = $normal_path;
......
......@@ -110,7 +110,7 @@ public function adminOverview($keys) {
// If the system path maps to a different URL alias, highlight this table
// row to let the user know of old aliases.
if ($data->alias != $this->aliasManager->getPathAlias($data->source, $data->langcode)) {
if ($data->alias != $this->aliasManager->getAliasByPath($data->source, $data->langcode)) {
$row['class'] = array('warning');
}
......
......@@ -137,7 +137,7 @@ public function buildForm(array $form, array &$form_state, $pid = NULL) {
*/
public function validateForm(array &$form, array &$form_state) {
$source = &$form_state['values']['source'];
$source = $this->aliasManager->getSystemPath($source);
$source = $this->aliasManager->getPathByAlias($source);
$alias = $form_state['values']['alias'];
// Language is only set if language.module is enabled, otherwise save for all
// languages.
......@@ -160,7 +160,7 @@ public function submitForm(array &$form, array &$form_state) {
$pid = isset($form_state['values']['pid']) ? $form_state['values']['pid'] : 0;
$source = &$form_state['values']['source'];
$source = $this->aliasManager->getSystemPath($source);
$source = $this->aliasManager->getPathByAlias($source);
$alias = $form_state['values']['alias'];
// Language is only set if language.module is enabled, otherwise save for all
// languages.
......
......@@ -177,17 +177,17 @@ function testAliasTranslation() {
// The alias manager has an internal path lookup cache. Check to see that
// it has the appropriate contents at this point.
$this->container->get('path.alias_manager')->cacheClear();
$french_node_path = $this->container->get('path.alias_manager')->getSystemPath($french_alias, 'fr');
$french_node_path = $this->container->get('path.alias_manager')->getPathByAlias($french_alias, 'fr');
$this->assertEqual($french_node_path, 'node/' . $french_node->id(), 'Normal path works.');
// Second call should return the same path.
$french_node_path = $this->container->get('path.alias_manager')->getSystemPath($french_alias, 'fr');
$french_node_path = $this->container->get('path.alias_manager')->getPathByAlias($french_alias, 'fr');
$this->assertEqual($french_node_path, 'node/' . $french_node->id(), 'Normal path is the same.');
// Confirm that the alias works.
$french_node_alias = $this->container->get('path.alias_manager')->getPathAlias('node/' . $french_node->id(), 'fr');
$french_node_alias = $this->container->get('path.alias_manager')->getAliasByPath('node/' . $french_node->id(), 'fr');
$this->assertEqual($french_node_alias, $french_alias, 'Alias works.');
// Second call should return the same alias.
$french_node_alias = $this->container->get('path.alias_manager')->getPathAlias('node/' . $french_node->id(), 'fr');
$french_node_alias = $this->container->get('path.alias_manager')->getAliasByPath('node/' . $french_node->id(), 'fr');
$this->assertEqual($french_node_alias, $french_alias, 'Alias is the same.');
}
}
......@@ -273,7 +273,7 @@ function shortcut_set_title_exists($title) {
*/
function shortcut_valid_link($path) {
// Do not use URL aliases.
$normal_path = \Drupal::service('path.alias_manager')->getSystemPath($path);
$normal_path = \Drupal::service('path.alias_manager')->getPathByAlias($path);
if ($path != $normal_path) {
$path = $normal_path;
}
......
......@@ -40,7 +40,7 @@ public function getValue() {
*/
public function setValue($value, $notify = TRUE) {
// Normalize the path in case it is an alias.
$value = \Drupal::service('path.alias_manager')->getSystemPath($value);
$value = \Drupal::service('path.alias_manager')->getPathByAlias($value);
if (empty($value)) {
$value = '<front>';
}
......
......@@ -62,7 +62,7 @@ public function testShortcutLinkAdd() {
$this->assertResponse(200);
$saved_set = shortcut_set_load($set->id());
$paths = $this->getShortcutInformation($saved_set, 'path');
$this->assertTrue(in_array($this->container->get('path.alias_manager')->getSystemPath($test['path']), $paths), 'Shortcut created: ' . $test['path']);
$this->assertTrue(in_array($this->container->get('path.alias_manager')->getPathByAlias($test['path']), $paths), 'Shortcut created: ' . $test['path']);
$this->assertLink($title, 0, 'Shortcut link found on the page.');
}
}
......
......@@ -169,7 +169,7 @@ function statistics_get($nid) {
* A string as a link, truncated to the width, linked to the given $path.
*/
function _statistics_link($path, $width = 35) {
$title = \Drupal::service('path.alias_manager')->getPathAlias($path);
$title = \Drupal::service('path.alias_manager')->getAliasByPath($path);
$title = truncate_utf8($title, $width, FALSE, TRUE);
return l($title, $path);
}
......
......@@ -94,7 +94,7 @@ public function buildForm(array $form, array &$form_state) {
'#title' => t('Front page'),
'#open' => TRUE,
);
$front_page = $site_config->get('page.front') != 'user' ? $this->aliasManager->getPathAlias($site_config->get('page.front')) : '';
$front_page = $site_config->get('page.front') != 'user' ? $this->aliasManager->getAliasByPath($site_config->get('page.front')) : '';
$form['front_page']['site_frontpage'] = array(
'#type' => 'textfield',
'#title' => t('Default front page'),
......@@ -139,7 +139,7 @@ public function validateForm(array &$form, array &$form_state) {
}
else {
// Get the normal path of the front page.
form_set_value($form['front_page']['site_frontpage'], $this->aliasManager->getSystemPath($form_state['values']['site_frontpage']), $form_state);
form_set_value($form['front_page']['site_frontpage'], $this->aliasManager->getPathByAlias($form_state['values']['site_frontpage']), $form_state);
}
// Validate front page path.
if (!drupal_valid_path($form_state['values']['site_frontpage'])) {
......@@ -147,10 +147,10 @@ public function validateForm(array &$form, array &$form_state) {
}
// Get the normal paths of both error pages.
if (!empty($form_state['values']['site_403'])) {
form_set_value($form['error_page']['site_403'], $this->aliasManager->getSystemPath($form_state['values']['site_403']), $form_state);
form_set_value($form['error_page']['site_403'], $this->aliasManager->getPathByAlias($form_state['values']['site_403']), $form_state);
}
if (!empty($form_state['values']['site_404'])) {
form_set_value($form['error_page']['site_404'], $this->aliasManager->getSystemPath($form_state['values']['site_404']), $form_state);
form_set_value($form['error_page']['site_404'], $this->aliasManager->getPathByAlias($form_state['values']['site_404']), $form_state);
}
// Validate 403 error path.
if (!empty($form_state['values']['site_403']) && !drupal_valid_path($form_state['values']['site_403'])) {
......
......@@ -95,8 +95,8 @@ function testLookupPath() {
);
$aliasStorage->save($path['source'], $path['alias']);
$this->assertEqual($aliasManager->getPathAlias($path['source']), $path['alias'], 'Basic alias lookup works.');
$this->assertEqual($aliasManager->getSystemPath($path['alias']), $path['source'], 'Basic source lookup works.');
$this->assertEqual($aliasManager->getAliasByPath($path['source']), $path['alias'], 'Basic alias lookup works.');
$this->assertEqual($aliasManager->getPathByAlias($path['alias']), $path['source'], 'Basic source lookup works.');
// Create a language specific alias for the default language (English).
$path = array(
......@@ -107,8 +107,8 @@ function testLookupPath() {
$aliasStorage->save($path['source'], $path['alias'], $path['langcode']);
// Hook that clears cache is not executed with unit tests.
\Drupal::service('path.alias_manager.cached')->cacheClear();
$this->assertEqual($aliasManager->getPathAlias($path['source']), $path['alias'], 'English alias overrides language-neutral alias.');
$this->assertEqual($aliasManager->getSystemPath($path['alias']), $path['source'], 'English source overrides language-neutral source.');
$this->assertEqual($aliasManager->getAliasByPath($path['source']), $path['alias'], 'English alias overrides language-neutral alias.');
$this->assertEqual($aliasManager->getPathByAlias($path['alias']), $path['source'], 'English source overrides language-neutral source.');
// Create a language-neutral alias for the same path, again.
$path = array(
......@@ -116,7 +116,7 @@ function testLookupPath() {
'alias' => 'bar',
);
$aliasStorage->save($path['source'], $path['alias']);
$this->assertEqual($aliasManager->getPathAlias($path['source']), "users/Dries", 'English alias still returned after entering a language-neutral alias.');
$this->assertEqual($aliasManager->getAliasByPath($path['source']), "users/Dries", 'English alias still returned after entering a language-neutral alias.');
// Create a language-specific (xx-lolspeak) alias for the same path.
$path = array(
......@@ -125,9 +125,9 @@ function testLookupPath() {
'langcode' => 'xx-lolspeak',
);
$aliasStorage->save($path['source'], $path['alias'], $path['langcode']);
$this->assertEqual($aliasManager->getPathAlias($path['source']), "users/Dries", 'English alias still returned after entering a LOLspeak alias.');
$this->assertEqual($aliasManager->getAliasByPath($path['source']), "users/Dries", 'English alias still returned after entering a LOLspeak alias.');
// The LOLspeak alias should be returned if we really want LOLspeak.
$this->assertEqual($aliasManager->getPathAlias($path['source'], 'xx-lolspeak'), 'LOL', 'LOLspeak alias returned if we specify xx-lolspeak to the alias manager.');
$this->assertEqual($aliasManager->getAliasByPath($path['source'], 'xx-lolspeak'), 'LOL', 'LOLspeak alias returned if we specify xx-lolspeak to the alias manager.');
// Create a new alias for this path in English, which should override the
// previous alias for "user/1".
......@@ -139,22 +139,22 @@ function testLookupPath() {
$aliasStorage->save($path['source'], $path['alias'], $path['langcode']);
// Hook that clears cache is not executed with unit tests.
$aliasManager->cacheClear();
$this->assertEqual($aliasManager->getPathAlias($path['source']), $path['alias'], 'Recently created English alias returned.');
$this->assertEqual($aliasManager->getSystemPath($path['alias']), $path['source'], 'Recently created English source returned.');
$this->assertEqual($aliasManager->getAliasByPath($path['source']), $path['alias'], 'Recently created English alias returned.');
$this->assertEqual($aliasManager->getPathByAlias($path['alias']), $path['source'], 'Recently created English source returned.');
// Remove the English aliases, which should cause a fallback to the most
// recently created language-neutral alias, 'bar'.
$aliasStorage->delete(array('langcode' => 'en'));
// Hook that clears cache is not executed with unit tests.
$aliasManager->cacheClear();
$this->assertEqual($aliasManager->getPathAlias($path['source']), 'bar', 'Path lookup falls back to recently created language-neutral alias.');
$this->assertEqual($aliasManager->getAliasByPath($path['source']), 'bar', 'Path lookup falls back to recently created language-neutral alias.');
// Test the situation where the alias and language are the same, but
// the source differs. The newer alias record should be returned.
$aliasStorage->save('user/2', 'bar');
// Hook that clears cache is not executed with unit tests.
$aliasManager->cacheClear();
$this->assertEqual($aliasManager->getSystemPath('bar'), 'user/2', 'Newer alias record is returned when comparing two Language::LANGCODE_NOT_SPECIFIED paths with the same alias.');
$this->assertEqual($aliasManager->getPathByAlias('bar'), 'user/2', 'Newer alias record is returned when comparing two Language::LANGCODE_NOT_SPECIFIED paths with the same alias.');
}
/**
......
......@@ -119,7 +119,7 @@ protected function assertUrlOutboundAlter($original, $final) {
*/
protected function assertUrlInboundAlter($original, $final) {
// Test inbound altering.
$result = $this->container->get('path.alias_manager')->getSystemPath($original);
$result = $this->container->get('path.alias_manager')->getPathByAlias($original);
$this->assertIdentical($result, $final, format_string('Altered inbound URL %original, expected %final, and got %result.', array('%original' => $original, '%final' => $final, '%result' => $result)));
}
}
......@@ -62,18 +62,21 @@ public function addAlias($path, $alias, $path_language = NULL) {
/**
* {@inheritdoc}
*/
public function getSystemPath($path, $path_language = NULL) {
$language = $path_language ?: $this->defaultLanguage;
return $this->systemPaths[$path][$language];
public function getPathByAlias($alias, $langcode = NULL) {
$langcode = $langcode ?: $this->defaultLanguage;
return $this->systemPaths[$alias][$langcode];
}
/**
* {@inheritdoc}
* @param $path
* @param null $langcode
* @return
*/
public function getPathAlias($path, $path_language = NULL) {
$language = $path_language ?: $this->defaultLanguage;
public function getAliasByPath($path, $langcode = NULL) {
$langcode = $langcode ?: $this->defaultLanguage;
$this->lookedUp[$path] = 1;
return $this->aliases[$path][$language];
return $this->aliases[$path][$langcode];
}
/**
......
......@@ -101,7 +101,7 @@ public function buildOptionsForm(&$form, &$form_state) {
public function getArgument() {
$path = $this->request->attributes->get('_system_path');
if ($this->options['use_alias']) {
$path = $this->aliasManager->getPathAlias($path);
$path = $this->aliasManager->getAliasByPath($path);
}
$args = explode('/', $path);
if (isset($args[$this->options['index']])) {
......
......@@ -42,7 +42,7 @@ public function testGetArgument() {
$request = new Request(array(), array(), array('_system_path' => 'test/example'));
$alias_manager = $this->getMock('Drupal\Core\Path\AliasManagerInterface');
$alias_manager->expects($this->never())
->method('getPathAlias');
->method('getAliasByPath');
// Don't use aliases.
$raw = new Raw(array(), 'raw', array(), $request, $alias_manager);
......@@ -64,7 +64,7 @@ public function testGetArgument() {
// Setup an alias manager with a path alias.
$alias_manager = $this->getMock('Drupal\Core\Path\AliasManagerInterface');
$alias_manager->expects($this->any())
->method('getPathAlias')