wordpress-develop/tests/phpunit/tests/option/getOptions.php
flixos90 81423c3ea6 Options, Meta APIs: Introduce prime_options() to load multiple options with a single database request.
WordPress's `get_option()` function generally relies on making individual database requests for each option, however with the majority of options (in most cases) being autoloaded, i.e. fetched once with a single database request and then stored in (memory) cache.

As part of a greater effort to reduce the amount of options that are unnecessarily autoloaded, this changeset introduces an alternative way to retrieve multiple options in a performant manner, with a single database request. This provides a reasonable alternative for e.g. plugins that use several options which only need to be loaded in a few specific screens.

Specifically, this changeset introduces the following functions:
* `prime_options( $options )` is the foundation to load multiple specific options with a single database request. Only options that aren't already cached (in `alloptions` or an individual cache) are retrieved from the database.
* `prime_options_by_group( $option_group )` is a convenience wrapper function for the above which allows to prime all options of a specific option group (as configured via `register_setting()`).
* `get_options( $options )` is another wrapper function which first primes the requested options and then returns them in an associative array, calling `get_option()` for each of them.

Props mukesh27, joemcgill, costdev, olliejones.
Fixes #58962.


git-svn-id: https://develop.svn.wordpress.org/trunk@56445 602fd350-edb4-49c9-b593-d223f7449a82
2023-08-24 14:40:01 +00:00

92 lines
3.2 KiB
PHP

<?php
/**
* Test get_options().
*
* @group option
*
* @covers ::get_options
*/
class Tests_Option_GetOptions extends WP_UnitTestCase {
/**
* Tests that get_options() retrieves specified options.
*
* @ticket 58962
*/
public function test_get_options() {
// Create some options to prime.
$options_to_prime = array(
'option1',
'option2',
);
/*
* Set values for the options,
* clear the cache for the options,
* check options are not in cache initially.
*/
foreach ( $options_to_prime as $option ) {
update_option( $option, "value_$option", false );
wp_cache_delete( $option, 'options' );
$this->assertFalse( wp_cache_get( $option, 'options' ), "$option was not deleted from the cache." );
}
// Call the get_options function to retrieve the options.
$options = get_options( array( 'option1', 'option2' ) );
// Check that options are now in the cache.
foreach ( $options_to_prime as $option ) {
$this->assertSame( wp_cache_get( $option, 'options' ), get_option( $option ), "$option was not primed." );
}
// Check that the retrieved options are correct.
$this->assertSame( get_option( 'option1' ), $options['option1'], 'Retrieved option1 does not match expected value.' );
$this->assertSame( get_option( 'option2' ), $options['option2'], 'Retrieved option2 does not match expected value.' );
}
/**
* Tests get_options() with an empty input array.
*
* @ticket 58962
*/
public function test_get_options_with_empty_array() {
// Call the get_options function with an empty array.
$options = get_options( array() );
// Make sure the result is an empty array.
$this->assertIsArray( $options, 'An array should have been returned.' );
$this->assertEmpty( $options, 'No options should have been returned.' );
}
/**
* Tests get_options() with options that include some nonexistent options.
*/
public function test_get_options_with_nonexistent_options() {
// Create some options to prime.
$options_to_prime = array(
'option1',
);
// Make sure options are not in cache or database initially.
$this->assertFalse( wp_cache_get( 'option1', 'options' ), 'option1 was not deleted from the cache.' );
$this->assertFalse( wp_cache_get( 'nonexistent_option', 'options' ), 'nonexistent_option was not deleted from the cache.' );
// Call the get_options function with an array that includes a nonexistent option.
$options = get_options( array( 'option1', 'nonexistent_option' ) );
// Check that the retrieved options are correct.
$this->assertSame( get_option( 'option1' ), $options['option1'], 'Retrieved option1 does not match expected value.' );
// Check that options are present in the notoptions cache.
$new_notoptions = wp_cache_get( 'notoptions', 'options' );
foreach ( $options_to_prime as $option ) {
$this->assertTrue( isset( $new_notoptions[ $option ] ), "$option was not added to the notoptions cache." );
}
// Check that the nonexistent option is in the result array.
$this->assertArrayHasKey( 'nonexistent_option', $options, 'Result array should not contain nonexistent_option.' );
$this->assertFalse( $options['nonexistent_option'], 'nonexistent_option is present in option.' );
}
}