Skip to content

Commit 4c5b37f

Browse files
authored
Merge pull request #378 from lloc/raise-coverage
Raise coverage
2 parents f3684d7 + 3808348 commit 4c5b37f

File tree

1 file changed

+91
-9
lines changed

1 file changed

+91
-9
lines changed

tests/phpunit/TestMslsPostTag.php

Lines changed: 91 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

@@ -31,7 +32,13 @@ protected function setUp(): void {
3132

3233
$collection = \Mockery::mock( MslsBlogCollection::class );
3334
$collection->shouldReceive( 'get' )->andReturn( $blogs );
34-
35+
$collection->shouldReceive( 'has_current_blog' )->andReturnTrue();
36+
$collection->shouldReceive( 'get_current_blog' )->andReturn( $blogs[0] );
37+
$collection->shouldReceive( 'get_blog_id' )->andReturnUsing(
38+
function ( $language ) {
39+
return $language === 'de_DE' ? 1 : null;
40+
}
41+
);
3542
$this->test = new MslsPostTag( $options, $collection );
3643
}
3744

@@ -83,6 +90,8 @@ public function test_edit_input(): void {
8390
$taxonomy->shouldReceive( 'get_request' )->atLeast()->once()->andReturn( 'post' );
8491
$taxonomy->shouldReceive( 'acl_request' )->atLeast()->once()->andReturn( array( 'taxonomy', 'post_tag' ) );
8592

93+
$term = \Mockery::mock( \WP_Term::class );
94+
8695
Functions\expect( 'msls_content_types' )->atLeast()->once()->andReturn( $taxonomy );
8796
Functions\expect( 'get_queried_object_id' )->atLeast()->once()->andReturn( 42 );
8897
Functions\expect( 'is_woocommerce' )->atLeast()->once()->andReturn( false );
@@ -91,6 +100,8 @@ public function test_edit_input(): void {
91100
Functions\expect( 'add_query_arg' )->atLeast()->once()->andReturn( 'added_query_arg' );
92101
Functions\expect( 'get_current_blog_id' )->atLeast()->once()->andReturn( 23 );
93102
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' );
94105

95106
$output = '<tr>
96107
<th colspan="2">
@@ -100,10 +111,10 @@ public function test_edit_input(): void {
100111
</th>
101112
</tr><tr class="form-field">
102113
<th scope="row">
103-
<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>
104115
</th>
105116
<td>
106-
<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"/>
107118
<input class="msls_title" id="msls_title_" name="msls_title_" type="text" value=""/>
108119
</td>
109120
</tr><tr class="form-field">
@@ -117,13 +128,10 @@ public function test_edit_input(): void {
117128
</tr>';
118129

119130
$this->expectOutputString( $output );
120-
121-
$tag = \Mockery::mock( \WP_Term::class );
122-
123-
$this->test->edit_input( $tag, 'test' );
131+
$this->test->edit_input( $term, 'test' );
124132

125133
// second call should not output anything
126-
$this->test->edit_input( $tag, 'test' );
134+
$this->test->edit_input( $term, 'test' );
127135
}
128136

129137
public function test_add_input(): void {
@@ -140,6 +148,7 @@ public function test_add_input(): void {
140148
Functions\expect( 'add_query_arg' )->atLeast()->once()->andReturn( 'added_query_arg' );
141149
Functions\expect( 'get_current_blog_id' )->atLeast()->once()->andReturn( 23 );
142150
Functions\expect( 'get_admin_url' )->atLeast()->once()->andReturn( '/wp-admin/edit-tags.php' );
151+
Functions\expect( 'get_term' )->atLeast()->once()->andReturnNull();
143152

144153
$output = '<div class="form-field"><h3>Multisite Language Switcher</h3>
145154
<input type="hidden" name="msls_post_type" id="msls_post_type" value="post"/>
@@ -163,4 +172,77 @@ public function test_the_input_no_blogs(): void {
163172

164173
$this->assertFalse( $value );
165174
}
175+
176+
public function test_set() {
177+
$taxonomy = \Mockery::mock( MslsTaxonomy::class );
178+
$taxonomy->shouldReceive( 'acl_request' )->once()->andReturn( 'post_tag' );
179+
180+
Functions\expect( 'msls_content_types' )->once()->andReturn( $taxonomy );
181+
Functions\expect( 'delete_option' )->twice();
182+
Functions\expect( 'switch_to_blog' )->twice();
183+
Functions\expect( 'restore_current_blog' )->twice();
184+
185+
$this->expectNotToPerformAssertions();
186+
$this->test->set( 42 );
187+
}
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+
}
166248
}

0 commit comments

Comments
 (0)