Skip to content

Commit 89f89d7

Browse files
committed
Tests: Cover REST boolean and cookie authentication helpers.
Cover `rest_is_boolean()` rejecting `null` and the authentication-state handling in `rest_cookie_check_errors()`. Developed in: #15272, #15294 Props shailu25. See #65819. git-svn-id: https://develop.svn.wordpress.org/trunk@65166 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 601ae8a commit 89f89d7

2 files changed

Lines changed: 137 additions & 0 deletions

File tree

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
/**
4+
* Tests for rest_cookie_check_errors().
5+
*
6+
* @group restapi
7+
*
8+
* @covers ::rest_cookie_check_errors
9+
*/
10+
class Tests_REST_API_RestCookieCheckErrors extends WP_UnitTestCase {
11+
12+
public function tear_down() {
13+
$GLOBALS['wp_rest_server'] = null;
14+
unset( $GLOBALS['wp_rest_auth_cookie'] );
15+
unset( $_SERVER['HTTP_X_WP_NONCE'] );
16+
parent::tear_down();
17+
}
18+
19+
/**
20+
* @ticket 65819
21+
*/
22+
public function test_returns_existing_result_unchanged() {
23+
$result = new WP_Error( 'existing', 'Existing error' );
24+
25+
$this->assertSame( $result, rest_cookie_check_errors( $result ) );
26+
}
27+
28+
/**
29+
* @ticket 65819
30+
*/
31+
public function test_returns_true_and_clears_user_when_nonce_missing() {
32+
$user_id = self::factory()->user->create();
33+
wp_set_current_user( $user_id );
34+
35+
$GLOBALS['wp_rest_auth_cookie'] = true;
36+
37+
$this->assertTrue( rest_cookie_check_errors( null ) );
38+
$this->assertSame( 0, get_current_user_id() );
39+
}
40+
41+
/**
42+
* @ticket 65819
43+
*
44+
* @dataProvider data_nonce_sources
45+
*
46+
* @param string $source Where the nonce is provided from.
47+
*/
48+
public function test_returns_error_for_invalid_nonce( $source ) {
49+
$GLOBALS['wp_rest_auth_cookie'] = true;
50+
$this->set_nonce_source( $source, 'invalid-nonce' );
51+
52+
$result = rest_cookie_check_errors( null );
53+
54+
$this->assertWPError( $result );
55+
$this->assertSame( 'rest_cookie_invalid_nonce', $result->get_error_code() );
56+
}
57+
58+
/**
59+
* @ticket 65819
60+
*
61+
* @dataProvider data_nonce_sources
62+
*
63+
* @param string $source Where the nonce is provided from.
64+
*/
65+
public function test_returns_true_for_valid_nonce( $source ) {
66+
$user_id = self::factory()->user->create();
67+
wp_set_current_user( $user_id );
68+
69+
$GLOBALS['wp_rest_auth_cookie'] = true;
70+
$this->set_nonce_source( $source, wp_create_nonce( 'wp_rest' ) );
71+
72+
$this->assertTrue( rest_cookie_check_errors( null ) );
73+
$this->assertSame( $user_id, get_current_user_id() );
74+
}
75+
76+
/**
77+
* @ticket 65819
78+
*/
79+
public function test_skips_check_when_another_auth_method_is_in_use() {
80+
$user_id = self::factory()->user->create();
81+
wp_set_current_user( $user_id );
82+
83+
$GLOBALS['wp_rest_auth_cookie'] = 'malformed';
84+
85+
$this->assertNull( rest_cookie_check_errors( null ) );
86+
$this->assertSame( $user_id, get_current_user_id() );
87+
}
88+
89+
/**
90+
* Data provider.
91+
*
92+
* @return array[]
93+
*/
94+
public static function data_nonce_sources() {
95+
return array(
96+
'request param' => array( 'request' ),
97+
'header' => array( 'header' ),
98+
);
99+
}
100+
101+
/**
102+
* Sets the nonce on the requested source.
103+
*
104+
* @param string $source Nonce source: 'request' or 'header'.
105+
* @param string $nonce Nonce value.
106+
*/
107+
private function set_nonce_source( $source, $nonce ) {
108+
if ( 'header' === $source ) {
109+
$_SERVER['HTTP_X_WP_NONCE'] = $nonce;
110+
return;
111+
}
112+
113+
$_REQUEST['_wpnonce'] = $nonce;
114+
}
115+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
/**
4+
* Tests for rest_is_boolean().
5+
*
6+
* Schema sanitization/validation already covers boolean-like acceptance paths
7+
* and common string/int rejections. This class only checks a remaining gap:
8+
* non-bool/string/int values hit the final rejection branch.
9+
*
10+
* @group restapi
11+
*
12+
* @covers ::rest_is_boolean
13+
*/
14+
class Tests_REST_API_RestSanitizeBoolean extends WP_UnitTestCase {
15+
16+
/**
17+
* @ticket 65819
18+
*/
19+
public function test_rest_is_boolean_rejects_non_scalar_values() {
20+
$this->assertFalse( rest_is_boolean( null ) );
21+
}
22+
}

0 commit comments

Comments
 (0)