mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2025-10-16 12:05:38 +00:00
PHP 8.0 introduced a new function: `str_contains()`. It performs a case-sensitive check indicating if given substring (needle) is contained in the string to search in (haystack). This polyfill makes this function available for use in Core. Ref: * PHP RFC https://wiki.php.net/rfc/str_contains * PHP manual https://www.php.net/manual/en/function.str-contains.php Props ayeshrajans, costdev, desrosj, hellofromTonya, knutsp, pbearne. Fixes #49652. git-svn-id: https://develop.svn.wordpress.org/trunk@52039 602fd350-edb4-49c9-b593-d223f7449a82
105 lines
2.5 KiB
PHP
105 lines
2.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* @group compat
|
|
*
|
|
* @covers ::str_contains
|
|
*/
|
|
class Tests_Compat_strContains extends WP_UnitTestCase {
|
|
|
|
/**
|
|
* Test that str_contains() is always available (either from PHP or WP).
|
|
*
|
|
* @ticket 49652
|
|
*/
|
|
public function test_is_str_contains_availability() {
|
|
$this->assertTrue( function_exists( 'str_contains' ) );
|
|
}
|
|
|
|
/**
|
|
* @dataProvider data_str_contains
|
|
*
|
|
* @ticket 49652
|
|
*
|
|
* @param bool $expected Whether or not `$haystack` is expected to contain `$needle`.
|
|
* @param string $haystack The string to search in.
|
|
* @param string $needle The substring to search for in `$haystack`.
|
|
*/
|
|
public function test_str_contains( $expected, $haystack, $needle ) {
|
|
if ( ! function_exists( 'str_contains' ) ) {
|
|
$this->markTestSkipped( 'str_contains() is not available.' );
|
|
} else {
|
|
$this->assertSame(
|
|
$expected,
|
|
str_contains( $haystack, $needle )
|
|
);
|
|
}
|
|
|
|
}
|
|
|
|
/**
|
|
* Data provider.
|
|
*
|
|
* @return array
|
|
*/
|
|
public function data_str_contains() {
|
|
return array(
|
|
'empty needle' => array(
|
|
'expected' => true,
|
|
'haystack' => 'This is a Test',
|
|
'needle' => '',
|
|
),
|
|
'empty haystack and needle' => array(
|
|
'expected' => true,
|
|
'haystack' => '',
|
|
'needle' => '',
|
|
),
|
|
'empty haystack' => array(
|
|
'expected' => false,
|
|
'haystack' => '',
|
|
'needle' => 'test',
|
|
),
|
|
'start of string' => array(
|
|
'expected' => true,
|
|
'haystack' => 'This is a Test',
|
|
'needle' => 'This',
|
|
),
|
|
'middle of string' => array(
|
|
'expected' => true,
|
|
'haystack' => 'The needle in middle of string.',
|
|
'needle' => 'middle',
|
|
),
|
|
'end of string' => array(
|
|
'expected' => true,
|
|
'string' => 'The needle is at end.',
|
|
'needle' => 'end',
|
|
),
|
|
'lowercase' => array(
|
|
'expected' => true,
|
|
'string' => 'This is a test',
|
|
'needle' => 'test',
|
|
),
|
|
'uppercase' => array(
|
|
'expected' => true,
|
|
'string' => 'This is a TEST',
|
|
'needle' => 'TEST',
|
|
),
|
|
'camelCase' => array(
|
|
'expected' => true,
|
|
'string' => 'String contains camelCase.',
|
|
'needle' => 'camelCase',
|
|
),
|
|
'with hyphen' => array(
|
|
'expected' => true,
|
|
'string' => 'String contains foo-bar needle.',
|
|
'needle' => 'foo-bar',
|
|
),
|
|
'missing' => array(
|
|
'expected' => false,
|
|
'haystack' => 'This is a camelcase',
|
|
'needle' => 'camelCase',
|
|
),
|
|
);
|
|
}
|
|
}
|