From 7b43b08dd5814e291e63d2a9fbfa8408a96e299f Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Thu, 28 Jul 2022 22:37:00 +0000 Subject: [PATCH] Tests: Simplify some assertions in `Tests_Media`. A number of assertions are checking for the number of times a hard-coded substring existed in a larger `$haystack`. These assertions were using `preg_match_all()` to do so, but without actually using a regex. In these cases, it is more performant (and simpler) to use the PHP native `substr_count()` function, which will yield the same result, without the overhead of regex parsing. Reference: [https://www.php.net/manual/en/function.substr-count.php PHP Manual: substr_count()] Follow-up to [711/tests], [38838], [42694], [53558]. Props jrf. See #55652. git-svn-id: https://develop.svn.wordpress.org/trunk@53790 602fd350-edb4-49c9-b593-d223f7449a82 --- tests/phpunit/tests/media.php | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/tests/phpunit/tests/media.php b/tests/phpunit/tests/media.php index 592b19e328..d5a6fc5e13 100644 --- a/tests/phpunit/tests/media.php +++ b/tests/phpunit/tests/media.php @@ -157,14 +157,14 @@ CAP; ) ); - $this->assertSame( 2, preg_match_all( '/wp-caption/', $result, $_r ) ); - $this->assertSame( 1, preg_match_all( '/alignnone/', $result, $_r ) ); - $this->assertSame( 1, preg_match_all( '/' . self::CAPTION . '/', $result, $_r ) ); + $this->assertSame( 2, substr_count( $result, 'wp-caption' ) ); + $this->assertSame( 1, substr_count( $result, 'alignnone' ) ); + $this->assertSame( 1, substr_count( $result, self::CAPTION ) ); if ( current_theme_supports( 'html5', 'caption' ) ) { - $this->assertSame( 1, preg_match_all( '/width: 20/', $result, $_r ) ); + $this->assertSame( 1, substr_count( $result, 'width: 20' ) ); } else { - $this->assertSame( 1, preg_match_all( '/width: 30/', $result, $_r ) ); + $this->assertSame( 1, substr_count( $result, 'width: 30' ) ); } } @@ -177,9 +177,9 @@ CAP; 'align' => '&myAlignment', ) ); - $this->assertSame( 1, preg_match_all( '/wp-caption &myAlignment/', $result, $_r ) ); - $this->assertSame( 1, preg_match_all( '/id="myId"/', $result, $_r ) ); - $this->assertSame( 1, preg_match_all( '/' . self::CAPTION . '/', $result, $_r ) ); + $this->assertSame( 1, substr_count( $result, 'wp-caption &myAlignment' ) ); + $this->assertSame( 1, substr_count( $result, 'id="myId"' ) ); + $this->assertSame( 1, substr_count( $result, self::CAPTION ) ); } public function test_img_caption_shortcode_with_old_format_and_class() { @@ -190,20 +190,19 @@ CAP; 'caption' => self::CAPTION, ) ); - $this->assertSame( 1, preg_match_all( '/wp-caption alignnone some-class another-class/', $result, $_r ) ); + $this->assertSame( 1, substr_count( $result, 'wp-caption alignnone some-class another-class' ) ); } public function test_new_img_caption_shortcode_with_html_caption() { - $result = img_caption_shortcode( + $result = img_caption_shortcode( array( 'width' => 20, 'caption' => self::HTML_CONTENT, ) ); - $our_preg = preg_quote( self::HTML_CONTENT ); - $this->assertSame( 1, preg_match_all( "~{$our_preg}~", $result, $_r ) ); + $this->assertSame( 1, substr_count( $result, self::HTML_CONTENT ) ); } public function test_new_img_caption_shortcode_new_format() { @@ -256,7 +255,7 @@ CAP; self::IMG_CONTENT . self::HTML_CONTENT ); - $this->assertSame( 1, preg_match_all( '/aria-describedby="caption-myId"/', $result, $_r ) ); + $this->assertSame( 1, substr_count( $result, 'aria-describedby="caption-myId"' ) ); } public function test_add_remove_oembed_provider() {