From d0e8a4467e2cf107ce7d12d56044819a70bcc96f Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Tue, 24 Sep 2024 15:37:24 +0200 Subject: [PATCH] Tests fixed --- includes/MslsAdminIcon.php | 3 ++- tests/phpunit/MslsUnitTestCase.php | 7 +++---- tests/phpunit/TestMslsLink.php | 4 ---- tests/phpunit/TestMslsLinkImageOnly.php | 2 -- tests/phpunit/TestMslsLinkTextImage.php | 2 -- tests/phpunit/TestMslsLinkTextOnly.php | 2 -- tests/phpunit/TestMslsMetaBox.php | 9 ++------- tests/phpunit/TestMslsSqlCacher.php | 4 ++-- tests/phpunit/WP_Widget.php | 4 ++-- 9 files changed, 11 insertions(+), 26 deletions(-) diff --git a/includes/MslsAdminIcon.php b/includes/MslsAdminIcon.php index cc06766c..35828188 100644 --- a/includes/MslsAdminIcon.php +++ b/includes/MslsAdminIcon.php @@ -69,7 +69,8 @@ class MslsAdminIcon { * @param string $type */ public function __construct( ?string $type = null ) { - $this->type = esc_attr( $type ); + $this->type = $type; + $this->set_path(); } diff --git a/tests/phpunit/MslsUnitTestCase.php b/tests/phpunit/MslsUnitTestCase.php index 00c9f02e..6e2ed13f 100644 --- a/tests/phpunit/MslsUnitTestCase.php +++ b/tests/phpunit/MslsUnitTestCase.php @@ -21,22 +21,21 @@ protected function setUp(): void { \Mockery::namedMock( 'WooCommerce', \stdClass::class ); + Functions\when( '__' )->returnArg(); + Functions\when( 'esc_attr' )->returnArg(); Functions\when( 'esc_html' )->returnArg(); Functions\when( 'esc_html__' )->returnArg(); - Functions\when( 'esc_attr' )->returnArg(); Functions\when( 'esc_url' )->returnArg(); - Functions\when( '__' )->returnArg(); Functions\when( 'wp_kses' )->returnArg(); Functions\when( 'wp_kses_post' )->returnArg(); Functions\when( 'sanitize_text_field' )->returnArg(); } - protected function tearDown(): void { restore_error_handler(); - Monkey\tearDown(); \Mockery::close(); + Monkey\tearDown(); parent::tearDown(); } diff --git a/tests/phpunit/TestMslsLink.php b/tests/phpunit/TestMslsLink.php index 99bbe959..984cae66 100644 --- a/tests/phpunit/TestMslsLink.php +++ b/tests/phpunit/TestMslsLink.php @@ -27,14 +27,10 @@ public function test_get_types(): void { } public function test_get_description(): void { - Functions\when( '__' )->returnArg(); - $this->assertEquals( 'Flag and description', MslsLink::get_description() ); } public function test_get_types_description(): void { - Functions\when( '__' )->returnArg(); - $this->assertCount( 4, MslsLink::get_types_description() ); } diff --git a/tests/phpunit/TestMslsLinkImageOnly.php b/tests/phpunit/TestMslsLinkImageOnly.php index f32f69ec..154777da 100644 --- a/tests/phpunit/TestMslsLinkImageOnly.php +++ b/tests/phpunit/TestMslsLinkImageOnly.php @@ -8,8 +8,6 @@ class TestMslsLinkImageOnly extends MslsUnitTestCase { public function test_get_description_method(): void { - Functions\when( '__' )->returnArg(); - $this->assertIsSTring( MslsLinkImageOnly::get_description() ); } } diff --git a/tests/phpunit/TestMslsLinkTextImage.php b/tests/phpunit/TestMslsLinkTextImage.php index 87cf961a..7719abf1 100644 --- a/tests/phpunit/TestMslsLinkTextImage.php +++ b/tests/phpunit/TestMslsLinkTextImage.php @@ -8,8 +8,6 @@ class TestMslsLinkTextImage extends MslsUnitTestCase { public function test_get_description_method(): void { - Functions\when( '__' )->returnArg(); - $this->assertIsSTring( MslsLinkTextImage::get_description() ); } } diff --git a/tests/phpunit/TestMslsLinkTextOnly.php b/tests/phpunit/TestMslsLinkTextOnly.php index 8a010ef1..72757b11 100644 --- a/tests/phpunit/TestMslsLinkTextOnly.php +++ b/tests/phpunit/TestMslsLinkTextOnly.php @@ -8,8 +8,6 @@ class TestMslsLinkTextOnly extends MslsUnitTestCase { public function test_get_description_method(): void { - Functions\when( '__' )->returnArg(); - $this->assertIsSTring( MslsLinkTextOnly::get_description() ); } } diff --git a/tests/phpunit/TestMslsMetaBox.php b/tests/phpunit/TestMslsMetaBox.php index 9b81955d..b14306e7 100644 --- a/tests/phpunit/TestMslsMetaBox.php +++ b/tests/phpunit/TestMslsMetaBox.php @@ -17,13 +17,9 @@ class TestMslsMetaBox extends MslsUnitTestCase { protected function setUp(): void { - Functions\when( 'plugin_dir_path' )->justReturn( dirname( __DIR__, 2 ) . '/' ); + parent::setUp(); - Functions\when( 'esc_attr' )->returnArg(); - Functions\when( 'esc_html' )->returnArg(); - Functions\when( 'esc_url' )->returnArg(); - Functions\when( 'wp_kses' )->returnArg(); - Functions\when( '__' )->returnArg(); + Functions\when( 'plugin_dir_path' )->justReturn( dirname( __DIR__, 2 ) . '/' ); $blog = \Mockery::mock( MslsBlog::class ); $blog->shouldReceive( 'get_language' )->andReturn( 'de_DE' ); @@ -69,7 +65,6 @@ public function test_suggest(): void { Functions\expect( 'filter_input' )->once()->with( INPUT_POST, MslsFields::FIELD_S, FILTER_SANITIZE_FULL_SPECIAL_CHARS )->andReturn( 17 ); Functions\expect( 'get_post_stati' )->once()->andReturn( array( 'pending', 'draft', 'future' ) ); Functions\expect( 'get_the_title' )->once()->andReturn( 'Test' ); - Functions\expect( 'sanitize_text_field' )->times( 2 )->andReturnFirstArg(); Functions\expect( 'get_posts' )->once()->andReturn( array( $post ) ); Functions\expect( 'switch_to_blog' )->once(); Functions\expect( 'restore_current_blog' )->once(); diff --git a/tests/phpunit/TestMslsSqlCacher.php b/tests/phpunit/TestMslsSqlCacher.php index 324a00cb..18f9bed9 100644 --- a/tests/phpunit/TestMslsSqlCacher.php +++ b/tests/phpunit/TestMslsSqlCacher.php @@ -8,6 +8,8 @@ class TestMslsSqlCacher extends MslsUnitTestCase { protected function setUp(): void { + parent::setUp(); + $wpdb = \Mockery::mock( \WPDB::class ); $wpdb->shouldReceive( 'prepare' )->andReturn( '' ); @@ -19,8 +21,6 @@ protected function setUp(): void { public function test_create(): void { global $wpdb; - Functions\expect( 'esc_attr' )->atLeast()->once()->andReturnFirstArg(); - $wpdb = \Mockery::mock( \WPDB::class ); $this->assertInstanceOf( MslsSqlCacher::class, MslsSqlCacher::create( 'MslsSqlCacherTest', '' ) ); diff --git a/tests/phpunit/WP_Widget.php b/tests/phpunit/WP_Widget.php index 142a0a06..61892f47 100644 --- a/tests/phpunit/WP_Widget.php +++ b/tests/phpunit/WP_Widget.php @@ -30,7 +30,7 @@ public function __construct( $id_base, $name, $widget_options = array(), $contro * @return string ID attribute for `$field_name`. */ public function get_field_id( string $field_name ) { - return sprintf( 'field-id-%s', esc_attr( $field_name ) ); + return sprintf( 'field-id-%s', $field_name ); } /** @@ -39,6 +39,6 @@ public function get_field_id( string $field_name ) { * @return string Name attribute for `$field_name`. */ public function get_field_name( $field_name ) { - return sprintf( 'field-name-%s', esc_attr( $field_name ) ); + return sprintf( 'field-name-%s', $field_name ); } }