Skip to content

Commit

Permalink
Updating according to feedback in #4655 (review)
Browse files Browse the repository at this point in the history
  • Loading branch information
ramonjd committed Jun 26, 2023
1 parent a56784e commit 9fcf6d0
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 141 deletions.
3 changes: 1 addition & 2 deletions src/wp-includes/class-wp-theme-json.php
Original file line number Diff line number Diff line change
Expand Up @@ -2396,8 +2396,7 @@ function( $pseudo_selector ) use ( $selector ) {

// 3. Generate and append the rules that use the duotone selector.
if ( isset( $block_metadata['duotone'] ) && ! empty( $declarations_duotone ) ) {
$selector_duotone = static::scope_selector( $block_metadata['selector'], $block_metadata['duotone'] );
$block_rules .= static::to_ruleset( $selector_duotone, $declarations_duotone );
$block_rules .= static::to_ruleset( $block_metadata['duotone'], $declarations_duotone );
}

// 4. Generate Layout block gap styles.
Expand Down
206 changes: 67 additions & 139 deletions tests/phpunit/tests/theme/wpGetBlockCssSelector.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
/**
* Tests wp_get_block_css_selector().
*
* @since 6.3.0
*
* @group themes
*
* @covers ::wp_get_block_css_selector
Expand All @@ -18,30 +20,26 @@ public function set_up() {
public function tear_down() {
unregister_block_type( $this->test_block_name );
$this->test_block_name = null;
set_current_screen( '' );
parent::tear_down();
}

private function register_test_block( $name, $selectors = null, $supports = null, $editor_selectors = null ) {
private function register_test_block( $name, $selectors = null, $supports = null ) {
$this->test_block_name = $name;

return register_block_type(
$this->test_block_name,
array(
'api_version' => 2,
'attributes' => array(),
'selectors' => $selectors,
'editor_selectors' => $editor_selectors,
'supports' => $supports,
'api_version' => 2,
'attributes' => array(),
'selectors' => $selectors,
'supports' => $supports,
)
);
}

private function set_screen_to_block_editor() {
set_current_screen( 'edit-post' );
get_current_screen()->is_block_editor( true );
}

/**
* @ticket 58586
*/
public function test_get_root_selector_via_selectors_api() {
$block_type = self::register_test_block(
'test/block-with-selectors',
Expand All @@ -52,6 +50,9 @@ public function test_get_root_selector_via_selectors_api() {
$this->assertEquals( '.wp-custom-block-class', $selector );
}

/**
* @ticket 58586
*/
public function test_get_root_selector_via_experimental_property() {
$block_type = self::register_test_block(
'test/block-without-selectors',
Expand All @@ -63,6 +64,9 @@ public function test_get_root_selector_via_experimental_property() {
$this->assertEquals( '.experimental-selector', $selector );
}

/**
* @ticket 58586
*/
public function test_default_root_selector_generation_for_core_block() {
$block_type = self::register_test_block(
'core/without-selectors-or-supports',
Expand All @@ -74,6 +78,9 @@ public function test_default_root_selector_generation_for_core_block() {
$this->assertEquals( '.wp-block-without-selectors-or-supports', $selector );
}

/**
* @ticket 58586
*/
public function test_default_root_selector_generation() {
$block_type = self::register_test_block(
'test/without-selectors-or-supports',
Expand All @@ -85,30 +92,9 @@ public function test_default_root_selector_generation() {
$this->assertEquals( '.wp-block-test-without-selectors-or-supports', $selector );
}

public function test_get_duotone_selector_via_selectors_api() {
$block_type = self::register_test_block(
'test/duotone-selector',
array(
'filters' => array( 'duotone' => '.duotone-selector' ),
),
null
);

$selector = wp_get_block_css_selector( $block_type, array( 'filters', 'duotone' ) );
$this->assertEquals( '.duotone-selector', $selector );
}

public function test_no_duotone_selector_set() {
$block_type = self::register_test_block(
'test/null-duotone-selector',
null,
null
);

$selector = wp_get_block_css_selector( $block_type, 'filters.duotone' );
$this->assertEquals( null, $selector );
}

/**
* @ticket 58586
*/
public function test_get_feature_selector_via_selectors_api() {
$block_type = self::register_test_block(
'test/feature-selector',
Expand All @@ -120,6 +106,9 @@ public function test_get_feature_selector_via_selectors_api() {
$this->assertEquals( '.typography', $selector );
}

/**
* @ticket 58586
*/
public function test_get_feature_selector_via_selectors_api_shorthand_property() {
$block_type = self::register_test_block(
'test/shorthand-feature-selector',
Expand All @@ -131,6 +120,9 @@ public function test_get_feature_selector_via_selectors_api_shorthand_property()
$this->assertEquals( '.typography', $selector );
}

/**
* @ticket 58586
*/
public function test_no_feature_level_selector_via_selectors_api() {
$block_type = self::register_test_block(
'test/null-feature-selector',
Expand All @@ -142,6 +134,9 @@ public function test_no_feature_level_selector_via_selectors_api() {
$this->assertEquals( null, $selector );
}

/**
* @ticket 58586
*/
public function test_fallback_feature_level_selector_via_selectors_api_to_generated_class() {
$block_type = self::register_test_block(
'test/fallback-feature-selector',
Expand All @@ -153,7 +148,9 @@ public function test_fallback_feature_level_selector_via_selectors_api_to_genera
$this->assertEquals( '.wp-block-test-fallback-feature-selector', $selector );
}


/**
* @ticket 58586
*/
public function test_fallback_feature_level_selector_via_selectors_api() {
$block_type = self::register_test_block(
'test/fallback-feature-selector',
Expand All @@ -165,6 +162,9 @@ public function test_fallback_feature_level_selector_via_selectors_api() {
$this->assertEquals( '.fallback-root-selector', $selector );
}

/**
* @ticket 58586
*/
public function test_get_feature_selector_via_experimental_property() {
$block_type = self::register_test_block(
'test/experimental-feature-selector',
Expand All @@ -180,6 +180,9 @@ public function test_get_feature_selector_via_experimental_property() {
$this->assertEquals( '.wp-block-test-experimental-feature-selector .experimental-typography', $selector );
}

/**
* @ticket 58586
*/
public function test_fallback_feature_selector_via_experimental_property() {
$block_type = self::register_test_block(
'test/fallback-feature-selector',
Expand All @@ -191,6 +194,9 @@ public function test_fallback_feature_selector_via_experimental_property() {
$this->assertEquals( '.wp-block-test-fallback-feature-selector', $selector );
}

/**
* @ticket 58586
*/
public function test_no_feature_selector_via_experimental_property() {
$block_type = self::register_test_block(
'test/null-experimental-feature-selector',
Expand All @@ -202,6 +208,9 @@ public function test_no_feature_selector_via_experimental_property() {
$this->assertEquals( null, $selector );
}

/**
* @ticket 58586
*/
public function test_get_subfeature_selector_via_selectors_api() {
$block_type = self::register_test_block(
'test/subfeature-selector',
Expand All @@ -221,6 +230,9 @@ public function test_get_subfeature_selector_via_selectors_api() {
$this->assertEquals( '.root .typography .text-decoration', $selector );
}

/**
* @ticket 58586
*/
public function test_fallback_subfeature_selector_via_selectors_api() {
$block_type = self::register_test_block(
'test/subfeature-selector',
Expand All @@ -239,6 +251,9 @@ public function test_fallback_subfeature_selector_via_selectors_api() {
$this->assertEquals( '.root .typography', $selector );
}

/**
* @ticket 58586
*/
public function test_no_subfeature_level_selector_via_selectors_api() {
$block_type = self::register_test_block(
'test/null-subfeature-selector',
Expand All @@ -250,6 +265,9 @@ public function test_no_subfeature_level_selector_via_selectors_api() {
$this->assertEquals( null, $selector );
}

/**
* @ticket 58586
*/
public function test_fallback_subfeature_selector_via_experimental_property() {
$block_type = self::register_test_block(
'test/fallback-subfeature-selector',
Expand All @@ -265,6 +283,9 @@ public function test_fallback_subfeature_selector_via_experimental_property() {
$this->assertEquals( '.wp-block-test-fallback-subfeature-selector', $selector );
}

/**
* @ticket 58586
*/
public function test_no_subfeature_selector_via_experimental_property() {
$block_type = self::register_test_block(
'test/null-experimental-subfeature-selector',
Expand All @@ -279,6 +300,9 @@ public function test_no_subfeature_selector_via_experimental_property() {
$this->assertEquals( null, $selector );
}

/**
* @ticket 58586
*/
public function test_empty_target_returns_null() {
$block_type = self::register_test_block(
'test/null-experimental-subfeature-selector',
Expand All @@ -293,6 +317,9 @@ public function test_empty_target_returns_null() {
$this->assertEquals( null, $selector );
}

/**
* @ticket 58586
*/
public function test_string_targets_for_features() {
$block_type = self::register_test_block(
'test/target-types-for-features',
Expand All @@ -307,6 +334,9 @@ public function test_string_targets_for_features() {
$this->assertEquals( '.found', $selector );
}

/**
* @ticket 58586
*/
public function test_string_targets_for_subfeatures() {
$block_type = self::register_test_block(
'test/target-types-for-features',
Expand All @@ -322,107 +352,5 @@ public function test_string_targets_for_subfeatures() {
$selector = wp_get_block_css_selector( $block_type, array( 'typography', 'fontSize' ) );
$this->assertEquals( '.found', $selector );
}

public function test_editor_only_root_selector() {
self::set_screen_to_block_editor();

$block_type = self::register_test_block(
'test/editor-only-selectors',
array( 'root' => '.wp-custom-block-class' ),
null,
array( 'root' => '.wp-custom-block-class' )
);

$selector = wp_get_block_css_selector( $block_type, 'root' );
$this->assertEquals( '.wp-custom-block-class', $selector );
}

public function test_editor_only_duotone_selector() {
self::set_screen_to_block_editor();

$block_type = self::register_test_block(
'test/editor-duotone-selector',
array(
'filters' => array( 'duotone' => '.duotone-selector' ),
),
null,
array(
'filters' => array( 'duotone' => '.duotone-selector' ),
)
);

$selector = wp_get_block_css_selector( $block_type, 'filters.duotone' );
$this->assertEquals( '.duotone-selector', $selector );
}

public function test_editor_only_feature_selector() {
self::set_screen_to_block_editor();

$block_type = self::register_test_block(
'test/editor-feature-selector',
array( 'typography' => array( 'root' => '.typography' ) ),
null,
array( 'typography' => array( 'root' => '.typography' ) )
);

$selector = wp_get_block_css_selector( $block_type, 'typography' );
$this->assertEquals( '.typography', $selector );
}

public function test_editor_only_feature_selector_shorthand() {
self::set_screen_to_block_editor();

$block_type = self::register_test_block(
'test/editor-feature-selector',
array( 'typography' => '.typography' ),
null,
array( 'typography' => '.typography' )
);

$selector = wp_get_block_css_selector( $block_type, 'typography' );
$this->assertEquals( '.typography', $selector );
}

public function test_editor_only_subfeature_selector() {
self::set_screen_to_block_editor();

$block_type = self::register_test_block(
'test/editor-subfeature-selector',
array( 'typography' => array( 'fontSize' => '.font-size' ) ),
null,
array( 'typography' => array( 'fontSize' => '.font-size' ) )
);

$selector = wp_get_block_css_selector( $block_type, 'typography.fontSize' );
$this->assertEquals( '.font-size', $selector );
}

public function test_non_editor_subfeature_does_not_fall_back_to_editor_only_feature_selector() {
self::set_screen_to_block_editor();

$block_type = self::register_test_block(
'test/editor-subfeature-selector',
array( 'typography' => array( 'fontSize' => '.font-size' ) ),
null,
array( 'typography' => '.font-size' )
);

$selector = wp_get_block_css_selector( $block_type, 'typography.fontSize', true );
$this->assertEquals( '.font-size', $selector );
}

public function test_unspecified_subfeature_falls_back_to_editor_only_feature_selector() {
self::set_screen_to_block_editor();

$block_type = self::register_test_block(
'test/editor-subfeature-selector',
array( 'typography' => '.typography' ),
null,
array( 'typography' => '.typography' )
);

$selector = wp_get_block_css_selector( $block_type, 'typography.fontSize', true );
$this->assertEquals( '.typography', $selector );
}
}

0 comments on commit 9fcf6d0

Please sign in to comment.