BackendChainImplementationUnitTest.php

Definition of Drupal\Tests\Core\Cache\BackendChainImplementationUnitTest.

Namespace

Drupal\Tests\Core\Cache

File

drupal/core/tests/Drupal/Tests/Core/Cache/BackendChainImplementationUnitTest.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of Drupal\Tests\Core\Cache\BackendChainImplementationUnitTest.
  5. */
  6. namespace Drupal\Tests\Core\Cache;
  7. use Drupal\Core\Cache\CacheBackendInterface;
  8. use Drupal\Core\Cache\BackendChain;
  9. use Drupal\Core\Cache\MemoryBackend;
  10. use Drupal\Tests\UnitTestCase;
  11. /**
  12. * Tests implementation-specific functionality of the BackendChain backend.
  13. *
  14. * @group Cache
  15. */
  16. class BackendChainImplementationUnitTest extends UnitTestCase {
  17. public static function getInfo() {
  18. return array(
  19. 'name' => 'Backend chain implementation',
  20. 'description' => 'Unit test of backend chain implementation specifics.',
  21. 'group' => 'Cache'
  22. );
  23. }
  24. /**
  25. * Chain that will be heavily tested.
  26. *
  27. * @var Drupal\Core\Cache\BackendChain
  28. */
  29. protected $chain;
  30. /**
  31. * First backend in the chain.
  32. *
  33. * @var Drupal\Core\Cache\CacheBackendInterface
  34. */
  35. protected $firstBackend;
  36. /**
  37. * Second backend in the chain.
  38. *
  39. * @var Drupal\Core\Cache\CacheBackendInterface
  40. */
  41. protected $secondBackend;
  42. /**
  43. * Third backend in the chain.
  44. *
  45. * @var Drupal\Core\Cache\CacheBackendInterface
  46. */
  47. protected $thirdBackend;
  48. public function setUp() {
  49. parent::setUp();
  50. // Set up three memory backends to be used in the chain.
  51. $this->firstBackend = new MemoryBackend('foo');
  52. $this->secondBackend = new MemoryBackend('bar');
  53. $this->thirdBackend = new MemoryBackend('baz');
  54. // Set an initial fixed dataset for all testing. The next three data
  55. // collections will test two edge cases (last backend has the data, and
  56. // first backend has the data) and will test a normal use case (middle
  57. // backend has the data). We should have a complete unit test with those.
  58. // Note that in all cases, when the same key is set on more than one
  59. // backend, the values are voluntarily different, this ensures in which
  60. // backend we actually fetched the key when doing get calls.
  61. // Set a key present on all backends (for delete).
  62. $this->firstBackend->set('t123', 1231);
  63. $this->secondBackend->set('t123', 1232);
  64. $this->thirdBackend->set('t123', 1233);
  65. // Set a key present on the second and the third (for get), those two will
  66. // be different, this will ensure from where we get the key.
  67. $this->secondBackend->set('t23', 232);
  68. $this->thirdBackend->set('t23', 233);
  69. // Set a key on only the third, we will ensure propagation using this one.
  70. $this->thirdBackend->set('t3', 33);
  71. // Create the chain.
  72. $this->chain = new BackendChain('foobarbaz');
  73. $this->chain
  74. ->appendBackend($this->firstBackend)
  75. ->appendBackend($this->secondBackend)
  76. ->appendBackend($this->thirdBackend);
  77. }
  78. /**
  79. * Test the get feature.
  80. */
  81. public function testGet() {
  82. $cached = $this->chain->get('t123');
  83. $this->assertNotSame(FALSE, $cached, 'Got key that is on all backends');
  84. $this->assertSame(1231, $cached->data, 'Got the key from the backend 1');
  85. $cached = $this->chain->get('t23');
  86. $this->assertNotSame(FALSE, $cached, 'Got key that is on backends 2 and 3');
  87. $this->assertSame(232, $cached->data, 'Got the key from the backend 2');
  88. $cached = $this->chain->get('t3');
  89. $this->assertNotSame(FALSE, $cached, 'Got key that is on the backend 3');
  90. $this->assertSame(33, $cached->data, 'Got the key from the backend 3');
  91. }
  92. /**
  93. * Test the get multiple feature.
  94. */
  95. public function testGetMultiple() {
  96. $cids = array('t123', 't23', 't3', 't4');
  97. $ret = $this->chain->getMultiple($cids);
  98. $this->assertSame($ret['t123']->data, 1231, 'Got key 123 and value is from the first backend');
  99. $this->assertSame($ret['t23']->data, 232, 'Got key 23 and value is from the second backend');
  100. $this->assertSame($ret['t3']->data, 33, 'Got key 3 and value is from the third backend');
  101. $this->assertFalse(array_key_exists('t4', $ret), "Didn't get the nonexistent key");
  102. $this->assertFalse(in_array('t123', $cids), "Existing key 123 has been removed from &\$cids");
  103. $this->assertFalse(in_array('t23', $cids), "Existing key 23 has been removed from &\$cids");
  104. $this->assertFalse(in_array('t3', $cids), "Existing key 3 has been removed from &\$cids");
  105. $this->assertTrue(in_array('t4', $cids), "Non existing key 4 is still in &\$cids");
  106. }
  107. /**
  108. * Test that set will propagate.
  109. */
  110. public function testSet() {
  111. $this->chain->set('test', 123);
  112. $cached = $this->firstBackend->get('test');
  113. $this->assertNotSame(FALSE, $cached, 'Test key is in the first backend');
  114. $this->assertSame(123, $cached->data, 'Test key has the right value');
  115. $cached = $this->secondBackend->get('test');
  116. $this->assertNotSame(FALSE, $cached, 'Test key is in the second backend');
  117. $this->assertSame(123, $cached->data, 'Test key has the right value');
  118. $cached = $this->thirdBackend->get('test');
  119. $this->assertNotSame(FALSE, $cached, 'Test key is in the third backend');
  120. $this->assertSame(123, $cached->data, 'Test key has the right value');
  121. }
  122. /**
  123. * Test that delete will propagate.
  124. */
  125. public function testDelete() {
  126. $this->chain->set('test', 5);
  127. $cached = $this->firstBackend->get('test');
  128. $this->assertNotSame(FALSE, $cached, 'Test key has been added to the first backend');
  129. $cached = $this->secondBackend->get('test');
  130. $this->assertNotSame(FALSE, $cached, 'Test key has been added to the first backend');
  131. $cached = $this->thirdBackend->get('test');
  132. $this->assertNotSame(FALSE, $cached, 'Test key has been added to the first backend');
  133. $this->chain->delete('test');
  134. $cached = $this->firstBackend->get('test');
  135. $this->assertSame(FALSE, $cached, 'Test key is removed from the first backend');
  136. $cached = $this->secondBackend->get('test');
  137. $this->assertSame(FALSE, $cached, 'Test key is removed from the second backend');
  138. $cached = $this->thirdBackend->get('test');
  139. $this->assertSame(FALSE, $cached, 'Test key is removed from the third backend');
  140. }
  141. /**
  142. * Ensure get values propagation to previous backends.
  143. */
  144. public function testGetHasPropagated() {
  145. $this->chain->get('t23');
  146. $cached = $this->firstBackend->get('t23');
  147. $this->assertNotSame(FALSE, $cached, 'Test 2 has been propagated to the first backend');
  148. $this->chain->get('t3');
  149. $cached = $this->firstBackend->get('t3');
  150. $this->assertNotSame(FALSE, $cached, 'Test 3 has been propagated to the first backend');
  151. $cached = $this->secondBackend->get('t3');
  152. $this->assertNotSame(FALSE, $cached, 'Test 3 has been propagated to the second backend');
  153. }
  154. /**
  155. * Ensure get multiple values propagation to previous backends.
  156. */
  157. public function testGetMultipleHasPropagated() {
  158. $cids = array('t3', 't23');
  159. $this->chain->getMultiple($cids);
  160. $cached = $this->firstBackend->get('t3');
  161. $this->assertNotSame(FALSE, $cached, 'Test 3 has been propagated to the first backend');
  162. $this->assertSame(33, $cached->data, 'And value has been kept');
  163. $cached = $this->secondBackend->get('t3');
  164. $this->assertNotSame(FALSE, $cached, 'Test 3 has been propagated to the second backend');
  165. $this->assertSame(33, $cached->data, 'And value has been kept');
  166. $cached = $this->firstBackend->get('t23');
  167. $this->assertNotSame(FALSE, $cached, 'Test 2 has been propagated to the first backend');
  168. $this->assertSame(232, $cached->data, 'And value has been kept');
  169. }
  170. /**
  171. * Test that the chain is not empty when at least one backend has data.
  172. */
  173. public function testNotEmptyIfOneBackendHasTheKey() {
  174. $this->assertFalse($this->chain->isEmpty(), 'Chain is not empty');
  175. // This is the only test that needs to start with an empty chain.
  176. $this->chain->deleteAll();
  177. $this->assertTrue($this->chain->isEmpty(), 'Chain have been emptied by the deleteAll() call');
  178. $this->secondBackend->set('test', 5);
  179. $this->assertFalse($this->chain->isEmpty(), 'Chain is not empty anymore now that the second backend has something');
  180. }
  181. /**
  182. * Test that the delete all operation is propagated to all backends in the chain.
  183. */
  184. public function testDeleteAllPropagation() {
  185. // Set both expiring and permanent keys.
  186. $this->chain->set('test1', 1, CacheBackendInterface::CACHE_PERMANENT);
  187. $this->chain->set('test2', 3, time() + 1000);
  188. $this->chain->deleteAll();
  189. $this->assertTrue($this->firstBackend->isEmpty(), 'First backend is empty after delete all.');
  190. $this->assertTrue($this->secondBackend->isEmpty(), 'Second backend is empty after delete all.');
  191. $this->assertTrue($this->thirdBackend->isEmpty(), 'Third backend is empty after delete all.');
  192. }
  193. /**
  194. * Test that the delete tags operation is propagated to all backends
  195. * in the chain.
  196. */
  197. public function testDeleteTagsPropagation() {
  198. // Create two cache entries with the same tag and tag value.
  199. $this->chain->set('test_cid_clear1', 'foo', CacheBackendInterface::CACHE_PERMANENT, array('test_tag' => 2));
  200. $this->chain->set('test_cid_clear2', 'foo', CacheBackendInterface::CACHE_PERMANENT, array('test_tag' => 2));
  201. $this->assertNotSame(FALSE, $this->firstBackend->get('test_cid_clear1')
  202. && $this->firstBackend->get('test_cid_clear2')
  203. && $this->secondBackend->get('test_cid_clear1')
  204. && $this->secondBackend->get('test_cid_clear2')
  205. && $this->thirdBackend->get('test_cid_clear1')
  206. && $this->thirdBackend->get('test_cid_clear2'),
  207. 'Two cache items were created in all backends.');
  208. // Invalidate test_tag of value 1. This should invalidate both entries.
  209. $this->chain->deleteTags(array('test_tag' => 2));
  210. $this->assertSame(FALSE, $this->firstBackend->get('test_cid_clear1')
  211. && $this->firstBackend->get('test_cid_clear2')
  212. && $this->secondBackend->get('test_cid_clear1')
  213. && $this->secondBackend->get('test_cid_clear2')
  214. && $this->thirdBackend->get('test_cid_clear1')
  215. && $this->thirdBackend->get('test_cid_clear2'),
  216. 'Two caches removed from all backends after clearing a cache tag.');
  217. // Create two cache entries with the same tag and an array tag value.
  218. $this->chain->set('test_cid_clear1', 'foo', CacheBackendInterface::CACHE_PERMANENT, array('test_tag' => array(1)));
  219. $this->chain->set('test_cid_clear2', 'foo', CacheBackendInterface::CACHE_PERMANENT, array('test_tag' => array(1)));
  220. $this->assertNotSame(FALSE, $this->firstBackend->get('test_cid_clear1')
  221. && $this->firstBackend->get('test_cid_clear2')
  222. && $this->secondBackend->get('test_cid_clear1')
  223. && $this->secondBackend->get('test_cid_clear2')
  224. && $this->thirdBackend->get('test_cid_clear1')
  225. && $this->thirdBackend->get('test_cid_clear2'),
  226. 'Two cache items were created in all backends.');
  227. // Invalidate test_tag of value 1. This should invalidate both entries.
  228. $this->chain->deleteTags(array('test_tag' => array(1)));
  229. $this->assertSame(FALSE, $this->firstBackend->get('test_cid_clear1')
  230. && $this->firstBackend->get('test_cid_clear2')
  231. && $this->secondBackend->get('test_cid_clear1')
  232. && $this->secondBackend->get('test_cid_clear2')
  233. && $this->thirdBackend->get('test_cid_clear1')
  234. && $this->thirdBackend->get('test_cid_clear2'),
  235. 'Two caches removed from all backends after clearing a cache tag.');
  236. // Create three cache entries with a mix of tags and tag values.
  237. $this->chain->set('test_cid_clear1', 'foo', CacheBackendInterface::CACHE_PERMANENT, array('test_tag' => array(1)));
  238. $this->chain->set('test_cid_clear2', 'foo', CacheBackendInterface::CACHE_PERMANENT, array('test_tag' => array(2)));
  239. $this->chain->set('test_cid_clear3', 'foo', CacheBackendInterface::CACHE_PERMANENT, array('test_tag_foo' => array(3)));
  240. $this->assertNotSame(FALSE, $this->firstBackend->get('test_cid_clear1')
  241. && $this->firstBackend->get('test_cid_clear2')
  242. && $this->firstBackend->get('test_cid_clear3')
  243. && $this->secondBackend->get('test_cid_clear1')
  244. && $this->secondBackend->get('test_cid_clear2')
  245. && $this->secondBackend->get('test_cid_clear3')
  246. && $this->thirdBackend->get('test_cid_clear1')
  247. && $this->thirdBackend->get('test_cid_clear2')
  248. && $this->thirdBackend->get('test_cid_clear3'),
  249. 'Three cached items were created in all backends.');
  250. $this->chain->deleteTags(array('test_tag_foo' => array(3)));
  251. $this->assertNotSame(FALSE, $this->firstBackend->get('test_cid_clear1')
  252. && $this->firstBackend->get('test_cid_clear2')
  253. && $this->secondBackend->get('test_cid_clear1')
  254. && $this->secondBackend->get('test_cid_clear2')
  255. && $this->thirdBackend->get('test_cid_clear1')
  256. && $this->thirdBackend->get('test_cid_clear2'),
  257. 'Cached items not matching the tag were not cleared from any of the backends.');
  258. $this->assertSame(FALSE, $this->firstBackend->get('test_cid_clear3')
  259. && $this->secondBackend->get('test_cid_clear3')
  260. && $this->thirdBackend->get('test_cid_clear3'),
  261. 'Cached item matching the tag was removed from all backends.');
  262. }
  263. }

Classes

Namesort descending Description
BackendChainImplementationUnitTest Tests implementation-specific functionality of the BackendChain backend.