Skip to content

MslsMetaBox tested #338

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion includes/MslsCustomFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
21 changes: 10 additions & 11 deletions includes/MslsMetaBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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() );
Expand All @@ -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
*
Expand All @@ -80,7 +79,6 @@ public static function get_suggested_fields( $json, $args ) {
}

wp_reset_postdata();
restore_current_blog();

return $json;
}
Expand Down Expand Up @@ -110,6 +108,7 @@ public static function init() {
*/
public function add() {
foreach ( MslsPostType::instance()->get() as $post_type ) {

add_meta_box(
'msls',
apply_filters(
Expand Down
26 changes: 18 additions & 8 deletions includes/MslsRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}

/**
Expand Down
19 changes: 18 additions & 1 deletion tests/phpunit/TestMslsMetaBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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"}' );
Expand All @@ -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 );
Expand Down
Loading