Skip to content

Commit

Permalink
Move html5lib tests to new class
Browse files Browse the repository at this point in the history
  • Loading branch information
sirreal committed Dec 18, 2023
1 parent 8f9386f commit 68734db
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 113 deletions.
113 changes: 0 additions & 113 deletions tests/phpunit/tests/html-api/wpHtmlProcessorBreadcrumbs.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* Unit tests covering WP_HTML_Processor functionality.
*
Expand Down Expand Up @@ -541,116 +540,4 @@ public function test_can_seek_back_and_forth() {
$p->seek( 'second' );
$this->assertTrue( $p->get_attribute( 'two' ) );
}

/**
* @dataProvider data_external_html5lib_tests
*/
public function test_external_html5lib( $html, $result ) {

$processed_tree = build_html5_treelike_string( $html );
$this->assertEquals( $processed_tree, $result );
}


/**
* Data provider.
*
* Tests from https://github.com/html5lib/html5lib-tests
*/
public function data_external_html5lib_tests() {
$test_dir = __DIR__ . '/html5lib-tests/tree-construction/';

$handle = opendir( $test_dir );
while ( false !== ( $entry = readdir( $handle ) ) ) {
if ( !stripos( $entry, '.dat' ) ) {
continue;
}

foreach (parse_html5_dat_testfile($test_dir . $entry) as $k => $test) {
yield "{$entry}/case {$k}" => $test;
}
}
closedir( $handle );
}
}

function build_html5_treelike_string( $html ) {
$p = WP_HTML_Processor::create_fragment( $html );

$output = "<html>\n <head>\n <body>\n";
while ( $p->next_tag() ) {
// breadcrumbs include our tag, so skip 1 nesting level
foreach ( $p->get_breadcrumbs() as $index => $_) {
if ( $index ) {
$output .= ' ';
}
}
$t = strtolower( $p->get_tag() );
$output .= "<{$t}>\n";
}

return $output;
}

function parse_html5_dat_testfile( $filename ) {
$handle = fopen( $filename, 'r', false );

/**
* @var ?string
*/
$state = null;

$test_html = '';
$test_dom = '';

while ( false !== ( $line = fgets( $handle ) ) ) {

if ( $line[0] === '#' ) {
// finish section
if ( $line == "#data\n" ) {
// If we're switching from a previous state, yield
if ( $state ) {
yield [ $test_html, $test_dom ];
}

// finish previous test
$test_html = "";
$test_dom = "";
}

$state = trim( substr( $line, 1 ) );

continue;
}

switch ( $state ) {
// Each test must begin with a string "#data" followed by a newline (LF). All
// subsequent lines until a line that says "#errors" are the test data and must be
// passed to the system being tested unchanged, except with the final newline (on the
// last line) removed.
case 'data':
$test_html .= $line;
break;

// Then there must be a line that says "#document", which must be followed by a dump of
// the tree of the parsed DOM. Each node must be represented by a single line. Each line
// must start with "| ", followed by two spaces per parent node that the node has before
// the root document node.
case 'document':
// Ignore everything that doesn't look like an element
if ( '|' === $line[0] ) {
$candidate = substr( $line, 2 );
$trimmed = trim( $candidate );
if ( '<' === $trimmed[0] && '<!DOCTYPE' !== substr( $trimmed, 0, 9 ) ) {
$test_dom .= $candidate;
}
}
break;
}
}

// EOF - return our last result
return [ $test_html, $test_dom ];

fclose( $handle );
}
129 changes: 129 additions & 0 deletions tests/phpunit/tests/html-api/wpHtmlProcessorHtml5lib.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
<?php

