cache.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /**
  3. * @group cache
  4. */
  5. class Tests_Cache extends WP_UnitTestCase {
  6. var $cache = NULL;
  7. function setUp() {
  8. parent::setUp();
  9. // create two cache objects with a shared cache dir
  10. // this simulates a typical cache situation, two separate requests interacting
  11. $this->cache =& $this->init_cache();
  12. }
  13. function tearDown() {
  14. parent::tearDown();
  15. $this->flush_cache();
  16. }
  17. function &init_cache() {
  18. $cache = new WP_Object_Cache();
  19. $cache->add_global_groups( array( 'global-cache-test', 'users', 'userlogins', 'usermeta', 'user_meta', 'site-transient', 'site-options', 'site-lookup', 'blog-lookup', 'blog-details', 'rss', 'global-posts', 'blog-id-cache' ) );
  20. return $cache;
  21. }
  22. function test_miss() {
  23. $this->assertEquals(NULL, $this->cache->get(rand_str()));
  24. }
  25. function test_add_get() {
  26. $key = rand_str();
  27. $val = rand_str();
  28. $this->cache->add($key, $val);
  29. $this->assertEquals($val, $this->cache->get($key));
  30. }
  31. function test_add_get_0() {
  32. $key = rand_str();
  33. $val = 0;
  34. // you can store zero in the cache
  35. $this->cache->add($key, $val);
  36. $this->assertEquals($val, $this->cache->get($key));
  37. }
  38. function test_add_get_null() {
  39. $key = rand_str();
  40. $val = null;
  41. $this->assertTrue( $this->cache->add($key, $val) );
  42. // null is converted to empty string
  43. $this->assertEquals( '', $this->cache->get($key) );
  44. }
  45. function test_add() {
  46. $key = rand_str();
  47. $val1 = rand_str();
  48. $val2 = rand_str();
  49. // add $key to the cache
  50. $this->assertTrue($this->cache->add($key, $val1));
  51. $this->assertEquals($val1, $this->cache->get($key));
  52. // $key is in the cache, so reject new calls to add()
  53. $this->assertFalse($this->cache->add($key, $val2));
  54. $this->assertEquals($val1, $this->cache->get($key));
  55. }
  56. function test_replace() {
  57. $key = rand_str();
  58. $val = rand_str();
  59. $val2 = rand_str();
  60. // memcached rejects replace() if the key does not exist
  61. $this->assertFalse($this->cache->replace($key, $val));
  62. $this->assertFalse($this->cache->get($key));
  63. $this->assertTrue($this->cache->add($key, $val));
  64. $this->assertEquals($val, $this->cache->get($key));
  65. $this->assertTrue($this->cache->replace($key, $val2));
  66. $this->assertEquals($val2, $this->cache->get($key));
  67. }
  68. function test_set() {
  69. $key = rand_str();
  70. $val1 = rand_str();
  71. $val2 = rand_str();
  72. // memcached accepts set() if the key does not exist
  73. $this->assertTrue($this->cache->set($key, $val1));
  74. $this->assertEquals($val1, $this->cache->get($key));
  75. // Second set() with same key should be allowed
  76. $this->assertTrue($this->cache->set($key, $val2));
  77. $this->assertEquals($val2, $this->cache->get($key));
  78. }
  79. function test_flush() {
  80. global $_wp_using_ext_object_cache;
  81. if ( $_wp_using_ext_object_cache )
  82. return;
  83. $key = rand_str();
  84. $val = rand_str();
  85. $this->cache->add($key, $val);
  86. // item is visible to both cache objects
  87. $this->assertEquals($val, $this->cache->get($key));
  88. $this->cache->flush();
  89. // If there is no value get returns false.
  90. $this->assertFalse($this->cache->get($key));
  91. }
  92. // Make sure objects are cloned going to and from the cache
  93. function test_object_refs() {
  94. $key = rand_str();
  95. $object_a = new stdClass;
  96. $object_a->foo = 'alpha';
  97. $this->cache->set( $key, $object_a );
  98. $object_a->foo = 'bravo';
  99. $object_b = $this->cache->get( $key );
  100. $this->assertEquals( 'alpha', $object_b->foo );
  101. $object_b->foo = 'charlie';
  102. $this->assertEquals( 'bravo', $object_a->foo );
  103. $key = rand_str();
  104. $object_a = new stdClass;
  105. $object_a->foo = 'alpha';
  106. $this->cache->add( $key, $object_a );
  107. $object_a->foo = 'bravo';
  108. $object_b = $this->cache->get( $key );
  109. $this->assertEquals( 'alpha', $object_b->foo );
  110. $object_b->foo = 'charlie';
  111. $this->assertEquals( 'bravo', $object_a->foo );
  112. }
  113. function test_incr() {
  114. $key = rand_str();
  115. $this->assertFalse( $this->cache->incr( $key ) );
  116. $this->cache->set( $key, 0 );
  117. $this->cache->incr( $key );
  118. $this->assertEquals( 1, $this->cache->get( $key ) );
  119. $this->cache->incr( $key, 2 );
  120. $this->assertEquals( 3, $this->cache->get( $key ) );
  121. }
  122. function test_wp_cache_incr() {
  123. $key = rand_str();
  124. $this->assertFalse( wp_cache_incr( $key ) );
  125. wp_cache_set( $key, 0 );
  126. wp_cache_incr( $key );
  127. $this->assertEquals( 1, wp_cache_get( $key ) );
  128. wp_cache_incr( $key, 2 );
  129. $this->assertEquals( 3, wp_cache_get( $key ) );
  130. }
  131. function test_decr() {
  132. $key = rand_str();
  133. $this->assertFalse( $this->cache->decr( $key ) );
  134. $this->cache->set( $key, 0 );
  135. $this->cache->decr( $key );
  136. $this->assertEquals( 0, $this->cache->get( $key ) );
  137. $this->cache->set( $key, 3 );
  138. $this->cache->decr( $key );
  139. $this->assertEquals( 2, $this->cache->get( $key ) );
  140. $this->cache->decr( $key, 2 );
  141. $this->assertEquals( 0, $this->cache->get( $key ) );
  142. }
  143. /**
  144. * @group 21327
  145. */
  146. function test_wp_cache_decr() {
  147. $key = rand_str();
  148. $this->assertFalse( wp_cache_decr( $key ) );
  149. wp_cache_set( $key, 0 );
  150. wp_cache_decr( $key );
  151. $this->assertEquals( 0, wp_cache_get( $key ) );
  152. wp_cache_set( $key, 3 );
  153. wp_cache_decr( $key );
  154. $this->assertEquals( 2, wp_cache_get( $key ) );
  155. wp_cache_decr( $key, 2 );
  156. $this->assertEquals( 0, wp_cache_get( $key ) );
  157. }
  158. function test_delete() {
  159. $key = rand_str();
  160. $val = rand_str();
  161. // Verify set
  162. $this->assertTrue( $this->cache->set( $key, $val ) );
  163. $this->assertEquals( $val, $this->cache->get( $key ) );
  164. // Verify successful delete
  165. $this->assertTrue( $this->cache->delete( $key ) );
  166. $this->assertFalse( $this->cache->get( $key ) );
  167. $this->assertFalse( $this->cache->delete( $key, 'default') );
  168. }
  169. function test_wp_cache_delete() {
  170. $key = rand_str();
  171. $val = rand_str();
  172. // Verify set
  173. $this->assertTrue( wp_cache_set( $key, $val ) );
  174. $this->assertEquals( $val, wp_cache_get( $key ) );
  175. // Verify successful delete
  176. $this->assertTrue( wp_cache_delete( $key ) );
  177. $this->assertFalse( wp_cache_get( $key ) );
  178. // wp_cache_delete() does not have a $force method.
  179. // Delete returns (bool) true when key is not set and $force is true
  180. // $this->assertTrue( wp_cache_delete( $key, 'default', true ) );
  181. $this->assertFalse( wp_cache_delete( $key, 'default') );
  182. }
  183. function test_switch_to_blog() {
  184. if ( ! method_exists( $this->cache, 'switch_to_blog' ) )
  185. return;
  186. $key = rand_str();
  187. $val = rand_str();
  188. $val2 = rand_str();
  189. if ( ! is_multisite() ) {
  190. // Single site ingnores switch_to_blog().
  191. $this->assertTrue( $this->cache->set( $key, $val ) );
  192. $this->assertEquals( $val, $this->cache->get( $key ) );
  193. $this->cache->switch_to_blog( 999 );
  194. $this->assertEquals( $val, $this->cache->get( $key ) );
  195. $this->assertTrue( $this->cache->set( $key, $val2 ) );
  196. $this->assertEquals( $val2, $this->cache->get( $key ) );
  197. $this->cache->switch_to_blog( get_current_blog_id() );
  198. $this->assertEquals( $val2, $this->cache->get( $key ) );
  199. } else {
  200. // Multisite should have separate per-blog caches
  201. $this->assertTrue( $this->cache->set( $key, $val ) );
  202. $this->assertEquals( $val, $this->cache->get( $key ) );
  203. $this->cache->switch_to_blog( 999 );
  204. $this->assertFalse( $this->cache->get( $key ) );
  205. $this->assertTrue( $this->cache->set( $key, $val2 ) );
  206. $this->assertEquals( $val2, $this->cache->get( $key ) );
  207. $this->cache->switch_to_blog( get_current_blog_id() );
  208. $this->assertEquals( $val, $this->cache->get( $key ) );
  209. $this->cache->switch_to_blog( 999 );
  210. $this->assertEquals( $val2, $this->cache->get( $key ) );
  211. $this->cache->switch_to_blog( get_current_blog_id() );
  212. $this->assertEquals( $val, $this->cache->get( $key ) );
  213. }
  214. // Global group
  215. $this->assertTrue( $this->cache->set( $key, $val, 'global-cache-test' ) );
  216. $this->assertEquals( $val, $this->cache->get( $key, 'global-cache-test' ) );
  217. $this->cache->switch_to_blog( 999 );
  218. $this->assertEquals( $val, $this->cache->get( $key, 'global-cache-test' ) );
  219. $this->assertTrue( $this->cache->set( $key, $val2, 'global-cache-test' ) );
  220. $this->assertEquals( $val2, $this->cache->get( $key, 'global-cache-test' ) );
  221. $this->cache->switch_to_blog( get_current_blog_id() );
  222. $this->assertEquals( $val2, $this->cache->get( $key, 'global-cache-test' ) );
  223. }
  224. function test_wp_cache_init() {
  225. $new_blank_cache_object = new WP_Object_Cache();
  226. wp_cache_init();
  227. global $wp_object_cache;
  228. $this->assertEquals( $wp_object_cache, $new_blank_cache_object );
  229. }
  230. function test_wp_cache_replace() {
  231. $key = 'my-key';
  232. $val1 = 'first-val';
  233. $val2 = 'second-val';
  234. $fake_key = 'my-fake-key';
  235. // Save the first value to cache and verify
  236. wp_cache_set( $key, $val1 );
  237. $this->assertEquals( $val1, wp_cache_get( $key ) );
  238. // Replace the value and verify
  239. wp_cache_replace( $key, $val2 );
  240. $this->assertEquals( $val2, wp_cache_get( $key ) );
  241. // Non-existant key should fail
  242. $this->assertFalse( wp_cache_replace( $fake_key, $val1 ) );
  243. // Make sure $fake_key is not stored
  244. $this->assertFalse( wp_cache_get( $fake_key ) );
  245. }
  246. }