/** * User object. * * @return \Drupal\moodle\Sql\User */ public function user() { // Static cache of already retrieved user data. $data =& drupal_static(__METHOD__, array()); $user_cid = "moodle-user:{$this->user->id()}"; // If we do not have this user id in the static cache, check {cache_data}. if (!isset($data[$user_cid])) { $cache = $this->cacheBackend->get($user_cid); if ($cache && $cache->data && isset($cache->data[$user_cid])) { $data[$user_cid] = $cache->data[$user_cid]; } } // If nothing in the cache then retrieve it from the database. if (!isset($data[$user_cid])) { $user = new User(); $this->query(); $this->addFields(); $statement = $this->query->execute(); $statement->setFetchMode(\PDO::FETCH_INTO, $user); $data[$user_cid] = $statement->fetch(); // Store the results for a day. $this->cacheBackend->set($user_cid, $data, REQUEST_TIME + 86400); } return $data[$user_cid]; }
/** * {@inheritdoc} * * Cached by role, invalidated whenever permissions change. */ public function generate(AccountInterface $account) { // User 1 is the super user, and can always access all permissions. Use a // different, unique identifier for the hash. if ($account->id() == 1) { return $this->hash('is-super-user'); } $sorted_roles = $account->getRoles(); sort($sorted_roles); $role_list = implode(',', $sorted_roles); $cid = "user_permissions_hash:{$role_list}"; if ($static_cache = $this->static->get($cid)) { return $static_cache->data; } else { $tags = Cache::buildTags('config:user.role', $sorted_roles, '.'); if ($cache = $this->cache->get($cid)) { $permissions_hash = $cache->data; } else { $permissions_hash = $this->doGenerate($sorted_roles); $this->cache->set($cid, $permissions_hash, Cache::PERMANENT, $tags); } $this->static->set($cid, $permissions_hash, Cache::PERMANENT, $tags); } return $permissions_hash; }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->keyValueFactory = $this->prophesize(KeyValueFactoryInterface::class); $this->entityTypeManager = $this->prophesize(EntityTypeManagerInterface::class); $this->entityFieldManager = $this->prophesize(EntityFieldManagerInterface::class); $this->cacheBackend = $this->prophesize(CacheBackendInterface::class); $this->fieldDefinitionListener = new FieldDefinitionListener($this->entityTypeManager->reveal(), $this->entityFieldManager->reveal(), $this->keyValueFactory->reveal(), $this->cacheBackend->reveal()); }
/** * {@inheritdoc} */ protected function setUp() { parent::setUp(); $this->moduleHandler = $this->prophesize(ModuleHandlerInterface::class); $this->moduleHandler->getImplementations('entity_type_build')->willReturn([]); $this->moduleHandler->alter('entity_type', Argument::type('array'))->willReturn(NULL); $this->cacheBackend = $this->prophesize(CacheBackendInterface::class); $this->translationManager = $this->prophesize(TranslationInterface::class); $this->entityTypeManager = new TestEntityTypeManager(new \ArrayObject(), $this->moduleHandler->reveal(), $this->cacheBackend->reveal(), $this->translationManager->reveal(), $this->getClassResolverStub()); $this->discovery = $this->prophesize(DiscoveryInterface::class); $this->entityTypeManager->setDiscovery($this->discovery->reveal()); }
/** * Tests the destruct method. * * @covers ::destruct */ public function testDestruct() { $this->libraryDiscoveryParser->expects($this->once())->method('buildByExtension')->with('test')->will($this->returnValue($this->libraryData)); $lock_key = 'library_info:Drupal\\Core\\Cache\\CacheCollector'; $this->lock->expects($this->once())->method('acquire')->with($lock_key)->will($this->returnValue(TRUE)); $this->cache->expects($this->exactly(2))->method('get')->with('library_info')->will($this->returnValue(FALSE)); $this->cache->expects($this->once())->method('set')->with('library_info', array('test' => $this->libraryData), Cache::PERMANENT, array('library_info')); $this->lock->expects($this->once())->method('release')->with($lock_key); // This should get data and persist the key. $this->libraryDiscoveryCollector->get('test'); $this->libraryDiscoveryCollector->destruct(); }
/** * {@inheritdoc} * * Cached by role, invalidated whenever permissions change. */ public function generate(AccountInterface $account) { $sorted_roles = $account->getRoles(); sort($sorted_roles); $role_list = implode(',', $sorted_roles); if ($cache = $this->cache->get("user_permissions_hash:{$role_list}")) { $permissions_hash = $cache->data; } else { $permissions_hash = $this->doGenerate($sorted_roles); $this->cache->set("user_permissions_hash:{$role_list}", $permissions_hash, Cache::PERMANENT, array('user_role' => $sorted_roles)); } return $permissions_hash; }
/** * {@inheritdoc} * * Cached by role, invalidated whenever permissions change. */ public function generate(AccountInterface $account) { $sorted_roles = $account->getRoles(); sort($sorted_roles); $role_list = implode(',', $sorted_roles); if ($cache = $this->cache->get("user_permissions_hash:{$role_list}")) { $permissions_hash = $cache->data; } else { $permissions_hash = $this->doGenerate($sorted_roles); $tags = Cache::buildTags('config:user.role', $sorted_roles, '.'); $this->cache->set("user_permissions_hash:{$role_list}", $permissions_hash, Cache::PERMANENT, $tags); } return $permissions_hash; }
/** * Loads the base country definitions. * * @return array */ protected function loadBaseDefinitions() { if (!empty($this->baseDefinitions)) { return $this->baseDefinitions; } $cache_key = 'address.countries.base'; if ($cached = $this->cache->get($cache_key)) { $this->baseDefinitions = $cached->data; } else { $this->baseDefinitions = json_decode(file_get_contents($this->definitionPath . 'base.json'), TRUE); $this->cache->set($cache_key, $this->baseDefinitions, CacheBackendInterface::CACHE_PERMANENT, ['countries']); } return $this->baseDefinitions; }
/** * {@inheritdoc} */ public function onFieldDefinitionDelete(FieldDefinitionInterface $field_definition) { $entity_type_id = $field_definition->getTargetEntityTypeId(); $bundle = $field_definition->getTargetBundle(); $field_name = $field_definition->getName(); // Notify the storage about the field deletion. $this->entityTypeManager->getStorage($entity_type_id)->onFieldDefinitionDelete($field_definition); // Unset the bundle from the bundle field map key value collection. $bundle_field_map = $this->keyValueFactory->get('entity.definitions.bundle_field_map')->get($entity_type_id); unset($bundle_field_map[$field_name]['bundles'][$bundle]); if (empty($bundle_field_map[$field_name]['bundles'])) { // If there are no bundles left, remove the field from the map. unset($bundle_field_map[$field_name]); } $this->keyValueFactory->get('entity.definitions.bundle_field_map')->set($entity_type_id, $bundle_field_map); // Delete the cache entry. $this->cacheBackend->delete('entity_field_map'); // If the field map is initialized, update it as well, so that calls to it // do not have to rebuild it again. if ($field_map = $this->entityFieldManager->getFieldMap()) { unset($field_map[$entity_type_id][$field_name]['bundles'][$bundle]); if (empty($field_map[$entity_type_id][$field_name]['bundles'])) { unset($field_map[$entity_type_id][$field_name]); } $this->entityFieldManager->setFieldMap($field_map); } }
/** * @covers ::disable * @depends testSetStatus */ public function testDisable() { $this->cacheBackend->expects($this->once())->method('invalidateTags')->with(array($this->entityTypeId . ':' . $this->id)); $this->entity->setStatus(TRUE); $this->assertSame($this->entity, $this->entity->disable()); $this->assertFalse($this->entity->status()); }
/** * Tests the getInfo method. * * @covers ::getInfo * @covers ::buildInfo * * @dataProvider providerTestGetInfo */ public function testGetInfo($type, $expected_info, $element_info, callable $alter_callback = NULL) { $this->moduleHandler->expects($this->once())->method('invokeAll')->with('element_info')->will($this->returnValue($element_info)); $this->moduleHandler->expects($this->once())->method('alter')->with('element_info', $this->anything())->will($this->returnCallback($alter_callback ?: function ($info) { return $info; })); $this->themeManager->expects($this->once())->method('getActiveTheme')->willReturn(new ActiveTheme(['name' => 'test'])); $this->themeManager->expects($this->once())->method('alter')->with('element_info', $this->anything())->will($this->returnCallback($alter_callback ?: function ($info) { return $info; })); $this->cache->expects($this->at(0))->method('get')->with('element_info_build:test')->will($this->returnValue(FALSE)); $this->cache->expects($this->at(1))->method('get')->with('element_info')->will($this->returnValue(FALSE)); $this->cache->expects($this->at(2))->method('set')->with('element_info'); $this->cache->expects($this->at(3))->method('set')->with('element_info_build:test'); $this->assertEquals($expected_info, $this->elementInfo->getInfo($type)); }
/** * Test that the delete tags operation is propagated to all backends * in the chain. */ public function testDeleteTagsPropagation() { // Create two cache entries with the same tag and tag value. $this->chain->set('test_cid_clear1', 'foo', Cache::PERMANENT, array('test_tag:2')); $this->chain->set('test_cid_clear2', 'foo', Cache::PERMANENT, array('test_tag:2')); $this->assertNotSame(FALSE, $this->firstBackend->get('test_cid_clear1') && $this->firstBackend->get('test_cid_clear2') && $this->secondBackend->get('test_cid_clear1') && $this->secondBackend->get('test_cid_clear2') && $this->thirdBackend->get('test_cid_clear1') && $this->thirdBackend->get('test_cid_clear2'), 'Two cache items were created in all backends.'); // Invalidate test_tag of value 1. This should invalidate both entries. $this->chain->invalidateTags(array('test_tag:2')); $this->assertSame(FALSE, $this->firstBackend->get('test_cid_clear1') && $this->firstBackend->get('test_cid_clear2') && $this->secondBackend->get('test_cid_clear1') && $this->secondBackend->get('test_cid_clear2') && $this->thirdBackend->get('test_cid_clear1') && $this->thirdBackend->get('test_cid_clear2'), 'Two caches removed from all backends after clearing a cache tag.'); // Create two cache entries with the same tag and an array tag value. $this->chain->set('test_cid_clear1', 'foo', Cache::PERMANENT, array('test_tag:1')); $this->chain->set('test_cid_clear2', 'foo', Cache::PERMANENT, array('test_tag:1')); $this->assertNotSame(FALSE, $this->firstBackend->get('test_cid_clear1') && $this->firstBackend->get('test_cid_clear2') && $this->secondBackend->get('test_cid_clear1') && $this->secondBackend->get('test_cid_clear2') && $this->thirdBackend->get('test_cid_clear1') && $this->thirdBackend->get('test_cid_clear2'), 'Two cache items were created in all backends.'); // Invalidate test_tag of value 1. This should invalidate both entries. $this->chain->invalidateTags(array('test_tag:1')); $this->assertSame(FALSE, $this->firstBackend->get('test_cid_clear1') && $this->firstBackend->get('test_cid_clear2') && $this->secondBackend->get('test_cid_clear1') && $this->secondBackend->get('test_cid_clear2') && $this->thirdBackend->get('test_cid_clear1') && $this->thirdBackend->get('test_cid_clear2'), 'Two caches removed from all backends after clearing a cache tag.'); // Create three cache entries with a mix of tags and tag values. $this->chain->set('test_cid_clear1', 'foo', Cache::PERMANENT, array('test_tag:1')); $this->chain->set('test_cid_clear2', 'foo', Cache::PERMANENT, array('test_tag:2')); $this->chain->set('test_cid_clear3', 'foo', Cache::PERMANENT, array('test_tag_foo:3')); $this->assertNotSame(FALSE, $this->firstBackend->get('test_cid_clear1') && $this->firstBackend->get('test_cid_clear2') && $this->firstBackend->get('test_cid_clear3') && $this->secondBackend->get('test_cid_clear1') && $this->secondBackend->get('test_cid_clear2') && $this->secondBackend->get('test_cid_clear3') && $this->thirdBackend->get('test_cid_clear1') && $this->thirdBackend->get('test_cid_clear2') && $this->thirdBackend->get('test_cid_clear3'), 'Three cached items were created in all backends.'); $this->chain->invalidateTags(array('test_tag_foo:3')); $this->assertNotSame(FALSE, $this->firstBackend->get('test_cid_clear1') && $this->firstBackend->get('test_cid_clear2') && $this->secondBackend->get('test_cid_clear1') && $this->secondBackend->get('test_cid_clear2') && $this->thirdBackend->get('test_cid_clear1') && $this->thirdBackend->get('test_cid_clear2'), 'Cached items not matching the tag were not cleared from any of the backends.'); $this->assertSame(FALSE, $this->firstBackend->get('test_cid_clear3') && $this->secondBackend->get('test_cid_clear3') && $this->thirdBackend->get('test_cid_clear3'), 'Cached item matching the tag was removed from all backends.'); }
/** * Responds after a request has finished, but before it is sent to the client. * * @param \Guzzle\Common\Event $event * The Guzzle event object. */ public function onRequestSent(Event $event) { $request = $event['request']; $response = $event['response']; // Handle permanent redirects by setting the redirected URL so that the // client can grab it quickly. $redirect = FALSE; $url = $old_url = $request->getUrl(); if ($previous_response = $response->getPreviousResponse()) { if ($previous_response->getStatusCode() == 301 && ($location = $previous_response->getLocation())) { $response->getParams()->set('feeds.redirect', $location); $redirect = TRUE; $url = $request->getUrl(); } } $cache_hit = $response->getStatusCode() == 304; if ($redirect) { // Delete the old cache entry. $this->cacheBackend->delete($this->getCacheKey($old_url)); // Not sure if the repeated requests are smart enough to find the // redirect, so cache the old URL with the new response. static::$downloadCache[$old_url] = $response; } if ($redirect || !$cache_hit) { $cache = new \stdClass(); $cache->headers = array_change_key_case($response->getHeaders()->toArray()); // @todo We should only cache for certain status codes. $cache->code = $response->getStatusCode(); $this->cacheBackend->set($this->getCacheKey($url), $cache); } // Set in-page download cache. static::$downloadCache[$url] = $response; }
/** * Sets a cache of plugin definitions for the decorated discovery class. * * @param array $definitions * List of definitions to store in cache. */ protected function setCachedDefinitions($definitions) { if ($this->cacheBackend) { $this->cacheBackend->set($this->cacheKey, $definitions, Cache::PERMANENT, $this->cacheTags); } $this->definitions = $definitions; }
/** * 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(); }
/** * Provides information about modules' implementations of a hook. * * @param string $hook * The name of the hook (e.g. "help" or "menu"). * * @return mixed[] * An array whose keys are the names of the modules which are implementing * this hook and whose values are either a string identifying a file in * which the implementation is to be found, or FALSE, if the implementation * is in the module file. */ protected function getImplementationInfo($hook) { if (!isset($this->implementations)) { $this->implementations = array(); $this->verified = array(); if ($cache = $this->cacheBackend->get('module_implements')) { $this->implementations = $cache->data; } } if (!isset($this->implementations[$hook])) { // The hook is not cached, so ensure that whether or not it has // implementations, the cache is updated at the end of the request. $this->cacheNeedsWriting = TRUE; // Discover implementations. $this->implementations[$hook] = $this->buildImplementationInfo($hook); // Implementations are always "verified" as part of the discovery. $this->verified[$hook] = TRUE; } elseif (!isset($this->verified[$hook])) { if (!$this->verifyImplementations($this->implementations[$hook], $hook)) { // One or more of the implementations did not exist and need to be // removed in the cache. $this->cacheNeedsWriting = TRUE; } $this->verified[$hook] = TRUE; } return $this->implementations[$hook]; }
/** * Tests buildTree with simple menu_name and no parameters. */ public function testBuildTreeWithoutParameters() { $language = new Language(array('id' => 'en')); $this->languageManager->expects($this->any())->method('getCurrentLanguage')->will($this->returnValue($language)); // Setup query and the query result. $query = $this->getMock('Drupal\\Core\\Entity\\Query\\QueryInterface'); $this->entityQueryFactory->expects($this->once())->method('get')->with('menu_link')->will($this->returnValue($query)); $query->expects($this->once())->method('condition')->with('menu_name', 'test_menu'); $query->expects($this->once())->method('execute')->will($this->returnValue(array(1, 2, 3))); $storage = $this->getMock('Drupal\\Core\\Entity\\EntityStorageInterface'); $base = array('access' => TRUE, 'weight' => 0, 'title' => 'title'); $menu_link = $base + array('mlid' => 1, 'p1' => 3, 'p2' => 2, 'p3' => 1); $links[1] = $menu_link; $menu_link = $base + array('mlid' => 3, 'p1' => 3, 'depth' => 1); $links[3] = $menu_link; $menu_link = $base + array('mlid' => 2, 'p1' => 3, 'p2' => 2, 'depth' => 2); $links[2] = $menu_link; $storage->expects($this->once())->method('loadMultiple')->with(array(1, 2, 3))->will($this->returnValue($links)); $this->menuTree->setStorage($storage); // Ensure that static/non static caching works. // First setup no working caching. $this->cacheBackend->expects($this->at(0))->method('get')->with('links:test_menu:tree-data:en:35786c7117b4e38d0f169239752ce71158266ae2f6e4aa230fbbb87bd699c0e3')->will($this->returnValue(FALSE)); $this->cacheBackend->expects($this->at(1))->method('set')->with('links:test_menu:tree-data:en:35786c7117b4e38d0f169239752ce71158266ae2f6e4aa230fbbb87bd699c0e3', $this->anything(), Cache::PERMANENT, array('menu' => 'test_menu')); // Ensure that the static caching triggered. $this->cacheBackend->expects($this->exactly(1))->method('get'); $this->menuTree->buildTree('test_menu'); $this->menuTree->buildTree('test_menu'); }
/** * @covers ::delete() * @covers ::doDelete() */ public function testDeleteNothing() { $this->moduleHandler->expects($this->never())->method($this->anything()); $this->configFactory->expects($this->never())->method('get'); $this->cacheBackend->expects($this->never())->method('invalidateTags'); $this->entityStorage->delete(array()); }
/** * {@inheritdoc} */ public function loadTreeData($menu_name, MenuTreeParameters $parameters) { // Build the cache ID; sort 'expanded' and 'conditions' to prevent duplicate // cache items. sort($parameters->expandedParents); asort($parameters->conditions); $tree_cid = "tree-data:{$menu_name}:" . serialize($parameters); $cache = $this->menuCacheBackend->get($tree_cid); if ($cache && isset($cache->data)) { $data = $cache->data; // Cache the definitions in memory so they don't need to be loaded again. $this->definitions += $data['definitions']; unset($data['definitions']); } else { $links = $this->loadLinks($menu_name, $parameters); $data['tree'] = $this->doBuildTreeData($links, $parameters->activeTrail, $parameters->minDepth); $data['definitions'] = array(); $data['route_names'] = $this->collectRoutesAndDefinitions($data['tree'], $data['definitions']); $this->menuCacheBackend->set($tree_cid, $data, Cache::PERMANENT, ['config:system.menu.' . $menu_name]); // The definitions were already added to $this->definitions in // $this->doBuildTreeData() unset($data['definitions']); } return $data; }
/** * Tests the getAllBundleInfo() method. * * @covers ::getAllBundleInfo */ public function testGetAllBundleInfo() { $this->moduleHandler->invokeAll('entity_bundle_info')->willReturn([]); $this->moduleHandler->alter('entity_bundle_info', Argument::type('array'))->willReturn(NULL); $apple = $this->prophesize(EntityTypeInterface::class); $apple->getLabel()->willReturn('Apple'); $apple->getBundleOf()->willReturn(NULL); $banana = $this->prophesize(EntityTypeInterface::class); $banana->getLabel()->willReturn('Banana'); $banana->getBundleOf()->willReturn(NULL); $this->setUpEntityTypeDefinitions(['apple' => $apple, 'banana' => $banana]); $this->cacheBackend->get('entity_bundle_info:en')->willReturn(FALSE); $this->cacheBackend->set('entity_bundle_info:en', Argument::any(), Cache::PERMANENT, ['entity_types', 'entity_bundles'])->will(function () { $this->get('entity_bundle_info:en')->willReturn((object) ['data' => 'cached data'])->shouldBeCalled(); })->shouldBeCalled(); $this->cacheTagsInvalidator->invalidateTags(['entity_bundles'])->shouldBeCalled(); $this->typedDataManager->clearCachedDefinitions()->shouldBeCalled(); $expected = ['apple' => ['apple' => ['label' => 'Apple']], 'banana' => ['banana' => ['label' => 'Banana']]]; $bundle_info = $this->entityTypeBundleInfo->getAllBundleInfo(); $this->assertSame($expected, $bundle_info); $bundle_info = $this->entityTypeBundleInfo->getAllBundleInfo(); $this->assertSame($expected, $bundle_info); $this->entityTypeBundleInfo->clearCachedBundles(); $bundle_info = $this->entityTypeBundleInfo->getAllBundleInfo(); $this->assertSame('cached data', $bundle_info); }
/** * Builds the "format_tags" configuration part of the CKEditor JS settings. * * @see getConfig() * * @param \Drupal\editor\Entity\Editor $editor * A configured text editor object. * * @return array * An array containing the "format_tags" configuration. */ protected function generateFormatTagsSetting(Editor $editor) { // When no text format is associated yet, assume no tag is allowed. // @see \Drupal\Editor\EditorInterface::hasAssociatedFilterFormat() if (!$editor->hasAssociatedFilterFormat()) { return array(); } $format = $editor->getFilterFormat(); $cid = 'ckeditor_internal_format_tags:' . $format->id(); if ($cached = $this->cache->get($cid)) { $format_tags = $cached->data; } else { // The <p> tag is always allowed — HTML without <p> tags is nonsensical. $format_tags = ['p']; // Given the list of possible format tags, automatically determine whether // the current text format allows this tag, and thus whether it should show // up in the "Format" dropdown. $possible_format_tags = ['h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'pre']; foreach ($possible_format_tags as $tag) { $input = '<' . $tag . '>TEST</' . $tag . '>'; $output = trim(check_markup($input, $editor->id())); if ($input == $output) { $format_tags[] = $tag; } } $format_tags = implode(';', $format_tags); // Cache the "format_tags" configuration. This cache item is infinitely // valid; it only changes whenever the text format is changed, hence it's // tagged with the text format's cache tag. $this->cache->set($cid, $format_tags, Cache::PERMANENT, $format->getCacheTags()); } return $format_tags; }
/** * Provides information about modules' implementations of a hook. * * @param string $hook * The name of the hook (e.g. "help" or "menu"). * * @return array * An array whose keys are the names of the modules which are implementing * this hook and whose values are either an array of information from * hook_hook_info() or FALSE if the implementation is in the module file. */ protected function getImplementationInfo($hook) { if (!isset($this->implementations)) { $this->implementations = array(); if ($cache = $this->cacheBackend->get('module_implements')) { $this->implementations = $cache->data; } } if (!isset($this->implementations[$hook])) { // The hook is not cached, so ensure that whether or not it has // implementations, the cache is updated at the end of the request. $this->cacheNeedsWriting = TRUE; $this->implementations[$hook] = $this->buildImplementationInfo($hook); } else { foreach ($this->implementations[$hook] as $module => $group) { // If this hook implementation is stored in a lazy-loaded file, include // that file first. if ($group) { $this->loadInclude($module, 'inc', "{$module}.{$group}"); } // It is possible that a module removed a hook implementation without // the implementations cache being rebuilt yet, so we check whether the // function exists on each request to avoid undefined function errors. // Since ModuleHandler::implementsHook() may needlessly try to // load the include file again, function_exists() is used directly here. if (!function_exists($module . '_' . $hook)) { // Clear out the stale implementation from the cache and force a cache // refresh to forget about no longer existing hook implementations. unset($this->implementations[$hook][$module]); $this->cacheNeedsWriting = TRUE; } } } return $this->implementations[$hook]; }
/** * Invalidate the cache. */ protected function invalidateCache() { // Invalidate the cache to make sure that other requests immediately see the // deletion before this request is terminated. $this->cache->invalidate($this->getCid()); $this->cacheInvalidated = TRUE; }
/** * 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(); }
/** * @covers ::postDelete */ public function testPostDelete() { $this->cacheBackend->expects($this->once())->method('invalidateTags')->with(array($this->entityTypeId . ':' . $this->values['id'], $this->entityTypeId . '_list')); $storage = $this->getMock('\\Drupal\\Core\\Entity\\EntityStorageInterface'); $storage->expects($this->once())->method('getEntityType')->willReturn($this->entityType); $entities = array($this->values['id'] => $this->entity); $this->entity->postDelete($storage, $entities); }
/** * Tests the generate method with no cache returned. */ public function testGenerateNoCache() { // Set expectations for the mocked cache backend. $expected_cid = 'user_permissions_hash:administrator,authenticated'; $this->cache->expects($this->once())->method('get')->with($expected_cid)->will($this->returnValue(FALSE)); $this->cache->expects($this->once())->method('set')->with($expected_cid, $this->isType('string')); $this->permissionsHash->generate($this->account_1); }
/** * {@inheritdoc} */ public function getActiveThemeByName($theme_name) { if ($cached = $this->cache->get('theme.active_theme.' . $theme_name)) { return $cached->data; } $themes = $this->themeHandler->listInfo(); // If no theme could be negotiated, or if the negotiated theme is not within // the list of installed themes, fall back to the default theme output of // core and modules (like Stark, but without a theme extension at all). This // is possible, because loadActiveTheme() always loads the Twig theme // engine. This is desired, because missing or malformed theme configuration // should not leave the application in a broken state. By falling back to // default output, the user is able to reconfigure the theme through the UI. // Lastly, tests are expected to operate with no theme by default, so as to // only assert the original theme output of modules (unless a test manually // installs a specific theme). if (empty($themes) || !$theme_name || !isset($themes[$theme_name])) { $theme_name = 'core'; // /core/core.info.yml does not actually exist, but is required because // Extension expects a pathname. $active_theme = $this->getActiveTheme(new Extension($this->root, 'theme', 'core/core.info.yml')); // Early-return and do not set state, because the initialized $theme_name // differs from the original $theme_name. return $active_theme; } // Find all our ancestor themes and put them in an array. $base_themes = array(); $ancestor = $theme_name; while ($ancestor && isset($themes[$ancestor]->base_theme)) { $ancestor = $themes[$ancestor]->base_theme; if (!$this->themeHandler->themeExists($ancestor)) { if ($ancestor == 'stable') { // Themes that depend on Stable will be fixed by system_update_8014(). // There is no harm in not adding it as an ancestor since at worst // some people might experience slight visual regressions on // update.php. continue; } throw new MissingThemeDependencyException(sprintf('Base theme %s has not been installed.', $ancestor), $ancestor); } $base_themes[] = $themes[$ancestor]; } $active_theme = $this->getActiveTheme($themes[$theme_name], $base_themes); $this->cache->set('theme.active_theme.' . $theme_name, $active_theme); return $active_theme; }
/** * Asserts a render cache item. * * @param string $cid * The expected cache ID. * @param mixed $data * The expected data for that cache ID. */ protected function assertRenderCacheItem($cid, $data) { $cached = $this->memoryCache->get($cid); $this->assertNotFalse($cached, sprintf('Expected cache item "%s" exists.', $cid)); if ($cached !== FALSE) { $this->assertEquals($data, $cached->data, sprintf('Cache item "%s" has the expected data.', $cid)); $this->assertSame(Cache::mergeTags($data['#cache']['tags'], ['rendered']), $cached->tags, "The cache item's cache tags also has the 'rendered' cache tag."); } }
/** * {@inheritdoc} */ public function getTimestamp($key) { $cid = 'twig:' . $key; if ($cache = $this->cache->get($cid)) { return $cache->data; } else { return 0; } }
/** * @covers ::getFieldMap */ public function testGetFieldMapFromCache() { $expected = array('test_entity_type' => array('id' => array('type' => 'integer', 'bundles' => array('first_bundle', 'second_bundle')), 'by_bundle' => array('type' => 'string', 'bundles' => array('second_bundle')))); $this->setUpEntityManager(); $this->cacheBackend->expects($this->once())->method('get')->with('entity_field_map')->will($this->returnValue((object) array('data' => $expected))); // Call the field map twice to make sure the static cache works. $this->assertEquals($expected, $this->entityManager->getFieldMap()); $this->assertEquals($expected, $this->entityManager->getFieldMap()); }