From 6172eecaa60def80f6f787be7fde71655d7b8622 Mon Sep 17 00:00:00 2001 From: Dries Date: Fri, 22 Mar 2013 11:28:58 -0400 Subject: [PATCH] Issue #1915752 by dawehner, tim.plunkett: routes are not found when 0 is used as a placeholder value. --- core/includes/common.inc | 4 ++- .../lib/Drupal/Core/Routing/RouteProvider.php | 6 +++- .../Tests/Routing/RouteProviderTest.php | 35 +++++++++++++++++++ .../system/Tests/Routing/RouterTest.php | 10 ++++-- 4 files changed, 50 insertions(+), 5 deletions(-) diff --git a/core/includes/common.inc b/core/includes/common.inc index 77619a09f21..60f57f296f5 100644 --- a/core/includes/common.inc +++ b/core/includes/common.inc @@ -5099,7 +5099,9 @@ function drupal_set_page_content($content = NULL) { $content_block = &drupal_static(__FUNCTION__, NULL); $main_content_display = &drupal_static('system_main_content_added', FALSE); - if (!empty($content)) { + // Filter out each empty value, though allow '0' and 0, which would be + // filtered out by empty(). + if ($content !== NULL && $content !== '') { $content_block = (is_array($content) ? $content : array('main' => array('#markup' => $content))); } else { diff --git a/core/lib/Drupal/Core/Routing/RouteProvider.php b/core/lib/Drupal/Core/Routing/RouteProvider.php index 8892866f35b..de6f3e6ea32 100644 --- a/core/lib/Drupal/Core/Routing/RouteProvider.php +++ b/core/lib/Drupal/Core/Routing/RouteProvider.php @@ -97,7 +97,11 @@ class RouteProvider implements RouteProviderInterface { $path = rtrim($request->getPathInfo(), '/'); } - $parts = array_slice(array_filter(explode('/', $path)), 0, MatcherDumper::MAX_PARTS); + // Filter out each empty value, though allow '0' and 0, which would be + // filtered out by empty(). + $parts = array_slice(array_filter(explode('/', $path), function($value) { + return $value !== NULL && $value !== ''; + }), 0, MatcherDumper::MAX_PARTS); $ancestors = $this->getCandidateOutlines($parts); diff --git a/core/modules/system/lib/Drupal/system/Tests/Routing/RouteProviderTest.php b/core/modules/system/lib/Drupal/system/Tests/Routing/RouteProviderTest.php index 6dde88204dd..3191b99836d 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Routing/RouteProviderTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Routing/RouteProviderTest.php @@ -271,6 +271,41 @@ class RouteProviderTest extends UnitTestBase { } } + /** + * Tests a route with a 0 as value. + */ + public function testOutlinePathMatchZero() { + $connection = Database::getConnection(); + $provider = new RouteProvider($connection, 'test_routes'); + + $this->fixtures->createTables($connection); + + $collection = new RouteCollection(); + $collection->add('poink', new Route('/some/path/{value}')); + + $dumper = new MatcherDumper($connection, 'test_routes'); + $dumper->addRoutes($collection); + $dumper->dump(); + + $path = '/some/path/0'; + + $request = Request::create($path, 'GET'); + + try { + $routes = $provider->getRouteCollectionForRequest($request); + + // All of the matching paths have the correct pattern. + foreach ($routes as $route) { + $this->assertEqual($route->compile()->getPatternOutline(), '/some/path/%', 'Found path has correct pattern'); + } + + $this->assertEqual(count($routes), 1, 'The correct number of routes was found.'); + } + catch (ResourceNotFoundException $e) { + $this->fail('No matchout route found with 0 as argument value'); + } + } + /** * Confirms that an exception is thrown when no matching path is found. */ diff --git a/core/modules/system/lib/Drupal/system/Tests/Routing/RouterTest.php b/core/modules/system/lib/Drupal/system/Tests/Routing/RouterTest.php index e4611354fa9..8a3f45f0d7e 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Routing/RouterTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Routing/RouterTest.php @@ -57,9 +57,13 @@ class RouterTest extends WebTestBase { * Confirms that placeholders in paths work correctly. */ public function testControllerPlaceholders() { - $value = $this->randomName(); - $this->drupalGet('router_test/test3/' . $value); - $this->assertRaw($value, 'The correct string was returned because the route was successful.'); + // Test with 0 and a random value. + $values = array("0", $this->randomName()); + foreach ($values as $value) { + $this->drupalGet('router_test/test3/' . $value); + $this->assertResponse(200); + $this->assertRaw($value, 'The correct string was returned because the route was successful.'); + } // Confirm that the page wrapping is being added, so we're not getting a // raw body returned.