/**
  * 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();
 }
 /**
  * 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();
 }
Exemple #3
0
 /**
  * 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 ::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));
 }
 /**
  * @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());
 }
 /**
  * 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 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);
 }
 /**
  * @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);
 }
 /**
  * @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());
 }
 /**
  * @covers ::postDelete
  */
 public function testPostDelete()
 {
     $this->cacheBackend->expects($this->once())->method('invalidateTags')->with(array($this->entityTypeId => array($this->values['id']), $this->entityTypeId . 's' => TRUE));
     $storage = $this->getMock('\\Drupal\\Core\\Entity\\EntityStorageInterface');
     $entity = $this->getMockBuilder('\\Drupal\\Core\\Entity\\Entity')->setConstructorArgs(array($this->values, $this->entityTypeId))->setMethods(array('onSaveOrDelete'))->getMock();
     $entity->expects($this->once())->method('onSaveOrDelete');
     $entities = array($this->values['id'] => $entity);
     $this->entity->postDelete($storage, $entities);
 }
 /**
  * Tests the getContextualLinkPluginsByGroup method with a prefilled cache.
  */
 public function testGetContextualLinkPluginsByGroupWithCache()
 {
     $definitions = array('test_plugin1' => array('id' => 'test_plugin1', 'class' => '\\Drupal\\Core\\Menu\\ContextualLinkDefault', 'group' => 'group1', 'route_name' => 'test_route'), 'test_plugin2' => array('id' => 'test_plugin2', 'class' => '\\Drupal\\Core\\Menu\\ContextualLinkDefault', 'group' => 'group1', 'route_name' => 'test_route2'));
     $this->cacheBackend->expects($this->once())->method('get')->with('contextual_links_plugins:en:group1')->will($this->returnValue((object) array('data' => $definitions)));
     $result = $this->contextualLinkManager->getContextualLinkPluginsByGroup('group1');
     $this->assertEquals($definitions, $result);
     // Ensure that the static cache works, so no second cache get is executed.
     $result = $this->contextualLinkManager->getContextualLinkPluginsByGroup('group1');
     $this->assertEquals($definitions, $result);
 }
Exemple #13
0
 /**
  * @covers ::getInfo
  */
 public function testGetInfo()
 {
     $token_info = array('types' => array('foo' => array('name' => $this->randomMachineName())));
     $this->language->expects($this->atLeastOnce())->method('getId')->will($this->returnValue($this->randomMachineName()));
     $this->languageManager->expects($this->once())->method('getCurrentLanguage')->with(LanguageInterface::TYPE_CONTENT)->will($this->returnValue($this->language));
     // The persistent cache must only be hit once, after which the info is
     // cached statically.
     $this->cache->expects($this->once())->method('get');
     $this->cache->expects($this->once())->method('set')->with('token_info:' . $this->language->getId(), $token_info);
     $this->moduleHandler->expects($this->once())->method('invokeAll')->with('token_info')->will($this->returnValue($token_info));
     $this->moduleHandler->expects($this->once())->method('alter')->with('token_info', $token_info);
     // Get the information for the first time. The cache should be checked, the
     // hooks invoked, and the info should be set to the cache should.
     $this->token->getInfo();
     // Get the information for the second time. The data must be returned from
     // the static cache, so the persistent cache must not be accessed and the
     // hooks must not be invoked.
     $this->token->getInfo();
 }
