@@ -1937,12 +1937,7 @@ static function () use ( $input ) {
19371937 $ response = $ this ->dispatch_run ( $ method , 'test/coercion-error ' , 'request ' === $ source ? $ input : null );
19381938 $ this ->assertSame ( 400 , $ response ->get_status () );
19391939 $ data = $ response ->get_data ();
1940- if ( 'request ' === $ source ) {
1941- $ this ->assertSame ( 'rest_invalid_param ' , $ data ['code ' ] );
1942- $ this ->assertSame ( 'ability_invalid_input ' , $ data ['data ' ]['details ' ]['input ' ]['code ' ] );
1943- } else {
1944- $ this ->assertSame ( 'ability_invalid_input ' , $ data ['code ' ] );
1945- }
1940+ $ this ->assertSame ( 'ability_invalid_input ' , $ data ['code ' ] );
19461941 }
19471942
19481943 /**
@@ -2173,4 +2168,166 @@ public function test_permission_callback_receives_typed_input( $input ): void {
21732168 $ this ->assertSame ( 'integer ' , $ seen ->types [0 ], 'The REST permission gate should receive coerced (typed) input, not a raw string. ' );
21742169 $ this ->assertSame ( array ( 'count ' => 10 ), $ response ->get_data (), 'The execute callback should receive coerced input too. ' );
21752170 }
2171+
2172+ /**
2173+ * Data provider for input sent with the request and input left out.
2174+ *
2175+ * @return array<string, array{0: array|null}> Request input, or null to use the schema default.
2176+ */
2177+ public static function data_input_prepared_once (): array {
2178+ return array (
2179+ 'request input ' => array ( array ( 'count ' => '10 ' ) ),
2180+ 'schema default ' => array ( null ),
2181+ );
2182+ }
2183+
2184+ /**
2185+ * Tests that the input is prepared only once before the permission check.
2186+ *
2187+ * @ticket 66383
2188+ * @dataProvider data_input_prepared_once
2189+ *
2190+ * @param array|null $input Request input, or null to use the schema default.
2191+ */
2192+ public function test_input_is_prepared_once_before_permission_check ( ?array $ input ): void {
2193+ $ calls = new stdClass ();
2194+
2195+ $ calls ->validate = 0 ;
2196+ add_filter (
2197+ 'wp_ability_validate_input ' ,
2198+ static function ( $ is_valid ) use ( $ calls ) {
2199+ ++$ calls ->validate ;
2200+ return $ is_valid ;
2201+ }
2202+ );
2203+
2204+ $ this ->register_test_ability (
2205+ 'test/prepared-once ' ,
2206+ array (
2207+ 'label ' => 'Prepared Once ' ,
2208+ 'description ' => 'Records how the input was prepared for the permission check. ' ,
2209+ 'category ' => 'general ' ,
2210+ 'input_schema ' => array (
2211+ 'type ' => 'object ' ,
2212+ 'default ' => array ( 'count ' => '10 ' ),
2213+ 'properties ' => array (
2214+ 'count ' => array ( 'type ' => 'integer ' ),
2215+ ),
2216+ ),
2217+ 'permission_callback ' => static function ( $ value ) use ( $ calls ) {
2218+ $ calls ->permission ??= array ( $ value , $ calls ->validate );
2219+ return true ;
2220+ },
2221+ 'execute_callback ' => static function ( $ value ) {
2222+ return $ value ;
2223+ },
2224+ 'meta ' => array (
2225+ 'annotations ' => array ( 'readonly ' => true ),
2226+ 'show_in_rest ' => true ,
2227+ ),
2228+ )
2229+ );
2230+
2231+ $ response = $ this ->dispatch_run ( 'GET ' , 'test/prepared-once ' , $ input );
2232+
2233+ $ this ->assertSame ( 200 , $ response ->get_status () );
2234+ $ this ->assertSame ( array ( array ( 'count ' => 10 ), 1 ), $ calls ->permission , 'The permission callback should get input prepared once. ' );
2235+ }
2236+
2237+ /**
2238+ * Data provider for requests rejected by the permission check.
2239+ *
2240+ * @return array<string, array{0: string, 1: bool, 2: string}> HTTP method, whether the
2241+ * ability is shown in REST, and the expected error code.
2242+ */
2243+ public static function data_invalid_input_does_not_hide_request_errors (): array {
2244+ return array (
2245+ 'hidden ability ' => array ( 'GET ' , false , 'rest_ability_not_found ' ),
2246+ 'wrong HTTP method ' => array ( 'POST ' , true , 'rest_ability_invalid_method ' ),
2247+ );
2248+ }
2249+
2250+ /**
2251+ * Tests that invalid input does not replace not found and wrong method errors.
2252+ *
2253+ * @ticket 66383
2254+ * @dataProvider data_invalid_input_does_not_hide_request_errors
2255+ *
2256+ * @param string $method HTTP method.
2257+ * @param bool $show_in_rest Whether the ability is shown in REST.
2258+ * @param string $error_code Expected error code.
2259+ */
2260+ public function test_invalid_input_does_not_hide_request_errors ( string $ method , bool $ show_in_rest , string $ error_code ): void {
2261+ $ this ->register_test_ability (
2262+ 'test/rejected-request ' ,
2263+ array (
2264+ 'label ' => 'Rejected Request ' ,
2265+ 'description ' => 'Is rejected before its input is checked. ' ,
2266+ 'category ' => 'general ' ,
2267+ 'input_schema ' => array (
2268+ 'type ' => 'object ' ,
2269+ 'properties ' => array (
2270+ 'count ' => array ( 'type ' => 'integer ' ),
2271+ ),
2272+ ),
2273+ 'permission_callback ' => '__return_true ' ,
2274+ 'execute_callback ' => '__return_true ' ,
2275+ 'meta ' => array (
2276+ 'annotations ' => array ( 'readonly ' => true ),
2277+ 'show_in_rest ' => $ show_in_rest ,
2278+ ),
2279+ )
2280+ );
2281+
2282+ $ response = $ this ->dispatch_run ( $ method , 'test/rejected-request ' , array ( 'count ' => 'not-a-number ' ) );
2283+
2284+ $ this ->assertSame ( $ error_code , $ response ->get_data ()['code ' ] );
2285+ }
2286+
2287+ /**
2288+ * Tests that a REST run normalizes the input only once.
2289+ *
2290+ * The normalize filter adds `!` to the input, and the schema allows at most
2291+ * 4 characters. So `raw` becomes a valid `raw!`, but normalizing it again
2292+ * gives `raw!!`, which fails validation.
2293+ *
2294+ * @ticket 66383
2295+ * @dataProvider data_coercion_transports
2296+ *
2297+ * @param string $method HTTP method.
2298+ * @param array $annotations Ability annotations.
2299+ */
2300+ public function test_rest_run_normalizes_input_once ( string $ method , array $ annotations ): void {
2301+ add_filter (
2302+ 'wp_ability_normalize_input ' ,
2303+ static function ( $ input ) {
2304+ return is_string ( $ input ) ? $ input . '! ' : $ input ;
2305+ }
2306+ );
2307+ $ this ->register_test_ability (
2308+ 'test/normalize-once ' ,
2309+ array (
2310+ 'label ' => 'Normalize Once ' ,
2311+ 'description ' => 'Returns the normalized input. ' ,
2312+ 'category ' => 'general ' ,
2313+ 'input_schema ' => array (
2314+ 'type ' => 'string ' ,
2315+ 'maxLength ' => 4 ,
2316+ ),
2317+ 'permission_callback ' => '__return_true ' ,
2318+ 'execute_callback ' => static function ( string $ input ): string {
2319+ return $ input ;
2320+ },
2321+ 'meta ' => array (
2322+ 'show_in_rest ' => true ,
2323+ 'annotations ' => $ annotations ,
2324+ ),
2325+ )
2326+ );
2327+
2328+ $ response = $ this ->dispatch_run ( $ method , 'test/normalize-once ' , 'raw ' );
2329+
2330+ $ this ->assertSame ( 200 , $ response ->get_status () );
2331+ $ this ->assertSame ( 'raw! ' , $ response ->get_data () );
2332+ }
21762333}
0 commit comments