Skip to content

Commit

Permalink
Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mukeshpanchal27 committed May 24, 2024
1 parent 46e8087 commit 25dd859
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions tests/phpunit/tests/admin/wpSiteHealth.php
Original file line number Diff line number Diff line change
Expand Up @@ -499,4 +499,80 @@ public function data_object_cache_thresholds() {
array( 'alloptions_bytes', 1000 ),
);
}

/**
* Tests get_test_autoloaded_options() when autoloaded options less than warning size.
*
* @ticket 61276
*
* @covers ::get_test_autoloaded_options()
*/
public function test_wp_autoloaded_options_test_no_warning(): void {
$expected_label = esc_html__( 'Autoloaded options are acceptable' );
$expected_status = 'good';

$result = $this->instance->get_test_autoloaded_options();
$this->assertSame( $expected_label, $result['label'] );
$this->assertSame( $expected_status, $result['status'] );
}

/**
* Tests get_test_autoloaded_options() when autoloaded options more than warning size.
*
* @ticket 61276
*
* @covers ::get_test_autoloaded_options()
*/
public function test_wp_autoloaded_options_test_warning(): void {
self::set_autoloaded_option( 800000 );

$expected_label = esc_html__( 'Autoloaded options could affect performance' );
$expected_status = 'critical';

$result = $this->instance->get_test_autoloaded_options();
$this->assertSame( $expected_label, $result['label'] );
$this->assertSame( $expected_status, $result['status'] );
}

/**
* Tests wp_autoloaded_options_size().
*
* @ticket 61276
*
* @covers ::wp_autoload_values_to_autoload()
*/
public function test_wp_autoloaded_options_size(): void {
global $wpdb;

$autoload_values = wp_autoload_values_to_autoload();

$autoloaded_options_size = $wpdb->get_var(
$wpdb->prepare(
sprintf(
"SELECT SUM(LENGTH(option_value)) FROM $wpdb->options WHERE autoload IN (%s)",
implode( ',', array_fill( 0, count( $autoload_values ), '%s' ) )
),
$autoload_values
)
);
$this->assertEquals( $autoloaded_options_size, $this->instance->wp_autoloaded_options_size() );

// Add autoload option.
$test_option_string = 'test';
$test_option_string_bytes = mb_strlen( $test_option_string, '8bit' );
self::set_autoloaded_option( $test_option_string_bytes );
$this->assertSame( $autoloaded_options_size + $test_option_string_bytes, $this->instance->wp_autoloaded_options_size() );
}

/**
* Sets a test autoloaded option.
*
* @param int $bytes bytes to load in options.
*/
public static function set_autoloaded_option( int $bytes = 800000 ): void {
$heavy_option_string = wp_generate_password( $bytes );

// Force autoloading so that WordPress core does not override it. See https://core.trac.wordpress.org/changeset/57920.
add_option( 'test_set_autoloaded_option', $heavy_option_string, '', true );
}
}

0 comments on commit 25dd859

Please sign in to comment.