Extract HTTP caching into new test base class

This commit is contained in:
Alex Kirk 2022-12-09 11:59:11 +01:00
parent 7ebb89e92e
commit be369b11e5
5 changed files with 151 additions and 97 deletions

View file

@ -30,3 +30,4 @@ function _manually_load_plugin() {
// Start up the WP testing environment.
require $_tests_dir . '/includes/bootstrap.php';
require __DIR__ . '/class-activitypub-testcase-cache-http.php';

View file

@ -0,0 +1,120 @@
<?php
class ActivityPub_TestCase_Cache_HTTP extends \WP_UnitTestCase {
public function set_up() {
parent::set_up();
// Manually activate the REST server.
global $wp_rest_server;
$wp_rest_server = new \Spy_REST_Server();
$this->server = $wp_rest_server;
do_action( 'rest_api_init' );
add_filter(
'rest_url',
function() {
return get_option( 'home' ) . '/wp-json/';
}
);
add_filter( 'pre_http_request', array( get_called_class(), 'pre_http_request' ), 10, 3 );
add_filter( 'http_response', array( get_called_class(), 'http_response' ), 10, 3 );
add_filter( 'http_request_host_is_external', array( get_called_class(), 'http_request_host_is_external' ), 10, 2 );
add_filter( 'http_request_args', array( get_called_class(), 'http_request_args' ), 10, 2 );
}
public function tear_down() {
remove_filter( 'pre_http_request', array( get_called_class(), 'pre_http_request' ) );
remove_filter( 'http_response', array( get_called_class(), 'http_response' ) );
remove_filter( 'http_request_host_is_external', array( get_called_class(), 'http_request_host_is_external' ) );
remove_filter( 'http_request_args', array( get_called_class(), 'http_request_args' ) );
parent::tear_down();
}
public static function http_request_host_is_external( $in, $host ) {
if ( in_array( $host, array( 'mastodon.local' ), true ) ) {
return true;
}
return $in;
}
public static function http_request_args( $args, $url ) {
if ( in_array( wp_parse_url( $url, PHP_URL_HOST ), array( 'mastodon.local' ), true ) ) {
$args['reject_unsafe_urls'] = false;
}
return $args;
}
public static function pre_http_request( $preempt, $request, $url ) {
$p = wp_parse_url( $url );
$cache = __DIR__ . '/fixtures/' . sanitize_title( $p['host'] . '-' . $p['path'] ) . '.json';
if ( file_exists( $cache ) ) {
return apply_filters(
'fake_http_response',
json_decode( file_get_contents( $cache ), true ), // phpcs:ignore
$p['scheme'] . '://' . $p['host'],
$url,
$request
);
}
$home_url = home_url();
// Pretend the url now is the requested one.
update_option( 'home', $p['scheme'] . '://' . $p['host'] );
$rest_prefix = home_url() . '/wp-json';
if ( false === strpos( $url, $rest_prefix ) ) {
// Restore the old home_url.
update_option( 'home', $home_url );
return $preempt;
}
$url = substr( $url, strlen( $rest_prefix ) );
$r = new \WP_REST_Request( $request['method'], $url );
if ( ! empty( $request['body'] ) ) {
foreach ( $request['body'] as $key => $value ) {
$r->set_param( $key, $value );
}
}
global $wp_rest_server;
$response = $wp_rest_server->dispatch( $r );
// Restore the old url.
update_option( 'home', $home_url );
return apply_filters(
'fake_http_response',
array(
'headers' => array(
'content-type' => 'text/json',
),
'body' => wp_json_encode( $response->data ),
'response' => array(
'code' => $response->status,
),
),
$p['scheme'] . '://' . $p['host'],
$url,
$request
);
}
public static function http_response( $response, $args, $url ) {
$p = wp_parse_url( $url );
$cache = __DIR__ . '/fixtures/' . sanitize_title( $p['host'] . '-' . $p['path'] ) . '.json';
if ( ! file_exists( $cache ) ) {
$headers = wp_remote_retrieve_headers( $response );
file_put_contents( // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents
$cache,
wp_json_encode(
array(
'headers' => $headers->getAll(),
'body' => wp_remote_retrieve_body( $response ),
'response' => array(
'code' => wp_remote_retrieve_response_code( $response ),
),
)
)
);
}
return $response;
}
}

File diff suppressed because one or more lines are too long

View file

@ -10,12 +10,11 @@ class Test_Activitypub_Activity extends WP_UnitTestCase {
add_filter(
'activitypub_extract_mentions',
function( $mentions, $post ) {
$mentions[] = 'https://example.com/alex';
function( $mentions ) {
$mentions['@alex'] = 'https://example.com/alex';
return $mentions;
},
10,
2
10
);
$activitypub_post = new \Activitypub\Model\Post( $post );
@ -26,7 +25,7 @@ class Test_Activitypub_Activity extends WP_UnitTestCase {
$this->assertContains( \get_rest_url( null, '/activitypub/1.0/users/1/followers' ), $activitypub_activity->get_cc() );
$this->assertContains( 'https://example.com/alex', $activitypub_activity->get_cc() );
remove_all_filters( 'activitypub_from_post_object' );
remove_all_filters( 'activitypub_extract_mentions' );
\wp_trash_post( $post );
}
}

View file

@ -1,9 +1,10 @@
<?php
class Test_Friends_Feed_Parser_ActivityPub extends \WP_UnitTestCase {
class Test_Friends_Feed_Parser_ActivityPub extends ActivityPub_TestCase_Cache_HTTP {
public static $users = array();
private $friend_id;
private $friend_name;
private $friend_nicename;
private $actor;
public function test_incoming_post() {
@ -224,11 +225,8 @@ class Test_Friends_Feed_Parser_ActivityPub extends \WP_UnitTestCase {
}
public function tear_down() {
remove_filter( 'pre_http_request', array( get_called_class(), 'pre_http_request' ) );
remove_filter( 'http_response', array( get_called_class(), 'http_response' ) );
remove_filter( 'http_request_host_is_external', array( get_called_class(), 'http_request_host_is_external' ) );
remove_filter( 'http_request_args', array( get_called_class(), 'http_request_args' ) );
remove_filter( 'pre_get_remote_metadata_by_actor', array( get_called_class(), 'pre_get_remote_metadata_by_actor' ) );
parent::tear_down();
}
public static function pre_get_remote_metadata_by_actor( $pre, $actor ) {
@ -237,90 +235,4 @@ class Test_Friends_Feed_Parser_ActivityPub extends \WP_UnitTestCase {
}
return $pre;
}
public static function http_request_host_is_external( $in, $host ) {
if ( in_array( $host, array( 'mastodon.local' ), true ) ) {
return true;
}
return $in;
}
public static function http_request_args( $args, $url ) {
if ( in_array( wp_parse_url( $url, PHP_URL_HOST ), array( 'mastodon.local' ), true ) ) {
$args['reject_unsafe_urls'] = false;
}
return $args;
}
public static function pre_http_request( $preempt, $request, $url ) {
$p = wp_parse_url( $url );
$cache = __DIR__ . '/fixtures/' . sanitize_title( $p['host'] . '-' . $p['path'] ) . '.json';
if ( file_exists( $cache ) ) {
return apply_filters(
'fake_http_response',
json_decode( file_get_contents( $cache ), true ), // phpcs:ignore
$p['scheme'] . '://' . $p['host'],
$url,
$request
);
}
$home_url = home_url();
// Pretend the url now is the requested one.
update_option( 'home', $p['scheme'] . '://' . $p['host'] );
$rest_prefix = home_url() . '/wp-json';
if ( false === strpos( $url, $rest_prefix ) ) {
// Restore the old home_url.
update_option( 'home', $home_url );
return $preempt;
}
$url = substr( $url, strlen( $rest_prefix ) );
$r = new \WP_REST_Request( $request['method'], $url );
if ( ! empty( $request['body'] ) ) {
foreach ( $request['body'] as $key => $value ) {
$r->set_param( $key, $value );
}
}
global $wp_rest_server;
$response = $wp_rest_server->dispatch( $r );
// Restore the old url.
update_option( 'home', $home_url );
return apply_filters(
'fake_http_response',
array(
'headers' => array(
'content-type' => 'text/json',
),
'body' => wp_json_encode( $response->data ),
'response' => array(
'code' => $response->status,
),
),
$p['scheme'] . '://' . $p['host'],
$url,
$request
);
}
public static function http_response( $response, $args, $url ) {
$p = wp_parse_url( $url );
$cache = __DIR__ . '/fixtures/' . sanitize_title( $p['host'] . '-' . $p['path'] ) . '.json';
if ( ! file_exists( $cache ) ) {
$headers = wp_remote_retrieve_headers( $response );
file_put_contents( // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_file_put_contents
$cache,
wp_json_encode(
array(
'headers' => $headers->getAll(),
'body' => wp_remote_retrieve_body( $response ),
'response' => array(
'code' => wp_remote_retrieve_response_code( $response ),
),
)
)
);
}
return $response;
}
}