From e511c52374dea0dd278853f0d115cb17c9e55962 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Fri, 8 Dec 2023 14:01:13 +0100 Subject: [PATCH] Review API functions --- MultisiteLanguageSwitcher.php | 32 +++++-- includes/MslsBlogCollection.php | 6 +- phpunit.xml | 3 + tests/bootstrap.php | 2 - tests/test-mslsblogcollection.php | 138 ++++++++++++++++++++++-------- tests/test-mslsplugin.php | 13 +-- 6 files changed, 140 insertions(+), 54 deletions(-) diff --git a/MultisiteLanguageSwitcher.php b/MultisiteLanguageSwitcher.php index 8c44cbab..d25931e0 100644 --- a/MultisiteLanguageSwitcher.php +++ b/MultisiteLanguageSwitcher.php @@ -101,24 +101,41 @@ function get_msls_flag_url( string $locale ): string { * * @return string */ - function get_msls_blog_description( string $locale ): string { - $blog = \lloc\Msls\MslsBlogCollection::instance()->get_blog( $locale ); + function get_msls_blog_description( string $locale, string $default = '' ): string { + $blog = msls_blog( $locale ); - return $blog->get_description(); + return $blog ? $blog->get_description() : $default; } /** * Gets the permalink for a translation of the current post in a given language * * @param string $locale + * @param string $default * * @return string */ - function get_msls_permalink( $locale ) { - $options = \lloc\Msls\MslsOptions::create(); - $blog = \lloc\Msls\MslsBlogCollection::instance()->get_blog( $locale ); + function get_msls_permalink( string $locale, string $default = '' ): string { + $url = null; + $blog = msls_blog( $locale ); - return $blog->get_url( $options ); + if ( $blog ) { + $options = \lloc\Msls\MslsOptions::create(); + $url = $blog->get_url( $options ); + } + + return $url ?? $default; + } + + /** + * Gets a blog by locale + * + * @param string $locale + * + * @return \lloc\Msls\MslsBlog|null + */ + function msls_blog( string $locale ): ?\lloc\Msls\MslsBlog { + return msls_blog_collection()->get_blog( $locale ); } /** @@ -129,4 +146,5 @@ function get_msls_permalink( $locale ) { function msls_blog_collection(): \lloc\Msls\MslsBlogCollection { return \lloc\Msls\MslsBlogCollection::instance(); } + } diff --git a/includes/MslsBlogCollection.php b/includes/MslsBlogCollection.php index 37adb1d6..7b747370 100644 --- a/includes/MslsBlogCollection.php +++ b/includes/MslsBlogCollection.php @@ -102,7 +102,7 @@ public function __construct() { } /** - * Returns the description of an configured blog or false if it is not configured + * Returns the description of a configured blog or false if it is not configured * * @param int $blog_id * @param string|bool $description @@ -110,13 +110,13 @@ public function __construct() { * @return string|bool */ public static function get_configured_blog_description( $blog_id, $description = false ) { - if ( false != $description ) { + if ( $description ) { return $description; } $temp = get_blog_option( $blog_id, 'msls' ); if ( is_array( $temp ) && empty( $temp['exclude_current_blog'] ) ) { - return $temp['description']; + return $temp['description'] ?? ''; } return false; diff --git a/phpunit.xml b/phpunit.xml index 813a329b..4a147f1e 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -5,6 +5,9 @@ ./includes/ + + + ./tests/ diff --git a/tests/bootstrap.php b/tests/bootstrap.php index e3fb947c..70416d94 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -2,8 +2,6 @@ namespace lloc\MslsTests; -use lloc\Msls\MslsBlog; -use lloc\Msls\MslsBlogCollection; use PHPUnit\Framework\TestCase; use Brain\Monkey; use Brain\Monkey\Functions; diff --git a/tests/test-mslsblogcollection.php b/tests/test-mslsblogcollection.php index e579ccc6..68c1a675 100644 --- a/tests/test-mslsblogcollection.php +++ b/tests/test-mslsblogcollection.php @@ -2,98 +2,164 @@ namespace lloc\MslsTests; +use lloc\Msls\MslsBlog; use lloc\Msls\MslsBlogCollection; use lloc\Msls\MslsOptions; use Brain\Monkey\Functions; +use lloc\Msls\MslsPlugin; /** * WP_Test_MslsBlogCollection */ class WP_Test_MslsBlogCollection extends Msls_UnitTestCase { - function get_test() { - Functions\expect( 'get_users' )->atLeast()->once()->andReturn( [] ); - Functions\expect( 'get_blogs_of_user' )->atLeast()->once()->andReturn( [] ); - Functions\expect( 'get_current_blog_id' )->once()->andReturn( 1 ); + public function setUp(): void { + parent::setUp(); // TODO: Change the autogenerated stub - return new MslsBlogCollection(); - } + $a = \Mockery::mock( MslsBlog::class ); + $a->userblog_id = 1; - function test_get_configured_blog_description_not_empty() { - Functions\expect( 'get_option' )->andReturn( [] ); + $b = \Mockery::mock( MslsBlog::class ); + $b->userblog_id = 2; - $obj = $this->get_test(); + Functions\expect( 'get_current_blog_id' )->atLeast( 1 )->andReturn( 1 ); + Functions\expect( 'get_users' )->atLeast()->once()->andReturn( [] ); + Functions\expect( 'get_blogs_of_user' )->atLeast()->once()->andReturn( [ $a, $b ] ); + Functions\expect( 'get_site_option' )->once()->andReturn( [] ); - $this->assertEquals( 'Test', $obj->get_configured_blog_description( 0, 'Test' ) ); - } + Functions\expect( 'get_option' )->andReturn( [] ); - function test_get_configured_blog_description_empty() { - Functions\expect( 'get_blog_option' )->once()->andReturnNull(); + Functions\expect( 'get_blog_option' )->atLeast( 1 )->andReturnUsing( function( $blog_id, $option ) { + $wplang = [ + 1 => 'de_DE', + 2 => 'it_IT', + ]; + + $msls = [ + 1 => [ 'description' => 'Deutsch' ], + 2 => [ 'description' => 'Italiano' ], + ]; + + switch( $option ) { + case 'active_plugins': + $value = in_array( $blog_id, [ 1, 2 ] ) ? ['multisite-language-switcher/MultisiteLanguageSwitcher.php' ] : []; + break; + case 'WPLANG': + $value = $wplang[ $blog_id ] ?? false; + break; + case 'msls': + $value = $msls[ $blog_id ] ?? false; + break; + } + + return $value; + } ); + } - $obj = $this->get_test(); + public function test_get_configured_blog_description_empty(): void { + $obj = new MslsBlogCollection(); - $this->assertEquals( false, $obj->get_configured_blog_description( 0, false ) ); + $this->assertEquals( 'Test', $obj->get_configured_blog_description( 0, 'Test' ) ); + $this->assertEquals( 'Deutsch', $obj->get_configured_blog_description( 1 ) ); + $this->assertEquals( 'Italiano', $obj->get_configured_blog_description( 2 ) ); + $this->assertFalse( $obj->get_configured_blog_description( 3 ) ); } - function test_get_blogs_of_reference_user() { + public function test_get_blogs_of_reference_user(): void { $options = \Mockery::mock( MslsOptions::class ); $options->shouldReceive( 'has_value' )->andReturn( true ); - $obj = $this->get_test(); + $obj = new MslsBlogCollection(); $this->assertIsArray( $obj->get_blogs_of_reference_user( $options ) ); } - function test_get_current_blog_id() { - $obj = $this->get_test(); + public function test_get_current_blog_id(): void { + $obj = new MslsBlogCollection(); $this->assertIsInt( $obj->get_current_blog_id() ); } - function test_has_current_blog() { - $obj = $this->get_test(); + public function test_has_current_blog(): void { + $obj = new MslsBlogCollection(); $this->assertIsBool( $obj->has_current_blog() ); } - function test_get_objects() { - $obj = $this->get_test(); + public function test_is_current_blog_true(): void { + $obj = new MslsBlogCollection(); - $this->assertIsArray( $obj->get_objects() ); + $blog = \Mockery::mock( MslsBlog::class ); + $blog->userblog_id = 1; + + $this->assertTrue( $obj->is_current_blog( $blog ) ); } - function test_is_plugin_active() { - Functions\expect( 'get_site_option' )->once()->andReturn( [] ); - Functions\expect( 'get_blog_option' )->once()->andReturn( [] ); + public function test_is_current_blog_false(): void { + $obj = new MslsBlogCollection(); + + $blog = \Mockery::mock( MslsBlog::class ); + $blog->userblog_id = 2; - $obj = $this->get_test(); + $this->assertFalse( $obj->is_current_blog( $blog ) ); + } + + public function test_get_objects(): void { + $obj = new MslsBlogCollection(); + + $this->assertIsArray( $obj->get_objects() ); + } + + public function test_is_plugin_active(): void { + $obj = new MslsBlogCollection(); $this->assertIsBool( $obj->is_plugin_active( 0 ) ); } - function test_get_plugin_active_blogs() { - $obj = $this->get_test(); + public function test_get_plugin_active_blogs(): void { + $obj = new MslsBlogCollection(); $this->assertIsArray( $obj->get_plugin_active_blogs() ); } - function test_get() { - $obj = $this->get_test(); + public function test_get(): void { + $obj = new MslsBlogCollection(); $this->assertIsArray( $obj->get() ); } - function test_get_filtered() { - $obj = $this->get_test(); + public function test_get_filtered(): void { + $obj = new MslsBlogCollection(); $this->assertIsArray( $obj->get_filtered() ); } - function test_get_users() { - $obj = $this->get_test(); + public function test_get_users(): void { + $obj = new MslsBlogCollection(); $this->assertIsArray( $obj->get_users() ); } + public function test_get_current_blog(): void { + $obj = new MslsBlogCollection(); + + $this->assertInstanceOf( MslsBlog::class, $obj->get_current_blog() ); + } + + public function test_get_blog_language(): void { + $obj = new MslsBlogCollection(); + + $this->assertEquals( 'de_DE', $obj->get_blog_language( 1 ) ); + $this->assertEquals( 'it_IT', $obj->get_blog_language( 2 ) ); + + $this->assertEquals( 'de_DE', $obj->get_blog_language() ); + } + + public function test_get_blog_id(): void { + $obj = new MslsBlogCollection(); + + $this->assertEquals( 1, $obj->get_blog_id( 'de_DE' ) ); + $this->assertEquals( 2, $obj->get_blog_id( 'it_IT' ) ); + } } diff --git a/tests/test-mslsplugin.php b/tests/test-mslsplugin.php index fabcd189..ebe7aaf0 100644 --- a/tests/test-mslsplugin.php +++ b/tests/test-mslsplugin.php @@ -19,7 +19,7 @@ function get_test() { /** * Verify the static init-method */ - function test_admin_menu_method() { + function test_admin_menu(): void { Functions\when( 'wp_enqueue_style' )->returnArg(); Functions\when( 'plugins_url' )->justReturn( 'https://lloc.de/wp-content/plugins' ); @@ -29,7 +29,7 @@ function test_admin_menu_method() { /** * Verify the static init_widget-method */ - function test_init_widget_method() { + function test_init_widget(): void { Functions\when( 'register_widget' )->justReturn( true ); $this->assertIsBool( $this->get_test()->init_widget() ); @@ -38,7 +38,7 @@ function test_init_widget_method() { /** * Verify the static init_i18n_support-method */ - function test_init_i18n_support_method() { + function test_init_i18n_support(): void { Functions\when( 'load_plugin_textdomain' )->justReturn( true ); $this->assertIsBool( $this->get_test()->init_i18n_support() ); @@ -47,15 +47,16 @@ function test_init_i18n_support_method() { /** * Verify the static message_handler-method */ - function test_message_handler_method() { + function test_message_handler(): void { $this->expectOutputString( '

Test

' ); + MslsPlugin::message_handler( 'Test' ); } /** * Verify the static uninstall-method */ - function test_uninstall_method() { + function test_uninstall(): void { Functions\when( 'delete_option' )->justReturn( false ); $this->assertIsBool( $this->get_test()->uninstall() ); @@ -64,7 +65,7 @@ function test_uninstall_method() { /** * Verify the static cleanup-method */ - function test_cleanup_method() { + function test_cleanup(): void { Functions\when( 'delete_option' )->justReturn( false ); $this->assertIsBool( MslsPlugin::cleanup() );