Exemple #14
0
 /**
  * Tests that getting all data has same results as getting data with NULL
  * logic.
  *
  * @covers ::getAll
  */
 public function testGetAllEqualsToGetNull()
 {
     $expected_views_data = $this->viewsDataWithProvider();
     $this->setupMockedModuleHandler();
     // Setup a warm cache backend for a single table.
     $this->cacheBackend->expects($this->once())->method('get')->with("views_data:en");
     $this->cacheBackend->expects($this->once())->method('set')->with('views_data:en', $expected_views_data);
     // Initialize the views data cache and repeat with no specified table. This
     // should only load the cache entry for all tables.
     for ($i = 0; $i < 5; $i++) {
         $this->assertSame($expected_views_data, $this->viewsData->getAll());
         $this->assertSame($expected_views_data, $this->viewsData->get());
     }
 }
 /**
  * Test internal implementation cache reset.
  *
  * @covers ::resetImplementations()
  */
 public function testResetImplementations()
 {
     // Prime caches
     $this->moduleHandler->getImplementations('hook');
     $this->moduleHandler->getHookInfo();
     // Reset all caches internal and external.
     $this->cacheBackend->expects($this->once())->method('delete')->with('hook_info');
     $this->cacheBackend->expects($this->exactly(2))->method('set')->with($this->logicalOr('module_implements', 'hook_info'));
     $this->moduleHandler->resetImplementations();
     // Request implementation and ensure hook_info and module_implements skip
     // local caches.
     $this->cacheBackend->expects($this->exactly(2))->method('get')->with($this->logicalOr('module_implements', 'hook_info'));
     $this->moduleHandler->getImplementations('hook');
 }
 /**
  * Tests the destruct method.
  *
  * @covers ::destruct
  */
 public function testDestruct()
 {
     $this->activeTheme = $this->getMockBuilder('Drupal\\Core\\Theme\\ActiveTheme')->disableOriginalConstructor()->getMock();
     $this->themeManager->expects($this->once())->method('getActiveTheme')->willReturn($this->activeTheme);
     $this->activeTheme->expects($this->once())->method('getName')->willReturn('kitten_theme');
     $this->libraryDiscoveryCollector = new LibraryDiscoveryCollector($this->cache, $this->lock, $this->libraryDiscoveryParser, $this->themeManager);
     $this->libraryDiscoveryParser->expects($this->once())->method('buildByExtension')->with('test')->willReturn($this->libraryData);
     $lock_key = 'library_info:kitten_theme: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:kitten_theme')->willReturn(FALSE);
     $this->cache->expects($this->once())->method('set')->with('library_info:kitten_theme', array('test' => $this->libraryData), Cache::PERMANENT, ['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();
 }
Exemple #17
0
 /**
  * Tests the cache calls for multiple tables without warm caches.
  *
  * @covers ::get
  */
 public function testCacheCallsWithoutWarmCacheAndGetMultipleTables()
 {
     $expected_views_data = $this->viewsDataWithProvider();
     $table_name = 'views_test_data';
     $table_name_2 = 'views_test_data_2';
     // Setup a warm cache backend for all table data, but not single tables.
     $this->cacheBackend->expects($this->at(0))->method('get')->with("views_data:{$table_name}:en")->will($this->returnValue(FALSE));
     $this->cacheBackend->expects($this->at(1))->method('get')->with('views_data:en')->will($this->returnValue((object) array('data' => $expected_views_data)));
     $this->cacheBackend->expects($this->at(2))->method('set')->with("views_data:{$table_name}:en", $expected_views_data[$table_name]);
     $this->cacheBackend->expects($this->at(3))->method('get')->with("views_data:{$table_name_2}:en")->will($this->returnValue(FALSE));
     $this->cacheBackend->expects($this->at(4))->method('set')->with("views_data:{$table_name_2}:en", $expected_views_data[$table_name_2]);
     $this->assertSame($expected_views_data[$table_name], $this->viewsData->get($table_name));
     $this->assertSame($expected_views_data[$table_name_2], $this->viewsData->get($table_name_2));
     // Should only be invoked the first time.
     $this->assertSame($expected_views_data[$table_name], $this->viewsData->get($table_name));
     $this->assertSame($expected_views_data[$table_name_2], $this->viewsData->get($table_name_2));
 }
 /**
  * Tests a clear of the cache collector using tags.
  */
 public function testUpdateCacheClearTags()
 {
     $key = $this->randomMachineName();
     $value = $this->randomMachineName();
     $tags = array($this->randomMachineName());
     $this->collector = new CacheCollectorHelper($this->cid, $this->cacheBackend, $this->lock, $tags);
     // Set the data and request it.
     $this->collector->setCacheMissData($key, $value);
     $this->assertEquals($value, $this->collector->get($key));
     $this->assertEquals($value, $this->collector->get($key));
     // Should have been added to the storage and only be requested once.
     $this->assertEquals(1, $this->collector->getCacheMisses());
     // Clear the collected cache using the tags, should call it again.
     $this->cacheBackend->expects($this->never())->method('delete');
     $this->cacheTagsInvalidator->expects($this->once())->method('invalidateTags')->with($tags);
     $this->collector->clear();
     $this->assertEquals($value, $this->collector->get($key));
     $this->assertEquals(2, $this->collector->getCacheMisses());
 }
 /**
  * @covers ::doLoadMultiple
  * @covers ::buildCacheId
  * @covers ::getFromPersistentCache
  * @covers ::setPersistentCache
  */
 public function testLoadMultiplePersistentCacheMiss()
 {
     $this->setUpModuleHandlerNoImplementations();
     $id = 1;
     $entity = $this->getMockBuilder('\\Drupal\\Tests\\Core\\Entity\\Sql\\SqlContentEntityStorageTestEntityInterface')->getMockForAbstractClass();
     $entity->expects($this->any())->method('id')->will($this->returnValue($id));
     $this->entityType->expects($this->any())->method('isPersistentlyCacheable')->will($this->returnValue(TRUE));
     $this->entityType->expects($this->atLeastOnce())->method('id')->will($this->returnValue($this->entityTypeId));
     $this->entityType->expects($this->atLeastOnce())->method('getClass')->will($this->returnValue(get_class($entity)));
     // In case of a cache miss, the entity is loaded from the storage and then
     // set in the cache.
     $key = 'values:' . $this->entityTypeId . ':1';
     $this->cache->expects($this->once())->method('getMultiple')->with(array($key))->will($this->returnValue(array()));
     $this->cache->expects($this->once())->method('set')->with($key, $entity, CacheBackendInterface::CACHE_PERMANENT, array($this->entityTypeId . '_values', 'entity_field_info'));
     $entity_storage = $this->getMockBuilder('Drupal\\Core\\Entity\\Sql\\SqlContentEntityStorage')->setConstructorArgs(array($this->entityType, $this->connection, $this->entityManager, $this->cache))->setMethods(array('getFromStorage'))->getMock();
     $entity_storage->expects($this->once())->method('getFromStorage')->with(array($id))->will($this->returnValue(array($id => $entity)));
     $entities = $entity_storage->loadMultiple(array($id));
     $this->assertEquals($entity, $entities[$id]);
 }
 /**
  * Tests getCid()
  *
  * @covers ::getCid
  */
 public function testGetCid()
 {
     $data = $this->provider()[1];
     /** @var \Symfony\Component\HttpFoundation\Request $request */
     $request = $data[0];
     /** @var \Symfony\Component\Routing\Route $route */
     $route = $request->attributes->get(RouteObjectInterface::ROUTE_OBJECT);
     $route->setPath('/test/{b}/{a}');
     $request->attributes->get('_raw_variables')->add(['b' => 1, 'a' => 0]);
     $this->requestStack->push($request);
     $this->menuLinkManager->expects($this->any())->method('loadLinksbyRoute')->with('baby_llama')->will($this->returnValue($data[1]));
     $expected_link = $data[3];
     $expected_trail = $data[4];
     $expected_trail_ids = array_combine($expected_trail, $expected_trail);
     $this->menuLinkManager->expects($this->any())->method('getParentIds')->will($this->returnValueMap(array(array($expected_link->getPluginId(), $expected_trail_ids))));
     $this->assertSame($expected_trail_ids, $this->menuActiveTrail->getActiveTrailIds($data[2]));
     $this->cache->expects($this->once())->method('set')->with('active-trail:route:baby_llama:route_parameters:' . serialize(['a' => 0, 'b' => 1]));
     $this->lock->expects($this->any())->method('acquire')->willReturn(TRUE);
     $this->menuActiveTrail->destruct();
 }
Exemple #21
0
 /**
  * Tests locale lookups with fallback.
  *
  * Note that context is irrelevant here. It is not used but it is required.
  *
  * @covers ::resolveCacheMiss
  *
  * @dataProvider resolveCacheMissWithFallbackProvider
  */
 public function testResolveCacheMissWithFallback($langcode, $string, $context, $expected)
 {
     // These are fake words!
     $translations = array('en' => array('test' => 'test', 'fake' => 'fake', 'missing pl' => 'missing pl', 'missing cs' => 'missing cs', 'missing both' => 'missing both'), 'pl' => array('test' => 'test po polsku', 'fake' => 'ściema', 'missing cs' => 'zaginiony czech'), 'cs' => array('test' => 'test v české', 'fake' => 'falešný', 'missing pl' => 'chybějící pl'));
     $this->storage->expects($this->any())->method('findTranslation')->will($this->returnCallback(function ($argument) use($translations) {
         if (isset($translations[$argument['language']][$argument['source']])) {
             return (object) array('translation' => $translations[$argument['language']][$argument['source']]);
         }
         return TRUE;
     }));
     $this->languageManager->expects($this->any())->method('getFallbackCandidates')->will($this->returnCallback(function (array $context = array()) {
         switch ($context['langcode']) {
             case 'pl':
                 return array('cs', 'en');
             case 'cs':
                 return array('en');
             default:
                 return array();
         }
     }));
     $this->cache->expects($this->once())->method('get')->with('locale:' . $langcode . ':' . $context . ':anonymous', FALSE);
     $locale_lookup = new LocaleLookup($langcode, $context, $this->storage, $this->cache, $this->lock, $this->configFactory, $this->languageManager, $this->requestStack);
     $this->assertSame($expected, $locale_lookup->get($string));
 }