From 32dfd5fa086ae91833c4a594f9aa10d5556196a4 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Sun, 8 Sep 2024 21:17:57 +0200 Subject: [PATCH] MslsMetaBox fully tested --- .../Importers/TestBaseImporter.php | 6 +- .../ContentImport/TestContentImporter.php | 8 +- tests/phpunit/TestMslsContentFilter.php | 8 +- tests/phpunit/TestMslsMetaBox.php | 103 +++++++++++++++--- tests/phpunit/TestMslsOptions.php | 4 +- tests/phpunit/TestMslsOptionsPost.php | 4 +- tests/phpunit/TestMslsOptionsQuery.php | 2 +- tests/phpunit/TestMslsOptionsTaxTerm.php | 2 +- tests/phpunit/TestMslsOutput.php | 16 +-- tests/phpunit/TestMslsPlugin.php | 2 +- tests/phpunit/TestMslsPostTag.php | 6 +- tests/phpunit/TestMslsPostTagClassic.php | 4 +- tests/phpunit/TestMslsSqlCacher.php | 2 +- 13 files changed, 121 insertions(+), 46 deletions(-) diff --git a/tests/phpunit/ContentImport/Importers/TestBaseImporter.php b/tests/phpunit/ContentImport/Importers/TestBaseImporter.php index 91a883de..2acc5a6b 100644 --- a/tests/phpunit/ContentImport/Importers/TestBaseImporter.php +++ b/tests/phpunit/ContentImport/Importers/TestBaseImporter.php @@ -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() ); } } diff --git a/tests/phpunit/ContentImport/TestContentImporter.php b/tests/phpunit/ContentImport/TestContentImporter.php index 6a3c4541..81cab160 100644 --- a/tests/phpunit/ContentImport/TestContentImporter.php +++ b/tests/phpunit/ContentImport/TestContentImporter.php @@ -33,15 +33,15 @@ 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(); @@ -49,7 +49,7 @@ public function test_handle_false() { $this->test->handle( false ); } - public function test_handle_true() { + public function test_handle_true(): void { $this->expectNotToPerformAssertions(); Actions\expectRemoved( 'msls_main_save' )->once(); diff --git a/tests/phpunit/TestMslsContentFilter.php b/tests/phpunit/TestMslsContentFilter.php index a51a57ce..797c2a8c 100644 --- a/tests/phpunit/TestMslsContentFilter.php +++ b/tests/phpunit/TestMslsContentFilter.php @@ -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' ); @@ -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() ); @@ -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' ); @@ -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' ); diff --git a/tests/phpunit/TestMslsMetaBox.php b/tests/phpunit/TestMslsMetaBox.php index 40716156..9b81955d 100644 --- a/tests/phpunit/TestMslsMetaBox.php +++ b/tests/phpunit/TestMslsMetaBox.php @@ -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 { @@ -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 ); } @@ -104,7 +106,7 @@ public function test_render_option_not_selected(): void { $this->assertEquals( '', $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; @@ -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' ); @@ -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' ); @@ -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 ); @@ -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 ); @@ -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 ); + } } diff --git a/tests/phpunit/TestMslsOptions.php b/tests/phpunit/TestMslsOptions.php index 70edabf7..a09c8fe7 100644 --- a/tests/phpunit/TestMslsOptions.php +++ b/tests/phpunit/TestMslsOptions.php @@ -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() ); } } diff --git a/tests/phpunit/TestMslsOptionsPost.php b/tests/phpunit/TestMslsOptionsPost.php index a0bc2b4c..32b71d8e 100644 --- a/tests/phpunit/TestMslsOptionsPost.php +++ b/tests/phpunit/TestMslsOptionsPost.php @@ -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' ) ); } @@ -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() ); } } diff --git a/tests/phpunit/TestMslsOptionsQuery.php b/tests/phpunit/TestMslsOptionsQuery.php index fb20fa69..539e1551 100644 --- a/tests/phpunit/TestMslsOptionsQuery.php +++ b/tests/phpunit/TestMslsOptionsQuery.php @@ -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() ); } diff --git a/tests/phpunit/TestMslsOptionsTaxTerm.php b/tests/phpunit/TestMslsOptionsTaxTerm.php index b0181095..b64a6660 100644 --- a/tests/phpunit/TestMslsOptionsTaxTerm.php +++ b/tests/phpunit/TestMslsOptionsTaxTerm.php @@ -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() ); } } diff --git a/tests/phpunit/TestMslsOutput.php b/tests/phpunit/TestMslsOutput.php index a50506a4..0ddaa7f3 100644 --- a/tests/phpunit/TestMslsOutput.php +++ b/tests/phpunit/TestMslsOutput.php @@ -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 ); @@ -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 ); @@ -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 ); @@ -137,14 +137,14 @@ public function test_get_alternate_links_one_url() { $this->assertEquals( '', $this->test->get_alternate_links() ); } - public function test___toString_no_translation() { + public function test___toString_no_translation(): void { $expected = 'Example'; 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' ); @@ -177,7 +177,7 @@ public function test___toString_output() { $this->assertEquals( 'de_DE Deutsch', 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' ); @@ -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' ); @@ -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' ); diff --git a/tests/phpunit/TestMslsPlugin.php b/tests/phpunit/TestMslsPlugin.php index 486b0f24..3b000676 100644 --- a/tests/phpunit/TestMslsPlugin.php +++ b/tests/phpunit/TestMslsPlugin.php @@ -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() ); diff --git a/tests/phpunit/TestMslsPostTag.php b/tests/phpunit/TestMslsPostTag.php index ed9a29c8..3708cb82 100644 --- a/tests/phpunit/TestMslsPostTag.php +++ b/tests/phpunit/TestMslsPostTag.php @@ -35,7 +35,7 @@ protected function setUp(): void { $this->test = new MslsPostTag( $options, $collection ); } - public function test_init() { + public function test_init(): void { $options = \Mockery::mock( MslsOptions::class ); $options->activate_autocomplete = true; @@ -117,7 +117,7 @@ public function test_edit_input(): void { $this->test->edit_input( $tag, 'test' ); } - public function test_add_input() { + public function test_add_input(): void { $taxonomy = \Mockery::mock( MslsTaxonomy::class ); $taxonomy->shouldReceive( 'is_taxonomy' )->atLeast()->once()->andReturnTrue(); $taxonomy->shouldReceive( 'get_request' )->atLeast()->once()->andReturn( 'post' ); @@ -145,7 +145,7 @@ public function test_add_input() { $this->test->add_input( 'test' ); } - public function test_the_input_no_blogs() { + public function test_the_input_no_blogs(): void { $options = \Mockery::mock( MslsOptions::class ); $collection = \Mockery::mock( MslsBlogCollection::class ); $collection->shouldReceive( 'get' )->andReturn( array() ); diff --git a/tests/phpunit/TestMslsPostTagClassic.php b/tests/phpunit/TestMslsPostTagClassic.php index d06fba91..f8a9897c 100644 --- a/tests/phpunit/TestMslsPostTagClassic.php +++ b/tests/phpunit/TestMslsPostTagClassic.php @@ -109,7 +109,7 @@ public function test_edit_input(): void { $this->test->edit_input( $tag, 'test' ); } - public function test_add_input() { + public function test_add_input(): void { $terms = array( (object) array( 'term_id' => 42, @@ -152,7 +152,7 @@ public function test_add_input() { $this->test->add_input( 'test' ); } - public function test_the_input_no_blogs() { + public function test_the_input_no_blogs(): void { $options = \Mockery::mock( MslsOptions::class ); $collection = \Mockery::mock( MslsBlogCollection::class ); $collection->shouldReceive( 'get' )->andReturn( array() ); diff --git a/tests/phpunit/TestMslsSqlCacher.php b/tests/phpunit/TestMslsSqlCacher.php index 3992d074..324a00cb 100644 --- a/tests/phpunit/TestMslsSqlCacher.php +++ b/tests/phpunit/TestMslsSqlCacher.php @@ -16,7 +16,7 @@ protected function setUp(): void { $this->test = new MslsSqlCacher( $wpdb, 'MslsSqlCacherTest' ); } - public function test_create() { + public function test_create(): void { global $wpdb; Functions\expect( 'esc_attr' )->atLeast()->once()->andReturnFirstArg();