Skip to content

Commit

Permalink
PHP Unit Tests: Use global transients (#37122)
Browse files Browse the repository at this point in the history
* Testing whether changing to the pre_site_transient_filter fixes the PHP unit tests, reflecting the change in wordpress/develop 54499

* Use global transients

Co-authored-by: George Mamadashvili <[email protected]>
  • Loading branch information
ramonjd and Mamaduka authored Dec 5, 2021
1 parent d4e57dc commit 8073d19
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/class-wp-rest-url-details-controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ private function build_cache_key_for_url( $url ) {
* @return mixed The value from the cache.
*/
private function get_cache( $key ) {
return get_transient( $key );
return get_site_transient( $key );
}

/**
Expand All @@ -406,7 +406,7 @@ private function set_cache( $key, $data = '' ) {
*/
$cache_expiration = apply_filters( 'rest_url_details_cache_expiration', $ttl );

return set_transient( $key, $data, $cache_expiration );
return set_site_transient( $key, $data, $cache_expiration );
}

/**
Expand Down
6 changes: 3 additions & 3 deletions phpunit/class-wp-rest-url-details-controller-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,11 @@ function( $args, $url ) {
public function test_will_return_from_cache_if_populated() {
$transient_name = 'g_url_details_response_' . md5( static::$url_placeholder );

remove_filter( "pre_transient_{$transient_name}", '__return_null' );
remove_filter( "pre_site_transient_{$transient_name}", '__return_null' );

// Force cache to return a known value as the remote URL http response body.
add_filter(
"pre_transient_{$transient_name}",
"pre_site_transient_{$transient_name}",
function() {
return '<html><head><title>This value from cache.</title></head><body></body></html>';
}
Expand All @@ -301,7 +301,7 @@ function() {
// Data should be that from cache not from mocked network response.
$this->assertContains( 'This value from cache', $data['title'] );

remove_all_filters( "pre_transient_{$transient_name}" );
remove_all_filters( "pre_site_transient_{$transient_name}" );
}

public function test_allows_filtering_data_retrieved_for_a_given_url() {
Expand Down

0 comments on commit 8073d19

Please sign in to comment.