Skip to content

Commit 8d52517

Browse files
committed
Build/Test Tools: Use semantic HTML comparison in script tests.
The `assertEqualHTML()` method makes tests resilient to irrelevant syntactic changes in HTML output, focusing on semantic equivalence rather than exact string matching. Developed in WordPress#10727. Follow up to [61394], [61391]. Props jonsurrell, cbravobernal. See #64225. git-svn-id: https://develop.svn.wordpress.org/trunk@61478 602fd350-edb4-49c9-b593-d223f7449a82
1 parent c55a0f2 commit 8d52517

1 file changed

Lines changed: 9 additions & 8 deletions

File tree

tests/phpunit/tests/dependencies/scripts.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,8 @@ public function test_delayed_dependent_with_blocking_dependency_not_enqueued( $s
333333
// This dependent is registered but not enqueued, so it should not factor into the eligible loading strategy.
334334
wp_register_script( 'dependent-script-a4', '/dependent-script-a4.js', array( 'main-script-a4' ), null );
335335
$output = get_echo( 'wp_print_scripts' );
336-
$expected = str_replace( "'", '"', "<script src='/main-script-a4.js' id='main-script-a4-js' {$strategy} data-wp-strategy='{$strategy}'></script>" );
337-
$this->assertStringContainsString( $expected, $output, 'Only enqueued dependents should affect the eligible strategy.' );
336+
$expected = "<script src='/main-script-a4.js' id='main-script-a4-js' {$strategy} data-wp-strategy='{$strategy}'></script>";
337+
$this->assertEqualHTMLScriptTagById( $expected, $output, 'Only enqueued dependents should affect the eligible strategy.' );
338338
}
339339

340340
/**
@@ -1076,8 +1076,8 @@ public function test_various_strategy_dependency_chains( $set_up, $expected_mark
10761076
public function test_loading_strategy_with_defer_having_no_dependents_nor_dependencies() {
10771077
wp_enqueue_script( 'main-script-d1', 'http://example.com/main-script-d1.js', array(), null, array( 'strategy' => 'defer' ) );
10781078
$output = get_echo( 'wp_print_scripts' );
1079-
$expected = str_replace( "'", '"', "<script src='http://example.com/main-script-d1.js' id='main-script-d1-js' defer data-wp-strategy='defer'></script>\n" );
1080-
$this->assertStringContainsString( $expected, $output, 'Expected defer, as there is no dependent or dependency' );
1079+
$expected = "<script src='http://example.com/main-script-d1.js' id='main-script-d1-js' defer data-wp-strategy='defer'></script>\n";
1080+
$this->assertEqualHTMLScriptTagById( $expected, $output, 'Expected defer, as there is no dependent or dependency' );
10811081
}
10821082

10831083
/**
@@ -1096,7 +1096,7 @@ public function test_loading_strategy_with_defer_dependent_and_varied_dependenci
10961096
wp_enqueue_script( 'main-script-d2', 'http://example.com/main-script-d2.js', array( 'dependency-script-d2-1', 'dependency-script-d2-3' ), null, array( 'strategy' => 'defer' ) );
10971097
$output = get_echo( 'wp_print_scripts' );
10981098
$expected = '<script src="http://example.com/main-script-d2.js" id="main-script-d2-js" defer data-wp-strategy="defer"></script>';
1099-
$this->assertStringContainsString( $expected, $output, 'Expected defer, as all dependencies are either deferred or blocking' );
1099+
$this->assertEqualHTMLScriptTagById( $expected, $output, 'Expected defer, as all dependencies are either deferred or blocking' );
11001100
}
11011101

11021102
/**
@@ -1115,7 +1115,7 @@ public function test_loading_strategy_with_all_defer_dependencies() {
11151115
wp_enqueue_script( 'dependent-script-d3-3', 'http://example.com/dependent-script-d3-3.js', array( 'dependent-script-d3-2' ), null, array( 'strategy' => 'defer' ) );
11161116
$output = get_echo( 'wp_print_scripts' );
11171117
$expected = '<script src="http://example.com/main-script-d3.js" id="main-script-d3-js" defer data-wp-strategy="defer"></script>';
1118-
$this->assertStringContainsString( $expected, $output, 'Expected defer, as all dependents have defer loading strategy' );
1118+
$this->assertEqualHTMLScriptTagById( $expected, $output, 'Expected defer, as all dependents have defer loading strategy' );
11191119
}
11201120

11211121
/**
@@ -1495,9 +1495,10 @@ public function test_loading_strategy_with_invalid_defer_registration() {
14951495
wp_enqueue_script( 'dependent-script-d4-1', '/dependent-script-d4-1.js', array( 'main-script-d4' ), null, array( 'strategy' => 'defer' ) );
14961496
wp_enqueue_script( 'dependent-script-d4-2', '/dependent-script-d4-2.js', array( 'dependent-script-d4-1' ), null );
14971497
wp_enqueue_script( 'dependent-script-d4-3', '/dependent-script-d4-3.js', array( 'dependent-script-d4-2' ), null, array( 'strategy' => 'defer' ) );
1498+
14981499
$output = get_echo( 'wp_print_scripts' );
1499-
$expected = str_replace( "'", '"', "<script src='/main-script-d4.js' id='main-script-d4-js' data-wp-strategy='defer'></script>\n" );
1500-
$this->assertStringContainsString( $expected, $output, 'Scripts registered as defer but that have all dependents with no strategy, should become blocking (no strategy).' );
1500+
$expected = "<script src='/main-script-d4.js' id='main-script-d4-js' data-wp-strategy='defer'></script>\n";
1501+
$this->assertEqualHTMLScriptTagById( $expected, $output, 'Scripts registered as defer but that have all dependents with no strategy, should become blocking (no strategy).' );
15011502
}
15021503

15031504
/**

0 commit comments

Comments
 (0)