Issue #2344487 by tim.plunkett, dawehner: Provide special route names for <current> and <none>.

8.0.x
webchick 2014-09-26 23:47:52 -07:00
parent 17aac91777
commit 8bd63769fa
10 changed files with 275 additions and 2 deletions

View File

@ -819,6 +819,14 @@ services:
- { name: path_processor_inbound, priority: 200 }
- { name: path_processor_outbound, priority: 200 }
arguments: ['@config.factory']
path_processor_none:
class: Drupal\Core\PathProcessor\PathProcessorNone
tags:
- { name: path_processor_outbound, priority: 200 }
path_processor_current:
class: Drupal\Core\PathProcessor\PathProcessorCurrent
tags:
- { name: path_processor_outbound, priority: 200 }
path_processor_alias:
class: Drupal\Core\PathProcessor\PathProcessorAlias
tags:

View File

@ -187,6 +187,16 @@ class Drupal {
return static::$container->get('request_stack')->getCurrentRequest();
}
/**
* Retrives the request stack.
*
* @return \Symfony\Component\HttpFoundation\RequestStack
* The request stack
*/
public static function requestStack() {
return static::$container->get('request_stack');
}
/**
* Retrieves the currently active route match object.
*

View File

@ -0,0 +1,31 @@
<?php
/**
* @file
* Contains \Drupal\Core\PathProcessor\PathProcessorCurrent.
*/
namespace Drupal\Core\PathProcessor;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides a path processor to replace <current>.
*/
class PathProcessorCurrent implements OutboundPathProcessorInterface {
/**
* {@inheritdoc}
*/
public function processOutbound($path, &$options = array(), Request $request = NULL) {
if ($path == '%3Ccurrent%3E' && $request) {
$request_uri = $request->getRequestUri();
$current_base_path = $request->getBasePath() . '/';
return substr($request_uri, strlen($current_base_path));
}
return $path;
}
}

View File

@ -0,0 +1,27 @@
<?php
/**
* @file
* Contains \Drupal\Core\PathProcessor\PathProcessorNone.
*/
namespace Drupal\Core\PathProcessor;
use Symfony\Component\HttpFoundation\Request;
/**
* Provides a path processor to replace <none>.
*/
class PathProcessorNone implements OutboundPathProcessorInterface {
/**
* {@inheritdoc}
*/
public function processOutbound($path, &$options = array(), Request $request = NULL) {
if ($path == '%3Cnone%3E') {
return '';
}
return $path;
}
}

View File

