From 15dcf53cb818ad1d16b966e4a5da3481bbeffb2c Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Sun, 10 Dec 2023 14:22:48 +0100 Subject: [PATCH] Tests added --- includes/MslsOptions.php | 3 +- tests/test-mslsadmin.php | 9 +++- tests/test-mslsadminicontaxonomy.php | 51 +++++++++++----------- tests/test-mslsblogcollection.php | 65 +++++++++++++++++++++++++--- 4 files changed, 94 insertions(+), 34 deletions(-) diff --git a/includes/MslsOptions.php b/includes/MslsOptions.php index a2902d8e..66b81f0c 100644 --- a/includes/MslsOptions.php +++ b/includes/MslsOptions.php @@ -13,10 +13,11 @@ * General options class * @package Msls * @property bool $activate_autocomplete + * @property bool output_current_blog * @property int $display - * @property string $admin_display * @property int $reference_user * @property int $content_priority + * @property string $admin_display * @property string $admin_language * @property string $description * @property string $before_item diff --git a/tests/test-mslsadmin.php b/tests/test-mslsadmin.php index 15d12174..ee40cd27 100644 --- a/tests/test-mslsadmin.php +++ b/tests/test-mslsadmin.php @@ -116,10 +116,17 @@ function test_blog_language() { function test_display() { $obj = $this->get_sut(); - $this->expectOutputRegex( '/^' ); $obj->display(); } + function test_admin_display() { + $obj = $this->get_sut(); + + $this->expectOutputString( '' ); + $obj->admin_display(); + } + function test_reference_user() { $users = []; $too_much = MslsAdmin::MAX_REFERENCE_USERS + 1; diff --git a/tests/test-mslsadminicontaxonomy.php b/tests/test-mslsadminicontaxonomy.php index 70373dc7..b5c9d06a 100644 --- a/tests/test-mslsadminicontaxonomy.php +++ b/tests/test-mslsadminicontaxonomy.php @@ -7,64 +7,63 @@ class WP_Test_MslsAdminIconTaxonomy extends Msls_UnitTestCase { - protected $lang = 'de_DE'; + const LANGUAGE = 'de_DE'; + const IMAGE_SRC = '/dev/german_flag.png'; - protected $src = '/dev/german_flag.png'; + public function setUp(): void { + parent::setUp(); - public function get_test() { - return ( new MslsAdminIconTaxonomy( 'post_tag' ) ) - ->set_path() - ->set_language( $this->lang ) - ->set_src( $this->src ); + Functions\expect( 'add_query_arg' )->twice()->andReturn( 'https://example.org/added-args' ); } - function test_get_img() { - Functions\expect( 'add_query_arg' )->twice()->andReturn( 'https://example.org/added-args' ); + public function test_get_img(): void { Functions\expect( 'get_query_var' )->once()->andReturn( 'post_tag' ); Functions\expect( 'get_taxonomies' )->once()->andReturn( [] ); - $obj = $this->get_test(); + $obj = ( new MslsAdminIconTaxonomy( 'post_tag' ) )->set_path()->set_language( self::LANGUAGE )->set_src( self::IMAGE_SRC ); - $this->assertEquals( 'de_DE', $obj->get_img() ); + $expected = sprintf( 'de_DE', self::IMAGE_SRC ); + + $this->assertEquals( $expected, $obj->get_img() ); } - function test_get_edit_new() { + public function test_get_edit_new(): void { $admin_url = 'https://example.org/wp-admin/?new_tag'; - Functions\expect( 'add_query_arg' )->twice()->andReturn( 'https://example.org/added-args' ); Functions\expect( 'get_admin_url' )->once()->andReturn( $admin_url ); Functions\expect( 'get_current_blog_id' )->once()->andReturn( 1 ); - $obj = $this->get_test(); + $obj = ( new MslsAdminIconTaxonomy( 'post_tag' ) )->set_path()->set_language( self::LANGUAGE )->set_src( self::IMAGE_SRC ); $this->assertEquals( $admin_url, $obj->get_edit_new() ); } - function test_set_href() { - Functions\expect( 'add_query_arg' )->twice()->andReturn( 'add-query-args' ); + public function test_set_href(): void { Functions\expect( 'get_edit_term_link' )->once()->andReturn( 'get-edit-post-link' ); - $obj = $this->get_test(); + $obj = ( new MslsAdminIconTaxonomy( 'post_tag' ) )->set_path()->set_language( self::LANGUAGE )->set_src( self::IMAGE_SRC ); $this->assertInstanceOf( MslsAdminIconTaxonomy::class, $obj->set_href( 42 ) ); - $value = ' '; - $this->assertEquals( $value, $obj->get_a() ); - $this->assertEquals( $value, $obj->__toString() ); + + $expected = ' '; + + $this->assertEquals( $expected, $obj->get_a() ); + $this->assertEquals( $expected, $obj->__toString() ); } - function test_set_href_empty() { + public function test_set_href_empty(): void { Functions\expect( 'get_current_blog_id' )->twice()->andReturn( 1 ); - Functions\expect( 'add_query_arg' )->twice()->andReturn( 'add-query-args' ); Functions\expect( 'get_edit_term_link' )->once()->andReturn( '' ); Functions\expect( 'get_admin_url' )->twice()->andReturn( 'admin-url-empty' ); - $obj = $this->get_test(); + $obj = ( new MslsAdminIconTaxonomy( 'post_tag' ) )->set_path()->set_language( self::LANGUAGE )->set_src( self::IMAGE_SRC ); $this->assertInstanceOf( MslsAdminIconTaxonomy::class, $obj->set_href( 0 ) ); - $value = ' '; - $this->assertEquals( $value, $obj->get_a() ); - $this->assertEquals( $value, $obj->__toString() ); + $expected = ' '; + + $this->assertEquals( $expected, $obj->get_a() ); + $this->assertEquals( $expected, $obj->__toString() ); } } diff --git a/tests/test-mslsblogcollection.php b/tests/test-mslsblogcollection.php index 68c1a675..bb9c76a8 100644 --- a/tests/test-mslsblogcollection.php +++ b/tests/test-mslsblogcollection.php @@ -23,27 +23,31 @@ public function setUp(): void { $b = \Mockery::mock( MslsBlog::class ); $b->userblog_id = 2; + $c = \Mockery::mock( MslsBlog::class ); + $c->userblog_id = 3; + 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( [] ); + Functions\expect( 'get_blogs_of_user' )->atLeast()->once()->andReturn( [ $a, $b, $c ] ); - Functions\expect( 'get_option' )->andReturn( [] ); + Functions\expect( 'get_option' )->andReturn( [ 'output_current_blog' => true ] ); Functions\expect( 'get_blog_option' )->atLeast( 1 )->andReturnUsing( function( $blog_id, $option ) { $wplang = [ 1 => 'de_DE', 2 => 'it_IT', + 3 => 'fr_FR', ]; $msls = [ 1 => [ 'description' => 'Deutsch' ], 2 => [ 'description' => 'Italiano' ], + 3 => [ 'description' => 'Français' ], ]; switch( $option ) { case 'active_plugins': - $value = in_array( $blog_id, [ 1, 2 ] ) ? ['multisite-language-switcher/MultisiteLanguageSwitcher.php' ] : []; + $value = in_array( $blog_id, [ 1, 2 ] ) ? [ 'multisite-language-switcher/MultisiteLanguageSwitcher.php' ] : []; break; case 'WPLANG': $value = $wplang[ $blog_id ] ?? false; @@ -58,15 +62,22 @@ public function setUp(): void { } public function test_get_configured_blog_description_empty(): void { + Functions\expect( 'get_site_option' )->once()->andReturn( [] ); + $obj = new MslsBlogCollection(); $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 ) ); + $this->assertEquals( 'Français', $obj->get_configured_blog_description( 3 ) ); + + $this->assertFalse( $obj->get_configured_blog_description( 4 ) ); } public function test_get_blogs_of_reference_user(): void { + Functions\expect( 'get_site_option' )->once()->andReturn( [] ); + $options = \Mockery::mock( MslsOptions::class ); $options->shouldReceive( 'has_value' )->andReturn( true ); @@ -76,18 +87,24 @@ public function test_get_blogs_of_reference_user(): void { } public function test_get_current_blog_id(): void { + Functions\expect( 'get_site_option' )->once()->andReturn( [] ); + $obj = new MslsBlogCollection(); $this->assertIsInt( $obj->get_current_blog_id() ); } public function test_has_current_blog(): void { + Functions\expect( 'get_site_option' )->once()->andReturn( [] ); + $obj = new MslsBlogCollection(); $this->assertIsBool( $obj->has_current_blog() ); } public function test_is_current_blog_true(): void { + Functions\expect( 'get_site_option' )->once()->andReturn( [] ); + $obj = new MslsBlogCollection(); $blog = \Mockery::mock( MslsBlog::class ); @@ -97,6 +114,8 @@ public function test_is_current_blog_true(): void { } public function test_is_current_blog_false(): void { + Functions\expect( 'get_site_option' )->once()->andReturn( [] ); + $obj = new MslsBlogCollection(); $blog = \Mockery::mock( MslsBlog::class ); @@ -106,60 +125,94 @@ public function test_is_current_blog_false(): void { } public function test_get_objects(): void { + Functions\expect( 'get_site_option' )->once()->andReturn( [] ); + $obj = new MslsBlogCollection(); $this->assertIsArray( $obj->get_objects() ); } + public function test_is_plugin_active_networkwide(): void { + Functions\expect( 'get_site_option' )->once()->andReturn( [ 'multisite-language-switcher/MultisiteLanguageSwitcher.php' => 'Multisite Language Switcher' ] ); + + $obj = new MslsBlogCollection(); + + $this->assertTrue( $obj->is_plugin_active( 4 ) ); + } + public function test_is_plugin_active(): void { + Functions\expect( 'get_site_option' )->once()->andReturn( [] ); + $obj = new MslsBlogCollection(); - $this->assertIsBool( $obj->is_plugin_active( 0 ) ); + $this->assertTrue( $obj->is_plugin_active( 1 ) ); + $this->assertTrue( $obj->is_plugin_active( 2 ) ); + + $this->assertFalse( $obj->is_plugin_active( 3 ) ); } public function test_get_plugin_active_blogs(): void { + Functions\expect( 'get_site_option' )->once()->andReturn( [] ); + $obj = new MslsBlogCollection(); $this->assertIsArray( $obj->get_plugin_active_blogs() ); } public function test_get(): void { + Functions\expect( 'get_site_option' )->once()->andReturn( [] ); + $obj = new MslsBlogCollection(); $this->assertIsArray( $obj->get() ); } public function test_get_filtered(): void { + Functions\expect( 'get_site_option' )->once()->andReturn( [] ); + $obj = new MslsBlogCollection(); $this->assertIsArray( $obj->get_filtered() ); + $this->assertIsArray( $obj->get_filtered( true ) ); } public function test_get_users(): void { + Functions\expect( 'get_site_option' )->once()->andReturn( [] ); + $obj = new MslsBlogCollection(); $this->assertIsArray( $obj->get_users() ); } public function test_get_current_blog(): void { + Functions\expect( 'get_site_option' )->once()->andReturn( [] ); + $obj = new MslsBlogCollection(); $this->assertInstanceOf( MslsBlog::class, $obj->get_current_blog() ); } public function test_get_blog_language(): void { + Functions\expect( 'get_site_option' )->once()->andReturn( [] ); + $obj = new MslsBlogCollection(); $this->assertEquals( 'de_DE', $obj->get_blog_language( 1 ) ); $this->assertEquals( 'it_IT', $obj->get_blog_language( 2 ) ); + $this->assertEquals( 'fr_FR', $obj->get_blog_language( 3 ) ); $this->assertEquals( 'de_DE', $obj->get_blog_language() ); } public function test_get_blog_id(): void { + Functions\expect( 'get_site_option' )->once()->andReturn( [] ); + $obj = new MslsBlogCollection(); $this->assertEquals( 1, $obj->get_blog_id( 'de_DE' ) ); $this->assertEquals( 2, $obj->get_blog_id( 'it_IT' ) ); + + $this->assertNull( $obj->get_blog_id( 'fr_FR' ) ); } + }