Skip to content

Commit

Permalink
Formatting and renaming unit tests to WP standard
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Sep 21, 2022
1 parent b8d1326 commit 5e392ae
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 32 deletions.
92 changes: 66 additions & 26 deletions lib/block-supports/typography.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,44 +106,82 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {

$typography_block_styles = array();
if ( $has_font_size_support && ! $should_skip_font_size ) {
$preset_font_size = array_key_exists( 'fontSize', $block_attributes ) ? "var:preset|font-size|{$block_attributes['fontSize']}" : null;
$custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] ) ? $block_attributes['style']['typography']['fontSize'] : null;
$preset_font_size = array_key_exists( 'fontSize', $block_attributes )
? "var:preset|font-size|{$block_attributes['fontSize']}"
: null;
$custom_font_size = isset( $block_attributes['style']['typography']['fontSize'] )
? $block_attributes['style']['typography']['fontSize']
: null;
$typography_block_styles['fontSize'] = $preset_font_size ? $preset_font_size : $custom_font_size;
}

if ( $has_font_family_support && ! $should_skip_font_family ) {
$preset_font_family = array_key_exists( 'fontFamily', $block_attributes ) ? "var:preset|font-family|{$block_attributes['fontFamily']}" : null;
$custom_font_family = isset( $block_attributes['style']['typography']['fontFamily'] ) ? gutenberg_typography_get_preset_inline_style_value( $block_attributes['style']['typography']['fontFamily'], 'font-family' ) : null;
$preset_font_family = array_key_exists( 'fontFamily', $block_attributes )
? "var:preset|font-family|{$block_attributes['fontFamily']}"
: null;
$custom_font_family = isset( $block_attributes['style']['typography']['fontFamily'] )
? gutenberg_typography_get_preset_inline_style_value( $block_attributes['style']['typography']['fontFamily'], 'font-family' )
: null;
$typography_block_styles['fontFamily'] = $preset_font_family ? $preset_font_family : $custom_font_family;
}

