From c181423e1f88dd59d4b8abe64be9799db4124424 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Thu, 18 Aug 2022 05:54:01 -0500 Subject: [PATCH] PHPUnit: turns on PHP notices and deprecations (#43102) --- phpunit.xml.dist | 1 + phpunit/blocks/render-last-posts-test.php | 41 ++++- phpunit/bootstrap.php | 13 ++ ...ass-block-library-navigation-link-test.php | 19 +++ phpunit/class-wp-theme-json-test.php | 149 +++++++++++------- 5 files changed, 155 insertions(+), 68 deletions(-) diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 65ef5a17e8b9d5..2f6e997b45b086 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -5,6 +5,7 @@ convertErrorsToExceptions="true" convertNoticesToExceptions="true" convertWarningsToExceptions="true" + convertDeprecationsToExceptions="true" > diff --git a/phpunit/blocks/render-last-posts-test.php b/phpunit/blocks/render-last-posts-test.php index 49734adb515fdc..a20b6f717a4fa3 100644 --- a/phpunit/blocks/render-last-posts-test.php +++ b/phpunit/blocks/render-last-posts-test.php @@ -24,6 +24,10 @@ class Tests_Blocks_RenderLastPosts extends WP_UnitTestCase { * @var array */ protected static $attachment_ids; + /** + * @var array|null + */ + private $original_block_supports; public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) { self::$sticky_post = $factory->post->create_and_get( @@ -49,6 +53,21 @@ public static function wpTearDownAfterClass() { } } + public function set_up() { + parent::set_up(); + + $this->original_block_supports = WP_Block_Supports::$block_to_render; + WP_Block_Supports::$block_to_render = array( + 'attrs' => array(), + 'blockName' => '', + ); + } + + public function tear_down() { + WP_Block_Supports::$block_to_render = $this->original_block_supports; + parent::tear_down(); + } + /** * @covers ::render_block_core_latest_posts */ @@ -56,10 +75,13 @@ public function test_render_block_core_latest_posts() { $action = new MockAction(); add_filter( 'update_post_metadata_cache', array( $action, 'filter' ), 10, 2 ); $attributes = array( - 'displayFeaturedImage' => true, - 'postsToShow' => 5, - 'orderBy' => 'date', - 'order' => 'DESC', + 'displayFeaturedImage' => true, + 'postsToShow' => 5, + 'orderBy' => 'date', + 'order' => 'DESC', + 'excerptLength' => 0, + 'featuredImageSizeSlug' => '', + 'addLinkToFeaturedImage' => false, ); gutenberg_render_block_core_latest_posts( $attributes ); @@ -75,10 +97,13 @@ public function test_render_block_core_latest_posts_no_priming() { $action = new MockAction(); add_filter( 'update_post_metadata_cache', array( $action, 'filter' ), 10, 2 ); $attributes = array( - 'displayFeaturedImage' => false, - 'postsToShow' => 5, - 'orderBy' => 'date', - 'order' => 'DESC', + 'displayFeaturedImage' => false, + 'postsToShow' => 5, + 'orderBy' => 'date', + 'order' => 'DESC', + 'excerptLength' => 0, + 'featuredImageSizeSlug' => '', + 'addLinkToFeaturedImage' => false, ); gutenberg_render_block_core_latest_posts( $attributes ); diff --git a/phpunit/bootstrap.php b/phpunit/bootstrap.php index 1b7967118a6038..fac777864200a8 100644 --- a/phpunit/bootstrap.php +++ b/phpunit/bootstrap.php @@ -5,9 +5,22 @@ * @package Gutenberg */ +// Debug settings for parity with WordPress Core's PHPUnit tests. if ( ! defined( 'WP_DEBUG' ) ) { define( 'WP_DEBUG', true ); } +if ( ! defined( 'LOCAL_WP_DEBUG_LOG' ) ) { + define( 'LOCAL_WP_DEBUG_LOG', true ); +} +if ( ! defined( 'LOCAL_WP_DEBUG_DISPLAY' ) ) { + define( 'LOCAL_WP_DEBUG_DISPLAY', true ); +} +if ( ! defined( 'LOCAL_SCRIPT_DEBUG' ) ) { + define( 'LOCAL_SCRIPT_DEBUG', true ); +} +if ( ! defined( 'LOCAL_WP_ENVIRONMENT_TYPE' ) ) { + define( 'LOCAL_WP_ENVIRONMENT_TYPE', 'local' ); +} // Require composer dependencies. require_once dirname( __DIR__ ) . '/vendor/autoload.php'; diff --git a/phpunit/class-block-library-navigation-link-test.php b/phpunit/class-block-library-navigation-link-test.php index 982ed393ea0b2e..7b91742ec22724 100644 --- a/phpunit/class-block-library-navigation-link-test.php +++ b/phpunit/class-block-library-navigation-link-test.php @@ -18,6 +18,10 @@ class Block_Library_Navigation_Link_Test extends WP_UnitTestCase { private static $pages; private static $terms; + /** + * @var array|null + */ + private $original_block_supports; public static function wpSetUpBeforeClass() { @@ -90,6 +94,21 @@ public static function wpTearDownAfterClass() { } } + public function set_up() { + parent::set_up(); + + $this->original_block_supports = WP_Block_Supports::$block_to_render; + WP_Block_Supports::$block_to_render = array( + 'attrs' => array(), + 'blockName' => '', + ); + } + + public function tear_down() { + WP_Block_Supports::$block_to_render = $this->original_block_supports; + parent::tear_down(); + } + function test_returns_link_when_post_is_published() { $page_id = self::$page->ID; diff --git a/phpunit/class-wp-theme-json-test.php b/phpunit/class-wp-theme-json-test.php index 34d9905743c61e..593b66374e6321 100644 --- a/phpunit/class-wp-theme-json-test.php +++ b/phpunit/class-wp-theme-json-test.php @@ -736,66 +736,7 @@ function test_set_spacing_sizes( $spacing_scale, $expected_output ) { */ function data_generate_spacing_scale_fixtures() { return array( - 'empty_spacing_scale' => array( - 'spacing_scale' => array(), - 'expected_output' => null, - ), - - 'invalid_spacing_scale_values_missing_operator' => array( - 'spacingScale' => array( - 'operator' => '', - 'increment' => 1.5, - 'steps' => 1, - 'mediumStep' => 4, - 'unit' => 'rem', - ), - 'expected_output' => null, - ), - - 'invalid_spacing_scale_values_non_numeric_increment' => array( - 'spacingScale' => array( - 'operator' => '+', - 'increment' => 'add two to previous value', - 'steps' => 1, - 'mediumStep' => 4, - 'unit' => 'rem', - ), - 'expected_output' => null, - ), - - 'invalid_spacing_scale_values_non_numeric_steps' => array( - 'spacingScale' => array( - 'operator' => '+', - 'increment' => 1.5, - 'steps' => 'spiral staircase preferred', - 'mediumStep' => 4, - 'unit' => 'rem', - ), - 'expected_output' => null, - ), - - 'invalid_spacing_scale_values_non_numeric_medium_step' => array( - 'spacingScale' => array( - 'operator' => '+', - 'increment' => 1.5, - 'steps' => 5, - 'mediumStep' => 'That which is just right', - 'unit' => 'rem', - ), - 'expected_output' => null, - ), - - 'invalid_spacing_scale_values_missing_unit' => array( - 'spacingScale' => array( - 'operator' => '+', - 'increment' => 1.5, - 'steps' => 5, - 'mediumStep' => 4, - ), - 'expected_output' => null, - ), - - 'one_step_spacing_scale' => array( + 'one_step_spacing_scale' => array( 'spacingScale' => array( 'operator' => '+', 'increment' => 1.5, @@ -1006,6 +947,94 @@ function data_generate_spacing_scale_fixtures() { ); } + /** + * Tests generating the spacing presets array based on the spacing scale provided. + * + * @dataProvider data_set_spacing_sizes_when_invalid + */ + public function test_set_spacing_sizes_when_invalid( $spacing_scale, $expected_output ) { + $this->expectNotice(); + $this->expectNoticeMessage( 'Some of the theme.json settings.spacing.spacingScale values are invalid' ); + + $theme_json = new WP_Theme_JSON_Gutenberg( + array( + 'version' => 2, + 'settings' => array( + 'spacing' => array( + 'spacingScale' => $spacing_scale, + ), + ), + ) + ); + + $theme_json->set_spacing_sizes(); + $this->assertSame( $expected_output, _wp_array_get( $theme_json->get_raw_data(), array( 'settings', 'spacing', 'spacingSizes', 'default' ) ) ); + } + + /** + * Data provider for spacing scale tests. + * + * @return array + */ + function data_set_spacing_sizes_when_invalid() { + return array( + + 'invalid_spacing_scale_values_missing_operator' => array( + 'spacingScale' => array( + 'operator' => '', + 'increment' => 1.5, + 'steps' => 1, + 'mediumStep' => 4, + 'unit' => 'rem', + ), + 'expected_output' => null, + ), + + 'invalid_spacing_scale_values_non_numeric_increment' => array( + 'spacingScale' => array( + 'operator' => '+', + 'increment' => 'add two to previous value', + 'steps' => 1, + 'mediumStep' => 4, + 'unit' => 'rem', + ), + 'expected_output' => null, + ), + + 'invalid_spacing_scale_values_non_numeric_steps' => array( + 'spacingScale' => array( + 'operator' => '+', + 'increment' => 1.5, + 'steps' => 'spiral staircase preferred', + 'mediumStep' => 4, + 'unit' => 'rem', + ), + 'expected_output' => null, + ), + + 'invalid_spacing_scale_values_non_numeric_medium_step' => array( + 'spacingScale' => array( + 'operator' => '+', + 'increment' => 1.5, + 'steps' => 5, + 'mediumStep' => 'That which is just right', + 'unit' => 'rem', + ), + 'expected_output' => null, + ), + + 'invalid_spacing_scale_values_missing_unit' => array( + 'spacingScale' => array( + 'operator' => '+', + 'increment' => 1.5, + 'steps' => 5, + 'mediumStep' => 4, + ), + 'expected_output' => null, + ), + ); + } + function test_get_styles_for_block_with_padding_aware_alignments() { $theme_json = new WP_Theme_JSON_Gutenberg( array(