Skip to content

Commit 3808348

Browse files
committed
PostTag coverage completed
1 parent 0e92515 commit 3808348

File tree

1 file changed

+76
-9
lines changed

1 file changed

+76
-9
lines changed

tests/phpunit/TestMslsPostTag.php

Lines changed: 76 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use lloc\Msls\MslsBlog;
88
use lloc\Msls\MslsBlogCollection;
99
use lloc\Msls\MslsOptions;
10+
use lloc\Msls\MslsOptionsTax;
1011
use lloc\Msls\MslsPostTag;
1112
use lloc\Msls\MslsTaxonomy;
1213

@@ -15,7 +16,7 @@ class TestMslsPostTag extends MslsUnitTestCase {
1516
protected function setUp(): void {
1617
parent::setUp();
1718

18-
Functions\when( 'get_option' )->justReturn( array() );
19+
Functions\when( 'get_option' )->justReturn( array( 'de_DE' => 42 ) );
1920
Functions\expect( 'is_admin' )->andReturn( true );
2021
Functions\expect( 'get_post_types' )->andReturn( array( 'post', 'page' ) );
2122

@@ -33,7 +34,11 @@ protected function setUp(): void {
3334
$collection->shouldReceive( 'get' )->andReturn( $blogs );
3435
$collection->shouldReceive( 'has_current_blog' )->andReturnTrue();
3536
$collection->shouldReceive( 'get_current_blog' )->andReturn( $blogs[0] );
36-
37+
$collection->shouldReceive( 'get_blog_id' )->andReturnUsing(
38+
function ( $language ) {
39+
return $language === 'de_DE' ? 1 : null;
40+
}
41+
);
3742
$this->test = new MslsPostTag( $options, $collection );
3843
}
3944

@@ -85,6 +90,8 @@ public function test_edit_input(): void {
8590
$taxonomy->shouldReceive( 'get_request' )->atLeast()->once()->andReturn( 'post' );
8691
$taxonomy->shouldReceive( 'acl_request' )->atLeast()->once()->andReturn( array( 'taxonomy', 'post_tag' ) );
8792

93+
$term = \Mockery::mock( \WP_Term::class );
94+
8895
Functions\expect( 'msls_content_types' )->atLeast()->once()->andReturn( $taxonomy );
8996
Functions\expect( 'get_queried_object_id' )->atLeast()->once()->andReturn( 42 );
9097
Functions\expect( 'is_woocommerce' )->atLeast()->once()->andReturn( false );
@@ -93,6 +100,8 @@ public function test_edit_input(): void {
93100
Functions\expect( 'add_query_arg' )->atLeast()->once()->andReturn( 'added_query_arg' );
94101
Functions\expect( 'get_current_blog_id' )->atLeast()->once()->andReturn( 23 );
95102
Functions\expect( 'get_admin_url' )->atLeast()->once()->andReturn( '/wp-admin/edit-tags.php' );
103+
Functions\expect( 'get_term' )->atLeast()->once()->andReturn( $term );
104+
Functions\expect( 'get_edit_term_link' )->atLeast()->once()->andReturn( 'edit_term_link' );
96105

97106
$output = '<tr>
98107
<th colspan="2">
@@ -102,10 +111,10 @@ public function test_edit_input(): void {
102111
</th>
103112
</tr><tr class="form-field">
104113
<th scope="row">
105-
<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>
114+
<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>
106115
</th>
107116
<td>
108-
<input type="hidden" id="msls_id_" name="msls_input_de_DE" value=""/>
117+
<input type="hidden" id="msls_id_" name="msls_input_de_DE" value="42"/>
109118
<input class="msls_title" id="msls_title_" name="msls_title_" type="text" value=""/>
110119
</td>
111120
</tr><tr class="form-field">
@@ -119,13 +128,10 @@ public function test_edit_input(): void {
119128
</tr>';
120129

121130
$this->expectOutputString( $output );
122-
123-
$tag = \Mockery::mock( \WP_Term::class );
124-
125-
$this->test->edit_input( $tag, 'test' );
131+
$this->test->edit_input( $term, 'test' );
126132

127133
// second call should not output anything
128-
$this->test->edit_input( $tag, 'test' );
134+
$this->test->edit_input( $term, 'test' );
129135
}
130136

131137
public function test_add_input(): void {
@@ -142,6 +148,7 @@ public function test_add_input(): void {
142148
Functions\expect( 'add_query_arg' )->atLeast()->once()->andReturn( 'added_query_arg' );
143149
Functions\expect( 'get_current_blog_id' )->atLeast()->once()->andReturn( 23 );
144150
Functions\expect( 'get_admin_url' )->atLeast()->once()->andReturn( '/wp-admin/edit-tags.php' );
151+
Functions\expect( 'get_term' )->atLeast()->once()->andReturnNull();
145152

146153
$output = '<div class="form-field"><h3>Multisite Language Switcher</h3>
147154
<input type="hidden" name="msls_post_type" id="msls_post_type" value="post"/>
@@ -178,4 +185,64 @@ public function test_set() {
178185
$this->expectNotToPerformAssertions();
179186
$this->test->set( 42 );
180187
}
188+
189+
public function test_maybe_set_linked_term_origin_lang(): void {
190+
$mydata = \Mockery::mock( MslsOptionsTax::class );
191+
$mydata->de_DE = 42;
192+
193+
Functions\expect( 'filter_has_var' )->twice()->andReturnTrue();
194+
Functions\expect( 'filter_input' )->once()->andReturn( 'de_DE' );
195+
196+
$result = $this->test->maybe_set_linked_term( $mydata );
197+
198+
$this->assertSame( $mydata, $result );
199+
}
200+
201+
public function test_maybe_set_linked_term_blog_id_null(): void {
202+
$mydata = \Mockery::mock( MslsOptionsTax::class );
203+
$mydata->de_DE = 42;
204+
205+
Functions\expect( 'filter_has_var' )->twice()->andReturnTrue();
206+
Functions\expect( 'filter_input' )->twice()->andReturn( 'fr_FR', 13 );
207+
208+
$result = $this->test->maybe_set_linked_term( $mydata );
209+
210+
$this->assertSame( $mydata, $result );
211+
}
212+
213+
public function test_maybe_set_linked_term_origin_term_wrong(): void {
214+
$mydata = \Mockery::mock( MslsOptionsTax::class );
215+
$mydata->en_US = 42;
216+
217+
Functions\expect( 'filter_has_var' )->twice()->andReturnTrue();
218+
Functions\expect( 'filter_input' )->twice()->andReturn( 'de_DE', 13 );
219+
Functions\expect( 'switch_to_blog' )->once();
220+
Functions\expect( 'get_term' )->once()->andReturn( (object) array() );
221+
Functions\expect( 'restore_current_blog' )->once();
222+
223+
$result = $this->test->maybe_set_linked_term( $mydata );
224+
225+
$this->assertSame( $mydata, $result );
226+
}
227+
228+
public function test_maybe_set_linked_term_happy_path(): void {
229+
$mydata = \Mockery::mock( MslsOptionsTax::class );
230+
$mydata->en_US = 42;
231+
232+
$term = \Mockery::mock( \WP_Term::class );
233+
$term->en_US = 42;
234+
235+
Functions\expect( 'filter_has_var' )->twice()->andReturnTrue();
236+
Functions\expect( 'filter_input' )->twice()->andReturn( 'de_DE', 13 );
237+
Functions\expect( 'switch_to_blog' )->once();
238+
Functions\expect( 'get_term' )->once()->andReturn( $term );
239+
Functions\expect( 'restore_current_blog' )->once();
240+
241+
$result = $this->test->maybe_set_linked_term( $mydata );
242+
243+
$expected = array( $term->en_US, 13 );
244+
$actual = array( $result->en_US, $result->de_DE );
245+
246+
$this->assertEquals( $expected, $actual );
247+
}
181248
}

0 commit comments

Comments
 (0)