if ( $has_font_style_support && ! $should_skip_font_style && isset( $block_attributes['style']['typography']['fontStyle'] ) ) {
$typography_block_styles['fontStyle'] =
gutenberg_typography_get_preset_inline_style_value( $block_attributes['style']['typography']['fontStyle'], 'font-style' );
if (
$has_font_style_support &&
! $should_skip_font_style &&
isset( $block_attributes['style']['typography']['fontStyle'] )
) {
$typography_block_styles['fontStyle'] = gutenberg_typography_get_preset_inline_style_value(
$block_attributes['style']['typography']['fontStyle'],
'font-style'
);
}

if ( $has_font_weight_support && ! $should_skip_font_weight && isset( $block_attributes['style']['typography']['fontWeight'] ) ) {
$typography_block_styles['fontWeight'] =
gutenberg_typography_get_preset_inline_style_value( $block_attributes['style']['typography']['fontWeight'], 'font-weight' );
if (
$has_font_weight_support &&
! $should_skip_font_weight &&
isset( $block_attributes['style']['typography']['fontWeight'] )
) {
$typography_block_styles['fontWeight'] = gutenberg_typography_get_preset_inline_style_value(
$block_attributes['style']['typography']['fontWeight'],
'font-weight'
);
}

if ( $has_line_height_support && ! $should_skip_line_height ) {
$typography_block_styles['lineHeight'] = _wp_array_get( $block_attributes, array( 'style', 'typography', 'lineHeight' ), null );
}

if ( $has_text_decoration_support && ! $should_skip_text_decoration && isset( $block_attributes['style']['typography']['textDecoration'] ) ) {
$typography_block_styles['textDecoration'] =
gutenberg_typography_get_preset_inline_style_value( $block_attributes['style']['typography']['textDecoration'], 'text-decoration' );
if (
$has_text_decoration_support &&
! $should_skip_text_decoration &&
isset( $block_attributes['style']['typography']['textDecoration'] )
) {
$typography_block_styles['textDecoration'] = gutenberg_typography_get_preset_inline_style_value(
$block_attributes['style']['typography']['textDecoration'],
'text-decoration'
);
}

if ( $has_text_transform_support && ! $should_skip_text_transform && isset( $block_attributes['style']['typography']['textTransform'] ) ) {
$typography_block_styles['textTransform'] =
gutenberg_typography_get_preset_inline_style_value( $block_attributes['style']['typography']['textTransform'], 'text-transform' );
if (
$has_text_transform_support &&
! $should_skip_text_transform &&
isset( $block_attributes['style']['typography']['textTransform'] )
) {
$typography_block_styles['textTransform'] = gutenberg_typography_get_preset_inline_style_value(
$block_attributes['style']['typography']['textTransform'],
'text-transform'
);
}

if ( $has_letter_spacing_support && ! $should_skip_letter_spacing && isset( $block_attributes['style']['typography']['letterSpacing'] ) ) {
$typography_block_styles['letterSpacing'] =
gutenberg_typography_get_preset_inline_style_value( $block_attributes['style']['typography']['letterSpacing'], 'letter-spacing' );
if (
$has_letter_spacing_support &&
! $should_skip_letter_spacing &&
isset( $block_attributes['style']['typography']['letterSpacing'] )
) {
$typography_block_styles['letterSpacing'] = gutenberg_typography_get_preset_inline_style_value(
$block_attributes['style']['typography']['letterSpacing'],
'letter-spacing'
);
}

$attributes = array();
Expand Down Expand Up @@ -172,21 +210,23 @@ function gutenberg_apply_typography_support( $block_type, $block_attributes ) {
* Generates an inline style value for a typography feature e.g. text decoration,
* text transform, and font style.
*
* @param string $style_value A raw style value for a single typography feature from a block's style attribute.
* @param string $css_property Slug for the CSS property the inline style sets.
* @param string $style_value A raw style value for a single typography feature from a block's style attribute.
* @param string $css_property Slug for the CSS property the inline style sets.
*
* @return string? A CSS inline style value.
* @return string? A CSS inline style value.
*/
function gutenberg_typography_get_preset_inline_style_value( $style_value, $css_property ) {
// If the style value is not a preset CSS variable go no further.
if ( empty( $style_value ) || ! str_contains( $style_value, "var:preset|{$css_property}|" ) ) {
return $style_value;
}

// For backwards compatibility.
// Presets were removed in WordPress/gutenberg#27555.
// We have a preset CSS variable as the style.
// Get the style value from the string and return CSS style.
/*
* For backwards compatibility.
* Presets were removed in WordPress/gutenberg#27555.
* We have a preset CSS variable as the style.
* Get the style value from the string and return CSS style.
*/
$index_to_splice = strrpos( $style_value, '|' ) + 1;
$slug = _wp_to_kebab_case( substr( $style_value, $index_to_splice ) );

Expand Down
10 changes: 4 additions & 6 deletions phpunit/block-supports/typography-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -248,29 +248,27 @@ function test_should_generate_classname_for_font_family() {
* Tests generating font size values, including fluid formulae, from fontSizes preset.
*
* @covers ::wp_get_typography_font_size_value
* @covers ::wp_get_typography_value_and_unit
* @covers ::wp_get_computed_fluid_typography_value
*
* @dataProvider data_generate_font_size_preset_fixtures
*
* @param array $font_size_preset {
* @param array $font_size_preset {
* Required. fontSizes preset value as seen in theme.json.
*
* @type string $name Name of the font size preset.
* @type string $slug Kebab-case unique identifier for the font size preset.
* @type string $size CSS font-size value, including units where applicable.
* }
* @param bool $should_use_fluid_typography An override to switch fluid typography "on". Can be used for unit testing.
* @param string $expected_output Expected output of gutenberg_get_typography_font_size_value().
* @param string $expected_output Expected output.
*/
function test_gutenberg_get_typography_font_size_value( $font_size_preset, $should_use_fluid_typography, $expected_output ) {
public function test_gutenberg_get_typography_font_size_value( $font_size_preset, $should_use_fluid_typography, $expected_output ) {
$actual = gutenberg_get_typography_font_size_value( $font_size_preset, $should_use_fluid_typography );

$this->assertSame( $expected_output, $actual );
}

/**
* Data provider for test_wp_get_typography_font_size_value.
* Data provider.
*
* @return array
*/
Expand Down

0 comments on commit 5e392ae

Please sign in to comment.