From 8af9f50ed42e117c7c1d7ddbad2008e243560e5f Mon Sep 17 00:00:00 2001
From: Nathaniel Catchpole <catch@35733.no-reply.drupal.org>
Date: Sun, 25 Aug 2013 21:23:22 +0100
Subject: [PATCH] Issue #2071655 by Berdir: Fixed AliasWhitelist implements
 CacheCollector incorrectly, resulting in cache write on every request.

---
 core/lib/Drupal/Core/Path/AliasWhitelist.php  |  7 +++++
 .../Drupal/system/Tests/Path/AliasTest.php    | 26 ++++++++++++++++++-
 2 files changed, 32 insertions(+), 1 deletion(-)

diff --git a/core/lib/Drupal/Core/Path/AliasWhitelist.php b/core/lib/Drupal/Core/Path/AliasWhitelist.php
index e0ba15b1ac2d..0138644af51c 100644
--- a/core/lib/Drupal/Core/Path/AliasWhitelist.php
+++ b/core/lib/Drupal/Core/Path/AliasWhitelist.php
@@ -53,6 +53,13 @@ public function __construct($cid, CacheBackendInterface $cache, LockBackendInter
     parent::__construct($cid, $cache, $lock);
     $this->state = $state;
     $this->connection = $connection;
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function lazyLoadCache() {
+    parent::lazyLoadCache();
 
     // On a cold start $this->storage will be empty and the whitelist will
     // need to be rebuilt from scratch. The whitelist is initialized from the
diff --git a/core/modules/system/lib/Drupal/system/Tests/Path/AliasTest.php b/core/modules/system/lib/Drupal/system/Tests/Path/AliasTest.php
index 37cc3e63e005..fed0ef494e75 100644
--- a/core/modules/system/lib/Drupal/system/Tests/Path/AliasTest.php
+++ b/core/modules/system/lib/Drupal/system/Tests/Path/AliasTest.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\system\Tests\Path;
 
+use Drupal\Core\Cache\MemoryCounterBackend;
 use Drupal\Core\Path\Path;
 use Drupal\Core\Database\Database;
 use Drupal\Core\Path\AliasManager;
@@ -158,8 +159,12 @@ function testWhitelist() {
     // Prepare database table.
     $connection = Database::getConnection();
     $this->fixtures->createTables($connection);
+
+    $memoryCounterBackend = new MemoryCounterBackend('cache');
+
     // Create AliasManager and Path object.
-    $whitelist = new AliasWhitelist('path_alias_whitelist', $this->container->get('cache.cache'), $this->container->get('lock'), $this->container->get('state'), $connection);
+
+    $whitelist = new AliasWhitelist('path_alias_whitelist', $memoryCounterBackend, $this->container->get('lock'), $this->container->get('state'), $connection);
     $aliasManager = new AliasManager($connection, $whitelist, $this->container->get('language_manager'));
     $path = new Path($connection, $aliasManager);
 
@@ -189,5 +194,24 @@ function testWhitelist() {
     $this->assertTrue($whitelist->get('admin'));
     $this->assertNull($whitelist->get($this->randomName()));
 
+    // Destruct the whitelist so that the caches are written.
+    $whitelist->destruct();
+    $this->assertEqual($memoryCounterBackend->getCounter('set', 'path_alias_whitelist'), 1);
+    $memoryCounterBackend->resetCounter();
+
+    // Re-initialize the whitelist using the same cache backend, should load
+    // from cache.
+    $whitelist = new AliasWhitelist('path_alias_whitelist', $memoryCounterBackend, $this->container->get('lock'), $this->container->get('state'), $connection);
+    $this->assertNull($whitelist->get('user'));
+    $this->assertTrue($whitelist->get('admin'));
+    $this->assertNull($whitelist->get($this->randomName()));
+    $this->assertEqual($memoryCounterBackend->getCounter('get', 'path_alias_whitelist'), 1);
+    $this->assertEqual($memoryCounterBackend->getCounter('set', 'path_alias_whitelist'), 0);
+
+    // Destruct the whitelist, should not attempt to write the cache again.
+    $whitelist->destruct();
+    $this->assertEqual($memoryCounterBackend->getCounter('get', 'path_alias_whitelist'), 1);
+    $this->assertEqual($memoryCounterBackend->getCounter('set', 'path_alias_whitelist'), 0);
   }
+
 }
-- 
GitLab