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

Add export content parsers tests #181

Closed
wants to merge 10 commits into from
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ phpcs.xml.dist
/source/wp-content/mu-plugins/*
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe need to add /source/wp-content/tests/* here then un-ignore phpunit to make docker load tests...


# ...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
Expand Down
11 changes: 11 additions & 0 deletions .wp-env.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
}
}
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
71 changes: 66 additions & 5 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions env/export-content/includes/parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
require_once __DIR__ . '/parsers/BasicText.php';
require_once __DIR__ . '/parsers/Button.php';
require_once __DIR__ . '/parsers/Heading.php';
require_once __DIR__ . '/parsers/ListItem.php';
require_once __DIR__ . '/parsers/Noop.php';
require_once __DIR__ . '/parsers/Paragraph.php';

Expand All @@ -21,10 +22,11 @@ class BlockParser {
public function __construct() {
$this->parsers = [
// Core blocks that have custom parsers.
'core/paragraph' => new Parsers\Paragraph(),
'core/heading' => new Parsers\Heading(),
'core/button' => new Parsers\Button(),
'core/spacer' => new Parsers\Noop(),
'core/paragraph' => new Parsers\Paragraph(),
'core/list-item' => new Parsers\ListItem(),
'core/heading' => new Parsers\Heading(),
'core/button' => new Parsers\Button(),
'core/spacer' => new Parsers\Noop(),

// Common core blocks that use the default parser.
'core/buttons' => new Parsers\BasicText(),
Expand Down
40 changes: 40 additions & 0 deletions env/export-content/includes/parsers/ListItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace WordPress_org\Main_2022\ExportToPatterns\Parsers;

class ListItem implements BlockParser {
use GetSetAttribute;

public function to_strings( array $block ) : array {
$strings = $this->get_attribute( 'placeholder', $block );

$matches = [];

if ( preg_match( '/<li[^>]*>(.+)<\/li>/is', $block['innerHTML'], $matches ) ) {
if ( ! empty( $matches[1] ) ) {
$strings[] = $matches[1];
}
}

return $strings;
}

// todo: this needs a fix to properly rebuild innerContent - similar to ParagraphParserTest
public function replace_strings( array $block, array $replacements ) : array {
$this->set_attribute( 'placeholder', $block, $replacements );

$html = $block['innerHTML'];

foreach ( $this->to_strings( $block ) as $original ) {
if ( ! empty( $original ) && isset( $replacements[ $original ] ) ) {
$regex = '#(<li[^>]*>)(' . preg_quote( $original, '/' ) . ')(<\/li>)#is';
$html = preg_replace( $regex, '${1}' . addcslashes( $replacements[ $original ], '\\$' ) . '${3}', $html );
}
}

$block['innerHTML'] = $html;
$block['innerContent'] = [ $html ];

return $block;
}
}
37 changes: 37 additions & 0 deletions env/export-content/tests/phpunit/parser-list-item-test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

defined( 'WPINC' ) || die();

require_once dirname( dirname( __DIR__ ) ) . '/includes/parsers/BlockParser.php';
require_once dirname( dirname( __DIR__ ) ) . '/includes/parsers/ListItem.php';

use WordPress_org\Main_2022\ExportToPatterns\Parsers;

class Test_Parser_List_Item extends WP_UnitTestCase {

/**
* @covers WordPress_org\Main_2022\ExportToPatterns\Parsers\ListItem\replace_strings
*/
public function test_totp_setup_returns_expected_data() : void {
$parser = new Parsers\ListItem();
$block = array(
'blockName' => 'core/list-item',
'innerHTML' => '<li><a href="https://learn.wordpress.org/">Learn with WordPress ↗</a></li>',
'innerContent' => array( '<li><a href="https://learn.wordpress.org/">Learn with WordPress ↗</a></li>' ),
);
$replacements = array(
'<a href="https://learn.wordpress.org/">Learn with WordPress ↗</a>' => '<?php _e( "<a href="https://learn.wordpress.org/">Learn with WordPress ↗</a>", "wporg" ); ?>',
);

$parsed_block = $parser->replace_strings( $block, $replacements );

$this->assertSame(
$parsed_block,
array(
'blockName' => 'core/list-item',
'innerHTML' => '<li><?php _e( "<a href="https://learn.wordpress.org/">Learn with WordPress ↗</a>", "wporg" ); ?></li>',
'innerContent' => array( '<li><?php _e( "<a href="https://learn.wordpress.org/">Learn with WordPress ↗</a>", "wporg" ); ?></li>' ),
),
);
}
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"lighthouse": "lighthouse-ci http://localhost:8888/ --accessibility=100 --best-practices=100 --seo=100",
"lighthouse:desktop": "lighthouse http://localhost:8888/ --view --preset=desktop --output-path=lighthouse.html",
"lighthouse:mobile": "lighthouse http://localhost:8888/ --view --screenEmulation.mobile --output-path=lighthouse.html",
"build:patterns": "./env/build-patterns.sh"
"build:patterns": "./env/build-patterns.sh",
"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"
Expand Down
44 changes: 44 additions & 0 deletions source/wp-content/tests/phpunit/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* PHPUnit bootstrap file
*/

