Skip to content

Instantly share code, notes, and snippets.

@peterwilsoncc
Last active June 4, 2024 00:46
Show Gist options
  • Save peterwilsoncc/e271afcc0f5a3a3d01c7d5cda3b8828d to your computer and use it in GitHub Desktop.
Save peterwilsoncc/e271afcc0f5a3a3d01c7d5cda3b8828d to your computer and use it in GitHub Desktop.
Unit tests for https://github.com/WordPress/wordpress-develop/pull/6720 -- the `wp_salt()` function uses a static variable so it's not possible to include these in the test suite.
<?php
/**
* @group pluggable
*
* @covers ::wp_salt
*/
class Tests_Pluggable_wp_salt extends WP_UnitTestCase {
/**
* Test that the signature options of wp_salt() are primed in a single database call (single site).
*
* As the slats can be defined via the `wp-config.php` file, this test is somewhat
* flaky and can not test the priming of the caches if the constant is properly
* defined.
*
* As such it is slightly reversed in that it tests for the absence of a database
* query rather than the presence of a cache priming query.
*
* @dataProvider data_wp_salt_cache_priming
*
* @ticket 59871
*
* @group ms-excluded
*/
public function test_wp_salt_cache_priming_for_single_site( $salt_scheme ) {
// Skip the test if the constant is defined and not the placeholder string.
if (
defined( strtoupper( $salt_scheme ) )
&& 'put your unique phrase here' !== constant( strtoupper( $salt_scheme ) )
) {
$this->markTestSkipped( 'The constant is defined and not the placeholder string.' );
}
// Set the options directly to ensure the option is populated.
update_site_option( "{$salt_scheme}_key", wp_generate_password() );
update_site_option( "{$salt_scheme}_salt", wp_generate_password() );
global $wpdb;
$unexpected_database_query_key = "SELECT option_value FROM {$wpdb->options} WHERE option_name = '{$salt_scheme}_key' LIMIT 1";
$unexpected_database_query_salt = "SELECT option_value FROM {$wpdb->options} WHERE option_name = '{$salt_scheme}_salt' LIMIT 1";
// Clear the options cache to ensure the query is not cached.
wp_cache_delete( "{$salt_scheme}_key", 'options' );
wp_cache_delete( "{$salt_scheme}_salt", 'options' );
// Cache database queries via the query hook.
$queries = array();
add_filter(
'query',
function ( $query ) use ( &$queries ) {
$queries[] = $query;
return $query;
}
);
// Call the function to prime the cache.
wp_salt( $salt_scheme );
// Check that the queries cache does not contain the unexpected queries.
$this->assertNotContains( $unexpected_database_query_key, $queries );
$this->assertNotContains( $unexpected_database_query_salt, $queries );
}
/**
* Test that the signature options of wp_salt() are primed in a single database call (multisite).
*
* See test above for limitations of this test.
*
* @dataProvider data_wp_salt_cache_priming
*
* @ticket 59871
*
* @group ms-required
*/
public function test_wp_salt_cache_priming_for_multisite( $salt_scheme ) {
// Skip the test if the constant is defined and not the placeholder string.
if (
defined( strtoupper( $salt_scheme ) )
&& 'put your unique phrase here' !== constant( strtoupper( $salt_scheme ) )
) {
$this->markTestSkipped( 'The constant is defined and not the placeholder string.' );
}
// Set the options directly to ensure the option is populated.
update_site_option( "{$salt_scheme}_key", wp_generate_password() );
update_site_option( "{$salt_scheme}_salt", wp_generate_password() );
global $wpdb;
$network_id = (int) get_current_network_id();
$unexpected_database_query_key = "SELECT meta_value FROM {$wpdb->sitemeta} WHERE meta_key = '{$salt_scheme}_key' AND site_id = {$network_id}";
$unexpected_database_query_salt = "SELECT meta_value FROM {$wpdb->sitemeta} WHERE meta_key = '{$salt_scheme}_salt' AND site_id = {$network_id}";
// Clear the options cache to ensure the query is not cached.
wp_cache_delete( "{$network_id}:{$salt_scheme}_key", 'site-options' );
wp_cache_delete( "{$network_id}:{$salt_scheme}_salt", 'site-options' );
// Cache database queries via the query hook.
$queries = array();
add_filter(
'query',
function ( $query ) use ( &$queries ) {
$queries[] = $query;
return $query;
}
);
// Call the function to prime the cache.
wp_salt( $salt_scheme );
// Check that the queries cache does not contain the unexpected queries.
$this->assertNotContains( $unexpected_database_query_key, $queries );
$this->assertNotContains( $unexpected_database_query_salt, $queries );
}
public function data_wp_salt_cache_priming() {
return array(
array( 'nonce' ),
array( 'auth' ),
array( 'secure_auth' ),
array( 'logged_in' ),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment