From 0e92515a31c61c0dda904c16044a5c37174d7305 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Mon, 16 Sep 2024 11:04:40 +0200 Subject: [PATCH 1/2] Set method tested --- tests/phpunit/TestMslsPostTag.php | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/tests/phpunit/TestMslsPostTag.php b/tests/phpunit/TestMslsPostTag.php index 597ba8ef..48b1b32a 100644 --- a/tests/phpunit/TestMslsPostTag.php +++ b/tests/phpunit/TestMslsPostTag.php @@ -31,6 +31,8 @@ protected function setUp(): void { $collection = \Mockery::mock( MslsBlogCollection::class ); $collection->shouldReceive( 'get' )->andReturn( $blogs ); + $collection->shouldReceive( 'has_current_blog' )->andReturnTrue(); + $collection->shouldReceive( 'get_current_blog' )->andReturn( $blogs[0] ); $this->test = new MslsPostTag( $options, $collection ); } @@ -163,4 +165,17 @@ public function test_the_input_no_blogs(): void { $this->assertFalse( $value ); } + + public function test_set() { + $taxonomy = \Mockery::mock( MslsTaxonomy::class ); + $taxonomy->shouldReceive( 'acl_request' )->once()->andReturn( 'post_tag' ); + + Functions\expect( 'msls_content_types' )->once()->andReturn( $taxonomy ); + Functions\expect( 'delete_option' )->twice(); + Functions\expect( 'switch_to_blog' )->twice(); + Functions\expect( 'restore_current_blog' )->twice(); + + $this->expectNotToPerformAssertions(); + $this->test->set( 42 ); + } } From 38083488736bce771d1dc5ea1ddc206e84fea0cc Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Thu, 19 Sep 2024 16:36:27 +0200 Subject: [PATCH 2/2] PostTag coverage completed --- tests/phpunit/TestMslsPostTag.php | 85 +++++++++++++++++++++++++++---- 1 file changed, 76 insertions(+), 9 deletions(-) diff --git a/tests/phpunit/TestMslsPostTag.php b/tests/phpunit/TestMslsPostTag.php index 48b1b32a..fdb69e1b 100644 --- a/tests/phpunit/TestMslsPostTag.php +++ b/tests/phpunit/TestMslsPostTag.php @@ -7,6 +7,7 @@ use lloc\Msls\MslsBlog; use lloc\Msls\MslsBlogCollection; use lloc\Msls\MslsOptions; +use lloc\Msls\MslsOptionsTax; use lloc\Msls\MslsPostTag; use lloc\Msls\MslsTaxonomy; @@ -15,7 +16,7 @@ class TestMslsPostTag extends MslsUnitTestCase { protected function setUp(): void { parent::setUp(); - Functions\when( 'get_option' )->justReturn( array() ); + Functions\when( 'get_option' )->justReturn( array( 'de_DE' => 42 ) ); Functions\expect( 'is_admin' )->andReturn( true ); Functions\expect( 'get_post_types' )->andReturn( array( 'post', 'page' ) ); @@ -33,7 +34,11 @@ protected function setUp(): void { $collection->shouldReceive( 'get' )->andReturn( $blogs ); $collection->shouldReceive( 'has_current_blog' )->andReturnTrue(); $collection->shouldReceive( 'get_current_blog' )->andReturn( $blogs[0] ); - + $collection->shouldReceive( 'get_blog_id' )->andReturnUsing( + function ( $language ) { + return $language === 'de_DE' ? 1 : null; + } + ); $this->test = new MslsPostTag( $options, $collection ); } @@ -85,6 +90,8 @@ public function test_edit_input(): void { $taxonomy->shouldReceive( 'get_request' )->atLeast()->once()->andReturn( 'post' ); $taxonomy->shouldReceive( 'acl_request' )->atLeast()->once()->andReturn( array( 'taxonomy', 'post_tag' ) ); + $term = \Mockery::mock( \WP_Term::class ); + Functions\expect( 'msls_content_types' )->atLeast()->once()->andReturn( $taxonomy ); Functions\expect( 'get_queried_object_id' )->atLeast()->once()->andReturn( 42 ); Functions\expect( 'is_woocommerce' )->atLeast()->once()->andReturn( false ); @@ -93,6 +100,8 @@ public function test_edit_input(): void { Functions\expect( 'add_query_arg' )->atLeast()->once()->andReturn( 'added_query_arg' ); Functions\expect( 'get_current_blog_id' )->atLeast()->once()->andReturn( 23 ); Functions\expect( 'get_admin_url' )->atLeast()->once()->andReturn( '/wp-admin/edit-tags.php' ); + Functions\expect( 'get_term' )->atLeast()->once()->andReturn( $term ); + Functions\expect( 'get_edit_term_link' )->atLeast()->once()->andReturn( 'edit_term_link' ); $output = ' @@ -102,10 +111,10 @@ public function test_edit_input(): void { - + - + @@ -119,13 +128,10 @@ public function test_edit_input(): void { '; $this->expectOutputString( $output ); - - $tag = \Mockery::mock( \WP_Term::class ); - - $this->test->edit_input( $tag, 'test' ); + $this->test->edit_input( $term, 'test' ); // second call should not output anything - $this->test->edit_input( $tag, 'test' ); + $this->test->edit_input( $term, 'test' ); } public function test_add_input(): void { @@ -142,6 +148,7 @@ public function test_add_input(): void { Functions\expect( 'add_query_arg' )->atLeast()->once()->andReturn( 'added_query_arg' ); Functions\expect( 'get_current_blog_id' )->atLeast()->once()->andReturn( 23 ); Functions\expect( 'get_admin_url' )->atLeast()->once()->andReturn( '/wp-admin/edit-tags.php' ); + Functions\expect( 'get_term' )->atLeast()->once()->andReturnNull(); $output = '

Multisite Language Switcher

@@ -178,4 +185,64 @@ public function test_set() { $this->expectNotToPerformAssertions(); $this->test->set( 42 ); } + + public function test_maybe_set_linked_term_origin_lang(): void { + $mydata = \Mockery::mock( MslsOptionsTax::class ); + $mydata->de_DE = 42; + + Functions\expect( 'filter_has_var' )->twice()->andReturnTrue(); + Functions\expect( 'filter_input' )->once()->andReturn( 'de_DE' ); + + $result = $this->test->maybe_set_linked_term( $mydata ); + + $this->assertSame( $mydata, $result ); + } + + public function test_maybe_set_linked_term_blog_id_null(): void { + $mydata = \Mockery::mock( MslsOptionsTax::class ); + $mydata->de_DE = 42; + + Functions\expect( 'filter_has_var' )->twice()->andReturnTrue(); + Functions\expect( 'filter_input' )->twice()->andReturn( 'fr_FR', 13 ); + + $result = $this->test->maybe_set_linked_term( $mydata ); + + $this->assertSame( $mydata, $result ); + } + + public function test_maybe_set_linked_term_origin_term_wrong(): void { + $mydata = \Mockery::mock( MslsOptionsTax::class ); + $mydata->en_US = 42; + + Functions\expect( 'filter_has_var' )->twice()->andReturnTrue(); + Functions\expect( 'filter_input' )->twice()->andReturn( 'de_DE', 13 ); + Functions\expect( 'switch_to_blog' )->once(); + Functions\expect( 'get_term' )->once()->andReturn( (object) array() ); + Functions\expect( 'restore_current_blog' )->once(); + + $result = $this->test->maybe_set_linked_term( $mydata ); + + $this->assertSame( $mydata, $result ); + } + + public function test_maybe_set_linked_term_happy_path(): void { + $mydata = \Mockery::mock( MslsOptionsTax::class ); + $mydata->en_US = 42; + + $term = \Mockery::mock( \WP_Term::class ); + $term->en_US = 42; + + Functions\expect( 'filter_has_var' )->twice()->andReturnTrue(); + Functions\expect( 'filter_input' )->twice()->andReturn( 'de_DE', 13 ); + Functions\expect( 'switch_to_blog' )->once(); + Functions\expect( 'get_term' )->once()->andReturn( $term ); + Functions\expect( 'restore_current_blog' )->once(); + + $result = $this->test->maybe_set_linked_term( $mydata ); + + $expected = array( $term->en_US, 13 ); + $actual = array( $result->en_US, $result->de_DE ); + + $this->assertEquals( $expected, $actual ); + } }