From 1d02ed45ef66ca15e913127db7047046b65a348d Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Wed, 13 Oct 2021 18:07:56 -0500 Subject: [PATCH] Replaces _doing_it_wrong() with trigger_error(). Per Ozz, this is an incorrect usage of doing it wrong. It should not be used for input validation. Use `trigger_error()` instead. --- .../class-wp-webfonts-schema-validator.php | 33 +++---------- .../webfonts-api/wpWebfontsController.php | 28 ++++++----- .../wpWebfontsProviderRegistry.php | 2 +- .../tests/webfonts-api/wpWebfontsRegistry.php | 49 ++++++++++++------- 4 files changed, 55 insertions(+), 57 deletions(-) diff --git a/src/wp-includes/webfonts-api/class-wp-webfonts-schema-validator.php b/src/wp-includes/webfonts-api/class-wp-webfonts-schema-validator.php index de2422e34e7c5..5b81a66b72b1b 100644 --- a/src/wp-includes/webfonts-api/class-wp-webfonts-schema-validator.php +++ b/src/wp-includes/webfonts-api/class-wp-webfonts-schema-validator.php @@ -73,11 +73,7 @@ private function is_provider_valid() { // @todo check if provider is registered. if ( empty( $this->webfont['provider'] ) || ! is_string( $this->webfont['provider'] ) ) { - _doing_it_wrong( - 'register_webfonts', - __( 'Webfont must define a string "provider".' ), - '5.9.0' - ); + trigger_error( __( 'Webfont provider must be a non-empty string.' ) ); return false; } @@ -94,11 +90,7 @@ private function is_provider_valid() { */ private function is_font_family_valid() { if ( empty( $this->webfont['fontFamily'] ) || ! is_string( $this->webfont['fontFamily'] ) ) { - _doing_it_wrong( - 'register_webfonts', - __( 'Webfont must define a string "fontFamily".' ), - '5.9.0' - ); + trigger_error( __( 'Webfont font family must be a non-empty string.' ) ); return false; } @@ -115,25 +107,18 @@ private function is_font_family_valid() { */ private function is_font_style_valid() { if ( empty( $this->webfont['fontStyle'] ) || ! is_string( $this->webfont['fontStyle'] ) ) { - _doing_it_wrong( - 'register_webfonts', - __( 'Webfont must define a string "fontStyle".' ), - '5.9.0' - ); - + trigger_error( __( 'Webfont font style must be a non-empty string.' ) ); return false; } if ( ! $this->is_font_style_value_valid( $this->webfont['fontStyle'] ) ) { - _doing_it_wrong( - 'register_webfonts', + trigger_error( sprintf( - /* translators: 1: Slant angle, 2: Given font style. */ + /* translators: 1: Slant angle, 2: Given font style. */ __( 'Webfont font style must be normal, italic, oblique, or oblique %1$s. Given: %2$s.' ), '', $this->webfont['fontStyle'] - ), - '5.9.0' + ) ); return false; @@ -170,11 +155,7 @@ private function is_font_style_value_valid( $font_style ) { private function is_font_weight_valid() { // @todo validate the value. if ( empty( $this->webfont['fontWeight'] ) || ! is_string( $this->webfont['fontWeight'] ) ) { - _doing_it_wrong( - 'register_webfonts', - __( 'Webfont must define a string "fontWeight".' ), - '5.9.0' - ); + trigger_error( __( 'Webfont font weight must be a non-empty string.' ) ); return false; } diff --git a/tests/phpunit/tests/webfonts-api/wpWebfontsController.php b/tests/phpunit/tests/webfonts-api/wpWebfontsController.php index 50f81723190d7..831e9e9a7721d 100644 --- a/tests/phpunit/tests/webfonts-api/wpWebfontsController.php +++ b/tests/phpunit/tests/webfonts-api/wpWebfontsController.php @@ -7,7 +7,7 @@ class Tests_Webfonts_API_wpWebfontsController extends WP_UnitTestCase { private static $webfonts; - public static function wpSetUpBeforeClass() { + public static function set_up_before_class() { require_once ABSPATH . WPINC . '/webfonts-api/class-wp-webfonts-schema-validator.php'; require_once ABSPATH . WPINC . '/webfonts-api/class-wp-webfonts-registry.php'; require_once ABSPATH . WPINC . '/webfonts-api/class-wp-webfonts-provider-registry.php'; @@ -51,19 +51,19 @@ public function test_register_webfonts_with_empty_schema() { } /** - * @covers WP_Webfonts_Controller::register_webfonts - * @covers WP_Webfonts_Controller::register_webfont + * @covers WP_Webfonts_Controller::register_webfonts + * @covers WP_Webfonts_Controller::register_webfont * * @dataProvider data_register_webfonts_with_invalid_schema * * @param array $webfonts Webfonts input. - * @param array $expected Exptected registered webfonts. + * @param array $expected Expected registered webfonts. */ - public function test_register_webfonts_with_invalid_schema( array $webfonts, array $expected ) { - $controller = $this->get_controller(); - - $this->setExpectedIncorrectUsage( 'register_webfonts' ); + public function test_register_webfonts_with_invalid_schema( array $webfonts, $expected_message, array $expected ) { + $this->expectNotice(); + $this->expectNoticeMessage( $expected_message ); + $controller = $this->get_controller(); $controller->register_webfonts( $webfonts ); $this->assertSame( $expected, $controller->get_webfonts() ); @@ -80,7 +80,7 @@ public function test_register_webfonts_with_invalid_schema( array $webfonts, arr public function data_register_webfonts_with_invalid_schema() { return array( 'provider: invalid type' => array( - 'webfonts' => array( + 'webfonts' => array( array( 'provider' => null, 'fontFamily' => 'Open Sans', @@ -94,7 +94,8 @@ public function data_register_webfonts_with_invalid_schema() { 'fontWeight' => '700', ), ), - 'expected' => array( + 'expected_message' => 'Webfont provider must be a non-empty string.', + 'expected' => array( 'open-sans.italic.700' => array( 'provider' => 'google', 'fontFamily' => 'Open Sans', @@ -104,7 +105,7 @@ public function data_register_webfonts_with_invalid_schema() { ), ), 'font family: invalid key' => array( - 'webfonts' => array( + 'webfonts' => array( array( 'provider' => 'google', 'fontFamily' => 'Roboto', @@ -118,7 +119,8 @@ public function data_register_webfonts_with_invalid_schema() { 'fontWeight' => '400', ), ), - 'expected' => array( + 'expected_message' => 'Webfont font family must be a non-empty string.', + 'expected' => array( 'roboto.normal.900' => array( 'provider' => 'google', 'fontFamily' => 'Roboto', @@ -191,7 +193,7 @@ public function test_get_webfonts_by_font_family() { } /** - * @covers WP_Webfonts_Controller::get_webfonts_by_font_family + * @covers WP_Webfonts_Controller::get_webfonts_by_font_family * * @dataProvider data_get_by_font_family_with_invalid_input * diff --git a/tests/phpunit/tests/webfonts-api/wpWebfontsProviderRegistry.php b/tests/phpunit/tests/webfonts-api/wpWebfontsProviderRegistry.php index e22a783a31abf..c9501d5483d6e 100644 --- a/tests/phpunit/tests/webfonts-api/wpWebfontsProviderRegistry.php +++ b/tests/phpunit/tests/webfonts-api/wpWebfontsProviderRegistry.php @@ -6,7 +6,7 @@ */ class Tests_Webfonts_API_wpWebfontsProviderRegistry extends WP_UnitTestCase { - public static function wpSetUpBeforeClass() { + public static function set_up_before_class() { require_once ABSPATH . WPINC . '/webfonts-api/class-wp-webfonts-provider-registry.php'; require_once __DIR__ . '/mocks/class-my-custom-webfonts-provider-mock.php'; } diff --git a/tests/phpunit/tests/webfonts-api/wpWebfontsRegistry.php b/tests/phpunit/tests/webfonts-api/wpWebfontsRegistry.php index 022295cc0f3fe..b0824fcec5656 100644 --- a/tests/phpunit/tests/webfonts-api/wpWebfontsRegistry.php +++ b/tests/phpunit/tests/webfonts-api/wpWebfontsRegistry.php @@ -27,8 +27,9 @@ private function get_registry() { * * @param array Webfonts input. */ - public function test_register_with_invalid_schema( array $webfont ) { - $this->setExpectedIncorrectUsage( 'register_webfonts' ); + public function test_register_with_invalid_schema( array $webfont, $expected_message ) { + $this->expectNotice(); + $this->expectNoticeMessage( $expected_message ); $registry = $this->get_registry(); @@ -43,102 +44,116 @@ public function test_register_with_invalid_schema( array $webfont ) { public function data_register_with_invalid_schema() { return array( 'empty array - no schema' => array( - array(), + 'webfont' => array(), + 'expected_message' => 'Webfont provider must be a non-empty string.', ), 'provider: not set' => array( - array( + 'webfont' => array( 'fontFamily' => 'Open Sans', 'fontStyle' => 'normal', 'fontWeight' => '400', ), + 'expected_message' => 'Webfont provider must be a non-empty string.', ), 'provider: empty string' => array( - array( + 'webfont' => array( 'provider' => '', 'fontFamily' => 'Open Sans', 'fontStyle' => 'normal', 'fontWeight' => '400', ), + 'expected_message' => 'Webfont provider must be a non-empty string.', ), 'provider: invalid type' => array( - array( + 'webfont' => array( 'provider' => true, 'fontFamily' => 'Open Sans', 'fontStyle' => 'normal', 'fontWeight' => '400', ), + 'expected_message' => 'Webfont provider must be a non-empty string.', ), 'font family: not set' => array( - array( + 'webfont' => array( 'provider' => 'local', 'fontStyle' => 'normal', 'fontWeight' => '400', ), + 'expected_message' => 'Webfont font family must be a non-empty string.', ), 'font family: empty string' => array( - array( + 'webfont' => array( 'provider' => 'local', 'fontFamily' => '', 'fontStyle' => 'normal', 'fontWeight' => '400', ), + 'expected_message' => 'Webfont font family must be a non-empty string.', ), 'font family: invalid type' => array( - array( + 'webfont' => array( 'provider' => 'local', 'fontFamily' => true, 'fontStyle' => 'normal', 'fontWeight' => '400', ), + 'expected_message' => 'Webfont font family must be a non-empty string.', ), 'font style: empty string' => array( - array( + 'webfont' => array( 'provider' => 'local', 'fontFamily' => 'Open Sans', 'fontStyle' => '', 'fontWeight' => '400', ), + 'expected_message' => 'Webfont font style must be a non-empty string.', ), 'font style: invalid type' => array( - array( + 'webfont' => array( 'provider' => 'local', 'fontFamily' => 'Open Sans', 'fontStyle' => true, 'fontWeight' => '400', ), + 'expected_message' => 'Webfont font style must be a non-empty string.', ), 'font style: invalid value' => array( - array( + 'webfont' => array( 'provider' => 'local', 'fontFamily' => 'Open Sans', 'fontStyle' => 'invalid', 'fontWeight' => '400', ), + 'expected_message' => 'Webfont font style must be normal, italic, oblique, or oblique . Given: invalid.', ), - 'font wegith: empty string' => array( - array( + 'font weight: empty string' => array( + 'webfont' => array( 'provider' => 'local', 'fontFamily' => 'Open Sans', 'fontStyle' => 'normal', 'fontWeight' => '', ), + 'expected_message' => 'Webfont font weight must be a non-empty string.', ), 'font weight: invalid type' => array( - array( + 'webfont' => array( 'provider' => 'local', 'fontFamily' => 'Open Sans', 'fontStyle' => 'normal', 'fontWeight' => true, ), + 'expected_message' => 'Webfont font weight must be a non-empty string.', ), - /* @todo uncomment once value validation is added. + // @todo uncomment once value validation is added. + /* 'font weight: invalid value' => array( - array( + 'webfont' => array( 'provider' => 'local', 'fontFamily' => 'Open Sans', 'fontStyle' => 'normal', 'fontWeight' => 'invalid', ), + 'expected_message' => 'Webfont font weight must be a non-empty string.', ), */ );