From d7b36dce82c35231d738a549663456db41ffa592 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Thu, 13 Jun 2024 14:06:33 +0200 Subject: [PATCH] MslsMetaBox tested --- includes/MslsCustomFilter.php | 1 - includes/MslsMetaBox.php | 21 ++++++++++----------- includes/MslsRequest.php | 26 ++++++++++++++++++-------- tests/phpunit/TestMslsMetaBox.php | 19 ++++++++++++++++++- 4 files changed, 46 insertions(+), 21 deletions(-) diff --git a/includes/MslsCustomFilter.php b/includes/MslsCustomFilter.php index b0608e94..4bd3f98c 100644 --- a/includes/MslsCustomFilter.php +++ b/includes/MslsCustomFilter.php @@ -77,7 +77,6 @@ public function execute_filter( \WP_Query $query ) { $id = MslsRequest::get_var( MslsFields::FIELD_MSLS_FILTER ); $blog = $this->collection->get_object( intval( $id ) ); - if ( ! $blog ) { return false; } diff --git a/includes/MslsMetaBox.php b/includes/MslsMetaBox.php index c1844d61..568a470a 100644 --- a/includes/MslsMetaBox.php +++ b/includes/MslsMetaBox.php @@ -20,29 +20,29 @@ class MslsMetaBox extends MslsMain { public static function suggest() { $json = new MslsJson(); - if ( MslsRequest::has_var( MslsFields::FIELD_BLOG_ID ) ) { - switch_to_blog( - MslsRequest::get_var( MslsFields::FIELD_BLOG_ID ) - ); + if ( MslsRequest::has_var( MslsFields::FIELD_BLOG_ID, INPUT_GET ) ) { + switch_to_blog( MslsRequest::get_var( MslsFields::FIELD_BLOG_ID, INPUT_GET ) ); $args = array( 'post_status' => get_post_stati( array( 'internal' => '' ) ), 'posts_per_page' => 10, ); - if ( MslsRequest::has_var( MslsFields::FIELD_POST_TYPE ) ) { + if ( MslsRequest::has_var( MslsFields::FIELD_POST_TYPE, INPUT_GET ) ) { $args['post_type'] = sanitize_text_field( - MslsRequest::get_var( MslsFields::FIELD_POST_TYPE ) + MslsRequest::get_var( MslsFields::FIELD_POST_TYPE, INPUT_GET ) ); } - if ( MslsRequest::has_var( MslsFields::FIELD_S ) ) { + if ( MslsRequest::has_var( MslsFields::FIELD_S, INPUT_GET ) ) { $args['s'] = sanitize_text_field( - MslsRequest::get_var( MslsFields::FIELD_S ) + MslsRequest::get_var( MslsFields::FIELD_S, INPUT_GET ) ); } $json = self::get_suggested_fields( $json, $args ); + + restore_current_blog(); } wp_die( $json->encode() ); @@ -64,8 +64,7 @@ public static function get_suggested_fields( $json, $args ) { */ $args = (array) apply_filters( 'msls_meta_box_suggest_args', $args ); - $posts = get_posts( $args ); - foreach ( $posts as $post ) { + foreach ( get_posts( $args ) as $post ) { /** * Manipulates the WP_Post object before using it * @@ -80,7 +79,6 @@ public static function get_suggested_fields( $json, $args ) { } wp_reset_postdata(); - restore_current_blog(); return $json; } @@ -110,6 +108,7 @@ public static function init() { */ public function add() { foreach ( MslsPostType::instance()->get() as $post_type ) { + add_meta_box( 'msls', apply_filters( diff --git a/includes/MslsRequest.php b/includes/MslsRequest.php index 4cd250b3..a13c86ca 100644 --- a/includes/MslsRequest.php +++ b/includes/MslsRequest.php @@ -14,24 +14,34 @@ public static function get_config( $name ): array { return $config; } - public static function has_var( string $name ): bool { - try { - list($type, ) = self::get_config( $name ); - } catch ( \InvalidArgumentException $e ) { - return false; + /** + * @param string $name + * @param ?int $input_type + */ + public static function has_var( string $name, ?int $input_type = null ): bool { + if ( null === $input_type ) { + try { + list($input_type, ) = self::get_config( $name ); + } catch ( \InvalidArgumentException $e ) { + return false; + } } - return filter_has_var( $type, $name ); + return filter_has_var( $input_type, $name ); } - public static function get_var( string $name ) { + /** + * @param string $name + * @param ?int $input_type + */ + public static function get_var( string $name, ?int $input_type = null ) { try { list($type, $filter) = self::get_config( $name ); } catch ( \InvalidArgumentException $e ) { return null; } - return filter_input( $type, $name, $filter ); + return filter_input( $input_type ?? $type, $name, $filter ); } /** diff --git a/tests/phpunit/TestMslsMetaBox.php b/tests/phpunit/TestMslsMetaBox.php index e7f1ea6f..cf4757e5 100644 --- a/tests/phpunit/TestMslsMetaBox.php +++ b/tests/phpunit/TestMslsMetaBox.php @@ -4,6 +4,7 @@ use Brain\Monkey\Functions; use lloc\Msls\MslsBlogCollection; +use lloc\Msls\MslsFields; use lloc\Msls\MslsJson; use lloc\Msls\MslsMetaBox; use lloc\Msls\MslsOptions; @@ -20,6 +21,23 @@ protected function setUp(): void { public function test_suggest(): void { $json = '{"some":"JSON"}'; + $post = \Mockery::mock( 'WP_Post' ); + $post->ID = 42; + + Functions\expect( 'filter_has_var' )->times( 3 )->andReturnTrue(); + Functions\expect( 'filter_input' )->once()->with( INPUT_GET, MslsFields::FIELD_BLOG_ID, FILTER_SANITIZE_NUMBER_INT )->andReturn( 17 ); + Functions\expect( 'filter_input' )->once()->with( INPUT_GET, MslsFields::FIELD_POST_TYPE, FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn( 17 ); + Functions\expect( 'filter_input' )->once()->with( INPUT_GET, MslsFields::FIELD_S, FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn( 17 ); + Functions\expect( 'get_post_stati' )->once()->andReturn( array( 'pending', 'draft', 'future' ) ); + Functions\expect( 'get_the_title' )->once()->andReturn( 'Test' ); + + Functions\expect( 'sanitize_text_field' )->times( 2 )->andReturnFirstArg(); + Functions\expect( 'get_posts' )->once()->andReturn( array( $post ) ); + + Functions\expect( 'switch_to_blog' )->once(); + Functions\expect( 'restore_current_blog' )->once(); + Functions\expect( 'wp_reset_postdata' )->once(); + Functions\when( 'wp_die' )->justEcho( $json ); $this->expectOutputString( '{"some":"JSON"}' ); @@ -29,7 +47,6 @@ public function test_suggest(): void { public function test_get_suggested_fields_no_posts(): void { Functions\expect( 'wp_reset_postdata' )->once(); - Functions\expect( 'restore_current_blog' )->once(); Functions\expect( 'get_posts' )->once()->andReturn( array() ); $json = \Mockery::mock( MslsJson::class );