Skip to content

Commit 7a54a93

Browse files
authored
Merge pull request #373 from lloc/raise-coverage
MslsMetaBox fully tested
2 parents 8aee872 + 32dfd5f commit 7a54a93

13 files changed

+121
-46
lines changed

tests/phpunit/ContentImport/Importers/TestBaseImporter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@ public function testSetImportCoordinates(): void {
2828
$this->test->set_import_coordinates( $import_coordinates );
2929
}
3030

31-
public function testGetLogger() {
31+
public function testGetLogger(): void {
3232
$this->assertInstanceOf( ImportLogger::class, $this->test->get_logger() );
3333
}
34-
public function testGetRelations() {
34+
public function testGetRelations(): void {
3535
$this->assertInstanceOf( Relations::class, $this->test->get_relations() );
3636
}
3737

38-
public function testInfo() {
38+
public function testInfo(): void {
3939
$this->assertInstanceOf( \stdClass::class, $this->test->info() );
4040
}
4141
}

tests/phpunit/ContentImport/TestContentImporter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,23 @@ public function test_relations(): void {
3333
$this->assertInstanceOf( Relations::class, $this->test->get_relations() );
3434
}
3535

36-
public function test_handle_import() {
36+
public function test_handle_import(): void {
3737
$this->assertEquals( array(), $this->test->handle_import() );
3838
}
3939

40-
public function test_parse_sources_no_post() {
40+
public function test_parse_sources_no_post(): void {
4141
$this->assertFalse( $this->test->parse_sources() );
4242
}
4343

44-
public function test_handle_false() {
44+
public function test_handle_false(): void {
4545
$this->expectNotToPerformAssertions();
4646

4747
Actions\expectAdded( 'msls_main_save' )->once();
4848

4949
$this->test->handle( false );
5050
}
5151

52-
public function test_handle_true() {
52+
public function test_handle_true(): void {
5353
$this->expectNotToPerformAssertions();
5454

5555
Actions\expectRemoved( 'msls_main_save' )->once();

tests/phpunit/TestMslsContentFilter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function test_content_filter_empty( string $content, string $expected, bo
4747
$this->assertEquals( $expected, $test->content_filter( $content ) );
4848
}
4949

50-
public function test_content_filter_one_link() {
50+
public function test_content_filter_one_link(): void {
5151
$blog = \Mockery::mock( MslsBlog::class );
5252
$blog->shouldReceive( 'get_language' )->once()->andReturn( 'de_DE' );
5353
$blog->shouldReceive( 'get_description' )->once()->andReturn( 'Deutsch' );
@@ -95,7 +95,7 @@ public function test_content_filter_one_link() {
9595
$this->assertEquals( $expected, $test->content_filter( $content ) );
9696
}
9797

98-
public function test_content_filter_zero_links() {
98+
public function test_content_filter_zero_links(): void {
9999
$collection = \Mockery::mock( MslsBlogCollection::class );
100100
$collection->shouldReceive( 'get_filtered' )->once()->andReturn( array() );
101101

@@ -121,7 +121,7 @@ public function test_content_filter_zero_links() {
121121
$this->assertEquals( $expected, $test->content_filter( $content ) );
122122
}
123123

124-
public function test_content_filter_more_links() {
124+
public function test_content_filter_more_links(): void {
125125
$a = \Mockery::mock( MslsBlog::class );
126126
$a->shouldReceive( 'get_language' )->once()->andReturn( 'it_IT' );
127127
$a->shouldReceive( 'get_description' )->once()->andReturn( 'Italiano' );
@@ -187,7 +187,7 @@ public function test_content_filter_more_links() {
187187
$this->assertEquals( $expected, $test->content_filter( $content ) );
188188
}
189189

190-
public function test_content_filter_with_filter() {
190+
public function test_content_filter_with_filter(): void {
191191
$blog = \Mockery::mock( MslsBlog::class );
192192
$blog->shouldReceive( 'get_language' )->once()->andReturn( 'de_DE' );
193193
$blog->shouldReceive( 'get_description' )->once()->andReturn( 'Deutsch' );

tests/phpunit/TestMslsMetaBox.php

Lines changed: 89 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use lloc\Msls\MslsJson;
1212
use lloc\Msls\MslsMetaBox;
1313
use lloc\Msls\MslsOptions;
14+
use lloc\Msls\MslsOptionsPost;
1415
use lloc\Msls\MslsPostType;
1516

1617
class TestMslsMetaBox extends MslsUnitTestCase {
@@ -34,6 +35,7 @@ protected function setUp(): void {
3435
$collection->shouldReceive( 'get' )->andReturn( array( $blog ) );
3536
$collection->shouldReceive( 'has_current_blog' )->andReturnTrue();
3637
$collection->shouldReceive( 'get_current_blog' )->andReturn( $blog );
38+
$collection->shouldReceive( 'get_blog_id' )->andReturn( 1 );
3739

3840
$this->test = new MslsMetaBox( $options, $collection );
3941
}
@@ -104,7 +106,7 @@ public function test_render_option_not_selected(): void {
104106
$this->assertEquals( '<option value="1" >Test</option>', $this->test->render_option( 1, 2 ) );
105107
}
106108

107-
public function test_render_options() {
109+
public function test_render_options(): void {
108110
$post = \Mockery::mock( 'WP_Post' );
109111
$post->ID = 42;
110112

@@ -142,7 +144,7 @@ public function test_add( $post_type, $content_import, $autocomplete ) {
142144
$this->test->add();
143145
}
144146

145-
public function test_render_select_not_hierarchical() {
147+
public function test_render_select_not_hierarchical(): void {
146148
global $post;
147149

148150
$post = \Mockery::mock( 'WP_Post' );
@@ -174,7 +176,7 @@ public function test_render_select_not_hierarchical() {
174176
$this->test->render_select();
175177
}
176178

177-
public function test_render_select_hierarchical() {
179+
public function test_render_select_hierarchical(): void {
178180
global $post;
179181

180182
$post = \Mockery::mock( 'WP_Post' );
@@ -241,7 +243,7 @@ public function test_render_input( $option, $the_title_times, $current_blog_id_t
241243
$this->test->render_input();
242244
}
243245

244-
public function test_render_select_only_one_blog() {
246+
public function test_render_select_only_one_blog(): void {
245247
$options = \Mockery::mock( MslsOptions::class );
246248

247249
$collection = \Mockery::mock( MslsBlogCollection::class );
@@ -255,7 +257,7 @@ public function test_render_select_only_one_blog() {
255257
$this->test->render_select();
256258
}
257259

258-
public function test_render_input_only_one_blog() {
260+
public function test_render_input_only_one_blog(): void {
259261
$options = \Mockery::mock( MslsOptions::class );
260262

261263
$collection = \Mockery::mock( MslsBlogCollection::class );
@@ -269,34 +271,107 @@ public function test_render_input_only_one_blog() {
269271
$this->test->render_input();
270272
}
271273

272-
public function test_set_no_request() {
274+
public function test_set_no_request(): void {
273275
Functions\expect( 'wp_is_post_revision' )->once()->andReturn( false );
274276

275277
$this->expectNotToPerformAssertions();
276278
$this->test->set( 13 );
277279
}
278280

279-
public function test_set_with_request() {
281+
public function test_set_with_request_current_user_cannot(): void {
280282
Functions\expect( 'wp_is_post_revision' )->once()->andReturn( false );
281283
Functions\expect( 'filter_has_var' )->once()->with( INPUT_POST, MslsFields::FIELD_MSLS_NONCENAME )->andReturnTrue();
282284
Functions\expect( 'wp_verify_nonce' )->once()->andReturnTrue();
283-
Functions\expect( 'current_user_can' )->once()->andReturnTrue();
284-
Functions\expect( 'get_option' )->atLeast()->once()->andReturn( array() );
285-
Functions\expect( 'delete_option' )->atLeast()->once();
286-
Functions\expect( 'switch_to_blog' )->once();
287-
Functions\expect( 'restore_current_blog' )->once();
285+
Functions\expect( 'current_user_can' )->once()->andReturnFalse();
288286

289287
$this->expectNotToPerformAssertions();
290288
$this->test->set( 13 );
291289
}
292290

293-
public function test_set_with_request_current_user_cannot() {
291+
public function test_set_with_request(): void {
294292
Functions\expect( 'wp_is_post_revision' )->once()->andReturn( false );
295293
Functions\expect( 'filter_has_var' )->once()->with( INPUT_POST, MslsFields::FIELD_MSLS_NONCENAME )->andReturnTrue();
296294
Functions\expect( 'wp_verify_nonce' )->once()->andReturnTrue();
297-
Functions\expect( 'current_user_can' )->once()->andReturnFalse();
295+
Functions\expect( 'current_user_can' )->once()->andReturnTrue();
296+
Functions\expect( 'get_option' )->atLeast()->once()->andReturn( array() );
297+
Functions\expect( 'delete_option' )->atLeast()->once();
298+
Functions\expect( 'switch_to_blog' )->once();
299+
Functions\expect( 'restore_current_blog' )->once();
298300

299301
$this->expectNotToPerformAssertions();
300302
$this->test->set( 13 );
301303
}
304+
305+
public function test_maybe_set_linked_post() {
306+
$post = \Mockery::mock( 'WP_Post' );
307+
308+
Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_ID )->andReturnTrue();
309+
Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_LANG )->andReturnTrue();
310+
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_LANG, FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn( 'de_DE' );
311+
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_ID, FILTER_SANITIZE_NUMBER_INT )->andReturn( 42 );
312+
Functions\expect( 'get_post' )->once()->andReturn( $post );
313+
Functions\expect( 'restore_current_blog' )->once();
314+
Functions\expect( 'switch_to_blog' )->once();
315+
Functions\expect( 'get_option' )->once()->andReturn( array() );
316+
317+
$mydata = new MslsOptionsPost();
318+
$mydata = $this->test->maybe_set_linked_post( $mydata );
319+
320+
$this->assertEquals( 42, $mydata->de_DE );
321+
}
322+
323+
public function test_maybe_set_linked_post_with_no_post() {
324+
Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_ID )->andReturnTrue();
325+
Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_LANG )->andReturnTrue();
326+
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_LANG, FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn( 'de_DE' );
327+
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_ID, FILTER_SANITIZE_NUMBER_INT )->andReturn( 42 );
328+
Functions\expect( 'get_post' )->once()->andReturn( null );
329+
Functions\expect( 'restore_current_blog' )->once();
330+
Functions\expect( 'switch_to_blog' )->once();
331+
Functions\expect( 'get_option' )->once()->andReturn( array() );
332+
333+
$mydata = new MslsOptionsPost();
334+
$mydata = $this->test->maybe_set_linked_post( $mydata );
335+
336+
$this->assertNull( $mydata->de_DE );
337+
}
338+
339+
function test_maybe_set_linked_post_with_no_blog_id() {
340+
$options = \Mockery::mock( MslsOptions::class );
341+
342+
$collection = \Mockery::mock( MslsBlogCollection::class );
343+
$collection->shouldReceive( 'get_blog_id' )->andReturn( null );
344+
345+
$this->test = new MslsMetaBox( $options, $collection );
346+
347+
Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_ID )->andReturnTrue();
348+
Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_LANG )->andReturnTrue();
349+
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_LANG, FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn( 'de_DE' );
350+
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_ID, FILTER_SANITIZE_NUMBER_INT )->andReturn( 42 );
351+
Functions\expect( 'get_option' )->once()->andReturn( array() );
352+
353+
$mydata = new MslsOptionsPost();
354+
$mydata = $this->test->maybe_set_linked_post( $mydata );
355+
356+
$this->assertNull( $mydata->de_DE );
357+
}
358+
359+
function test_maybe_set_linked_post_with_mydata_already_set() {
360+
$options = \Mockery::mock( MslsOptions::class );
361+
362+
$collection = \Mockery::mock( MslsBlogCollection::class );
363+
364+
$this->test = new MslsMetaBox( $options, $collection );
365+
366+
Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_ID )->andReturnTrue();
367+
Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_LANG )->andReturnTrue();
368+
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_LANG, FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn( 'de_DE' );
369+
Functions\expect( 'get_option' )->once()->andReturn( array() );
370+
371+
$mydata = new MslsOptionsPost();
372+
$mydata->de_DE = 42;
373+
$mydata = $this->test->maybe_set_linked_post( $mydata );
374+
375+
$this->assertEquals( 42, $mydata->de_DE );
376+
}
302377
}

tests/phpunit/TestMslsOptions.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,13 @@ function ( $url = '' ) {
232232
$this->assertEquals( $expected, MslsOptions::check_for_blog_slug( $url, $options ) );
233233
}
234234

235-
public function test_get_slug() {
235+
public function test_get_slug(): void {
236236
$obj = $this->get_test();
237237

238238
$this->assertEquals( '', $obj->get_slug( 'post' ) );
239239
}
240240

241-
public function test_get_option_name() {
241+
public function test_get_option_name(): void {
242242
$this->assertSame( 'msls', $this->get_test()->get_option_name() );
243243
}
244244
}

tests/phpunit/TestMslsOptionsPost.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ protected function setUp(): void {
1616
$this->test = new MslsOptionsPost( 42 );
1717
}
1818

19-
public function test_get_postlink_not_has_value() {
19+
public function test_get_postlink_not_has_value(): void {
2020
$this->assertEquals( '', $this->test->get_postlink( 'es_ES' ) );
2121
}
2222

@@ -55,7 +55,7 @@ public function test_get_current_link(): void {
5555
$this->assertEquals( 'https://msls.co/a-post', $this->test->get_current_link() );
5656
}
5757

58-
public function test_get_option_name() {
58+
public function test_get_option_name(): void {
5959
$this->assertSame( 'msls_42', $this->test->get_option_name() );
6060
}
6161
}

tests/phpunit/TestMslsOptionsQuery.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ protected function setUp(): void {
2121
$wpdb = \Mockery::mock( '\wpdb' );
2222
}
2323

24-
public function test_get_params() {
24+
public function test_get_params(): void {
2525
$this->assertEquals( array(), MslsOptionsQuery::get_params() );
2626
}
2727

tests/phpunit/TestMslsOptionsTaxTerm.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function test_check_url_permastruct_false(): void {
5252
$this->assertEquals( $expected, $this->test->check_base( $expected, $options ) );
5353
}
5454

55-
public function test_get_option_name() {
55+
public function test_get_option_name(): void {
5656
$this->assertSame( 'msls_term_42', $this->test->get_option_name() );
5757
}
5858
}

tests/phpunit/TestMslsOutput.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function test_get_method(): void {
3030
$this->assertEquals( array(), $this->test->get( 0 ) );
3131
}
3232

33-
public function test_get_alternate_links_two_url() {
33+
public function test_get_alternate_links_two_url(): void {
3434
$blogs = array();
3535

3636
$a = \Mockery::mock( MslsBlog::class );
@@ -74,7 +74,7 @@ public function test_get_alternate_links_two_url() {
7474
$this->assertEquals( $expected, $this->test->get_alternate_links() );
7575
}
7676

77-
public function test_get_alternate_links_null_url() {
77+
public function test_get_alternate_links_null_url(): void {
7878
$blogs = array();
7979

8080
$a = \Mockery::mock( MslsBlog::class );
@@ -104,7 +104,7 @@ public function test_get_alternate_links_null_url() {
104104
$this->assertEquals( '', $this->test->get_alternate_links() );
105105
}
106106

107-
public function test_get_alternate_links_one_url() {
107+
public function test_get_alternate_links_one_url(): void {
108108
$blogs = array();
109109

110110
$a = \Mockery::mock( MslsBlog::class );
@@ -137,14 +137,14 @@ public function test_get_alternate_links_one_url() {
137137
$this->assertEquals( '<link rel="alternate" hreflang="x-default" href="https://example.de/" title="Deutsch" />', $this->test->get_alternate_links() );
138138
}
139139

140-
public function test___toString_no_translation() {
140+
public function test___toString_no_translation(): void {
141141
$expected = '<a href="https://example.com" title="Example">Example</a>';
142142

143143
Filters\expectApplied( 'msls_output_no_translation_found' )->once()->andReturn( $expected );
144144
$this->assertEquals( $expected, strval( $this->test ) );
145145
}
146146

147-
public function test___toString_output() {
147+
public function test___toString_output(): void {
148148
$blog = \Mockery::mock( MslsBlog::class );
149149
$blog->shouldReceive( 'get_language' )->andReturn( 'de_DE' );
150150
$blog->shouldReceive( 'get_description' )->andReturn( 'Deutsch' );
@@ -177,7 +177,7 @@ public function test___toString_output() {
177177
$this->assertEquals( '<a href="/" title="Deutsch"><img src="https://msls.co/wp-content/plugins/msls/flags/de.png" alt="de_DE"/> Deutsch</a>', strval( $test ) );
178178
}
179179

180-
public function test___toString_current_blog() {
180+
public function test___toString_current_blog(): void {
181181
$blog = \Mockery::mock( MslsBlog::class );
182182
$blog->shouldReceive( 'get_language' )->andReturn( 'de_DE' );
183183
$blog->shouldReceive( 'get_description' )->andReturn( 'Deutsch' );
@@ -208,7 +208,7 @@ public function test___toString_current_blog() {
208208
$this->assertEquals( $expected, strval( new MslsOutput( $options, $collection ) ) );
209209
}
210210

211-
public function test___toString_filter() {
211+
public function test___toString_filter(): void {
212212
$blog = \Mockery::mock( MslsBlog::class );
213213
$blog->shouldReceive( 'get_language' )->andReturn( 'de_DE' );
214214
$blog->shouldReceive( 'get_description' )->andReturn( 'Deutsch' );
@@ -241,7 +241,7 @@ public function test___toString_filter() {
241241
$this->assertEquals( $expected, strval( new MslsOutput( $options, $collection ) ) );
242242
}
243243

244-
public function test_get_not_fulfilled() {
244+
public function test_get_not_fulfilled(): void {
245245
$blog = \Mockery::mock( MslsBlog::class );
246246
$blog->shouldReceive( 'get_language' )->once()->andReturn( 'de_DE' );
247247
// $blog->shouldReceive( 'get_description' )->once()->andReturn( 'Deutsch' );

tests/phpunit/TestMslsPlugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ function () {
133133
$this->assertEquals( $expected, MslsPlugin::plugin_dir_path( 'dist/msls-widget-block' ) );
134134
}
135135

136-
public function test_print_alternate_links() {
136+
public function test_print_alternate_links(): void {
137137
Functions\expect( 'is_admin' )->once()->andReturn( false );
138138
Functions\expect( 'is_front_page' )->once()->andReturn( true );
139139
Functions\expect( 'get_option' )->once()->andReturn( array() );

0 commit comments

Comments
 (0)