<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[57257] trunk/tests/phpunit/tests: Tests: Add hook priority call order tests.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/57257">57257</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/57257","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>hellofromTonya</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2024-01-09 16:32:14 +0000 (Tue, 09 Jan 2024)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Tests: Add hook priority call order tests.

Adds happy (integer) and unhappy (non-integer) tests for validating the priority call order for:

* `do_action()`
* `WP_Hook::do_action()`
* `apply_filters()`
* `WP_Hook::apply_filters()`

As each of these functions have differing code, the tests are added to each to ensure expected results and protect against future regressions.

Follow-up to <a href="https://core.trac.wordpress.org/changeset/53804">[53804]</a>, <a href="https://core.trac.wordpress.org/changeset/52010">[52010]</a>, <a href="https://core.trac.wordpress.org/changeset/25002">[25002]</a>, [25/tests], [62/tests].

Props hellofromTonya, mukesh27, dd32, valendesigns, drrobotnik.
Fixes <a href="https://core.trac.wordpress.org/ticket/60193">#60193</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsactionsphp">trunk/tests/phpunit/tests/actions.php</a></li>
<li><a href="#trunktestsphpunittestsfiltersphp">trunk/tests/phpunit/tests/filters.php</a></li>
<li><a href="#trunktestsphpunittestshooksapplyFiltersphp">trunk/tests/phpunit/tests/hooks/applyFilters.php</a></li>
<li><a href="#trunktestsphpunittestshooksdoActionphp">trunk/tests/phpunit/tests/hooks/doAction.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunktestsphpunittestsactionsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/actions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/actions.php     2024-01-09 11:39:16 UTC (rev 57256)
+++ trunk/tests/phpunit/tests/actions.php       2024-01-09 16:32:14 UTC (rev 57257)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -229,35 +229,133 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( array( $val ), array_pop( $argsvar ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function test_action_priority() {
-               $a         = new MockAction();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+        * @ticket 60193
+        *
+        * @dataProvider data_priority_callback_order_with_integers
+        * @dataProvider data_priority_callback_order_with_unhappy_path_nonintegers
+        *
+        * @covers ::do_action
+        *
+        * @param array $priorities {
+        *     Indexed array of the priorities for the MockAction callbacks.
+        *
+        *     @type mixed $0 Priority for 'action' callback.
+        *     @type mixed $1 Priority for 'action2' callback.
+        * }
+        * @param array  $expected_call_order  An array of callback names in expected call order.
+        * @param string $expected_deprecation Optional. Deprecation message. Default ''.
+        */
+       public function test_priority_callback_order( $priorities, $expected_call_order, $expected_deprecation = '' ) {
+               $mock      = new MockAction();
</ins><span class="cx" style="display: block; padding: 0 10px">                 $hook_name = __FUNCTION__;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                add_action( $hook_name, array( &$a, 'action' ), 10 );
-               add_action( $hook_name, array( &$a, 'action2' ), 9 );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( $expected_deprecation && PHP_VERSION_ID >= 80100 ) {
+                       $this->expectDeprecation();
+                       $this->expectDeprecationMessage( $expected_deprecation );
+               }
+
+               add_action( $hook_name, array( $mock, 'action' ), $priorities[0] );
+               add_action( $hook_name, array( $mock, 'action2' ), $priorities[1] );
</ins><span class="cx" style="display: block; padding: 0 10px">                 do_action( $hook_name );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Two events, one per action.
-               $this->assertSame( 2, $a->get_call_count() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame( 2, $mock->get_call_count(), 'The number of call counts does not match' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $expected = array(
-                       // 'action2' is called first because it has priority 9.
-                       array(
-                               'action'    => 'action2',
-                               'hook_name' => $hook_name,
-                               'tag'       => $hook_name, // Back compat.
-                               'args'      => array( '' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $actual_call_order = wp_list_pluck( $mock->get_events(), 'action' );
+               $this->assertSame( $expected_call_order, $actual_call_order, 'The action callback order does not match the expected order' );
+       }
+
+       /**
+        * Happy path data provider.
+        *
+        * @return array[]
+        */
+       public function data_priority_callback_order_with_integers() {
+               return array(
+                       'int DESC' => array(
+                               'priorities'          => array( 10, 9 ),
+                               'expected_call_order' => array( 'action2', 'action' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // 'action' is called second.
-                       array(
-                               'action'    => 'action',
-                               'hook_name' => $hook_name,
-                               'tag'       => $hook_name, // Back compat.
-                               'args'      => array( '' ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'int ASC'  => array(
+                               'priorities'          => array( 9, 10 ),
+                               'expected_call_order' => array( 'action', 'action2' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         ),
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertSame( $expected, $a->get_events() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+        * Unhappy path data provider.
+        *
+        * @return array[]
+        */
+       public function data_priority_callback_order_with_unhappy_path_nonintegers() {
+               return array(
+                       // Numbers as strings and floats.
+                       'int as string DESC'               => array(
+                               'priorities'          => array( '10', '9' ),
+                               'expected_call_order' => array( 'action2', 'action' ),
+                       ),
+                       'int as string ASC'                => array(
+                               'priorities'          => array( '9', '10' ),
+                               'expected_call_order' => array( 'action', 'action2' ),
+                       ),
+                       'float DESC'                       => array(
+                               'priorities'           => array( 10.0, 9.5 ),
+                               'expected_call_order'  => array( 'action2', 'action' ),
+                               'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision',
+                       ),
+                       'float ASC'                        => array(
+                               'priorities'           => array( 9.5, 10.0 ),
+                               'expected_call_order'  => array( 'action', 'action2' ),
+                               'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision',
+                       ),
+                       'float as string DESC'             => array(
+                               'priorities'          => array( '10.0', '9.5' ),
+                               'expected_call_order' => array( 'action2', 'action' ),
+                       ),
+                       'float as string ASC'              => array(
+                               'priorities'          => array( '9.5', '10.0' ),
+                               'expected_call_order' => array( 'action', 'action2' ),
+                       ),
+
+                       // Non-numeric.
+                       'null'                             => array(
+                               'priorities'          => array( null, null ),
+                               'expected_call_order' => array( 'action', 'action2' ),
+                       ),
+                       'bool DESC'                        => array(
+                               'priorities'          => array( true, false ),
+                               'expected_call_order' => array( 'action2', 'action' ),
+                       ),
+                       'bool ASC'                         => array(
+                               'priorities'          => array( false, true ),
+                               'expected_call_order' => array( 'action', 'action2' ),
+                       ),
+                       'non-numerical string DESC'        => array(
+                               'priorities'          => array( 'test1', 'test2' ),
+                               'expected_call_order' => array( 'action', 'action2' ),
+                       ),
+                       'non-numerical string ASC'         => array(
+                               'priorities'          => array( 'test1', 'test2' ),
+                               'expected_call_order' => array( 'action', 'action2' ),
+                       ),
+                       'int, non-numerical string DESC'   => array(
+                               'priorities'          => array( 10, 'test' ),
+                               'expected_call_order' => array( 'action2', 'action' ),
+                       ),
+                       'int, non-numerical string ASC'    => array(
+                               'priorities'          => array( 'test', 10 ),
+                               'expected_call_order' => array( 'action', 'action2' ),
+                       ),
+                       'float, non-numerical string DESC' => array(
+                               'priorities'          => array( 10.0, 'test' ),
+                               'expected_call_order' => array( 'action2', 'action' ),
+                       ),
+                       'float, non-numerical string ASC'  => array(
+                               'priorities'          => array( 'test', 10.0 ),
+                               'expected_call_order' => array( 'action', 'action2' ),
+                       ),
+               );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span></span></pre></div>
<a id="trunktestsphpunittestsfiltersphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/filters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/filters.php     2024-01-09 11:39:16 UTC (rev 57256)
+++ trunk/tests/phpunit/tests/filters.php       2024-01-09 16:32:14 UTC (rev 57257)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -118,37 +118,133 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( array( $val ), array_pop( $argsvar2 ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function test_filter_priority() {
-               $a         = new MockAction();
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+        * @ticket 60193
+        *
+        * @dataProvider data_priority_callback_order_with_integers
+        * @dataProvider data_priority_callback_order_with_unhappy_path_nonintegers
+        *
+        * @covers ::apply_filters
+        *
+        * @param array $priorities {
+        *     Indexed array of the priorities for the MockAction callbacks.
+        *
+        *     @type mixed $0 Priority for 'action' callback.
+        *     @type mixed $1 Priority for 'action2' callback.
+        * }
+        * @param array  $expected_call_order  An array of callback names in expected call order.
+        * @param string $expected_deprecation Optional. Deprecation message. Default ''.
+        */
+       public function test_priority_callback_order( $priorities, $expected_call_order, $expected_deprecation = '' ) {
+               $mock      = new MockAction();
</ins><span class="cx" style="display: block; padding: 0 10px">                 $hook_name = __FUNCTION__;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $val       = __FUNCTION__ . '_val';
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Make two filters with different priorities.
-               add_filter( $hook_name, array( $a, 'filter' ), 10 );
-               add_filter( $hook_name, array( $a, 'filter2' ), 9 );
-               $this->assertSame( $val, apply_filters( $hook_name, $val ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( $expected_deprecation && PHP_VERSION_ID >= 80100 ) {
+                       $this->expectDeprecation();
+                       $this->expectDeprecationMessage( $expected_deprecation );
+               }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // There should be two events, one per filter.
-               $this->assertSame( 2, $a->get_call_count() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         add_filter( $hook_name, array( $mock, 'filter' ), $priorities[0] );
+               add_filter( $hook_name, array( $mock, 'filter2' ), $priorities[1] );
+               apply_filters( $hook_name, __FUNCTION__ . '_val' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $expected = array(
-                       // 'filter2' is called first because it has priority 9.
-                       array(
-                               'filter'    => 'filter2',
-                               'hook_name' => $hook_name,
-                               'tag'       => $hook_name, // Back compat.
-                               'args'      => array( $val ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         $this->assertSame( 2, $mock->get_call_count(), 'The number of call counts does not match' );
+
+               $actual_call_order = wp_list_pluck( $mock->get_events(), 'filter' );
+               $this->assertSame( $expected_call_order, $actual_call_order, 'The filter callback order does not match the expected order' );
+       }
+
+       /**
+        * Happy path data provider.
+        *
+        * @return array[]
+        */
+       public function data_priority_callback_order_with_integers() {
+               return array(
+                       'int DESC' => array(
+                               'priorities'          => array( 10, 9 ),
+                               'expected_call_order' => array( 'filter2', 'filter' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         ),
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // 'filter' is called second.
-                       array(
-                               'filter'    => 'filter',
-                               'hook_name' => $hook_name,
-                               'tag'       => $hook_name, // Back compat.
-                               'args'      => array( $val ),
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 'int ASC'  => array(
+                               'priorities'          => array( 9, 10 ),
+                               'expected_call_order' => array( 'filter', 'filter2' ),
</ins><span class="cx" style="display: block; padding: 0 10px">                         ),
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        }
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                $this->assertSame( $expected, $a->get_events() );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /**
+        * Unhappy path data provider.
+        *
+        * @return array[]
+        */
+       public function data_priority_callback_order_with_unhappy_path_nonintegers() {
+               return array(
+                       // Numbers as strings and floats.
+                       'int as string DESC'               => array(
+                               'priorities'          => array( '10', '9' ),
+                               'expected_call_order' => array( 'filter2', 'filter' ),
+                       ),
+                       'int as string ASC'                => array(
+                               'priorities'          => array( '9', '10' ),
+                               'expected_call_order' => array( 'filter', 'filter2' ),
+                       ),
+                       'float DESC'                       => array(
+                               'priorities'           => array( 10.0, 9.5 ),
+                               'expected_call_order'  => array( 'filter2', 'filter' ),
+                               'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision',
+                       ),
+                       'float ASC'                        => array(
+                               'priorities'           => array( 9.5, 10.0 ),
+                               'expected_call_order'  => array( 'filter', 'filter2' ),
+                               'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision',
+                       ),
+                       'float as string DESC'             => array(
+                               'priorities'          => array( '10.0', '9.5' ),
+                               'expected_call_order' => array( 'filter2', 'filter' ),
+                       ),
+                       'float as string ASC'              => array(
+                               'priorities'          => array( '9.5', '10.0' ),
+                               'expected_call_order' => array( 'filter', 'filter2' ),
+                       ),
+
+                       // Non-numeric.
+                       'null'                             => array(
+                               'priorities'          => array( null, null ),
+                               'expected_call_order' => array( 'filter', 'filter2' ),
+                       ),
+                       'bool DESC'                        => array(
+                               'priorities'          => array( true, false ),
+                               'expected_call_order' => array( 'filter2', 'filter' ),
+                       ),
+                       'bool ASC'                         => array(
+                               'priorities'          => array( false, true ),
+                               'expected_call_order' => array( 'filter', 'filter2' ),
+                       ),
+                       'non-numerical string DESC'        => array(
+                               'priorities'          => array( 'test1', 'test2' ),
+                               'expected_call_order' => array( 'filter', 'filter2' ),
+                       ),
+                       'non-numerical string ASC'         => array(
+                               'priorities'          => array( 'test1', 'test2' ),
+                               'expected_call_order' => array( 'filter', 'filter2' ),
+                       ),
+                       'int, non-numerical string DESC'   => array(
+                               'priorities'          => array( 10, 'test' ),
+                               'expected_call_order' => array( 'filter2', 'filter' ),
+                       ),
+                       'int, non-numerical string ASC'    => array(
+                               'priorities'          => array( 'test', 10 ),
+                               'expected_call_order' => array( 'filter', 'filter2' ),
+                       ),
+                       'float, non-numerical string DESC' => array(
+                               'priorities'          => array( 10.0, 'test' ),
+                               'expected_call_order' => array( 'filter2', 'filter' ),
+                       ),
+                       'float, non-numerical string ASC'  => array(
+                               'priorities'          => array( 'test', 10.0 ),
+                               'expected_call_order' => array( 'filter', 'filter2' ),
+                       ),
+               );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span></span></pre></div>
<a id="trunktestsphpunittestshooksapplyFiltersphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/hooks/applyFilters.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/hooks/applyFilters.php  2024-01-09 11:39:16 UTC (rev 57256)
+++ trunk/tests/phpunit/tests/hooks/applyFilters.php    2024-01-09 16:32:14 UTC (rev 57257)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -42,4 +42,132 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( $returned_two, $arg );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( 2, $a->get_call_count() );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket 60193
+        *
+        * @dataProvider data_priority_callback_order_with_integers
+        * @dataProvider data_priority_callback_order_with_unhappy_path_nonintegers
+        *
+        * @param array $priorities {
+        *     Indexed array of the priorities for the MockAction callbacks.
+        *
+        *     @type mixed $0 Priority for 'action' callback.
+        *     @type mixed $1 Priority for 'action2' callback.
+        * }
+        * @param array  $expected_call_order  An array of callback names in expected call order.
+        * @param string $expected_deprecation Optional. Deprecation message. Default ''.
+        */
+       public function test_priority_callback_order( $priorities, $expected_call_order, $expected_deprecation = '' ) {
+               $mock      = new MockAction();
+               $hook      = new WP_Hook();
+               $hook_name = __FUNCTION__;
+
+               if ( $expected_deprecation && PHP_VERSION_ID >= 80100 ) {
+                       $this->expectDeprecation();
+                       $this->expectDeprecationMessage( $expected_deprecation );
+               }
+
+               $hook->add_filter( $hook_name, array( $mock, 'filter' ), $priorities[0], 1 );
+               $hook->add_filter( $hook_name, array( $mock, 'filter2' ), $priorities[1], 1 );
+               $hook->apply_filters( __FUNCTION__ . '_val', array( '' ) );
+
+               $this->assertSame( 2, $mock->get_call_count(), 'The number of call counts does not match' );
+
+               $actual_call_order = wp_list_pluck( $mock->get_events(), 'filter' );
+               $this->assertSame( $expected_call_order, $actual_call_order, 'The filter callback order does not match the expected order' );
+       }
+
+       /**
+        * Happy path data provider.
+        *
+        * @return array[]
+        */
+       public function data_priority_callback_order_with_integers() {
+               return array(
+                       'int DESC' => array(
+                               'priorities'          => array( 10, 9 ),
+                               'expected_call_order' => array( 'filter2', 'filter' ),
+                       ),
+                       'int ASC'  => array(
+                               'priorities'          => array( 9, 10 ),
+                               'expected_call_order' => array( 'filter', 'filter2' ),
+                       ),
+               );
+       }
+
+       /**
+        * Unhappy path data provider.
+        *
+        * @return array[]
+        */
+       public function data_priority_callback_order_with_unhappy_path_nonintegers() {
+               return array(
+                       // Numbers as strings and floats.
+                       'int as string DESC'               => array(
+                               'priorities'          => array( '10', '9' ),
+                               'expected_call_order' => array( 'filter2', 'filter' ),
+                       ),
+                       'int as string ASC'                => array(
+                               'priorities'          => array( '9', '10' ),
+                               'expected_call_order' => array( 'filter', 'filter2' ),
+                       ),
+                       'float DESC'                       => array(
+                               'priorities'           => array( 10.0, 9.5 ),
+                               'expected_call_order'  => array( 'filter2', 'filter' ),
+                               'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision',
+                       ),
+                       'float ASC'                        => array(
+                               'priorities'           => array( 9.5, 10.0 ),
+                               'expected_call_order'  => array( 'filter', 'filter2' ),
+                               'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision',
+                       ),
+                       'float as string DESC'             => array(
+                               'priorities'          => array( '10.0', '9.5' ),
+                               'expected_call_order' => array( 'filter2', 'filter' ),
+                       ),
+                       'float as string ASC'              => array(
+                               'priorities'          => array( '9.5', '10.0' ),
+                               'expected_call_order' => array( 'filter', 'filter2' ),
+                       ),
+
+                       // Non-numeric.
+                       'null'                             => array(
+                               'priorities'          => array( null, null ),
+                               'expected_call_order' => array( 'filter', 'filter2' ),
+                       ),
+                       'bool DESC'                        => array(
+                               'priorities'          => array( true, false ),
+                               'expected_call_order' => array( 'filter2', 'filter' ),
+                       ),
+                       'bool ASC'                         => array(
+                               'priorities'          => array( false, true ),
+                               'expected_call_order' => array( 'filter', 'filter2' ),
+                       ),
+                       'non-numerical string DESC'        => array(
+                               'priorities'          => array( 'test1', 'test2' ),
+                               'expected_call_order' => array( 'filter', 'filter2' ),
+                       ),
+                       'non-numerical string ASC'         => array(
+                               'priorities'          => array( 'test1', 'test2' ),
+                               'expected_call_order' => array( 'filter', 'filter2' ),
+                       ),
+                       'int, non-numerical string DESC'   => array(
+                               'priorities'          => array( 10, 'test' ),
+                               'expected_call_order' => array( 'filter2', 'filter' ),
+                       ),
+                       'int, non-numerical string ASC'    => array(
+                               'priorities'          => array( 'test', 10 ),
+                               'expected_call_order' => array( 'filter', 'filter2' ),
+                       ),
+                       'float, non-numerical string DESC' => array(
+                               'priorities'          => array( 10.0, 'test' ),
+                               'expected_call_order' => array( 'filter2', 'filter' ),
+                       ),
+                       'float, non-numerical string ASC'  => array(
+                               'priorities'          => array( 'test', 10.0 ),
+                               'expected_call_order' => array( 'filter', 'filter2' ),
+                       ),
+               );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunktestsphpunittestshooksdoActionphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/hooks/doAction.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/hooks/doAction.php      2024-01-09 11:39:16 UTC (rev 57256)
+++ trunk/tests/phpunit/tests/hooks/doAction.php        2024-01-09 16:32:14 UTC (rev 57257)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -85,6 +85,134 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( 1, $a->get_call_count() );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        /**
+        * @ticket 60193
+        *
+        * @dataProvider data_priority_callback_order_with_integers
+        * @dataProvider data_priority_callback_order_with_unhappy_path_nonintegers
+        *
+        * @param array $priorities {
+        *     Indexed array of the priorities for the MockAction callbacks.
+        *
+        *     @type mixed $0 Priority for 'action' callback.
+        *     @type mixed $1 Priority for 'action2' callback.
+        * }
+        * @param array  $expected_call_order  An array of callback names in expected call order.
+        * @param string $expected_deprecation Optional. Deprecation message. Default ''.
+        */
+       public function test_priority_callback_order( $priorities, $expected_call_order, $expected_deprecation = '' ) {
+               $mock      = new MockAction();
+               $hook      = new WP_Hook();
+               $hook_name = __FUNCTION__;
+
+               if ( $expected_deprecation && PHP_VERSION_ID >= 80100 ) {
+                       $this->expectDeprecation();
+                       $this->expectDeprecationMessage( $expected_deprecation );
+               }
+
+               $hook->add_filter( $hook_name, array( $mock, 'action' ), $priorities[0], 1 );
+               $hook->add_filter( $hook_name, array( $mock, 'action2' ), $priorities[1], 1 );
+               $hook->do_action( array( '' ) );
+
+               $this->assertSame( 2, $mock->get_call_count(), 'The number of call counts does not match' );
+
+               $actual_call_order = wp_list_pluck( $mock->get_events(), 'action' );
+               $this->assertSame( $expected_call_order, $actual_call_order, 'The action callback order does not match the expected order' );
+       }
+
+       /**
+        * Happy path data provider.
+        *
+        * @return array[]
+        */
+       public function data_priority_callback_order_with_integers() {
+               return array(
+                       'int DESC' => array(
+                               'priorities'          => array( 10, 9 ),
+                               'expected_call_order' => array( 'action2', 'action' ),
+                       ),
+                       'int ASC'  => array(
+                               'priorities'          => array( 9, 10 ),
+                               'expected_call_order' => array( 'action', 'action2' ),
+                       ),
+               );
+       }
+
+       /**
+        * Unhappy path data provider.
+        *
+        * @return array[]
+        */
+       public function data_priority_callback_order_with_unhappy_path_nonintegers() {
+               return array(
+                       // Numbers as strings and floats.
+                       'int as string DESC'               => array(
+                               'priorities'          => array( '10', '9' ),
+                               'expected_call_order' => array( 'action2', 'action' ),
+                       ),
+                       'int as string ASC'                => array(
+                               'priorities'          => array( '9', '10' ),
+                               'expected_call_order' => array( 'action', 'action2' ),
+                       ),
+                       'float DESC'                       => array(
+                               'priorities'           => array( 10.0, 9.5 ),
+                               'expected_call_order'  => array( 'action2', 'action' ),
+                               'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision',
+                       ),
+                       'float ASC'                        => array(
+                               'priorities'           => array( 9.5, 10.0 ),
+                               'expected_call_order'  => array( 'action', 'action2' ),
+                               'expected_deprecation' => 'Implicit conversion from float 9.5 to int loses precision',
+                       ),
+                       'float as string DESC'             => array(
+                               'priorities'          => array( '10.0', '9.5' ),
+                               'expected_call_order' => array( 'action2', 'action' ),
+                       ),
+                       'float as string ASC'              => array(
+                               'priorities'          => array( '9.5', '10.0' ),
+                               'expected_call_order' => array( 'action', 'action2' ),
+                       ),
+
+                       // Non-numeric.
+                       'null'                             => array(
+                               'priorities'          => array( null, null ),
+                               'expected_call_order' => array( 'action', 'action2' ),
+                       ),
+                       'bool DESC'                        => array(
+                               'priorities'          => array( true, false ),
+                               'expected_call_order' => array( 'action2', 'action' ),
+                       ),
+                       'bool ASC'                         => array(
+                               'priorities'          => array( false, true ),
+                               'expected_call_order' => array( 'action', 'action2' ),
+                       ),
+                       'non-numerical string DESC'        => array(
+                               'priorities'          => array( 'test1', 'test2' ),
+                               'expected_call_order' => array( 'action', 'action2' ),
+                       ),
+                       'non-numerical string ASC'         => array(
+                               'priorities'          => array( 'test1', 'test2' ),
+                               'expected_call_order' => array( 'action', 'action2' ),
+                       ),
+                       'int, non-numerical string DESC'   => array(
+                               'priorities'          => array( 10, 'test' ),
+                               'expected_call_order' => array( 'action2', 'action' ),
+                       ),
+                       'int, non-numerical string ASC'    => array(
+                               'priorities'          => array( 'test', 10 ),
+                               'expected_call_order' => array( 'action', 'action2' ),
+                       ),
+                       'float, non-numerical string DESC' => array(
+                               'priorities'          => array( 10.0, 'test' ),
+                               'expected_call_order' => array( 'action2', 'action' ),
+                       ),
+                       'float, non-numerical string ASC'  => array(
+                               'priorities'          => array( 'test', 10.0 ),
+                               'expected_call_order' => array( 'action', 'action2' ),
+                       ),
+               );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         public function test_do_action_with_no_accepted_args() {
</span><span class="cx" style="display: block; padding: 0 10px">                $callback      = array( $this, '_action_callback' );
</span><span class="cx" style="display: block; padding: 0 10px">                $hook          = new WP_Hook();
</span></span></pre>
</div>
</div>

</body>
</html>