diff --git a/MultisiteLanguageSwitcher.php b/MultisiteLanguageSwitcher.php index aa9a1751..8c44cbab 100644 --- a/MultisiteLanguageSwitcher.php +++ b/MultisiteLanguageSwitcher.php @@ -121,4 +121,12 @@ function get_msls_permalink( $locale ) { return $blog->get_url( $options ); } + /** + * Gets the MslsBlogCollection instance + * + * @return \lloc\Msls\MslsBlogCollection + */ + function msls_blog_collection(): \lloc\Msls\MslsBlogCollection { + return \lloc\Msls\MslsBlogCollection::instance(); + } } diff --git a/includes/ContentImport/MetaBox.php b/includes/ContentImport/MetaBox.php index 6868261d..825fab0f 100644 --- a/includes/ContentImport/MetaBox.php +++ b/includes/ContentImport/MetaBox.php @@ -24,7 +24,7 @@ public function render() { $input_lang = isset( $_GET['msls_lang'] ) ? $_GET['msls_lang'] : null; $input_id = isset( $_GET['msls_id'] ) ? $_GET['msls_id'] : null; $has_input = null !== $input_lang && null !== $input_id; - $blogs = MslsBlogCollection::instance(); + $blogs = msls_blog_collection(); $available = array_filter( array_map( function ( $lang ) use ( $mydata ) { return $mydata->{$lang}; }, array_keys( $languages ) ) ); diff --git a/includes/MslsAdmin.php b/includes/MslsAdmin.php index 0e45d970..8292c80a 100644 --- a/includes/MslsAdmin.php +++ b/includes/MslsAdmin.php @@ -27,7 +27,7 @@ class MslsAdmin extends MslsMain { public static function init() { if ( ! ( $obj = MslsRegistry::get_object( __CLASS__ ) ) ) { $options = MslsOptions::instance(); - $collection = MslsBlogCollection::instance(); + $collection = msls_blog_collection(); $obj = new static( $options, $collection ); diff --git a/includes/MslsBlog.php b/includes/MslsBlog.php index b94eb533..cb49c76f 100644 --- a/includes/MslsBlog.php +++ b/includes/MslsBlog.php @@ -107,7 +107,7 @@ public function get_alpha2() { * @return string|null */ public function get_url( $options ) { - if ( $this->obj->userblog_id == MslsBlogCollection::instance()->get_current_blog_id() ) { + if ( $this->obj->userblog_id == msls_blog_collection()->get_current_blog_id() ) { return $options->get_current_link(); } diff --git a/includes/MslsBlogCollection.php b/includes/MslsBlogCollection.php index 0c64a2e5..37adb1d6 100644 --- a/includes/MslsBlogCollection.php +++ b/includes/MslsBlogCollection.php @@ -7,6 +7,8 @@ namespace lloc\Msls; +use const Patchwork\CodeManipulation\Actions\RedefinitionOfNew\publicizeConstructors; + /** * Collection of blog-objects * @@ -330,4 +332,4 @@ public static function get_blog_language( $blog_id = null, $default = 'en_US' ) return '' !== $language ? $language : $default; } -} +} \ No newline at end of file diff --git a/includes/MslsCustomColumn.php b/includes/MslsCustomColumn.php index a4d92d7d..7bde5555 100644 --- a/includes/MslsCustomColumn.php +++ b/includes/MslsCustomColumn.php @@ -23,7 +23,7 @@ class MslsCustomColumn extends MslsMain { */ public static function init() { $options = MslsOptions::instance(); - $collection = MslsBlogCollection::instance(); + $collection = msls_blog_collection(); $obj = new static( $options, $collection ); if ( ! $options->is_excluded() ) { diff --git a/includes/MslsCustomColumnTaxonomy.php b/includes/MslsCustomColumnTaxonomy.php index c86986d6..7f83657b 100644 --- a/includes/MslsCustomColumnTaxonomy.php +++ b/includes/MslsCustomColumnTaxonomy.php @@ -23,7 +23,7 @@ class MslsCustomColumnTaxonomy extends MslsCustomColumn { */ public static function init() { $options = MslsOptions::instance(); - $collection = MslsBlogCollection::instance(); + $collection = msls_blog_collection(); $obj = new static( $options, $collection ); if ( ! $options->is_excluded() ) { diff --git a/includes/MslsCustomFilter.php b/includes/MslsCustomFilter.php index 2f1d40d0..1b38fb8b 100644 --- a/includes/MslsCustomFilter.php +++ b/includes/MslsCustomFilter.php @@ -23,7 +23,7 @@ class MslsCustomFilter extends MslsMain { */ public static function init() { $options = MslsOptions::instance(); - $collection = MslsBlogCollection::instance(); + $collection = msls_blog_collection(); $obj = new static( $options, $collection ); if ( ! $options->is_excluded() ) { diff --git a/includes/MslsMain.php b/includes/MslsMain.php index 4f519f35..d3a6499d 100644 --- a/includes/MslsMain.php +++ b/includes/MslsMain.php @@ -48,7 +48,7 @@ public function __construct( MslsOptions $options, MslsBlogCollection $collectio */ public static function init() { $options = MslsOptions::instance(); - $collection = MslsBlogCollection::instance(); + $collection = msls_blog_collection(); return new static( $options, $collection ); } @@ -169,7 +169,7 @@ protected function save( $object_id, $class ) { } if ( ! $this->collection->has_current_blog() ) { - $this->debugger( 'MslsBlogCollection::instance()->has_current_blog returns false.' ); + $this->debugger( 'BlogCollection returns false when calling has_current_blog.' ); return; } diff --git a/includes/MslsMetaBox.php b/includes/MslsMetaBox.php index fdba68c7..399c87d5 100644 --- a/includes/MslsMetaBox.php +++ b/includes/MslsMetaBox.php @@ -103,7 +103,7 @@ public static function get_suggested_fields( $json, $args ) { */ public static function init() { $options = MslsOptions::instance(); - $collection = MslsBlogCollection::instance(); + $collection = msls_blog_collection(); $obj = new static( $options, $collection ); if ( ! $options->is_excluded() ) { diff --git a/includes/MslsOptionsPost.php b/includes/MslsOptionsPost.php index 625634c6..54746a45 100644 --- a/includes/MslsOptionsPost.php +++ b/includes/MslsOptionsPost.php @@ -46,7 +46,7 @@ public function get_postlink( $language ) { } global $current_site; - $blog_id = MslsBlogCollection::instance()->get_blog_id( $language ); + $blog_id = msls_blog_collection()->get_blog_id( $language ); if ( $current_site->blog_id != $blog_id ) { $option = get_blog_option( $blog_id, 'msls' ); //error_log( print_r( $option, true ) ); diff --git a/includes/MslsOutput.php b/includes/MslsOutput.php index 6bebd59a..8bc44994 100644 --- a/includes/MslsOutput.php +++ b/includes/MslsOutput.php @@ -97,7 +97,7 @@ public function get( $display, $filter = false, $exists = false ) { * @return string */ public function get_alternate_links() { - $blogs = MslsBlogCollection::instance(); + $blogs = msls_blog_collection(); $hreflang = new HrefLang( $blogs ); $options = MslsOptions::create(); diff --git a/includes/MslsPlugin.php b/includes/MslsPlugin.php index 15f88bcf..121791e1 100644 --- a/includes/MslsPlugin.php +++ b/includes/MslsPlugin.php @@ -123,7 +123,7 @@ public static function get_output() { public static function update_adminbar( \WP_Admin_Bar $wp_admin_bar ): void { $icon_type = MslsOptions::instance()->get_icon_type(); - $blog_collection = MslsBlogCollection::instance(); + $blog_collection = msls_blog_collection(); foreach ( $blog_collection->get_plugin_active_blogs() as $blog ) { $title = $blog->get_blavatar() . $blog->get_title( $icon_type ); diff --git a/includes/MslsPostTag.php b/includes/MslsPostTag.php index 280c66f8..72ea0ae5 100644 --- a/includes/MslsPostTag.php +++ b/includes/MslsPostTag.php @@ -75,7 +75,7 @@ public static function suggest() { */ public static function init() { $options = MslsOptions::instance(); - $collection = MslsBlogCollection::instance(); + $collection = msls_blog_collection(); if ( $options->activate_autocomplete ) { $obj = new static( $options, $collection ); diff --git a/includes/MslsWidget.php b/includes/MslsWidget.php index d9f46ef1..38da9d37 100644 --- a/includes/MslsWidget.php +++ b/includes/MslsWidget.php @@ -37,33 +37,25 @@ public function __construct() { * @user MslsOutput */ public function widget( $args, $instance ) { - $args = wp_parse_args( - $args, - [ - 'before_widget' => '', - 'after_widget' => '', - 'before_title' => '', - 'after_title' => '', - ] - ); + $default = [ + 'before_widget' => '', + 'after_widget' => '', + 'before_title' => '', + 'after_title' => '', + ]; + + $args = wp_parse_args( $args, $default ); /** This filter is documented in wp-includes/default-widgets.php */ - $title = apply_filters( - 'widget_title', - ( $instance['title'] ?? '' ), - $instance, - $this->id_base - ); + $title = apply_filters( 'widget_title', $instance['title'] ?? '', $instance, $this->id_base ); if ( $title ) { $title = $args['before_title'] . esc_attr( $title ) . $args['after_title']; } $content = MslsOutput::init()->__toString(); - if ( '' == $content ) { - $content = apply_filters( - 'msls_widget_alternative_content', - __( 'No available translations found', 'multisite-language-switcher' ) - ); + if ( '' === $content ) { + $text = __( 'No available translations found', 'multisite-language-switcher' ); + $content = apply_filters( 'msls_widget_alternative_content', $text ); } echo $args['before_widget'], $title, $content, $args['after_widget']; diff --git a/tests/bootstrap.php b/tests/bootstrap.php index a89f4264..e3fb947c 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -27,30 +27,6 @@ protected function setUp(): void { Functions\when( '__' )->returnArg(); } - /** - * Get a of the MslsBlogCollection class that contains some blogs - * - * @param $map - * - * @return void - */ - public function getBlogsCollection( $map = [ 'de_DE' => 'de', 'en_US' => 'en' ] ): MslsBlogCollection { - foreach ( $map as $locale => $alpha2 ) { - $blog = \Mockery::mock( MslsBlog::class ); - $blog->shouldReceive( [ - 'get_alpha2' => $alpha2, - 'get_language' => $locale, - ] ); - - $blogs[] = $blog; - } - - $collection = \Mockery::mock( MslsBlogCollection::class ); - $collection->shouldReceive( 'get_objects' )->andReturn( $blogs ); - $collection->shouldReceive( 'get' )->andReturn( $blogs ); - - return $collection; - } protected function tearDown(): void { restore_error_handler(); diff --git a/tests/test-mslsblog.php b/tests/test-mslsblog.php index e228a104..1cd06d76 100644 --- a/tests/test-mslsblog.php +++ b/tests/test-mslsblog.php @@ -4,28 +4,91 @@ use lloc\Msls\MslsBlog; use Brain\Monkey\Functions; +use lloc\Msls\MslsOptions; +use lloc\Msls\MslsBlogCollection; class WP_Test_MslsBlog extends Msls_UnitTestCase { - /** - * Verify the __get-method - */ - function test___get_method() { + public function get_blog(): MslsBlog { 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; $blog->blogname = 'Test'; - $obj = new MslsBlog( $blog, 'Italiano' ); + return new MslsBlog( $blog, 'Italiano' ); + } + + public function test_get_userblog_id(): void { + $this->assertEquals( 1, $this->get_blog()->userblog_id ); + } - $this->assertEquals( 1, $obj->userblog_id ); - $this->assertEquals( 'Italiano', $obj->get_description() ); - $this->assertEquals( 'it_IT', $obj->get_language() ); - $this->assertEquals( 'it', $obj->get_alpha2() ); - $this->assertEquals( 'Test
', $obj->get_title() ); + public function test_get_description(): void { + $this->assertEquals( 'Italiano', $this->get_blog()->get_description() ); + } + + public function test_get_url_current(): void { + $url = 'https://example.org/'; + + $option = \Mockery::mock( MslsOptions::class ); + $option->shouldReceive( 'get_current_link' )->andReturn( $url ); + + $collection = \Mockery::mock( MslsBlogCollection::class ); + $collection->shouldReceive( 'get_current_blog_id' )->andReturn( 1 ); + + Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection ); + + $this->assertEquals( $url, $this->get_blog()->get_url( $option ) ); + } + + public function test_get_frontpage(): void { + $url = 'https://example.org/'; + + $option = \Mockery::mock( MslsOptions::class ); + $option->shouldReceive( 'get_permalink' )->once()->andReturn( $url ); + + $collection = \Mockery::mock( MslsBlogCollection::class ); + $collection->shouldReceive( 'get_current_blog_id' )->andReturn( 2 ); + + Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection ); + Functions\expect( 'is_front_page' )->once()->andReturn( true ); + Functions\expect( 'switch_to_blog' )->once(); + Functions\expect( 'restore_current_blog' )->once(); + + $this->assertEquals( $url, $this->get_blog()->get_url( $option ) ); + } + + public function test_get_url(): void { + $url = 'https://example.org/'; + + $option = \Mockery::mock( MslsOptions::class ); + $option->shouldReceive( 'get_permalink' )->once()->andReturn( $url ); + $option->shouldReceive( 'has_value' )->once()->andReturn( true ); + + $collection = \Mockery::mock( MslsBlogCollection::class ); + $collection->shouldReceive( 'get_current_blog_id' )->andReturn( 2 ); + + Functions\expect( 'msls_blog_collection' )->once()->andReturn( $collection ); + Functions\expect( 'is_front_page' )->once()->andReturn( false ); + Functions\expect( 'switch_to_blog' )->once(); + Functions\expect( 'restore_current_blog' )->once(); + + $this->assertEquals( $url, $this->get_blog()->get_url( $option ) ); + } + + public function test_get_language(): void { + $this->assertEquals( 'it_IT', $this->get_blog()->get_language() ); + } + + public function test_get_alpha2(): void { + $this->assertEquals( 'it', $this->get_blog()->get_alpha2() ); + } + + public function test_get_title(): void { + Functions\expect( 'add_query_arg' )->once()->andReturn( 'https://example.org/added-args' ); + + $this->assertEquals( 'Test ', $this->get_blog()->get_title() ); } /** @@ -47,8 +110,9 @@ public function compareProvider() { * Verify the _cmp-method * @dataProvider compareProvider */ - function test__cmp_method( $a, $b, $expected ) { + public function test__cmp_method( $a, $b, $expected ): void { $this->assertEquals( $expected, MslsBlog::_cmp( $a, $b ) ); + $obj = new MslsBlog( null, null ); $this->assertEquals( $expected, $obj->_cmp( $a, $b ) ); } @@ -56,19 +120,41 @@ function test__cmp_method( $a, $b, $expected ) { /** * Verify the language-method */ - function test_language_cmp() { + public function test_language_cmp(): void { $a = new MslsBlog( null, null ); $b = new MslsBlog( null, null ); + $this->assertEquals( 0, $a->language( $a, $b ) ); } /** * Verify the description-method */ - function test_description_cmp() { + public function test_description_cmp(): void { $a = new MslsBlog( null, null ); $b = new MslsBlog( null, null ); + $this->assertEquals( 0, $a->description( $a, $b ) ); } + public function test_get_blavatar_lazy(): void { + Functions\expect( 'switch_to_blog' )->once(); + Functions\expect( 'restore_current_blog' )->once(); + Functions\expect( 'has_site_icon' )->once()->andReturn( true ); + Functions\expect( 'get_site_icon_url' )->twice()->andReturn( 'https://example.org/icons/abc.png' ); + Functions\expect( 'wp_lazy_loading_enabled' )->once()->andReturn( true ); + + $this->assertEquals( '', $this->get_blog()->get_blavatar() ); + } + + public function test_get_blavatar(): void { + Functions\expect( 'switch_to_blog' )->once(); + Functions\expect( 'restore_current_blog' )->once(); + Functions\expect( 'has_site_icon' )->once()->andReturn( true ); + Functions\expect( 'get_site_icon_url' )->twice()->andReturn( 'https://example.org/icons/abc.png' ); + Functions\expect( 'wp_lazy_loading_enabled' )->once()->andReturn( false ); + + $this->assertEquals( '', $this->get_blog()->get_blavatar() ); + } + } diff --git a/tests/test-mslscustomcolumn.php b/tests/test-mslscustomcolumn.php index 8aaf9da8..ac6eb887 100644 --- a/tests/test-mslscustomcolumn.php +++ b/tests/test-mslscustomcolumn.php @@ -2,6 +2,7 @@ namespace lloc\MslsTests; +use lloc\Msls\MslsBlog; use lloc\Msls\MslsCustomColumn; use lloc\Msls\MslsOptions; use lloc\Msls\MslsBlogCollection; @@ -15,7 +16,23 @@ function test_th() { Functions\when( 'plugin_dir_path' )->justReturn( dirname( __DIR__, 1 ) . '/' ); $options = \Mockery::mock( MslsOptions::class ); - $obj = new MslsCustomColumn( $options, $this->getBlogsCollection() ); + + foreach ( [ 'de_DE' => 'de', 'en_US' => 'en' ] as $locale => $alpha2 ) { + $blog = \Mockery::mock( MslsBlog::class ); + $blog->shouldReceive( [ + 'get_alpha2' => $alpha2, + 'get_language' => $locale, + ] ); + + $blogs[] = $blog; + } + + $collection = \Mockery::mock( MslsBlogCollection::class ); + $collection->shouldReceive( 'get_objects' )->andReturn( $blogs ); + $collection->shouldReceive( 'get' )->andReturn( $blogs ); + $collection->shouldReceive( 'get_current_blog_id' )->andReturn( 1 ); + + $obj = new MslsCustomColumn( $options, $collection ); $expected = [ 'mslscol' => ' ' ]; $this->assertEquals( $expected, $obj->th( [] ) ); @@ -23,6 +40,7 @@ function test_th() { function test_th_empty() { $options = \Mockery::mock( MslsOptions::class ); + $collection = \Mockery::mock( MslsBlogCollection::class ); $collection->shouldReceive( 'get' )->once()->andReturn( [] ); diff --git a/tests/test-mslswidget.php b/tests/test-mslswidget.php index 4ffe71b2..ed2972d0 100644 --- a/tests/test-mslswidget.php +++ b/tests/test-mslswidget.php @@ -4,6 +4,7 @@ use Brain\Monkey\Functions; +use lloc\Msls\MslsBlogCollection; use lloc\Msls\MslsWidget; class WP_Test_MslsWidget extends Msls_UnitTestCase { @@ -14,27 +15,28 @@ public function get_sut() { return \Mockery::mock( MslsWidget::class )->makePartial(); } - function test_widget_method() { + function test_widget() { + $collection = \Mockery::mock( MslsBlogCollection::class ); + $collection->shouldReceive( 'get_filtered' )->once()->andReturn( [] ); + $arr = [ - 'before_widget' => '', - 'after_widget' => '', - 'before_title' => '', - 'after_title' => '', + 'before_widget' => '