From 7e25b96f066957da9ee29cb6b758178a0bde06b2 Mon Sep 17 00:00:00 2001 From: Tonya Mork Date: Tue, 4 Oct 2022 14:20:18 +0000 Subject: [PATCH] Editor: Add PHPUnit tests for 5.9.0 new functions. During the 5.9.0 cycle, tests were missed during the porting from Gutenberg to Core for the following functions: * `_disable_block_editor_for_navigation_post_type()` * `_disable_content_editor_for_navigation_post_type()` * `_enable_content_editor_for_navigation_post_type()` * `wp_filter_global_styles_post()` This commit adds new test classes for these functions. Reference: * [https://github.com/WordPress/gutenberg/blob/release/13.6/phpunit/global-styles-test.php Gutenberg v13.6] for `WP_Global_Styles_Test` * [https://github.com/WordPress/gutenberg/blob/release/13.6/phpunit/navigation-test.php Gutenberg v13.6] for `WP_Navigation_Test` Follow-up to [52298], [52145], [52052]. Props antonvlasenko, costdev, ironprogrammer, robinwpdeveloper, hellofromTonya. Fixes #56266. git-svn-id: https://develop.svn.wordpress.org/trunk@54382 602fd350-edb4-49c9-b593-d223f7449a82 --- ...isableBlockEditorForNavigationPostType.php | 105 ++++++++++++++ ...ableContentEditorForNavigationPostType.php | 113 +++++++++++++++ ...ableContentEditorForNavigationPostType.php | 130 ++++++++++++++++++ .../tests/kses/wpFilterGlobalStylesPost.php | 83 +++++++++++ 4 files changed, 431 insertions(+) create mode 100644 tests/phpunit/tests/editor/disableBlockEditorForNavigationPostType.php create mode 100644 tests/phpunit/tests/editor/disableContentEditorForNavigationPostType.php create mode 100644 tests/phpunit/tests/editor/enableContentEditorForNavigationPostType.php create mode 100644 tests/phpunit/tests/kses/wpFilterGlobalStylesPost.php diff --git a/tests/phpunit/tests/editor/disableBlockEditorForNavigationPostType.php b/tests/phpunit/tests/editor/disableBlockEditorForNavigationPostType.php new file mode 100644 index 0000000000..22e791dfd3 --- /dev/null +++ b/tests/phpunit/tests/editor/disableBlockEditorForNavigationPostType.php @@ -0,0 +1,105 @@ +assertFalse( _disable_block_editor_for_navigation_post_type( $supports, static::NAVIGATION_POST_TYPE ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_should_return_false_when_wp_navigation() { + return array( + 'support value: true' => array( true ), + 'support value: false' => array( false ), + ); + } + + /** + * @dataProvider data_should_return_given_value_for_non_wp_navigation_post_types + * @ticket 56266 + * + * @param bool $supports Whether the CPT supports block editor or not. + * @param string $post_type The post type + */ + public function test_should_return_given_value_for_non_wp_navigation_post_types( $supports, $post_type ) { + $this->assertSame( $supports, _disable_block_editor_for_navigation_post_type( $supports, $post_type ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_should_return_given_value_for_non_wp_navigation_post_types() { + return array( + 'post' => array( + 'post_type' => 'post', + 'supports' => true, + ), + 'page' => array( + 'post_type' => 'page', + 'supports' => true, + ), + 'attachments' => array( + 'post_type' => 'attachments', + 'supports' => false, + ), + 'revision' => array( + 'post_type' => 'revision', + 'supports' => false, + ), + 'custom_css' => array( + 'post_type' => 'custom_css', + 'supports' => false, + ), + 'customize_changeset' => array( + 'post_type' => 'customize_changeset', + 'supports' => false, + ), + 'nav_menu_item' => array( + 'post_type' => 'nav_menu_item', + 'supports' => true, + ), + 'oembed_cache' => array( + 'post_type' => 'oembed_cache', + 'supports' => true, + ), + 'user_request' => array( + 'post_type' => 'user_request', + 'supports' => true, + ), + 'wp_block' => array( + 'post_type' => 'wp_block', + 'supports' => true, + ), + 'wp_template' => array( + 'post_type' => 'wp_template', + 'supports' => true, + ), + 'wp_template_part' => array( + 'post_type' => 'wp_template_part', + 'supports' => true, + ), + 'wp_global_styles' => array( + 'post_type' => 'wp_global_styles', + 'supports' => true, + ), + ); + } +} diff --git a/tests/phpunit/tests/editor/disableContentEditorForNavigationPostType.php b/tests/phpunit/tests/editor/disableContentEditorForNavigationPostType.php new file mode 100644 index 0000000000..6f4de7ddea --- /dev/null +++ b/tests/phpunit/tests/editor/disableContentEditorForNavigationPostType.php @@ -0,0 +1,113 @@ +create_post( static::NAVIGATION_POST_TYPE ); + + $this->assertTrue( post_type_supports( static::NAVIGATION_POST_TYPE, 'editor' ) ); + + _disable_content_editor_for_navigation_post_type( $post ); + + $this->assertFalse( post_type_supports( static::NAVIGATION_POST_TYPE, 'editor' ) ); + } + + /** + * @dataProvider data_should_not_disable + * @ticket 56266 + * + * @param string $post_type Post type to test. + */ + public function test_should_not_disable( $post_type ) { + $post = $this->create_post( $post_type ); + + _disable_content_editor_for_navigation_post_type( $post ); + + $this->assertTrue( post_type_supports( $post_type, 'editor' ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_should_not_disable() { + return array( + 'post' => array( 'post' ), + 'page' => array( 'page' ), + 'nav_menu_item' => array( 'nav_menu_item' ), + 'oembed_cache' => array( 'oembed_cache' ), + 'user_request' => array( 'user_request' ), + 'wp_block' => array( 'wp_block' ), + 'wp_template' => array( 'wp_template' ), + 'wp_template_part' => array( 'wp_template_part' ), + 'wp_global_styles' => array( 'wp_global_styles' ), + ); + } + + /** + * @dataProvider data_should_not_change_post_type_support + * @ticket 56266 + * + * @param string $post_type Post type to test. + */ + public function test_should_not_change_post_type_support( $post_type ) { + $post = $this->create_post( $post_type ); + + // Capture the original support. + $before = post_type_supports( $post_type, 'editor' ); + + _disable_content_editor_for_navigation_post_type( $post ); + + // Ensure it did not change. + $this->assertSame( $before, post_type_supports( $post_type, 'editor' ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_should_not_change_post_type_support() { + return array( + 'post' => array( 'post' ), + 'page' => array( 'page' ), + 'attachments' => array( 'attachments' ), + 'revision' => array( 'revision' ), + 'custom_css' => array( 'custom_css' ), + 'customize_changeset' => array( 'customize_changeset' ), + 'nav_menu_item' => array( 'nav_menu_item' ), + 'oembed_cache' => array( 'oembed_cache' ), + 'user_request' => array( 'user_request' ), + 'wp_block' => array( 'wp_block' ), + 'wp_template' => array( 'wp_template' ), + 'wp_template_part' => array( 'wp_template_part' ), + 'wp_global_styles' => array( 'wp_global_styles' ), + ); + } + + /** + * Creates a post. + * + * @param string $post_type Post type to create. + * @return int + */ + private function create_post( $post_type ) { + return $this->factory()->post->create( + array( 'post_type' => $post_type ) + ); + } +} diff --git a/tests/phpunit/tests/editor/enableContentEditorForNavigationPostType.php b/tests/phpunit/tests/editor/enableContentEditorForNavigationPostType.php new file mode 100644 index 0000000000..bfa3c757ee --- /dev/null +++ b/tests/phpunit/tests/editor/enableContentEditorForNavigationPostType.php @@ -0,0 +1,130 @@ +assertTrue( post_type_supports( static::NAVIGATION_POST_TYPE, 'editor' ) ); + } + + /** + * @ticket 56266 + */ + public function test_should_enable() { + $post = $this->create_post( static::NAVIGATION_POST_TYPE ); + + _enable_content_editor_for_navigation_post_type( $post ); + + $this->assertTrue( post_type_supports( static::NAVIGATION_POST_TYPE, 'editor' ) ); + } + + /** + * @ticket 56266 + */ + public function test_should_reenable_when_disabled() { + $post = $this->create_post( static::NAVIGATION_POST_TYPE ); + + // Set up the test by removing the 'editor' post type support. + remove_post_type_support( static::NAVIGATION_POST_TYPE, 'editor' ); + $this->assertFalse( post_type_supports( static::NAVIGATION_POST_TYPE, 'editor' ) ); + + _enable_content_editor_for_navigation_post_type( $post ); + + $this->assertTrue( post_type_supports( static::NAVIGATION_POST_TYPE, 'editor' ) ); + } + + /** + * @dataProvider data_should_not_enable + * @ticket 56266 + * + * @param string $post_type Post type to test. + */ + public function test_should_not_enable( $post_type ) { + $post = $this->create_post( $post_type ); + + _enable_content_editor_for_navigation_post_type( $post ); + + $this->assertFalse( post_type_supports( $post_type, 'editor' ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_should_not_enable() { + return array( + 'invalid post type' => array( 'book' ), + 'attachments' => array( 'attachments' ), + 'revision' => array( 'revision' ), + 'custom_css' => array( 'custom_css' ), + 'customize_changeset' => array( 'customize_changeset' ), + ); + } + + /** + * @dataProvider data_should_not_change_post_type_support + * @ticket 56266 + * + * @param string $post_type Post type to test. + */ + public function test_should_not_change_post_type_support( $post_type ) { + $post = $this->create_post( $post_type ); + + // Capture the original support. + $before = post_type_supports( $post_type, 'editor' ); + + _enable_content_editor_for_navigation_post_type( $post ); + + // Ensure it did not change. + $this->assertSame( $before, post_type_supports( $post_type, 'editor' ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_should_not_change_post_type_support() { + return array( + 'post' => array( 'post' ), + 'page' => array( 'page' ), + 'attachments' => array( 'attachments' ), + 'revision' => array( 'revision' ), + 'custom_css' => array( 'custom_css' ), + 'customize_changeset' => array( 'customize_changeset' ), + 'nav_menu_item' => array( 'nav_menu_item' ), + 'oembed_cache' => array( 'oembed_cache' ), + 'user_request' => array( 'user_request' ), + 'wp_block' => array( 'wp_block' ), + 'wp_template' => array( 'wp_template' ), + 'wp_template_part' => array( 'wp_template_part' ), + 'wp_global_styles' => array( 'wp_global_styles' ), + ); + } + + /** + * Creates a post. + * + * @param string $post_type Post type to create. + * @return int + */ + private function create_post( $post_type ) { + return $this->factory()->post->create( + array( 'post_type' => $post_type ) + ); + } +} diff --git a/tests/phpunit/tests/kses/wpFilterGlobalStylesPost.php b/tests/phpunit/tests/kses/wpFilterGlobalStylesPost.php new file mode 100644 index 0000000000..49cf0078aa --- /dev/null +++ b/tests/phpunit/tests/kses/wpFilterGlobalStylesPost.php @@ -0,0 +1,83 @@ + 1, + 'version' => 1, + 'styles' => array( + 'blocks' => array( + 'core/button' => array( + 'border' => array( + 'radius' => '0', + ), + ), + ), + ), + ); + + /** + * @dataProvider data_should_not_remove_safe_global_style_rules + * @ticket 56266 + * + * @param string $rule A rule to test. + */ + public function test_should_not_remove_safe_global_style_rules( $rule ) { + $theme_data = wp_parse_args( $this->user_theme_data, array( $rule => 'someValue' ) ); + $filtered_user_theme_json = $this->filter_global_styles( $theme_data ); + $safe_rules = array_keys( $theme_data ); + foreach ( $safe_rules as $safe_rule ) { + $this->assertArrayHasKey( $safe_rule, $filtered_user_theme_json, sprintf( 'wp_filter_global_styles_post() must not remove the "%s" rule as it\'s considered safe.', $safe_rule ) ); + } + } + + /** + * Data provider. + * + * @return array + */ + public function data_should_not_remove_safe_global_style_rules() { + $result = array(); + foreach ( WP_Theme_JSON::VALID_TOP_LEVEL_KEYS as $safe_rule ) { + $result[ $safe_rule ] = array( $safe_rule ); + } + + // Settings always get removed. + unset( $result['settings'] ); + + return $result; + } + + /** + * @ticket 56266 + */ + public function test_should_remove_unsafe_global_style_rules() { + $filtered_user_theme_json = $this->filter_global_styles( $this->user_theme_data ); + $this->assertArrayNotHasKey( 'nonSchemaRule', $filtered_user_theme_json, 'Filtered json data must not contain unsafe global style rules.' ); + } + + /** + * This is a helper method. + * It filters JSON theme data and returns it as an array. + * + * @param array $theme_data Theme data to filter. + * + * @return array Filtered theme data. + */ + private function filter_global_styles( $theme_data ) { + $user_theme_json = wp_slash( wp_json_encode( $theme_data ) ); + $filtered_user_theme_json = wp_filter_global_styles_post( $user_theme_json ); + + return json_decode( wp_unslash( $filtered_user_theme_json ), true ); + } +}