-
Notifications
You must be signed in to change notification settings - Fork 29
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
Closed
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
f19c850
Parse core/list-items like paragraphs when building patterns
adamwoodnz 50c2687
Re export list items correctly
adamwoodnz d540451
composer update wporg/wporg-mu-plugins
adamwoodnz 22e6f72
Add ListItem parser rather than branching in RichText
adamwoodnz b0cbc70
Add tests for the export content List Item parser
adamwoodnz 04eb22f
Add tests mappings to wp-env
adamwoodnz 836a762
Add `yoast/phpunit-polyfills` required for running tests
ryelle 1560e4e
Update mapping to load test files in docker
ryelle 6d048bc
Fix paths to loader, tests
ryelle 485df02
Load parser files in test
ryelle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
34
env/export-content/tests/phpunit/parser-list-item-test.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>' ), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' ); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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-ignorephpunit
to make docker load tests...