/**
* Unit tests covering WP_HTML_Processor functionality.
*
* @package WordPress
* @subpackage HTML-API
*
* @since TODO
*
* @group html-api
*
* @coversDefaultClass WP_HTML_Processor
*/
class Tests_HtmlApi_WpHtmlProcessorHtml5lib extends WP_UnitTestCase {
/**
* @dataProvider data_external_html5lib_tests
*/
public function test_external_html5lib( $html, $result ) {

$processed_tree = self::build_html5_treelike_string( $html );
$this->assertEquals( $processed_tree, $result );
}


/**
* Data provider.
*
* Tests from https://github.com/html5lib/html5lib-tests
*/
public function data_external_html5lib_tests() {
$test_dir = __DIR__ . '/html5lib-tests/tree-construction/';

$handle = opendir( $test_dir );
while ( false !== ( $entry = readdir( $handle ) ) ) {
if ( !stripos( $entry, '.dat' ) ) {

Check failure on line 36 in tests/phpunit/tests/html-api/wpHtmlProcessorHtml5lib.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 space after "!"; 0 found
continue;
}

foreach (self::parse_html5_dat_testfile($test_dir . $entry) as $k => $test) {

Check failure on line 40 in tests/phpunit/tests/html-api/wpHtmlProcessorHtml5lib.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

No space after opening parenthesis is prohibited

Check failure on line 40 in tests/phpunit/tests/html-api/wpHtmlProcessorHtml5lib.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces after opening parenthesis; 0 found

Check failure on line 40 in tests/phpunit/tests/html-api/wpHtmlProcessorHtml5lib.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Expected 1 spaces before closing parenthesis; 0 found

Check failure on line 40 in tests/phpunit/tests/html-api/wpHtmlProcessorHtml5lib.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

No space before closing parenthesis is prohibited
yield "{$entry}/case {$k}" => $test;
}
}
closedir( $handle );
}


static function build_html5_treelike_string( $html ) {

Check failure on line 48 in tests/phpunit/tests/html-api/wpHtmlProcessorHtml5lib.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Visibility must be declared on method "build_html5_treelike_string"
$p = WP_HTML_Processor::create_fragment( $html );

$output = "<html>\n <head>\n <body>\n";
while ( $p->next_tag() ) {
// breadcrumbs include our tag, so skip 1 nesting level
foreach ( $p->get_breadcrumbs() as $index => $_) {

Check failure on line 54 in tests/phpunit/tests/html-api/wpHtmlProcessorHtml5lib.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

No space before closing parenthesis is prohibited
if ( $index ) {
$output .= ' ';
}
}
$t = strtolower( $p->get_tag() );
$output .= "<{$t}>\n";
}

return $output;
}

static function parse_html5_dat_testfile( $filename ) {

Check failure on line 66 in tests/phpunit/tests/html-api/wpHtmlProcessorHtml5lib.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Visibility must be declared on method "parse_html5_dat_testfile"
$handle = fopen( $filename, 'r', false );

/**
* @var ?string
*/
$state = null;

$test_html = '';
$test_dom = '';

while ( false !== ( $line = fgets( $handle ) ) ) {

if ( $line[0] === '#' ) {

Check failure on line 79 in tests/phpunit/tests/html-api/wpHtmlProcessorHtml5lib.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Use Yoda Condition checks, you must.
// finish section
if ( $line == "#data\n" ) {

Check failure on line 81 in tests/phpunit/tests/html-api/wpHtmlProcessorHtml5lib.php

View workflow job for this annotation

GitHub Actions / PHP coding standards

Use Yoda Condition checks, you must.
// If we're switching from a previous state, yield
if ( $state ) {
yield [ $test_html, $test_dom ];
}

// finish previous test
$test_html = "";
$test_dom = "";
}

$state = trim( substr( $line, 1 ) );

continue;
}

switch ( $state ) {
// Each test must begin with a string "#data" followed by a newline (LF). All
// subsequent lines until a line that says "#errors" are the test data and must be
// passed to the system being tested unchanged, except with the final newline (on the
// last line) removed.
case 'data':
$test_html .= $line;
break;

// Then there must be a line that says "#document", which must be followed by a dump of
// the tree of the parsed DOM. Each node must be represented by a single line. Each line
// must start with "| ", followed by two spaces per parent node that the node has before
// the root document node.
case 'document':
// Ignore everything that doesn't look like an element
if ( '|' === $line[0] ) {
$candidate = substr( $line, 2 );
$trimmed = trim( $candidate );
if ( '<' === $trimmed[0] && '<!DOCTYPE' !== substr( $trimmed, 0, 9 ) ) {
$test_dom .= $candidate;
}
}
break;
}
}

// EOF - return our last result
return [ $test_html, $test_dom ];

fclose( $handle );
}
}

0 comments on commit 68734db

Please sign in to comment.