@ -97,7 +97,7 @@ function contact_mail($key, &$message, $params) {
'!site-name' => \Drupal::config('system.site')->get('name'),
'!subject' => $contact_message->getSubject(),
'!form' => !empty($params['contact_form']) ? $params['contact_form']->label() : NULL,
'!form-url' => url(current_path(), array('absolute' => TRUE, 'language' => $language)),
'!form-url' => \Drupal::url('<current>', [], ['absolute' => TRUE, 'language' => $language]),
'!sender-name' => user_format_name($sender),
);
if ($sender->isAuthenticated()) {

View File

@ -49,7 +49,7 @@ function locale_test_locale_translation_projects_alter(&$projects) {
// Instead of the default ftp.drupal.org we use the file system of the test
// instance to simulate a remote file location.
$url = url(NULL, array('absolute' => TRUE));
$url = \Drupal::url('<none>', [], ['absolute' => TRUE]);
$remote_url = $url . PublicStream::basePath() . '/remote/';
// Completely replace the project data with a set of test projects.

View File

@ -0,0 +1,87 @@
<?php
/**
* @file
* Contains \Drupal\system\Tests\PathProcessor\PathProcessorIntegrationTest.
*/
namespace Drupal\system\Tests\PathProcessor;
use Drupal\simpletest\KernelTestBase;
use Symfony\Component\HttpFoundation\Request;
/**
* @see \Drupal\Core\PathProcessor\PathProcessorCurrent
* @group path_processor
*/
class PathProcessorCurrentIntegrationTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['system'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installSchema('system', ['router']);
\Drupal::service('router.builder')->rebuild();
}
/**
* Tests the output process.
*/
public function testProcessOutbound() {
$request_stack = \Drupal::requestStack();
/** @var \Symfony\Component\Routing\RequestContext $request_context */
$request_context = \Drupal::service('router.request_context');
// Test request with subdir on homepage.
$server = [
'SCRIPT_NAME' => '/subdir/index.php',
'SCRIPT_FILENAME' => DRUPAL_ROOT . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/subdir', 'GET', [], [], [], $server);
$request_stack->push($request);
$request_context->fromRequest($request);
$this->assertEqual('/subdir/', \Drupal::url('<current>'));
// Test request with subdir on other page.
$server = [
'SCRIPT_NAME' => '/subdir/index.php',
'SCRIPT_FILENAME' => DRUPAL_ROOT . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/subdir/node/add', 'GET', [], [], [], $server);
$request_stack->push($request);
$request_context->fromRequest($request);
$this->assertEqual('/subdir/node/add', \Drupal::url('<current>'));
// Test request without subdir on the homepage.
$server = [
'SCRIPT_NAME' => '/index.php',
'SCRIPT_FILENAME' => DRUPAL_ROOT . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/', 'GET', [], [], [], $server);
$request_stack->push($request);
$request_context->fromRequest($request);
$this->assertEqual('/', \Drupal::url('<current>'));
// Test request without subdir on other page.
$server = [
'SCRIPT_NAME' => '/index.php',
'SCRIPT_FILENAME' => DRUPAL_ROOT . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/node/add', 'GET', [], [], [], $server);
$request_stack->push($request);
$request_context->fromRequest($request);
$this->assertEqual('/node/add', \Drupal::url('<current>'));
}
}

View File

@ -0,0 +1,91 @@
<?php
/**
* @file
* Contains \Drupal\system\Tests\PathProcessor\PathProcessorNoneIntegrationTest.
*/
namespace Drupal\system\Tests\PathProcessor;
use Drupal\simpletest\KernelTestBase;
use Symfony\Component\HttpFoundation\Request;
/**
* @see \Drupal\Core\PathProcessor\PathProcessorNone
* @group path_processor
*/
class PathProcessorNoneIntegrationTest extends KernelTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['system'];
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installSchema('system', ['router']);
\Drupal::service('router.builder')->rebuild();
}
/**
* Tests the output process.
*/
public function testProcessOutbound() {
$request_stack = \Drupal::requestStack();
/** @var \Symfony\Component\Routing\RequestContext $request_context */
$request_context = \Drupal::service('router.request_context');
// Test request with subdir on homepage.
$server = [
'SCRIPT_NAME' => '/subdir/index.php',
'SCRIPT_FILENAME' => DRUPAL_ROOT . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/subdir', 'GET', [], [], [], $server);
$request_stack->push($request);
$request_context->fromRequest($request);
$this->assertEqual('/subdir/', \Drupal::url('<none>'));
$this->assertEqual('/subdir/#test-fragment', \Drupal::url('<none>', [], ['fragment' => 'test-fragment']));
// Test request with subdir on other page.
$server = [
'SCRIPT_NAME' => '/subdir/index.php',
'SCRIPT_FILENAME' => DRUPAL_ROOT . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/subdir/node/add', 'GET', [], [], [], $server);
$request_stack->push($request);
$request_context->fromRequest($request);
$this->assertEqual('/subdir/', \Drupal::url('<none>'));
$this->assertEqual('/subdir/#test-fragment', \Drupal::url('<none>', [], ['fragment' => 'test-fragment']));
// Test request without subdir on the homepage.
$server = [
'SCRIPT_NAME' => '/index.php',
'SCRIPT_FILENAME' => DRUPAL_ROOT . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/', 'GET', [], [], [], $server);
$request_stack->push($request);
$request_context->fromRequest($request);
$this->assertEqual('/', \Drupal::url('<none>'));
$this->assertEqual('/#test-fragment', \Drupal::url('<none>', [], ['fragment' => 'test-fragment']));
// Test request without subdir on other page.
$server = [
'SCRIPT_NAME' => '/index.php',
'SCRIPT_FILENAME' => DRUPAL_ROOT . '/index.php',
'SERVER_NAME' => 'http://www.example.com',
];
$request = Request::create('/node/add', 'GET', [], [], [], $server);
$request_stack->push($request);
$request_context->fromRequest($request);
$this->assertEqual('/', \Drupal::url('<none>'));
$this->assertEqual('/#test-fragment', \Drupal::url('<none>', [], ['fragment' => 'test-fragment']));
}
}

View File

@ -352,6 +352,12 @@ system.theme_settings_theme:
requirements:
_access: 'TRUE'
'<none>':
path: '<none>'
'<current>':
path: '<current>'
system.modules_uninstall:
path: '/admin/modules/uninstall'
defaults:

View File

@ -8,6 +8,7 @@
namespace Drupal\Tests\Core;
use Drupal\Tests\UnitTestCase;
use Symfony\Component\HttpFoundation\RequestStack;
/**
* Tests the Drupal class.
@ -131,6 +132,18 @@ class DrupalTest extends UnitTestCase {
$this->assertNotNull(\Drupal::queue('test_queue', TRUE));
}
/**
* Tests the testRequestStack() method.
*
* @covers ::requestStack
*/
public function testRequestStack() {
$request_stack = new RequestStack();
$this->setMockContainerService('request_stack', $request_stack);
$this->assertSame($request_stack, \Drupal::requestStack());
}
/**
* Tests the keyValue() method.
*/