Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Style Engine: move PHP unit tests to Gutenberg #44722

Merged
merged 2 commits into from
Oct 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions packages/style-engine/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,14 @@

## Unreleased

### Internal
- Style Engine: move PHP unit tests to Gutenberg [#44722](https://github.com/WordPress/gutenberg/pull/44722)

## 1.2.0 (2022-10-05)

### Internal
- Script loader: remove 6.1 wp actions ([#44519](https://github.com/WordPress/gutenberg/pull/44519))

## 1.1.0 (2022-09-21)

### Enhancement
Expand Down
3 changes: 0 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
<testsuite name="default">
<directory suffix="-test.php">./phpunit/</directory>
</testsuite>
<testsuite name="packages">
<directory suffix="-test.php">./packages/**/phpunit/</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,10 @@
* @subpackage style-engine
*/

// Check for the existence of Style Engine classes and methods.
// Once the Style Engine has been migrated to Core we can remove the if statements and require imports.
// Testing new features from the Gutenberg package may require
// testing against `gutenberg_` and `_Gutenberg` functions and methods in the future.
if ( ! class_exists( 'WP_Style_Engine_CSS_Declarations' ) ) {
require __DIR__ . '/../class-wp-style-engine-css-declarations.php';
}

/**
* Tests registering, storing and generating CSS declarations.
*
* @coversDefaultClass WP_Style_Engine_CSS_Declarations
* @coversDefaultClass WP_Style_Engine_CSS_Declarations_Gutenberg
*/
class WP_Style_Engine_CSS_Declarations_Test extends WP_UnitTestCase {
/**
Expand All @@ -30,7 +22,7 @@ public function test_should_instantiate_with_declarations() {
'margin-top' => '10px',
'font-size' => '2rem',
);
$css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $input_declarations );
$this->assertSame( $input_declarations, $css_declarations->get_declarations() );
}

Expand All @@ -45,7 +37,7 @@ public function test_should_add_declarations() {
'padding' => '20px',
'color' => 'var(--wp--preset--elbow-patches)',
);
$css_declarations = new WP_Style_Engine_CSS_Declarations();
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg();
$css_declarations->add_declarations( $input_declarations );

$this->assertSame( $input_declarations, $css_declarations->get_declarations() );
Expand All @@ -62,7 +54,7 @@ public function test_should_add_new_declarations_to_existing() {
'border-width' => '1%',
'background-color' => 'var(--wp--preset--english-mustard)',
);
$css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $input_declarations );
$extra_declaration = array(
'letter-spacing' => '1.5px',
);
Expand All @@ -82,7 +74,7 @@ public function test_should_sanitize_properties() {
'^--wp--style--sleepy-potato$' => '40px',
'<background-//color>' => 'var(--wp--preset--english-mustard)',
);
$css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $input_declarations );

$this->assertSame(
array(
Expand All @@ -107,7 +99,7 @@ public function test_should_strip_html_tags_and_remove_unsafe_css_properties() {
'cheese' => '10px',
'margin-right' => '10em',
);
$css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $input_declarations );
$safe_style_css_mock_action = new MockAction();

// filter_declaration() is called in get_declarations_string().
Expand Down Expand Up @@ -146,7 +138,7 @@ public function test_should_allow_css_functions_and_strip_unsafe_css_values() {
'line-height' => 'url("https://wordpress.org")',
'margin' => 'illegalfunction(30px)',
);
$css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $input_declarations );
$safecss_filter_attr_allow_css_mock_action = new MockAction();

// filter_declaration() is called in get_declarations_string().
Expand Down Expand Up @@ -183,7 +175,7 @@ public function test_should_compile_css_declarations_to_css_declarations_string(
'border-top-left-radius' => '99px',
'text-decoration' => 'underline',
);
$css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $input_declarations );

$this->assertSame(
$expected,
Expand Down Expand Up @@ -234,7 +226,7 @@ public function test_should_remove_single_declaration() {
'margin' => '10em 10em 20em 1px',
'font-family' => 'Happy Font serif',
);
$css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $input_declarations );

$this->assertSame(
'color:tomato;margin:10em 10em 20em 1px;font-family:Happy Font serif;',
Expand Down Expand Up @@ -262,7 +254,7 @@ public function test_should_remove_multiple_declarations() {
'margin' => '10em 10em 20em 1px',
'font-family' => 'Happy Font serif',
);
$css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $input_declarations );

$this->assertSame(
'color:cucumber;margin:10em 10em 20em 1px;font-family:Happy Font serif;',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,10 @@
* @subpackage style-engine
*/

// Check for the existence of Style Engine classes and methods.
// Once the Style Engine has been migrated to Core we can remove the if statements and require imports.
// Testing new features from the Gutenberg package may require
// testing against `gutenberg_` and `_Gutenberg` functions and methods in the future.
if ( ! class_exists( 'WP_Style_Engine_CSS_Declarations' ) ) {
require __DIR__ . '/../class-wp-style-engine-css-declarations.php';
}

if ( ! class_exists( 'WP_Style_Engine_CSS_Rule' ) ) {
require __DIR__ . '/../class-wp-style-engine-css-rule.php';
}

/**
* Tests for registering, storing and generating CSS rules.
*
* @coversDefaultClass WP_Style_Engine_CSS_Rule
* @coversDefaultClass WP_Style_Engine_CSS_Rule_Gutenberg
*/
class WP_Style_Engine_CSS_Rule_Test extends WP_UnitTestCase {
/**
Expand All @@ -35,8 +23,8 @@ public function test_should_instantiate_with_selector_and_rules() {
'margin-top' => '10px',
'font-size' => '2rem',
);
$css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
$css_rule = new WP_Style_Engine_CSS_Rule( $selector, $css_declarations );
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $input_declarations );
$css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( $selector, $css_declarations );

$this->assertSame( $selector, $css_rule->get_selector(), 'Return value of get_selector() does not match value passed to constructor.' );

Expand All @@ -59,8 +47,8 @@ public function test_should_dedupe_properties_in_rules() {
$overwrite_first_declaration = array(
'font-size' => '4px',
);
$css_rule = new WP_Style_Engine_CSS_Rule( $selector, $first_declaration );
$css_rule->add_declarations( new WP_Style_Engine_CSS_Declarations( $overwrite_first_declaration ) );
$css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( $selector, $first_declaration );
$css_rule->add_declarations( new WP_Style_Engine_CSS_Declarations_Gutenberg( $overwrite_first_declaration ) );

$expected = '.taggart{font-size:4px;}';
$this->assertSame( $expected, $css_rule->get_css() );
Expand All @@ -74,10 +62,10 @@ public function test_should_dedupe_properties_in_rules() {
*/
public function test_should_add_declarations_to_existing_rules() {
// Declarations using a WP_Style_Engine_CSS_Declarations object.
$some_css_declarations = new WP_Style_Engine_CSS_Declarations( array( 'margin-top' => '10px' ) );
$some_css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( array( 'margin-top' => '10px' ) );
// Declarations using a property => value array.
$some_more_css_declarations = array( 'font-size' => '1rem' );
$css_rule = new WP_Style_Engine_CSS_Rule( '.hill-street-blues', $some_css_declarations );
$css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.hill-street-blues', $some_css_declarations );
$css_rule->add_declarations( $some_more_css_declarations );

$expected = '.hill-street-blues{margin-top:10px;font-size:1rem;}';
Expand All @@ -92,7 +80,7 @@ public function test_should_add_declarations_to_existing_rules() {
*/
public function test_should_set_selector() {
$selector = '.taggart';
$css_rule = new WP_Style_Engine_CSS_Rule( $selector );
$css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( $selector );

$this->assertSame( $selector, $css_rule->get_selector(), 'Return value of get_selector() does not match value passed to constructor.' );

Expand All @@ -112,8 +100,8 @@ public function test_should_generate_css_rule_string() {
'margin-top' => '10px',
'font-size' => '2rem',
);
$css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
$css_rule = new WP_Style_Engine_CSS_Rule( $selector, $css_declarations );
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $input_declarations );
$css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( $selector, $css_declarations );
$expected = "$selector{{$css_declarations->get_declarations_string()}}";

$this->assertSame( $expected, $css_rule->get_css() );
Expand All @@ -127,8 +115,8 @@ public function test_should_generate_css_rule_string() {
public function test_should_return_empty_string_with_no_declarations() {
$selector = '.holmes';
$input_declarations = array();
$css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
$css_rule = new WP_Style_Engine_CSS_Rule( $selector, $css_declarations );
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $input_declarations );
$css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( $selector, $css_declarations );

$this->assertSame( '', $css_rule->get_css() );
}
Expand All @@ -144,8 +132,8 @@ public function test_should_prettify_css_rule_output() {
'margin-left' => '0',
'font-family' => 'Detective Sans',
);
$css_declarations = new WP_Style_Engine_CSS_Declarations( $input_declarations );
$css_rule = new WP_Style_Engine_CSS_Rule( $selector, $css_declarations );
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $input_declarations );
$css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( $selector, $css_declarations );
$expected = '.baptiste {
margin-left: 0;
font-family: Detective Sans;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,17 @@
* @subpackage style-engine
*/

// Check for the existence of Style Engine classes and methods.
// Once the Style Engine has been migrated to Core we can remove the if statements and require imports.
// Testing new features from the Gutenberg package may require
// testing against `gutenberg_` and `_Gutenberg` functions and methods in the future.
if ( ! class_exists( 'WP_Style_Engine_CSS_Declarations' ) ) {
require __DIR__ . '/../class-wp-style-engine-css-declarations.php';
}

if ( ! class_exists( 'WP_Style_Engine_CSS_Rule' ) ) {
require __DIR__ . '/../class-wp-style-engine-css-rule.php';
}

if ( ! class_exists( 'WP_Style_Engine_CSS_Rules_Store' ) ) {
require __DIR__ . '/../class-wp-style-engine-css-rules-store.php';
}

/**
* Tests for registering, storing and retrieving a collection of CSS Rules (a store).
*
* @coversDefaultClass WP_Style_Engine_CSS_Rules_Store
* @coversDefaultClass WP_Style_Engine_CSS_Rules_Store_Gutenberg
*/
class WP_Style_Engine_CSS_Rules_Store_Test extends WP_UnitTestCase {
/**
* Cleans up stores after each test.
*/
public function tear_down() {
WP_Style_Engine_CSS_Rules_Store::remove_all_stores();
WP_Style_Engine_CSS_Rules_Store_Gutenberg::remove_all_stores();
parent::tear_down();
}

Expand All @@ -42,9 +26,9 @@ public function tear_down() {
* @covers ::__construct
*/
public function test_should_create_new_store_on_instantiation() {
$new_pancakes_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'pancakes-with-strawberries' );
$new_pancakes_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'pancakes-with-strawberries' );

$this->assertInstanceOf( 'WP_Style_Engine_CSS_Rules_Store', $new_pancakes_store );
$this->assertInstanceOf( 'WP_Style_Engine_CSS_Rules_Store_Gutenberg', $new_pancakes_store );
}

/**
Expand All @@ -53,15 +37,15 @@ public function test_should_create_new_store_on_instantiation() {
* @covers ::get_store
*/
public function test_should_not_create_store_without_a_store_name() {
$not_a_store = WP_Style_Engine_CSS_Rules_Store::get_store( '' );
$not_a_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( '' );

$this->assertEmpty( $not_a_store, 'get_store() did not return an empty value with empty string as argument.' );

$also_not_a_store = WP_Style_Engine_CSS_Rules_Store::get_store( 123 );
$also_not_a_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 123 );

$this->assertEmpty( $also_not_a_store, 'get_store() did not return an empty value with number as argument.' );

$definitely_not_a_store = WP_Style_Engine_CSS_Rules_Store::get_store( null );
$definitely_not_a_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( null );

$this->assertEmpty( $definitely_not_a_store, 'get_store() did not return an empty value with `null` as argument.' );
}
Expand All @@ -72,14 +56,14 @@ public function test_should_not_create_store_without_a_store_name() {
* @covers ::get_store
*/
public function test_should_return_existing_store() {
$new_fish_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'fish-n-chips' );
$new_fish_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'fish-n-chips' );
$selector = '.haddock';

$new_fish_store->add_rule( $selector );

$this->assertSame( $selector, $new_fish_store->add_rule( $selector )->get_selector(), 'Selector string of store rule does not match expected value' );

$the_same_fish_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'fish-n-chips' );
$the_same_fish_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'fish-n-chips' );

$this->assertSame( $selector, $the_same_fish_store->add_rule( $selector )->get_selector(), 'Selector string of existing store rule does not match expected value' );
}
Expand All @@ -90,15 +74,15 @@ public function test_should_return_existing_store() {
* @covers ::get_stores
*/
public function test_should_get_all_existing_stores() {
$burrito_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'burrito' );
$quesadilla_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'quesadilla' );
$burrito_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'burrito' );
$quesadilla_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'quesadilla' );

