diff --git a/includes/MslsPostTag.php b/includes/MslsPostTag.php index 41ac4bc0..78c98fa8 100644 --- a/includes/MslsPostTag.php +++ b/includes/MslsPostTag.php @@ -71,6 +71,8 @@ public static function suggest() { /** * Init * + * @codeCoverageIgnore + * * @return MslsPostTag */ public static function init() { @@ -82,7 +84,7 @@ public static function init() { $taxonomy = MslsContentTypes::create()->acl_request(); if ( '' != $taxonomy ) { add_action( "{$taxonomy}_add_form_fields", [ $obj, 'add_input' ] ); - add_action( "{$taxonomy}_edit_form_fields", [ $obj, 'edit_input' ] ); + add_action( "{$taxonomy}_edit_form_fields", [ $obj, 'edit_input' ], 10, 2 ); add_action( "edited_{$taxonomy}", [ $obj, 'set' ] ); add_action( "create_{$taxonomy}", [ $obj, 'set' ] ); } @@ -109,16 +111,17 @@ public function add_input( string $taxonomy ): void { '; echo '
'; - $this->the_input( $taxonomy, $title_format, $item_format ); + $this->the_input( null, $title_format, $item_format ); echo '
'; } /** * Add the input fields to the edit-screen of the taxonomies * + * @param \WP_Term $tag * @param string $taxonomy */ - public function edit_input( string $taxonomy ): void { + public function edit_input( \WP_Term $tag, string $taxonomy ): void { if ( did_action( "{$taxonomy}_edit_form_fields" ) !== 1 ) { return; } @@ -141,7 +144,7 @@ public function edit_input( string $taxonomy ): void { '; - $this->the_input( $taxonomy, $title_format, $item_format ); + $this->the_input( $tag, $title_format, $item_format ); } /** @@ -149,14 +152,14 @@ public function edit_input( string $taxonomy ): void { * * Returns true if the blogcollection is not empty * - * @param mixed $tag + * @param ?\WP_Term $tag * @param string $title_format * @param string $item_format * * @return boolean */ - public function the_input( $tag, $title_format, $item_format ) { - $term_id = ( is_object( $tag ) ? $tag->term_id : 0 ); + public function the_input( ?\WP_Term $tag, $title_format, $item_format ) { + $term_id = $tag->term_id ?? 0; $blogs = $this->collection->get(); if ( $blogs ) { $my_data = MslsOptionsTax::create( $term_id ); @@ -191,9 +194,9 @@ public function the_input( $tag, $title_format, $item_format ) { } } - printf( $item_format, $blog->userblog_id, $icon, $language, $value, $title ); - restore_current_blog(); + + printf( $item_format, $blog->userblog_id, $icon, $language, $value, $title ); } return true; diff --git a/includes/MslsPostTagClassic.php b/includes/MslsPostTagClassic.php index 2bde8e0c..8615a362 100644 --- a/includes/MslsPostTagClassic.php +++ b/includes/MslsPostTagClassic.php @@ -33,16 +33,17 @@ public function add_input( string $taxonomy ): void { '; echo '
'; - $this->the_input( $taxonomy, $title_format, $item_format ); + $this->the_input( null, $title_format, $item_format ); echo '
'; } /** * Add the input fields to the edit-screen of the taxonomies * + * @param \WP_Term $tag * @param string $taxonomy */ - public function edit_input( string $taxonomy ): void { + public function edit_input( \WP_Term $tag, string $taxonomy ): void { if ( did_action( "{$taxonomy}_edit_form_fields" ) !== 1 ) { return; } diff --git a/tests/test-mslsposttag.php b/tests/test-mslsposttag.php index 15fd85e2..b3a7f743 100644 --- a/tests/test-mslsposttag.php +++ b/tests/test-mslsposttag.php @@ -35,9 +35,7 @@ public function setUp(): void { $collection = \Mockery::mock( MslsBlogCollection::class ); $collection->shouldReceive( 'get' )->andReturn( $blogs ); - Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection ); - - $this->test = MslsPostTag::init(); + $this->test = new MslsPostTag( $options, $collection ); } /** @@ -64,30 +62,33 @@ public function test_edit_input() { $output = ' Multisite Language Switcher + + - + + - + + + - + + - + + + '; self::expectOutputString( $output ); - $this->test->edit_input( 'test' ); - } + $tag = \Mockery::mock( \WP_Term::class ); + $this->test->edit_input( $tag, 'test' ); + } public function test_add_input_second_call() { Functions\expect( 'did_action' )->andReturn( 2 ); @@ -98,11 +99,13 @@ public function test_add_input_second_call() { } public function test_edit_input_second_call() { + $tag = \Mockery::mock( \WP_Term::class ); + Functions\expect( 'did_action' )->andReturn( 2 ); self::expectOutputString( '' ); - $this->test->edit_input( 'test' ); + $this->test->edit_input( $tag, 'test' ); } } diff --git a/tests/test-mslsposttagclassic.php b/tests/test-mslsposttagclassic.php index 31661b5f..abef00b3 100644 --- a/tests/test-mslsposttagclassic.php +++ b/tests/test-mslsposttagclassic.php @@ -2,30 +2,108 @@ namespace lloc\MslsTests; +use lloc\Msls\MslsBlog; use lloc\Msls\MslsPostTagClassic; use lloc\Msls\MslsOptions; use lloc\Msls\MslsBlogCollection; +use Brain\Monkey\Functions; +/** + * WP_Test_MslsPostTag + */ class WP_Test_MslsPostTagClassic extends Msls_UnitTestCase { - public function get_sut() { - $options = \Mockery::mock( MslsOptions::class ); - $collection = \Mockery::mock( MslsBlogCollection::class ); + public function setUp(): void { + parent::setUp(); + + Functions\when( 'get_option' )->justReturn( [] ); + Functions\expect( 'is_admin' )->andReturn( true ); + Functions\expect( 'get_post_types' )->andReturn( [ 'post', 'page' ] ); + + foreach ( [ 'de_DE', 'en_US' ] as $locale ) { + $blog = \Mockery::mock( MslsBlog::class ); + $blog->shouldReceive( [ + 'get_language' => $locale, + ] ); - $collection->shouldReceive( 'get' )->once()->andReturn( [] ); + $blogs[] = $blog; + } + + $options = \Mockery::mock( MslsOptions::class ); + $options->shouldReceive( 'get_icon_type' )->andReturn( 'label' ); + + $collection = \Mockery::mock( MslsBlogCollection::class ); + $collection->shouldReceive( 'get' )->andReturn( $blogs ); - return new MslsPostTagClassic( $options, $collection ); + $this->test = new MslsPostTagClassic( $options, $collection ); } /** - * Verify the static the_input-method + * Verify the static suggest-method */ - public function test_the_input_method() { - $obj = $this->get_sut(); + public function test_suggest(): void { + Functions\expect( 'wp_die' ); + + self::expectOutputString( '' ); + + MslsPostTagClassic::suggest(); + } + + public function test_edit_input() { + Functions\expect( 'did_action' )->andReturn( 1 ); + Functions\expect( 'get_queried_object_id' )->andReturn( 42 ); + Functions\expect( 'get_current_blog_id' )->andReturn( 23 ); + Functions\expect( 'get_admin_url' )->andReturn( '/wp-admin/edit-tags.php' ); + Functions\expect( 'switch_to_blog' )->atLeast(); + Functions\expect( 'restore_current_blog' )->atLeast(); + Functions\expect( 'get_terms' )->andReturn( [] ); + Functions\expect( 'plugin_dir_path' )->atLeast( 1 )->andReturn( dirname( __DIR__, 1 ) . '/' ); + + $output = ' + + Multisite Language Switcher + + + + + + + + + + + + '; - $tag = new \StdClass; - $tag->term_id = 1; + self::expectOutputString( $output ); - $this->assertIsBool( $obj->the_input( $tag, 'test', 'test' ) ); + $tag = \Mockery::mock( \WP_Term::class ); + + $this->test->edit_input( $tag, 'test' ); + } + + public function test_add_input_second_call() { + Functions\expect( 'did_action' )->andReturn( 2 ); + + self::expectOutputString( '' ); + + $this->test->add_input( 'test' ); } + + public function test_edit_input_second_call() { + $tag = \Mockery::mock( \WP_Term::class ); + + Functions\expect( 'did_action' )->andReturn( 2 ); + + self::expectOutputString( '' ); + + $this->test->edit_input( $tag, 'test' ); + } + }