diff --git a/core/lib/Drupal/Core/Routing/CachedUrlGenerator.php b/core/lib/Drupal/Core/Routing/CachedUrlGenerator.php index d6c5745..4ba98cb 100644 --- a/core/lib/Drupal/Core/Routing/CachedUrlGenerator.php +++ b/core/lib/Drupal/Core/Routing/CachedUrlGenerator.php @@ -139,7 +139,7 @@ public function generate($name, $parameters = array(), $absolute = FALSE) { * {@inheritdoc} */ public function generateFromPath($path = NULL, $options = array()) { - $key = self::PATH_CACHE_PREFIX . hash('sha256', $path . serialize($options)); + $key = static::PATH_CACHE_PREFIX . hash('sha256', $path . serialize($options)); if (!isset($this->cachedUrls[$key])) { $this->cachedUrls[$key] = $this->urlGenerator->generateFromPath($path, $options); $this->cacheNeedsWriting = TRUE; @@ -152,7 +152,7 @@ public function generateFromPath($path = NULL, $options = array()) { */ public function generateFromRoute($name, $parameters = array(), $options = array()) { // In some cases $name may be a Route object, rather than a string. - $key = self::ROUTE_CACHE_PREFIX . hash('sha256', serialize($name) . serialize($options) . serialize($parameters)); + $key = static::ROUTE_CACHE_PREFIX . hash('sha256', serialize($name) . serialize($options) . serialize($parameters)); if (!isset($this->cachedUrls[$key])) { $generated_url = $this->urlGenerator->generateFromRoute($name, $parameters, $options); $route = $this->getRoute($name); diff --git a/core/tests/Drupal/Tests/Core/Routing/CachedUrlGeneratorTest.php b/core/tests/Drupal/Tests/Core/Routing/CachedUrlGeneratorTest.php index 7fa538b..6242bb6 100644 --- a/core/tests/Drupal/Tests/Core/Routing/CachedUrlGeneratorTest.php +++ b/core/tests/Drupal/Tests/Core/Routing/CachedUrlGeneratorTest.php @@ -21,7 +21,7 @@ class CachedUrlGeneratorTest extends UnitTestCase { /** - * The wrapped url generator. + * The wrapped URL generator. * * @var \Drupal\Core\Routing\UrlGeneratorInterface|\PHPUnit_Framework_MockObject_MockObject */ @@ -63,7 +63,7 @@ class CachedUrlGeneratorTest extends UnitTestCase { protected $route; /** - *The request stack. + * The request stack. * * @var \Symfony\Component\HttpFoundation\RequestStack */ @@ -99,8 +99,9 @@ public function testGenerate() { ->method('generateFromRoute') ->with('test_route') ->will($this->returnValue('test-route-1')); + // First call will prime the cache. $this->assertEquals('test-route-1', $this->cachedUrlGenerator->generateFromRoute('test_route')); - // Second call will fetch from cache. + // Second call will fetch from the cache. $this->assertEquals('test-route-1', $this->cachedUrlGenerator->generateFromRoute('test_route')); } @@ -110,19 +111,24 @@ public function testGenerate() { * @see \Drupal\Core\Routing\CachedUrlGenerator::generate() */ public function testGenerateWithDifferentParameters() { + // We are generating URL's four times but since two of them are cached, + // we expect that generateFromRoute will only be called twice. $this->urlGenerator->expects($this->exactly(2)) ->method('generateFromRoute') ->will($this->returnValueMap(array( array('test_route', array('key' => 'value1'), array(), 'test-route-1/value1'), array('test_route', array('key' => 'value2'), array(), 'test-route-1/value2'), ))); + + // First call will prime the cache for key=value1. $this->assertEquals('test-route-1/value1', $this->cachedUrlGenerator->generate('test_route', array('key' => 'value1'))); - // Second call will fetch from cache. + // Second call will fetch from the cache. $this->assertEquals('test-route-1/value1', $this->cachedUrlGenerator->generate('test_route', array('key' => 'value1'))); - // Third call uses the same route with different parameters, so will call - // into the UrlGenerator mock. + // Third call uses the same route but since the parameters are different, + // the cache is not used and the generateFromRoute method will be called + // for the second time. $this->assertEquals('test-route-1/value2', $this->cachedUrlGenerator->generate('test_route', array('key' => 'value2'))); - // Fourth call will fetch from cache. + // Fourth call will fetch from the cache as it is now primed. $this->assertEquals('test-route-1/value2', $this->cachedUrlGenerator->generate('test_route', array('key' => 'value2'))); } @@ -136,8 +142,10 @@ public function testGenerateFromPath() { ->method('generateFromPath') ->with('test-route-1') ->will($this->returnValue('test-route-1')); + + // First call will prime the cache. $this->assertEquals('test-route-1', $this->cachedUrlGenerator->generateFromPath('test-route-1')); - // Second call will fetch from cache. + // Second call will fetch from the cache. $this->assertEquals('test-route-1', $this->cachedUrlGenerator->generateFromPath('test-route-1')); } @@ -147,17 +155,22 @@ public function testGenerateFromPath() { * @see \Drupal\Core\Routing\CachedUrlGenerator::generateFromPath() */ public function testGenerateFromPathWithDifferentParameters() { + // We are generating URL's four times but since two of them are cached, + // we expect that generateFromPath will only be called twice. $this->urlGenerator->expects($this->exactly(2)) ->method('generateFromPath') ->will($this->returnValueMap(array( array('test-route-1', array('absolute' => TRUE), 'http://localhost/test-route-1'), array('test-route-1', array('absolute' => FALSE), 'test-route-1'), ))); + + // First call will prime the cache. $this->assertEquals('http://localhost/test-route-1', $this->cachedUrlGenerator->generateFromPath('test-route-1', array('absolute' => TRUE))); // Second call will fetch from cache. $this->assertEquals('http://localhost/test-route-1', $this->cachedUrlGenerator->generateFromPath('test-route-1', array('absolute' => TRUE))); - // Third call uses the same route, with different parameters, so will call - // into the UrlGenerator mock. + // Third call uses the same path but since the options are different, the + // cache is not used and the generateFromPath method will be called for + // the second time. $this->assertEquals('test-route-1', $this->cachedUrlGenerator->generateFromPath('test-route-1', array('absolute' => FALSE))); // Fourth call will fetch from cache. $this->assertEquals('test-route-1', $this->cachedUrlGenerator->generateFromPath('test-route-1', array('absolute' => FALSE))); @@ -174,8 +187,10 @@ public function testGenerateFromRoute() { ->method('generateFromRoute') ->with('test_route') ->will($this->returnValue('test-route-1')); + + // First call will prime the cache. $this->assertEquals('test-route-1', $this->cachedUrlGenerator->generateFromRoute('test_route')); - // Second call will fetch from cache. + // Second call will fetch from the cache. $this->assertEquals('test-route-1', $this->cachedUrlGenerator->generateFromRoute('test_route')); } @@ -185,6 +200,8 @@ public function testGenerateFromRoute() { * @see \Drupal\Core\Routing\CachedUrlGenerator::generateFromRoute() */ public function testGenerateFromRouteWithDifferentParameters() { + // We are generating URL's eight times but since two of them are cached, + // we expect that generateFromRoute will only be called four times. $this->urlGenerator->expects($this->exactly(4)) ->method('generateFromRoute') ->will($this->returnValueMap(array( @@ -193,23 +210,28 @@ public function testGenerateFromRouteWithDifferentParameters() { array('test_route', array('key' => 'value2'), array(), 'test-route-1/value2'), array('test_route', array('key' => 'value2'), array('absolute' => TRUE), 'http://localhost/test-route-1/value2'), ))); + + // First call will prime the cache. $this->assertEquals('test-route-1/value1', $this->cachedUrlGenerator->generateFromRoute('test_route', array('key' => 'value1'))); - // Second call will fetch from cache. + // Second call will fetch from the cache. $this->assertEquals('test-route-1/value1', $this->cachedUrlGenerator->generateFromRoute('test_route', array('key' => 'value1'))); - // Third call uses the same route, with different parameters, so will call - // into the UrlGenerator mock. + // Third call uses the same route but since the options are different, the + // cache is not used and the generateFromRoute method will be called for + // the second time. $this->assertEquals('http://localhost/test-route-1/value1', $this->cachedUrlGenerator->generateFromRoute('test_route', array('key' => 'value1'), array('absolute' => TRUE))); - // Fourth call will fetch form cache. + // Fourth call will fetch from the cache. $this->assertEquals('http://localhost/test-route-1/value1', $this->cachedUrlGenerator->generateFromRoute('test_route', array('key' => 'value1'), array('absolute' => TRUE))); - // Fifth call uses the same route, with different parameters, so will call - // into the UrlGenerator mock. + // Fifth call uses the same route but since the parameters are different, + // the cache is not used and the generateFromRoute method will be called for + // the third time. $this->assertEquals('test-route-1/value2', $this->cachedUrlGenerator->generateFromRoute('test_route', array('key' => 'value2'))); - // Sixth call will fetch from cache. + // Sixth call will fetch from the cache. $this->assertEquals('test-route-1/value2', $this->cachedUrlGenerator->generateFromRoute('test_route', array('key' => 'value2'))); - // Seventh call uses the same route, with different parameters, so will call - // into the UrlGenerator mock. + // Seventh call uses the same route but since the options are different, the + // cache is not used and the generateFromRoute method will be called for + // the fourth time. $this->assertEquals('http://localhost/test-route-1/value2', $this->cachedUrlGenerator->generateFromRoute('test_route', array('key' => 'value2'), array('absolute' => TRUE))); - // Eighth call will fetch from cache. + // Eighth call will fetch from the cache. $this->assertEquals('http://localhost/test-route-1/value2', $this->cachedUrlGenerator->generateFromRoute('test_route', array('key' => 'value2'), array('absolute' => TRUE))); }