1212 */
1313class Tests_Functions_WpUniqueIdFromValues extends WP_UnitTestCase {
1414
15+ /**
16+ * Prefix used for testing.
17+ *
18+ * @var string
19+ */
20+ private $ prefix = 'my-prefix- ' ;
21+
1522 /**
1623 * Test that the function returns consistent ids for the passed params.
1724 *
@@ -21,11 +28,44 @@ class Tests_Functions_WpUniqueIdFromValues extends WP_UnitTestCase {
2128 *
2229 * @since 6.8.0
2330 */
24- public function test_wp_unique_id_from_values ( $ expected , $ data , $ prefix ) {
25- $ output1 = wp_unique_id_from_values ( $ data );
26- $ output2 = wp_unique_id_from_values ( $ data , $ prefix );
27- $ this ->assertSame ( $ expected , $ output1 );
28- $ this ->assertSame ( $ prefix . $ expected , $ output2 );
31+ public function test_wp_unique_id_from_values ( $ data ) {
32+ // Generate IDs.
33+ $ unique_id_original = wp_unique_id_from_values ( $ data );
34+ $ unique_id_prefixed = wp_unique_id_from_values ( $ data , $ this ->prefix );
35+
36+ // Ensure that the same input produces the same ID.
37+ $ this ->assertSame ( $ unique_id_original , wp_unique_id_from_values ( $ data ) );
38+ $ this ->assertSame ( $ unique_id_prefixed , wp_unique_id_from_values ( $ data , $ this ->prefix ) );
39+
40+ // Ensure that the prefixed ID is the prefix + the original ID.
41+ $ this ->assertSame ( $ this ->prefix . $ unique_id_original , $ unique_id_prefixed );
42+ }
43+
44+ /**
45+ * Test that different input data generates distinct IDs.
46+ *
47+ * @ticket 62985
48+ *
49+ * @dataProvider data_wp_unique_id_from_values
50+ *
51+ * @since 6.8.0
52+ */
53+ public function test_wp_unique_id_from_values_uniqueness ( $ data ) {
54+ // Generate IDs.
55+ $ unique_id_original = wp_unique_id_from_values ( $ data );
56+ $ unique_id_prefixed = wp_unique_id_from_values ( $ data , $ this ->prefix );
57+
58+ // Modify the data slightly to generate a different ID.
59+ $ data_modified = $ data ;
60+ $ data_modified ['value ' ] = 'modified ' ;
61+
62+ // Generate new IDs with the modified data.
63+ $ unique_id_modified = wp_unique_id_from_values ( $ data_modified );
64+ $ unique_id_prefixed_modified = wp_unique_id_from_values ( $ data_modified , $ this ->prefix );
65+
66+ // Assert that the IDs for different data are distinct.
67+ $ this ->assertNotSame ( $ unique_id_original , $ unique_id_modified );
68+ $ this ->assertNotSame ( $ unique_id_prefixed , $ unique_id_prefixed_modified );
2969 }
3070
3171 /**
@@ -35,63 +75,24 @@ public function test_wp_unique_id_from_values( $expected, $data, $prefix ) {
3575 */
3676 public function data_wp_unique_id_from_values () {
3777 return array (
38- 'string ' => array (
39- 'expected ' => '469f5989 ' ,
40- 'data ' => array (
41- 'value ' => 'text ' ,
42- ),
43- 'prefix ' => 'my-prefix- ' ,
44- ),
45- 'integer ' => array (
46- 'expected ' => 'b2f0842e ' ,
47- 'data ' => array (
48- 'value ' => 123 ,
49- ),
50- 'prefix ' => 'my-prefix- ' ,
51- ),
52- 'float ' => array (
53- 'expected ' => 'a756f54d ' ,
54- 'data ' => array (
55- 'value ' => 1.23 ,
56- ),
57- 'prefix ' => 'my-prefix- ' ,
58- ),
59- 'boolean ' => array (
60- 'expected ' => 'bdae8be3 ' ,
61- 'data ' => array (
62- 'value ' => true ,
63- ),
64- 'prefix ' => 'my-prefix- ' ,
65- ),
66- 'object ' => array (
67- 'expected ' => '477bd670 ' ,
68- 'data ' => array (
69- 'value ' => new StdClass (),
70- ),
71- 'prefix ' => 'my-prefix- ' ,
72- ),
73- 'null ' => array (
74- 'expected ' => 'a860dd95 ' ,
75- 'data ' => array (
76- 'value ' => null ,
77- ),
78- 'prefix ' => 'my-prefix- ' ,
79- ),
78+ 'string ' => array ( array ( 'value ' => 'text ' ) ),
79+ 'integer ' => array ( array ( 'value ' => 123 ) ),
80+ 'float ' => array ( array ( 'value ' => 1.23 ) ),
81+ 'boolean ' => array ( array ( 'value ' => true ) ),
82+ 'object ' => array ( array ( 'value ' => new StdClass () ) ),
83+ 'null ' => array ( array ( 'value ' => null ) ),
8084 'multiple values ' => array (
81- 'expected ' => 'ef258a5d ' ,
82- 'data ' => array (
85+ array (
8386 'value1 ' => 'text ' ,
8487 'value2 ' => 123 ,
8588 'value3 ' => 1.23 ,
8689 'value4 ' => true ,
8790 'value5 ' => new StdClass (),
8891 'value6 ' => null ,
8992 ),
90- 'prefix ' => 'my-prefix- ' ,
9193 ),
9294 'nested arrays ' => array (
93- 'expected ' => '4345cae5 ' ,
94- 'data ' => array (
95+ array (
9596 'list1 ' => array (
9697 'value1 ' => 'text ' ,
9798 'value2 ' => 123 ,
@@ -103,7 +104,6 @@ public function data_wp_unique_id_from_values() {
103104 'value6 ' => null ,
104105 ),
105106 ),
106- 'prefix ' => 'my-prefix- ' ,
107107 ),
108108 );
109109 }
@@ -118,7 +118,7 @@ public function data_wp_unique_id_from_values() {
118118 * @since 6.8.0
119119 */
120120 public function test_wp_unique_id_from_values_empty_array () {
121- wp_unique_id_from_values ( array (), ' my- prefix- ' );
121+ wp_unique_id_from_values ( array (), $ this -> prefix );
122122 }
123123
124124 /**
@@ -130,43 +130,25 @@ public function test_wp_unique_id_from_values_empty_array() {
130130 *
131131 * @since 6.8.0
132132 */
133- public function test_wp_unique_id_from_values_invalid_data ( $ data, $ prefix ) {
133+ public function test_wp_unique_id_from_values_invalid_data ( $ data ) {
134134 $ this ->expectException ( TypeError::class );
135135
136- wp_unique_id_from_values ( $ data , $ prefix );
136+ wp_unique_id_from_values ( $ data , $ this -> prefix );
137137 }
138138
139139 /**
140- * Data provider.
140+ * Data provider for invalid data tests .
141141 *
142142 * @return array[]
143143 */
144144 public function data_wp_unique_id_from_values_invalid_data () {
145145 return array (
146- 'string ' => array (
147- 'data ' => 'text ' ,
148- 'prefix ' => '' ,
149- ),
150- 'integer ' => array (
151- 'data ' => 123 ,
152- 'prefix ' => '' ,
153- ),
154- 'float ' => array (
155- 'data ' => 1.23 ,
156- 'prefix ' => '' ,
157- ),
158- 'boolean ' => array (
159- 'data ' => true ,
160- 'prefix ' => '' ,
161- ),
162- 'object ' => array (
163- 'data ' => new StdClass (),
164- 'prefix ' => '' ,
165- ),
166- 'null ' => array (
167- 'data ' => null ,
168- 'prefix ' => '' ,
169- ),
146+ 'string ' => array ( 'text ' ),
147+ 'integer ' => array ( 123 ),
148+ 'float ' => array ( 1.23 ),
149+ 'boolean ' => array ( true ),
150+ 'object ' => array ( new StdClass () ),
151+ 'null ' => array ( null ),
170152 );
171153 }
172154}
0 commit comments