diff --git a/core/modules/book/src/BookManager.php b/core/modules/book/src/BookManager.php index 3f5cfdc..e1f4282 100644 --- a/core/modules/book/src/BookManager.php +++ b/core/modules/book/src/BookManager.php @@ -433,7 +433,7 @@ public function deleteFromBook($nid) { } $this->updateOriginalParent($original); $this->books = NULL; - \Drupal::cache('data')->deleteTags(array('bid:' . $original['bid'])); + Cache::deleteTags(array('bid:' . $original['bid'])); } /** @@ -763,7 +763,7 @@ public function saveBookLink(array $link, $new) { foreach ($affected_bids as $bid) { $cache_tags[] = 'bid:' . $bid; } - \Drupal::cache('data')->deleteTags($cache_tags); + Cache::deleteTags($cache_tags); return $link; } diff --git a/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php b/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php index bd2952e..518383b 100644 --- a/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php +++ b/core/modules/system/src/Tests/Cache/DatabaseBackendUnitTest.php @@ -30,7 +30,7 @@ class DatabaseBackendUnitTest extends GenericCacheBackendUnitTestBase { * A new DatabaseBackend object. */ protected function createCacheBackend($bin) { - return new DatabaseBackend($this->container->get('database'), $bin); + return new DatabaseBackend($this->container->get('database'), $this->container->get('cache_tag_storage'), $bin); } } diff --git a/core/modules/views/tests/src/Unit/ViewsDataTest.php b/core/modules/views/tests/src/Unit/ViewsDataTest.php index 0476ffd..4bd856d 100644 --- a/core/modules/views/tests/src/Unit/ViewsDataTest.php +++ b/core/modules/views/tests/src/Unit/ViewsDataTest.php @@ -26,6 +26,13 @@ class ViewsDataTest extends UnitTestCase { protected $cacheBackend; /** + * The mocked cache tag handler. + * + * @var \Drupal\Core\Cache\CacheTagHandlerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $cacheTagHandler; + + /** * The mocked module handler. * * @var \Drupal\Core\Extension\ModuleHandlerInterface|\PHPUnit_Framework_MockObject_MockObject @@ -57,8 +64,9 @@ class ViewsDataTest extends UnitTestCase { * {@inheritdoc} */ protected function setUp() { + $this->cacheTagHandler = $this->getMock('Drupal\Core\Cache\CacheTagHandlerInterface'); $this->cacheBackend = $this->getMock('Drupal\Core\Cache\CacheBackendInterface'); - $this->getContainerWithCacheBins($this->cacheBackend); + $this->getContainerWithCacheBins($this->cacheTagHandler); $configs = array(); $configs['views.settings']['skip_cache'] = FALSE; @@ -250,20 +258,21 @@ public function testFullAndTableGetCache() { $this->cacheBackend->expects($this->at(3)) ->method('set') ->with("views_data:$random_table_name:en", array()); + $this->cacheTagHandler->expects($this->once()) + ->method('deleteTags') + ->with(['views_data']); $this->cacheBackend->expects($this->at(4)) - ->method('deleteAll'); + ->method('get') + ->with("views_data:en") + ->will($this->returnValue(FALSE)); $this->cacheBackend->expects($this->at(5)) - ->method('get') - ->with("views_data:en") - ->will($this->returnValue(FALSE)); + ->method('set') + ->with("views_data:en", $expected_views_data); $this->cacheBackend->expects($this->at(6)) - ->method('set') - ->with("views_data:en", $expected_views_data); + ->method('get') + ->with("views_data:$random_table_name:en") + ->will($this->returnValue(FALSE)); $this->cacheBackend->expects($this->at(7)) - ->method('get') - ->with("views_data:$random_table_name:en") - ->will($this->returnValue(FALSE)); - $this->cacheBackend->expects($this->at(8)) ->method('set') ->with("views_data:$random_table_name:en", array()); diff --git a/core/tests/Drupal/Tests/Core/Cache/CacheCollectorTest.php b/core/tests/Drupal/Tests/Core/Cache/CacheCollectorTest.php index d8744d8..2b53e0a 100644 --- a/core/tests/Drupal/Tests/Core/Cache/CacheCollectorTest.php +++ b/core/tests/Drupal/Tests/Core/Cache/CacheCollectorTest.php @@ -21,7 +21,14 @@ class CacheCollectorTest extends UnitTestCase { * * @var \PHPUnit_Framework_MockObject_MockObject */ - protected $cache; + protected $cacheBackend; + + /** + * The cache tag handler. + * + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $cacheTagHandler; /** * The lock backend that should be used. @@ -48,12 +55,13 @@ class CacheCollectorTest extends UnitTestCase { * {@inheritdoc} */ protected function setUp() { - $this->cache = $this->getMock('Drupal\Core\Cache\CacheTagHandlerInterface'); + $this->cacheBackend = $this->getMock('Drupal\Core\Cache\CacheBackendInterface'); + $this->cacheTagHandler = $this->getMock('Drupal\Core\Cache\CacheTagHandlerInterface'); $this->lock = $this->getMock('Drupal\Core\Lock\LockBackendInterface'); $this->cid = $this->randomMachineName(); - $this->collector = new CacheCollectorHelper($this->cid, $this->cache, $this->lock); + $this->collector = new CacheCollectorHelper($this->cid, $this->cacheBackend, $this->lock); - $this->getContainerWithCacheBins($this->cache); + $this->getContainerWithCacheBins($this->cacheTagHandler); } @@ -90,7 +98,7 @@ public function testSetAndGetNull() { $key = $this->randomMachineName(); $value = NULL; - $this->cache->expects($this->once()) + $this->cacheBackend->expects($this->once()) ->method('invalidate') ->with($this->cid); $this->collector->set($key, $value); @@ -115,7 +123,7 @@ public function testGetFromCache() { 'data' => array($key => $value), 'created' => REQUEST_TIME, ); - $this->cache->expects($this->once()) + $this->cacheBackend->expects($this->once()) ->method('get') ->with($this->cid) ->will($this->returnValue($cache)); @@ -137,7 +145,7 @@ public function testDelete() { $this->assertTrue($this->collector->has($key)); $this->assertEquals($value, $this->collector->get($key)); - $this->cache->expects($this->once()) + $this->cacheBackend->expects($this->once()) ->method('invalidate') ->with($this->cid); $this->collector->delete($key); @@ -151,7 +159,7 @@ public function testDelete() { public function testUpdateCacheNoChanges() { $this->lock->expects($this->never()) ->method('acquire'); - $this->cache->expects($this->never()) + $this->cacheBackend->expects($this->never()) ->method('set'); // Destruct the object to trigger the update data process. @@ -175,10 +183,10 @@ public function testUpdateCache() { ->method('acquire') ->with($this->cid . ':Drupal\Core\Cache\CacheCollector') ->will($this->returnValue(TRUE)); - $this->cache->expects($this->once()) + $this->cacheBackend->expects($this->once()) ->method('get') ->with($this->cid, FALSE); - $this->cache->expects($this->once()) + $this->cacheBackend->expects($this->once()) ->method('set') ->with($this->cid, array($key => $value), Cache::PERMANENT, array()); $this->lock->expects($this->once()) @@ -205,7 +213,7 @@ public function testUpdateCacheLockFail() { ->method('acquire') ->with($this->cid . ':Drupal\Core\Cache\CacheCollector') ->will($this->returnValue(FALSE)); - $this->cache->expects($this->never()) + $this->cacheBackend->expects($this->never()) ->method('set'); // Destruct the object to trigger the update data process. @@ -223,12 +231,12 @@ public function testUpdateCacheInvalidatedConflict() { 'data' => array($key => $value), 'created' => REQUEST_TIME, ); - $this->cache->expects($this->at(0)) + $this->cacheBackend->expects($this->at(0)) ->method('get') ->with($this->cid) ->will($this->returnValue($cache)); - $this->cache->expects($this->at(1)) + $this->cacheBackend->expects($this->at(1)) ->method('invalidate') ->with($this->cid); $this->collector->set($key, 'new value'); @@ -244,11 +252,11 @@ public function testUpdateCacheInvalidatedConflict() { 'data' => array($key => $value), 'created' => REQUEST_TIME + 1, ); - $this->cache->expects($this->at(0)) + $this->cacheBackend->expects($this->at(0)) ->method('get') ->with($this->cid) ->will($this->returnValue($cache)); - $this->cache->expects($this->once()) + $this->cacheBackend->expects($this->once()) ->method('delete') ->with($this->cid); $this->lock->expects($this->once()) @@ -280,11 +288,11 @@ public function testUpdateCacheMerge() { 'data' => array('other key' => 'other value'), 'created' => REQUEST_TIME + 1, ); - $this->cache->expects($this->at(0)) + $this->cacheBackend->expects($this->at(0)) ->method('get') ->with($this->cid) ->will($this->returnValue($cache)); - $this->cache->expects($this->once()) + $this->cacheBackend->expects($this->once()) ->method('set') ->with($this->cid, array('other key' => 'other value', $key => $value), Cache::PERMANENT, array()); $this->lock->expects($this->once()) @@ -306,7 +314,7 @@ public function testUpdateCacheDelete() { 'data' => array($key => $value), 'created' => REQUEST_TIME, ); - $this->cache->expects($this->at(0)) + $this->cacheBackend->expects($this->at(0)) ->method('get') ->with($this->cid) ->will($this->returnValue($cache)); @@ -322,10 +330,10 @@ public function testUpdateCacheDelete() { ->will($this->returnValue(TRUE)); // The second argument is set to TRUE because we triggered a cache // invalidation. - $this->cache->expects($this->at(0)) + $this->cacheBackend->expects($this->at(0)) ->method('get') ->with($this->cid, TRUE); - $this->cache->expects($this->once()) + $this->cacheBackend->expects($this->once()) ->method('set') ->with($this->cid, array(), Cache::PERMANENT, array()); $this->lock->expects($this->once()) @@ -373,10 +381,10 @@ public function testUpdateCacheClear() { $this->assertEquals(1, $this->collector->getCacheMisses()); // Clear the collected cache, should call it again. - $this->cache->expects($this->once()) + $this->cacheBackend->expects($this->once()) ->method('delete') ->with($this->cid); - $this->cache->expects($this->never()) + $this->cacheTagHandler->expects($this->never()) ->method('deleteTags'); $this->collector->clear(); $this->assertEquals($value, $this->collector->get($key)); @@ -390,7 +398,7 @@ public function testUpdateCacheClearTags() { $key = $this->randomMachineName(); $value = $this->randomMachineName(); $tags = array($this->randomMachineName()); - $this->collector = new CacheCollectorHelper($this->cid, $this->cache, $this->lock, $tags); + $this->collector = new CacheCollectorHelper($this->cid, $this->cacheBackend, $this->lock, $tags); // Set the data and request it. $this->collector->setCacheMissData($key, $value); @@ -401,9 +409,9 @@ public function testUpdateCacheClearTags() { $this->assertEquals(1, $this->collector->getCacheMisses()); // Clear the collected cache using the tags, should call it again. - $this->cache->expects($this->never()) + $this->cacheBackend->expects($this->never()) ->method('delete'); - $this->cache->expects($this->once()) + $this->cacheTagHandler->expects($this->once()) ->method('deleteTags') ->with($tags); $this->collector->clear(); diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php index beaa312..cf7a2f4 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php @@ -63,7 +63,14 @@ class EntityManagerTest extends UnitTestCase { * * @var \Drupal\Core\Cache\CacheBackendInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $cache; + protected $cacheBackend; + + /** + * The cache tag handler. + * + * @var \Drupal\Core\Cache\CacheTagHandlerInterface|\PHPUnit_Framework_MockObject_MockObject + */ + protected $cacheTagHandler; /** * The language manager. @@ -119,8 +126,8 @@ protected function setUp() { ->with('entity_type_build') ->will($this->returnValue(array())); - $this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface'); - $cache_tag_handler = $this->getMock('Drupal\Core\Cache\CacheTagHandlerInterface'); + $this->cacheBackend = $this->getMock('Drupal\Core\Cache\CacheBackendInterface'); + $this->cacheTagHandler = $this->getMock('Drupal\Core\Cache\CacheTagHandlerInterface'); $language = $this->getMock('Drupal\Core\Language\LanguageInterface'); $language->expects($this->any()) @@ -140,7 +147,7 @@ protected function setUp() { $this->formBuilder = $this->getMock('Drupal\Core\Form\FormBuilderInterface'); $this->controllerResolver = $this->getClassResolverStub(); - $this->container = $this->getContainerWithCacheBins($cache_tag_handler); + $this->container = $this->getContainerWithCacheBins($this->cacheTagHandler); $this->discovery = $this->getMock('Drupal\Component\Plugin\Discovery\DiscoveryInterface'); @@ -181,7 +188,7 @@ protected function setUpEntityManager($definitions = array()) { ->method('getDefinitions') ->will($this->returnValue($definitions)); - $this->entityManager = new TestEntityManager(new \ArrayObject(), $this->moduleHandler, $this->cache, $this->languageManager, $this->translationManager, $this->getClassResolverStub(), $this->typedDataManager, $this->installedDefinitions, $this->eventDispatcher); + $this->entityManager = new TestEntityManager(new \ArrayObject(), $this->moduleHandler, $this->cacheBackend, $this->languageManager, $this->translationManager, $this->getClassResolverStub(), $this->typedDataManager, $this->installedDefinitions, $this->eventDispatcher); $this->entityManager->setContainer($this->container); $this->entityManager->setDiscovery($this->discovery); } @@ -194,13 +201,13 @@ protected function setUpEntityManager($definitions = array()) { */ public function testClearCachedDefinitions() { $this->setUpEntityManager(); - $this->cache->expects($this->at(0)) + $this->cacheTagHandler->expects($this->at(0)) ->method('deleteTags') ->with(array('entity_types')); - $this->cache->expects($this->at(1)) + $this->cacheTagHandler->expects($this->at(1)) ->method('deleteTags') ->with(array('entity_bundles')); - $this->cache->expects($this->at(2)) + $this->cacheTagHandler->expects($this->at(2)) ->method('deleteTags') ->with(array('entity_field_info')); @@ -525,21 +532,21 @@ public function testGetBaseFieldDefinitionsWithCaching() { $expected = array('id' => $field_definition); - $this->cache->expects($this->at(0)) + $this->cacheBackend->expects($this->at(0)) ->method('get') ->with('entity_base_field_definitions:test_entity_type:en', FALSE) ->will($this->returnValue(FALSE)); - $this->cache->expects($this->at(1)) + $this->cacheBackend->expects($this->at(1)) ->method('get') ->with('entity_type', FALSE) ->will($this->returnValue(FALSE)); - $this->cache->expects($this->at(2)) + $this->cacheBackend->expects($this->at(2)) ->method('set') ->with('entity_type'); - $this->cache->expects($this->at(3)) + $this->cacheBackend->expects($this->at(3)) ->method('set') ->with('entity_base_field_definitions:test_entity_type:en'); - $this->cache->expects($this->at(4)) + $this->cacheBackend->expects($this->at(4)) ->method('get') ->with('entity_base_field_definitions:test_entity_type:en', FALSE) ->will($this->returnValue((object) array('data' => $expected))); @@ -559,27 +566,27 @@ public function testGetFieldDefinitionsWithCaching() { $expected = array('id' => $field_definition); - $this->cache->expects($this->at(0)) + $this->cacheBackend->expects($this->at(0)) ->method('get') ->with('entity_base_field_definitions:test_entity_type:en', FALSE) ->will($this->returnValue((object) array('data' => $expected))); - $this->cache->expects($this->at(1)) + $this->cacheBackend->expects($this->at(1)) ->method('get') ->with('entity_bundle_field_definitions:test_entity_type:test_bundle:en', FALSE) ->will($this->returnValue(FALSE)); - $this->cache->expects($this->at(2)) + $this->cacheBackend->expects($this->at(2)) ->method('get') ->with('entity_type', FALSE) ->will($this->returnValue(FALSE)); - $this->cache->expects($this->at(3)) + $this->cacheBackend->expects($this->at(3)) ->method('set'); - $this->cache->expects($this->at(4)) + $this->cacheBackend->expects($this->at(4)) ->method('set'); - $this->cache->expects($this->at(5)) + $this->cacheBackend->expects($this->at(5)) ->method('get') ->with('entity_base_field_definitions:test_entity_type:en', FALSE) ->will($this->returnValue((object) array('data' => $expected))); - $this->cache->expects($this->at(6)) + $this->cacheBackend->expects($this->at(6)) ->method('get') ->with('entity_bundle_field_definitions:test_entity_type:test_bundle:en', FALSE) ->will($this->returnValue((object) array('data' => $expected))); @@ -618,29 +625,29 @@ public function testGetFieldStorageDefinitionsWithCaching() { 'field_storage' => $field_storage_definition, ); - $this->cache->expects($this->at(0)) + $this->cacheBackend->expects($this->at(0)) ->method('get') ->with('entity_base_field_definitions:test_entity_type:en', FALSE) ->will($this->returnValue((object) array('data' => array('id' => $expected['id'])))); - $this->cache->expects($this->at(1)) + $this->cacheBackend->expects($this->at(1)) ->method('get') ->with('entity_field_storage_definitions:test_entity_type:en', FALSE) ->will($this->returnValue(FALSE)); - $this->cache->expects($this->at(2)) + $this->cacheBackend->expects($this->at(2)) ->method('get') ->with('entity_type', FALSE) ->will($this->returnValue(FALSE)); - $this->cache->expects($this->at(3)) + $this->cacheBackend->expects($this->at(3)) ->method('set') ->with('entity_type'); - $this->cache->expects($this->at(4)) + $this->cacheBackend->expects($this->at(4)) ->method('set') ->with('entity_field_storage_definitions:test_entity_type:en'); - $this->cache->expects($this->at(5)) + $this->cacheBackend->expects($this->at(5)) ->method('get') ->with('entity_base_field_definitions:test_entity_type:en', FALSE) ->will($this->returnValue((object) array('data' => array('id' => $expected['id'])))); - $this->cache->expects($this->at(6)) + $this->cacheBackend->expects($this->at(6)) ->method('get') ->with('entity_field_storage_definitions:test_entity_type:en', FALSE) ->will($this->returnValue((object) array('data' => $expected))); @@ -772,7 +779,7 @@ protected function setUpEntityWithFieldDefinition($custom_invoke_all = FALSE, $f */ public function testClearCachedFieldDefinitions() { $this->setUpEntityManager(); - $this->cache->expects($this->once()) + $this->cacheTagHandler->expects($this->once()) ->method('deleteTags') ->with(array('entity_field_info')); $this->typedDataManager->expects($this->once()) @@ -788,7 +795,7 @@ public function testClearCachedFieldDefinitions() { */ public function testClearCachedBundles() { $this->setUpEntityManager(); - $this->cache->expects($this->once()) + $this->cacheTagHandler->expects($this->once()) ->method('deleteTags') ->with(array('entity_bundles')); @@ -860,30 +867,30 @@ public function testGetAllBundleInfo() { 'apple' => $apple, 'banana' => $banana, )); - $this->cache->expects($this->at(0)) + $this->cacheBackend->expects($this->at(0)) ->method('get') ->with("entity_bundle_info:en", FALSE) ->will($this->returnValue(FALSE)); - $this->cache->expects($this->at(1)) + $this->cacheBackend->expects($this->at(1)) ->method('get') ->with("entity_type", FALSE) ->will($this->returnValue(FALSE)); - $this->cache->expects($this->at(2)) + $this->cacheBackend->expects($this->at(2)) ->method('set') ->with("entity_type"); - $this->cache->expects($this->at(3)) + $this->cacheBackend->expects($this->at(3)) ->method('set') ->with("entity_bundle_info:en"); - $this->cache->expects($this->at(4)) + $this->cacheTagHandler->expects($this->at(0)) ->method('deleteTags') ->with(array('entity_types')); - $this->cache->expects($this->at(5)) + $this->cacheTagHandler->expects($this->at(1)) ->method('deleteTags') ->with(array('entity_bundles')); - $this->cache->expects($this->at(6)) + $this->cacheTagHandler->expects($this->at(2)) ->method('deleteTags') ->with(array('entity_field_info')); - $this->cache->expects($this->at(7)) + $this->cacheBackend->expects($this->at(4)) ->method('get') ->with("entity_bundle_info:en", FALSE) ->will($this->returnValue((object) array('data' => 'cached data'))); @@ -1016,7 +1023,7 @@ function testgetExtraFields() { ->method('getCurrentLanguage') ->will($this->returnValue($language)); - $this->cache->expects($this->once()) + $this->cacheBackend->expects($this->once()) ->method('get') ->with($cache_id); @@ -1028,7 +1035,7 @@ function testgetExtraFields() { ->method('alter') ->with('entity_extra_field_info', $hook_bundle_extra_fields); - $this->cache->expects($this->once()) + $this->cacheBackend->expects($this->once()) ->method('set') ->with($cache_id, $processed_hook_bundle_extra_fields[$entity_type_id][$bundle]); @@ -1168,7 +1175,7 @@ public function testGetFieldMapFromCache() { ) ); $this->setUpEntityManager(); - $this->cache->expects($this->once()) + $this->cacheBackend->expects($this->once()) ->method('get') ->with('entity_field_map') ->will($this->returnValue((object) array('data' => $expected))); diff --git a/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php b/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php index e9bd4ce..db6bda3 100644 --- a/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Plugin/DefaultPluginManagerTest.php @@ -190,10 +190,9 @@ public function testDefaultPluginManagerWithFilledCache() { */ public function testCacheClearWithTags() { $cid = $this->randomMachineName(); - $cache_backend = $this->getMockBuilder('Drupal\Core\Cache\CacheTagHandlerInterface') - ->disableOriginalConstructor() - ->getMock(); - $cache_backend + $cache_backend = $this->getMock('Drupal\Core\Cache\CacheBackendInterface'); + $cache_tag_handler = $this->getMock('Drupal\Core\Cache\CacheTagHandlerInterface'); + $cache_tag_handler ->expects($this->once()) ->method('deleteTags') ->with(array('tag')); @@ -201,7 +200,7 @@ public function testCacheClearWithTags() { ->expects($this->never()) ->method('deleteMultiple'); - $this->getContainerWithCacheBins($cache_backend); + $this->getContainerWithCacheBins($cache_tag_handler); $plugin_manager = new TestPluginManager($this->namespaces, $this->expectedDefinitions, NULL, NULL, '\Drupal\plugin_test\Plugin\plugin_test\fruit\FruitInterface'); $plugin_manager->setCacheBackend($cache_backend, $cid, array('tag'));