Tests: Move the tests for individual pluggable functions into their own directory.

This aims to make the tests more discoverable and easier to expand.

Follow-up to [50790], [53473], [53477].

See #55652.

git-svn-id: https://develop.svn.wordpress.org/trunk@53478 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2022-06-07 14:44:00 +00:00
parent e907a66546
commit 89cc0f5d95
3 changed files with 124 additions and 110 deletions

View File

@ -338,114 +338,4 @@ class Tests_Pluggable extends WP_UnitTestCase {
return $signatures;
}
/**
* @ticket 28020
*/
public function test_get_user_by_should_return_same_instance_as_wp_get_current_user() {
// Create a test user.
$new_user = self::factory()->user->create( array( 'role' => 'subscriber' ) );
// Set the test user as the current user.
$current_user = wp_set_current_user( $new_user );
// Get the test user using get_user_by().
$from_get_user_by = get_user_by( 'id', $new_user );
$this->assertSame( $current_user, $from_get_user_by );
}
/**
* Tests that wp_rand() returns zero when `$min` and `$max` are zero.
*
* @ticket 55194
* @dataProvider data_wp_rand_should_return_zero_when_min_and_max_are_zero
* @covers ::wp_rand
*
* @param mixed $min Lower limit for the generated number.
* @param mixed $max Upper limit for the generated number.
*/
public function test_wp_rand_should_return_zero_when_min_and_max_are_zero( $min, $max ) {
$this->assertSame( 0, wp_rand( $min, $max ) );
}
/**
* Data provider.
*
* @return array
*/
public function data_wp_rand_should_return_zero_when_min_and_max_are_zero() {
return array(
'min and max as 0' => array(
'min' => 0,
'max' => 0,
),
'min and max as 0.0' => array(
'min' => 0.0,
'max' => 0.0,
),
'min as null, max as 0' => array(
'min' => null,
'max' => 0,
),
);
}
/**
* Tests that wp_rand() returns a positive integer for both positive and negative input.
*
* @ticket 55194
* @dataProvider data_wp_rand_should_return_a_positive_integer
* @covers ::wp_rand
*
* @param int $min Lower limit for the generated number.
* @param int $max Upper limit for the generated number.
*/
public function test_wp_rand_should_return_a_positive_integer( $min, $max ) {
$this->assertGreaterThan(
0,
wp_rand( $min, $max ),
'The value was not greater than 0'
);
$this->assertLessThan(
100,
wp_rand( $min, $max ),
'The value was not less than 100'
);
}
/**
* Data provider.
*
* @return array
*/
public function data_wp_rand_should_return_a_positive_integer() {
return array(
'1 and 99' => array(
'min' => 1,
'max' => 99,
),
'-1 and 99' => array(
'min' => -1,
'max' => 99,
),
'1 and -99' => array(
'min' => 1,
'max' => -99,
),
'-1 and -99' => array(
'min' => -1,
'max' => -99,
),
'1.0 and 99.0' => array(
'min' => 1.0,
'max' => 99.0,
),
'-1.0 and -99.0' => array(
'min' => -1.0,
'max' => -99.0,
),
);
}
}

View File

@ -0,0 +1,24 @@
<?php
/**
* @group pluggable
* @covers ::get_user_by
*/
class Tests_Pluggable_GetUserBy extends WP_UnitTestCase {
/**
* @ticket 28020
*/
public function test_get_user_by_should_return_same_instance_as_wp_get_current_user() {
// Create a test user.
$new_user = self::factory()->user->create( array( 'role' => 'subscriber' ) );
// Set the test user as the current user.
$current_user = wp_set_current_user( $new_user );
// Get the test user using get_user_by().
$from_get_user_by = get_user_by( 'id', $new_user );
$this->assertSame( $current_user, $from_get_user_by );
}
}

View File

@ -0,0 +1,100 @@
<?php
/**
* @group pluggable
* @covers ::wp_rand
*/
class Tests_Pluggable_wpRand extends WP_UnitTestCase {
/**
* Tests that wp_rand() returns a positive integer for both positive and negative input.
*
* @ticket 55194
* @dataProvider data_wp_rand_should_return_a_positive_integer
*
* @param int $min Lower limit for the generated number.
* @param int $max Upper limit for the generated number.
*/
public function test_wp_rand_should_return_a_positive_integer( $min, $max ) {
$this->assertGreaterThan(
0,
wp_rand( $min, $max ),
'The value was not greater than 0'
);
$this->assertLessThan(
100,
wp_rand( $min, $max ),
'The value was not less than 100'
);
}
/**
* Data provider.
*
* @return array
*/
public function data_wp_rand_should_return_a_positive_integer() {
return array(
'1 and 99' => array(
'min' => 1,
'max' => 99,
),
'-1 and 99' => array(
'min' => -1,
'max' => 99,
),
'1 and -99' => array(
'min' => 1,
'max' => -99,
),
'-1 and -99' => array(
'min' => -1,
'max' => -99,
),
'1.0 and 99.0' => array(
'min' => 1.0,
'max' => 99.0,
),
'-1.0 and -99.0' => array(
'min' => -1.0,
'max' => -99.0,
),
);
}
/**
* Tests that wp_rand() returns zero when `$min` and `$max` are zero.
*
* @ticket 55194
* @dataProvider data_wp_rand_should_return_zero_when_min_and_max_are_zero
*
* @param mixed $min Lower limit for the generated number.
* @param mixed $max Upper limit for the generated number.
*/
public function test_wp_rand_should_return_zero_when_min_and_max_are_zero( $min, $max ) {
$this->assertSame( 0, wp_rand( $min, $max ) );
}
/**
* Data provider.
*
* @return array
*/
public function data_wp_rand_should_return_zero_when_min_and_max_are_zero() {
return array(
'min and max as 0' => array(
'min' => 0,
'max' => 0,
),
'min and max as 0.0' => array(
'min' => 0.0,
'max' => 0.0,
),
'min as null, max as 0' => array(
'min' => null,
'max' => 0,
),
);
}
}