From b0419afba6c37d903eaf3037c2aa74161191d7d3 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 29 Oct 2019 14:26:41 +0000 Subject: [PATCH] Build/Test Tools: Ignore EOL differences in tests using multiline string assertions. Unix vs. Windows EOL style mismatches can cause misleading failures in tests using the heredoc syntax (`<<<`) or multiline strings as the expected result. Fixes #31432. See #42594, #47411. git-svn-id: https://develop.svn.wordpress.org/trunk@46612 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/includes/abstract-testcase.php | 12 +++++++ tests/phpunit/tests/blocks/render.php | 2 +- tests/phpunit/tests/dependencies/scripts.php | 14 ++++----- tests/phpunit/tests/dependencies/styles.php | 2 +- tests/phpunit/tests/formatting/Autop.php | 12 +++---- tests/phpunit/tests/formatting/EscUrl.php | 2 ++ .../tests/formatting/SanitizeTextField.php | 2 +- tests/phpunit/tests/functions/getArchives.php | 6 ++-- tests/phpunit/tests/general/paginateLinks.php | 8 ++--- tests/phpunit/tests/media.php | 2 +- tests/phpunit/tests/pomo/po.php | 19 ++++++------ tests/phpunit/tests/post/listPages.php | 31 ++++++++++--------- tests/phpunit/tests/post/template.php | 11 ++++--- 13 files changed, 70 insertions(+), 53 deletions(-) diff --git a/tests/phpunit/includes/abstract-testcase.php b/tests/phpunit/includes/abstract-testcase.php index 9c586ee9be..749803dffc 100644 --- a/tests/phpunit/includes/abstract-testcase.php +++ b/tests/phpunit/includes/abstract-testcase.php @@ -635,6 +635,18 @@ abstract class WP_UnitTestCase_Base extends PHPUnit_Framework_TestCase { $this->assertEquals( preg_replace( '/\s*/', '', $expected ), preg_replace( '/\s*/', '', $actual ) ); } + /** + * Asserts that two values are equal, with EOL differences discarded. + * + * @since 5.4.0 + * + * @param string $expected The expected value. + * @param string $actual The actual value. + */ + public function assertEqualsIgnoreEOL( $expected, $actual ) { + $this->assertEquals( str_replace( "\r\n", "\n", $expected ), str_replace( "\r\n", "\n", $actual ) ); + } + /** * Asserts that the contents of two un-keyed, single arrays are equal, without accounting for the order of elements. * diff --git a/tests/phpunit/tests/blocks/render.php b/tests/phpunit/tests/blocks/render.php index f6b6513298..ab16b498d8 100644 --- a/tests/phpunit/tests/blocks/render.php +++ b/tests/phpunit/tests/blocks/render.php @@ -60,7 +60,7 @@ class WP_Test_Block_Render extends WP_UnitTestCase { $actual_html = do_blocks( $original_html ); - $this->assertEquals( $expected_html, $actual_html ); + $this->assertEqualsIgnoreEOL( $expected_html, $actual_html ); } /** diff --git a/tests/phpunit/tests/dependencies/scripts.php b/tests/phpunit/tests/dependencies/scripts.php index 4cb86ca85f..2b3b4538d8 100644 --- a/tests/phpunit/tests/dependencies/scripts.php +++ b/tests/phpunit/tests/dependencies/scripts.php @@ -846,7 +846,7 @@ JS; ); $expected .= "\n"; - $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); + $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) ); } /** @@ -871,7 +871,7 @@ JS; ); $expected .= "\n"; - $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); + $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) ); } /** @@ -896,7 +896,7 @@ JS; ); $expected .= "\n"; - $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); + $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) ); } /** @@ -921,7 +921,7 @@ JS; ); $expected .= "\n"; - $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); + $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) ); } /** @@ -961,7 +961,7 @@ JS; ); $expected .= "\n"; - $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); + $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) ); } /** @@ -988,7 +988,7 @@ JS; ); $expected .= "\n"; - $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); + $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) ); } /** @@ -1016,7 +1016,7 @@ JS; $expected .= "\n"; $expected .= "\n"; - $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) ); + $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_scripts' ) ); } /** diff --git a/tests/phpunit/tests/dependencies/styles.php b/tests/phpunit/tests/dependencies/styles.php index 3908ed7af8..174a8e48b6 100644 --- a/tests/phpunit/tests/dependencies/styles.php +++ b/tests/phpunit/tests/dependencies/styles.php @@ -272,7 +272,7 @@ CSS; wp_style_add_data( 'handle', 'conditional', 'IE' ); wp_add_inline_style( 'handle', 'a { color: blue; }' ); - $this->assertEquals( $expected, get_echo( 'wp_print_styles' ) ); + $this->assertEqualsIgnoreEOL( $expected, get_echo( 'wp_print_styles' ) ); } /** diff --git a/tests/phpunit/tests/formatting/Autop.php b/tests/phpunit/tests/formatting/Autop.php index 071f202f38..087cbf25c6 100644 --- a/tests/phpunit/tests/formatting/Autop.php +++ b/tests/phpunit/tests/formatting/Autop.php @@ -502,7 +502,7 @@ line 3
line 4
line 5