$this->assertEquals(
array(
'burrito' => $burrito_store,
'quesadilla' => $quesadilla_store,
),
WP_Style_Engine_CSS_Rules_Store::get_stores()
WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_stores()
);
}

Expand All @@ -108,22 +92,22 @@ public function test_should_get_all_existing_stores() {
* @covers ::remove_all_stores
*/
public function test_should_remove_all_stores() {
$dolmades_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'dolmades' );
$tzatziki_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'tzatziki' );
$dolmades_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'dolmades' );
$tzatziki_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'tzatziki' );

$this->assertEquals(
array(
'dolmades' => $dolmades_store,
'tzatziki' => $tzatziki_store,
),
WP_Style_Engine_CSS_Rules_Store::get_stores(),
WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_stores(),
'Return value of get_stores() does not match expectation'
);
WP_Style_Engine_CSS_Rules_Store::remove_all_stores();
WP_Style_Engine_CSS_Rules_Store_Gutenberg::remove_all_stores();

$this->assertEquals(
array(),
WP_Style_Engine_CSS_Rules_Store::get_stores(),
WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_stores(),
'Return value of get_stores() is not an empty array after remove_all_stores() called.'
);
}
Expand All @@ -134,7 +118,7 @@ public function test_should_remove_all_stores() {
* @covers ::add_rule
*/
public function test_should_add_rule_to_existing_store() {
$new_pie_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'meat-pie' );
$new_pie_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'meat-pie' );
$selector = '.wp-block-sauce a:hover';
$store_rule = $new_pie_store->add_rule( $selector );
$expected = '';
Expand All @@ -146,7 +130,7 @@ public function test_should_add_rule_to_existing_store() {
'border-color' => 'yellow',
'border-radius' => '10rem',
);
$css_declarations = new WP_Style_Engine_CSS_Declarations( $pie_declarations );
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $pie_declarations );
$store_rule->add_declarations( $css_declarations );

$store_rule = $new_pie_store->add_rule( $selector );
Expand All @@ -161,7 +145,7 @@ public function test_should_add_rule_to_existing_store() {
* @covers ::get_all_rules
*/
public function test_should_get_all_rule_objects_for_a_store() {
$new_pizza_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'pizza-with-mozzarella' );
$new_pizza_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'pizza-with-mozzarella' );
$selector = '.wp-block-anchovies a:hover';
$store_rule = $new_pizza_store->add_rule( $selector );
$expected = array(
Expand All @@ -175,7 +159,7 @@ public function test_should_get_all_rule_objects_for_a_store() {
'padding' => '100px',
);
$new_store_rule = $new_pizza_store->add_rule( $new_selector );
$css_declarations = new WP_Style_Engine_CSS_Declarations( $newer_pizza_declarations );
$css_declarations = new WP_Style_Engine_CSS_Declarations_Gutenberg( $newer_pizza_declarations );
$new_store_rule->add_declarations( array( $css_declarations ) );

$expected = array(
Expand Down
Loading