/** * @covers ::cacheClear */ public function testCacheClear() { $path = 'path'; $alias = 'alias'; $language = $this->setUpCurrentLanguage(); $this->aliasStorage->expects($this->exactly(2))->method('lookupPathAlias')->with($path, $language->getId())->willReturn($alias); $this->aliasWhitelist->expects($this->any())->method('get')->willReturn(TRUE); // Populate the lookup map. $this->assertEquals($alias, $this->aliasManager->getAliasByPath($path, $language->getId())); // Check that the cache is populated. $original_storage = clone $this->aliasStorage; $this->aliasStorage->expects($this->never())->method('lookupPathSource'); $this->assertEquals($path, $this->aliasManager->getPathByAlias($alias, $language->getId())); // Clear specific source. $this->cache->expects($this->exactly(2))->method('delete'); $this->aliasManager->cacheClear($path); // Ensure cache has been cleared (this will be the 2nd call to // `lookupPathAlias` if cache is cleared). $this->assertEquals($alias, $this->aliasManager->getAliasByPath($path, $language->getId())); // Clear non-existent source. $this->aliasManager->cacheClear('non-existent'); }
/** * Tests the alias whitelist. */ function testWhitelist() { // Prepare database table. $connection = Database::getConnection(); $this->fixtures->createTables($connection); $memoryCounterBackend = new MemoryCounterBackend('default'); // Create AliasManager and Path object. $aliasStorage = new AliasStorage($connection, $this->container->get('module_handler')); $whitelist = new AliasWhitelist('path_alias_whitelist', $memoryCounterBackend, $this->container->get('lock'), $this->container->get('state'), $aliasStorage); $aliasManager = new AliasManager($aliasStorage, $whitelist, $this->container->get('language_manager'), $memoryCounterBackend); // No alias for user and admin yet, so should be NULL. $this->assertNull($whitelist->get('user')); $this->assertNull($whitelist->get('admin')); // Non-existing path roots should be NULL too. Use a length of 7 to avoid // possible conflict with random aliases below. $this->assertNull($whitelist->get($this->randomMachineName())); // Add an alias for user/1, user should get whitelisted now. $aliasStorage->save('user/1', $this->randomMachineName()); $aliasManager->cacheClear(); $this->assertTrue($whitelist->get('user')); $this->assertNull($whitelist->get('admin')); $this->assertNull($whitelist->get($this->randomMachineName())); // Add an alias for admin, both should get whitelisted now. $aliasStorage->save('admin/something', $this->randomMachineName()); $aliasManager->cacheClear(); $this->assertTrue($whitelist->get('user')); $this->assertTrue($whitelist->get('admin')); $this->assertNull($whitelist->get($this->randomMachineName())); // Remove the user alias again, whitelist entry should be removed. $aliasStorage->delete(array('source' => 'user/1')); $aliasManager->cacheClear(); $this->assertNull($whitelist->get('user')); $this->assertTrue($whitelist->get('admin')); $this->assertNull($whitelist->get($this->randomMachineName())); // 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'), $aliasStorage); $this->assertNull($whitelist->get('user')); $this->assertTrue($whitelist->get('admin')); $this->assertNull($whitelist->get($this->randomMachineName())); $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); }