/** * Tests the getAliasByPath cache with an unpreloaded path with alias. * * @covers ::getAliasByPath * @covers ::writeCache */ public function testGetAliasByPathUncachedMissWithAlias() { $path_part1 = $this->randomMachineName(); $path_part2 = $this->randomMachineName(); $path = $path_part1 . '/' . $path_part2; $cached_path = $this->randomMachineName(); $cached_no_alias_path = $this->randomMachineName(); $cached_alias = $this->randomMachineName(); $new_alias = $this->randomMachineName(); $language = $this->setUpCurrentLanguage(); $cached_paths = array($language->getId() => array($cached_path, $cached_no_alias_path)); $this->cache->expects($this->once())->method('get')->with($this->cacheKey)->will($this->returnValue((object) array('data' => $cached_paths))); // Simulate a request so that the preloaded paths are fetched. $this->aliasManager->setCacheKey($this->path); $this->aliasWhitelist->expects($this->any())->method('get')->with($path_part1)->will($this->returnValue(TRUE)); $this->aliasStorage->expects($this->once())->method('preloadPathAlias')->with($cached_paths[$language->getId()], $language->getId())->will($this->returnValue(array($cached_path => $cached_alias))); $this->aliasStorage->expects($this->once())->method('lookupPathAlias')->with($path, $language->getId())->will($this->returnValue($new_alias)); $this->assertEquals($new_alias, $this->aliasManager->getAliasByPath($path)); // Call it twice to test the static cache. $this->assertEquals($new_alias, $this->aliasManager->getAliasByPath($path)); // This needs to write out the cache. $expected_new_cache = array($language->getId() => array($cached_path, $path, $cached_no_alias_path)); $this->cache->expects($this->once())->method('set')->with($this->cacheKey, $expected_new_cache, (int) $_SERVER['REQUEST_TIME'] + 60 * 60 * 24); $this->aliasManager->writeCache(); }
/** * Tests the getAliasByPath cache with an unpreloaded path with alias. * * @covers ::getAliasByPath * @covers ::writeCache */ public function testGetAliasByPathUncachedMissWithAlias() { $path_part1 = $this->randomMachineName(); $path_part2 = $this->randomMachineName(); $path = '/' . $path_part1 . '/' . $path_part2; $cached_path = $this->randomMachineName(); $cached_no_alias_path = $this->randomMachineName(); $cached_alias = $this->randomMachineName(); $new_alias = $this->randomMachineName(); $language = $this->setUpCurrentLanguage(); $cached_paths = array($language->getId() => array($cached_path, $cached_no_alias_path)); $this->cache->expects($this->once())->method('get')->with($this->cacheKey)->will($this->returnValue((object) array('data' => $cached_paths))); // Simulate a request so that the preloaded paths are fetched. $this->aliasManager->setCacheKey($this->path); $this->aliasWhitelist->expects($this->any())->method('get')->with($path_part1)->will($this->returnValue(TRUE)); $this->aliasStorage->expects($this->once())->method('preloadPathAlias')->with($cached_paths[$language->getId()], $language->getId())->will($this->returnValue(array($cached_path => $cached_alias))); $this->aliasStorage->expects($this->once())->method('lookupPathAlias')->with($path, $language->getId())->will($this->returnValue($new_alias)); $this->assertEquals($new_alias, $this->aliasManager->getAliasByPath($path)); // Call it twice to test the static cache. $this->assertEquals($new_alias, $this->aliasManager->getAliasByPath($path)); // There is already a cache entry, so this should not write out to the // cache. $this->cache->expects($this->never())->method('set'); $this->aliasManager->writeCache(); }
/** * {@inheritdoc} */ public function setUp() { parent::setUp(); $container = new ContainerBuilder(); // Register plugin managers used by Rules, but mock some unwanted // dependencies requiring more stuff to loaded. $this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class); // Set all the modules as being existent. $this->enabledModules = new \ArrayObject(); $this->enabledModules['rules'] = TRUE; $this->enabledModules['rules_test'] = TRUE; $enabled_modules = $this->enabledModules; $this->moduleHandler->moduleExists(Argument::type('string'))->will(function ($arguments) use($enabled_modules) { return [$arguments[0], $enabled_modules[$arguments[0]]]; }); // Wed don't care about alter() calls on the module handler. $this->moduleHandler->alter(Argument::any(), Argument::any(), Argument::any(), Argument::any())->willReturn(NULL); $this->cacheBackend = new NullBackend('rules'); $rules_directory = __DIR__ . '/../../..'; $this->namespaces = new \ArrayObject(['Drupal\\rules' => $rules_directory . '/src', 'Drupal\\rules_test' => $rules_directory . '/tests/modules/rules_test/src', 'Drupal\\Core\\TypedData' => $this->root . '/core/lib/Drupal/Core/TypedData', 'Drupal\\Core\\Validation' => $this->root . '/core/lib/Drupal/Core/Validation']); $this->actionManager = new RulesActionManager($this->namespaces, $this->cacheBackend, $this->moduleHandler->reveal()); $this->conditionManager = new ConditionManager($this->namespaces, $this->cacheBackend, $this->moduleHandler->reveal()); $this->rulesExpressionManager = new ExpressionManager($this->namespaces, $this->moduleHandler->reveal()); $this->classResolver = $this->prophesize(ClassResolverInterface::class); $this->typedDataManager = new TypedDataManager($this->namespaces, $this->cacheBackend, $this->moduleHandler->reveal(), $this->classResolver->reveal()); $this->rulesDataProcessorManager = new DataProcessorManager($this->namespaces, $this->moduleHandler->reveal()); $this->aliasManager = $this->prophesize(AliasManagerInterface::class); // Keep the deprecated entity manager around because it is still used in a // few places. $this->entityManager = $this->prophesize(EntityManagerInterface::class); $this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class); $this->entityTypeManager->getDefinitions()->willReturn([]); $this->entityFieldManager = $this->prophesize(EntityFieldManagerInterface::class); $this->entityFieldManager->getBaseFieldDefinitions()->willReturn([]); $this->entityTypeBundleInfo = $this->prophesize(EntityTypeBundleInfoInterface::class); $this->entityTypeBundleInfo->getBundleInfo()->willReturn([]); $this->dataFetcher = new DataFetcher(); $this->dataFilterManager = new DataFilterManager($this->namespaces, $this->moduleHandler->reveal()); $this->placeholderResolver = new PlaceholderResolver($this->dataFetcher, $this->dataFilterManager); $container->set('entity.manager', $this->entityManager->reveal()); $container->set('entity_type.manager', $this->entityTypeManager->reveal()); $container->set('entity_field.manager', $this->entityFieldManager->reveal()); $container->set('entity_type.bundle.info', $this->entityTypeBundleInfo->reveal()); $container->set('context.repository', new LazyContextRepository($container, [])); $container->set('path.alias_manager', $this->aliasManager->reveal()); $container->set('plugin.manager.rules_action', $this->actionManager); $container->set('plugin.manager.condition', $this->conditionManager); $container->set('plugin.manager.rules_expression', $this->rulesExpressionManager); $container->set('plugin.manager.rules_data_processor', $this->rulesDataProcessorManager); $container->set('typed_data_manager', $this->typedDataManager); $container->set('string_translation', $this->getStringTranslationStub()); $container->set('uuid', new Php()); $container->set('typed_data.data_fetcher', $this->dataFetcher); $container->set('typed_data.placeholder_resolver', $this->placeholderResolver); \Drupal::setContainer($container); $this->container = $container; }
/** * Normalizes the path aliases. * * @param \Symfony\Component\HttpKernel\Event\GetResponseEvent $event */ public function globalredirectNormalizeAliases(GetResponseEvent $event) { if ($event->getRequestType() != HttpKernelInterface::MASTER_REQUEST || !$this->config->get('normalize_aliases') || !($path = trim($event->getRequest()->getPathInfo(), '/'))) { return; } $system_path = $this->aliasManager->getPathByAlias($path); $alias = $this->aliasManager->getAliasByPath($system_path, $this->languageManager->getCurrentLanguage()->getId()); // If the alias defined in the system is not the same as the one via which // the page has been accessed do a redirect to the one defined in the // system. if ($alias != $path) { if ($url = \Drupal::pathValidator()->getUrlIfValid($alias)) { $this->setResponse($event, $url); } } }
/** * 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); }