Skip to content

Commit

Permalink
Transient tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterwilsoncc committed Jun 23, 2024
1 parent b04edac commit 753dc25
Show file tree
Hide file tree
Showing 2 changed files with 235 additions and 0 deletions.
167 changes: 167 additions & 0 deletions tests/phpunit/tests/option/siteTransient.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,171 @@ public function test_set_site_transient_is_not_stored_as_autoload_option() {

$this->assertArrayNotHasKey( '_site_transient_' . $key, $options );
}

/**
* Ensure autoloaded transient does not query database (single site).
*
* group @ms-excluded
* @ticket 61193
* @ticket 61053
*/
public function test_autoloaded_site_transient_does_not_query_database_single_site() {
$key = 'test_transient';
$option_name = '_site_transient_' . $key;
$value = 'test_value';

// Set the transient.
set_site_transient( $key, $value );

// Clear the options caches.
wp_cache_delete( $option_name, 'options' );
wp_cache_delete( 'notoptions', 'options' );
wp_cache_delete( 'alloptions', 'options' );

// Prime the alloptions cache.
$alloptions = wp_load_alloptions();

// Ensure the transient is autoloaded.
$this->assertArrayHasKey( $option_name, $alloptions, 'Expected the transient to be autoloaded.' );

$before_queries = get_num_queries();
$this->assertSame( $value, get_site_transient( $key ) );
$transient_queries = get_num_queries() - $before_queries;
$this->assertSame( 0, $transient_queries, 'Expected no database queries to retrieve the transient.' );
}

/**
* Ensure known non-existent transient does not query database (single site).
*
* group @ms-excluded
* @ticket 61193
* @ticket 61053
*/
public function test_non_existent_transient_does_not_make_a_database_call_single_site() {
$key = 'non_existent_test_transient';
$option_name = '_transient_' . $key;

// Ensure the transient doesn't exist.
delete_transient( $key );

// Clear the options caches.
wp_cache_delete( $option_name, 'options' );
wp_cache_delete( 'notoptions', 'options' );
wp_cache_delete( 'alloptions', 'options' );

// Prime the alloptions & transient cache.
$alloptions = wp_load_alloptions();

// Ensure the transient is not autoloaded.
$this->assertArrayNotHasKey( $option_name, $alloptions, 'Expected the transient to not be autoloaded.' );

$before_queries = get_num_queries();
$this->assertFalse( get_transient( $key ) );
$transient_queries = get_num_queries() - $before_queries;
$this->assertSame( 1, $transient_queries, 'Expected one database query to retrieve the transient the first time.' );

$before_queries = get_num_queries();
$this->assertFalse( get_transient( $key ) );
$transient_queries = get_num_queries() - $before_queries;
$this->assertSame( 0, $transient_queries, 'Expected no database queries to retrieve the transient a second time.' );
}

/**
* Ensure known non-existent transient does not query database (single site).
*
* group @ms-excluded
* @ticket 61193
* @ticket 61053
*/
public function test_known_transient_does_not_make_a_database_call_single_site() {
$key = 'test_transient';
$option_name = '_transient_' . $key;
$value = 'test_value';
$timeout = YEAR_IN_SECONDS;

// Set the transient.
set_transient( $key, $value, $timeout );

// Clear the options caches.
wp_cache_delete( $option_name, 'options' );
wp_cache_delete( 'notoptions', 'options' );
wp_cache_delete( 'alloptions', 'options' );

// Prime the alloptions & transient cache.
$alloptions = wp_load_alloptions();

// Ensure the transient is not autoloaded.
$this->assertArrayNotHasKey( $option_name, $alloptions, 'Expected the transient to not be autoloaded.' );

$before_queries = get_num_queries();
$this->assertSame( $value, get_transient( $key ) );
$transient_queries = get_num_queries() - $before_queries;
$this->assertSame( 1, $transient_queries, 'Expected one database query to retrieve the transient the first time.' );

$before_queries = get_num_queries();
$this->assertSame( $value, get_transient( $key ) );
$transient_queries = get_num_queries() - $before_queries;
$this->assertSame( 0, $transient_queries, 'Expected no database queries to retrieve the transient a second time.' );
}

