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
12 changes: 12 additions & 0 deletions .wp-env.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,20 @@
],
"mappings": {
"env": "./env",
"wp-content": "./source/wp-content",
"wp-content/mu-plugins": "./source/wp-content/mu-plugins",
"wp-content/mu-plugins/0-sandbox.php": "./env/0-sandbox.php",
"wp-content/tests/phpunit": "./source/wp-content/tests/phpunit",
"wp-cli.yml": "./wp-cli.yml"
},
"env": {
"tests": {
"mappings": {
"wp-content": "./source/wp-content",
"wp-content/mu-plugins/0-sandbox.php": "./env/0-sandbox.php",
"wp-content/tests/phpunit": "./source/wp-content/tests/phpunit",
"vendor": "./vendor"
}
}
}
}
8 changes: 4 additions & 4 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;
}
}
34 changes: 34 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,34 @@
<?php

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

require_once __DIR__ . '../../parsers/ListItem.php';

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 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
63 changes: 63 additions & 0 deletions source/wp-content/tests/phpunit/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php
/**
* PHPUnit bootstrap file
*/

// Require composer dependencies.
require_once '/var/www/html/vendor/autoload.php';

// If we're running in WP's build directory, ensure that WP knows that, too.
if ( 'build' === getenv( 'LOCAL_DIR' ) ) {
define( 'WP_RUN_CORE_TESTS', true );
}

// Determine the tests directory (from a WP dev checkout).
// Try the WP_TESTS_DIR environment variable first.
$_tests_dir = getenv( 'WP_TESTS_DIR' );

// Next, try the WP_PHPUNIT composer package.
if ( ! $_tests_dir ) {
$_tests_dir = getenv( 'WP_PHPUNIT__DIR' );
}

// See if we're installed inside an existing WP dev instance.
if ( ! $_tests_dir ) {
$_try_tests_dir = __DIR__ . '/../../../../../tests/phpunit';
if ( file_exists( $_try_tests_dir . '/includes/functions.php' ) ) {
$_tests_dir = $_try_tests_dir;
}
}
// Fallback.
if ( ! $_tests_dir ) {
$_tests_dir = '/tmp/wordpress-tests-lib';
}

// Give access to tests_add_filter() function.
require_once $_tests_dir . '/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 $_tests_dir . '/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
18 changes: 9 additions & 9 deletions source/wp-content/themes/wporg-main-2022/patterns/enterprise.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,15 @@

<!-- wp:list {"className":"is-style-default","fontSize":"small"} -->
<ul class="is-style-default has-small-font-size"><!-- wp:list-item -->
<li><a href="https://wordpress.org/files/2020/11/case-studies-grupo-abril.pdf" data-type="URL" data-id="https://wordpress.org/files/2020/11/case-studies-grupo-abril.pdf"><?php _e( 'Abril (PDF)', 'wporg' ); ?></a></li>
<li><?php _e( '<a href="https://wordpress.org/files/2020/11/case-studies-grupo-abril.pdf" data-type="URL" data-id="https://wordpress.org/files/2020/11/case-studies-grupo-abril.pdf">Abril (PDF)</a>', 'wporg' ); ?></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><a href="https://wordpress.org/files/2020/11/case-studies-kff.pdf" data-type="URL" data-id="https://wordpress.org/files/2020/11/case-studies-kff.pdf"><?php _e( 'KFF (PDF)', 'wporg' ); ?></a></li>
<li><?php _e( '<a href="https://wordpress.org/files/2020/11/case-studies-kff.pdf" data-type="URL" data-id="https://wordpress.org/files/2020/11/case-studies-kff.pdf">KFF (PDF)</a>', 'wporg' ); ?></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><a href="https://wordpress.org/files/2020/11/case-studies-newscorp-australia.pdf" data-type="URL" data-id="https://wordpress.org/files/2020/11/case-studies-newscorp-australia.pdf"><?php _e( 'NewsCorp Australia (PDF)', 'wporg' ); ?></a></li>
<li><?php _e( '<a href="https://wordpress.org/files/2020/11/case-studies-newscorp-australia.pdf" data-type="URL" data-id="https://wordpress.org/files/2020/11/case-studies-newscorp-australia.pdf">NewsCorp Australia (PDF)</a>', 'wporg' ); ?></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list --></div>
<!-- /wp:column -->
Expand All @@ -216,15 +216,15 @@

