Tests: Improve documentation and variable names in some formatting tests.

Includes documenting data provider values using hash notation in the tests for:
* `convert_smilies()`
* `get_url_in_content()`
* `links_add_target()`
* `normalize_whitespace()`

Follow-up to [26191], [26327], [26328], [26972], [55562].

See #57841.

git-svn-id: https://develop.svn.wordpress.org/trunk@55563 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2023-03-19 12:51:14 +00:00
parent d1046dc5f3
commit a387dee3f9
9 changed files with 135 additions and 78 deletions

View File

@ -5337,19 +5337,19 @@ function _links_add_base( $m ) {
}
/**
* Adds a Target attribute to all links in passed content.
* Adds a target attribute to all links in passed content.
*
* This function by default only applies to `<a>` tags, however this can be
* modified by the 3rd param.
* modified by the `$tags` parameter.
*
* *NOTE:* Any current target attributed will be stripped and replaced.
* *NOTE:* Any current target attribute will be stripped and replaced.
*
* @since 2.7.0
*
* @global string $_links_add_target
*
* @param string $content String to search for links in.
* @param string $target The Target to add to the links.
* @param string $target The target to add to the links.
* @param string[] $tags An array of tags to apply to.
* @return string The processed content.
*/

View File

@ -9,29 +9,34 @@
class Tests_Formatting_ConvertSmilies extends WP_UnitTestCase {
/**
* @dataProvider data_convert_standard_smilies
* Basic validation test to confirm that smilies are converted to image
* when use_smilies = 1 and not when use_smilies = 0.
*
* Basic Validation Test to confirm that smilies are converted to image
* when use_smilies = 1 and not when use_smilies = 0
* @dataProvider data_convert_standard_smilies
*/
public function test_convert_standard_smilies( $in_txt, $converted_txt ) {
public function test_convert_standard_smilies( $input, $converted ) {
// Standard smilies, use_smilies: ON.
update_option( 'use_smilies', 1 );
smilies_init();
$this->assertSame( $converted_txt, convert_smilies( $in_txt ) );
$this->assertSame( $converted, convert_smilies( $input ) );
// Standard smilies, use_smilies: OFF.
update_option( 'use_smilies', 0 );
$this->assertSame( $in_txt, convert_smilies( $in_txt ) );
$this->assertSame( $input, convert_smilies( $input ) );
}
/**
* Basic Test Content DataProvider
* Data provider.
*
* array ( input_txt, converted_output_txt)
* @return array {
* @type array {
* @type string $input Input content.
* @type string $converted Converted output.
* }
* }
*/
public function data_convert_standard_smilies() {
$includes_path = includes_url( 'images/smilies/' );
@ -65,11 +70,11 @@ class Tests_Formatting_ConvertSmilies extends WP_UnitTestCase {
}
/**
* @dataProvider data_convert_custom_smilies
* Tests that custom smilies are converted to images when use_smilies = 1.
*
* Validate Custom Smilies are converted to images when use_smilies = 1
* @dataProvider data_convert_custom_smilies
*/
public function test_convert_custom_smilies( $in_txt, $converted_txt ) {
public function test_convert_custom_smilies( $input, $converted ) {
global $wpsmiliestrans;
// Custom smilies, use_smilies: ON.
@ -90,20 +95,25 @@ class Tests_Formatting_ConvertSmilies extends WP_UnitTestCase {
smilies_init();
$this->assertSame( $converted_txt, convert_smilies( $in_txt ) );
$this->assertSame( $converted, convert_smilies( $input ) );
// Standard smilies, use_smilies: OFF.
update_option( 'use_smilies', 0 );
$this->assertSame( $in_txt, convert_smilies( $in_txt ) );
$this->assertSame( $input, convert_smilies( $input ) );
$wpsmiliestrans = $trans_orig; // Reset original translations array.
}
/**
* Custom Smilies Test Content DataProvider
* Data provider.
*
* array ( input_txt, converted_output_txt)
* @return array {
* @type array {
* @type string $input Input content.
* @type string $converted Converted output.
* }
* }
*/
public function data_convert_custom_smilies() {
$includes_path = includes_url( 'images/smilies/' );
@ -125,8 +135,8 @@ class Tests_Formatting_ConvertSmilies extends WP_UnitTestCase {
}
/**
* Validate Conversion of Smilies is ignored in pre-determined tags
* pre, code, script, style
* Tests that conversion of smilies is ignored in pre-determined tags:
* pre, code, script, style.
*
* @ticket 16448
* @dataProvider data_ignore_smilies_in_tags
@ -134,21 +144,27 @@ class Tests_Formatting_ConvertSmilies extends WP_UnitTestCase {
public function test_ignore_smilies_in_tags( $element ) {
$includes_path = includes_url( 'images/smilies/' );
$in_str = 'Do we ingore smilies ;-) in ' . $element . ' tags <' . $element . ' class="foo">My Content Here :?: </' . $element . '>';
$exp_str = "Do we ingore smilies \xf0\x9f\x98\x89 in $element tags <$element class=\"foo\">My Content Here :?: </$element>";
$input = 'Do we ignore smilies ;-) in ' . $element . ' tags <' . $element . ' class="foo">My Content Here :?: </' . $element . '>';
$expected = "Do we ignore smilies \xf0\x9f\x98\x89 in $element tags <$element class=\"foo\">My Content Here :?: </$element>";
// Standard smilies, use_smilies: ON.
update_option( 'use_smilies', 1 );
smilies_init();
$this->assertSame( $exp_str, convert_smilies( $in_str ) );
$this->assertSame( $expected, convert_smilies( $input ) );
// Standard smilies, use_smilies: OFF.
update_option( 'use_smilies', 0 );
}
/**
* DataProvider of HTML elements/tags that smilie matches should be ignored in
* Data provider.
*
* @return array {
* @type array {
* @type string $element HTML tag name.
* }
* }
*/
public function data_ignore_smilies_in_tags() {
return array(
@ -161,27 +177,34 @@ class Tests_Formatting_ConvertSmilies extends WP_UnitTestCase {
}
/**
* Validate Combinations of Smilies separated by single space
* are converted correctly
* Tests that combinations of smilies separated by a single space
* are converted correctly.
*
* @ticket 20124
* @dataProvider data_smilies_combinations
*/
public function test_smilies_combinations( $in_txt, $converted_txt ) {
public function test_smilies_combinations( $input, $converted ) {
// Custom smilies, use_smilies: ON.
update_option( 'use_smilies', 1 );
smilies_init();
$this->assertSame( $converted_txt, convert_smilies( $in_txt ) );
$this->assertSame( $converted, convert_smilies( $input ) );
// Custom smilies, use_smilies: OFF.
update_option( 'use_smilies', 0 );
$this->assertSame( $in_txt, convert_smilies( $in_txt ) );
$this->assertSame( $input, convert_smilies( $input ) );
}
/**
* DataProvider of Smilie Combinations
* Data provider.
*
* @return array {
* @type array {
* @type string $input Input content.
* @type string $converted Converted output.
* }
* }
*/
public function data_smilies_combinations() {
$includes_path = includes_url( 'images/smilies/' );
@ -215,13 +238,13 @@ class Tests_Formatting_ConvertSmilies extends WP_UnitTestCase {
}
/**
* Validate Smilies are converted for single smilie in
* the $wpsmiliestrans global array
* Tests that smilies are converted for single smilie in
* the $wpsmiliestrans global array.
*
* @ticket 25303
* @dataProvider data_single_smilies_in_wpsmiliestrans
*/
public function test_single_smilies_in_wpsmiliestrans( $in_txt, $converted_txt ) {
public function test_single_smilies_in_wpsmiliestrans( $input, $converted ) {
global $wpsmiliestrans;
// Standard smilies, use_smilies: ON.
@ -239,18 +262,25 @@ class Tests_Formatting_ConvertSmilies extends WP_UnitTestCase {
smilies_init();
$this->assertSame( $converted_txt, convert_smilies( $in_txt ) );
$this->assertSame( $converted, convert_smilies( $input ) );
// Standard smilies, use_smilies: OFF.
update_option( 'use_smilies', 0 );
$this->assertSame( $in_txt, convert_smilies( $in_txt ) );
$this->assertSame( $input, convert_smilies( $input ) );
$wpsmiliestrans = $orig_trans; // Reset original translations array.
}
/**
* DataProvider of Single Smilies input and converted output
* Data provider.
*
* @return array {
* @type array {
* @type string $input Input content.
* @type string $converted Converted output.
* }
* }
*/
public function data_single_smilies_in_wpsmiliestrans() {
$includes_path = includes_url( 'images/smilies/' );
@ -272,27 +302,37 @@ class Tests_Formatting_ConvertSmilies extends WP_UnitTestCase {
}
/**
* Check that $wp_smiliessearch pattern will match smilies
* Tests that $wp_smiliessearch pattern will match smilies
* between spaces, but never capture those spaces.
*
* Further check that spaces aren't randomly deleted
* Further tests that spaces aren't randomly deleted
* or added when replacing the text with an image.
*
* @ticket 22692
* @dataProvider data_spaces_around_smilies
*/
public function test_spaces_around_smilies( $in_txt, $converted_txt ) {
public function test_spaces_around_smilies( $input, $converted ) {
// Standard smilies, use_smilies: ON.
update_option( 'use_smilies', 1 );
smilies_init();
$this->assertSame( $converted_txt, convert_smilies( $in_txt ) );
$this->assertSame( $converted, convert_smilies( $input ) );
// Standard smilies, use_smilies: OFF.
update_option( 'use_smilies', 0 );
}
/**
* Data provider.
*
* @return array {
* @type array {
* @type string $input Input content.
* @type string $converted Converted output.
* }
* }
*/
public function data_spaces_around_smilies() {
$nbsp = "\xC2\xA0";

View File

@ -8,18 +8,23 @@
class Tests_Formatting_GetUrlInContent extends WP_UnitTestCase {
/**
* Validate the get_url_in_content function
* Tests the get_url_in_content() function.
*
* @dataProvider data_get_url_in_content
*/
public function test_get_url_in_content( $in_str, $exp_str ) {
$this->assertSame( $exp_str, get_url_in_content( $in_str ) );
public function test_get_url_in_content( $input, $expected ) {
$this->assertSame( $expected, get_url_in_content( $input ) );
}
/**
* URL Content Data Provider
* Data provider.
*
* array ( input_txt, converted_output_txt )
* @return array {
* @type array {
* @type string $input Input content.
* @type string $expected Expected output.
* }
* }
*/
public function data_get_url_in_content() {
return array(

View File

@ -7,24 +7,31 @@
class Tests_Formatting_LinksAddTarget extends WP_UnitTestCase {
/**
* Validate the normalize_whitespace function
* Tests the links_add_target() function.
*
* @dataProvider data_links_add_target
*/
public function test_links_add_target( $content, $target, $tags, $exp_str ) {
if ( true === is_null( $target ) ) {
$this->assertSame( $exp_str, links_add_target( $content ) );
} elseif ( true === is_null( $tags ) ) {
$this->assertSame( $exp_str, links_add_target( $content, $target ) );
public function test_links_add_target( $content, $target, $tags, $expected ) {
if ( is_null( $target ) ) {
$this->assertSame( $expected, links_add_target( $content ) );
} elseif ( is_null( $tags ) ) {
$this->assertSame( $expected, links_add_target( $content, $target ) );
} else {
$this->assertSame( $exp_str, links_add_target( $content, $target, $tags ) );
$this->assertSame( $expected, links_add_target( $content, $target, $tags ) );
}
}
/**
* Test Content DataProvider
* Data provider.
*
* array ( input_txt, converted_output_txt)
* @return array {
* @type array {
* @type string $content String to search for links in.
* @type string $target The target to add to the links.
* @type string $tags An array of tags to apply to.
* @type string $expected Expected output.
* }
* }
*/
public function data_links_add_target() {
return array(

View File

@ -7,18 +7,23 @@
class Tests_Formatting_NormalizeWhitespace extends WP_UnitTestCase {
/**
* Validate the normalize_whitespace function
* Tests the the normalize_whitespace() function.
*
* @dataProvider data_normalize_whitespace
*/
public function test_normalize_whitespace( $in_str, $exp_str ) {
$this->assertSame( $exp_str, normalize_whitespace( $in_str ) );
public function test_normalize_whitespace( $input, $expected ) {
$this->assertSame( $expected, normalize_whitespace( $input ) );
}
/**
* WhitespaceTest Content DataProvider
* Data provider.
*
* array( input_txt, converted_output_txt)
* @return array {
* @type array {
* @type string $input Input content.
* @type string $expected Expected output.
* }
* }
*/
public function data_normalize_whitespace() {
return array(

View File

@ -9,7 +9,19 @@
class Tests_Formatting_UrlencodeDeep extends WP_UnitTestCase {
/**
* Data Provider
* Tests the urlencode_deep() function pair by pair.
*
* @dataProvider data_urlencode_deep
*
* @param string $input
* @param string $expected
*/
public function test_urlencode_deep_should_encode_individual_value( $input, $expected ) {
$this->assertSame( $expected, urlencode_deep( $input ) );
}
/**
* Data provider.
*/
public function data_urlencode_deep() {
return array(
@ -22,19 +34,7 @@ class Tests_Formatting_UrlencodeDeep extends WP_UnitTestCase {
}
/**
* Validate the urlencode_deep function pair by pair
*
* @dataProvider data_urlencode_deep
*
* @param string $actual
* @param string $expected
*/
public function test_urlencode_deep_should_encode_individual_value( $actual, $expected ) {
$this->assertSame( $expected, urlencode_deep( $actual ) );
}
/**
* Test the whole array as input
* Tests the whole array as input.
*/
public function test_urlencode_deep_should_encode_all_values_in_array() {
$data = $this->data_urlencode_deep();

View File

@ -24,7 +24,7 @@ class Tests_Formatting_wpParseStr extends WP_UnitTestCase {
}
/**
* Data Provider.
* Data provider.
*
* @return array
*/

View File

@ -947,7 +947,7 @@ EOF;
}
/**
* Data Provider for test_safecss_filter_attr().
* Data provider for test_safecss_filter_attr().
*
* @return array {
* @type array {
@ -1580,7 +1580,7 @@ EOF;
}
/**
* Data Provider for test_safecss_filter_attr_filtered().
* Data provider for test_safecss_filter_attr_filtered().
*
* @return array {
* @type array {

View File

@ -20,7 +20,7 @@ class Tests_Pluggable_wpRand extends WP_UnitTestCase {
$this->assertGreaterThanOrEqual(
0,
wp_rand( $min, $max ),
'The value was not greater than or equal 0'
'The value was not greater than or equal to 0'
);
$this->assertLessThan(