close
Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
144 changes: 63 additions & 81 deletions tests/phpunit/tests/functions/wpUniqueIdFromValues.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
*/
class Tests_Functions_WpUniqueIdFromValues extends WP_UnitTestCase {

/**
* Prefix used for testing.
*
* @var string
*/
private $prefix = 'my-prefix-';

/**
* Test that the function returns consistent ids for the passed params.
*
Expand All @@ -21,11 +28,44 @@ class Tests_Functions_WpUniqueIdFromValues extends WP_UnitTestCase {
*
* @since 6.8.0
*/
public function test_wp_unique_id_from_values( $expected, $data, $prefix ) {
$output1 = wp_unique_id_from_values( $data );
$output2 = wp_unique_id_from_values( $data, $prefix );
$this->assertSame( $expected, $output1 );
$this->assertSame( $prefix . $expected, $output2 );
public function test_wp_unique_id_from_values( $data ) {
// Generate IDs.
$unique_id_original = wp_unique_id_from_values( $data );
$unique_id_prefixed = wp_unique_id_from_values( $data, $this->prefix );

// Ensure that the same input produces the same ID.
$this->assertSame( $unique_id_original, wp_unique_id_from_values( $data ) );
$this->assertSame( $unique_id_prefixed, wp_unique_id_from_values( $data, $this->prefix ) );

// Ensure that the prefixed ID is the prefix + the original ID.
$this->assertSame( $this->prefix . $unique_id_original, $unique_id_prefixed );
}

/**
* Test that different input data generates distinct IDs.
*
* @ticket 62985
*
* @dataProvider data_wp_unique_id_from_values
*
* @since 6.8.0
*/
public function test_wp_unique_id_from_values_uniqueness( $data ) {
// Generate IDs.
$unique_id_original = wp_unique_id_from_values( $data );
$unique_id_prefixed = wp_unique_id_from_values( $data, $this->prefix );

// Modify the data slightly to generate a different ID.
$data_modified = $data;
$data_modified['value'] = 'modified';

// Generate new IDs with the modified data.
$unique_id_modified = wp_unique_id_from_values( $data_modified );
$unique_id_prefixed_modified = wp_unique_id_from_values( $data_modified, $this->prefix );

// Assert that the IDs for different data are distinct.
$this->assertNotSame( $unique_id_original, $unique_id_modified );
$this->assertNotSame( $unique_id_prefixed, $unique_id_prefixed_modified );
}

/**
Expand All @@ -35,63 +75,24 @@ public function test_wp_unique_id_from_values( $expected, $data, $prefix ) {
*/
public function data_wp_unique_id_from_values() {
return array(
'string' => array(
'expected' => '469f5989',
'data' => array(
'value' => 'text',
),
'prefix' => 'my-prefix-',
),
'integer' => array(
'expected' => 'b2f0842e',
'data' => array(
'value' => 123,
),
'prefix' => 'my-prefix-',
),
'float' => array(
'expected' => 'a756f54d',
'data' => array(
'value' => 1.23,
),
'prefix' => 'my-prefix-',
),
'boolean' => array(
'expected' => 'bdae8be3',
'data' => array(
'value' => true,
),
'prefix' => 'my-prefix-',
),
'object' => array(
'expected' => '477bd670',
'data' => array(
'value' => new StdClass(),
),
'prefix' => 'my-prefix-',
),
'null' => array(
'expected' => 'a860dd95',
'data' => array(
'value' => null,
),
'prefix' => 'my-prefix-',
),
'string' => array( array( 'value' => 'text' ) ),
'integer' => array( array( 'value' => 123 ) ),
'float' => array( array( 'value' => 1.23 ) ),
'boolean' => array( array( 'value' => true ) ),
'object' => array( array( 'value' => new StdClass() ) ),
'null' => array( array( 'value' => null ) ),
'multiple values' => array(
'expected' => 'ef258a5d',
'data' => array(
array(
'value1' => 'text',
'value2' => 123,
'value3' => 1.23,
'value4' => true,
'value5' => new StdClass(),
'value6' => null,
),
'prefix' => 'my-prefix-',
),
'nested arrays' => array(
'expected' => '4345cae5',
'data' => array(
array(
'list1' => array(
'value1' => 'text',
'value2' => 123,
Expand All @@ -103,7 +104,6 @@ public function data_wp_unique_id_from_values() {
'value6' => null,
),
),
'prefix' => 'my-prefix-',
),
);
}
Expand All @@ -118,7 +118,7 @@ public function data_wp_unique_id_from_values() {
* @since 6.8.0
*/
public function test_wp_unique_id_from_values_empty_array() {
wp_unique_id_from_values( array(), 'my-prefix-' );
wp_unique_id_from_values( array(), $this->prefix );
}

/**
Expand All @@ -130,43 +130,25 @@ public function test_wp_unique_id_from_values_empty_array() {
*
* @since 6.8.0
*/
public function test_wp_unique_id_from_values_invalid_data( $data, $prefix ) {
public function test_wp_unique_id_from_values_invalid_data( $data ) {
$this->expectException( TypeError::class );

wp_unique_id_from_values( $data, $prefix );
wp_unique_id_from_values( $data, $this->prefix );
}

/**
* Data provider.
* Data provider for invalid data tests.
*
* @return array[]
*/
public function data_wp_unique_id_from_values_invalid_data() {
return array(
'string' => array(
'data' => 'text',
'prefix' => '',
),
'integer' => array(
'data' => 123,
'prefix' => '',
),
'float' => array(
'data' => 1.23,
'prefix' => '',
),
'boolean' => array(
'data' => true,
'prefix' => '',
),
'object' => array(
'data' => new StdClass(),
'prefix' => '',
),
'null' => array(
'data' => null,
'prefix' => '',
),
'string' => array( 'text' ),
'integer' => array( 123 ),
'float' => array( 1.23 ),
'boolean' => array( true ),
'object' => array( new StdClass() ),
'null' => array( null ),
);
}
}
Loading