// Require composer dependencies.
require_once __DIR__ . '/../../../vendor/autoload.php';

// Detect where to load the WordPress tests environment from.
if ( false !== getenv( 'WP_TESTS_DIR' ) ) {
$_test_root = getenv( 'WP_TESTS_DIR' );
} else {
$_test_root = __DIR__ . '/../../../vendor/wp-phpunit/wp-phpunit';
}

// Give access to tests_add_filter() function.
require_once $_test_root . '/includes/functions.php';

/**
* Adds a wp_die handler for use during tests.
*
* If bootstrap.php triggers wp_die, it will not cause the script to fail. This
* means that tests will look like they passed even though they should have
* failed. So we throw an exception if WordPress dies during test setup. This
* way the failure is observable.
*
* @param string|WP_Error $message The error message.
*
* @throws Exception When a `wp_die()` occurs.
*/
function fail_if_died( $message ) {
if ( is_wp_error( $message ) ) {
$message = $message->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' );
14 changes: 14 additions & 0 deletions source/wp-content/tests/phpunit/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<phpunit
bootstrap="./bootstrap.php"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite name="export-content">
<directory suffix="-test.php">../../../env/export-content/tests/phpunit</directory>
</testsuite>
</testsuites>
</phpunit>
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li><?php _e( 'Subversion: ', 'wporg' ); ?><code><?php _e( 'https://core.svn.wordpress.org/', 'wporg' ); ?></code></li>
<li><?php _e( 'Subversion: <code>https://core.svn.wordpress.org/</code>', 'wporg' ); ?></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><?php _e( 'Git mirror: ', 'wporg' ); ?><code><?php _e( 'git://core.git.wordpress.org/', 'wporg' ); ?></code></li>
<li><?php _e( 'Git mirror: <code>git://core.git.wordpress.org/</code>', 'wporg' ); ?></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

Expand All @@ -49,11 +49,11 @@

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li><?php _e( 'Subversion: ', 'wporg' ); ?><code><?php _e( 'https://develop.svn.wordpress.org/', 'wporg' ); ?></code></li>
<li><?php _e( 'Subversion: <code>https://develop.svn.wordpress.org/</code>', 'wporg' ); ?></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><?php _e( 'Git mirror: ', 'wporg' ); ?><code><?php _e( 'git://develop.git.wordpress.org/', 'wporg' ); ?></code></li>
<li><?php _e( 'Git mirror: <code>git://develop.git.wordpress.org/</code>', 'wporg' ); ?></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,19 +131,19 @@
<!-- wp:column -->
<div class="wp-block-column"><!-- wp:list {"className":"is-style-links-list"} -->
<ul class="is-style-links-list"><!-- wp:list-item -->
<li><a href="https://learn.wordpress.org/course/getting-started-with-wordpress-get-setup/"><?php _e( 'WordPress courses ↗', 'wporg' ); ?></a></li>
<li><?php _e( '<a href="https://learn.wordpress.org/course/getting-started-with-wordpress-get-setup/">WordPress courses ↗</a>', 'wporg' ); ?></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><a href="https://developer.wordpress.org/"><?php _e( 'Developer resources ↗', 'wporg' ); ?></a></li>
<li><?php _e( '<a href="https://developer.wordpress.org/">Developer resources ↗</a>', 'wporg' ); ?></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><a href="https://wordpress.org/support/"><?php _e( 'WordPress support ↗', 'wporg' ); ?></a></li>
<li><?php _e( '<a href="https://wordpress.org/support/">WordPress support ↗</a>', 'wporg' ); ?></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><a href="https://wordpress.org/support/forum/installation/"><?php _e( 'User forums ↗', 'wporg' ); ?></a></li>
<li><?php _e( '<a href="https://wordpress.org/support/forum/installation/">User forums ↗</a>', 'wporg' ); ?></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list --></div>
<!-- /wp:column --></div>
Expand Down
Loading