Changeset 57257 for trunk/tests/phpunit/tests/actions.php
- Timestamp:
- 01/09/2024 04:32:14 PM (8 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/tests/phpunit/tests/actions.php
r56559 r57257 230 230 } 231 231 232 public function test_action_priority() { 233 $a = new MockAction(); 234 $hook_name = __FUNCTION__; 235 236 add_action( $hook_name, array( &$a, 'action' ), 10 ); 237 add_action( $hook_name, array( &$a, 'action2' ), 9 ); 232 /** 233 * @ticket 60193 234 * 235 * @dataProvider data_priority_callback_order_with_integers 236 * @dataProvider data_priority_callback_order_with_unhappy_path_nonintegers 237 * 238 * @covers ::do_action 239 * 240 * @param array $priorities { 241 * Indexed array of the priorities for the MockAction callbacks. 242 * 243 * @type mixed $0 Priority for 'action' callback. 244 * @type mixed $1 Priority for 'action2' callback. 245 * } 246 * @param array $expected_call_order An array of callback names in expected call order. 247 * @param string $expected_deprecation Optional. Deprecation message. Default ''. 248 */ 249 public function test_priority_callback_order( $priorities, $expected_call_order, $expected_deprecation = '' ) { 250 $mock = new MockAction(); 251 $hook_name = __FUNCTION__; 252 253 if ( $expected_deprecation && PHP_VERSION_ID >= 80100 ) { 254 $this->expectDeprecation(); 255 $this->expectDeprecationMessage( $expected_deprecation ); 256 } 257 258 add_action( $hook_name, array( $mock, 'action' ), $priorities[0] ); 259 add_action( $hook_name, array( $mock, 'action2' ), $priorities[1] ); 238 260 do_action( $hook_name ); 239 261 240 // Two events, one per action. 241 $this->assertSame( 2, $a->get_call_count() ); 242 243 $expected = array( 244 // 'action2' is called first because it has priority 9. 245 array( 246 'action' => 'action2', 247 'hook_name' => $hook_name, 248 'tag' => $hook_name, // Back compat. 249 'args' => array( '' ), 250 ), 251 // 'action' is called second. 252 array( 253 'action' => 'action', 254 'hook_name' => $hook_name, 255 'tag' => $hook_name, // Back compat. 256 'args' => array( '' ), 262 $this->assertSame( 2, $mock->get_call_count(), 'The number of call counts does not match' ); 263 264 $actual_call_order = wp_list_pluck( $mock->get_events(), 'action' ); 265 $this->assertSame( $expected_call_order, $actual_call_order, 'The action callback order does not match the expected order' ); 266 } 267 268 /** 269 * Happy path data provider. 270 * 271 * @return array[] 272 */ 273 public function data_priority_callback_order_with_integers() { 274 return array( 275 'int DESC' => array( 276 'priorities' => array( 10, 9 ), 277 'expected_call_order' => array( 'action2', 'action' ), 278 ), 279 'int ASC' => array( 280 'priorities' => array( 9, 10 ), 281 'expected_call_order' => array( 'action', 'action2' ), 257 282 ), 258 283 ); 259 260 $this->assertSame( $expected, $a->get_events() ); 284 } 285 286 /** 287 * Unhappy path data provider. 288 * 289 * @return array[] 290 */ 291 public function data_priority_callback_order_with_unhappy_path_nonintegers() { 292 return array( 293 // Numbers as strings and floats. 294 'int as string DESC' => array( 295 'priorities' => array( '10', '9' ), 296 'expected_call_order' => array( 'action2', 'action' ), 297 ), 298 'int as string ASC' => array( 299 'priorities' => array( '9', '10' ), 300 'expected_call_order' => array( 'action', 'action2' ), 301 ), 302 'float DESC' => array( 303 'priorities' => array( 10.0, 9.5 ), 304 'expected_call_order' => array( 'action2', 'action' ), 305 'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision', 306 ), 307 'float ASC' => array( 308 'priorities' => array( 9.5, 10.0 ), 309 'expected_call_order' => array( 'action', 'action2' ), 310 'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision', 311 ), 312 'float as string DESC' => array( 313 'priorities' => array( '10.0', '9.5' ), 314 'expected_call_order' => array( 'action2', 'action' ), 315 ), 316 'float as string ASC' => array( 317 'priorities' => array( '9.5', '10.0' ), 318 'expected_call_order' => array( 'action', 'action2' ), 319 ), 320 321 // Non-numeric. 322 'null' => array( 323 'priorities' => array( null, null ), 324 'expected_call_order' => array( 'action', 'action2' ), 325 ), 326 'bool DESC' => array( 327 'priorities' => array( true, false ), 328 'expected_call_order' => array( 'action2', 'action' ), 329 ), 330 'bool ASC' => array( 331 'priorities' => array( false, true ), 332 'expected_call_order' => array( 'action', 'action2' ), 333 ), 334 'non-numerical string DESC' => array( 335 'priorities' => array( 'test1', 'test2' ), 336 'expected_call_order' => array( 'action', 'action2' ), 337 ), 338 'non-numerical string ASC' => array( 339 'priorities' => array( 'test1', 'test2' ), 340 'expected_call_order' => array( 'action', 'action2' ), 341 ), 342 'int, non-numerical string DESC' => array( 343 'priorities' => array( 10, 'test' ), 344 'expected_call_order' => array( 'action2', 'action' ), 345 ), 346 'int, non-numerical string ASC' => array( 347 'priorities' => array( 'test', 10 ), 348 'expected_call_order' => array( 'action', 'action2' ), 349 ), 350 'float, non-numerical string DESC' => array( 351 'priorities' => array( 10.0, 'test' ), 352 'expected_call_order' => array( 'action2', 'action' ), 353 ), 354 'float, non-numerical string ASC' => array( 355 'priorities' => array( 'test', 10.0 ), 356 'expected_call_order' => array( 'action', 'action2' ), 357 ), 358 ); 261 359 } 262 360
Note: See TracChangeset
for help on using the changeset viewer.