diff --git a/.gitignore b/.gitignore index 97da45fa..f2dbdb60 100644 --- a/.gitignore +++ b/.gitignore @@ -23,6 +23,7 @@ phpcs.xml.dist /source/wp-content/mu-plugins/* # ...except the actual source of this project. +!/source/wp-content/tests !/source/wp-content/themes !/source/wp-content/themes/wporg-main-2022 !/source/wp-content/mu-plugins diff --git a/.wp-env.json b/.wp-env.json index a90954ef..d3a76826 100644 --- a/.wp-env.json +++ b/.wp-env.json @@ -28,5 +28,16 @@ "wp-content/mu-plugins": "./source/wp-content/mu-plugins", "wp-content/mu-plugins/0-sandbox.php": "./env/0-sandbox.php", "wp-cli.yml": "./wp-cli.yml" + }, + "env": { + "tests": { + "config": { + "WP_ENVIRONMENT_TYPE": false + }, + "mappings": { + "wp-content/tests": "./source/wp-content/tests", + "vendor": "./vendor" + } + } } } diff --git a/composer.json b/composer.json index eb07ef49..23936b5b 100644 --- a/composer.json +++ b/composer.json @@ -100,7 +100,8 @@ "wpackagist-plugin/wordpress-importer": "*", "wporg/wporg-repo-tools": "dev-trunk", "wporg/wporg-mu-plugins": "dev-build", - "wporg/wporg-parent-2021": "dev-build" + "wporg/wporg-parent-2021": "dev-build", + "yoast/phpunit-polyfills": "^1.0" }, "scripts": { "format": "phpcbf -p", diff --git a/composer.lock b/composer.lock index b2501792..6fdb599c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "dbc668c35d44dec32c81c5dce68077a4", + "content-hash": "652c9baa4366ca13314c13344b96f9cc", "packages": [], "packages-dev": [ { @@ -2367,12 +2367,12 @@ "source": { "type": "git", "url": "https://github.com/WordPress/wporg-mu-plugins.git", - "reference": "a6217709c8706986582c855a121661178ff685d0" + "reference": "ae072f27720086a01364d090077698c81a2efd87" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/WordPress/wporg-mu-plugins/zipball/a6217709c8706986582c855a121661178ff685d0", - "reference": "a6217709c8706986582c855a121661178ff685d0", + "url": "https://api.github.com/repos/WordPress/wporg-mu-plugins/zipball/ae072f27720086a01364d090077698c81a2efd87", + "reference": "ae072f27720086a01364d090077698c81a2efd87", "shasum": "" }, "require-dev": { @@ -2410,7 +2410,7 @@ "source": "https://github.com/WordPress/wporg-mu-plugins/tree/build", "issues": "https://github.com/WordPress/wporg-mu-plugins/issues" }, - "time": "2023-03-29T04:29:54+00:00" + "time": "2023-03-31T15:33:47+00:00" }, { "name": "wporg/wporg-parent-2021", @@ -2462,6 +2462,66 @@ "source": "https://github.com/WordPress/wporg-repo-tools/tree/trunk" }, "time": "2022-11-16T17:56:26+00:00" + }, + { + "name": "yoast/phpunit-polyfills", + "version": "1.0.5", + "source": { + "type": "git", + "url": "https://github.com/Yoast/PHPUnit-Polyfills.git", + "reference": "3b59adeef77fb1c03ff5381dbb9d68b0aaff3171" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Yoast/PHPUnit-Polyfills/zipball/3b59adeef77fb1c03ff5381dbb9d68b0aaff3171", + "reference": "3b59adeef77fb1c03ff5381dbb9d68b0aaff3171", + "shasum": "" + }, + "require": { + "php": ">=5.4", + "phpunit/phpunit": "^4.8.36 || ^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0" + }, + "require-dev": { + "yoast/yoastcs": "^2.3.0" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-main": "2.x-dev" + } + }, + "autoload": { + "files": [ + "phpunitpolyfills-autoload.php" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "BSD-3-Clause" + ], + "authors": [ + { + "name": "Team Yoast", + "email": "support@yoast.com", + "homepage": "https://yoast.com" + }, + { + "name": "Contributors", + "homepage": "https://github.com/Yoast/PHPUnit-Polyfills/graphs/contributors" + } + ], + "description": "Set of polyfills for changed PHPUnit functionality to allow for creating PHPUnit cross-version compatible tests", + "homepage": "https://github.com/Yoast/PHPUnit-Polyfills", + "keywords": [ + "phpunit", + "polyfill", + "testing" + ], + "support": { + "issues": "https://github.com/Yoast/PHPUnit-Polyfills/issues", + "source": "https://github.com/Yoast/PHPUnit-Polyfills" + }, + "time": "2023-03-30T23:39:05+00:00" } ], "aliases": [], diff --git a/env/export-content/tests/block-parser-test.php b/env/export-content/tests/block-parser-test.php new file mode 100644 index 00000000..97b86c78 --- /dev/null +++ b/env/export-content/tests/block-parser-test.php @@ -0,0 +1,141 @@ +\n

One.

\n\n\n\n

Two.

\n", + [ 'One.', 'Two.' ], + ], + [ + // A paragraph with nested HTML. + "\n

A paragraph with bold text.

\n", + [ 'A paragraph with bold text.' ], + ], + [ + // A paragraph with a nested link. + "\n

A paragraph with a link.

\n", + [ 'A paragraph with a link.' ], + ], + [ + // Empty paragraph. + "\n

\n\n", + [], + ], + [ + // Buttons. + "\n
\n
Button 1
\n\n\n\n
Button 2
\n
\n", + [ 'Button 1', 'Button 2' ], + ], + [ + // Column with a list, list-items. + "\n
\n\n
\n", + [ 'Simple', 'Intuitive', 'Extendable', 'Free', 'Open' ], + ], + [ + // Image block with an alt. + "\n
\"Download
\n", + [ 'Download on the Apple App Store' ], + ], + [ + // Navigation with custom navigation links. + "\n\n\n\n\n\n\n\n", + [ 'Releases', 'Nightly', 'Counter', 'Source' ], + ], + [ + // Cover with background image (with alt), and nested paragraph. + "\n
\"Some
\n

Testing a Cover

\n
\n", + [ 'Some alt.', 'Testing a Cover' ], + ], + [ + // Social links. + "\n\n", + [ 'Visit our Facebook page', 'Visit our Twitter account' ], + ], + [ + // List with links + "\n\n", + [ 'Fonts API', 'Interactivity Link API', 'Block API' ], + ], + [ + // List of lists + "\n\n", + [ 'APIs:', 'Fonts API', 'Interactivity API', 'Block API' ], + ], + ]; + } + + /** + * Test string parsing from valid block content. + * + * @dataProvider data_block_content_strings + */ + public function test_strings_parser( $block_content, $expected ) { + $parser = new BlockParser( $block_content ); + $strings = $parser->to_strings(); + $this->assertSame( $expected, $strings ); + } + + /** + * Data provider for valid block content and the i18n-ized results. + * + * @return array + */ + public function data_block_content_i18n() { + return [ + [ + // Two plain paragraphs. + "\n

One.

\n\n\n\n

Two.

\n", + "\n

\n\n\n\n

\n", + ], + [ + // Image block with an alt. + "\n
\"Download
\n", + "\n
\"<?php\" width=\"150\" height=\"45\" />
\n", + ], + [ + // Navigation with custom navigation links. + "\n\n", + "\n\n", + ], + [ + // List with links + "\n\n", + "\n\n", + ], + [ + // List of lists + "\n\n\n", + "\n\n\n", + ], + [ + "\n
\m

I'm interested in running the open-source WordPress <https://wordpress.org/> web software and I was wondering if my account supported the following:

\n
\n", + "\n
\m

\n
\n", + ], + ]; + } + + /** + * Test the i18n replacement. + * + * @dataProvider data_block_content_i18n + */ + public function test_i18n_replacement( $block_content, $expected ) { + $content_with_i18n = replace_with_i18n( $block_content ); + $this->assertSame( $expected, $content_with_i18n ); + } +} diff --git a/package.json b/package.json index c036c987..54cbe512 100644 --- a/package.json +++ b/package.json @@ -26,7 +26,8 @@ "build:theme": "yarn workspace wporg-main-2022-theme build", "lint:frontend": "yarn workspace wporg-main-2022-theme lint:css && yarn workspace wporg-main-2022-theme lint:js", "lint:php": "composer run lint source/wp-content/themes/wporg-main-2022 source/wp-content/mu-plugins/theme-switcher.php env", - "start:theme": "yarn workspace wporg-main-2022-theme start" + "start:theme": "yarn workspace wporg-main-2022-theme start", + "test:php": "wp-env run phpunit 'WP_MULTISITE=1 phpunit -c /var/www/html/wp-content/tests/phpunit/phpunit.xml --verbose'" }, "workspaces": [ "source/wp-content/themes/wporg-main-2022" diff --git a/source/wp-content/tests/phpunit/bootstrap.php b/source/wp-content/tests/phpunit/bootstrap.php new file mode 100644 index 00000000..2d4525f3 --- /dev/null +++ b/source/wp-content/tests/phpunit/bootstrap.php @@ -0,0 +1,44 @@ +get_error_message(); + } + + throw new Exception( 'WordPress died: ' . $message ); +} +tests_add_filter( 'wp_die_handler', 'fail_if_died' ); + +// Start up the WP testing environment. +require_once $_test_root . '/includes/bootstrap.php'; + +// Use existing behavior for wp_die during actual test execution. +remove_filter( 'wp_die_handler', 'fail_if_died' ); diff --git a/source/wp-content/tests/phpunit/phpunit.xml b/source/wp-content/tests/phpunit/phpunit.xml new file mode 100644 index 00000000..fcda9e0b --- /dev/null +++ b/source/wp-content/tests/phpunit/phpunit.xml @@ -0,0 +1,14 @@ + + + + ../../../env/export-content/tests + + +