'; - $this->assertEquals( $expected, trim( wpautop( $content ) ) ); + $this->assertEqualsIgnoreEOL( $expected, trim( wpautop( $content ) ) ); } /** @@ -521,7 +521,7 @@ line 2
$expected = '

line 1

line 2

'; - $this->assertEquals( $expected, trim( wpautop( $content ) ) ); + $this->assertEqualsIgnoreEOL( $expected, trim( wpautop( $content ) ) ); } @@ -543,7 +543,7 @@ line 2
* * @ticket 39307 */ - function test_that_wpautop_doses_not_add_extra_closing_p_in_figure() { + function test_that_wpautop_does_not_add_extra_closing_p_in_figure() { $content1 = '
Caption
'; $expected1 = $content1; @@ -556,7 +556,7 @@ line 2
Caption
'; $this->assertEquals( $expected1, trim( wpautop( $content1 ) ) ); - $this->assertEquals( $expected2, trim( wpautop( $content2 ) ) ); + $this->assertEqualsIgnoreEOL( $expected2, trim( wpautop( $content2 ) ) ); } /** @@ -584,7 +584,7 @@ line 2
$expected = '

' . $content . '

'; - $this->assertEquals( $expected, trim( wpautop( $content ) ) ); + $this->assertEqualsIgnoreEOL( $expected, trim( wpautop( $content ) ) ); } /** @@ -600,6 +600,6 @@ line 2
$expected = '

' . $content . '

'; - $this->assertEquals( $expected, trim( wpautop( $content ) ) ); + $this->assertEqualsIgnoreEOL( $expected, trim( wpautop( $content ) ) ); } } diff --git a/tests/phpunit/tests/formatting/EscUrl.php b/tests/phpunit/tests/formatting/EscUrl.php index 9532976ce2..d183f6f9cb 100644 --- a/tests/phpunit/tests/formatting/EscUrl.php +++ b/tests/phpunit/tests/formatting/EscUrl.php @@ -209,6 +209,7 @@ Hi there, I thought you might want to sign up for this newsletter EOT; + $body = str_replace( "\r\n", "\n", $body ); $email_link = 'mailto:?body=' . rawurlencode( $body ); $email_link = esc_url( $email_link ); $this->assertEquals( 'mailto:?body=Hi%20there%2C%0A%0AI%20thought%20you%20might%20want%20to%20sign%20up%20for%20this%20newsletter', $email_link ); @@ -223,6 +224,7 @@ Hi there, I thought you might want to sign up for this newsletter EOT; + $body = str_replace( "\r\n", "\n", $body ); $email_link = 'http://example.com/mailto:?body=' . rawurlencode( $body ); $email_link = esc_url( $email_link ); $this->assertEquals( 'http://example.com/mailto:?body=Hi%20there%2CI%20thought%20you%20might%20want%20to%20sign%20up%20for%20this%20newsletter', $email_link ); diff --git a/tests/phpunit/tests/formatting/SanitizeTextField.php b/tests/phpunit/tests/formatting/SanitizeTextField.php index cba4bee9ec..5e87ff1c89 100644 --- a/tests/phpunit/tests/formatting/SanitizeTextField.php +++ b/tests/phpunit/tests/formatting/SanitizeTextField.php @@ -137,7 +137,7 @@ class Tests_Formatting_SanitizeTextField extends WP_UnitTestCase { $expected_multiline = $expected; } $this->assertEquals( $expected_oneline, sanitize_text_field( $string ) ); - $this->assertEquals( $expected_multiline, sanitize_textarea_field( $string ) ); + $this->assertEqualsIgnoreEOL( $expected_multiline, sanitize_textarea_field( $string ) ); } } diff --git a/tests/phpunit/tests/functions/getArchives.php b/tests/phpunit/tests/functions/getArchives.php index f3cc4536df..c771eece09 100644 --- a/tests/phpunit/tests/functions/getArchives.php +++ b/tests/phpunit/tests/functions/getArchives.php @@ -71,7 +71,7 @@ class Tests_Get_Archives extends WP_UnitTestCase {
  • $title4
  • $title5
  • EOF; - $this->assertEquals( + $this->assertEqualsIgnoreEOL( $expected['limit'], trim( wp_get_archives( @@ -153,7 +153,7 @@ EOF;
  • October 2012
  • $date_full
  • EOF; - $this->assertEquals( + $this->assertEqualsIgnoreEOL( $expected['order_asc'], trim( wp_get_archives( @@ -169,7 +169,7 @@ EOF;
  • $date_full
  • October 2012
  • EOF; - $this->assertEquals( + $this->assertEqualsIgnoreEOL( $expected['order_desc'], trim( wp_get_archives( diff --git a/tests/phpunit/tests/general/paginateLinks.php b/tests/phpunit/tests/general/paginateLinks.php index 466f5bd7e2..dc7195c5f9 100644 --- a/tests/phpunit/tests/general/paginateLinks.php +++ b/tests/phpunit/tests/general/paginateLinks.php @@ -25,7 +25,7 @@ class Tests_Paginate_Links extends WP_UnitTestCase { EXPECTED; $links = paginate_links( array( 'total' => 50 ) ); - $this->assertEquals( $expected, $links ); + $this->assertEqualsIgnoreEOL( $expected, $links ); } function test_format() { @@ -48,7 +48,7 @@ EXPECTED; 'format' => 'page/%#%/', ) ); - $this->assertEquals( $expected, $links ); + $this->assertEqualsIgnoreEOL( $expected, $links ); } function test_prev_next_false() { @@ -73,7 +73,7 @@ EXPECTED; 'current' => 2, ) ); - $this->assertEquals( $expected, $links ); + $this->assertEqualsIgnoreEOL( $expected, $links ); } function test_prev_next_true() { @@ -100,7 +100,7 @@ EXPECTED; 'current' => 2, ) ); - $this->assertEquals( $expected, $links ); + $this->assertEqualsIgnoreEOL( $expected, $links ); } function increment_i18n_count() { diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index b9a73a336d..26d8468bc2 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -1249,7 +1249,7 @@ Stop.

    EOF; $result = apply_filters( 'the_content', $content ); - $this->assertEquals( $expected, $result ); + $this->assertEqualsIgnoreEOL( $expected, $result ); } /** diff --git a/tests/phpunit/tests/pomo/po.php b/tests/phpunit/tests/pomo/po.php index 4f5bef31fa..0bd6f461dc 100644 --- a/tests/phpunit/tests/pomo/po.php +++ b/tests/phpunit/tests/pomo/po.php @@ -22,6 +22,7 @@ We hope you enjoy your new blog. Thanks! --The WordPress Team http://wordpress.org/ '; + $this->mail = str_replace( "\r\n", "\n", $this->mail ); $this->po_mail = '"" "Your new WordPress blog has been successfully set up at:\n" "\n" @@ -63,7 +64,7 @@ http://wordpress.org/ $src = 'Categories can be selectively converted to tags using the category to tag converter.'; $this->assertEquals( '"Categories can be selectively converted to tags using the category to tag converter."', $po->poify( $src ) ); - $this->assertEquals( $this->po_mail, $po->poify( $this->mail ) ); + $this->assertEqualsIgnoreEOL( $this->po_mail, $po->poify( $this->mail ) ); } function test_unpoify() { @@ -74,7 +75,7 @@ http://wordpress.org/ $this->assertEquals( '\\t\\n', $po->unpoify( '"\\\\t\\\\n"' ) ); // wordwrapped $this->assertEquals( 'babadyado', $po->unpoify( "\"\"\n\"baba\"\n\"dyado\"" ) ); - $this->assertEquals( $this->mail, $po->unpoify( $this->po_mail ) ); + $this->assertEqualsIgnoreEOL( $this->mail, $po->unpoify( $this->po_mail ) ); } function test_export_entry() { @@ -88,7 +89,7 @@ http://wordpress.org/ 'plural' => 'babas', ) ); - $this->assertEquals( + $this->assertEqualsIgnoreEOL( 'msgid "baba" msgid_plural "babas" msgstr[0] "" @@ -101,7 +102,7 @@ msgstr[1] ""', 'translator_comments' => "baba\ndyado", ) ); - $this->assertEquals( + $this->assertEqualsIgnoreEOL( '# baba # dyado msgid "baba" @@ -114,7 +115,7 @@ msgstr ""', 'extracted_comments' => 'baba', ) ); - $this->assertEquals( + $this->assertEqualsIgnoreEOL( '#. baba msgid "baba" msgstr ""', @@ -127,7 +128,7 @@ msgstr ""', 'references' => range( 1, 29 ), ) ); - $this->assertEquals( + $this->assertEqualsIgnoreEOL( '#. baba #: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 #: 29 @@ -158,7 +159,7 @@ msgstr ""', 'translations' => array( 'кукубуку' ), ) ); - $this->assertEquals( + $this->assertEqualsIgnoreEOL( 'msgid "baba" msgid_plural "babas" msgstr[0] "кукубуку"', @@ -172,7 +173,7 @@ msgstr[0] "кукубуку"', 'translations' => array( 'кукубуку', 'кукуруку', 'бабаяга' ), ) ); - $this->assertEquals( + $this->assertEqualsIgnoreEOL( 'msgid "baba" msgid_plural "babas" msgstr[0] "кукубуку" @@ -190,7 +191,7 @@ msgstr[2] "бабаяга"', 'flags' => array( 'fuzzy', 'php-format' ), ) ); - $this->assertEquals( + $this->assertEqualsIgnoreEOL( '#, fuzzy, php-format msgctxt "ctxt" msgid "baba" diff --git a/tests/phpunit/tests/post/listPages.php b/tests/phpunit/tests/post/listPages.php index 8d8b85bd52..01efc5157d 100644 --- a/tests/phpunit/tests/post/listPages.php +++ b/tests/phpunit/tests/post/listPages.php @@ -134,7 +134,7 @@ class Tests_List_Pages extends WP_UnitTestCase { '; - $this->AssertEquals( $expected, wp_list_pages( $args ) ); + $this->assertEqualsIgnoreEOL( $expected, wp_list_pages( $args ) ); } function test_wp_list_pages_depth() { @@ -148,7 +148,7 @@ class Tests_List_Pages extends WP_UnitTestCase {
  • Parent 3
  • '; - $this->AssertEquals( $expected, wp_list_pages( $args ) ); + $this->assertEqualsIgnoreEOL( $expected, wp_list_pages( $args ) ); } function test_wp_list_pages_show_date() { @@ -164,7 +164,7 @@ class Tests_List_Pages extends WP_UnitTestCase {
  • Parent 3 ' . $date . '
  • '; - $this->AssertEquals( $expected, wp_list_pages( $args ) ); + $this->assertEqualsIgnoreEOL( $expected, wp_list_pages( $args ) ); } function test_wp_list_pages_date_format() { @@ -198,7 +198,7 @@ class Tests_List_Pages extends WP_UnitTestCase { '; - $this->AssertEquals( $expected, wp_list_pages( $args ) ); + $this->assertEqualsIgnoreEOL( $expected, wp_list_pages( $args ) ); } function test_wp_list_pages_child_of() { @@ -212,7 +212,7 @@ class Tests_List_Pages extends WP_UnitTestCase {
  • Child 3
  • '; - $this->AssertEquals( $expected, wp_list_pages( $args ) ); + $this->assertEqualsIgnoreEOL( $expected, wp_list_pages( $args ) ); } function test_wp_list_pages_exclude() { @@ -240,7 +240,7 @@ class Tests_List_Pages extends WP_UnitTestCase {
  • Child 3
  • '; - $this->AssertEquals( $expected, wp_list_pages( $args ) ); + $this->assertEqualsIgnoreEOL( $expected, wp_list_pages( $args ) ); } function test_wp_list_pages_title_li() { @@ -255,7 +255,7 @@ class Tests_List_Pages extends WP_UnitTestCase {
  • Parent 3
  • '; - $this->AssertEquals( $expected, wp_list_pages( $args ) ); + $this->assertEqualsIgnoreEOL( $expected, wp_list_pages( $args ) ); } function test_wp_list_pages_echo() { @@ -268,6 +268,7 @@ class Tests_List_Pages extends WP_UnitTestCase {
  • Parent 2
  • Parent 3
  • '; + $expected = str_replace( "\r\n", "\n", $expected ); $this->expectOutputString( $expected ); wp_list_pages( $args ); } @@ -281,7 +282,7 @@ class Tests_List_Pages extends WP_UnitTestCase { $expected = ''; - $this->AssertEquals( $expected, wp_list_pages( $args ) ); + $this->assertEqualsIgnoreEOL( $expected, wp_list_pages( $args ) ); } function test_wp_list_pages_number() { @@ -293,7 +294,7 @@ class Tests_List_Pages extends WP_UnitTestCase { $expected = ''; - $this->AssertEquals( $expected, wp_list_pages( $args ) ); + $this->assertEqualsIgnoreEOL( $expected, wp_list_pages( $args ) ); } function test_wp_list_pages_sort_column() { @@ -326,7 +327,7 @@ class Tests_List_Pages extends WP_UnitTestCase { '; - $this->AssertEquals( $expected, wp_list_pages( $args ) ); + $this->assertEqualsIgnoreEOL( $expected, wp_list_pages( $args ) ); } function test_wp_list_pages_link_before() { @@ -358,7 +359,7 @@ class Tests_List_Pages extends WP_UnitTestCase { '; - $this->AssertEquals( $expected, wp_list_pages( $args ) ); + $this->assertEqualsIgnoreEOL( $expected, wp_list_pages( $args ) ); } function test_wp_list_pages_link_after() { @@ -390,7 +391,7 @@ class Tests_List_Pages extends WP_UnitTestCase { '; - $this->AssertEquals( $expected, wp_list_pages( $args ) ); + $this->assertEqualsIgnoreEOL( $expected, wp_list_pages( $args ) ); } @@ -404,7 +405,7 @@ class Tests_List_Pages extends WP_UnitTestCase {
  • Parent 3
  • '; - $this->AssertEquals( $expected, wp_list_pages( $args ) ); + $this->assertEqualsIgnoreEOL( $expected, wp_list_pages( $args ) ); } function test_wp_list_pages_exclude_tree() { @@ -422,7 +423,7 @@ class Tests_List_Pages extends WP_UnitTestCase { '; - $this->AssertEquals( $expected, wp_list_pages( $args ) ); + $this->assertEqualsIgnoreEOL( $expected, wp_list_pages( $args ) ); } function test_wp_list_pages_discarded_whitespace() { @@ -433,6 +434,6 @@ class Tests_List_Pages extends WP_UnitTestCase { $expected = ''; - $this->AssertEquals( $expected, wp_list_pages( $args ) ); + $this->assertEquals( $expected, wp_list_pages( $args ) ); } } diff --git a/tests/phpunit/tests/post/template.php b/tests/phpunit/tests/post/template.php index 9be2fb63d2..56d26cf77b 100644 --- a/tests/phpunit/tests/post/template.php +++ b/tests/phpunit/tests/post/template.php @@ -164,7 +164,7 @@ class Tests_Post_Template extends WP_UnitTestCase { LINEAGE; $output = wp_dropdown_pages( array( 'echo' => 0 ) ); - $this->assertEquals( $lineage, $output ); + $this->assertEqualsIgnoreEOL( $lineage, $output ); $depth = << @@ -179,7 +179,7 @@ DEPTH; 'depth' => 1, ) ); - $this->assertEquals( $depth, $output ); + $this->assertEqualsIgnoreEOL( $depth, $output ); $option_none = << @@ -197,7 +197,7 @@ NONE; 'option_none_value' => 'Woo', ) ); - $this->assertEquals( $option_none, $output ); + $this->assertEqualsIgnoreEOL( $option_none, $output ); $option_no_change = << @@ -207,7 +207,8 @@ NONE; NO; - $output = wp_dropdown_pages( + + $output = wp_dropdown_pages( array( 'echo' => 0, 'depth' => 1, @@ -216,7 +217,7 @@ NO; 'show_option_no_change' => 'Burrito', ) ); - $this->assertEquals( $option_no_change, $output ); + $this->assertEqualsIgnoreEOL( $option_no_change, $output ); } /**