From 8bd63769fafe7f7ff1bc47572d6efa9e4696bb70 Mon Sep 17 00:00:00 2001 From: webchick Date: Fri, 26 Sep 2014 23:47:52 -0700 Subject: [PATCH] Issue #2344487 by tim.plunkett, dawehner: Provide special route names for and . --- core/core.services.yml | 8 ++ core/lib/Drupal.php | 10 ++ .../PathProcessor/PathProcessorCurrent.php | 31 +++++++ .../Core/PathProcessor/PathProcessorNone.php | 27 ++++++ core/modules/contact/contact.module | 2 +- .../modules/locale_test/locale_test.module | 2 +- .../PathProcessorCurrentIntegrationTest.php | 87 ++++++++++++++++++ .../PathProcessorNoneIntegrationTest.php | 91 +++++++++++++++++++ core/modules/system/system.routing.yml | 6 ++ core/tests/Drupal/Tests/Core/DrupalTest.php | 13 +++ 10 files changed, 275 insertions(+), 2 deletions(-) create mode 100644 core/lib/Drupal/Core/PathProcessor/PathProcessorCurrent.php create mode 100644 core/lib/Drupal/Core/PathProcessor/PathProcessorNone.php create mode 100644 core/modules/system/src/Tests/PathProcessor/PathProcessorCurrentIntegrationTest.php create mode 100644 core/modules/system/src/Tests/PathProcessor/PathProcessorNoneIntegrationTest.php diff --git a/core/core.services.yml b/core/core.services.yml index f5ed5db9d74..8d22238b237 100644 --- a/core/core.services.yml +++ b/core/core.services.yml @@ -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: diff --git a/core/lib/Drupal.php b/core/lib/Drupal.php index d1025593bf3..b39663c9dac 100644 --- a/core/lib/Drupal.php +++ b/core/lib/Drupal.php @@ -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. * diff --git a/core/lib/Drupal/Core/PathProcessor/PathProcessorCurrent.php b/core/lib/Drupal/Core/PathProcessor/PathProcessorCurrent.php new file mode 100644 index 00000000000..8931462161d --- /dev/null +++ b/core/lib/Drupal/Core/PathProcessor/PathProcessorCurrent.php @@ -0,0 +1,31 @@ +. + */ +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; + } + +} + diff --git a/core/lib/Drupal/Core/PathProcessor/PathProcessorNone.php b/core/lib/Drupal/Core/PathProcessor/PathProcessorNone.php new file mode 100644 index 00000000000..2808ed1d671 --- /dev/null +++ b/core/lib/Drupal/Core/PathProcessor/PathProcessorNone.php @@ -0,0 +1,27 @@ +. + */ +class PathProcessorNone implements OutboundPathProcessorInterface { + + /** + * {@inheritdoc} + */ + public function processOutbound($path, &$options = array(), Request $request = NULL) { + if ($path == '%3Cnone%3E') { + return ''; + } + return $path; + } + +} diff --git a/core/modules/contact/contact.module b/core/modules/contact/contact.module index f1dfca7c8f0..90fb153b0c9 100644 --- a/core/modules/contact/contact.module +++ b/core/modules/contact/contact.module @@ -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('', [], ['absolute' => TRUE, 'language' => $language]), '!sender-name' => user_format_name($sender), ); if ($sender->isAuthenticated()) { diff --git a/core/modules/locale/tests/modules/locale_test/locale_test.module b/core/modules/locale/tests/modules/locale_test/locale_test.module index d9fd38207b4..ee5e7bfbb4e 100644 --- a/core/modules/locale/tests/modules/locale_test/locale_test.module +++ b/core/modules/locale/tests/modules/locale_test/locale_test.module @@ -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('', [], ['absolute' => TRUE]); $remote_url = $url . PublicStream::basePath() . '/remote/'; // Completely replace the project data with a set of test projects. diff --git a/core/modules/system/src/Tests/PathProcessor/PathProcessorCurrentIntegrationTest.php b/core/modules/system/src/Tests/PathProcessor/PathProcessorCurrentIntegrationTest.php new file mode 100644 index 00000000000..74388ed52f2 --- /dev/null +++ b/core/modules/system/src/Tests/PathProcessor/PathProcessorCurrentIntegrationTest.php @@ -0,0 +1,87 @@ +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('')); + + // 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('')); + + // 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('')); + + // 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('')); + } + +} diff --git a/core/modules/system/src/Tests/PathProcessor/PathProcessorNoneIntegrationTest.php b/core/modules/system/src/Tests/PathProcessor/PathProcessorNoneIntegrationTest.php new file mode 100644 index 00000000000..354131acd1e --- /dev/null +++ b/core/modules/system/src/Tests/PathProcessor/PathProcessorNoneIntegrationTest.php @@ -0,0 +1,91 @@ +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('')); + $this->assertEqual('/subdir/#test-fragment', \Drupal::url('', [], ['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('')); + $this->assertEqual('/subdir/#test-fragment', \Drupal::url('', [], ['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('')); + $this->assertEqual('/#test-fragment', \Drupal::url('', [], ['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('')); + $this->assertEqual('/#test-fragment', \Drupal::url('', [], ['fragment' => 'test-fragment'])); + } + +} diff --git a/core/modules/system/system.routing.yml b/core/modules/system/system.routing.yml index 6df0e0cca69..c2446d64d00 100644 --- a/core/modules/system/system.routing.yml +++ b/core/modules/system/system.routing.yml @@ -352,6 +352,12 @@ system.theme_settings_theme: requirements: _access: 'TRUE' +'': + path: '' + +'': + path: '' + system.modules_uninstall: path: '/admin/modules/uninstall' defaults: diff --git a/core/tests/Drupal/Tests/Core/DrupalTest.php b/core/tests/Drupal/Tests/Core/DrupalTest.php index 5af3c73a1c6..4ca6f09ce4f 100644 --- a/core/tests/Drupal/Tests/Core/DrupalTest.php +++ b/core/tests/Drupal/Tests/Core/DrupalTest.php @@ -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. */