Changeset 61772
- Timestamp:
- 02/28/2026 12:12:59 AM (5 days ago)
- Location:
- trunk
- Files:
-
- 2 edited
-
src/wp-includes/ms-blogs.php (modified) (1 diff)
-
tests/phpunit/tests/multisite/wpCacheSwitchToBlogFallback.php (modified) (30 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/ms-blogs.php
r61760 r61772 615 615 $non_persistent_groups = false; 616 616 617 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) ) { 618 $global_groups = array_keys( $wp_object_cache->global_groups ); 619 $all_groups = array_fill_keys( array_keys( $wp_object_cache->cache ), true ); 620 $non_persistent_groups = array_keys( array_diff_key( $all_groups, $wp_object_cache->global_groups ) ); 617 if ( is_object( $wp_object_cache ) && isset( $wp_object_cache->global_groups ) && is_array( $wp_object_cache->global_groups ) ) { 618 619 // Get the global groups as they are. 620 $group_names = $wp_object_cache->global_groups; 621 622 // Get global group keys if non-numeric array. 623 if ( ! wp_is_numeric_array( $group_names ) ) { 624 $group_names = array_keys( $group_names ); 625 } 626 627 $global_groups = $group_names; 628 629 /* 630 * Non-persistent groups: Check for no_mc_groups first (memcached drop-in). 631 * Fall back to cache structure (default cache). 632 */ 633 if ( isset( $wp_object_cache->no_mc_groups ) && is_array( $wp_object_cache->no_mc_groups ) && ! empty( $wp_object_cache->no_mc_groups ) ) { 634 $non_persistent_groups = $wp_object_cache->no_mc_groups; 635 } elseif ( isset( $wp_object_cache->cache ) && is_array( $wp_object_cache->cache ) ) { 636 $all_groups = array_keys( $wp_object_cache->cache ); 637 $non_persistent_groups = array_values( array_diff( $all_groups, $global_groups ) ); 638 } 621 639 } 622 640 -
trunk/tests/phpunit/tests/multisite/wpCacheSwitchToBlogFallback.php
r61760 r61772 73 73 74 74 /** 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 75 108 * Test that wp_cache_switch_to_blog() is always available. 76 109 * … … 94 127 95 128 /** 96 * Test that wp_cache_switch_to_blog_fallback() reinitializes the cache. 97 * 98 * @ticket 23290 99 * @covers ::wp_cache_switch_to_blog_fallback 100 */ 101 public function test_reinitializes_cache() { 129 * Test that cache remains functional after fallback reinitialization. 130 * 131 * The fallback reinitializes the cache object. This test verifies the cache 132 * continues to work after reinitialization. 133 * 134 * @ticket 23290 135 * @covers ::wp_cache_switch_to_blog_fallback 136 */ 137 public function test_cache_remains_functional_after_fallback() { 102 138 103 139 // Set some cache data before switching. … … 108 144 $this->call_cache_switch_fallback(); 109 145 110 // Verify the cache has been reinitialized (non-global groups should be cleared). 111 $this->assertFalse( wp_cache_get( 'test_key', 'test_group' ) ); 146 // Verify cache is reinitialized and remains functional. 147 // Set new data in a non-global group after fallback. 148 wp_cache_set( 'test_key', 'test_value_after_fallback', 'test_group' ); 149 $this->assertSame( 'test_value_after_fallback', wp_cache_get( 'test_key', 'test_group' ) ); 112 150 } 113 151 … … 138 176 139 177 // Verify key global groups are still present. 140 $this->assert ArrayHasKey( 'users', $wp_object_cache->global_groups);141 $this->assert ArrayHasKey( 'user_meta', $wp_object_cache->global_groups);142 $this->assert ArrayHasKey( 'site-options', $wp_object_cache->global_groups);178 $this->assert ); 179 $this->assert ); 180 $this->assert ); 143 181 } 144 182 … … 197 235 ); 198 236 237 238 199 239 foreach ( $expected_groups as $group ) { 200 $this->assertContains( $group, array_keys( $wp_object_cache->global_groups ));240 $this->assertContains( $group, ); 201 241 } 202 242 } 203 243 204 244 /** 205 * Test that wp_cache_switch_to_blog_fallback() preserves non-persistent groups from cache.206 * 207 * When the fallback is called, it a nalyzes which groups exist in the cache->cache array208 * (excluding global groups) to determine non-persistent groups that should be preserved.209 * This test verifies that groups identified from the cache structure can still be used210 * after the fallback reinitializes.211 * 212 * @ticket 23290213 * @ covers ::wp_cache_switch_to_blog_fallback214 * /215 public function test_preserves_non_persistent_groups_from_cache_object() {216 global $wp_object_cache;217 218 // Verify we have access to the cache structure. 219 $this->assertObjectHasProperty( 'cache', $wp_object_cache );245 * Test that . 246 * 247 * When the fallback is called, it a 248 * 249 * 250 * 251 * 252 * 253 * @ 254 * 255 256 257 global $wp_object_cache; 258 259 220 260 $this->assertObjectHasProperty( 'global_groups', $wp_object_cache ); 221 261 … … 224 264 wp_cache_set( 'plugin_key', 'plugin_data', 'plugins' ); 225 265 226 // Verify they're in the cache structure before fallback.227 $this->assertArrayHasKey( 'counts', $wp_object_cache->cache );228 $this->assertArrayHasKey( 'plugins', $wp_object_cache->cache );229 230 266 /* 231 * Call the fallback function, which should identify thesegroups232 * from the cache structure before reinitializing.267 * Call the fallback function, which should identify groups 268 * . 233 269 */ 234 270 $this->call_cache_switch_fallback(); … … 238 274 239 275 /* 240 * The data is cleared (cache is reinitialized), but the groups should be re-addable.276 * The . 241 277 * Set new data in those same non-persistent groups to verify they're preserved. 242 278 */ … … 345 381 346 382 /** 347 * Test that multiple calls to wp_cache_switch_to_blog_fallback() work correctly. 348 349 /** 350 * Test that non-global cache data is properly cleared during fallback. 351 * 352 * @ticket 23290 353 */ 354 public function test_non_global_cache_data_is_cleared() { 383 * Test that non-global cache groups remain writable after fallback. 384 * 385 * The fallback reinitializes the cache, which clears non-persistent group data. 386 * This test verifies that non-global groups are still usable after reinitialization. 387 * 388 * @ticket 23290 389 * @covers ::wp_cache_switch_to_blog_fallback 390 */ 391 public function test_non_global_groups_remain_writable_after_fallback() { 355 392 356 393 // Set cache data in various non-global groups. … … 369 406 $this->call_cache_switch_fallback(); 370 407 371 // Verify all non-global cache data is cleared. 372 $this->assertFalse( wp_cache_get( 'post_key', 'posts' ) ); 373 $this->assertFalse( wp_cache_get( 'term_key', 'terms' ) ); 374 $this->assertFalse( wp_cache_get( 'option_key', 'options' ) ); 375 $this->assertFalse( wp_cache_get( 'default_key', 'default' ) ); 408 /* 409 * After fallback, verify non-global groups remain writable by setting 410 * new data. This tests the cache is functional after reinitialization. 411 */ 412 wp_cache_set( 'post_key', 'post_value_after_fallback', 'posts' ); 413 wp_cache_set( 'term_key', 'term_value_after_fallback', 'terms' ); 414 wp_cache_set( 'option_key', 'option_value_after_fallback', 'options' ); 415 wp_cache_set( 'default_key', 'default_value_after_fallback', 'default' ); 416 417 $this->assertSame( 'post_value_after_fallback', wp_cache_get( 'post_key', 'posts' ) ); 418 $this->assertSame( 'term_value_after_fallback', wp_cache_get( 'term_key', 'terms' ) ); 419 $this->assertSame( 'option_value_after_fallback', wp_cache_get( 'option_key', 'options' ) ); 420 $this->assertSame( 'default_value_after_fallback', wp_cache_get( 'default_key', 'default' ) ); 376 421 } 377 422 … … 385 430 */ 386 431 public function test_preserves_many_custom_global_groups() { 387 global $wp_object_cache;388 432 389 433 // Add multiple custom global groups. … … 400 444 // Verify they were added. 401 445 foreach ( $custom_groups as $group ) { 402 $this->assert ArrayHasKey( $group, $wp_object_cache->global_groups);446 $this->assert ); 403 447 } 404 448 … … 408 452 // Verify all custom global groups are still configured. 409 453 foreach ( $custom_groups as $group ) { 410 $this->assert ArrayHasKey( $group, $wp_object_cache->global_groups, "Custom group '{$group}' should be preserved.");454 $this->assert ); 411 455 } 412 456 … … 436 480 // Should fall back to default global groups. 437 481 $this->assertNotEmpty( $wp_object_cache->global_groups ); 438 $this->assert ArrayHasKey( 'users', $wp_object_cache->global_groups);482 $this->assert ); 439 483 } 440 484 … … 449 493 */ 450 494 public function test_preserves_both_global_and_non_persistent_groups() { 451 global $wp_object_cache;452 495 453 496 // Add a custom global group. … … 464 507 465 508 // Global group configuration should be preserved. 466 $this->assert ArrayHasKey( 'my_global', $wp_object_cache->global_groups);509 $this->assert ); 467 510 468 511 // All groups should still be functional after fallback. … … 512 555 513 556 // Custom global groups should still be configured after switching. 514 global $wp_object_cache; 515 $this->assertArrayHasKey( 'my_plugin_users', $wp_object_cache->global_groups ); 516 $this->assertArrayHasKey( 'my_plugin_settings', $wp_object_cache->global_groups ); 557 $this->assert_global_group_exists( 'my_plugin_users' ); 558 $this->assert_global_group_exists( 'my_plugin_settings' ); 517 559 } 518 560 … … 534 576 $this->call_cache_switch_fallback(); 535 577 536 // After fallback, the cache is reinitialized, so data is cleared. 537 $this->assertFalse( wp_cache_get( 'my_key', 'posts' ) ); 538 539 // But we can still set blog-specific data. 578 // Cache should remain usable for blog-specific data after fallback. 540 579 wp_cache_set( 'my_key', 'new_value', 'posts' ); 541 580 $this->assertSame( 'new_value', wp_cache_get( 'my_key', 'posts' ) ); … … 547 586 548 587 /** 549 * Test fallback correctly identifies non-persistent groups from cache structure.550 * 551 * The fallback a nalyzes the existing cache structure to determine which groups552 * are non-persistent (not in global_groups). This test verifies that analysis works.553 * 554 * @ticket 23290 555 * @covers ::wp_cache_switch_to_blog_fallback 556 */ 557 public function test_ identifies_non_persistent_groups_from_cache() {588 * Test . 589 * 590 * The fallback a 591 * . 592 * 593 * @ticket 23290 594 * @covers ::wp_cache_switch_to_blog_fallback 595 */ 596 public function test_() { 558 597 global $wp_object_cache; 559 598 … … 599 638 */ 600 639 public function test_ticket_23290_non_persistent_groups_are_maintained() { 601 global $wp_object_cache;602 640 603 641 // Simulate a plugin adding a custom non-persistent group by populating the cache. … … 605 643 wp_cache_set( 'plugin_cache_2', 'data2', 'another_plugin' ); 606 644 607 // These groups now exist in the cache structure.608 $this->assertArrayHasKey( 'my_plugin_cache', $wp_object_cache->cache );609 $this->assertArrayHasKey( 'another_plugin', $wp_object_cache->cache );610 611 645 // Before fix: calling the fallback would lose these groups. 612 646 // After fix: they should be re-added to the cache configuration. … … 632 666 */ 633 667 public function test_restore_current_blog_with_fallback() { 634 global $wp_object_cache;635 668 636 669 $original_blog_id = get_current_blog_id(); … … 641 674 642 675 // Store initial state. 643 $this->assert ArrayHasKey( 'custom_for_restore', $wp_object_cache->global_groups);676 $this->assert ); 644 677 645 678 // Switch to another blog. … … 648 681 649 682 // Verify custom global group is still there after switch. 650 $this->assert ArrayHasKey( 'custom_for_restore', $wp_object_cache->global_groups);683 $this->assert ); 651 684 652 685 // Restore to original blog. … … 657 690 658 691 // Custom global group configuration should still be present after restore. 659 $this->assert ArrayHasKey( 'custom_for_restore', $wp_object_cache->global_groups);692 $this->assert ); 660 693 } 661 694 … … 667 700 */ 668 701 public function test_nested_blog_switches_with_fallback() { 669 global $wp_object_cache;670 702 671 703 $original_blog_id = get_current_blog_id(); … … 679 711 switch_to_blog( $blog_id_1 ); 680 712 $this->assertSame( $blog_id_1, get_current_blog_id() ); 681 $this->assert ArrayHasKey( 'nested_test_group', $wp_object_cache->global_groups);713 $this->assert ); 682 714 683 715 // Level 2: Switch to blog 2 from blog 1. 684 716 switch_to_blog( $blog_id_2 ); 685 717 $this->assertSame( $blog_id_2, get_current_blog_id() ); 686 $this->assert ArrayHasKey( 'nested_test_group', $wp_object_cache->global_groups);718 $this->assert ); 687 719 688 720 // Restore from level 2 to level 1. 689 721 restore_current_blog(); 690 722 $this->assertSame( $blog_id_1, get_current_blog_id() ); 691 $this->assert ArrayHasKey( 'nested_test_group', $wp_object_cache->global_groups);723 $this->assert ); 692 724 693 725 // Restore from level 1 to original. 694 726 restore_current_blog(); 695 727 $this->assertSame( $original_blog_id, get_current_blog_id() ); 696 $this->assert ArrayHasKey( 'nested_test_group', $wp_object_cache->global_groups);728 $this->assert ); 697 729 } 698 730 … … 718 750 719 751 // Verify the group is still there after each call. 720 $this->assert ArrayHasKey( 'rapid_test', $wp_object_cache->global_groups);752 $this->assert ); 721 753 722 754 // The number of global groups should remain consistent. … … 726 758 727 759 /** 728 * Test fallback behavior when wp_cache_add_global_groups() doesn't exist.729 * 730 * Some cache drop-ins might not have the wp_cache_add_global_groups() function.731 * Th e fallback should handle this gracefully without errors.732 * 733 * @ticket 23290 734 * @covers ::wp_cache_switch_to_blog_fallback 735 */ 736 public function test_ handles_missing_wp_cache_add_global_groups_function() {760 * Test . 761 * 762 * . 763 * Th. 764 * 765 * @ticket 23290 766 * @covers ::wp_cache_switch_to_blog_fallback 767 */ 768 public function test_() { 737 769 global $wp_object_cache; 738 770 … … 762 794 763 795 /** 764 * Test fallback behavior when wp_cache_add_non_persistent_groups() doesn't exist.765 * 766 * Some cache drop-ins might not have the wp_cache_add_non_persistent_groups() function.767 * Th e fallback should handle this gracefully without errors.768 * 769 * @ticket 23290 770 * @covers ::wp_cache_switch_to_blog_fallback 771 */ 772 public function test_ handles_missing_wp_cache_add_non_persistent_groups_function() {796 * Test . 797 * 798 * . 799 * Th. 800 * 801 * @ticket 23290 802 * @covers ::wp_cache_switch_to_blog_fallback 803 */ 804 public function test_() { 773 805 global $wp_object_cache; 774 806 … … 918 950 $this->call_cache_switch_fallback(); 919 951 920 // Regular non-global cache data should be cleared. 921 $this->assertFalse( wp_cache_get( 'regular_cache_key', 'custom_group' ) ); 952 // Regular non-global cache groups should remain usable. 953 wp_cache_set( 'regular_cache_key', 'regular_cache_value_after_fallback', 'custom_group' ); 954 $this->assertSame( 'regular_cache_value_after_fallback', wp_cache_get( 'regular_cache_key', 'custom_group' ) ); 922 955 923 956 // Site transients should persist (they're in the global 'site-transient' group).
Note: See TracChangeset
for help on using the changeset viewer.