1
0
Fork 0
mirror of https://github.com/buddypress/buddypress.git synced 2026-07-23 21:06:56 +08:00
buddypress/tests/phpunit/testcases/core/functions/bpVerifyNonceRequest.php
Mathieu Viet 89a6666dc6 Fully enjoy Yoast’s PHPUnit polyfills
Using these polyfills let us use PHPUnit v9.x for our tests and add PHP 8.1 to our testing matrix. Some additional edits to our PHP unit tests suite were needed:

- Stop using PHPunit deprecated functions.
- Rename some `BP_UnitTestCase` methods to use Yoast's polyfills.
- Edit the PHP Unit test GH action and also run this action on pull requests.
- Update some composer dependencies, remove the one about `phpunit/phpunit:^7.5` and add a new composer script to use PHPUnit v9.x. 

Props renatonascalves, rafiahmedd

Closes https://github.com/buddypress/buddypress/pull/13
Fixes #8649


git-svn-id: https://buddypress.svn.wordpress.org/trunk@13314 cdf35c40-ae34-48e0-9cc9-0c9da1808c22
2022-08-13 08:58:51 +00:00

81 lines
2.4 KiB
PHP

<?php
/**
* @group core
* @group functions
* @group bp_verify_nonce_request
*/
class BP_Tests_Core_Functions_BPVerifyNonceRequest extends BP_UnitTestCase {
private $http_host = '';
private $server_port = '';
private $request_uri = '';
public function set_up() {
parent::set_up();
if ( isset( $_SERVER['HTTP_HOST'] ) ) {
$this->http_host = $_SERVER['HTTP_HOST'];
}
if ( isset( $_SERVER['SERVER_PORT'] ) ) {
$this->server_port = $_SERVER['SERVER_PORT'];
}
if ( isset( $_SERVER['REQUEST_URI'] ) ) {
$this->request_uri = $_SERVER['REQUEST_URI'];
}
}
public function tear_down() {
if ( '' !== $this->http_host ) {
$_SERVER['HTTP_HOST'] = $this->http_host;
}
if ( '' !== $this->server_port ) {
$_SERVER['SERVER_PORT'] = $this->server_port;
}
if ( '' !== $this->request_uri ) {
$_SERVER['REQUEST_URI'] = $this->request_uri;
}
parent::tear_down();
}
public function test_bp_verify_nonce_request_with_port_in_home_url_and_wordpress_installed_in_subdirectory() {
// fake various $_SERVER parameters
$host = explode( ':', $_SERVER['HTTP_HOST'] );
$_SERVER['HTTP_HOST'] = $host[0] . ':80';
$_SERVER['SERVER_PORT'] = 80;
$_SERVER['REQUEST_URI'] = '/wordpress/';
// add port number and subdirectory to home URL for testing
add_filter( 'home_url', array( $this, 'add_port_and_subdirectory_to_home_url' ), 10, 3 );
// test bp_verify_nonce_request()
$action = 'verify-this';
$_REQUEST[$action] = wp_create_nonce( $action );
$test = bp_verify_nonce_request( $action, $action );
// clean up!
remove_filter( 'home_url', array( $this, 'add_port_and_subdirectory_to_home_url' ), 10 );
unset( $_REQUEST[$action] );
// assert!
$this->assertSame( 1, $test );
}
/**
* Add port 80 and /wordpress/ subdirectory to home URL.
*
* @param string $url The complete home URL including scheme and path.
* @param string $path Path relative to the home URL. Blank string if no path is specified.
* @param string|null $orig_scheme Scheme to give the home URL context. Accepts 'http', 'https', 'relative' or null.
* @return string
*/
public function add_port_and_subdirectory_to_home_url( $url, $path, $scheme ) {
$home = parse_url( get_option( 'home' ) );
$home_path = isset( $home['path'] ) ? $home['path'] : '';
return $scheme . '://' . $home['host'] . ':80' . $home_path . '/wordpress' . $path;
}
}