From 6e7529c08898cb24c3ab08befdaae6c64f6a20fa Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Thu, 30 Nov 2023 11:09:00 +0100 Subject: [PATCH] Unit tests adjusted --- includes/MslsAdminIcon.php | 33 ++++++++++++++++++--------------- includes/MslsBlog.php | 14 ++++---------- includes/MslsPlugin.php | 8 +++++--- tests/test-mslsadminicon.php | 2 +- tests/test-mslsblog.php | 4 +++- 5 files changed, 31 insertions(+), 30 deletions(-) diff --git a/includes/MslsAdminIcon.php b/includes/MslsAdminIcon.php index be11fc7a..9aa9b601 100644 --- a/includes/MslsAdminIcon.php +++ b/includes/MslsAdminIcon.php @@ -232,25 +232,28 @@ public function get_a(): string { * @return string */ public function get_icon(): string { - if ( 'flag' === $this->iconType ) { - return sprintf( '%s', - ( new IconSvg() )->get( $this->language ), - $this->language - ); + if ( ! $this->language ) { + return ''; } - if ( 'label' === $this->iconType ) { - return sprintf( '%s', - $this->language, - ( new IconLabel() )->get( $this->language ) - ); - } - - if ( empty( $this->href ) ) { - return ''; + switch ( $this->iconType ) { + case MslsAdminIcon::TYPE_FLAG: + $icon = sprintf( '%s', + ( new IconSvg() )->get( $this->language ), + $this->language + ); + break; + case MslsAdminIcon::TYPE_LABEL: + $icon = sprintf( '%s', + $this->language, + ( new IconLabel() )->get( $this->language ) + ); + break; + default: + $icon = sprintf( '', empty( $this->href ) ? 'dashicons-plus' : 'dashicons-edit' ); } - return ''; + return $icon; } /** diff --git a/includes/MslsBlog.php b/includes/MslsBlog.php index 4f63b49c..b94eb533 100644 --- a/includes/MslsBlog.php +++ b/includes/MslsBlog.php @@ -40,8 +40,6 @@ public function __construct( $obj, $description ) { $this->language = MslsBlogCollection::get_blog_language( $this->obj->userblog_id ); } - $this->options = MslsOptions::instance(); - $this->description = (string) $description; } @@ -71,16 +69,12 @@ public function get_description(): string { /** * Gets a customized title for the blog * + * @param string $icon_type + * * @return string */ - public function get_title(): string { - $icon = new MslsAdminIcon( null ); - $icon->set_language( $this->language ); - if( $this->options->admin_display === 'label' ) { - $icon->set_icon_type( 'label' ); - } else { - $icon->set_icon_type( 'flag' ); - } + public function get_title( string $icon_type = 'flag' ): string { + $icon = ( new MslsAdminIcon( null ) )->set_language( $this->language )->set_icon_type( $icon_type ); return sprintf( '%1$s %2$s', $this->obj->blogname, '' . $icon->get_icon() . '' ); } diff --git a/includes/MslsPlugin.php b/includes/MslsPlugin.php index 29f7694e..ed055470 100644 --- a/includes/MslsPlugin.php +++ b/includes/MslsPlugin.php @@ -121,16 +121,18 @@ public static function get_output() { * @return void */ public static function update_adminbar( \WP_Admin_Bar $wp_admin_bar ): void { + $icon_type = MslsAdminIcon::TYPE_LABEL === MslsOptions::instance()->admin_display ? MslsAdminIcon::TYPE_LABEL : MslsAdminIcon::TYPE_FLAG; + $blog_collection = MslsBlogCollection::instance(); foreach ( $blog_collection->get_plugin_active_blogs() as $blog ) { - $title = $blog->get_blavatar() . $blog->get_title(); + $title = $blog->get_blavatar() . $blog->get_title( $icon_type ); $wp_admin_bar->add_node( [ 'id' => 'blog-' . $blog->userblog_id, 'title' => $title ] ); } $blog = $blog_collection->get_current_blog(); if ( is_object( $blog ) && method_exists( $blog, 'get_title' ) ) { - $wp_admin_bar->add_node( [ 'id' => 'site-name', 'title' => $blog->get_title() ] ); + $wp_admin_bar->add_node( [ 'id' => 'site-name', 'title' => $blog->get_title( $icon_type ) ] ); } } @@ -148,7 +150,7 @@ public static function print_alternate_links() { * * @return string */ - function content_filter( $content ) { + public function content_filter( $content ) { if ( ! is_front_page() && is_singular() ) { $options = $this->options; diff --git a/tests/test-mslsadminicon.php b/tests/test-mslsadminicon.php index 3bff63bb..ce59d741 100644 --- a/tests/test-mslsadminicon.php +++ b/tests/test-mslsadminicon.php @@ -164,7 +164,7 @@ public function test_set_icon_type() { } public function test_get_icon() { - Functions\when( 'plugin_dir_path' )->justReturn( dirname( __DIR__, 1 ) . '/' ); + Functions\expect( 'plugin_dir_path' )->atLeast( 1 )->andReturn( dirname( __DIR__, 1 ) . '/' ); $obj = new MslsAdminIcon( 'post' ); $obj->set_icon_type( 'flag' ); diff --git a/tests/test-mslsblog.php b/tests/test-mslsblog.php index 75fa0840..e228a104 100644 --- a/tests/test-mslsblog.php +++ b/tests/test-mslsblog.php @@ -12,6 +12,8 @@ class WP_Test_MslsBlog extends Msls_UnitTestCase { */ function test___get_method() { Functions\expect( 'get_blog_option' )->once()->andReturn( 'it_IT' ); + Functions\expect( 'add_query_arg' )->once()->andReturn( 'https://example.org/added-args' ); + Functions\expect( 'plugin_dir_path' )->atLeast( 1 )->andReturn( dirname( __DIR__, 1 ) . '/' ); $blog = new \stdClass(); $blog->userblog_id = 1; @@ -23,7 +25,7 @@ function test___get_method() { $this->assertEquals( 'Italiano', $obj->get_description() ); $this->assertEquals( 'it_IT', $obj->get_language() ); $this->assertEquals( 'it', $obj->get_alpha2() ); - $this->assertEquals( 'Test (Italiano)', $obj->get_title() ); + $this->assertEquals( 'Test it_IT', $obj->get_title() ); } /**