@@ -2362,7 +2362,7 @@ public function test_prepare_input( array $schema, $input, $expected ): void {
23622362 public static function data_input_coercion (): array {
23632363 return array_intersect_key (
23642364 self ::data_input_is_coerced (),
2365- array_flip ( array ( 'integer number ' , 'stdClass object input ' , 'default ' , 'text-field format ' ) )
2365+ array_flip ( array ( 'integer number ' , 'stdClass object input ' , 'default ' , 'text-field format ' , ' top-level string unchanged ' ) )
23662366 );
23672367 }
23682368
@@ -2420,14 +2420,18 @@ static function ( $name, $value ) use ( &$seen ) {
24202420
24212421 $ this ->assertSame ( 0 , $ ability ->execute ( $ input ) );
24222422 $ this ->assertSame ( $ input , $ seen ['invoked ' ] );
2423- $ this ->assertSame ( empty ( $ schema ) ? array () : array ( $ normalized ), $ seen ['validation ' ], 'Validate the normalized input exactly once. ' );
2423+ $ expected_validation = array ( $ normalized );
2424+ if ( $ expected !== $ normalized ) {
2425+ $ expected_validation [] = $ expected ;
2426+ }
2427+ $ this ->assertSame ( $ expected_validation , $ seen ['validation ' ], 'Validate normalized input, then sanitized input only when it changes. ' );
24242428 $ this ->assertSame ( $ expected , $ seen ['permission ' ] );
24252429 $ this ->assertSame ( $ expected , $ seen ['before ' ] );
24262430 $ this ->assertSame ( $ expected , $ seen ['execute ' ] );
24272431 }
24282432
24292433 /**
2430- * Provides top-level and nested collisions introduced by sanitization .
2434+ * Provides input that sanitization rejects or makes invalid .
24312435 *
24322436 * @return array
24332437 */
@@ -2449,16 +2453,43 @@ public static function data_input_sanitization_errors(): array {
24492453 'ids ' => '1,01 ' ,
24502454 );
24512455
2456+ $ text = array (
2457+ 'type ' => 'object ' ,
2458+ 'properties ' => array (
2459+ 'title ' => array (
2460+ 'type ' => 'string ' ,
2461+ 'format ' => 'text-field ' ,
2462+ 'minLength ' => 1 ,
2463+ ),
2464+ ),
2465+ );
2466+
24522467 return array (
2453- 'top-level array ' => array ( $ unique , array ( '1 ' , '01 ' ), 'input ' ),
2454- 'nested object ' => array ( $ object , $ input , 'input[ids] ' ),
2455- 'object in list ' => array (
2468+ 'top-level array ' => array ( $ unique , array ( '1 ' , '01 ' ), 'input has duplicate items. ' ),
2469+ 'nested object ' => array ( $ object , $ input , 'input[ids] has duplicate items. ' ),
2470+ 'object in list ' => array (
24562471 array (
24572472 'type ' => 'array ' ,
24582473 'items ' => $ object ,
24592474 ),
24602475 array ( $ input ),
2461- 'input[0][ids] ' ,
2476+ 'input[0][ids] has duplicate items. ' ,
2477+ ),
2478+ 'whitespace below minLength ' => array ( $ text , array ( 'title ' => ' ' ), 'input[title] must be at least 1 character long. ' ),
2479+ 'markup below minLength ' => array ( $ text , array ( 'title ' => '<b></b> ' ), 'input[title] must be at least 1 character long. ' ),
2480+ // An out-of-range numeric string is cast to `PHP_INT_MAX`, which is not a multiple of 10.
2481+ 'integer cast breaks multipleOf ' => array (
2482+ array (
2483+ 'type ' => 'object ' ,
2484+ 'properties ' => array (
2485+ 'count ' => array (
2486+ 'type ' => 'integer ' ,
2487+ 'multipleOf ' => 10 ,
2488+ ),
2489+ ),
2490+ ),
2491+ array ( 'count ' => '1e19 ' ),
2492+ 'input[count] must be a multiple of 10. ' ,
24622493 ),
24632494 );
24642495 }
@@ -2469,11 +2500,11 @@ public static function data_input_sanitization_errors(): array {
24692500 * @ticket 66383
24702501 * @dataProvider data_input_sanitization_errors
24712502 *
2472- * @param array $schema Input schema.
2473- * @param mixed $input Input accepted by validation but rejected by sanitization.
2474- * @param string $path Path of the duplicate items in the error message.
2503+ * @param array $schema Input schema.
2504+ * @param mixed $input Input accepted by validation but rejected by sanitization.
2505+ * @param string $message Expected error message fragment .
24752506 */
2476- public function test_input_sanitization_errors ( array $ schema , $ input , string $ path ): void {
2507+ public function test_input_sanitization_errors ( array $ schema , $ input , string $ message ): void {
24772508 $ args = self ::$ test_ability_properties ;
24782509 $ args ['input_schema ' ] = $ schema ;
24792510 $ unexpected_callback = function () {
@@ -2491,8 +2522,7 @@ public function test_input_sanitization_errors( array $schema, $input, string $p
24912522 $ error = $ ability ->prepare_input ( $ input );
24922523 $ this ->assertWPError ( $ error );
24932524 $ this ->assertSame ( 'ability_invalid_input ' , $ error ->get_error_code () );
2494- $ this ->assertStringContainsString ( $ path . ' has duplicate items. ' , $ error ->get_error_message () );
2495- // Keep assertEquals() because the objects are intentionally compared by value.
2525+ $ this ->assertStringContainsString ( $ message , $ error ->get_error_message () );
24962526 $ this ->assertEquals ( $ error , $ ability ->execute ( $ input ), 'Execution should propagate the sanitization error. ' );
24972527 }
24982528
@@ -2551,6 +2581,30 @@ public function test_sanitization_respects_input_validation(): void {
25512581 $ this ->assertSame ( array ( array ( 'count ' => 10 ) ), $ seen );
25522582 }
25532583
2584+ /**
2585+ * Validation filters can reject the sanitized value before callbacks receive it.
2586+ *
2587+ * @ticket 66665
2588+ */
2589+ public function test_validation_filter_can_reject_sanitized_input (): void {
2590+ $ args = self ::$ test_ability_properties ;
2591+ $ args ['input_schema ' ] = array ( 'type ' => 'integer ' );
2592+ $ ability = new WP_Ability ( self ::$ test_ability_name , $ args );
2593+ $ error = new WP_Error ( 'sanitized_input_rejected ' );
2594+ $ seen = array ();
2595+ $ validate = static function ( $ valid , $ input ) use ( &$ seen , $ error ) {
2596+ $ seen [] = $ input ;
2597+ return is_int ( $ input ) ? $ error : $ valid ;
2598+ };
2599+
2600+ add_filter ( 'wp_ability_validate_input ' , $ validate , 10 , 2 );
2601+ $ result = $ ability ->execute ( '2 ' );
2602+ remove_filter ( 'wp_ability_validate_input ' , $ validate );
2603+
2604+ $ this ->assertSame ( array ( '2 ' , 2 ), $ seen , 'The filter should receive both raw and sanitized input. ' );
2605+ $ this ->assertSame ( $ error , $ result , 'Execution should propagate the filter rejection unchanged. ' );
2606+ }
2607+
25542608 /**
25552609 * Returns the test ability properties with required capabilities and no permission callback.
25562610 *
0 commit comments