diff --git a/core/lib/Drupal/Core/PathProcessor/PathProcessorAlias.php b/core/lib/Drupal/Core/PathProcessor/PathProcessorAlias.php index cfcf32e196d03ff3696e98cdc20a1e3cc2e43c8b..66979d1c699c6c9a9347d056bd3569eb0a32c814 100644 --- a/core/lib/Drupal/Core/PathProcessor/PathProcessorAlias.php +++ b/core/lib/Drupal/Core/PathProcessor/PathProcessorAlias.php @@ -44,8 +44,11 @@ public function processInbound($path, Request $request) { * Implements Drupal\Core\PathProcessor\OutboundPathProcessorInterface::processOutbound(). */ public function processOutbound($path, &$options = array(), Request $request = NULL) { - $langcode = isset($options['language']) ? $options['language']->id : NULL; - $path = $this->aliasManager->getPathAlias($path, $langcode); + if (empty($options['alias'])) { + $langcode = isset($options['language']) ? $options['language']->id : NULL; + $path = $this->aliasManager->getPathAlias($path, $langcode); + } return $path; } + } diff --git a/core/modules/path/lib/Drupal/path/Tests/PathAliasTest.php b/core/modules/path/lib/Drupal/path/Tests/PathAliasTest.php index 37012e23a7f85b3a4c29fbd56ab68a592760d1ec..184c9d9afa15ea677aae9601c16e5062b7809879 100644 --- a/core/modules/path/lib/Drupal/path/Tests/PathAliasTest.php +++ b/core/modules/path/lib/Drupal/path/Tests/PathAliasTest.php @@ -154,6 +154,12 @@ function testNodeAlias() { $this->assertText($node1->label(), 'Alias works.'); $this->assertResponse(200); + // Confirm the 'canonical' and 'shortlink' URLs. + $elements = $this->xpath("//link[contains(@rel, 'canonical') and contains(@href, '" . $edit['path[alias]'] . "')]"); + $this->assertTrue(!empty($elements), 'Page contains canonical link URL.'); + $elements = $this->xpath("//link[contains(@rel, 'shortlink') and contains(@href, 'node/" . $node1->id() . "')]"); + $this->assertTrue(!empty($elements), 'Page contains shortlink URL.'); + // Change alias to one containing "exotic" characters. $previous = $edit['path[alias]']; $edit['path[alias]'] = "- ._~!$'\"()*@[]?&+%#,;=:" . // "Special" ASCII characters. diff --git a/core/modules/path/lib/Drupal/path/Tests/PathTaxonomyTermTest.php b/core/modules/path/lib/Drupal/path/Tests/PathTaxonomyTermTest.php index 99c6fc24f237fccd07d82640d8d39f4d2527a950..c483d9e96325b3d9084d64094b4c97ec58775633 100644 --- a/core/modules/path/lib/Drupal/path/Tests/PathTaxonomyTermTest.php +++ b/core/modules/path/lib/Drupal/path/Tests/PathTaxonomyTermTest.php @@ -55,13 +55,19 @@ function testTermAlias() { 'path[alias]' => $this->randomName(), ); $this->drupalPostForm('admin/structure/taxonomy/manage/' . $vocabulary->id() . '/add', $edit, t('Save')); + $tid = db_query("SELECT tid FROM {taxonomy_term_data} WHERE name = :name", array(':name' => $edit['name']))->fetchField(); // Confirm that the alias works. $this->drupalGet($edit['path[alias]']); $this->assertText($description, 'Term can be accessed on URL alias.'); + // Confirm the 'canonical' and 'shortlink' URLs. + $elements = $this->xpath("//link[contains(@rel, 'canonical') and contains(@href, '" . $edit['path[alias]'] . "')]"); + $this->assertTrue(!empty($elements), 'Term page contains canonical link URL.'); + $elements = $this->xpath("//link[contains(@rel, 'shortlink') and contains(@href, 'taxonomy/term/" . $tid . "')]"); + $this->assertTrue(!empty($elements), 'Term page contains shortlink URL.'); + // Change the term's URL alias. - $tid = db_query("SELECT tid FROM {taxonomy_term_data} WHERE name = :name", array(':name' => $edit['name']))->fetchField(); $edit2 = array(); $edit2['path[alias]'] = $this->randomName(); $this->drupalPostForm('taxonomy/term/' . $tid . '/edit', $edit2, t('Save')); diff --git a/core/tests/Drupal/Tests/Core/PathProcessor/PathProcessorAliasTest.php b/core/tests/Drupal/Tests/Core/PathProcessor/PathProcessorAliasTest.php new file mode 100644 index 0000000000000000000000000000000000000000..40c8de0aaf4208f88595c580f0e5fa9eccf7456d --- /dev/null +++ b/core/tests/Drupal/Tests/Core/PathProcessor/PathProcessorAliasTest.php @@ -0,0 +1,89 @@ +<?php + +/** + * @file + * Contains \Drupal\Tests\Core\PathProcessor\PathProcessorAliasTest. + */ + +namespace Drupal\Tests\Core\PathProcessor; + +use Drupal\Core\PathProcessor\PathProcessorAlias; +use Drupal\Tests\UnitTestCase; +use Symfony\Component\HttpFoundation\Request; + +/** + * Tests the path alias path processor. + * + * @group Drupal + * + * @see \Drupal\Core\PathProcessor\PathProcessorAlias + */ +class PathProcessorAliasTest extends UnitTestCase { + + /** + * The mocked alias manager. + * + * @var \Drupal\Core\Path\AliasManagerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $aliasManager; + + /** + * The tested path processor. + * + * @var \Drupal\Core\PathProcessor\PathProcessorAlias + */ + protected $pathProcessor; + + public static function getInfo() { + return array( + 'name' => t('Path Processor alias'), + 'description' => t('Tests the path alias path processor.'), + 'group' => t('Path API'), + ); + } + + protected function setUp() { + $this->aliasManager = $this->getMock('Drupal\Core\Path\AliasManagerInterface'); + $this->pathProcessor = new PathProcessorAlias($this->aliasManager); + } + + /** + * Tests the processInbound method. + * + * @see \Drupal\Core\PathProcessor\PathProcessorAlias::processInbound + */ + public function testProcessInbound() { + $this->aliasManager->expects($this->exactly(2)) + ->method('getSystemPath') + ->will($this->returnValueMap(array( + array('urlalias', NULL, 'internal-url'), + array('url', NULL, 'url'), + ))); + + $request = Request::create('/urlalias'); + $this->assertEquals('internal-url', $this->pathProcessor->processInbound('urlalias', $request)); + $request = Request::create('/url'); + $this->assertEquals('url', $this->pathProcessor->processInbound('url', $request)); + } + + /** + * Tests the processOutbound method. + * + * @see \Drupal\Core\PathProcessor\PathProcessorAlias::processOutbound + */ + public function testProcessOutbound() { + $this->aliasManager->expects($this->exactly(2)) + ->method('getPathAlias') + ->will($this->returnValueMap(array( + array('internal-url', NULL, 'urlalias'), + array('url', NULL, 'url'), + ))); + + $this->assertEquals('urlalias', $this->pathProcessor->processOutbound('internal-url')); + $options = array('alias' => TRUE); + $this->assertEquals('internal-url', $this->pathProcessor->processOutbound('internal-url', $options)); + + $this->assertEquals('url', $this->pathProcessor->processOutbound('url')); + } + +}