Skip to content

Commit f255ebf

Browse files
committed
Tests: Add unit tests for WP_Object_Cache::stats to handle serializable and non-serializable data
1 parent aa72dfe commit f255ebf

2 files changed

Lines changed: 57 additions & 1 deletion

File tree

src/wp-includes/class-wp-object-cache.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -637,7 +637,11 @@ public function stats() {
637637
echo '</p>';
638638
echo '<ul>';
639639
foreach ( $this->cache as $group => $cache ) {
640-
echo '<li><strong>Group:</strong> ' . esc_html( $group ) . ' - ( ' . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>';
640+
try {
641+
echo '<li><strong>Group:</strong> ' . esc_html( $group ) . ' - ( ' . number_format( strlen( serialize( $cache ) ) / KB_IN_BYTES, 2 ) . 'k )</li>';
642+
} catch( Exception $e ) {
643+
echo '<li><strong>Group:</strong> ' . esc_html( $group ) . ' - ( ' . number_format( strlen( print_r( $cache, true ) ) / KB_IN_BYTES, 2 ) . 'k )</li>';
644+
}
641645
}
642646
echo '</ul>';
643647
}

tests/phpunit/tests/cache.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)