<!-- wp:list {"fontSize":"small"} -->
<ul class="has-small-font-size"><!-- wp:list-item -->
<li><a href="https://wordpress.org/files/2020/11/IDC-Report-Choosing-a-CMS-to-Meet-Todays-Digital-Experience-Challenges.pdf" data-type="URL" data-id="https://wordpress.org/files/2020/11/IDC-Report-Choosing-a-CMS-to-Meet-Todays-Digital-Experience-Challenges.pdf"><?php _e( 'IDC Report (PDF)', 'wporg' ); ?></a></li>
<li><?php _e( '<a href="https://wordpress.org/files/2020/11/IDC-Report-Choosing-a-CMS-to-Meet-Todays-Digital-Experience-Challenges.pdf" data-type="URL" data-id="https://wordpress.org/files/2020/11/IDC-Report-Choosing-a-CMS-to-Meet-Todays-Digital-Experience-Challenges.pdf">IDC Report (PDF)</a>', 'wporg' ); ?></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><a href="https://wordpress.org/files/2020/11/wordpress-as-a-content-hub-whitepaper.pdf" data-type="URL" data-id="https://wordpress.org/files/2020/11/wordpress-as-a-content-hub-whitepaper.pdf"><?php _e( 'WP as a Content Hub (PDF)', 'wporg' ); ?></a></li>
<li><?php _e( '<a href="https://wordpress.org/files/2020/11/wordpress-as-a-content-hub-whitepaper.pdf" data-type="URL" data-id="https://wordpress.org/files/2020/11/wordpress-as-a-content-hub-whitepaper.pdf">WP as a Content Hub (PDF)</a>', 'wporg' ); ?></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><a href="https://wordpress.org/files/2020/11/Personalization-whitepaper.pdf" data-type="URL" data-id="https://wordpress.org/files/2020/11/Personalization-whitepaper.pdf"><?php _e( 'Personalization (PDF)', 'wporg' ); ?></a></li>
<li><?php _e( '<a href="https://wordpress.org/files/2020/11/Personalization-whitepaper.pdf" data-type="URL" data-id="https://wordpress.org/files/2020/11/Personalization-whitepaper.pdf">Personalization (PDF)</a>', 'wporg' ); ?></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list --></div>
<!-- /wp:column -->
Expand All @@ -236,15 +236,15 @@

<!-- wp:list {"fontSize":"small"} -->
<ul class="has-small-font-size"><!-- wp:list-item {"className":"external-link"} -->
<li class="external-link"><a rel="noreferrer noopener" href="https://vimeo.com/443114625/c2f798174f" target="_blank"><?php _e( 'Drive Digital Commerce', 'wporg' ); ?></a></li>
<li class="external-link"><?php _e( '<a rel="noreferrer noopener" href="https://vimeo.com/443114625/c2f798174f" target="_blank">Drive Digital Commerce</a>', 'wporg' ); ?></li>
<!-- /wp:list-item -->

<!-- wp:list-item {"className":"external-link"} -->
<li class="external-link"><a href="https://vimeo.com/374961815/040fe06be7" target="_blank" rel="noreferrer noopener"><?php _e( 'Gutenberg for Enterprise', 'wporg' ); ?></a></li>
<li class="external-link"><?php _e( '<a href="https://vimeo.com/374961815/040fe06be7" target="_blank" rel="noreferrer noopener">Gutenberg for Enterprise</a>', 'wporg' ); ?></li>
<!-- /wp:list-item -->

<!-- wp:list-item {"className":"external-link"} -->
<li class="external-link"><a href="https://vimeo.com/425968597/80d24a1bf4" target="_blank" rel="noreferrer noopener"><?php _e( 'Lead a Successful Digital Transformation', 'wporg' ); ?></a></li>
<li class="external-link"><?php _e( '<a href="https://vimeo.com/425968597/80d24a1bf4" target="_blank" rel="noreferrer noopener">Lead a Successful Digital Transformation</a>', 'wporg' ); ?></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list --></div>
<!-- /wp:column --></div>
Expand Down
8 changes: 4 additions & 4 deletions source/wp-content/themes/wporg-main-2022/patterns/home.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,19 +219,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/"><?php _e( 'Learn with WordPress ↗', 'wporg' ); ?></a></li>
<li><?php _e( '<a href="https://learn.wordpress.org/">Learn with WordPress ↗</a>', 'wporg' ); ?></li>
<!-- /wp:list-item -->

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

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

<!-- wp:list-item -->
<li><a href="https://www.meetup.com/pro/wordpress/"><?php _e( 'Meet local WordPress users ↗', 'wporg' ); ?></a></li>
<li><?php _e( '<a href="https://www.meetup.com/pro/wordpress/">Meet local WordPress users ↗</a>', 'wporg' ); ?></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list --></div>
<!-- /wp:column --></div>
Expand Down