Tests: Use more appropriate assertions in various tests.

This replaces instances of `assertTrue( strpos( ... ) > 0 )` with `assertStringContainsString()` to use native PHPUnit functionality.

Going forward, these methods introduced in PHPUnit 7.5 should be used for similar assertions:

* `assertStringContainsString()`
* `assertStringNotContainsString()`

As WordPress currently uses PHPUnit 5.7.x to run tests on PHP 5.6, polyfills for these methods are now added to the `WP_UnitTestCase` class for PHPUnit < 7.5.

Follow-up to [51335], [51337], [51367], [51397], [51403], [51404], [51436], [51438], [51448], [51449].

See #53363.

git-svn-id: https://develop.svn.wordpress.org/trunk@51451 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov
2021-07-17 10:36:52 +00:00
parent 570d86da90
commit 54c54f9a1e
6 changed files with 66 additions and 34 deletions
+32
View File
@@ -372,4 +372,36 @@ class WP_UnitTestCase extends WP_UnitTestCase_Base {
public static function assertIsNotIterable( $actual, $message = '' ) {
static::assertFalse( is_iterable( $actual ), $message );
}
/**
* Asserts that a string haystack contains a needle.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param string $needle The string to search for.
* @param string $haystack The string to treat as the haystack.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertStringContainsString( $needle, $haystack, $message = '' ) {
static::assertContains( $needle, $haystack, $message );
}
/**
* Asserts that a string haystack does not contain a needle.
*
* This method has been backported from a more recent PHPUnit version,
* as tests running on PHP 5.6 use PHPUnit 5.7.x.
*
* @since 5.9.0
*
* @param string $needle The string to search for.
* @param string $haystack The string to treat as the haystack.
* @param string $message Optional. Message to display when the assertion fails.
*/
public static function assertStringNotContainsString( $needle, $haystack, $message = '' ) {
static::assertNotContains( $needle, $haystack, $message );
}
}
+3 -3
View File
@@ -79,7 +79,7 @@ class Tests_Ajax_Autosave extends WP_Ajax_UnitTestCase {
// Check that the edit happened.
$post = get_post( self::$post_id );
$this->assertNotFalse( strpos( $post->post_content, $md5 ) );
$this->assertStringContainsString( $md5, $post->post_content );
}
/**
@@ -125,12 +125,12 @@ class Tests_Ajax_Autosave extends WP_Ajax_UnitTestCase {
// Check that the original post was NOT edited.
$post = get_post( self::$post_id );
$this->assertFalse( strpos( $post->post_content, $md5 ) );
$this->assertStringNotContainsString( $md5, $post->post_content );
// Check if the autosave post was created.
$autosave = wp_get_post_autosave( self::$post_id, get_current_user_id() );
$this->assertNotEmpty( $autosave );
$this->assertNotFalse( strpos( $autosave->post_content, $md5 ) );
$this->assertStringContainsString( $md5, $autosave->post_content );
}
/**
@@ -106,7 +106,7 @@ class Tests_Image_Intermediate_Size extends WP_UnitTestCase {
// Test for the expected string because the array will by definition
// return with the correct height and width attributes.
$this->assertTrue( strpos( $image['file'], '330x220' ) > 0 );
$this->assertStringContainsString( '330x220', $image['file'] );
}
/**
@@ -128,7 +128,7 @@ class Tests_Image_Intermediate_Size extends WP_UnitTestCase {
// Test for the expected string because the array will by definition
// return with the correct height and width attributes.
$this->assertTrue( strpos( $image['file'], '330x220' ) > 0 );
$this->assertStringContainsString( '330x220', $image['file'] );
}
/**
@@ -151,7 +151,7 @@ class Tests_Image_Intermediate_Size extends WP_UnitTestCase {
// Test for the expected string because the array will by definition
// return with the correct height and width attributes.
$this->assertTrue( strpos( $image['file'], '450x300' ) > 0 );
$this->assertStringContainsString( '450x300', $image['file'] );
}
/**
@@ -201,7 +201,7 @@ class Tests_Image_Intermediate_Size extends WP_UnitTestCase {
// Test for the expected string because the array will by definition
// return with the correct height and width attributes.
$this->assertTrue( strpos( $image['file'], $image_w . 'x' . $image_h ) > 0 );
$this->assertStringContainsString( $image_w . 'x' . $image_h, $image['file'] );
}
/**
@@ -230,7 +230,7 @@ class Tests_Image_Intermediate_Size extends WP_UnitTestCase {
// Test for the expected string because the array will by definition
// return with the correct height and width attributes.
$this->assertTrue( strpos( $image['file'], $image_w . 'x' . $image_h ) > 0 );
$this->assertStringContainsString( $image_w . 'x' . $image_h, $image['file'] );
}
/**
@@ -255,7 +255,7 @@ class Tests_Image_Intermediate_Size extends WP_UnitTestCase {
// Note: Staying larger than 300px to miss default medium crop.
$image = image_get_intermediate_size( $id, array( 0, $height ) );
$this->assertTrue( strpos( $image['file'], $width . 'x' . $height ) > 0 );
$this->assertStringContainsString( $width . 'x' . $height, $image['file'] );
}
/**
@@ -273,7 +273,7 @@ class Tests_Image_Intermediate_Size extends WP_UnitTestCase {
$image = image_get_intermediate_size( $id, array( 50, 25 ) );
// We should get the 'test-size' file and not the thumbnail.
$this->assertTrue( strpos( $image['file'], '200x100' ) > 0 );
$this->assertStringContainsString( '200x100', $image['file'] );
}
/**
+8 -8
View File
@@ -81,8 +81,8 @@ class Tests_Mail extends WP_UnitTestCase {
// We need some better assertions here but these catch the failure for now.
$this->assertSameIgnoreEOL( $body, $mailer->get_sent()->body );
$this->assertTrue( strpos( iconv_mime_decode_headers( ( $mailer->get_sent()->header ) )['Content-Type'][0], 'boundary="----=_Part_4892_25692638.1192452070893"' ) > 0 );
$this->assertTrue( strpos( $mailer->get_sent()->header, 'charset=' ) > 0 );
$this->assertStringContainsString( 'boundary="----=_Part_4892_25692638.1192452070893"', iconv_mime_decode_headers( ( $mailer->get_sent()->header ) )['Content-Type'][0] );
$this->assertStringContainsString( 'charset=', $mailer->get_sent()->header );
}
/**
@@ -190,7 +190,7 @@ class Tests_Mail extends WP_UnitTestCase {
wp_mail( $to, $subject, $message, $headers );
$mailer = tests_retrieve_phpmailer_instance();
$this->assertTrue( strpos( $mailer->get_sent()->header, $expected ) > 0 );
$this->assertStringContainsString( $expected, $mailer->get_sent()->header );
}
/**
@@ -206,7 +206,7 @@ class Tests_Mail extends WP_UnitTestCase {
wp_mail( $to, $subject, $message, $headers );
$mailer = tests_retrieve_phpmailer_instance();
$this->assertTrue( strpos( $mailer->get_sent()->header, $expected ) > 0 );
$this->assertStringContainsString( $expected, $mailer->get_sent()->header );
}
/**
@@ -222,7 +222,7 @@ class Tests_Mail extends WP_UnitTestCase {
wp_mail( $to, $subject, $message, $headers );
$mailer = tests_retrieve_phpmailer_instance();
$this->assertTrue( strpos( $mailer->get_sent()->header, $expected ) > 0 );
$this->assertStringContainsString( $expected, $mailer->get_sent()->header );
}
/**
@@ -238,7 +238,7 @@ class Tests_Mail extends WP_UnitTestCase {
wp_mail( $to, $subject, $message, $headers );
$mailer = tests_retrieve_phpmailer_instance();
$this->assertTrue( strpos( $mailer->get_sent()->header, $expected ) > 0 );
$this->assertStringContainsString( $expected, $mailer->get_sent()->header );
}
/**
@@ -254,7 +254,7 @@ class Tests_Mail extends WP_UnitTestCase {
wp_mail( $to, $subject, $message, $headers );
$mailer = tests_retrieve_phpmailer_instance();
$this->assertTrue( strpos( $mailer->get_sent()->header, $expected ) > 0 );
$this->assertStringContainsString( $expected, $mailer->get_sent()->header );
}
/**
@@ -270,7 +270,7 @@ class Tests_Mail extends WP_UnitTestCase {
wp_mail( $to, $subject, $message, $headers );
$mailer = tests_retrieve_phpmailer_instance();
$this->assertTrue( strpos( $mailer->get_sent()->header, $expected ) > 0 );
$this->assertStringContainsString( $expected, $mailer->get_sent()->header );
}
/**
+2 -2
View File
@@ -1654,7 +1654,7 @@ EOF;
// Test to confirm all sources in the array include the same edit hash.
foreach ( $sizes as $size ) {
$this->assertNotFalse( strpos( $size, $hash ) );
$this->assertStringContainsString( $hash, $size );
}
}
@@ -2230,7 +2230,7 @@ EOF;
// Full size GIFs should not return a srcset.
$this->assertFalse( wp_calculate_image_srcset( $size_array, $full_src, $image_meta ) );
// Intermediate sized GIFs should not include the full size in the srcset.
$this->assertFalse( strpos( wp_calculate_image_srcset( $size_array, $large_src, $image_meta ), $full_src ) );
$this->assertStringNotContainsString( $full_src, wp_calculate_image_srcset( $size_array, $large_src, $image_meta ) );
}
/**
+14 -14
View File
@@ -32,8 +32,8 @@ class Tests_Embed_Template extends WP_UnitTestCase {
$doc = new DOMDocument();
$this->assertTrue( $doc->loadHTML( $actual ) );
$this->assertFalse( strpos( $actual, 'That embed can&#8217;t be found.' ) );
$this->assertNotFalse( strpos( $actual, 'Hello World' ) );
$this->assertStringNotContainsString( 'That embed can&#8217;t be found.', $actual );
$this->assertStringContainsString( 'Hello World', $actual );
}
function test_oembed_output_post_with_thumbnail() {
@@ -64,9 +64,9 @@ class Tests_Embed_Template extends WP_UnitTestCase {
$doc = new DOMDocument();
$this->assertTrue( $doc->loadHTML( $actual ) );
$this->assertFalse( strpos( $actual, 'That embed can&#8217;t be found.' ) );
$this->assertNotFalse( strpos( $actual, 'Hello World' ) );
$this->assertNotFalse( strpos( $actual, 'canola.jpg' ) );
$this->assertStringNotContainsString( 'That embed can&#8217;t be found.', $actual );
$this->assertStringContainsString( 'Hello World', $actual );
$this->assertStringContainsString( 'canola.jpg', $actual );
}
function test_oembed_output_404() {
@@ -81,7 +81,7 @@ class Tests_Embed_Template extends WP_UnitTestCase {
$doc = new DOMDocument();
$this->assertTrue( $doc->loadHTML( $actual ) );
$this->assertNotFalse( strpos( $actual, 'That embed can&#8217;t be found.' ) );
$this->assertStringContainsString( 'That embed can&#8217;t be found.', $actual );
}
function test_oembed_output_attachment() {
@@ -108,9 +108,9 @@ class Tests_Embed_Template extends WP_UnitTestCase {
$doc = new DOMDocument();
$this->assertTrue( $doc->loadHTML( $actual ) );
$this->assertFalse( strpos( $actual, 'That embed can&#8217;t be found.' ) );
$this->assertNotFalse( strpos( $actual, 'Hello World' ) );
$this->assertNotFalse( strpos( $actual, 'canola.jpg' ) );
$this->assertStringNotContainsString( 'That embed can&#8217;t be found.', $actual );
$this->assertStringContainsString( 'Hello World', $actual );
$this->assertStringContainsString( 'canola.jpg', $actual );
}
function test_oembed_output_draft_post() {
@@ -133,7 +133,7 @@ class Tests_Embed_Template extends WP_UnitTestCase {
$doc = new DOMDocument();
$this->assertTrue( $doc->loadHTML( $actual ) );
$this->assertNotFalse( strpos( $actual, 'That embed can&#8217;t be found.' ) );
$this->assertStringContainsString( 'That embed can&#8217;t be found.', $actual );
}
function test_oembed_output_scheduled_post() {
@@ -157,7 +157,7 @@ class Tests_Embed_Template extends WP_UnitTestCase {
$doc = new DOMDocument();
$this->assertTrue( $doc->loadHTML( $actual ) );
$this->assertNotFalse( strpos( $actual, 'That embed can&#8217;t be found.' ) );
$this->assertStringContainsString( 'That embed can&#8217;t be found.', $actual );
}
function test_oembed_output_private_post() {
@@ -180,7 +180,7 @@ class Tests_Embed_Template extends WP_UnitTestCase {
$doc = new DOMDocument();
$this->assertTrue( $doc->loadHTML( $actual ) );
$this->assertNotFalse( strpos( $actual, 'That embed can&#8217;t be found.' ) );
$this->assertStringContainsString( 'That embed can&#8217;t be found.', $actual );
}
function test_oembed_output_private_post_with_permissions() {
@@ -207,8 +207,8 @@ class Tests_Embed_Template extends WP_UnitTestCase {
$doc = new DOMDocument();
$this->assertTrue( $doc->loadHTML( $actual ) );
$this->assertFalse( strpos( $actual, 'That embed can&#8217;t be found.' ) );
$this->assertNotFalse( strpos( $actual, 'Hello World' ) );
$this->assertStringNotContainsString( 'That embed can&#8217;t be found.', $actual );
$this->assertStringContainsString( 'Hello World', $actual );
}
function test_wp_embed_excerpt_more_no_embed() {