From f61275ca5bdce92f40e07018161ef8f0df5fd7b1 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Sat, 6 Jan 2024 15:05:50 +0100 Subject: [PATCH] Tests adjusted --- includes/MslsPlugin.php | 19 +++---- includes/MslsPostTag.php | 72 +++++++++++++------------- includes/MslsPostTagClassic.php | 43 +++++++++------- includes/MslsWidget.php | 10 ++-- tests/bootstrap.php | 2 +- tests/test-mslsposttag.php | 89 +++++++++++++++++++++++++++------ 6 files changed, 149 insertions(+), 86 deletions(-) diff --git a/includes/MslsPlugin.php b/includes/MslsPlugin.php index 792e0c82..a0b0e9e2 100644 --- a/includes/MslsPlugin.php +++ b/includes/MslsPlugin.php @@ -220,15 +220,16 @@ public function block_init() { $callback = [ $this, 'block_render' ]; global $pagenow; - $toLoad = [ 'wp-blocks', 'wp-element', 'wp-components' ]; - if ( $pagenow === 'widgets.php' ) $toLoad[] = 'wp-edit-widgets'; - else $toLoad[] = 'wp-editor'; - - wp_register_script( - $handle, - self::plugins_url( 'js/msls-widget-block.js' ), - $toLoad - ); + + $toLoad = [ 'wp-blocks', 'wp-element', 'wp-components' ]; + if ( $pagenow === 'widgets.php' ) $toLoad[] = 'wp-edit-widgets'; + else $toLoad[] = 'wp-editor'; + + wp_register_script( + $handle, + self::plugins_url( 'js/msls-widget-block.js' ), + $toLoad + ); register_block_type( 'lloc/msls-widget-block', [ 'attributes' => [ 'title' => [ 'type' => 'string' ] ], diff --git a/includes/MslsPostTag.php b/includes/MslsPostTag.php index 72ea0ae5..41ac4bc0 100644 --- a/includes/MslsPostTag.php +++ b/includes/MslsPostTag.php @@ -1,6 +1,7 @@ * @since 0.9.8 */ @@ -9,6 +10,7 @@ /** * Post Tag + * * @package Msls */ class MslsPostTag extends MslsMain { @@ -27,12 +29,12 @@ public static function suggest() { filter_input( INPUT_POST, 'blog_id', FILTER_SANITIZE_NUMBER_INT ) ); - $args = array( + $args = [ 'orderby' => 'name', 'order' => 'ASC', 'number' => 10, 'hide_empty' => 0, - ); + ]; if ( filter_has_var( INPUT_POST, 's' ) ) { $args['search'] = sanitize_text_field( @@ -69,20 +71,13 @@ public static function suggest() { /** * Init * - * @codeCoverageIgnore - * * @return MslsPostTag */ public static function init() { $options = MslsOptions::instance(); $collection = msls_blog_collection(); - - if ( $options->activate_autocomplete ) { - $obj = new static( $options, $collection ); - } - else { - $obj = new MslsPostTagClassic( $options, $collection ); - } + $class = $options->activate_autocomplete ? MslsPostTag::class : MslsPostTagClassic::class; + $obj = new $class( $options, $collection ); $taxonomy = MslsContentTypes::create()->acl_request(); if ( '' != $taxonomy ) { @@ -98,9 +93,13 @@ public static function init() { /** * Add the input fields to the add-screen of the taxonomies * - * @param \StdClass $tag + * @param string $taxonomy */ - public function add_input( $tag ) { + public function add_input( string $taxonomy ): void { + if ( did_action( "{$taxonomy}_add_form_fields" ) !== 1 ) { + return; + } + $title_format = '

%s

'; @@ -110,15 +109,20 @@ public function add_input( $tag ) { '; echo '
'; - $this->the_input( $tag, $title_format, $item_format ); + $this->the_input( $taxonomy, $title_format, $item_format ); echo '
'; } /** * Add the input fields to the edit-screen of the taxonomies - * @param \StdClass $tag + * + * @param string $taxonomy */ - public function edit_input( $tag ) { + public function edit_input( string $taxonomy ): void { + if ( did_action( "{$taxonomy}_edit_form_fields" ) !== 1 ) { + return; + } + $title_format = ' %s @@ -128,7 +132,7 @@ public function edit_input( $tag ) { '; $item_format = ' - + @@ -137,15 +141,18 @@ public function edit_input( $tag ) { '; - $this->the_input( $tag, $title_format, $item_format ); + $this->the_input( $taxonomy, $title_format, $item_format ); } /** * Print the input fields + * * Returns true if the blogcollection is not empty - * @param \StdClass $tag + * + * @param mixed $tag * @param string $title_format * @param string $item_format + * * @return boolean */ public function the_input( $tag, $title_format, $item_format ) { @@ -156,7 +163,7 @@ public function the_input( $tag, $title_format, $item_format ) { $this->maybe_set_linked_term( $my_data ); - $type = MslsContentTypes::create()->get_request(); + $type = MslsContentTypes::create()->get_request(); printf( $title_format, @@ -166,17 +173,13 @@ public function the_input( $tag, $title_format, $item_format ) { ), $type ); + foreach ( $blogs as $blog ) { switch_to_blog( $blog->userblog_id ); - $language = $blog->get_language(); - $icon = MslsAdminIcon::create() - ->set_language( $language ); - if( $this->options->admin_display === 'label' ) { - $icon->set_icon_type( 'label' ); - } else { - $icon->set_icon_type( 'flag' ); - } + $language = $blog->get_language(); + $icon_type = $this->options->get_icon_type(); + $icon = MslsAdminIcon::create()->set_language( $language )->set_icon_type( $icon_type ); $value = $title = ''; if ( $my_data->has_value( $language ) ) { @@ -188,23 +191,20 @@ public function the_input( $tag, $title_format, $item_format ) { } } - printf( - $item_format, - $blog->userblog_id, - $icon, - $language, - $value, - $title - ); + printf( $item_format, $blog->userblog_id, $icon, $language, $value, $title ); + restore_current_blog(); } + return true; } + return false; } /** * Set calls the save method if taxonomy is set + * * @param int $term_id * @codeCoverageIgnore */ diff --git a/includes/MslsPostTagClassic.php b/includes/MslsPostTagClassic.php index 6d3c4a2f..2bde8e0c 100644 --- a/includes/MslsPostTagClassic.php +++ b/includes/MslsPostTagClassic.php @@ -8,16 +8,22 @@ namespace lloc\Msls; /** - * Post Tag Clasic + * Post Tag Classic + * * @package Msls */ class MslsPostTagClassic extends MslsPostTag { /** * Add the input fields to the add-screen of the taxonomies - * @param \StdClass $tag + * + * @param string $taxonomy */ - public function add_input( $tag ): void { + public function add_input( string $taxonomy ): void { + if ( did_action( "{$taxonomy}_add_form_fields" ) !== 1 ) { + return; + } + $title_format = '

%s

'; $item_format = ' @@ -27,15 +33,20 @@ public function add_input( $tag ): void { '; echo '
'; - $this->the_input( $tag, $title_format, $item_format ); + $this->the_input( $taxonomy, $title_format, $item_format ); echo '
'; } /** * Add the input fields to the edit-screen of the taxonomies - * @param \StdClass $tag + * + * @param string $taxonomy */ - public function edit_input( $tag ): void { + public function edit_input( string $taxonomy ): void { + if ( did_action( "{$taxonomy}_edit_form_fields" ) !== 1 ) { + return; + } + $title_format = ' %s @@ -52,7 +63,7 @@ public function edit_input( $tag ): void { '; - $this->the_input( $tag, $title_format, $item_format ); + $this->the_input( $taxonomy, $title_format, $item_format ); } /** @@ -67,22 +78,16 @@ public function edit_input( $tag ): void { public function print_option( MslsBlog $blog, string $type, MslsOptionsTax $mydata, string $item_format ): void { switch_to_blog( $blog->userblog_id ); - $language = $blog->get_language(); - $icon = MslsAdminIcon::create() - ->set_language( $language ); - - if( $this->options->admin_display === 'label' ) { - $icon->set_icon_type( 'label' ); - } else { - $icon->set_icon_type( 'flag' ); - } - $options = ''; - $terms = get_terms( [ 'taxonomy' => $type, 'hide_empty' => false ] ); + $language = $blog->get_language(); + $icon_type = $this->options->get_icon_type(); + $icon = MslsAdminIcon::create()->set_language( $language )->set_icon_type( $icon_type ); if ( $mydata->has_value( $language ) ) { $icon->set_href( $mydata->$language ); } + $options = ''; + $terms = get_terms( [ 'taxonomy' => $type, 'hide_empty' => false ] ); if ( ! empty( $terms ) ) { foreach ( $terms as $term ) { $options .= sprintf( @@ -103,7 +108,7 @@ public function print_option( MslsBlog $blog, string $type, MslsOptionsTax $myda * Print the input fields * Returns true if the blogcollection is not empty * - * @param \StdClass $tag + * @param mixed $tag * @param string $title_format * @param string $item_format * @return boolean diff --git a/includes/MslsWidget.php b/includes/MslsWidget.php index 38da9d37..660648ce 100644 --- a/includes/MslsWidget.php +++ b/includes/MslsWidget.php @@ -19,13 +19,9 @@ class MslsWidget extends \WP_Widget { * Constructor */ public function __construct() { - parent::__construct( - $this->id_base, - apply_filters( - 'msls_widget_title', - __( 'Multisite Language Switcher', 'multisite-language-switcher' ) - ) - ); + $name = apply_filters('msls_widget_title', __( 'Multisite Language Switcher', 'multisite-language-switcher' ) ); + + parent::__construct( $this->id_base, $name, [ 'show_instance_in_rest' => true ] ); } /** diff --git a/tests/bootstrap.php b/tests/bootstrap.php index 70416d94..0d54b4de 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -13,7 +13,7 @@ class Msls_UnitTestCase extends TestCase { * * @var object $test */ - protected $test; + protected object $test; protected function setUp(): void { parent::setUp(); diff --git a/tests/test-mslsposttag.php b/tests/test-mslsposttag.php index 5d90c43d..15fd85e2 100644 --- a/tests/test-mslsposttag.php +++ b/tests/test-mslsposttag.php @@ -2,6 +2,7 @@ namespace lloc\MslsTests; +use lloc\Msls\MslsBlog; use lloc\Msls\MslsPostTag; use lloc\Msls\MslsOptions; use lloc\Msls\MslsBlogCollection; @@ -12,36 +13,96 @@ */ class WP_Test_MslsPostTag extends Msls_UnitTestCase { - function get_test() { + 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, + ] ); + + $blogs[] = $blog; + } + + $options = \Mockery::mock( MslsOptions::class ); + $options->shouldReceive( 'get_icon_type' )->andReturn( 'label' ); - $options = \Mockery::mock( MslsOptions::class ); $collection = \Mockery::mock( MslsBlogCollection::class ); + $collection->shouldReceive( 'get' )->andReturn( $blogs ); - $collection->shouldReceive( 'get' )->once()->andReturn( [] ); + Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection ); - return new MslsPostTag( $options, $collection ); + $this->test = MslsPostTag::init(); } /** * Verify the static suggest-method */ - function test_suggest_method() { + public function test_suggest(): void { Functions\expect( 'wp_die' ); - $this->assertNull( MslsPostTag::suggest() ); + self::expectOutputString( '' ); + + MslsPostTag::suggest(); } - /** - * Verify the static the_input-method - */ - function test_the_input_method() { - $obj = $this->get_test(); + 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 + + + + + + + + + + + + '; + + self::expectOutputString( $output ); + + $this->test->edit_input( '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() { + Functions\expect( 'did_action' )->andReturn( 2 ); - $tag = new \stdClass; - $tag->term_id = 1; + self::expectOutputString( '' ); - $this->assertIsBool( $obj->the_input( $tag, 'test', 'test' ) ); + $this->test->edit_input( 'test' ); } }