From 88eff634bcb85425d8edf3deb6f0c28461d0e1f9 Mon Sep 17 00:00:00 2001 From: ramonjd Date: Thu, 6 Oct 2022 15:38:25 +1100 Subject: [PATCH 1/2] Moves style engine PHP unit tests to Gutenberg phpunit directory and adds '_Gutenberg' and 'gutenberg_' prefixes/suffixes. After 6.1, the style engine classes and methods are in Core. Moving the tests and using the Gutenberg functions and classes allows us to continue development and avoid class collisions. This PR also proposes to add a bash script to make backporting more easy. --- packages/style-engine/.gitignore | 2 + .../prepare-unit-tests-for-backporting.sh | 59 +++++++++++++++ phpunit.xml.dist | 3 - ...-wp-style-engine-css-declarations-test.php | 28 +++----- .../class-wp-style-engine-css-rule-test.php | 40 ++++------- ...s-wp-style-engine-css-rules-store-test.php | 58 ++++++--------- .../class-wp-style-engine-processor-test.php | 72 +++++++------------ .../style-engine}/style-engine-test.php | 68 ++++++------------ 8 files changed, 152 insertions(+), 178 deletions(-) create mode 100644 packages/style-engine/.gitignore create mode 100755 packages/style-engine/prepare-unit-tests-for-backporting.sh rename {packages/style-engine/phpunit => phpunit/style-engine}/class-wp-style-engine-css-declarations-test.php (88%) rename {packages/style-engine/phpunit => phpunit/style-engine}/class-wp-style-engine-css-rule-test.php (68%) rename {packages/style-engine/phpunit => phpunit/style-engine}/class-wp-style-engine-css-rules-store-test.php (66%) rename {packages/style-engine/phpunit => phpunit/style-engine}/class-wp-style-engine-processor-test.php (75%) rename {packages/style-engine/phpunit => phpunit/style-engine}/style-engine-test.php (89%) diff --git a/packages/style-engine/.gitignore b/packages/style-engine/.gitignore new file mode 100644 index 00000000000000..f2cc4148c8d24a --- /dev/null +++ b/packages/style-engine/.gitignore @@ -0,0 +1,2 @@ +# generated files +phpunit/ diff --git a/packages/style-engine/prepare-unit-tests-for-backporting.sh b/packages/style-engine/prepare-unit-tests-for-backporting.sh new file mode 100755 index 00000000000000..c0f20ca039e01e --- /dev/null +++ b/packages/style-engine/prepare-unit-tests-for-backporting.sh @@ -0,0 +1,59 @@ +#!/bin/bash + +# This script replaces '_Gutenberg' and 'gutenberg_' with '' and 'wp_' for the tests in this directory. +# The intention is to make things a little less tedious for backporting files during Core releases. +# It might be useful to genericize later for wider use. + +# Exit if any command fails. +set -e + +# Enable nicer messaging for build status. +BLUE_BOLD='\033[1;34m'; +GREEN_BOLD='\033[1;32m'; +RED_BOLD='\033[1;31m'; +YELLOW_BOLD='\033[1;33m'; +COLOR_RESET='\033[0m'; +error () { + echo -e "\n${RED_BOLD}$1${COLOR_RESET}\n" +} +status () { + echo -e "\n${BLUE_BOLD}$1${COLOR_RESET}\n" +} +success () { + echo -e "\n${GREEN_BOLD}$1${COLOR_RESET}\n" +} +warning () { + echo -e "\n${YELLOW_BOLD}$1${COLOR_RESET}\n" +} + +warning "This script must be run in the Gutenberg development environment. +It will copy the Gutenberg PHP Unit tests and replace '_Gutenberg' and 'gutenberg_' with '' and 'wp_'" +warning "Shall we go ahead?" +echo -n "[y]es/[N]o: " +read answer +if [ "$answer" != "${answer#[Yy]}" ]; then + cp -r ../../phpunit/style-engine ./ + mv ./style-engine ./phpunit + cd phpunit + success "Copied files ✅" + status "The following matches >>>>>" + find ./ -type f -name "*-test.php" -exec grep -E "_Gutenberg|gutenberg_" {} \; + status "Will be replaced by >>>>>" + find ./ -type f -name "*-test.php" -exec sed -n "s/_Gutenberg/\x0/gp; s/gutenberg_/wp_/gp" {} \; + warning "Shall we go ahead?" + echo -n "[y]es/[N]o: " + read answer + if [ "$answer" != "${answer#[Yy]}" ]; then + # Replaces suffixes and prefixes. + find ./ -type f -name "*-test.php" -exec sed -i '' "s/_Gutenberg/\x0/g; s/gutenberg_/function wp_/g" {} + + success "Okay, done! ✅" + else + error "No worries, aborting.🙂" + exit 1 + fi +else + error "No worries, aborting.🙂" + exit 1 +fi + + diff --git a/phpunit.xml.dist b/phpunit.xml.dist index 2f6e997b45b086..2bc6b7b29900de 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -14,9 +14,6 @@ ./phpunit/ - - ./packages/**/phpunit/ - diff --git a/packages/style-engine/phpunit/class-wp-style-engine-css-declarations-test.php b/phpunit/style-engine/class-wp-style-engine-css-declarations-test.php similarity index 88% rename from packages/style-engine/phpunit/class-wp-style-engine-css-declarations-test.php rename to phpunit/style-engine/class-wp-style-engine-css-declarations-test.php index bbd272b1f9bebc..795933da9efe9b 100644 --- a/packages/style-engine/phpunit/class-wp-style-engine-css-declarations-test.php +++ b/phpunit/style-engine/class-wp-style-engine-css-declarations-test.php @@ -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 { /** @@ -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() ); } @@ -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() ); @@ -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', ); @@ -82,7 +74,7 @@ public function test_should_sanitize_properties() { '^--wp--style--sleepy-potato$' => '40px', '' => '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( @@ -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(). @@ -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(). @@ -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, @@ -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;', @@ -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;', diff --git a/packages/style-engine/phpunit/class-wp-style-engine-css-rule-test.php b/phpunit/style-engine/class-wp-style-engine-css-rule-test.php similarity index 68% rename from packages/style-engine/phpunit/class-wp-style-engine-css-rule-test.php rename to phpunit/style-engine/class-wp-style-engine-css-rule-test.php index 08aa84ce32d11f..26ea41c7ce830f 100644 --- a/packages/style-engine/phpunit/class-wp-style-engine-css-rule-test.php +++ b/phpunit/style-engine/class-wp-style-engine-css-rule-test.php @@ -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 { /** @@ -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.' ); @@ -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() ); @@ -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;}'; @@ -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.' ); @@ -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() ); @@ -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() ); } @@ -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; diff --git a/packages/style-engine/phpunit/class-wp-style-engine-css-rules-store-test.php b/phpunit/style-engine/class-wp-style-engine-css-rules-store-test.php similarity index 66% rename from packages/style-engine/phpunit/class-wp-style-engine-css-rules-store-test.php rename to phpunit/style-engine/class-wp-style-engine-css-rules-store-test.php index e781f5aa49334b..700f63c556c255 100644 --- a/packages/style-engine/phpunit/class-wp-style-engine-css-rules-store-test.php +++ b/phpunit/style-engine/class-wp-style-engine-css-rules-store-test.php @@ -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(); } @@ -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 ); } /** @@ -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.' ); } @@ -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' ); } @@ -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() ); } @@ -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.' ); } @@ -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 = ''; @@ -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 ); @@ -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( @@ -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( diff --git a/packages/style-engine/phpunit/class-wp-style-engine-processor-test.php b/phpunit/style-engine/class-wp-style-engine-processor-test.php similarity index 75% rename from packages/style-engine/phpunit/class-wp-style-engine-processor-test.php rename to phpunit/style-engine/class-wp-style-engine-processor-test.php index d900e615c0776c..cfbde08704bdcb 100644 --- a/packages/style-engine/phpunit/class-wp-style-engine-processor-test.php +++ b/phpunit/style-engine/class-wp-style-engine-processor-test.php @@ -6,30 +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_Processor' ) ) { - require __DIR__ . '/../class-wp-style-engine-processor.php'; -} - -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 compiling and rendering styles from a store of CSS rules. * - * @coversDefaultClass WP_Style_Engine_Processor + * @coversDefaultClass WP_Style_Engine_Processor_Gutenberg */ class WP_Style_Engine_Processor_Test extends WP_UnitTestCase { /** @@ -39,14 +19,14 @@ class WP_Style_Engine_Processor_Test extends WP_UnitTestCase { * @covers ::get_css */ public function test_should_return_rules_as_compiled_css() { - $a_nice_css_rule = new WP_Style_Engine_CSS_Rule( '.a-nice-rule' ); + $a_nice_css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.a-nice-rule' ); $a_nice_css_rule->add_declarations( array( 'color' => 'var(--nice-color)', 'background-color' => 'purple', ) ); - $a_nicer_css_rule = new WP_Style_Engine_CSS_Rule( '.a-nicer-rule' ); + $a_nicer_css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.a-nicer-rule' ); $a_nicer_css_rule->add_declarations( array( 'font-family' => 'Nice sans', @@ -54,7 +34,7 @@ public function test_should_return_rules_as_compiled_css() { 'background-color' => 'purple', ) ); - $a_nice_processor = new WP_Style_Engine_Processor(); + $a_nice_processor = new WP_Style_Engine_Processor_Gutenberg(); $a_nice_processor->add_rules( array( $a_nice_css_rule, $a_nicer_css_rule ) ); $this->assertSame( @@ -69,21 +49,21 @@ public function test_should_return_rules_as_compiled_css() { * @covers ::get_css */ public function test_should_return_prettified_css_rules() { - $a_wonderful_css_rule = new WP_Style_Engine_CSS_Rule( '.a-wonderful-rule' ); + $a_wonderful_css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.a-wonderful-rule' ); $a_wonderful_css_rule->add_declarations( array( 'color' => 'var(--wonderful-color)', 'background-color' => 'orange', ) ); - $a_very_wonderful_css_rule = new WP_Style_Engine_CSS_Rule( '.a-very_wonderful-rule' ); + $a_very_wonderful_css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.a-very_wonderful-rule' ); $a_very_wonderful_css_rule->add_declarations( array( 'color' => 'var(--wonderful-color)', 'background-color' => 'orange', ) ); - $a_more_wonderful_css_rule = new WP_Style_Engine_CSS_Rule( '.a-more-wonderful-rule' ); + $a_more_wonderful_css_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.a-more-wonderful-rule' ); $a_more_wonderful_css_rule->add_declarations( array( 'font-family' => 'Wonderful sans', @@ -91,7 +71,7 @@ public function test_should_return_prettified_css_rules() { 'background-color' => 'orange', ) ); - $a_wonderful_processor = new WP_Style_Engine_Processor(); + $a_wonderful_processor = new WP_Style_Engine_Processor_Gutenberg(); $a_wonderful_processor->add_rules( array( $a_wonderful_css_rule, $a_very_wonderful_css_rule, $a_more_wonderful_css_rule ) ); $expected = '.a-more-wonderful-rule { @@ -117,7 +97,7 @@ public function test_should_return_prettified_css_rules() { * @covers ::add_store */ public function test_should_return_store_rules_as_css() { - $a_nice_store = WP_Style_Engine_CSS_Rules_Store::get_store( 'nice' ); + $a_nice_store = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_store( 'nice' ); $a_nice_store->add_rule( '.a-nice-rule' )->add_declarations( array( 'color' => 'var(--nice-color)', @@ -131,7 +111,7 @@ public function test_should_return_store_rules_as_css() { 'background-color' => 'purple', ) ); - $a_nice_renderer = new WP_Style_Engine_Processor(); + $a_nice_renderer = new WP_Style_Engine_Processor_Gutenberg(); $a_nice_renderer->add_store( $a_nice_store ); $this->assertSame( '.a-nice-rule{color:var(--nice-color);background-color:purple;}.a-nicer-rule{font-family:Nice sans;font-size:1em;background-color:purple;}', @@ -146,8 +126,8 @@ public function test_should_return_store_rules_as_css() { * @covers ::get_css */ public function test_should_dedupe_and_merge_css_declarations() { - $an_excellent_rule = new WP_Style_Engine_CSS_Rule( '.an-excellent-rule' ); - $an_excellent_processor = new WP_Style_Engine_Processor(); + $an_excellent_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.an-excellent-rule' ); + $an_excellent_processor = new WP_Style_Engine_Processor_Gutenberg(); $an_excellent_rule->add_declarations( array( 'color' => 'var(--excellent-color)', @@ -156,7 +136,7 @@ public function test_should_dedupe_and_merge_css_declarations() { ); $an_excellent_processor->add_rules( $an_excellent_rule ); - $another_excellent_rule = new WP_Style_Engine_CSS_Rule( '.an-excellent-rule' ); + $another_excellent_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.an-excellent-rule' ); $another_excellent_rule->add_declarations( array( 'color' => 'var(--excellent-color)', @@ -171,7 +151,7 @@ public function test_should_dedupe_and_merge_css_declarations() { 'Return value of get_css() does not match expectations with new, deduped and merged declarations.' ); - $yet_another_excellent_rule = new WP_Style_Engine_CSS_Rule( '.an-excellent-rule' ); + $yet_another_excellent_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.an-excellent-rule' ); $yet_another_excellent_rule->add_declarations( array( 'color' => 'var(--excellent-color)', @@ -193,7 +173,7 @@ public function test_should_dedupe_and_merge_css_declarations() { * @covers ::get_css */ public function test_should_not_optimize_css_output() { - $a_sweet_rule = new WP_Style_Engine_CSS_Rule( + $a_sweet_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.a-sweet-rule', array( 'color' => 'var(--sweet-color)', @@ -201,7 +181,7 @@ public function test_should_not_optimize_css_output() { ) ); - $a_sweeter_rule = new WP_Style_Engine_CSS_Rule( + $a_sweeter_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '#an-even-sweeter-rule > marquee', array( 'color' => 'var(--sweet-color)', @@ -209,7 +189,7 @@ public function test_should_not_optimize_css_output() { ) ); - $the_sweetest_rule = new WP_Style_Engine_CSS_Rule( + $the_sweetest_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.the-sweetest-rule-of-all a', array( 'color' => 'var(--sweet-color)', @@ -217,7 +197,7 @@ public function test_should_not_optimize_css_output() { ) ); - $a_sweet_processor = new WP_Style_Engine_Processor(); + $a_sweet_processor = new WP_Style_Engine_Processor_Gutenberg(); $a_sweet_processor->add_rules( array( $a_sweet_rule, $a_sweeter_rule, $the_sweetest_rule ) ); $this->assertSame( @@ -237,7 +217,7 @@ public function test_should_not_optimize_css_output() { * @covers ::get_css */ public function test_should_optimize_css_output_by_default() { - $a_sweet_rule = new WP_Style_Engine_CSS_Rule( + $a_sweet_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.a-sweet-rule', array( 'color' => 'var(--sweet-color)', @@ -245,7 +225,7 @@ public function test_should_optimize_css_output_by_default() { ) ); - $a_sweeter_rule = new WP_Style_Engine_CSS_Rule( + $a_sweeter_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '#an-even-sweeter-rule > marquee', array( 'color' => 'var(--sweet-color)', @@ -253,7 +233,7 @@ public function test_should_optimize_css_output_by_default() { ) ); - $a_sweet_processor = new WP_Style_Engine_Processor(); + $a_sweet_processor = new WP_Style_Engine_Processor_Gutenberg(); $a_sweet_processor->add_rules( array( $a_sweet_rule, $a_sweeter_rule ) ); $this->assertSame( @@ -268,15 +248,15 @@ public function test_should_optimize_css_output_by_default() { * @covers ::add_rules */ public function test_should_combine_previously_added_css_rules() { - $a_lovely_processor = new WP_Style_Engine_Processor(); - $a_lovely_rule = new WP_Style_Engine_CSS_Rule( + $a_lovely_processor = new WP_Style_Engine_Processor_Gutenberg(); + $a_lovely_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.a-lovely-rule', array( 'border-color' => 'purple', ) ); $a_lovely_processor->add_rules( $a_lovely_rule ); - $a_lovelier_rule = new WP_Style_Engine_CSS_Rule( + $a_lovelier_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.a-lovelier-rule', array( 'border-color' => 'purple', @@ -289,7 +269,7 @@ public function test_should_combine_previously_added_css_rules() { 'Return value of get_css() does not match expectations when combining 2 CSS rules' ); - $a_most_lovely_rule = new WP_Style_Engine_CSS_Rule( + $a_most_lovely_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.a-most-lovely-rule', array( 'border-color' => 'purple', @@ -297,7 +277,7 @@ public function test_should_combine_previously_added_css_rules() { ); $a_lovely_processor->add_rules( $a_most_lovely_rule ); - $a_perfectly_lovely_rule = new WP_Style_Engine_CSS_Rule( + $a_perfectly_lovely_rule = new WP_Style_Engine_CSS_Rule_Gutenberg( '.a-perfectly-lovely-rule', array( 'border-color' => 'purple', diff --git a/packages/style-engine/phpunit/style-engine-test.php b/phpunit/style-engine/style-engine-test.php similarity index 89% rename from packages/style-engine/phpunit/style-engine-test.php rename to phpunit/style-engine/style-engine-test.php index 2ec8205c742021..aa3fdb29871527 100644 --- a/packages/style-engine/phpunit/style-engine-test.php +++ b/phpunit/style-engine/style-engine-test.php @@ -6,34 +6,6 @@ * @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_Processor' ) ) { - require __DIR__ . '/../class-wp-style-engine-processor.php'; -} - -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'; -} - -if ( ! class_exists( 'WP_Style_Engine' ) ) { - require __DIR__ . '/../class-wp-style-engine.php'; -} - -if ( ! function_exists( 'wp_style_engine_get_styles' ) ) { - require __DIR__ . '/../style-engine.php'; -} - /** * Tests for registering, storing and generating styles. */ @@ -42,22 +14,22 @@ class WP_Style_Engine_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(); } /** * Tests generating block styles and classnames based on various manifestations of the $block_styles argument. * - * @covers ::wp_style_engine_get_styles - * @covers WP_Style_Engine::parse_block_styles - * @covers WP_Style_Engine::compile_css + * @covers ::gutenberg_style_engine_get_styles + * @covers WP_Style_Engine_Gutenberg::parse_block_styles + * @covers WP_Style_Engine_Gutenberg::compile_css * * @dataProvider data_wp_style_engine_get_styles * * @param array $block_styles The incoming block styles object. * @param array $options { - * An array of options to pass to `wp_style_engine_get_styles()`. + * An array of options to pass to `gutenberg_style_engine_get_styles()`. * * @type string|null $context An identifier describing the origin of the style object, e.g., 'block-supports' or 'global-styles'. Default is `null`. * When set, the Style Engine will attempt to store the CSS rules, where a selector is also passed. @@ -68,7 +40,7 @@ public function tear_down() { * @param string $expected_output The expected output. */ public function test_wp_style_engine_get_styles( $block_styles, $options, $expected_output ) { - $generated_styles = wp_style_engine_get_styles( $block_styles, $options ); + $generated_styles = gutenberg_style_engine_get_styles( $block_styles, $options ); $this->assertSame( $expected_output, $generated_styles ); } @@ -508,8 +480,8 @@ public function data_wp_style_engine_get_styles() { /** * Tests adding rules to a store and retrieving a generated stylesheet. * - * @covers ::wp_style_engine_get_styles - * @covers WP_Style_Engine::store_css_rule + * @covers ::gutenberg_style_engine_get_styles + * @covers WP_Style_Engine_Gutenberg::store_css_rule */ public function test_should_store_block_styles_using_context() { $block_styles = array( @@ -523,14 +495,14 @@ public function test_should_store_block_styles_using_context() { ), ); - $generated_styles = wp_style_engine_get_styles( + $generated_styles = gutenberg_style_engine_get_styles( $block_styles, array( 'context' => 'block-supports', 'selector' => 'article', ) ); - $store = WP_Style_Engine::get_store( 'block-supports' ); + $store = WP_Style_Engine_Gutenberg::get_store( 'block-supports' ); $rule = $store->get_all_rules()['article']; $this->assertSame( $generated_styles['css'], $rule->get_css() ); @@ -548,14 +520,14 @@ public function test_should_not_store_block_styles_without_context() { ), ); - wp_style_engine_get_styles( + gutenberg_style_engine_get_styles( $block_styles, array( 'selector' => '#font-size-rulez', ) ); - $all_stores = WP_Style_Engine_CSS_Rules_Store::get_stores(); + $all_stores = WP_Style_Engine_CSS_Rules_Store_Gutenberg::get_stores(); $this->assertEmpty( $all_stores ); } @@ -586,21 +558,21 @@ public function test_should_get_stored_stylesheet_from_context() { ), ), ); - $compiled_stylesheet = wp_style_engine_get_stylesheet_from_css_rules( + $compiled_stylesheet = gutenberg_style_engine_get_stylesheet_from_css_rules( $css_rules, array( 'context' => 'test-store', ) ); - $this->assertSame( $compiled_stylesheet, wp_style_engine_get_stylesheet_from_context( 'test-store' ) ); + $this->assertSame( $compiled_stylesheet, gutenberg_style_engine_get_stylesheet_from_context( 'test-store' ) ); } /** * Tests returning a generated stylesheet from a set of rules. * - * @covers ::wp_style_engine_get_stylesheet_from_css_rules - * @covers WP_Style_Engine::compile_stylesheet_from_css_rules + * @covers ::gutenberg_style_engine_get_stylesheet_from_css_rules + * @covers WP_Style_Engine_Gutenberg::compile_stylesheet_from_css_rules */ public function test_should_return_stylesheet_from_css_rules() { $css_rules = array( @@ -633,7 +605,7 @@ public function test_should_return_stylesheet_from_css_rules() { ), ); - $compiled_stylesheet = wp_style_engine_get_stylesheet_from_css_rules( $css_rules, array( 'prettify' => false ) ); + $compiled_stylesheet = gutenberg_style_engine_get_stylesheet_from_css_rules( $css_rules, array( 'prettify' => false ) ); $this->assertSame( '.saruman{color:white;height:100px;border-style:solid;align-self:unset;}.gandalf{color:grey;height:90px;border-style:dotted;align-self:safe center;}.radagast{color:brown;height:60px;border-style:dashed;align-self:stretch;}', $compiled_stylesheet ); } @@ -641,8 +613,8 @@ public function test_should_return_stylesheet_from_css_rules() { /** * Tests that incoming styles are deduped and merged. * - * @covers ::wp_style_engine_get_stylesheet_from_css_rules - * @covers WP_Style_Engine::compile_stylesheet_from_css_rules + * @covers ::gutenberg_style_engine_get_stylesheet_from_css_rules + * @covers WP_Style_Engine_Gutenberg::compile_stylesheet_from_css_rules */ public function test_should_dedupe_and_merge_css_rules() { $css_rules = array( @@ -681,7 +653,7 @@ public function test_should_dedupe_and_merge_css_rules() { ), ); - $compiled_stylesheet = wp_style_engine_get_stylesheet_from_css_rules( $css_rules, array( 'prettify' => false ) ); + $compiled_stylesheet = gutenberg_style_engine_get_stylesheet_from_css_rules( $css_rules, array( 'prettify' => false ) ); $this->assertSame( '.gandalf{color:white;height:190px;border-style:dotted;padding:10px;margin-bottom:100px;}.dumbledore,.rincewind{color:grey;height:90px;border-style:dotted;}', $compiled_stylesheet ); } From b1fda6f2205b39d472ad02ea3232621f220fa082 Mon Sep 17 00:00:00 2001 From: ramonjd Date: Thu, 6 Oct 2022 19:49:43 +1100 Subject: [PATCH 2/2] Remove copy/rename script Update CHANGELOG.md --- packages/style-engine/.gitignore | 2 - packages/style-engine/CHANGELOG.md | 6 ++ .../prepare-unit-tests-for-backporting.sh | 59 ------------------- 3 files changed, 6 insertions(+), 61 deletions(-) delete mode 100644 packages/style-engine/.gitignore delete mode 100755 packages/style-engine/prepare-unit-tests-for-backporting.sh diff --git a/packages/style-engine/.gitignore b/packages/style-engine/.gitignore deleted file mode 100644 index f2cc4148c8d24a..00000000000000 --- a/packages/style-engine/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -# generated files -phpunit/ diff --git a/packages/style-engine/CHANGELOG.md b/packages/style-engine/CHANGELOG.md index 22fa4ad53848c4..a63ce43535a224 100644 --- a/packages/style-engine/CHANGELOG.md +++ b/packages/style-engine/CHANGELOG.md @@ -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 diff --git a/packages/style-engine/prepare-unit-tests-for-backporting.sh b/packages/style-engine/prepare-unit-tests-for-backporting.sh deleted file mode 100755 index c0f20ca039e01e..00000000000000 --- a/packages/style-engine/prepare-unit-tests-for-backporting.sh +++ /dev/null @@ -1,59 +0,0 @@ -#!/bin/bash - -# This script replaces '_Gutenberg' and 'gutenberg_' with '' and 'wp_' for the tests in this directory. -# The intention is to make things a little less tedious for backporting files during Core releases. -# It might be useful to genericize later for wider use. - -# Exit if any command fails. -set -e - -# Enable nicer messaging for build status. -BLUE_BOLD='\033[1;34m'; -GREEN_BOLD='\033[1;32m'; -RED_BOLD='\033[1;31m'; -YELLOW_BOLD='\033[1;33m'; -COLOR_RESET='\033[0m'; -error () { - echo -e "\n${RED_BOLD}$1${COLOR_RESET}\n" -} -status () { - echo -e "\n${BLUE_BOLD}$1${COLOR_RESET}\n" -} -success () { - echo -e "\n${GREEN_BOLD}$1${COLOR_RESET}\n" -} -warning () { - echo -e "\n${YELLOW_BOLD}$1${COLOR_RESET}\n" -} - -warning "This script must be run in the Gutenberg development environment. -It will copy the Gutenberg PHP Unit tests and replace '_Gutenberg' and 'gutenberg_' with '' and 'wp_'" -warning "Shall we go ahead?" -echo -n "[y]es/[N]o: " -read answer -if [ "$answer" != "${answer#[Yy]}" ]; then - cp -r ../../phpunit/style-engine ./ - mv ./style-engine ./phpunit - cd phpunit - success "Copied files ✅" - status "The following matches >>>>>" - find ./ -type f -name "*-test.php" -exec grep -E "_Gutenberg|gutenberg_" {} \; - status "Will be replaced by >>>>>" - find ./ -type f -name "*-test.php" -exec sed -n "s/_Gutenberg/\x0/gp; s/gutenberg_/wp_/gp" {} \; - warning "Shall we go ahead?" - echo -n "[y]es/[N]o: " - read answer - if [ "$answer" != "${answer#[Yy]}" ]; then - # Replaces suffixes and prefixes. - find ./ -type f -name "*-test.php" -exec sed -i '' "s/_Gutenberg/\x0/g; s/gutenberg_/function wp_/g" {} + - success "Okay, done! ✅" - else - error "No worries, aborting.🙂" - exit 1 - fi -else - error "No worries, aborting.🙂" - exit 1 -fi - -