Changeset 60148 for trunk/tests/phpunit/tests/multisite/siteMeta.php
- Timestamp:
- 04/09/2025 01:29:39 PM (6 weeks ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/multisite/siteMeta.php
r55747 r60148 1 1 <?php 2 2 3 if ( is_multisite() ) : 3 /** 4 * @group ms-required 5 * @group ms-site 6 * @group multisite 7 * @group meta 8 * @ticket 37923 9 */ 10 class Tests_Multisite_SiteMeta extends WP_UnitTestCase { 11 12 protected static $site_id; 13 protected static $site_id2; 14 protected static $flag_was_set; 15 16 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 17 self::$site_id = $factory->blog->create( 18 array( 19 'domain' => 'wordpress.org', 20 'path' => '/', 21 ) 22 ); 23 self::$site_id2 = $factory->blog->create( 24 array( 25 'domain' => 'wordpress.org', 26 'path' => '/foo/', 27 ) 28 ); 29 30 // Populate the main network flag as necessary. 31 self::$flag_was_set = true; 32 if ( false === get_network_option( get_main_network_id(), 'site_meta_supported', false ) ) { 33 self::$flag_was_set = false; 34 is_site_meta_supported(); 35 } 36 } 37 38 public static function wpTearDownAfterClass() { 39 // Delete the possibly previously populated main network flag. 40 if ( ! self::$flag_was_set ) { 41 delete_network_option( get_main_network_id(), 'site_meta_supported' ); 42 } 43 44 wp_delete_site( self::$site_id ); 45 wp_delete_site( self::$site_id2 ); 46 47 wp_update_network_site_counts(); 48 } 49 50 public function test_is_site_meta_supported() { 51 $this->assertTrue( is_site_meta_supported() ); 52 } 53 54 public function test_is_site_meta_supported_filtered() { 55 add_filter( 'pre_site_option_site_meta_supported', '__return_zero' ); 56 $this->assertFalse( is_site_meta_supported() ); 57 } 58 59 public function test_add() { 60 if ( ! is_site_meta_supported() ) { 61 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 62 } 63 64 $this->assertNotEmpty( add_site_meta( self::$site_id, 'foo', 'bar' ) ); 65 $this->assertSame( 'bar', get_site_meta( self::$site_id, 'foo', true ) ); 66 } 67 68 public function test_add_unique() { 69 if ( ! is_site_meta_supported() ) { 70 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 71 } 72 73 $this->assertNotEmpty( add_site_meta( self::$site_id, 'foo', 'bar' ) ); 74 $this->assertFalse( add_site_meta( self::$site_id, 'foo', 'bar', true ) ); 75 } 76 77 public function test_delete() { 78 if ( ! is_site_meta_supported() ) { 79 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 80 } 81 82 add_site_meta( self::$site_id, 'foo', 'bar' ); 83 84 $this->assertTrue( delete_site_meta( self::$site_id, 'foo' ) ); 85 $this->assertEmpty( get_site_meta( self::$site_id, 'foo', true ) ); 86 } 87 88 public function test_delete_with_invalid_meta_key_should_return_false() { 89 if ( ! is_site_meta_supported() ) { 90 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 91 } 92 93 $this->assertFalse( delete_site_meta( self::$site_id, 'foo' ) ); 94 } 95 96 public function test_delete_should_respect_meta_value() { 97 if ( ! is_site_meta_supported() ) { 98 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 99 } 100 101 add_site_meta( self::$site_id, 'foo', 'bar' ); 102 add_site_meta( self::$site_id, 'foo', 'baz' ); 103 104 $this->assertTrue( delete_site_meta( self::$site_id, 'foo', 'bar' ) ); 105 106 $metas = get_site_meta( self::$site_id, 'foo' ); 107 $this->assertSame( array( 'baz' ), $metas ); 108 } 109 110 public function test_get_with_no_key_should_fetch_all_keys() { 111 if ( ! is_site_meta_supported() ) { 112 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 113 } 114 115 add_site_meta( self::$site_id, 'foo', 'bar' ); 116 add_site_meta( self::$site_id, 'foo1', 'baz' ); 117 118 $found = get_site_meta( self::$site_id ); 119 $expected = array( 120 'foo' => array( 'bar' ), 121 'foo1' => array( 'baz' ), 122 ); 123 124 $this->assertSameSets( $expected, $found ); 125 } 126 127 public function test_get_with_key_should_fetch_all_for_key() { 128 if ( ! is_site_meta_supported() ) { 129 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 130 } 131 132 add_site_meta( self::$site_id, 'foo', 'bar' ); 133 add_site_meta( self::$site_id, 'foo', 'baz' ); 134 add_site_meta( self::$site_id, 'foo1', 'baz' ); 135 136 $found = get_site_meta( self::$site_id, 'foo' ); 137 $expected = array( 'bar', 'baz' ); 138 139 $this->assertSameSets( $expected, $found ); 140 } 141 142 public function test_get_should_respect_single_true() { 143 if ( ! is_site_meta_supported() ) { 144 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 145 } 146 147 add_site_meta( self::$site_id, 'foo', 'bar' ); 148 add_site_meta( self::$site_id, 'foo', 'baz' ); 149 150 $found = get_site_meta( self::$site_id, 'foo', true ); 151 $this->assertSame( 'bar', $found ); 152 } 153 154 public function test_update_should_pass_to_add_when_no_value_exists_for_key() { 155 if ( ! is_site_meta_supported() ) { 156 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 157 } 158 159 $actual = update_site_meta( self::$site_id, 'foo', 'bar' ); 160 $this->assertIsInt( $actual ); 161 $this->assertNotEmpty( $actual ); 162 163 $meta = get_site_meta( self::$site_id, 'foo', true ); 164 $this->assertSame( 'bar', $meta ); 165 } 166 167 public function test_update_should_return_true_when_updating_existing_value_for_key() { 168 if ( ! is_site_meta_supported() ) { 169 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 170 } 171 172 add_site_meta( self::$site_id, 'foo', 'bar' ); 173 174 $actual = update_site_meta( self::$site_id, 'foo', 'baz' ); 175 $this->assertTrue( $actual ); 176 177 $meta = get_site_meta( self::$site_id, 'foo', true ); 178 $this->assertSame( 'baz', $meta ); 179 } 180 181 public function test_delete_by_key() { 182 if ( ! is_site_meta_supported() ) { 183 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 184 } 185 186 add_site_meta( self::$site_id, 'unique_delete_by_key', 'value', true ); 187 add_site_meta( self::$site_id2, 'unique_delete_by_key', 'value', true ); 188 189 $this->assertSame( 'value', get_site_meta( self::$site_id, 'unique_delete_by_key', true ) ); 190 $this->assertSame( 'value', get_site_meta( self::$site_id2, 'unique_delete_by_key', true ) ); 191 192 $this->assertTrue( delete_site_meta_by_key( 'unique_delete_by_key' ) ); 193 194 $this->assertSame( '', get_site_meta( self::$site_id, 'unique_delete_by_key', true ) ); 195 $this->assertSame( '', get_site_meta( self::$site_id2, 'unique_delete_by_key', true ) ); 196 } 197 198 public function test_site_meta_should_be_deleted_when_site_is_deleted() { 199 if ( ! is_site_meta_supported() ) { 200 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 201 } 202 203 $site_id = self::factory()->blog->create( 204 array( 205 'domain' => 'foo.org', 206 'path' => '/', 207 ) 208 ); 209 210 add_site_meta( $site_id, 'foo', 'bar' ); 211 add_site_meta( $site_id, 'foo1', 'bar' ); 212 213 $this->assertSame( 'bar', get_site_meta( $site_id, 'foo', true ) ); 214 $this->assertSame( 'bar', get_site_meta( $site_id, 'foo1', true ) ); 215 216 wp_delete_site( $site_id ); 217 218 $this->assertSame( '', get_site_meta( $site_id, 'foo', true ) ); 219 $this->assertSame( '', get_site_meta( $site_id, 'foo1', true ) ); 220 } 221 222 public function test_update_site_meta_cache() { 223 if ( ! is_site_meta_supported() ) { 224 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 225 } 226 227 update_site_meta( self::$site_id, 'foo', 'bar' ); 228 update_sitemeta_cache( array( self::$site_id ) ); 229 230 $num_queries = get_num_queries(); 231 get_site_meta( self::$site_id, 'foo', true ); 232 $this->assertSame( $num_queries, get_num_queries() ); 233 } 234 235 public function test_query_update_site_meta_cache_true() { 236 if ( ! is_site_meta_supported() ) { 237 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 238 } 239 240 update_site_meta( self::$site_id, 'foo', 'bar' ); 241 242 // Do not include 'update_site_meta_cache' as true as its the default. 243 new WP_Site_Query( 244 array( 245 'ID' => self::$site_id, 246 ) 247 ); 248 249 $num_queries = get_num_queries(); 250 get_site_meta( self::$site_id, 'foo', true ); 251 $this->assertSame( 1, get_num_queries() - $num_queries ); 252 } 4 253 5 254 /** 6 * @group ms-site 7 * @group multisite 8 * @group meta 9 * @ticket 37923 255 * @ticket 58185 10 256 */ 11 class Tests_Multisite_SiteMeta extends WP_UnitTestCase { 12 protected static $site_id; 13 protected static $site_id2; 14 protected static $flag_was_set; 15 16 public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 17 self::$site_id = $factory->blog->create( 18 array( 19 'domain' => 'wordpress.org', 20 'path' => '/', 21 ) 22 ); 23 self::$site_id2 = $factory->blog->create( 24 array( 25 'domain' => 'wordpress.org', 26 'path' => '/foo/', 27 ) 28 ); 29 30 // Populate the main network flag as necessary. 31 self::$flag_was_set = true; 32 if ( false === get_network_option( get_main_network_id(), 'site_meta_supported', false ) ) { 33 self::$flag_was_set = false; 34 is_site_meta_supported(); 35 } 36 } 37 38 public static function wpTearDownAfterClass() { 39 // Delete the possibly previously populated main network flag. 40 if ( ! self::$flag_was_set ) { 41 delete_network_option( get_main_network_id(), 'site_meta_supported' ); 42 } 43 44 wp_delete_site( self::$site_id ); 45 wp_delete_site( self::$site_id2 ); 46 47 wp_update_network_site_counts(); 48 } 49 50 public function test_is_site_meta_supported() { 51 $this->assertTrue( is_site_meta_supported() ); 52 } 53 54 public function test_is_site_meta_supported_filtered() { 55 add_filter( 'pre_site_option_site_meta_supported', '__return_zero' ); 56 $this->assertFalse( is_site_meta_supported() ); 57 } 58 59 public function test_add() { 60 if ( ! is_site_meta_supported() ) { 61 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 62 } 63 64 $this->assertNotEmpty( add_site_meta( self::$site_id, 'foo', 'bar' ) ); 65 $this->assertSame( 'bar', get_site_meta( self::$site_id, 'foo', true ) ); 66 } 67 68 public function test_add_unique() { 69 if ( ! is_site_meta_supported() ) { 70 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 71 } 72 73 $this->assertNotEmpty( add_site_meta( self::$site_id, 'foo', 'bar' ) ); 74 $this->assertFalse( add_site_meta( self::$site_id, 'foo', 'bar', true ) ); 75 } 76 77 public function test_delete() { 78 if ( ! is_site_meta_supported() ) { 79 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 80 } 81 82 add_site_meta( self::$site_id, 'foo', 'bar' ); 83 84 $this->assertTrue( delete_site_meta( self::$site_id, 'foo' ) ); 85 $this->assertEmpty( get_site_meta( self::$site_id, 'foo', true ) ); 86 } 87 88 public function test_delete_with_invalid_meta_key_should_return_false() { 89 if ( ! is_site_meta_supported() ) { 90 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 91 } 92 93 $this->assertFalse( delete_site_meta( self::$site_id, 'foo' ) ); 94 } 95 96 public function test_delete_should_respect_meta_value() { 97 if ( ! is_site_meta_supported() ) { 98 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 99 } 100 101 add_site_meta( self::$site_id, 'foo', 'bar' ); 102 add_site_meta( self::$site_id, 'foo', 'baz' ); 103 104 $this->assertTrue( delete_site_meta( self::$site_id, 'foo', 'bar' ) ); 105 106 $metas = get_site_meta( self::$site_id, 'foo' ); 107 $this->assertSame( array( 'baz' ), $metas ); 108 } 109 110 public function test_get_with_no_key_should_fetch_all_keys() { 111 if ( ! is_site_meta_supported() ) { 112 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 113 } 114 115 add_site_meta( self::$site_id, 'foo', 'bar' ); 116 add_site_meta( self::$site_id, 'foo1', 'baz' ); 117 118 $found = get_site_meta( self::$site_id ); 119 $expected = array( 120 'foo' => array( 'bar' ), 121 'foo1' => array( 'baz' ), 122 ); 123 124 $this->assertSameSets( $expected, $found ); 125 } 126 127 public function test_get_with_key_should_fetch_all_for_key() { 128 if ( ! is_site_meta_supported() ) { 129 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 130 } 131 132 add_site_meta( self::$site_id, 'foo', 'bar' ); 133 add_site_meta( self::$site_id, 'foo', 'baz' ); 134 add_site_meta( self::$site_id, 'foo1', 'baz' ); 135 136 $found = get_site_meta( self::$site_id, 'foo' ); 137 $expected = array( 'bar', 'baz' ); 138 139 $this->assertSameSets( $expected, $found ); 140 } 141 142 public function test_get_should_respect_single_true() { 143 if ( ! is_site_meta_supported() ) { 144 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 145 } 146 147 add_site_meta( self::$site_id, 'foo', 'bar' ); 148 add_site_meta( self::$site_id, 'foo', 'baz' ); 149 150 $found = get_site_meta( self::$site_id, 'foo', true ); 151 $this->assertSame( 'bar', $found ); 152 } 153 154 public function test_update_should_pass_to_add_when_no_value_exists_for_key() { 155 if ( ! is_site_meta_supported() ) { 156 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 157 } 158 159 $actual = update_site_meta( self::$site_id, 'foo', 'bar' ); 160 $this->assertIsInt( $actual ); 161 $this->assertNotEmpty( $actual ); 162 163 $meta = get_site_meta( self::$site_id, 'foo', true ); 164 $this->assertSame( 'bar', $meta ); 165 } 166 167 public function test_update_should_return_true_when_updating_existing_value_for_key() { 168 if ( ! is_site_meta_supported() ) { 169 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 170 } 171 172 add_site_meta( self::$site_id, 'foo', 'bar' ); 173 174 $actual = update_site_meta( self::$site_id, 'foo', 'baz' ); 175 $this->assertTrue( $actual ); 176 177 $meta = get_site_meta( self::$site_id, 'foo', true ); 178 $this->assertSame( 'baz', $meta ); 179 } 180 181 public function test_delete_by_key() { 182 if ( ! is_site_meta_supported() ) { 183 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 184 } 185 186 add_site_meta( self::$site_id, 'unique_delete_by_key', 'value', true ); 187 add_site_meta( self::$site_id2, 'unique_delete_by_key', 'value', true ); 188 189 $this->assertSame( 'value', get_site_meta( self::$site_id, 'unique_delete_by_key', true ) ); 190 $this->assertSame( 'value', get_site_meta( self::$site_id2, 'unique_delete_by_key', true ) ); 191 192 $this->assertTrue( delete_site_meta_by_key( 'unique_delete_by_key' ) ); 193 194 $this->assertSame( '', get_site_meta( self::$site_id, 'unique_delete_by_key', true ) ); 195 $this->assertSame( '', get_site_meta( self::$site_id2, 'unique_delete_by_key', true ) ); 196 } 197 198 public function test_site_meta_should_be_deleted_when_site_is_deleted() { 199 if ( ! is_site_meta_supported() ) { 200 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 201 } 202 203 $site_id = self::factory()->blog->create( 204 array( 205 'domain' => 'foo.org', 206 'path' => '/', 207 ) 208 ); 209 210 add_site_meta( $site_id, 'foo', 'bar' ); 211 add_site_meta( $site_id, 'foo1', 'bar' ); 212 213 $this->assertSame( 'bar', get_site_meta( $site_id, 'foo', true ) ); 214 $this->assertSame( 'bar', get_site_meta( $site_id, 'foo1', true ) ); 215 216 wp_delete_site( $site_id ); 217 218 $this->assertSame( '', get_site_meta( $site_id, 'foo', true ) ); 219 $this->assertSame( '', get_site_meta( $site_id, 'foo1', true ) ); 220 } 221 222 public function test_update_site_meta_cache() { 223 if ( ! is_site_meta_supported() ) { 224 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 225 } 226 227 update_site_meta( self::$site_id, 'foo', 'bar' ); 228 update_sitemeta_cache( array( self::$site_id ) ); 229 230 $num_queries = get_num_queries(); 231 get_site_meta( self::$site_id, 'foo', true ); 232 $this->assertSame( $num_queries, get_num_queries() ); 233 } 234 235 public function test_query_update_site_meta_cache_true() { 236 if ( ! is_site_meta_supported() ) { 237 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 238 } 239 240 update_site_meta( self::$site_id, 'foo', 'bar' ); 241 242 // Do not include 'update_site_meta_cache' as true as its the default. 243 new WP_Site_Query( 244 array( 245 'ID' => self::$site_id, 246 ) 247 ); 248 249 $num_queries = get_num_queries(); 250 get_site_meta( self::$site_id, 'foo', true ); 251 $this->assertSame( 1, get_num_queries() - $num_queries ); 252 } 253 254 /** 255 * @ticket 58185 256 */ 257 public function test_lazy_load_site_meta() { 258 if ( ! is_site_meta_supported() ) { 259 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 260 } 261 262 $filter = new MockAction(); 263 add_filter( 'update_blog_metadata_cache', array( $filter, 'filter' ), 10, 2 ); 264 265 $q = new WP_Site_Query( 266 array( 267 'ID' => self::$site_id, 268 ) 269 ); 270 271 $this->assertSameSets( array( (string) self::$site_id ), wp_list_pluck( $q->sites, 'blog_id' ), 'Site query should return the first test site' ); 272 273 $q = new WP_Site_Query( 274 array( 275 'ID' => self::$site_id2, 276 ) 277 ); 278 279 $this->assertSameSets( array( (string) self::$site_id2 ), wp_list_pluck( $q->sites, 'blog_id' ), 'Site query should return the second test site' ); 280 281 get_site_meta( self::$site_id2 ); 282 283 $args = $filter->get_args(); 284 $first = reset( $args ); 285 $site_ids = end( $first ); 286 $this->assertSameSets( $site_ids, array( self::$site_id, self::$site_id2 ), 'This should have two site\'s meta' ); 287 } 288 289 /** 290 * @ticket 58185 291 */ 292 public function test_lazy_load_site_meta_fields_id() { 293 if ( ! is_site_meta_supported() ) { 294 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 295 } 296 297 $filter = new MockAction(); 298 add_filter( 'update_blog_metadata_cache', array( $filter, 'filter' ), 10, 2 ); 299 300 $q = new WP_Site_Query( 301 array( 302 'ID' => self::$site_id, 303 'fields' => 'ids', 304 ) 305 ); 306 307 $this->assertSameSets( array( self::$site_id ), $q->sites, 'Site query should return the first test site' ); 308 309 $q = new WP_Site_Query( 310 array( 311 'ID' => self::$site_id2, 312 'fields' => 'ids', 313 ) 314 ); 315 316 $this->assertSameSets( array( self::$site_id2 ), $q->sites, 'Site query should return the second test site' ); 317 318 get_site_meta( self::$site_id2 ); 319 320 $args = $filter->get_args(); 321 $first = reset( $args ); 322 $site_ids = end( $first ); 323 $this->assertSameSets( $site_ids, array( self::$site_id, self::$site_id2 ), 'This should have two sites meta' ); 324 } 325 326 public function test_query_update_site_meta_cache_false() { 327 if ( ! is_site_meta_supported() ) { 328 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 329 } 330 331 update_site_meta( self::$site_id, 'foo', 'bar' ); 332 333 new WP_Site_Query( 334 array( 335 'ID' => self::$site_id, 336 'update_site_meta_cache' => false, 337 ) 338 ); 339 340 $num_queries = get_num_queries(); 341 get_site_meta( self::$site_id, 'foo', true ); 342 $this->assertSame( 1, get_num_queries() - $num_queries ); 343 } 344 345 /** 346 * @ticket 40229 347 */ 348 public function test_add_site_meta_should_bust_get_sites_cache() { 349 if ( ! is_site_meta_supported() ) { 350 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 351 } 352 353 add_site_meta( self::$site_id, 'foo', 'bar' ); 354 355 // Prime cache. 356 $found = get_sites( 357 array( 358 'fields' => 'ids', 359 'meta_query' => array( 360 array( 361 'key' => 'foo', 362 'value' => 'bar', 363 ), 364 ), 365 ) 366 ); 367 368 $this->assertSameSets( array( self::$site_id ), $found ); 369 370 add_site_meta( self::$site_id2, 'foo', 'bar' ); 371 372 $found = get_sites( 373 array( 374 'fields' => 'ids', 375 'meta_query' => array( 376 array( 377 'key' => 'foo', 378 'value' => 'bar', 379 ), 380 ), 381 ) 382 ); 383 384 $this->assertSameSets( array( self::$site_id, self::$site_id2 ), $found ); 385 } 386 387 /** 388 * @ticket 40229 389 */ 390 public function test_update_site_meta_should_bust_get_sites_cache() { 391 if ( ! is_site_meta_supported() ) { 392 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 393 } 394 395 add_site_meta( self::$site_id, 'foo', 'bar' ); 396 add_site_meta( self::$site_id2, 'foo', 'baz' ); 397 398 // Prime cache. 399 $found = get_sites( 400 array( 401 'fields' => 'ids', 402 'meta_query' => array( 403 array( 404 'key' => 'foo', 405 'value' => 'bar', 406 ), 407 ), 408 ) 409 ); 410 411 $this->assertSameSets( array( self::$site_id ), $found ); 412 413 update_site_meta( self::$site_id2, 'foo', 'bar' ); 414 415 $found = get_sites( 416 array( 417 'fields' => 'ids', 418 'meta_query' => array( 419 array( 420 'key' => 'foo', 421 'value' => 'bar', 422 ), 423 ), 424 ) 425 ); 426 427 $this->assertSameSets( array( self::$site_id, self::$site_id2 ), $found ); 428 } 429 430 /** 431 * @ticket 40229 432 */ 433 public function test_delete_site_meta_should_bust_get_sites_cache() { 434 if ( ! is_site_meta_supported() ) { 435 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 436 } 437 438 add_site_meta( self::$site_id, 'foo', 'bar' ); 439 add_site_meta( self::$site_id2, 'foo', 'bar' ); 440 441 // Prime cache. 442 $found = get_sites( 443 array( 444 'fields' => 'ids', 445 'meta_query' => array( 446 array( 447 'key' => 'foo', 448 'value' => 'bar', 449 ), 450 ), 451 ) 452 ); 453 454 $this->assertSameSets( array( self::$site_id, self::$site_id2 ), $found ); 455 456 delete_site_meta( self::$site_id2, 'foo', 'bar' ); 457 458 $found = get_sites( 459 array( 460 'fields' => 'ids', 461 'meta_query' => array( 462 array( 463 'key' => 'foo', 464 'value' => 'bar', 465 ), 466 ), 467 ) 468 ); 469 470 $this->assertSameSets( array( self::$site_id ), $found ); 471 } 472 } 473 474 endif; 257 public function test_lazy_load_site_meta() { 258 if ( ! is_site_meta_supported() ) { 259 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 260 } 261 262 $filter = new MockAction(); 263 add_filter( 'update_blog_metadata_cache', array( $filter, 'filter' ), 10, 2 ); 264 265 $q = new WP_Site_Query( 266 array( 267 'ID' => self::$site_id, 268 ) 269 ); 270 271 $this->assertSameSets( array( (string) self::$site_id ), wp_list_pluck( $q->sites, 'blog_id' ), 'Site query should return the first test site' ); 272 273 $q = new WP_Site_Query( 274 array( 275 'ID' => self::$site_id2, 276 ) 277 ); 278 279 $this->assertSameSets( array( (string) self::$site_id2 ), wp_list_pluck( $q->sites, 'blog_id' ), 'Site query should return the second test site' ); 280 281 get_site_meta( self::$site_id2 ); 282 283 $args = $filter->get_args(); 284 $first = reset( $args ); 285 $site_ids = end( $first ); 286 $this->assertSameSets( $site_ids, array( self::$site_id, self::$site_id2 ), 'This should have two site\'s meta' ); 287 } 288 289 /** 290 * @ticket 58185 291 */ 292 public function test_lazy_load_site_meta_fields_id() { 293 if ( ! is_site_meta_supported() ) { 294 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 295 } 296 297 $filter = new MockAction(); 298 add_filter( 'update_blog_metadata_cache', array( $filter, 'filter' ), 10, 2 ); 299 300 $q = new WP_Site_Query( 301 array( 302 'ID' => self::$site_id, 303 'fields' => 'ids', 304 ) 305 ); 306 307 $this->assertSameSets( array( self::$site_id ), $q->sites, 'Site query should return the first test site' ); 308 309 $q = new WP_Site_Query( 310 array( 311 'ID' => self::$site_id2, 312 'fields' => 'ids', 313 ) 314 ); 315 316 $this->assertSameSets( array( self::$site_id2 ), $q->sites, 'Site query should return the second test site' ); 317 318 get_site_meta( self::$site_id2 ); 319 320 $args = $filter->get_args(); 321 $first = reset( $args ); 322 $site_ids = end( $first ); 323 $this->assertSameSets( $site_ids, array( self::$site_id, self::$site_id2 ), 'This should have two sites meta' ); 324 } 325 326 public function test_query_update_site_meta_cache_false() { 327 if ( ! is_site_meta_supported() ) { 328 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 329 } 330 331 update_site_meta( self::$site_id, 'foo', 'bar' ); 332 333 new WP_Site_Query( 334 array( 335 'ID' => self::$site_id, 336 'update_site_meta_cache' => false, 337 ) 338 ); 339 340 $num_queries = get_num_queries(); 341 get_site_meta( self::$site_id, 'foo', true ); 342 $this->assertSame( 1, get_num_queries() - $num_queries ); 343 } 344 345 /** 346 * @ticket 40229 347 */ 348 public function test_add_site_meta_should_bust_get_sites_cache() { 349 if ( ! is_site_meta_supported() ) { 350 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 351 } 352 353 add_site_meta( self::$site_id, 'foo', 'bar' ); 354 355 // Prime cache. 356 $found = get_sites( 357 array( 358 'fields' => 'ids', 359 'meta_query' => array( 360 array( 361 'key' => 'foo', 362 'value' => 'bar', 363 ), 364 ), 365 ) 366 ); 367 368 $this->assertSameSets( array( self::$site_id ), $found ); 369 370 add_site_meta( self::$site_id2, 'foo', 'bar' ); 371 372 $found = get_sites( 373 array( 374 'fields' => 'ids', 375 'meta_query' => array( 376 array( 377 'key' => 'foo', 378 'value' => 'bar', 379 ), 380 ), 381 ) 382 ); 383 384 $this->assertSameSets( array( self::$site_id, self::$site_id2 ), $found ); 385 } 386 387 /** 388 * @ticket 40229 389 */ 390 public function test_update_site_meta_should_bust_get_sites_cache() { 391 if ( ! is_site_meta_supported() ) { 392 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 393 } 394 395 add_site_meta( self::$site_id, 'foo', 'bar' ); 396 add_site_meta( self::$site_id2, 'foo', 'baz' ); 397 398 // Prime cache. 399 $found = get_sites( 400 array( 401 'fields' => 'ids', 402 'meta_query' => array( 403 array( 404 'key' => 'foo', 405 'value' => 'bar', 406 ), 407 ), 408 ) 409 ); 410 411 $this->assertSameSets( array( self::$site_id ), $found ); 412 413 update_site_meta( self::$site_id2, 'foo', 'bar' ); 414 415 $found = get_sites( 416 array( 417 'fields' => 'ids', 418 'meta_query' => array( 419 array( 420 'key' => 'foo', 421 'value' => 'bar', 422 ), 423 ), 424 ) 425 ); 426 427 $this->assertSameSets( array( self::$site_id, self::$site_id2 ), $found ); 428 } 429 430 /** 431 * @ticket 40229 432 */ 433 public function test_delete_site_meta_should_bust_get_sites_cache() { 434 if ( ! is_site_meta_supported() ) { 435 $this->markTestSkipped( 'Test only runs with the blogmeta database table installed.' ); 436 } 437 438 add_site_meta( self::$site_id, 'foo', 'bar' ); 439 add_site_meta( self::$site_id2, 'foo', 'bar' ); 440 441 // Prime cache. 442 $found = get_sites( 443 array( 444 'fields' => 'ids', 445 'meta_query' => array( 446 array( 447 'key' => 'foo', 448 'value' => 'bar', 449 ), 450 ), 451 ) 452 ); 453 454 $this->assertSameSets( array( self::$site_id, self::$site_id2 ), $found ); 455 456 delete_site_meta( self::$site_id2, 'foo', 'bar' ); 457 458 $found = get_sites( 459 array( 460 'fields' => 'ids', 461 'meta_query' => array( 462 array( 463 'key' => 'foo', 464 'value' => 'bar', 465 ), 466 ), 467 ) 468 ); 469 470 $this->assertSameSets( array( self::$site_id ), $found ); 471 } 472 }
Note: See TracChangeset
for help on using the changeset viewer.