Changeset 60116
- Timestamp:
- 04/01/2025 01:30:09 PM (3 months ago)
- Location:
- branches/6.8
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/6.8
-
branches/6.8/tests/phpunit/tests/functions/wpUniqueIdFromValues.php
r60038 r60116 14 14 15 15 /** 16 * Prefix used for testing. 17 * 18 * @var string 19 */ 20 private $prefix = 'my-prefix-'; 21 22 /** 16 23 * Test that the function returns consistent ids for the passed params. 17 24 * … … 22 29 * @since 6.8.0 23 30 */ 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 ); 29 69 } 30 70 … … 36 76 public function data_wp_unique_id_from_values() { 37 77 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 ) ), 80 84 'multiple values' => array( 81 'expected' => 'ef258a5d', 82 'data' => array( 85 array( 83 86 'value1' => 'text', 84 87 'value2' => 123, … … 88 91 'value6' => null, 89 92 ), 90 'prefix' => 'my-prefix-',91 93 ), 92 94 'nested arrays' => array( 93 'expected' => '4345cae5', 94 'data' => array( 95 array( 95 96 'list1' => array( 96 97 'value1' => 'text', … … 104 105 ), 105 106 ), 106 'prefix' => 'my-prefix-',107 107 ), 108 108 ); … … 119 119 */ 120 120 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 ); 122 122 } 123 123 … … 131 131 * @since 6.8.0 132 132 */ 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 ) { 134 134 $this->expectException( TypeError::class ); 135 135 136 wp_unique_id_from_values( $data, $ prefix );136 wp_unique_id_from_values( $data, $this->prefix ); 137 137 } 138 138 139 139 /** 140 * Data provider .140 * Data provider for invalid data tests. 141 141 * 142 142 * @return array[] … … 144 144 public function data_wp_unique_id_from_values_invalid_data() { 145 145 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 ), 170 152 ); 171 153 }
Note: See TracChangeset
for help on using the changeset viewer.