Skip to content

Commit

Permalink
Merge pull request #373 from lloc/raise-coverage
Browse files Browse the repository at this point in the history
MslsMetaBox fully tested
  • Loading branch information
lloc authored Sep 8, 2024
2 parents 8aee872 + 32dfd5f commit 7a54a93
Show file tree
Hide file tree
Showing 13 changed files with 121 additions and 46 deletions.
6 changes: 3 additions & 3 deletions tests/phpunit/ContentImport/Importers/TestBaseImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ public function testSetImportCoordinates(): void {
$this->test->set_import_coordinates( $import_coordinates );
}

public function testGetLogger() {
public function testGetLogger(): void {
$this->assertInstanceOf( ImportLogger::class, $this->test->get_logger() );
}
public function testGetRelations() {
public function testGetRelations(): void {
$this->assertInstanceOf( Relations::class, $this->test->get_relations() );
}

public function testInfo() {
public function testInfo(): void {
$this->assertInstanceOf( \stdClass::class, $this->test->info() );
}
}
8 changes: 4 additions & 4 deletions tests/phpunit/ContentImport/TestContentImporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,23 +33,23 @@ public function test_relations(): void {
$this->assertInstanceOf( Relations::class, $this->test->get_relations() );
}

public function test_handle_import() {
public function test_handle_import(): void {
$this->assertEquals( array(), $this->test->handle_import() );
}

public function test_parse_sources_no_post() {
public function test_parse_sources_no_post(): void {
$this->assertFalse( $this->test->parse_sources() );
}

public function test_handle_false() {
public function test_handle_false(): void {
$this->expectNotToPerformAssertions();

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

$this->test->handle( false );
}

public function test_handle_true() {
public function test_handle_true(): void {
$this->expectNotToPerformAssertions();

Actions\expectRemoved( 'msls_main_save' )->once();
Expand Down
8 changes: 4 additions & 4 deletions tests/phpunit/TestMslsContentFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function test_content_filter_empty( string $content, string $expected, bo
$this->assertEquals( $expected, $test->content_filter( $content ) );
}

public function test_content_filter_one_link() {
public function test_content_filter_one_link(): void {
$blog = \Mockery::mock( MslsBlog::class );
$blog->shouldReceive( 'get_language' )->once()->andReturn( 'de_DE' );
$blog->shouldReceive( 'get_description' )->once()->andReturn( 'Deutsch' );
Expand Down Expand Up @@ -95,7 +95,7 @@ public function test_content_filter_one_link() {
$this->assertEquals( $expected, $test->content_filter( $content ) );
}

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

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

public function test_content_filter_more_links() {
public function test_content_filter_more_links(): void {
$a = \Mockery::mock( MslsBlog::class );
$a->shouldReceive( 'get_language' )->once()->andReturn( 'it_IT' );
$a->shouldReceive( 'get_description' )->once()->andReturn( 'Italiano' );
Expand Down Expand Up @@ -187,7 +187,7 @@ public function test_content_filter_more_links() {
$this->assertEquals( $expected, $test->content_filter( $content ) );
}

public function test_content_filter_with_filter() {
public function test_content_filter_with_filter(): void {
$blog = \Mockery::mock( MslsBlog::class );
$blog->shouldReceive( 'get_language' )->once()->andReturn( 'de_DE' );
$blog->shouldReceive( 'get_description' )->once()->andReturn( 'Deutsch' );
Expand Down
103 changes: 89 additions & 14 deletions tests/phpunit/TestMslsMetaBox.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use lloc\Msls\MslsJson;
use lloc\Msls\MslsMetaBox;
use lloc\Msls\MslsOptions;
use lloc\Msls\MslsOptionsPost;
use lloc\Msls\MslsPostType;

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

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

public function test_render_options() {
public function test_render_options(): void {
$post = \Mockery::mock( 'WP_Post' );
$post->ID = 42;

Expand Down Expand Up @@ -142,7 +144,7 @@ public function test_add( $post_type, $content_import, $autocomplete ) {
$this->test->add();
}

public function test_render_select_not_hierarchical() {
public function test_render_select_not_hierarchical(): void {
global $post;

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

public function test_render_select_hierarchical() {
public function test_render_select_hierarchical(): void {
global $post;

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

public function test_render_select_only_one_blog() {
public function test_render_select_only_one_blog(): void {
$options = \Mockery::mock( MslsOptions::class );

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

public function test_render_input_only_one_blog() {
public function test_render_input_only_one_blog(): void {
$options = \Mockery::mock( MslsOptions::class );

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

public function test_set_no_request() {
public function test_set_no_request(): void {
Functions\expect( 'wp_is_post_revision' )->once()->andReturn( false );

$this->expectNotToPerformAssertions();
$this->test->set( 13 );
}

public function test_set_with_request() {
public function test_set_with_request_current_user_cannot(): void {
Functions\expect( 'wp_is_post_revision' )->once()->andReturn( false );
Functions\expect( 'filter_has_var' )->once()->with( INPUT_POST, MslsFields::FIELD_MSLS_NONCENAME )->andReturnTrue();
Functions\expect( 'wp_verify_nonce' )->once()->andReturnTrue();
Functions\expect( 'current_user_can' )->once()->andReturnTrue();
Functions\expect( 'get_option' )->atLeast()->once()->andReturn( array() );
Functions\expect( 'delete_option' )->atLeast()->once();
Functions\expect( 'switch_to_blog' )->once();
Functions\expect( 'restore_current_blog' )->once();
Functions\expect( 'current_user_can' )->once()->andReturnFalse();

$this->expectNotToPerformAssertions();
$this->test->set( 13 );
}

public function test_set_with_request_current_user_cannot() {
public function test_set_with_request(): void {
Functions\expect( 'wp_is_post_revision' )->once()->andReturn( false );
Functions\expect( 'filter_has_var' )->once()->with( INPUT_POST, MslsFields::FIELD_MSLS_NONCENAME )->andReturnTrue();
Functions\expect( 'wp_verify_nonce' )->once()->andReturnTrue();
Functions\expect( 'current_user_can' )->once()->andReturnFalse();
Functions\expect( 'current_user_can' )->once()->andReturnTrue();
Functions\expect( 'get_option' )->atLeast()->once()->andReturn( array() );
Functions\expect( 'delete_option' )->atLeast()->once();
Functions\expect( 'switch_to_blog' )->once();
Functions\expect( 'restore_current_blog' )->once();

$this->expectNotToPerformAssertions();
$this->test->set( 13 );
}

public function test_maybe_set_linked_post() {
$post = \Mockery::mock( 'WP_Post' );

Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_ID )->andReturnTrue();
Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_LANG )->andReturnTrue();
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_LANG, FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn( 'de_DE' );
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_ID, FILTER_SANITIZE_NUMBER_INT )->andReturn( 42 );
Functions\expect( 'get_post' )->once()->andReturn( $post );
Functions\expect( 'restore_current_blog' )->once();
Functions\expect( 'switch_to_blog' )->once();
Functions\expect( 'get_option' )->once()->andReturn( array() );

$mydata = new MslsOptionsPost();
$mydata = $this->test->maybe_set_linked_post( $mydata );

$this->assertEquals( 42, $mydata->de_DE );
}

public function test_maybe_set_linked_post_with_no_post() {
Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_ID )->andReturnTrue();
Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_LANG )->andReturnTrue();
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_LANG, FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn( 'de_DE' );
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_ID, FILTER_SANITIZE_NUMBER_INT )->andReturn( 42 );
Functions\expect( 'get_post' )->once()->andReturn( null );
Functions\expect( 'restore_current_blog' )->once();
Functions\expect( 'switch_to_blog' )->once();
Functions\expect( 'get_option' )->once()->andReturn( array() );

$mydata = new MslsOptionsPost();
$mydata = $this->test->maybe_set_linked_post( $mydata );

$this->assertNull( $mydata->de_DE );
}

function test_maybe_set_linked_post_with_no_blog_id() {
$options = \Mockery::mock( MslsOptions::class );

$collection = \Mockery::mock( MslsBlogCollection::class );
$collection->shouldReceive( 'get_blog_id' )->andReturn( null );

$this->test = new MslsMetaBox( $options, $collection );

Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_ID )->andReturnTrue();
Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_LANG )->andReturnTrue();
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_LANG, FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn( 'de_DE' );
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_ID, FILTER_SANITIZE_NUMBER_INT )->andReturn( 42 );
Functions\expect( 'get_option' )->once()->andReturn( array() );

$mydata = new MslsOptionsPost();
$mydata = $this->test->maybe_set_linked_post( $mydata );

$this->assertNull( $mydata->de_DE );
}

function test_maybe_set_linked_post_with_mydata_already_set() {
$options = \Mockery::mock( MslsOptions::class );

$collection = \Mockery::mock( MslsBlogCollection::class );

$this->test = new MslsMetaBox( $options, $collection );

Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_ID )->andReturnTrue();
Functions\expect( 'filter_has_var' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_LANG )->andReturnTrue();
Functions\expect( 'filter_input' )->once()->with( INPUT_GET, MslsFields::FIELD_MSLS_LANG, FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn( 'de_DE' );
Functions\expect( 'get_option' )->once()->andReturn( array() );

$mydata = new MslsOptionsPost();
$mydata->de_DE = 42;
$mydata = $this->test->maybe_set_linked_post( $mydata );

$this->assertEquals( 42, $mydata->de_DE );
}
}
4 changes: 2 additions & 2 deletions tests/phpunit/TestMslsOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -232,13 +232,13 @@ function ( $url = '' ) {
$this->assertEquals( $expected, MslsOptions::check_for_blog_slug( $url, $options ) );
}

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

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

public function test_get_option_name() {
public function test_get_option_name(): void {
$this->assertSame( 'msls', $this->get_test()->get_option_name() );
}
}
4 changes: 2 additions & 2 deletions tests/phpunit/TestMslsOptionsPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ protected function setUp(): void {
$this->test = new MslsOptionsPost( 42 );
}

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

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

public function test_get_option_name() {
public function test_get_option_name(): void {
$this->assertSame( 'msls_42', $this->test->get_option_name() );
}
}
2 changes: 1 addition & 1 deletion tests/phpunit/TestMslsOptionsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ protected function setUp(): void {
$wpdb = \Mockery::mock( '\wpdb' );
}

public function test_get_params() {
public function test_get_params(): void {
$this->assertEquals( array(), MslsOptionsQuery::get_params() );
}

Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/TestMslsOptionsTaxTerm.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function test_check_url_permastruct_false(): void {
$this->assertEquals( $expected, $this->test->check_base( $expected, $options ) );
}

public function test_get_option_name() {
public function test_get_option_name(): void {
$this->assertSame( 'msls_term_42', $this->test->get_option_name() );
}
}
16 changes: 8 additions & 8 deletions tests/phpunit/TestMslsOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function test_get_method(): void {
$this->assertEquals( array(), $this->test->get( 0 ) );
}

public function test_get_alternate_links_two_url() {
public function test_get_alternate_links_two_url(): void {
$blogs = array();

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

public function test_get_alternate_links_null_url() {
public function test_get_alternate_links_null_url(): void {
$blogs = array();

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

public function test_get_alternate_links_one_url() {
public function test_get_alternate_links_one_url(): void {
$blogs = array();

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

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

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

public function test___toString_output() {
public function test___toString_output(): void {
$blog = \Mockery::mock( MslsBlog::class );
$blog->shouldReceive( 'get_language' )->andReturn( 'de_DE' );
$blog->shouldReceive( 'get_description' )->andReturn( 'Deutsch' );
Expand Down Expand Up @@ -177,7 +177,7 @@ public function test___toString_output() {
$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 ) );
}

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

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

public function test_get_not_fulfilled() {
public function test_get_not_fulfilled(): void {
$blog = \Mockery::mock( MslsBlog::class );
$blog->shouldReceive( 'get_language' )->once()->andReturn( 'de_DE' );
// $blog->shouldReceive( 'get_description' )->once()->andReturn( 'Deutsch' );
Expand Down
2 changes: 1 addition & 1 deletion tests/phpunit/TestMslsPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ function () {
$this->assertEquals( $expected, MslsPlugin::plugin_dir_path( 'dist/msls-widget-block' ) );
}

public function test_print_alternate_links() {
public function test_print_alternate_links(): void {
Functions\expect( 'is_admin' )->once()->andReturn( false );
Functions\expect( 'is_front_page' )->once()->andReturn( true );
Functions\expect( 'get_option' )->once()->andReturn( array() );
Expand Down
Loading

0 comments on commit 7a54a93

Please sign in to comment.