Skip to content

Commit

Permalink
Merge pull request #378 from lloc/raise-coverage
Browse files Browse the repository at this point in the history
Raise coverage
  • Loading branch information
lloc authored Sep 19, 2024
2 parents f3684d7 + 3808348 commit 4c5b37f
Showing 1 changed file with 91 additions and 9 deletions.
100 changes: 91 additions & 9 deletions tests/phpunit/TestMslsPostTag.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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' ) );

Expand All @@ -31,7 +32,13 @@ 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] );
$collection->shouldReceive( 'get_blog_id' )->andReturnUsing(
function ( $language ) {
return $language === 'de_DE' ? 1 : null;
}
);
$this->test = new MslsPostTag( $options, $collection );
}

Expand Down Expand Up @@ -83,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 );
Expand All @@ -91,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 = '<tr>
<th colspan="2">
Expand All @@ -100,10 +111,10 @@ public function test_edit_input(): void {
</th>
</tr><tr class="form-field">
<th scope="row">
<label for="msls_title_"><a title="Create a new translation in the de_DE-blog" href="/wp-admin/edit-tags.php"><span class="language-badge de_DE"><span>de</span><span>DE</span></span></a>&nbsp;</label>
<label for="msls_title_"><a title="Edit the translation in the de_DE-blog" href="edit_term_link"><span class="language-badge de_DE"><span>de</span><span>DE</span></span></a>&nbsp;</label>
</th>
<td>
<input type="hidden" id="msls_id_" name="msls_input_de_DE" value=""/>
<input type="hidden" id="msls_id_" name="msls_input_de_DE" value="42"/>
<input class="msls_title" id="msls_title_" name="msls_title_" type="text" value=""/>
</td>
</tr><tr class="form-field">
Expand All @@ -117,13 +128,10 @@ public function test_edit_input(): void {
</tr>';

$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 {
Expand All @@ -140,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 = '<div class="form-field"><h3>Multisite Language Switcher</h3>
<input type="hidden" name="msls_post_type" id="msls_post_type" value="post"/>
Expand All @@ -163,4 +172,77 @@ 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 );
}

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 );
}
}

0 comments on commit 4c5b37f

Please sign in to comment.