test_block_name = null; } public function tear_down() { WP_Style_Engine_CSS_Rules_Store_Gutenberg::remove_all_stores(); unregister_block_type( $this->test_block_name ); $this->test_block_name = null; parent::tear_down(); } /** * Registers a test block type with the provided color block supports. * * @param array $color_settings The color block support settings used for elements. */ public function register_block_with_color_settings( $color_settings ) { $this->test_block_name = 'test/element-block-supports'; register_block_type( $this->test_block_name, array( 'api_version' => 3, 'attributes' => array( 'style' => array( 'type' => 'object', ), ), 'supports' => array( 'color' => $color_settings, ), ) ); } /** * Tests that elements block support applies the correct classname. * * @covers ::gutenberg_render_elements_support * * @dataProvider data_elements_block_support_class * * @param array $color_settings The color block support settings used for elements support. * @param array $elements_styles The elements styles within the block attributes. * @param string $block_markup Original block markup. * @param string $expected_markup Resulting markup after application of elements block support. */ public function test_elements_block_support_class( $color_settings, $elements_styles, $block_markup, $expected_markup ) { $this->register_block_with_color_settings( $color_settings ); $block = array( 'blockName' => $this->test_block_name, 'attrs' => array( 'style' => array( 'elements' => $elements_styles, ), ), ); /* * To ensure a consistent elements class name it is generated within a * `render_block_data` filter and stored in the `className` attribute. * As a result the block data needs to be passed through the same * function for this test. */ $filtered_block = gutenberg_render_elements_support_styles( $block ); $actual = gutenberg_render_elements_class_name( $block_markup, $filtered_block ); $this->assertMatchesRegularExpression( $expected_markup, $actual, 'Elements block wrapper markup should be correct' ); } /** * Tests that a non-string `className` attribute does not cause a fatal * error and the block content is returned unmodified. * * Block attributes such as `className` are always expected to be strings, * however invalid stored data can result in other types being present. The * render filter should fail gracefully rather than passing an array to * `preg_match()`. * * @covers ::gutenberg_render_elements_class_name */ public function test_elements_block_support_class_with_non_string_class_name() { $block = array( 'blockName' => 'core/paragraph', 'attrs' => array( 'className' => array( '0', '1' ), ), ); $block_content = "
Test
\n"; $this->assertSame( $block_content, gutenberg_render_elements_class_name( $block_content, $block ), 'Block content should be returned unchanged when className is not a string' ); } /** * Tests that a 'my-wp-elements-*' class name is skipped from processing. * * @covers ::gutenberg_render_elements_class_name */ public function test_elements_block_support_class_with_invalid_elements_prefix() { $block = array( 'blockName' => 'core/paragraph', 'attrs' => array( 'className' => 'my-wp-elements-foo', ), ); $block_content = "Test
\n"; $this->assertSame( $block_content, gutenberg_render_elements_class_name( $block_content, $block ), 'Block content should be returned unchanged when className lacks a class with the expected prefix' ); } /** * Data provider. * * @return array */ public function data_elements_block_support_class() { $color_styles = array( 'text' => 'var:preset|color|vivid-red', 'background' => '#fff', ); // The `wp-elements-*` class suffix is an md5 hash on older WordPress and a // sequential id (via `wp_unique_prefixed_id()`) on newer cores, so the // patterns below accept either form to stay green across supported versions. return array( 'button element styles with serialization skipped' => array( 'color_settings' => array( 'button' => true, '__experimentalSkipSerialization' => true, ), 'elements_styles' => array( 'button' => array( 'color' => $color_styles ), ), 'block_markup' => 'Hello WordPress!
', 'expected_markup' => '/^Hello WordPress<\/a>!<\/p>$/',
),
'link element styles with serialization skipped' => array(
'color_settings' => array(
'link' => true,
'__experimentalSkipSerialization' => true,
),
'elements_styles' => array(
'link' => array( 'color' => $color_styles ),
),
'block_markup' => ' Hello WordPress! Hello WordPress<\/a>!<\/p>$/',
),
'heading element styles with serialization skipped' => array(
'color_settings' => array(
'heading' => true,
'__experimentalSkipSerialization' => true,
),
'elements_styles' => array(
'heading' => array( 'color' => $color_styles ),
),
'block_markup' => ' Hello WordPress! Hello WordPress<\/a>!<\/p>$/',
),
'button element styles apply class to wrapper' => array(
'color_settings' => array( 'button' => true ),
'elements_styles' => array(
'button' => array( 'color' => $color_styles ),
),
'block_markup' => ' Hello WordPress! Hello WordPress<\/a>!<\/p>$/',
),
'link element styles apply class to wrapper' => array(
'color_settings' => array( 'link' => true ),
'elements_styles' => array(
'link' => array( 'color' => $color_styles ),
),
'block_markup' => ' Hello WordPress! Hello WordPress<\/a>!<\/p>$/',
),
'heading element styles apply class to wrapper' => array(
'color_settings' => array( 'heading' => true ),
'elements_styles' => array(
'heading' => array( 'color' => $color_styles ),
),
'block_markup' => ' Hello WordPress! Hello WordPress<\/a>!<\/p>$/',
),
'element styles apply class to wrapper when it has other classes' => array(
'color_settings' => array( 'link' => true ),
'elements_styles' => array(
'link' => array( 'color' => $color_styles ),
),
'block_markup' => ' Hello WordPress! Hello WordPress<\/a>!<\/p>$/',
),
'element styles apply class to wrapper when it has other attributes' => array(
'color_settings' => array( 'link' => true ),
'elements_styles' => array(
'link' => array( 'color' => $color_styles ),
),
'block_markup' => ' Hello WordPress!