/**
* Ensure primed transient does not query database (multi site).
*
* group @ms-required
* @ticket 61193
* @ticket 61053
*/
public function test_primed_site_transient_does_not_query_database_multi_site() {
$key = 'test_transient';
$option_name = '_site_transient_' . $key;
$value = 'test_value';
$network_id = get_current_network_id();

// Set the transient.
set_site_transient( $key, $value );

// Clear the options caches.
wp_cache_delete( "{$network_id}:{$option_name}", 'site-options' );
wp_cache_delete( "{$network_id}:notoptions", 'site-options' );

$before_queries = get_num_queries();
$this->assertSame( $value, get_transient( $key ) );
$transient_queries = get_num_queries() - $before_queries;
$this->assertSame( 1, $transient_queries, 'Expected one database query to retrieve the transient the first time.' );

$before_queries = get_num_queries();
$this->assertSame( $value, get_transient( $key ) );
$transient_queries = get_num_queries() - $before_queries;
$this->assertSame( 0, $transient_queries, 'Expected no database queries to retrieve the transient a second time.' );
}

/**
* Ensure primed unknown transient does not query database (multi site).
*
* group @ms-required
* @ticket 61193
* @ticket 61053
*/
public function test_primed_non_existent_site_transient_does_not_query_database_multi_site() {
$key = 'non_existent_transient';
$option_name = '_site_transient_' . $key;
$network_id = get_current_network_id();

// Delete the transient.
delete_site_transient( $key );

// Clear the options caches.
wp_cache_delete( "{$network_id}:{$option_name}", 'site-options' );
wp_cache_delete( "{$network_id}:notoptions", 'site-options' );

$before_queries = get_num_queries();
$this->assertFalse( get_transient( $key ) );
$transient_queries = get_num_queries() - $before_queries;
$this->assertSame( 1, $transient_queries, 'Expected one database query to retrieve the transient the first time.' );

$before_queries = get_num_queries();
$this->assertFalse( get_transient( $key ) );
$transient_queries = get_num_queries() - $before_queries;
$this->assertSame( 0, $transient_queries, 'Expected no database queries to retrieve the transient a second time.' );
}
}
68 changes: 68 additions & 0 deletions tests/phpunit/tests/option/transient.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,74 @@ function ( $query ) use ( &$queries ) {
$this->assertNotContains( $unexpected_query_timeout, $queries, 'Unexpected query of transient timeout option individually.' );
}

/**
* Ensure get_transient() doesn't query the database for a transient without a timeout.
*
* @ticket 61193
*
* @covers ::get_transient
*/
public function test_autoloaded_transient_does_not_make_a_database_call() {
$key = 'test_transient';
$option_name = '_transient_' . $key;
$value = 'test_value';

// Set transient without a timeout.
set_transient( $key, $value );

// Clear the options caches.
wp_cache_delete( $option_name, 'options' );
wp_cache_delete( 'notoptions', 'options' );
wp_cache_delete( 'alloptions', 'options' );

// Prime the alloptions cache.
$alloptions = wp_load_alloptions();

// Ensure the transient is autoloaded.
$this->assertArrayHasKey( $option_name, $alloptions, 'Expected the transient to be autoloaded.' );

$before_queries = get_num_queries();
$this->assertSame( $value, get_transient( $key ) );
$transient_queries = get_num_queries() - $before_queries;
$this->assertSame( 0, $transient_queries, 'Expected no database queries to retrieve the transient.' );
}

/**
* Ensure get_transient() doesn't query the database for a non-existent transient.
*
* @ticket 61193
*
* @covers ::get_transient
*/
public function test_non_existent_transient_does_not_make_a_database_call() {
$key = 'non_existent_test_transient';
$option_name = '_transient_' . $key;

// Ensure the transient doesn't exist.
delete_transient( $key );

// Clear the options caches.
wp_cache_delete( $option_name, 'options' );
wp_cache_delete( 'notoptions', 'options' );
wp_cache_delete( 'alloptions', 'options' );

// Prime the alloptions & transient cache.
$alloptions = wp_load_alloptions();

// Ensure the transient is not autoloaded.
$this->assertArrayNotHasKey( $option_name, $alloptions, 'Expected the transient to not be autoloaded.' );

$before_queries = get_num_queries();
$this->assertFalse( get_transient( $key ) );
$transient_queries = get_num_queries() - $before_queries;
$this->assertSame( 1, $transient_queries, 'Expected one database queries to retrieve the transient on first request.' );

$before_queries = get_num_queries();
$this->assertFalse( get_transient( $key ) );
$transient_queries = get_num_queries() - $before_queries;
$this->assertSame( 0, $transient_queries, 'Expected no database queries to retrieve the transient on second request.' );
}

/**
* Ensure set_transient() primes the option cache checking for an existing transient.
*
Expand Down

0 comments on commit 753dc25

Please sign in to comment.