@@ -491,4 +491,56 @@ public function test_wp_cache_delete_multiple() {
491491
492492 $ this ->assertSame ( $ expected , $ found );
493493 }
494+
495+ /**
496+ * Tests that stats() outputs cache group information for serializable data.
497+ *
498+ * @ticket 21650
499+ *
500+ * @covers WP_Object_Cache::stats
501+ */
502+ public function test_stats_with_serializable_data () {
503+ $ this ->cache ->set ( 'key1 ' , 'value1 ' , 'test-group ' );
504+ $ this ->cache ->set ( 'key2 ' , array ( 'a ' , 'b ' , 'c ' ), 'test-group ' );
505+
506+ ob_start ();
507+ $ this ->cache ->stats ();
508+ $ output = ob_get_clean ();
509+
510+ $ this ->assertStringContainsString ( 'test-group ' , $ output , 'stats() output should contain the cache group name. ' );
511+ $ this ->assertStringContainsString ( '<li> ' , $ output , 'stats() output should contain list items. ' );
512+ }
513+
514+ /**
515+ * Tests that stats() does not fatal error when a cache group contains a
516+ * non-serializable object such as SimpleXMLElement, and falls back to
517+ * print_r() for size estimation.
518+ *
519+ * @ticket 21650
520+ *
521+ * @covers WP_Object_Cache::stats
522+ */
523+ public function test_stats_with_non_serializable_simplexml_data () {
524+ if ( ! class_exists ( 'SimpleXMLElement ' ) ) {
525+ $ this ->markTestSkipped ( 'SimpleXMLElement class is not available. ' );
526+ }
527+
528+ $ xml_object = new SimpleXMLElement ( '<root><item>value</item></root> ' );
529+
530+ // Directly inject the SimpleXMLElement into the cache storage to bypass
531+ // any object-clone logic in set(), simulating a real-world scenario where
532+ // a non-serializable object ends up in the cache.
533+ $ cache_property = new ReflectionProperty ( $ this ->cache , 'cache ' );
534+ $ cache_data = $ cache_property ->getValue ( $ this ->cache );
535+ $ cache_data ['xml-group ' ]['item1 ' ] = $ xml_object ;
536+ $ cache_property ->setValue ( $ this ->cache , $ cache_data );
537+
538+ // stats() should not throw a fatal error or exception.
539+ ob_start ();
540+ $ this ->cache ->stats ();
541+ $ output = ob_get_clean ();
542+
543+ $ this ->assertStringContainsString ( 'xml-group ' , $ output , 'stats() output should contain the group name even for non-serializable data. ' );
544+ $ this ->assertStringContainsString ( '<li> ' , $ output , 'stats() output should contain a list item for the non-serializable group. ' );
545+ }
494546}
0 commit comments