diff --git a/core/tests/Drupal/Tests/Core/Routing/CachedUrlGeneratorTest.php b/core/tests/Drupal/Tests/Core/Routing/CachedUrlGeneratorTest.php index 42dd4de..27c262d 100644 --- a/core/tests/Drupal/Tests/Core/Routing/CachedUrlGeneratorTest.php +++ b/core/tests/Drupal/Tests/Core/Routing/CachedUrlGeneratorTest.php @@ -9,10 +9,13 @@ use Drupal\Core\Routing\CachedUrlGenerator; use Drupal\Tests\UnitTestCase; +use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\RequestStack; /** * Tests the cache url generator. * + * @coversDefaultClass \Drupal\Core\Routing\CachedUrlGenerator * @group Routing */ class CachedUrlGeneratorTest extends UnitTestCase { @@ -60,6 +63,13 @@ class CachedUrlGeneratorTest extends UnitTestCase { protected $route; /** + * The log of all caches accessed. Keyed by cid. + * + * $var array + */ + protected $cacheIds = array(); + + /** * {@inheritdoc} */ protected function setUp() { @@ -181,5 +191,66 @@ public function testGenerateFromRouteWithDifferentParameters() { $this->assertEquals('http://localhost/test-route-1/value2', $this->cachedUrlGenerator->generateFromRoute('test_route', array('key' => 'value2'), array('absolute' => TRUE))); } + /** + * Tests that prepareCache's treatment of routes is case sensitive. + * i.e. Different caches are accessed for similar but distinctly named + * requests. + * + * For example, these URLs + * + * "/PAGE/A" + * "/PAGE/a" + * + * are not the same and that when processing the request the accumulation of + * generated URLs should be cached separately. + * + * @covers \Drupal\Core\Routing\CachedUrlGenerator::prepareCache() + */ + public function testPrepareCache(){ + + // Attach a monitor to capture the number of unique caches accessed. + $this->cache->expects($this->any()) + ->method('set') + ->will($this->returnCallback(array($this, 'logCacheIdentifiers'))); + + $requestStack = new RequestStack(); + + // For a lowercase request populate the array of URLs and then cache it. + $request1 = Request::create('/page/a'); + $request1->attributes->set('_system_path', '/page/a'); + $requestStack->push($request1); + $this->cachedUrlGenerator->prepareCache($requestStack); + $this->cachedUrlGenerator->generateFromRoute('test-route-1'); + $this->cachedUrlGenerator->destruct(); // write to cache + + // For a uppercase request populate the array of URLs and then cache it. + $request2 = Request::create('/PAGE/A'); + $request2->attributes->set('_system_path', '/PAGE/A'); + $requestStack->push($request2); + $this->cachedUrlGenerator->prepareCache($requestStack); + $this->cachedUrlGenerator->generateFromRoute('test-route-1'); + $this->cachedUrlGenerator->destruct(); // write to cache. + + // Verify that two uniquely named caches have been accessed - One for each request. + $this->assertEquals(count($this->cacheIds), 2, 'Two unique caches have been stored.'); + } + + /** + * Monitor the number of distinct caches that are accessed. + * + * Build up a list of unique cache identifiers as access to them is sought. + * + * The form of the set method being mocked :- + * + * set($cid, $data, $expire = Cache::PERMANENT, array $tags = array()); + */ + public function logCacheIdentifiers() { + $arg = func_get_args(); + + // cid is insensitive to the case of the identifier. + $cid = strtolower($arg[0]); + + $this->cacheIds[$cid] = 1; + } }