From 64625eccebad94a00dc5c66aecdd66a3d2305709 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Wed, 31 Jan 2024 06:52:45 -0600 Subject: [PATCH 01/11] Rename test classes. Renamed test classes per WordPress Core's Test Classes coding standards: For a test covering the entire class: Tests_[GroupName]_[ClassName] For a test covering a class' method: Tests_[GroupName]_[ClassName]_[MethodName] For a function: Tests_[GroupName]_[FunctionName] See https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/#test-classes. --- phpunit/tests/fonts/font-library/fontLibraryHooks.php | 5 ++++- .../font-library/wpRestFontCollectionsController.php | 2 +- .../fonts/font-library/wpRestFontFacesController.php | 8 ++++---- .../fonts/font-library/wpRestFontFamiliesController.php | 8 ++++---- 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/phpunit/tests/fonts/font-library/fontLibraryHooks.php b/phpunit/tests/fonts/font-library/fontLibraryHooks.php index 2c471e2a9759c6..cd8bd568d66d91 100644 --- a/phpunit/tests/fonts/font-library/fontLibraryHooks.php +++ b/phpunit/tests/fonts/font-library/fontLibraryHooks.php @@ -4,9 +4,12 @@ * * @package WordPress * @subpackage Font Library + * + * @group fonts + * @group font-library */ +class Tests_Fonts_FontLibraryHooks extends WP_UnitTestCase { -class Tests_Font_Library_Hooks extends WP_UnitTestCase { public function test_deleting_font_family_deletes_child_font_faces() { $font_family_id = self::factory()->post->create( array( diff --git a/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php b/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php index 06ae5edf3e9893..d3b57fe278419c 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php @@ -10,7 +10,7 @@ * * @coversDefaultClass WP_REST_Font_Collections_Controller */ -class WP_REST_Font_Collections_Controller_Test extends WP_Test_REST_Controller_Testcase { +class Tests_REST_WpRestFontCollectionsController extends WP_Test_REST_Controller_Testcase { protected static $admin_id; protected static $editor_id; protected static $mock_file; diff --git a/phpunit/tests/fonts/font-library/wpRestFontFacesController.php b/phpunit/tests/fonts/font-library/wpRestFontFacesController.php index d6a95814b205a2..27e3d35a68cc2d 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontFacesController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontFacesController.php @@ -8,9 +8,9 @@ * * @group restapi * - * @coversDefaultClass WP_REST_Font_Faces_Controller_Test + * @coversDefaultClass WP_REST_Font_Faces_Controller */ -class WP_REST_Font_Faces_Controller_Test extends WP_Test_REST_Controller_Testcase { +class Tests_REST_WpRestFontFacesController extends WP_Test_REST_Controller_Testcase { protected static $admin_id; protected static $editor_id; @@ -28,8 +28,8 @@ class WP_REST_Font_Faces_Controller_Test extends WP_Test_REST_Controller_Testcas ); public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { - self::$font_family_id = WP_REST_Font_Families_Controller_Test::create_font_family_post(); - self::$other_font_family_id = WP_REST_Font_Families_Controller_Test::create_font_family_post(); + self::$font_family_id = Tests_REST_WpRestFontFamiliesController::create_font_family_post(); + self::$other_font_family_id = Tests_REST_WpRestFontFamiliesController::create_font_family_post(); self::$font_face_id1 = self::create_font_face_post( self::$font_family_id, diff --git a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php index 601ff32e2b6c9e..007c8ae73fb3be 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php @@ -8,9 +8,9 @@ * * @group restapi * - * @coversDefaultClass WP_REST_Font_Families_Controller_Test + * @coversDefaultClass WP_REST_Font_Families_Controller */ -class WP_REST_Font_Families_Controller_Test extends WP_Test_REST_Controller_Testcase { +class Tests_REST_WpRestFontFamiliesController extends WP_Test_REST_Controller_Testcase { protected static $admin_id; protected static $editor_id; @@ -54,7 +54,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 'fontFamily' => 'Helvetica, Arial, sans-serif', ) ); - self::$font_face_id1 = WP_REST_Font_Faces_Controller_Test::create_font_face_post( + self::$font_face_id1 = Tests_REST_WpRestFontFacesController::create_font_face_post( self::$font_family_id1, array( 'fontFamily' => '"Open Sans"', @@ -63,7 +63,7 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 'src' => home_url( '/wp-content/fonts/open-sans-medium.ttf' ), ) ); - self::$font_face_id2 = WP_REST_Font_Faces_Controller_Test::create_font_face_post( + self::$font_face_id2 = Tests_REST_WpRestFontFacesController::create_font_face_post( self::$font_family_id1, array( 'fontFamily' => '"Open Sans"', From cdffdc8fd9fac8e5dfac0abf072f8b5fc5d79d72 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Wed, 31 Jan 2024 07:11:35 -0600 Subject: [PATCH 02/11] Update covers tag --- phpunit/tests/fonts/font-library/fontsDir.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/phpunit/tests/fonts/font-library/fontsDir.php b/phpunit/tests/fonts/font-library/fontsDir.php index 5c13f1d120f9a5..91f5a27be1395a 100644 --- a/phpunit/tests/fonts/font-library/fontsDir.php +++ b/phpunit/tests/fonts/font-library/fontsDir.php @@ -8,7 +8,7 @@ * @group fonts * @group font-library * - * @covers wp_get_font_dir + * @covers ::wp_get_font_dir */ class Tests_Fonts_WpFontDir extends WP_UnitTestCase { private $dir_defaults; From 5507b127a52b9f1546f083f5513195248428271d Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Wed, 31 Jan 2024 07:12:56 -0600 Subject: [PATCH 03/11] Use set_up_before_class() instead of __construct(). Test classes should not have a constructor. Instead, use the set_up_before_class() fixture method to perform tasks that pre-setup all tests within the test class. --- phpunit/tests/fonts/font-library/fontsDir.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/phpunit/tests/fonts/font-library/fontsDir.php b/phpunit/tests/fonts/font-library/fontsDir.php index 91f5a27be1395a..63d60596f26a78 100644 --- a/phpunit/tests/fonts/font-library/fontsDir.php +++ b/phpunit/tests/fonts/font-library/fontsDir.php @@ -11,11 +11,12 @@ * @covers ::wp_get_font_dir */ class Tests_Fonts_WpFontDir extends WP_UnitTestCase { - private $dir_defaults; + private static $dir_defaults; - public function __construct() { - parent::__construct(); - $this->dir_defaults = array( + public static function set_up_before_class() { + parent::set_up_before_class(); + + static::$dir_defaults = array( 'path' => path_join( WP_CONTENT_DIR, 'fonts' ), 'url' => content_url( 'fonts' ), 'subdir' => '', @@ -27,7 +28,7 @@ public function __construct() { public function test_fonts_dir() { $font_dir = wp_get_font_dir(); - $this->assertEquals( $font_dir, $this->dir_defaults ); + $this->assertEquals( $font_dir, static::$dir_defaults ); } public function test_fonts_dir_with_filter() { @@ -65,6 +66,6 @@ function set_new_values( $defaults ) { // Gets the fonts dir. $font_dir = wp_get_font_dir(); - $this->assertEquals( $font_dir, $this->dir_defaults, 'The wp_get_font_dir() method should return the default values.' ); + $this->assertEquals( $font_dir, static::$dir_defaults, 'The wp_get_font_dir() method should return the default values.' ); } } From fb28513e735d487f1528a62eea4eae6ee33eac8d Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Wed, 31 Jan 2024 07:17:39 -0600 Subject: [PATCH 04/11] Use assertSame() instead of assertEquals(). This is an ongoing effort in Core. Reasoning is shared in the 6.5 Trac ticket: >The assertEquals() test method does not check that the types >of the expected and actual values match. This can hide subtle >bugs especially when the values are falsey. https://core.trac.wordpress.org/ticket/59655 --- phpunit/tests/fonts/font-library/fontsDir.php | 7 ++++--- .../fonts/font-library/wpFontLibrary/getMimeTypes.php | 2 +- .../fonts/font-library/wpRestFontCollectionsController.php | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/phpunit/tests/fonts/font-library/fontsDir.php b/phpunit/tests/fonts/font-library/fontsDir.php index 63d60596f26a78..a7c1c326d62a2b 100644 --- a/phpunit/tests/fonts/font-library/fontsDir.php +++ b/phpunit/tests/fonts/font-library/fontsDir.php @@ -28,7 +28,8 @@ public static function set_up_before_class() { public function test_fonts_dir() { $font_dir = wp_get_font_dir(); - $this->assertEquals( $font_dir, static::$dir_defaults ); + + $this->assertSame( $font_dir, static::$dir_defaults ); } public function test_fonts_dir_with_filter() { @@ -58,7 +59,7 @@ function set_new_values( $defaults ) { 'error' => false, ); - $this->assertEquals( $font_dir, $expected, 'The wp_get_font_dir() method should return the expected values.' ); + $this->assertSame( $font_dir, $expected, 'The wp_get_font_dir() method should return the expected values.' ); // Remove the filter. remove_filter( 'font_dir', 'set_new_values' ); @@ -66,6 +67,6 @@ function set_new_values( $defaults ) { // Gets the fonts dir. $font_dir = wp_get_font_dir(); - $this->assertEquals( $font_dir, static::$dir_defaults, 'The wp_get_font_dir() method should return the default values.' ); + $this->assertSame( $font_dir, static::$dir_defaults, 'The wp_get_font_dir() method should return the default values.' ); } } diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php b/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php index 579baa2d248e0b..ec5a89e4bd393f 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php @@ -21,7 +21,7 @@ class Tests_Fonts_WpFontLibrary_GetMimeTypes extends WP_Font_Library_UnitTestCas */ public function test_should_supply_correct_mime_type_for_php_version( $php_version_id, $expected ) { $mimes = WP_Font_Library::get_expected_font_mime_types_per_php_version( $php_version_id ); - $this->assertEquals( $mimes, $expected ); + $this->assertSame( $mimes, $expected ); } /** diff --git a/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php b/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php index d3b57fe278419c..2a28efe6161ec5 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php @@ -61,7 +61,7 @@ public function test_get_items() { $response = rest_get_server()->dispatch( $request ); $content = $response->get_data(); $this->assertIsArray( $content ); - $this->assertEquals( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status() ); } /** @@ -71,7 +71,7 @@ public function test_get_item() { wp_set_current_user( self::$admin_id ); $request = new WP_REST_Request( 'GET', '/wp/v2/font-collections/mock-col-slug' ); $response = rest_get_server()->dispatch( $request ); - $this->assertEquals( 200, $response->get_status(), 'Response code is not 200' ); + $this->assertSame( 200, $response->get_status(), 'Response code is not 200' ); $response_data = $response->get_data(); $this->assertArrayHasKey( 'name', $response_data, 'Response data does not have the name key.' ); From 860ddb48c6617776d1578cc8f23ff36093cad2fa Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Wed, 31 Jan 2024 08:25:10 -0600 Subject: [PATCH 05/11] Rename test file to match func, class, or method. Per the WordPress Core coding standards, test files should be named according to the function, class, or method under test within its test class. Function under test: wpFunctioName.php Class under test: wpClassName.php Method under test: wpClassName/methodName.php See https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/#naming-and-organization. --- phpunit/tests/fonts/font-library/{fontsDir.php => wpFontsDir.php} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename phpunit/tests/fonts/font-library/{fontsDir.php => wpFontsDir.php} (100%) diff --git a/phpunit/tests/fonts/font-library/fontsDir.php b/phpunit/tests/fonts/font-library/wpFontsDir.php similarity index 100% rename from phpunit/tests/fonts/font-library/fontsDir.php rename to phpunit/tests/fonts/font-library/wpFontsDir.php From 6122917f812b2c2dddc26c2e10754f50e591bbbe Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Wed, 31 Jan 2024 08:55:25 -0600 Subject: [PATCH 06/11] Update dataProvider usage, formatting, and docs. Updates: * Refactors repetitive tests into a dataProvider. * Data provider's docblock. * Adds a brief explanation to each dataset (helps for understanding when a dataset fails). * Adds each argument name with a dataset. * Documents each argument in the test method's docblock. --- .../wpFontCollection/__construct.php | 8 +- .../wpFontLibrary/getMimeTypes.php | 7 +- .../wpFontUtils/formatFontFamily.php | 6 +- .../wpFontUtils/getFontFaceSlug.php | 8 ++ .../wpRestFontCollectionsController.php | 1 - .../wpRestFontFacesController.php | 80 +++++++++++--- .../wpRestFontFamiliesController.php | 100 +++++++++++++++--- 7 files changed, 172 insertions(+), 38 deletions(-) diff --git a/phpunit/tests/fonts/font-library/wpFontCollection/__construct.php b/phpunit/tests/fonts/font-library/wpFontCollection/__construct.php index bdc3d3b82331c6..4021c9eeedf3f4 100644 --- a/phpunit/tests/fonts/font-library/wpFontCollection/__construct.php +++ b/phpunit/tests/fonts/font-library/wpFontCollection/__construct.php @@ -11,11 +11,13 @@ * @covers WP_Font_Collection::__construct */ class Tests_Fonts_WpFontCollection_Construct extends WP_UnitTestCase { + /** * @dataProvider data_should_assign_properties_from_php_config * - * @param array $config Font collection config options. - * @param array $expected_data Expected output data. + * @param string $slug Font collection slug. + * @param array $config Font collection config options. + * @param array $expected_data Expected output data. */ public function test_should_assign_properties_from_php_config( $slug, $config, $expected_data ) { $collection = new WP_Font_Collection( $slug, $config ); @@ -32,7 +34,7 @@ public function test_should_assign_properties_from_php_config( $slug, $config, $ /** * Data provider. * - * @return array[] + * @return array */ public function data_should_assign_properties_from_php_config() { return array( diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php b/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php index ec5a89e4bd393f..f5e5fc64e611e1 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php @@ -13,11 +13,10 @@ class Tests_Fonts_WpFontLibrary_GetMimeTypes extends WP_Font_Library_UnitTestCase { /** - * * @dataProvider data_should_supply_correct_mime_type_for_php_version * - * @param array $php_version_id PHP_VERSION_ID value. - * @param array $expected Expected mime types. + * @param int $php_version_id PHP_VERSION_ID value. + * @param array $expected Expected mime types. */ public function test_should_supply_correct_mime_type_for_php_version( $php_version_id, $expected ) { $mimes = WP_Font_Library::get_expected_font_mime_types_per_php_version( $php_version_id ); @@ -27,7 +26,7 @@ public function test_should_supply_correct_mime_type_for_php_version( $php_versi /** * Data provider. * - * @return array[] + * @return array */ public function data_should_supply_correct_mime_type_for_php_version() { return array( diff --git a/phpunit/tests/fonts/font-library/wpFontUtils/formatFontFamily.php b/phpunit/tests/fonts/font-library/wpFontUtils/formatFontFamily.php index 53eaced4875207..f1acf5422a8b47 100644 --- a/phpunit/tests/fonts/font-library/wpFontUtils/formatFontFamily.php +++ b/phpunit/tests/fonts/font-library/wpFontUtils/formatFontFamily.php @@ -15,8 +15,8 @@ class Tests_Fonts_WpFontUtils_FormatFontFamily extends WP_UnitTestCase { /** * @dataProvider data_should_format_font_family * - * @param string $font_family Font family. - * @param string $expected Expected family. + * @param string $font_family Font family to test. + * @param string $expected Expected family. */ public function test_should_format_font_family( $font_family, $expected ) { $this->assertSame( @@ -30,7 +30,7 @@ public function test_should_format_font_family( $font_family, $expected ) { /** * Data provider. * - * @return array[] + * @return array */ public function data_should_format_font_family() { return array( diff --git a/phpunit/tests/fonts/font-library/wpFontUtils/getFontFaceSlug.php b/phpunit/tests/fonts/font-library/wpFontUtils/getFontFaceSlug.php index ded19749b9123e..32575d4bb19a28 100644 --- a/phpunit/tests/fonts/font-library/wpFontUtils/getFontFaceSlug.php +++ b/phpunit/tests/fonts/font-library/wpFontUtils/getFontFaceSlug.php @@ -10,6 +10,9 @@ class Tests_Fonts_WpFontUtils_GetFontFaceSlug extends WP_UnitTestCase { /** * @dataProvider data_get_font_face_slug_normalizes_values + * + * @param string[] $settings Settings to test. + * @param string $expected_slug Expected slug results. */ public function test_get_font_face_slug_normalizes_values( $settings, $expected_slug ) { $slug = WP_Font_Utils::get_font_face_slug( $settings ); @@ -17,6 +20,11 @@ public function test_get_font_face_slug_normalizes_values( $settings, $expected_ $this->assertSame( $expected_slug, $slug ); } + /** + * Data provider. + * + * @return array + */ public function data_get_font_face_slug_normalizes_values() { return array( 'Sets defaults' => array( diff --git a/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php b/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php index 2a28efe6161ec5..fdeb7d3a513d16 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php @@ -39,7 +39,6 @@ public static function wpTearDownAfterClass() { wp_unregister_font_collection( 'mock-col-slug' ); } - /** * @covers WP_REST_Font_Collections_Controller::register_routes */ diff --git a/phpunit/tests/fonts/font-library/wpRestFontFacesController.php b/phpunit/tests/fonts/font-library/wpRestFontFacesController.php index 27e3d35a68cc2d..d44a0542d2d2e5 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontFacesController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontFacesController.php @@ -128,26 +128,46 @@ public function test_font_faces_no_autosave_routes() { } /** - * @covers WP_REST_Font_Faces_Controller::get_context_param + * @doesNotPerformAssertions */ public function test_context_param() { - // Collection. - $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/font-families/' . self::$font_family_id . '/font-faces' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertArrayNotHasKey( 'allow_batch', $data['endpoints'][0] ); - $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] ); - $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); + // See test_get_context_param(). + } + + /** + * @dataProvider data_get_context_param + * + * @covers WP_REST_Font_Faces_Controller::get_context_param + * + * @param bool $single_route Whether to test a single route. + */ + public function test_get_context_param( $single_route ) { + $route = '/wp/v2/font-families/' . self::$font_family_id . '/font-faces'; + if ( $single_route ) { + $route .= '/' . self::$font_face_id1; + } - // Single. - $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/font-families/' . self::$font_family_id . '/font-faces/' . self::$font_face_id1 ); + $request = new WP_REST_Request( 'OPTIONS', $route ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); + $this->assertArrayNotHasKey( 'allow_batch', $data['endpoints'][0] ); $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] ); $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); } + /** + * Data provider. + * + * @return array + */ + public function data_get_context_param() { + return array( + 'Collection' => array( false ), + 'Single' => array( true ), + ); + } + /** * @covers WP_REST_Font_Faces_Controller::get_items */ @@ -567,6 +587,8 @@ public function test_create_item_default_theme_json_version() { * @dataProvider data_create_item_invalid_theme_json_version * * @covers WP_REST_Font_Faces_Controller::create_item + * + * @param int $theme_json_version Version input to test. */ public function test_create_item_invalid_theme_json_version( $theme_json_version ) { wp_set_current_user( self::$admin_id ); @@ -578,6 +600,11 @@ public function test_create_item_invalid_theme_json_version( $theme_json_version $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); } + /** + * Data provider. + * + * @return array + */ public function data_create_item_invalid_theme_json_version() { return array( array( 1 ), @@ -589,6 +616,8 @@ public function data_create_item_invalid_theme_json_version() { * @dataProvider data_create_item_invalid_settings * * @covers WP_REST_Font_Faces_Controller::validate_create_font_face_settings + * + * @param mixed $settings Settings to test. */ public function test_create_item_invalid_settings( $settings ) { wp_set_current_user( self::$admin_id ); @@ -601,6 +630,11 @@ public function test_create_item_invalid_settings( $settings ) { $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); } + /** + * Data provider. + * + * @return array + */ public function data_create_item_invalid_settings() { return array( 'Missing fontFamily' => array( @@ -682,6 +716,9 @@ public function test_create_item_invalid_file_src() { * @dataProvider data_create_item_santize_font_family * * @covers WP_REST_Font_Face_Controller::sanitize_font_face_settings + * + * @param string $font_family_setting Setting to test. + * @param string $expected Expected result. */ public function test_create_item_santize_font_family( $font_family_setting, $expected ) { $settings = array_merge( self::$default_settings, array( 'fontFamily' => $font_family_setting ) ); @@ -696,11 +733,25 @@ public function test_create_item_santize_font_family( $font_family_setting, $exp $this->assertSame( $expected, $data['font_face_settings']['fontFamily'] ); } + /** + * Data provider. + * + * @return array + */ public function data_create_item_santize_font_family() { return array( - array( 'Libre Barcode 128 Text', '"Libre Barcode 128 Text"' ), - array( 'B612 Mono', '"B612 Mono"' ), - array( 'Open Sans, Noto Sans, sans-serif', '"Open Sans", "Noto Sans", sans-serif' ), + 'multiword font with integer' => array( + 'font_family_setting' => 'Libre Barcode 128 Text', + 'expected' => '"Libre Barcode 128 Text"', + ), + 'multiword font' => array( + 'font_family_setting' => 'B612 Mono', + 'expected' => '"B612 Mono"', + ), + 'comma-separated fonts' => array( + 'font_family_setting' => 'Open Sans, Noto Sans, sans-serif', + 'expected' => '"Open Sans", "Noto Sans", sans-serif', + ), ); } @@ -709,6 +760,9 @@ public function data_create_item_santize_font_family() { */ // public function test_create_item_no_permission() {} + /** + * @covers WP_REST_Font_Faces_Controller::update_item + */ public function test_update_item() { $request = new WP_REST_Request( 'POST', '/wp/v2/font-families/' . self::$font_family_id . '/font-faces/' . self::$font_face_id1 ); $response = rest_get_server()->dispatch( $request ); diff --git a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php index 007c8ae73fb3be..e9a376c774a6dd 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php @@ -143,26 +143,45 @@ public function test_font_families_no_autosave_routes() { } /** - * @covers WP_REST_Font_Families_Controller::get_context_param + * @doesNotPerformAssertions */ public function test_context_param() { - // Collection. - $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/font-families' ); - $response = rest_get_server()->dispatch( $request ); - $data = $response->get_data(); - $this->assertArrayNotHasKey( 'allow_batch', $data['endpoints'][0] ); - $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] ); - $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); + // See test_get_context_param(). + } + + /** + * @dataProvider data_get_context_param + * + * @covers WP_REST_Font_Families_Controller::get_context_param + * + * @param bool $single_route Whether to test a single route. + */ + public function test_get_context_param( $single_route ) { + $route = '/wp/v2/font-families'; + if ( $single_route ) { + $route .= '/' . self::$font_family_id1; + } - // Single. - $request = new WP_REST_Request( 'OPTIONS', '/wp/v2/font-families/' . self::$font_family_id1 ); + $request = new WP_REST_Request( 'OPTIONS', $route ); $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); + $this->assertArrayNotHasKey( 'allow_batch', $data['endpoints'][0] ); $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] ); $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); } + /** + * Data provider. + * + * @return array + */ + public function data_get_context_param() { + return array( + 'Collection' => array( false ), + 'Single' => array( true ), + ); + } /** * @covers WP_REST_Font_Faces_Controller::get_items @@ -371,6 +390,8 @@ public function test_create_item_default_theme_json_version() { * @dataProvider data_create_item_invalid_theme_json_version * * @covers WP_REST_Font_Faces_Controller::create_item + * + * @param int $theme_json_version Version to test. */ public function test_create_item_invalid_theme_json_version( $theme_json_version ) { wp_set_current_user( self::$admin_id ); @@ -382,6 +403,11 @@ public function test_create_item_invalid_theme_json_version( $theme_json_version $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); } + /** + * Data provider. + * + * @return array + */ public function data_create_item_invalid_theme_json_version() { return array( array( 1 ), @@ -393,6 +419,8 @@ public function data_create_item_invalid_theme_json_version() { * @dataProvider data_create_item_with_default_preview * * @covers WP_REST_Font_Faces_Controller::sanitize_font_family_settings + * + * @param array $settings Settings to test. */ public function test_create_item_with_default_preview( $settings ) { wp_set_current_user( self::$admin_id ); @@ -410,6 +438,11 @@ public function test_create_item_with_default_preview( $settings ) { wp_delete_post( $data['id'], true ); } + /** + * Data provider. + * + * @return array + */ public function data_create_item_with_default_preview() { $default_settings = array( 'name' => 'Open Sans', @@ -430,6 +463,8 @@ public function data_create_item_with_default_preview() { * @dataProvider data_create_item_invalid_settings * * @covers WP_REST_Font_Faces_Controller::validate_create_font_face_settings + * + * @param array $settings Settings to test. */ public function test_create_item_invalid_settings( $settings ) { wp_set_current_user( self::$admin_id ); @@ -441,6 +476,11 @@ public function test_create_item_invalid_settings( $settings ) { $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); } + /** + * Data provider. + * + * @return array + */ public function data_create_item_invalid_settings() { return array( 'Missing name' => array( @@ -572,7 +612,10 @@ public function test_update_item() { /** * @dataProvider data_update_item_individual_settings + * * @covers WP_REST_Font_Families_Controller::update_item + * + * @param array $settings Settings to test. */ public function test_update_item_individual_settings( $settings ) { wp_set_current_user( self::$admin_id ); @@ -592,6 +635,11 @@ public function test_update_item_individual_settings( $settings ) { wp_delete_post( $font_family_id, true ); } + /** + * Data provider. + * + * @return array + */ public function data_update_item_individual_settings() { return array( array( array( 'name' => 'Opened Sans' ) ), @@ -602,10 +650,13 @@ public function data_update_item_individual_settings() { ); } - /** + /** * @dataProvider data_update_item_santize_font_family * * @covers WP_REST_Font_Families_Controller::sanitize_font_face_settings + * + * @param string $font_family_setting Font family setting to test. + * @param string $expected Expected result. */ public function test_update_item_santize_font_family( $font_family_setting, $expected ) { wp_set_current_user( self::$admin_id ); @@ -622,11 +673,25 @@ public function test_update_item_santize_font_family( $font_family_setting, $exp wp_delete_post( $font_family_id, true ); } + /** + * Data provider. + * + * @return array + */ public function data_update_item_santize_font_family() { return array( - array( 'Libre Barcode 128 Text', '"Libre Barcode 128 Text"' ), - array( 'B612 Mono', '"B612 Mono"' ), - array( 'Open Sans, Noto Sans, sans-serif', '"Open Sans", "Noto Sans", sans-serif' ), + 'multiword font with integer' => array( + 'font_family_setting' => 'Libre Barcode 128 Text', + 'expected' => '"Libre Barcode 128 Text"', + ), + 'multiword font' => array( + 'font_family_setting' => 'B612 Mono', + 'expected' => '"B612 Mono"', + ), + 'comma-separated fonts' => array( + 'font_family_setting' => 'Open Sans, Noto Sans, sans-serif', + 'expected' => '"Open Sans", "Noto Sans", sans-serif', + ), ); } @@ -634,6 +699,8 @@ public function data_update_item_santize_font_family() { * @dataProvider data_update_item_invalid_settings * * @covers WP_REST_Font_Faces_Controller::update_item + * + * @param array $settings Settings to test. */ public function test_update_item_empty_settings( $settings ) { wp_set_current_user( self::$admin_id ); @@ -646,6 +713,11 @@ public function test_update_item_empty_settings( $settings ) { $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); } + /** + * Data provider. + * + * @return array + */ public function data_update_item_invalid_settings() { return array( 'Empty name' => array( From de5d6cbffe2a92a7f578d83fe6d7f500cefdd3ff Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Wed, 31 Jan 2024 15:30:27 -0600 Subject: [PATCH 07/11] Add assertion failure messages. When there are more than 1 assertion in a test method, a failure message helps to identify which assertion failed. Once an assertion fails, any other assertions after it in the same test method will not run. Thus, the message helps to know which one of the assertions failed. This can help with debugging process. --- .../fontFamilyBackwardsCompatibility.php | 58 +++---- .../fonts/font-library/fontLibraryHooks.php | 8 +- .../wpRestFontCollectionsController.php | 14 +- .../wpRestFontFacesController.php | 131 +++++++------- .../wpRestFontFamiliesController.php | 161 +++++++++--------- 5 files changed, 192 insertions(+), 180 deletions(-) diff --git a/phpunit/tests/fonts/font-library/fontFamilyBackwardsCompatibility.php b/phpunit/tests/fonts/font-library/fontFamilyBackwardsCompatibility.php index a971bd51234305..2f7e2d2db2a194 100644 --- a/phpunit/tests/fonts/font-library/fontFamilyBackwardsCompatibility.php +++ b/phpunit/tests/fonts/font-library/fontFamilyBackwardsCompatibility.php @@ -33,68 +33,68 @@ public function test_font_faces_with_remote_src() { list( $font_face1, $font_face2, $font_face3 ) = $font_faces; // Updated font family post. - $this->assertSame( 'wp_font_family', $font_family->post_type ); - $this->assertSame( 'publish', $font_family->post_status ); + $this->assertSame( 'wp_font_family', $font_family->post_type, 'The font family post type should be wp_font_family.' ); + $this->assertSame( 'publish', $font_family->post_status, 'The font family post status should be publish.' ); $font_family_title = 'Open Sans'; - $this->assertSame( $font_family_title, $font_family->post_title ); + $this->assertSame( $font_family_title, $font_family->post_title, 'The font family post title should be Open Sans.' ); $font_family_slug = 'open-sans'; - $this->assertSame( $font_family_slug, $font_family->post_name ); + $this->assertSame( $font_family_slug, $font_family->post_name, 'The font family post name should be open-sans.' ); $font_family_content = wp_json_encode( json_decode( '{"fontFamily":"\'Open Sans\', sans-serif","preview":"https://s.w.org/images/fonts/16.7/previews/open-sans/open-sans.svg"}', true ) ); - $this->assertSame( $font_family_content, $font_family->post_content ); + $this->assertSame( $font_family_content, $font_family->post_content, 'The font family post content should match.' ); $meta = get_post_meta( $font_family_id, '_gutenberg_legacy_font_family', true ); - $this->assertSame( $legacy_content, $meta ); + $this->assertSame( $legacy_content, $meta, 'The _gutenberg_legacy_font_family post meta content should match.' ); // First font face post. - $this->assertSame( 'wp_font_face', $font_face1->post_type ); - $this->assertSame( $font_family_id, $font_face1->post_parent ); - $this->assertSame( 'publish', $font_face1->post_status ); + $this->assertSame( 'wp_font_face', $font_face1->post_type, 'The 1st font face post type should be wp_font_face.' ); + $this->assertSame( $font_family_id, $font_face1->post_parent, 'The 1st font face post parent should match.' ); + $this->assertSame( 'publish', $font_face1->post_status, 'The 1st font face post status should be publish.' ); $font_face1_title = 'open sans;normal;400;100%;U+0-10FFFF'; - $this->assertSame( $font_face1_title, $font_face1->post_title ); - $this->assertSame( sanitize_title( $font_face1_title ), $font_face1->post_name ); + $this->assertSame( $font_face1_title, $font_face1->post_title, 'The 1st font face post title should match.' ); + $this->assertSame( sanitize_title( $font_face1_title ), $font_face1->post_name, 'The 1st font face post name should match.' ); $font_face1_content = wp_json_encode( json_decode( '{"fontFamily":"Open Sans","fontStyle":"normal","fontWeight":"400","preview":"https://s.w.org/images/fonts/16.7/previews/open-sans/open-sans-400-normal.svg","src":"https://fonts.gstatic.com/s/opensans/v35/memSYaGs126MiZpBA-UvWbX2vVnXBbObj2OVZyOOSr4dVJWUgsjZ0C4nY1M2xLER.ttf"}' ) ); - $this->assertSame( $font_face1_content, $font_face1->post_content ); + $this->assertSame( $font_face1_content, $font_face1->post_content, 'The 1st font face post content should match.' ); // With a remote url, file post meta should not be set. $meta = get_post_meta( $font_face1->ID, '_wp_font_face_file', true ); - $this->assertSame( '', $meta ); + $this->assertSame( '', $meta, 'The _wp_font_face_file post meta for the 1st font face should be an empty string.' ); // Second font face post. - $this->assertSame( 'wp_font_face', $font_face2->post_type ); - $this->assertSame( $font_family_id, $font_face2->post_parent ); - $this->assertSame( 'publish', $font_face2->post_status ); + $this->assertSame( 'wp_font_face', $font_face2->post_type, 'The 2nd font face post type should be wp_font_face.' ); + $this->assertSame( $font_family_id, $font_face2->post_parent, 'The 2md font face post type should be wp_font_face.' ); + $this->assertSame( 'publish', $font_face2->post_status, 'The 2nd font face post status should be publish.' ); $font_face2_title = 'open sans;italic;400;100%;U+0-10FFFF'; - $this->assertSame( $font_face2_title, $font_face2->post_title ); - $this->assertSame( sanitize_title( $font_face2_title ), $font_face2->post_name ); + $this->assertSame( $font_face2_title, $font_face2->post_title, 'The 2nd font face post title should match.' ); + $this->assertSame( sanitize_title( $font_face2_title ), $font_face2->post_name, 'The 2nd font face post name should match.' ); $font_face2_content = wp_json_encode( json_decode( '{"fontFamily":"Open Sans","fontStyle":"italic","fontWeight":"400","preview":"https://s.w.org/images/fonts/16.7/previews/open-sans/open-sans-400-italic.svg","src":"https://fonts.gstatic.com/s/opensans/v35/memQYaGs126MiZpBA-UFUIcVXSCEkx2cmqvXlWq8tWZ0Pw86hd0Rk8ZkaVcUwaERZjA.ttf"}' ) ); - $this->assertSame( $font_face2_content, $font_face2->post_content ); + $this->assertSame( $font_face2_content, $font_face2->post_content, 'The 2nd font face post content should match.' ); // With a remote url, file post meta should not be set. $meta = get_post_meta( $font_face2->ID, '_wp_font_face_file', true ); - $this->assertSame( '', $meta ); + $this->assertSame( '', $meta, 'The _wp_font_face_file post meta for the 2nd font face should be an empty string.' ); // Third font face post. - $this->assertSame( 'wp_font_face', $font_face3->post_type ); - $this->assertSame( $font_family_id, $font_face3->post_parent ); - $this->assertSame( 'publish', $font_face3->post_status ); + $this->assertSame( 'wp_font_face', $font_face3->post_type, 'The 3rd font face post type should be wp_font_face.' ); + $this->assertSame( $font_family_id, $font_face3->post_parent, 'The 3rd font face post type should be wp_font_face.' ); + $this->assertSame( 'publish', $font_face3->post_status, 'The 3rd font face post status should be publish.' ); $font_face3_title = 'open sans;normal;700;100%;U+0-10FFFF'; - $this->assertSame( $font_face3_title, $font_face3->post_title ); - $this->assertSame( sanitize_title( $font_face3_title ), $font_face3->post_name ); + $this->assertSame( $font_face3_title, $font_face3->post_title, 'The 3rd font face post title should match.' ); + $this->assertSame( sanitize_title( $font_face3_title ), $font_face3->post_name, 'The 3rd font face post name should match.' ); $font_face3_content = wp_json_encode( json_decode( '{"fontFamily":"Open Sans","fontStyle":"normal","fontWeight":"700","preview":"https://s.w.org/images/fonts/16.7/previews/open-sans/open-sans-700-normal.svg","src":"https://fonts.gstatic.com/s/opensans/v35/memSYaGs126MiZpBA-UvWbX2vVnXBbObj2OVZyOOSr4dVJWUgsg-1y4nY1M2xLER.ttf"}' ) ); - $this->assertSame( $font_face3_content, $font_face3->post_content ); + $this->assertSame( $font_face3_content, $font_face3->post_content, 'The 3rd font face post content should match.' ); // With a remote url, file post meta should not be set. $meta = get_post_meta( $font_face3->ID, '_wp_font_face_file', true ); - $this->assertSame( '', $meta ); + $this->assertSame( '', $meta, 'The _wp_font_face_file post meta for the 3rd font face should be an empty string.' ); wp_delete_post( $font_family_id, true ); wp_delete_post( $font_face1->ID, true ); @@ -110,13 +110,13 @@ public function test_font_faces_with_local_src() { gutenberg_convert_legacy_font_family_format(); $font_faces = $this->get_font_faces( $font_family_id ); - $this->assertCount( 1, $font_faces ); + $this->assertCount( 1, $font_faces, 'There should be 1 font face.' ); $font_face = reset( $font_faces ); // Check that file meta is present. $file_path = 'open-sans_normal_400.ttf'; $meta = get_post_meta( $font_face->ID, '_wp_font_face_file', true ); - $this->assertSame( $file_path, $meta ); + $this->assertSame( $file_path, $meta, 'The _wp_font_face_file should match.' ); wp_delete_post( $font_family_id, true ); wp_delete_post( $font_face->ID, true ); diff --git a/phpunit/tests/fonts/font-library/fontLibraryHooks.php b/phpunit/tests/fonts/font-library/fontLibraryHooks.php index cd8bd568d66d91..eabc6876988d63 100644 --- a/phpunit/tests/fonts/font-library/fontLibraryHooks.php +++ b/phpunit/tests/fonts/font-library/fontLibraryHooks.php @@ -36,8 +36,8 @@ public function test_deleting_font_family_deletes_child_font_faces() { wp_delete_post( $font_family_id, true ); - $this->assertNull( get_post( $font_face_id ) ); - $this->assertNotNull( get_post( $other_font_face_id ) ); + $this->assertNull( get_post( $font_face_id ), 'Font face post should also have been deleted.' ); + $this->assertNotNull( get_post( $other_font_face_id ), 'The other post should exist.' ); } public function test_deleting_font_faces_deletes_associated_font_files() { @@ -46,8 +46,8 @@ public function test_deleting_font_faces_deletes_associated_font_files() { wp_delete_post( $font_face_id, true ); - $this->assertFalse( file_exists( $font_path ) ); - $this->assertTrue( file_exists( $other_font_path ) ); + $this->assertFalse( file_exists( $font_path ), 'The font file should have been deleted when the post was deleted' ); + $this->assertTrue( file_exists( $other_font_path ), 'The other font file should exist' ); } protected function create_font_face_with_file( $filename ) { diff --git a/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php b/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php index fdeb7d3a513d16..d73315c311ad7f 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php @@ -152,13 +152,13 @@ public function test_get_item_schema() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); $properties = $data['schema']['properties']; - $this->assertCount( 5, $properties ); - $this->assertArrayHasKey( 'slug', $properties ); - $this->assertArrayHasKey( 'name', $properties ); - $this->assertArrayHasKey( 'description', $properties ); - $this->assertArrayHasKey( 'font_families', $properties ); - $this->assertArrayHasKey( 'categories', $properties ); + $this->assertCount( 5, $properties, 'There should be 5 properties in the response data schema.' ); + $this->assertArrayHasKey( 'slug', $properties, 'The slug property should exist in the response data schema.' ); + $this->assertArrayHasKey( 'name', $properties, 'The name property should exist in the response data schema.' ); + $this->assertArrayHasKey( 'description', $properties, 'The description property should exist in the response data schema.' ); + $this->assertArrayHasKey( 'font_families', $properties, 'The slug font_families should exist in the response data schema.' ); + $this->assertArrayHasKey( 'categories', $properties, 'The categories property should exist in the response data schema.' ); } } diff --git a/phpunit/tests/fonts/font-library/wpRestFontFacesController.php b/phpunit/tests/fonts/font-library/wpRestFontFacesController.php index d44a0542d2d2e5..52b99629645973 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontFacesController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontFacesController.php @@ -151,9 +151,10 @@ public function test_get_context_param( $single_route ) { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertArrayNotHasKey( 'allow_batch', $data['endpoints'][0] ); - $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] ); - $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); + $endpoint_data = $data['endpoints'][0]; + $this->assertArrayNotHasKey( 'allow_batch', $endpoint_data, 'The allow_batch property should not exist in the endpoint data.' ); + $this->assertSame( 'view', $endpoint_data['args']['context']['default'], 'The endpoint\'s args::context::default should be set to view.' ); + $this->assertSame( array( 'view', 'embed', 'edit' ), $endpoint_data['args']['context']['enum'], 'The endpoint\'s args::context::enum should be set to [ view, embed, edit ].' ); } /** @@ -177,11 +178,11 @@ public function test_get_items() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 200, $response->get_status() ); - $this->assertCount( 2, $data ); - $this->assertArrayHasKey( '_links', $data[0] ); + $this->assertSame( 200, $response->get_status(), 'The response status should be 200' ); + $this->assertCount( 2, $data, 'There should be 2 properties in the response data.' ); + $this->assertArrayHasKey( '_links', $data[0], 'The _links property should exist in the response data 0.' ); $this->check_font_face_data( $data[0], self::$font_face_id2, $data[0]['_links'] ); - $this->assertArrayHasKey( '_links', $data[1] ); + $this->assertArrayHasKey( '_links', $data[1], 'The _links property should exist in the response data 1.' ); $this->check_font_face_data( $data[1], self::$font_face_id1, $data[1]['_links'] ); } @@ -192,11 +193,11 @@ public function test_get_items_no_permission() { wp_set_current_user( 0 ); $request = new WP_REST_Request( 'GET', '/wp/v2/font-families/' . self::$font_family_id . '/font-faces' ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_read', $response, 401 ); + $this->assertErrorResponse( 'rest_cannot_read', $response, 401, 'The response should return an error with a "rest_cannot_read" code and 401 status.' ); wp_set_current_user( self::$editor_id ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_read', $response, 403 ); + $this->assertErrorResponse( 'rest_cannot_read', $response, 403, 'The response should return an error with a "rest_cannot_read" code and 403 status.' ); } /** @@ -218,7 +219,7 @@ public function test_get_item() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); $this->check_font_face_data( $data, self::$font_face_id1, $response->get_links() ); } @@ -233,8 +234,9 @@ public function test_get_item_removes_extra_settings() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 200, $response->get_status() ); - $this->assertArrayNotHasKey( 'extra', $data['font_face_settings'] ); + $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); + $this->assertArrayHasKey( 'font_face_settings', $data, 'The font_face_settings property should exist in the response data.' ); + $this->assertArrayNotHasKey( 'extra', $data['font_face_settings'], 'The extra property should exist in the font_face_settings data.' ); } /** @@ -260,8 +262,9 @@ public function test_get_item_malformed_post_content_returns_empty_settings() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 200, $response->get_status() ); - $this->assertSame( $empty_settings, $data['font_face_settings'] ); + $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); + $this->assertArrayHasKey( 'font_face_settings', $data, 'The font_face_settings property should exist in the response data.' ); + $this->assertSame( $empty_settings, $data['font_face_settings'], 'The empty settings should exist in the font_face_settings data.' ); wp_delete_post( $font_face_id, true ); } @@ -284,11 +287,11 @@ public function test_get_item_no_permission() { $request = new WP_REST_Request( 'GET', '/wp/v2/font-families/' . self::$font_family_id . '/font-faces/' . self::$font_face_id1 ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_read', $response, 401 ); + $this->assertErrorResponse( 'rest_cannot_read', $response, 401, 'The response should return an error with a "rest_cannot_read" code and 401 status.' ); wp_set_current_user( self::$editor_id ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_read', $response, 403 ); + $this->assertErrorResponse( 'rest_cannot_read', $response, 403, 'The response should return an error with a "rest_cannot_read" code and 403 status.' ); } /** @@ -311,7 +314,7 @@ public function test_get_item_valid_parent_id() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); $this->assertSame( self::$font_family_id, $data['parent'], 'The returned parent id should match the font family id.' ); } @@ -353,7 +356,7 @@ public function test_create_item() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 201, $response->get_status() ); + $this->assertSame( 201, $response->get_status(), 'The response status should be 201.' ); $this->check_font_face_data( $data, $data['id'], $response->get_links() ); $this->check_file_meta( $data['id'], array( $data['font_face_settings']['src'] ) ); @@ -365,7 +368,8 @@ public function test_create_item() { 'fontFamily' => '"Open Sans"', 'fontWeight' => '200', 'fontStyle' => 'normal', - ) + ), + 'The font_face_settings data should match the expected data.' ); $this->assertSame( self::$font_family_id, $data['parent'], 'The returned parent id should match the font family id.' ); @@ -398,12 +402,12 @@ public function test_create_item_with_multiple_font_files() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 201, $response->get_status() ); + $this->assertSame( 201, $response->get_status(), 'The response status should be 201.' ); $this->check_font_face_data( $data, $data['id'], $response->get_links() ); $this->check_file_meta( $data['id'], $data['font_face_settings']['src'] ); $settings = $data['font_face_settings']; - $this->assertCount( 4, $settings['src'] ); + $this->assertCount( 4, $settings['src'], 'There should be 4 items in the font_face_settings::src data.' ); wp_delete_post( $data['id'], true ); } @@ -471,7 +475,7 @@ public function test_create_item_with_url_src() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 201, $response->get_status() ); + $this->assertSame( 201, $response->get_status(), 'The response status should be 201.' ); $this->check_font_face_data( $data, $data['id'], $response->get_links() ); wp_delete_post( $data['id'], true ); @@ -508,9 +512,9 @@ public function test_create_item_with_all_properties() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 201, $response->get_status() ); - $this->assertArrayHasKey( 'font_face_settings', $data ); - $this->assertSame( $properties, $data['font_face_settings'] ); + $this->assertSame( 201, $response->get_status(), 'The response status should be 201.' ); + $this->assertArrayHasKey( 'font_face_settings', $data, 'The font_face_settings property should exist in the response data.' ); + $this->assertSame( $properties, $data['font_face_settings'], 'The font_face_settings should match the expected properties.' ); wp_delete_post( $data['id'], true ); } @@ -548,10 +552,10 @@ public function test_create_item_with_duplicate_properties() { $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_duplicate_font_face', $response, 400 ); + $this->assertErrorResponse( 'rest_duplicate_font_face', $response, 400, 'The response should return an error for "rest_duplicate_font_face" with 400 status.' ); $expected_message = 'A font face matching those settings already exists.'; $message = $response->as_error()->get_error_messages()[0]; - $this->assertSame( $expected_message, $message ); + $this->assertSame( $expected_message, $message, 'The response error message should match.' ); wp_delete_post( $font_face_id, true ); } @@ -576,8 +580,8 @@ public function test_create_item_default_theme_json_version() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 201, $response->get_status() ); - $this->assertArrayHasKey( 'theme_json_version', $data ); + $this->assertSame( 201, $response->get_status(), 'The response status should be 201.' ); + $this->assertArrayHasKey( 'theme_json_version', $data, 'The theme_json_version property should exist in the response data.' ); $this->assertSame( 2, $data['theme_json_version'], 'The default theme.json version should be 2.' ); wp_delete_post( $data['id'], true ); @@ -681,10 +685,10 @@ public function test_create_item_invalid_settings_json() { $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); + $this->assertErrorResponse( 'rest_invalid_param', $response, 400, 'The response should return an error for "rest_invalid_param" with 400 status.' ); $expected_message = 'font_face_settings parameter must be a valid JSON string.'; $message = $response->as_error()->get_all_error_data()[0]['params']['font_face_settings']; - $this->assertSame( $expected_message, $message ); + $this->assertSame( $expected_message, $message, 'The response error message should match.' ); } /** @@ -706,10 +710,10 @@ public function test_create_item_invalid_file_src() { $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); + $this->assertErrorResponse( 'rest_invalid_param', $response, 400, 'The response should return an error for "rest_invalid_param" with 400 status.' ); $expected_message = 'File ' . array_keys( $files )[0] . ' must be used in font_face_settings[src].'; $message = $response->as_error()->get_all_error_data()[0]['params']['font_face_settings']; - $this->assertSame( $expected_message, $message ); + $this->assertSame( $expected_message, $message, 'The response error message should match.' ); } /** @@ -729,8 +733,8 @@ public function test_create_item_santize_font_family( $font_family_setting, $exp $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 201, $response->get_status() ); - $this->assertSame( $expected, $data['font_face_settings']['fontFamily'] ); + $this->assertSame( 201, $response->get_status(), 'The response status should be 201.' ); + $this->assertSame( $expected, $data['font_face_settings']['fontFamily'], 'The response fontFamily should match.' ); } /** @@ -779,8 +783,8 @@ public function test_delete_item() { $request->set_param( 'force', true ); $response = rest_get_server()->dispatch( $request ); - $this->assertSame( 200, $response->get_status() ); - $this->assertNull( get_post( $font_face_id ) ); + $this->assertSame( 200, $response->get_status(), 'The response status should be 201.' ); + $this->assertNull( get_post( $font_face_id ), 'The deleted post should not exist.' ); } /** @@ -793,15 +797,15 @@ public function test_delete_item_no_trash() { // Attempt trashing. $request = new WP_REST_Request( 'DELETE', '/wp/v2/font-families/' . self::$font_family_id . '/font-faces/' . $font_face_id ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); + $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501, 'The response should return an error for "rest_trash_not_supported" with 501 status.' ); $request->set_param( 'force', 'false' ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); + $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501, 'When "force" is false, the response should return an error for "rest_trash_not_supported" with 501 status.' ); // Ensure the post still exists. $post = get_post( $font_face_id ); - $this->assertNotEmpty( $post ); + $this->assertNotEmpty( $post, 'The post should still exists.' ); } /** @@ -835,7 +839,7 @@ public function test_delete_item_invalid_parent_id() { $request = new WP_REST_Request( 'DELETE', '/wp/v2/font-families/' . self::$other_font_family_id . '/font-faces/' . self::$font_face_id1 ); $request->set_param( 'force', true ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_font_face_parent_id_mismatch', $response, 404 ); + $this->assertErrorResponse( 'rest_font_face_parent_id_mismatch', $response, 404, 'The response should return an error for "rest_font_face_parent_id_mismatch" with 404 status.' ); $expected_message = 'The font face does not belong to the specified font family with id of "' . self::$other_font_family_id . '"'; $this->assertSame( $expected_message, $response->as_error()->get_error_messages()[0], 'The message must contain the correct parent ID.' ); @@ -850,12 +854,12 @@ public function test_delete_item_no_permissions() { wp_set_current_user( 0 ); $request = new WP_REST_Request( 'DELETE', '/wp/v2/font-families/' . self::$font_family_id . '/font-faces/' . $font_face_id ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_delete', $response, 401 ); + $this->assertErrorResponse( 'rest_cannot_delete', $response, 401, 'The response should return an error for "rest_cannot_delete" with 401 status for an invalid user.' ); wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'DELETE', '/wp/v2/font-families/' . self::$font_family_id . '/font-faces/' . $font_face_id ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 ); + $this->assertErrorResponse( 'rest_cannot_delete', $response, 403, 'The response should return an error for "rest_cannot_delete" with 403 status for a user without permission.' ); } /** @@ -867,7 +871,7 @@ public function test_prepare_item() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); $this->check_font_face_data( $data, self::$font_face_id2, $response->get_links() ); } @@ -879,34 +883,37 @@ public function test_get_item_schema() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); $properties = $data['schema']['properties']; - $this->assertCount( 4, $properties ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'theme_json_version', $properties ); - $this->assertArrayHasKey( 'parent', $properties ); - $this->assertArrayHasKey( 'font_face_settings', $properties ); + $this->assertCount( 4, $properties, 'There should be 4 properties in the schema::properties data.' ); + $this->assertArrayHasKey( 'id', $properties, 'The id property should exist in the schema::properties data.' ); + $this->assertArrayHasKey( 'theme_json_version', $properties, 'The id property should exist in the schema::properties data.' ); + $this->assertArrayHasKey( 'parent', $properties, 'The id property should exist in the schema::properties data.' ); + $this->assertArrayHasKey( 'font_face_settings', $properties, 'The id property should exist in the schema::properties data.' ); } protected function check_font_face_data( $data, $post_id, $links ) { $post = get_post( $post_id ); - $this->assertArrayHasKey( 'id', $data ); - $this->assertSame( $post->ID, $data['id'] ); + $this->assertArrayHasKey( 'id', $data, 'The id property should exist in response data.' ); + $this->assertSame( $post->ID, $data['id'], 'The "id" from the response data should match the post ID.' ); - $this->assertArrayHasKey( 'parent', $data ); - $this->assertSame( $post->post_parent, $data['parent'] ); + $this->assertArrayHasKey( 'parent', $data, 'The parent property should exist in response data.' ); + $this->assertSame( $post->post_parent, $data['parent'], 'The "parent" from the response data should match the post parent.' ); - $this->assertArrayHasKey( 'theme_json_version', $data ); - $this->assertSame( WP_Theme_JSON::LATEST_SCHEMA, $data['theme_json_version'] ); + $this->assertArrayHasKey( 'theme_json_version', $data, 'The theme_json_version property should exist in response data.' ); + $this->assertSame( WP_Theme_JSON::LATEST_SCHEMA, $data['theme_json_version'], 'The "theme_json_version" from the response data should match WP_Theme_JSON::LATEST_SCHEMA.' ); - $this->assertArrayHasKey( 'font_face_settings', $data ); - $this->assertSame( $post->post_content, wp_json_encode( $data['font_face_settings'] ) ); + $this->assertArrayHasKey( 'font_face_settings', $data, 'The font_face_settings property should exist in response data.' ); + $this->assertSame( $post->post_content, wp_json_encode( $data['font_face_settings'] ), 'The encoded "font_face_settings" from the response data should match the post content.' ); - $this->assertNotEmpty( $links ); - $this->assertSame( rest_url( 'wp/v2/font-families/' . $post->post_parent . '/font-faces/' . $post->ID ), $links['self'][0]['href'] ); - $this->assertSame( rest_url( 'wp/v2/font-families/' . $post->post_parent . '/font-faces' ), $links['collection'][0]['href'] ); - $this->assertSame( rest_url( 'wp/v2/font-families/' . $post->post_parent ), $links['parent'][0]['href'] ); + $this->assertNotEmpty( $links, 'The links should not be empty in the response data.' ); + $expected = rest_url( 'wp/v2/font-families/' . $post->post_parent . '/font-faces/' . $post->ID ); + $this->assertSame( $expected, $links['self'][0]['href'], 'The links URL from the response data should match the post\'s REST endpoint.' ); + $expected = rest_url( 'wp/v2/font-families/' . $post->post_parent . '/font-faces' ); + $this->assertSame( $expected, $links['collection'][0]['href'], 'The links collection URL from the response data should match the REST endpoint.' ); + $expected = rest_url( 'wp/v2/font-families/' . $post->post_parent ); + $this->assertSame( $expected, $links['parent'][0]['href'], 'The links for a parent URL from the response data should match the parent\'s REST endpoint.' ); } protected function check_file_meta( $font_face_id, $srcs ) { diff --git a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php index e9a376c774a6dd..29e56bb4a0c25d 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php @@ -166,9 +166,10 @@ public function test_get_context_param( $single_route ) { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertArrayNotHasKey( 'allow_batch', $data['endpoints'][0] ); - $this->assertSame( 'view', $data['endpoints'][0]['args']['context']['default'] ); - $this->assertSame( array( 'view', 'embed', 'edit' ), $data['endpoints'][0]['args']['context']['enum'] ); + $endpoint_data = $data['endpoints'][0]; + $this->assertArrayNotHasKey( 'allow_batch', $endpoint_data, 'The allow_batch property should not exist in the endpoint data.' ); + $this->assertSame( 'view', $endpoint_data['args']['context']['default'], 'The endpoint\'s args::context::default should be set to view.' ); + $this->assertSame( array( 'view', 'embed', 'edit' ), $endpoint_data['args']['context']['enum'], 'The endpoint\'s args::context::enum should be set to [ view, embed, edit ].' ); } /** @@ -192,11 +193,11 @@ public function test_get_items() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 200, $response->get_status() ); - $this->assertCount( 2, $data ); - $this->assertArrayHasKey( '_links', $data[0] ); + $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); + $this->assertCount( 2, $data, 'There should be 2 properties in the response data.' ); + $this->assertArrayHasKey( '_links', $data[0], 'The _links property should exist in the response data 0.' ); $this->check_font_family_data( $data[0], self::$font_family_id2, $data[0]['_links'] ); - $this->assertArrayHasKey( '_links', $data[1] ); + $this->assertArrayHasKey( '_links', $data[1], 'The _links property should exist in the response data 1.' ); $this->check_font_family_data( $data[1], self::$font_family_id1, $data[1]['_links'] ); } @@ -212,9 +213,10 @@ public function test_get_items_by_slug() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 200, $response->get_status() ); - $this->assertCount( 1, $data ); - $this->assertSame( $font_family->ID, $data[0]['id'] ); + $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); + $this->assertCount( 1, $data, 'There should be 2 properties in the response data.' ); + $this->assertArrayHasKey( 'id', $data[0], 'The id property should exist in the response data.' ); + $this->assertSame( $font_family->ID, $data[0]['id'], 'The id should match the expected ID in the response data.' ); } /** @@ -224,12 +226,12 @@ public function test_get_items_no_permission() { wp_set_current_user( 0 ); $request = new WP_REST_Request( 'GET', '/wp/v2/font-families' ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_read', $response, 401 ); + $this->assertErrorResponse( 'rest_cannot_read', $response, 401, 'The response should return an error with a "rest_cannot_read" code and 401 status.' ); wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'GET', '/wp/v2/font-families' ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_read', $response, 403 ); + $this->assertErrorResponse( 'rest_cannot_read', $response, 403, 'The response should return an error with a "rest_cannot_read" code and 403 status.' ); } /** @@ -241,7 +243,7 @@ public function test_get_item() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); $this->check_font_family_data( $data, self::$font_family_id1, $response->get_links() ); } @@ -255,19 +257,19 @@ public function test_get_item_embedded_font_faces() { $response = rest_get_server()->dispatch( $request ); $data = rest_get_server()->response_to_data( $response, true ); - $this->assertSame( 200, $response->get_status() ); - $this->assertArrayHasKey( '_embedded', $data ); - $this->assertArrayHasKey( 'font_faces', $data['_embedded'] ); - $this->assertCount( 2, $data['_embedded']['font_faces'] ); + $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); + $this->assertArrayHasKey( '_embedded', $data, 'The _embedded property should exist in the response data.' ); + $this->assertArrayHasKey( 'font_faces', $data['_embedded'], 'The font_faces property should exist in _embedded data.' ); + $this->assertCount( 2, $data['_embedded']['font_faces'], 'There should be 2 font_faces in the _embedded data.' ); foreach ( $data['_embedded']['font_faces'] as $font_face ) { - $this->assertArrayHasKey( 'id', $font_face ); + $this->assertArrayHasKey( 'id', $font_face, 'The id property should exist in the _embedded font_face data.' ); $font_face_request = new WP_REST_Request( 'GET', '/wp/v2/font-families/' . self::$font_family_id1 . '/font-faces/' . $font_face['id'] ); $font_face_response = rest_get_server()->dispatch( $font_face_request ); $font_face_data = rest_get_server()->response_to_data( $font_face_response, true ); - $this->assertSame( $font_face_data, $font_face ); + $this->assertSame( $font_face_data, $font_face, 'The embedded font_face data should match when the data from a single request' ); } } @@ -282,8 +284,8 @@ public function test_get_item_removes_extra_settings() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 200, $response->get_status() ); - $this->assertArrayNotHasKey( 'fontFace', $data['font_family_settings'] ); + $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); + $this->assertArrayNotHasKey( 'fontFace', $data['font_family_settings'], 'The fontFace property should not exist in the font_family_settings data.' ); wp_delete_post( $font_family_id, true ); } @@ -313,8 +315,8 @@ public function test_get_item_malformed_post_content_returns_empty_settings() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 200, $response->get_status() ); - $this->assertSame( $empty_settings, $data['font_family_settings'] ); + $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); + $this->assertSame( $empty_settings, $data['font_family_settings'], 'The empty settings should exist in the font_family_settings data.' ); wp_delete_post( $font_family_id, true ); } @@ -337,11 +339,11 @@ public function test_get_item_no_permission() { $request = new WP_REST_Request( 'GET', '/wp/v2/font-families/' . self::$font_family_id1 ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_read', $response, 401 ); + $this->assertErrorResponse( 'rest_cannot_read', $response, 401, 'The response should return an error with a "rest_cannot_read" code and 401 status.' ); wp_set_current_user( self::$editor_id ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_read', $response, 403 ); + $this->assertErrorResponse( 'rest_cannot_read', $response, 403, 'The response should return an error with a "rest_cannot_read" code and 403 status.' ); } /** @@ -357,12 +359,12 @@ public function test_create_item() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 201, $response->get_status() ); + $this->assertSame( 201, $response->get_status(), 'The response status should be 201.' ); $this->check_font_family_data( $data, $data['id'], $response->get_links() ); $reponse_settings = $data['font_family_settings']; - $this->assertSame( $settings, $reponse_settings ); - $this->assertEmpty( $data['font_faces'] ); + $this->assertSame( $settings, $reponse_settings, 'The expected settings should exist in the font_family_settings data.' ); + $this->assertEmpty( $data['font_faces'], 'The font_faces should be empty or not exist in the response data.' ); wp_delete_post( $data['id'], true ); } @@ -379,8 +381,8 @@ public function test_create_item_default_theme_json_version() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 201, $response->get_status() ); - $this->assertArrayHasKey( 'theme_json_version', $data ); + $this->assertSame( 201, $response->get_status(), 'The response status should be 201.' ); + $this->assertArrayHasKey( 'theme_json_version', $data, 'The theme_json_version property should exist in the response data.' ); $this->assertSame( 2, $data['theme_json_version'], 'The default theme.json version should be 2.' ); wp_delete_post( $data['id'], true ); @@ -430,10 +432,10 @@ public function test_create_item_with_default_preview( $settings ) { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 201, $response->get_status() ); + $this->assertSame( 201, $response->get_status(), 'The response status should be 201.' ); $response_settings = $data['font_family_settings']; - $this->assertArrayHasKey( 'preview', $response_settings ); - $this->assertSame( '', $response_settings['preview'] ); + $this->assertArrayHasKey( 'preview', $response_settings, 'The preview property should exist in the font_family_settings data.' ); + $this->assertSame( '', $response_settings['preview'], 'The preview data should be an empty string.' ); wp_delete_post( $data['id'], true ); } @@ -524,10 +526,10 @@ public function test_create_item_invalid_settings_json() { $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); + $this->assertErrorResponse( 'rest_invalid_param', $response, 400, 'The response should return an error for "rest_invalid_param" with 400 status.' ); $expected_message = 'font_family_settings parameter must be a valid JSON string.'; $message = $response->as_error()->get_all_error_data()[0]['params']['font_family_settings']; - $this->assertSame( $expected_message, $message ); + $this->assertSame( $expected_message, $message, 'The response error message should match.' ); } /** @@ -541,10 +543,10 @@ public function test_create_item_with_duplicate_slug() { $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_duplicate_font_family', $response, 400 ); + $this->assertErrorResponse( 'rest_duplicate_font_family', $response, 400, 'The response should return an error for "rest_duplicate_font_family" with 400 status.' ); $expected_message = 'A font family with slug "helvetica" already exists.'; $message = $response->as_error()->get_error_messages()[0]; - $this->assertSame( $expected_message, $message ); + $this->assertSame( $expected_message, $message, 'The response error message should match.' ); } /** @@ -556,7 +558,7 @@ public function test_create_item_no_permission() { $request = new WP_REST_Request( 'POST', '/wp/v2/font-families' ); $request->set_param( 'font_family_settings', wp_json_encode( $settings ) ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_create', $response, 401 ); + $this->assertErrorResponse( 'rest_cannot_create', $response, 401, 'The response should return an error for "rest_cannot_create" with 401 status.' ); wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/font-families' ); @@ -572,7 +574,7 @@ public function test_create_item_no_permission() { ) ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_create', $response, 403 ); + $this->assertErrorResponse( 'rest_cannot_create', $response, 403, 'The response should return an error for "rest_cannot_create" with 403 status.' ); } /** @@ -596,7 +598,7 @@ public function test_update_item() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); $this->check_font_family_data( $data, $font_family_id, $response->get_links() ); $expected_settings = array( @@ -605,7 +607,7 @@ public function test_update_item() { 'fontFamily' => $settings['fontFamily'], 'preview' => $settings['preview'], ); - $this->assertSame( $expected_settings, $data['font_family_settings'] ); + $this->assertSame( $expected_settings, $data['font_family_settings'], 'The response font_family_settings should match expected settings.' ); wp_delete_post( $font_family_id, true ); } @@ -626,11 +628,11 @@ public function test_update_item_individual_settings( $settings ) { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); $key = key( $settings ); $value = current( $settings ); - $this->assertArrayHasKey( $key, $data['font_family_settings'] ); - $this->assertSame( $value, $data['font_family_settings'][ $key ] ); + $this->assertArrayHasKey( $key, $data['font_family_settings'], 'The expected key should exist in the font_family_settings data.' ); + $this->assertSame( $value, $data['font_family_settings'][ $key ], 'The font_family_settings data should match.' ); wp_delete_post( $font_family_id, true ); } @@ -667,8 +669,8 @@ public function test_update_item_santize_font_family( $font_family_setting, $exp $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 200, $response->get_status() ); - $this->assertSame( $expected, $data['font_family_settings']['fontFamily'] ); + $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); + $this->assertSame( $expected, $data['font_family_settings']['fontFamily'], 'The font family should match.' ); wp_delete_post( $font_family_id, true ); } @@ -747,10 +749,10 @@ public function test_update_item_update_slug_not_allowed() { ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_invalid_param', $response, 400 ); + $this->assertErrorResponse( 'rest_invalid_param', $response, 400, 'The response should return an error for "rest_invalid_param" with 400 status.' ); $expected_message = 'font_family_settings[slug] cannot be updated.'; $message = $response->as_error()->get_all_error_data()[0]['params']['font_family_settings']; - $this->assertSame( $expected_message, $message ); + $this->assertSame( $expected_message, $message, 'The response error message should match.' ); } /** @@ -763,7 +765,7 @@ public function test_update_item_invalid_font_family_id() { $request = new WP_REST_Request( 'POST', '/wp/v2/font-families/' . REST_TESTS_IMPOSSIBLY_HIGH_NUMBER ); $request->set_param( 'font_family_settings', wp_json_encode( $settings ) ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_post_invalid_id', $response, 404 ); + $this->assertErrorResponse( 'rest_post_invalid_id', $response, 404, 'The response should return an error for "rest_post_invalid_id" with 404 status.' ); } /** @@ -776,13 +778,13 @@ public function test_update_item_no_permission() { $request = new WP_REST_Request( 'POST', '/wp/v2/font-families/' . self::$font_family_id1 ); $request->set_param( 'font_family_settings', wp_json_encode( $settings ) ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_edit', $response, 401 ); + $this->assertErrorResponse( 'rest_cannot_edit', $response, 401, 'The response should return an error for "rest_cannot_edit" with 401 status for an invalid user.' ); wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/font-families/' . self::$font_family_id1 ); $request->set_param( 'font_family_settings', wp_json_encode( $settings ) ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_edit', $response, 403 ); + $this->assertErrorResponse( 'rest_cannot_edit', $response, 403, 'The response should return an error for "rest_cannot_edit" with 403 status for a user without permission.' ); } @@ -796,8 +798,8 @@ public function test_delete_item() { $request['force'] = true; $response = rest_get_server()->dispatch( $request ); - $this->assertSame( 200, $response->get_status() ); - $this->assertNull( get_post( $font_family_id ) ); + $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); + $this->assertNull( get_post( $font_family_id ), 'The post should not exist after deleting.' ); } /** @@ -810,15 +812,15 @@ public function test_delete_item_no_trash() { // Attempt trashing. $request = new WP_REST_Request( 'DELETE', '/wp/v2/font-families/' . $font_family_id ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); + $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501, 'The response should return an error for "rest_trash_not_supported" with 501 status.' ); $request->set_param( 'force', 'false' ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501 ); + $this->assertErrorResponse( 'rest_trash_not_supported', $response, 501, 'When "force" is false, the response should return an error for "rest_trash_not_supported" with 501 status.' ); // Ensure the post still exists. $post = get_post( $font_family_id ); - $this->assertNotEmpty( $post ); + $this->assertNotEmpty( $post, 'The post should still exists.' ); wp_delete_post( $font_family_id, true ); } @@ -842,12 +844,12 @@ public function test_delete_item_no_permissions() { wp_set_current_user( 0 ); $request = new WP_REST_Request( 'DELETE', '/wp/v2/font-families/' . $font_family_id ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_delete', $response, 401 ); + $this->assertErrorResponse( 'rest_cannot_delete', $response, 401, 'The response should return an error for "rest_cannot_delete" with 401 status for an invalid user.' ); wp_set_current_user( self::$editor_id ); $request = new WP_REST_Request( 'DELETE', '/wp/v2/font-families/' . $font_family_id ); $response = rest_get_server()->dispatch( $request ); - $this->assertErrorResponse( 'rest_cannot_delete', $response, 403 ); + $this->assertErrorResponse( 'rest_cannot_delete', $response, 403, 'The response should return an error for "rest_cannot_delete" with 403 status for a user without permission.' ); wp_delete_post( $font_family_id, true ); } @@ -861,7 +863,7 @@ public function test_prepare_item() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); $this->check_font_family_data( $data, self::$font_family_id2, $response->get_links() ); } @@ -873,23 +875,23 @@ public function test_get_item_schema() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); - $this->assertSame( 200, $response->get_status() ); + $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); $properties = $data['schema']['properties']; - $this->assertCount( 4, $properties ); - $this->assertArrayHasKey( 'id', $properties ); - $this->assertArrayHasKey( 'theme_json_version', $properties ); - $this->assertArrayHasKey( 'font_faces', $properties ); - $this->assertArrayHasKey( 'font_family_settings', $properties ); + $this->assertCount( 4, $properties, 'There should be 4 properties in the schema::properties data' ); + $this->assertArrayHasKey( 'id', $properties, 'The id property should exist in the schema::properties data' ); + $this->assertArrayHasKey( 'theme_json_version', $properties, 'The theme_json_version property should exist in the schema::properties data' ); + $this->assertArrayHasKey( 'font_faces', $properties, 'The font_faces property should exist in the schema::properties data' ); + $this->assertArrayHasKey( 'font_family_settings', $properties, 'The font_family_settings property should exist in the schema::properties data' ); } protected function check_font_family_data( $data, $post_id, $links ) { $post = get_post( $post_id ); - $this->assertArrayHasKey( 'id', $data ); - $this->assertSame( $post->ID, $data['id'] ); + $this->assertArrayHasKey( 'id', $data, 'The id property should exist in response data.' ); + $this->assertSame( $post->ID, $data['id'], 'The "id" from the response data should match the post ID.' ); - $this->assertArrayHasKey( 'theme_json_version', $data ); - $this->assertSame( WP_Theme_JSON::LATEST_SCHEMA, $data['theme_json_version'] ); + $this->assertArrayHasKey( 'theme_json_version', $data, 'The theme_json_version property should exist in response data.' ); + $this->assertSame( WP_Theme_JSON::LATEST_SCHEMA, $data['theme_json_version'], 'The "theme_json_version" from the response data should match WP_Theme_JSON::LATEST_SCHEMA.' ); $font_face_ids = get_children( array( @@ -900,13 +902,13 @@ protected function check_font_family_data( $data, $post_id, $links ) { 'orderby' => 'ID', ) ); - $this->assertArrayHasKey( 'font_faces', $data ); + $this->assertArrayHasKey( 'font_faces', $data, 'The font_faces property should exist in the response data.' ); foreach ( $font_face_ids as $font_face_id ) { - $this->assertContains( $font_face_id, $data['font_faces'] ); + $this->assertContains( $font_face_id, $data['font_faces'], 'The ID is in the font_faces data.' ); } - $this->assertArrayHasKey( 'font_family_settings', $data ); + $this->assertArrayHasKey( 'font_family_settings', $data, 'The font_family_settings property should exist in the response data.' ); $settings = $data['font_family_settings']; $expected_settings = array( 'name' => $post->post_title, @@ -914,11 +916,13 @@ protected function check_font_family_data( $data, $post_id, $links ) { 'fontFamily' => $settings['fontFamily'], 'preview' => $settings['preview'], ); - $this->assertSame( $expected_settings, $settings ); + $this->assertSame( $expected_settings, $settings, 'The font_family_settings should match.' ); - $this->assertNotEmpty( $links ); - $this->assertSame( rest_url( 'wp/v2/font-families/' . $post->ID ), $links['self'][0]['href'] ); - $this->assertSame( rest_url( 'wp/v2/font-families' ), $links['collection'][0]['href'] ); + $this->assertNotEmpty( $links, 'The links should not be empty in the response data.' ); + $expected = rest_url( 'wp/v2/font-families/' . $post->ID ); + $this->assertSame( $expected, $links['self'][0]['href'], 'The links URL from the response data should match the post\'s REST endpoint.' ); + $expected = rest_url( 'wp/v2/font-families' ); + $this->assertSame( $expected, $links['collection'][0]['href'], 'The links collection URL from the response data should match the REST endpoint.' ); if ( ! $font_face_ids ) { return; @@ -927,12 +931,13 @@ protected function check_font_family_data( $data, $post_id, $links ) { // Check font_face links, if present. $this->assertArrayHasKey( 'font_faces', $links ); foreach ( $links['font_faces'] as $index => $link ) { - $this->assertSame( rest_url( 'wp/v2/font-families/' . $post->ID . '/font-faces/' . $font_face_ids[ $index ] ), $link['href'] ); + $expected = rest_url( 'wp/v2/font-families/' . $post->ID . '/font-faces/' . $font_face_ids[ $index ] ); + $this->assertSame( $expected, $link['href'], 'The links for a font faces URL from the response data should match the REST endpoint.' ); $embeddable = isset( $link['attributes']['embeddable'] ) ? $link['attributes']['embeddable'] : $link['embeddable']; - $this->assertTrue( $embeddable ); + $this->assertTrue( $embeddable, 'The embeddable should be true.' ); } } } From 58059dfdde4cb36a39a2f44538502e2fe512805e Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Thu, 1 Feb 2024 09:54:33 -0600 Subject: [PATCH 08/11] Reorder resets / cleanup. Resets are things like deleting posts, removing a filter/action, etc. These need to occur _before_ an assertion. Why? If a reset is after an assertion and any of the assertions before it fail, the test method bails out and the reset does not happen. To ensure resets happen, they need to be located in either: * tear_down() fixture. * just after invoking the thing under test but before the assertion(s). --- .../fontFamilyBackwardsCompatibility.php | 57 ++++++++++++++----- .../wpFontCollection/loadFromJson.php | 16 +++--- .../tests/fonts/font-library/wpFontsDir.php | 4 +- .../wpRestFontFacesController.php | 48 ++++++++++------ .../wpRestFontFamiliesController.php | 53 ++++++++++------- 5 files changed, 114 insertions(+), 64 deletions(-) diff --git a/phpunit/tests/fonts/font-library/fontFamilyBackwardsCompatibility.php b/phpunit/tests/fonts/font-library/fontFamilyBackwardsCompatibility.php index 2f7e2d2db2a194..a51cf094046a52 100644 --- a/phpunit/tests/fonts/font-library/fontFamilyBackwardsCompatibility.php +++ b/phpunit/tests/fonts/font-library/fontFamilyBackwardsCompatibility.php @@ -8,16 +8,23 @@ * @subpackage Font Library */ class Tests_Font_Family_Backwards_Compatibility extends WP_UnitTestCase { + private $post_ids_to_delete = array(); + public function set_up() { parent::set_up(); + $this->post_ids_to_delete = array(); delete_option( 'gutenberg_font_family_format_converted' ); } public function tear_down() { - parent::tear_down(); + foreach ( $this->post_ids_to_delete as $post_id ) { + wp_delete_post( $post_id, true ); + } delete_option( 'gutenberg_font_family_format_converted' ); + + parent::tear_down(); } public function test_font_faces_with_remote_src() { @@ -27,7 +34,7 @@ public function test_font_faces_with_remote_src() { gutenberg_convert_legacy_font_family_format(); - $font_family = get_post( $font_family_id ); + $font_family = $this->get_post( $font_family_id ); $font_faces = $this->get_font_faces( $font_family_id ); list( $font_face1, $font_face2, $font_face3 ) = $font_faces; @@ -95,11 +102,6 @@ public function test_font_faces_with_remote_src() { // With a remote url, file post meta should not be set. $meta = get_post_meta( $font_face3->ID, '_wp_font_face_file', true ); $this->assertSame( '', $meta, 'The _wp_font_face_file post meta for the 3rd font face should be an empty string.' ); - - wp_delete_post( $font_family_id, true ); - wp_delete_post( $font_face1->ID, true ); - wp_delete_post( $font_face2->ID, true ); - wp_delete_post( $font_face3->ID, true ); } public function test_font_faces_with_local_src() { @@ -110,6 +112,7 @@ public function test_font_faces_with_local_src() { gutenberg_convert_legacy_font_family_format(); $font_faces = $this->get_font_faces( $font_family_id ); + $this->assertCount( 1, $font_faces, 'There should be 1 font face.' ); $font_face = reset( $font_faces ); @@ -117,9 +120,6 @@ public function test_font_faces_with_local_src() { $file_path = 'open-sans_normal_400.ttf'; $meta = get_post_meta( $font_face->ID, '_wp_font_face_file', true ); $this->assertSame( $file_path, $meta, 'The _wp_font_face_file should match.' ); - - wp_delete_post( $font_family_id, true ); - wp_delete_post( $font_face->ID, true ); } public function test_migration_only_runs_once() { @@ -135,12 +135,10 @@ public function test_migration_only_runs_once() { // Meta with backup content will not be present if migration isn't triggered. $meta = get_post_meta( $font_family_id, '_gutenberg_legacy_font_family', true ); $this->assertSame( '', $meta ); - - wp_delete_post( $font_family_id, true ); } protected function create_font_family( $content ) { - return wp_insert_post( + $post_id = wp_insert_post( array( 'post_type' => 'wp_font_family', 'post_status' => 'publish', @@ -149,10 +147,22 @@ protected function create_font_family( $content ) { 'post_content' => $content, ) ); + + $this->store_id_for_cleanup_in_teardown( $post_id ); + + return $post_id; + } + + private function get_post( $post_id ) { + $post = get_post( $post_id ); + + $this->store_id_for_cleanup_in_teardown( $post ); + + return $post; } protected function get_font_faces( $font_family_id ) { - return get_posts( + $posts = get_posts( array( 'post_parent' => $font_family_id, 'post_type' => 'wp_font_face', @@ -160,5 +170,24 @@ protected function get_font_faces( $font_family_id ) { 'orderby' => 'id', ) ); + + $this->store_id_for_cleanup_in_teardown( $posts ); + + return $posts; + } + + private function store_id_for_cleanup_in_teardown( $post ) { + if ( null === $post ) { + return; + } + + $posts = is_array( $post ) ? $post : array( $post ); + + foreach ( $posts as $post ) { + if ( null === $post ) { + continue; + } + $this->post_ids_to_delete[] = is_int( $post ) ? $post : $post->ID; + } } } diff --git a/phpunit/tests/fonts/font-library/wpFontCollection/loadFromJson.php b/phpunit/tests/fonts/font-library/wpFontCollection/loadFromJson.php index d3ee04eba29f5f..6c7b3bd23a6f3e 100644 --- a/phpunit/tests/fonts/font-library/wpFontCollection/loadFromJson.php +++ b/phpunit/tests/fonts/font-library/wpFontCollection/loadFromJson.php @@ -31,9 +31,9 @@ public function test_should_load_json_from_url() { add_filter( 'pre_http_request', array( $this, 'mock_request' ), 10, 3 ); $config = WP_Font_Collection::load_from_json( 'https://localhost/fonts/mock-font-collection.json' ); - $this->assertSame( self::$mock_collection_data, $config ); - remove_filter( 'pre_http_request', array( $this, 'mock_request' ) ); + + $this->assertSame( self::$mock_collection_data, $config ); } public function test_should_error_with_invalid_file_path() { @@ -54,9 +54,9 @@ public function test_should_error_with_invalid_url_response() { add_filter( 'pre_http_request', array( $this, 'mock_request_invalid_response' ), 10, 3 ); $config = WP_Font_Collection::load_from_json( 'https://localhost/fonts/missing-collection.json' ); - $this->assertWPError( $config, 'font_collection_json_missing' ); - remove_filter( 'pre_http_request', array( $this, 'mock_request_invalid_response' ) ); + + $this->assertWPError( $config, 'font_collection_json_missing' ); } public function test_should_error_with_invalid_json_from_file() { @@ -72,9 +72,9 @@ public function test_should_error_with_invalid_json_from_url() { add_filter( 'pre_http_request', array( $this, 'mock_request_invalid_json' ), 10, 3 ); $config = WP_Font_Collection::load_from_json( 'https://localhost/fonts/invalid-collection.json' ); - $this->assertWPError( $config, 'font_collection_decode_error' ); - remove_filter( 'pre_http_request', array( $this, 'mock_request_invalid_json' ) ); + + $this->assertWPError( $config, 'font_collection_decode_error' ); } public function test_should_error_with_json_from_file_missing_slug() { @@ -93,9 +93,9 @@ public function test_should_error_with_json_from_url_missing_slug() { $this->setExpectedIncorrectUsage( 'WP_Font_Collection::load_from_url' ); $config = WP_Font_Collection::load_from_json( 'https://localhost/fonts/missing-slug.json' ); - $this->assertWPError( $config, 'font_collection_invalid_json' ); - remove_filter( 'pre_http_request', array( $this, 'mock_request_missing_slug' ) ); + + $this->assertWPError( $config, 'font_collection_invalid_json' ); } public function mock_request( $preempt, $args, $url ) { diff --git a/phpunit/tests/fonts/font-library/wpFontsDir.php b/phpunit/tests/fonts/font-library/wpFontsDir.php index a7c1c326d62a2b..e88add735b2d05 100644 --- a/phpunit/tests/fonts/font-library/wpFontsDir.php +++ b/phpunit/tests/fonts/font-library/wpFontsDir.php @@ -59,11 +59,11 @@ function set_new_values( $defaults ) { 'error' => false, ); - $this->assertSame( $font_dir, $expected, 'The wp_get_font_dir() method should return the expected values.' ); - // Remove the filter. remove_filter( 'font_dir', 'set_new_values' ); + $this->assertSame( $font_dir, $expected, 'The wp_get_font_dir() method should return the expected values.' ); + // Gets the fonts dir. $font_dir = wp_get_font_dir(); diff --git a/phpunit/tests/fonts/font-library/wpRestFontFacesController.php b/phpunit/tests/fonts/font-library/wpRestFontFacesController.php index 52b99629645973..277eb4dea761f6 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontFacesController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontFacesController.php @@ -20,6 +20,8 @@ class Tests_REST_WpRestFontFacesController extends WP_Test_REST_Controller_Testc protected static $font_face_id1; protected static $font_face_id2; + private static $post_ids_for_cleanup = array(); + protected static $default_settings = array( 'fontFamily' => '"Open Sans"', 'fontWeight' => '400', @@ -60,17 +62,32 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 'role' => 'editor', ) ); + + self::$post_ids_for_cleanup = array(); } public static function wpTearDownAfterClass() { self::delete_user( self::$admin_id ); self::delete_user( self::$editor_id ); + + wp_delete_post( self::$font_family_id, true ); + wp_delete_post( self::$other_font_family_id, true ); + wp_delete_post( self::$font_face_id1, true ); + wp_delete_post( self::$font_face_id2, true ); + } + + public function tear_down() { + foreach ( self::$post_ids_for_cleanup as $post_id ) { + wp_delete_post( $post_id, true ); + } + self::$post_ids_for_cleanup = array(); + parent::tear_down(); } public static function create_font_face_post( $parent_id, $settings = array() ) { $settings = array_merge( self::$default_settings, $settings ); $title = WP_Font_Utils::get_font_face_slug( $settings ); - return self::factory()->post->create( + $post_id = self::factory()->post->create( wp_slash( array( 'post_type' => 'wp_font_face', @@ -82,6 +99,10 @@ public static function create_font_face_post( $parent_id, $settings = array() ) ) ) ); + + self::$post_ids_for_cleanup[] = $post_id; + + return $post_id; } /** @@ -252,6 +273,8 @@ public function test_get_item_malformed_post_content_returns_empty_settings() { ) ); + self::$post_ids_for_cleanup[] = $font_face_id; + $empty_settings = array( 'fontFamily' => '', 'src' => array(), @@ -265,8 +288,6 @@ public function test_get_item_malformed_post_content_returns_empty_settings() { $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); $this->assertArrayHasKey( 'font_face_settings', $data, 'The font_face_settings property should exist in the response data.' ); $this->assertSame( $empty_settings, $data['font_face_settings'], 'The empty settings should exist in the font_face_settings data.' ); - - wp_delete_post( $font_face_id, true ); } /** @@ -373,8 +394,6 @@ public function test_create_item() { ); $this->assertSame( self::$font_family_id, $data['parent'], 'The returned parent id should match the font family id.' ); - - wp_delete_post( $data['id'], true ); } /** @@ -408,8 +427,6 @@ public function test_create_item_with_multiple_font_files() { $settings = $data['font_face_settings']; $this->assertCount( 4, $settings['src'], 'There should be 4 items in the font_face_settings::src data.' ); - - wp_delete_post( $data['id'], true ); } /** @@ -477,8 +494,6 @@ public function test_create_item_with_url_src() { $this->assertSame( 201, $response->get_status(), 'The response status should be 201.' ); $this->check_font_face_data( $data, $data['id'], $response->get_links() ); - - wp_delete_post( $data['id'], true ); } /** @@ -511,12 +526,11 @@ public function test_create_item_with_all_properties() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); + wp_delete_post( $data['id'], true ); $this->assertSame( 201, $response->get_status(), 'The response status should be 201.' ); $this->assertArrayHasKey( 'font_face_settings', $data, 'The font_face_settings property should exist in the response data.' ); $this->assertSame( $properties, $data['font_face_settings'], 'The font_face_settings should match the expected properties.' ); - - wp_delete_post( $data['id'], true ); } /** @@ -538,13 +552,13 @@ public function test_create_item_missing_parent() { * @covers WP_REST_Font_Faces_Controller::create_item */ public function test_create_item_with_duplicate_properties() { - $settings = array( + $settings = array( 'fontFamily' => '"Open Sans"', 'fontWeight' => '200', 'fontStyle' => 'italic', 'src' => home_url( '/wp-content/fonts/open-sans-italic-light.ttf' ), ); - $font_face_id = self::create_font_face_post( self::$font_family_id, $settings ); + self::create_font_face_post( self::$font_family_id, $settings ); wp_set_current_user( self::$admin_id ); $request = new WP_REST_Request( 'POST', '/wp/v2/font-families/' . self::$font_family_id . '/font-faces' ); @@ -556,8 +570,6 @@ public function test_create_item_with_duplicate_properties() { $expected_message = 'A font face matching those settings already exists.'; $message = $response->as_error()->get_error_messages()[0]; $this->assertSame( $expected_message, $message, 'The response error message should match.' ); - - wp_delete_post( $font_face_id, true ); } /** @@ -579,12 +591,11 @@ public function test_create_item_default_theme_json_version() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); + wp_delete_post( $data['id'], true ); $this->assertSame( 201, $response->get_status(), 'The response status should be 201.' ); $this->assertArrayHasKey( 'theme_json_version', $data, 'The theme_json_version property should exist in the response data.' ); $this->assertSame( 2, $data['theme_json_version'], 'The default theme.json version should be 2.' ); - - wp_delete_post( $data['id'], true ); } /** @@ -893,7 +904,8 @@ public function test_get_item_schema() { } protected function check_font_face_data( $data, $post_id, $links ) { - $post = get_post( $post_id ); + self::$post_ids_for_cleanup[] = $post_id; + $post = get_post( $post_id ); $this->assertArrayHasKey( 'id', $data, 'The id property should exist in response data.' ); $this->assertSame( $post->ID, $data['id'], 'The "id" from the response data should match the post ID.' ); diff --git a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php index 29e56bb4a0c25d..8deac60ee985ca 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php @@ -20,6 +20,8 @@ class Tests_REST_WpRestFontFamiliesController extends WP_Test_REST_Controller_Te protected static $font_face_id1; protected static $font_face_id2; + private static $post_ids_to_cleanup = array(); + protected static $default_settings = array( 'name' => 'Open Sans', 'slug' => 'open-sans', @@ -72,16 +74,32 @@ public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { 'src' => home_url( '/wp-content/fonts/open-sans-bold.ttf' ), ) ); + + static::$post_ids_to_cleanup = array(); } public static function wpTearDownAfterClass() { self::delete_user( self::$admin_id ); self::delete_user( self::$editor_id ); + + wp_delete_post( self::$font_family_id1 ); + wp_delete_post( self::$font_family_id2 ); + wp_delete_post( self::$font_face_id1 ); + wp_delete_post( self::$font_face_id2 ); + } + + public function tear_down() { + foreach ( static::$post_ids_to_cleanup as $post_id ) { + wp_delete_post( $post_id, true ); + } + static::$post_ids_to_cleanup = array(); + + parent::tear_down(); } public static function create_font_family_post( $settings = array() ) { $settings = array_merge( self::$default_settings, $settings ); - return self::factory()->post->create( + $post_id = self::factory()->post->create( wp_slash( array( 'post_type' => 'wp_font_family', @@ -97,6 +115,10 @@ public static function create_font_family_post( $settings = array() ) { ) ) ); + + static::$post_ids_to_cleanup[] = $post_id; + + return $post_id; } /** @@ -286,8 +308,6 @@ public function test_get_item_removes_extra_settings() { $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); $this->assertArrayNotHasKey( 'fontFace', $data['font_family_settings'], 'The fontFace property should not exist in the font_family_settings data.' ); - - wp_delete_post( $font_family_id, true ); } /** @@ -302,6 +322,8 @@ public function test_get_item_malformed_post_content_returns_empty_settings() { ) ); + static::$post_ids_to_cleanup[] = $font_family_id; + $empty_settings = array( 'name' => '', // Slug will default to the post id. @@ -317,8 +339,6 @@ public function test_get_item_malformed_post_content_returns_empty_settings() { $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); $this->assertSame( $empty_settings, $data['font_family_settings'], 'The empty settings should exist in the font_family_settings data.' ); - - wp_delete_post( $font_family_id, true ); } /** @@ -365,8 +385,6 @@ public function test_create_item() { $reponse_settings = $data['font_family_settings']; $this->assertSame( $settings, $reponse_settings, 'The expected settings should exist in the font_family_settings data.' ); $this->assertEmpty( $data['font_faces'], 'The font_faces should be empty or not exist in the response data.' ); - - wp_delete_post( $data['id'], true ); } /** @@ -381,11 +399,11 @@ public function test_create_item_default_theme_json_version() { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); + static::$post_ids_to_cleanup[] = $data['id']; + $this->assertSame( 201, $response->get_status(), 'The response status should be 201.' ); $this->assertArrayHasKey( 'theme_json_version', $data, 'The theme_json_version property should exist in the response data.' ); $this->assertSame( 2, $data['theme_json_version'], 'The default theme.json version should be 2.' ); - - wp_delete_post( $data['id'], true ); } /** @@ -432,12 +450,12 @@ public function test_create_item_with_default_preview( $settings ) { $response = rest_get_server()->dispatch( $request ); $data = $response->get_data(); + static::$post_ids_to_cleanup[] = $data['id']; + $this->assertSame( 201, $response->get_status(), 'The response status should be 201.' ); $response_settings = $data['font_family_settings']; $this->assertArrayHasKey( 'preview', $response_settings, 'The preview property should exist in the font_family_settings data.' ); $this->assertSame( '', $response_settings['preview'], 'The preview data should be an empty string.' ); - - wp_delete_post( $data['id'], true ); } /** @@ -608,8 +626,6 @@ public function test_update_item() { 'preview' => $settings['preview'], ); $this->assertSame( $expected_settings, $data['font_family_settings'], 'The response font_family_settings should match expected settings.' ); - - wp_delete_post( $font_family_id, true ); } /** @@ -633,8 +649,6 @@ public function test_update_item_individual_settings( $settings ) { $value = current( $settings ); $this->assertArrayHasKey( $key, $data['font_family_settings'], 'The expected key should exist in the font_family_settings data.' ); $this->assertSame( $value, $data['font_family_settings'][ $key ], 'The font_family_settings data should match.' ); - - wp_delete_post( $font_family_id, true ); } /** @@ -671,8 +685,6 @@ public function test_update_item_santize_font_family( $font_family_setting, $exp $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); $this->assertSame( $expected, $data['font_family_settings']['fontFamily'], 'The font family should match.' ); - - wp_delete_post( $font_family_id, true ); } /** @@ -821,8 +833,6 @@ public function test_delete_item_no_trash() { // Ensure the post still exists. $post = get_post( $font_family_id ); $this->assertNotEmpty( $post, 'The post should still exists.' ); - - wp_delete_post( $font_family_id, true ); } /** @@ -850,8 +860,6 @@ public function test_delete_item_no_permissions() { $request = new WP_REST_Request( 'DELETE', '/wp/v2/font-families/' . $font_family_id ); $response = rest_get_server()->dispatch( $request ); $this->assertErrorResponse( 'rest_cannot_delete', $response, 403, 'The response should return an error for "rest_cannot_delete" with 403 status for a user without permission.' ); - - wp_delete_post( $font_family_id, true ); } /** @@ -885,7 +893,8 @@ public function test_get_item_schema() { } protected function check_font_family_data( $data, $post_id, $links ) { - $post = get_post( $post_id ); + static::$post_ids_to_cleanup[] = $post_id; + $post = get_post( $post_id ); $this->assertArrayHasKey( 'id', $data, 'The id property should exist in response data.' ); $this->assertSame( $post->ID, $data['id'], 'The "id" from the response data should match the post ID.' ); From 4fe255ebebd69412c218e1f1ae07743cd09fa5f3 Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Thu, 1 Feb 2024 14:48:18 -0600 Subject: [PATCH 09/11] Put expected as first arg in each assert. Per the PHPUnit handbook: assertSomething( expected, actual, message ); --- .../tests/fonts/font-library/wpFontLibrary/getMimeTypes.php | 2 +- phpunit/tests/fonts/font-library/wpFontsDir.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php b/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php index f5e5fc64e611e1..4fb97e754611ff 100644 --- a/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php +++ b/phpunit/tests/fonts/font-library/wpFontLibrary/getMimeTypes.php @@ -20,7 +20,7 @@ class Tests_Fonts_WpFontLibrary_GetMimeTypes extends WP_Font_Library_UnitTestCas */ public function test_should_supply_correct_mime_type_for_php_version( $php_version_id, $expected ) { $mimes = WP_Font_Library::get_expected_font_mime_types_per_php_version( $php_version_id ); - $this->assertSame( $mimes, $expected ); + $this->assertSame( $expected, $mimes ); } /** diff --git a/phpunit/tests/fonts/font-library/wpFontsDir.php b/phpunit/tests/fonts/font-library/wpFontsDir.php index e88add735b2d05..a8f79888315bdf 100644 --- a/phpunit/tests/fonts/font-library/wpFontsDir.php +++ b/phpunit/tests/fonts/font-library/wpFontsDir.php @@ -62,11 +62,11 @@ function set_new_values( $defaults ) { // Remove the filter. remove_filter( 'font_dir', 'set_new_values' ); - $this->assertSame( $font_dir, $expected, 'The wp_get_font_dir() method should return the expected values.' ); + $this->assertSame( $expected, $font_dir, 'The wp_get_font_dir() method should return the expected values.' ); // Gets the fonts dir. $font_dir = wp_get_font_dir(); - $this->assertSame( $font_dir, static::$dir_defaults, 'The wp_get_font_dir() method should return the default values.' ); + $this->assertSame( static::$dir_defaults, $font_dir, 'The wp_get_font_dir() method should return the default values.' ); } } From d002d7b4ba6939c684187a4f829116b0f41392fe Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Thu, 1 Feb 2024 14:52:14 -0600 Subject: [PATCH 10/11] Add fonts and font-library group annotation. All of the test classes now have these groups: * @group fonts * @group font-library These annotations allow for specific groups of tests to be run, which is helping in local development. --- .../fonts/font-library/fontFamilyBackwardsCompatibility.php | 3 +++ .../tests/fonts/font-library/wpFontUtils/getFontFaceSlug.php | 5 ++++- .../fonts/font-library/wpRestFontCollectionsController.php | 2 ++ .../tests/fonts/font-library/wpRestFontFacesController.php | 2 ++ .../fonts/font-library/wpRestFontFamiliesController.php | 2 ++ 5 files changed, 13 insertions(+), 1 deletion(-) diff --git a/phpunit/tests/fonts/font-library/fontFamilyBackwardsCompatibility.php b/phpunit/tests/fonts/font-library/fontFamilyBackwardsCompatibility.php index a51cf094046a52..4305237d53292f 100644 --- a/phpunit/tests/fonts/font-library/fontFamilyBackwardsCompatibility.php +++ b/phpunit/tests/fonts/font-library/fontFamilyBackwardsCompatibility.php @@ -6,6 +6,9 @@ * * @package WordPress * @subpackage Font Library + * + * @group fonts + * @group font-library */ class Tests_Font_Family_Backwards_Compatibility extends WP_UnitTestCase { private $post_ids_to_delete = array(); diff --git a/phpunit/tests/fonts/font-library/wpFontUtils/getFontFaceSlug.php b/phpunit/tests/fonts/font-library/wpFontUtils/getFontFaceSlug.php index 32575d4bb19a28..de0b02e63185ed 100644 --- a/phpunit/tests/fonts/font-library/wpFontUtils/getFontFaceSlug.php +++ b/phpunit/tests/fonts/font-library/wpFontUtils/getFontFaceSlug.php @@ -4,7 +4,10 @@ * * @package WordPress * @subpackage Font Library - * * + * + * @group fonts + * @group font-library + * * @covers WP_Font_Utils::get_font_face_slug */ class Tests_Fonts_WpFontUtils_GetFontFaceSlug extends WP_UnitTestCase { diff --git a/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php b/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php index d73315c311ad7f..e6143da6da6464 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontCollectionsController.php @@ -7,6 +7,8 @@ * @since 6.5.0 * * @group restapi + * @group fonts + * @group font-library * * @coversDefaultClass WP_REST_Font_Collections_Controller */ diff --git a/phpunit/tests/fonts/font-library/wpRestFontFacesController.php b/phpunit/tests/fonts/font-library/wpRestFontFacesController.php index 277eb4dea761f6..de6ecc3d735146 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontFacesController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontFacesController.php @@ -7,6 +7,8 @@ * @since 6.5.0 * * @group restapi + * @group fonts + * @group font-library * * @coversDefaultClass WP_REST_Font_Faces_Controller */ diff --git a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php index 8deac60ee985ca..9d44f7e0c65fe7 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php @@ -7,6 +7,8 @@ * @since 6.5.0 * * @group restapi + * @group fonts + * @group font-library * * @coversDefaultClass WP_REST_Font_Families_Controller */ From 022869aaa9bf881c20aa0fc5adb8d3011b34a42c Mon Sep 17 00:00:00 2001 From: hellofromtonya Date: Thu, 1 Feb 2024 17:10:31 -0600 Subject: [PATCH 11/11] Messages: add endstop; single "exist" * Add endstop . for consistency. * Single form of "exist". Props creativecoder. --- .../tests/fonts/font-library/fontLibraryHooks.php | 4 ++-- .../font-library/wpRestFontFamiliesController.php | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/phpunit/tests/fonts/font-library/fontLibraryHooks.php b/phpunit/tests/fonts/font-library/fontLibraryHooks.php index eabc6876988d63..550f3992c9f73e 100644 --- a/phpunit/tests/fonts/font-library/fontLibraryHooks.php +++ b/phpunit/tests/fonts/font-library/fontLibraryHooks.php @@ -46,8 +46,8 @@ public function test_deleting_font_faces_deletes_associated_font_files() { wp_delete_post( $font_face_id, true ); - $this->assertFalse( file_exists( $font_path ), 'The font file should have been deleted when the post was deleted' ); - $this->assertTrue( file_exists( $other_font_path ), 'The other font file should exist' ); + $this->assertFalse( file_exists( $font_path ), 'The font file should have been deleted when the post was deleted.' ); + $this->assertTrue( file_exists( $other_font_path ), 'The other font file should exist.' ); } protected function create_font_face_with_file( $filename ) { diff --git a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php index 9d44f7e0c65fe7..6468eff7e24a42 100644 --- a/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php +++ b/phpunit/tests/fonts/font-library/wpRestFontFamiliesController.php @@ -293,7 +293,7 @@ public function test_get_item_embedded_font_faces() { $font_face_response = rest_get_server()->dispatch( $font_face_request ); $font_face_data = rest_get_server()->response_to_data( $font_face_response, true ); - $this->assertSame( $font_face_data, $font_face, 'The embedded font_face data should match when the data from a single request' ); + $this->assertSame( $font_face_data, $font_face, 'The embedded font_face data should match when the data from a single request.' ); } } @@ -834,7 +834,7 @@ public function test_delete_item_no_trash() { // Ensure the post still exists. $post = get_post( $font_family_id ); - $this->assertNotEmpty( $post, 'The post should still exists.' ); + $this->assertNotEmpty( $post, 'The post should still exist.' ); } /** @@ -887,11 +887,11 @@ public function test_get_item_schema() { $this->assertSame( 200, $response->get_status(), 'The response status should be 200.' ); $properties = $data['schema']['properties']; - $this->assertCount( 4, $properties, 'There should be 4 properties in the schema::properties data' ); - $this->assertArrayHasKey( 'id', $properties, 'The id property should exist in the schema::properties data' ); - $this->assertArrayHasKey( 'theme_json_version', $properties, 'The theme_json_version property should exist in the schema::properties data' ); - $this->assertArrayHasKey( 'font_faces', $properties, 'The font_faces property should exist in the schema::properties data' ); - $this->assertArrayHasKey( 'font_family_settings', $properties, 'The font_family_settings property should exist in the schema::properties data' ); + $this->assertCount( 4, $properties, 'There should be 4 properties in the schema::properties data.' ); + $this->assertArrayHasKey( 'id', $properties, 'The id property should exist in the schema::properties data.' ); + $this->assertArrayHasKey( 'theme_json_version', $properties, 'The theme_json_version property should exist in the schema::properties data.' ); + $this->assertArrayHasKey( 'font_faces', $properties, 'The font_faces property should exist in the schema::properties data.' ); + $this->assertArrayHasKey( 'font_family_settings', $properties, 'The font_family_settings property should exist in the schema::properties data.' ); } protected function check_font_family_data( $data, $post_id, $links ) {