<!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>[54421] trunk/tests/phpunit/tests/functions/wpListUtil.php: Build/Test Tools: Add tests for `WP_List_Util::pluck` and `WP_List_Util::sort`.</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/54421">54421</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/54421","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>audrasjb</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2022-10-08 08:44:45 +0000 (Sat, 08 Oct 2022)</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'>Build/Test Tools: Add tests for `WP_List_Util::pluck` and `WP_List_Util::sort`.
This changeset adds additional unit tests for these two functions.
Props pbearne, costdev, afragen.
Fixes <a href="https://core.trac.wordpress.org/ticket/55300">#55300</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsfunctionswpListUtilphp">trunk/tests/phpunit/tests/functions/wpListUtil.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunktestsphpunittestsfunctionswpListUtilphp"></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/functions/wpListUtil.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/functions/wpListUtil.php 2022-10-07 22:17:06 UTC (rev 54420)
+++ trunk/tests/phpunit/tests/functions/wpListUtil.php 2022-10-08 08:44:45 UTC (rev 54421)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -52,4 +52,1092 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEqualSets( $expected, $actual );
</span><span class="cx" style="display: block; padding: 0 10px"> $this->assertEqualSets( $expected, $util->get_output() );
</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 55300
+ *
+ * @dataProvider data_wp_list_util_pluck
+ *
+ * @covers WP_List_Util::pluck
+ * @covers ::wp_list_pluck
+ *
+ * @param array $target_array The array to create the list from.
+ * @param string $target_key The key to pluck.
+ * @param array $expected The expected array.
+ * @param string $index_key Optional. Field from the element to use as keys for the new array. Default null.
+ */
+ public function test_wp_list_util_pluck( $target_array, $target_key, $expected, $index_key = null ) {
+ $util = new WP_List_Util( $target_array );
+ $actual = $util->pluck( $target_key, $index_key );
+
+ $this->assertEqualSetsWithIndex(
+ $expected,
+ $actual,
+ 'The plucked value did not match the expected value.'
+ );
+
+ $this->assertEqualSetsWithIndex(
+ $expected,
+ $util->get_output(),
+ '::get_output() did not return the expected value.'
+ );
+ }
+
+ /**
+ * Data provider for test_wp_list_util_pluck_simple().
+ *
+ * @return array[]
+ */
+ public function data_wp_list_util_pluck() {
+ return array(
+ 'simple' => array(
+ 'target_array' => array(
+ 0 => array( 'foo' => 'bar' ),
+ ),
+ 'target_key' => 'foo',
+ 'expected' => array( 'bar' ),
+ ),
+ 'simple_object' => array(
+ 'target_array' => array(
+ 0 => (object) array( 'foo' => 'bar' ),
+ ),
+ 'target_key' => 'foo',
+ 'expected' => array( 'bar' ),
+ ),
+ );
+ }
+
+ /**
+ * @ticket 55300
+ *
+ * @covers WP_List_Util::sort
+ * @covers ::wp_list_sort
+ */
+ public function test_wp_list_util_sort_simple() {
+ $expected = array(
+ 1 => 'one',
+ 2 => 'two',
+ 3 => 'three',
+ 4 => 'four',
+ );
+ $target_array = array(
+ 4 => 'four',
+ 2 => 'two',
+ 3 => 'three',
+ 1 => 'one',
+ );
+
+ $util = new WP_List_Util( $target_array );
+ $actual = $util->sort();
+
+ $this->assertEqualSets(
+ $expected,
+ $actual,
+ 'The sorted value did not match the expected value.'
+ );
+
+ $this->assertEqualSets(
+ $expected,
+ $util->get_output(),
+ '::get_output() did not return the expected value.'
+ );
+ }
+
+ /**
+ * @ticket 55300
+ *
+ * @dataProvider data_wp_list_util_sort_string_arrays
+ * @dataProvider data_wp_list_util_sort_int_arrays
+ * @dataProvider data_wp_list_util_sort_arrays_of_arrays
+ * @dataProvider data_wp_list_util_sort_object_arrays
+ *
+ * @covers WP_List_Util::sort
+ * @covers ::wp_list_sort
+ *
+ * @param array $expected The expected array.
+ * @param array $target_array The array to create a list from.
+ * @param array $orderby Optional. Either the field name to order by or an array of multiple orderby fields as $orderby => $order.
+ * Default empty array.
+ * @param string $order Optional. Either 'ASC' or 'DESC'. Only used if $orderby is a string. Default 'ASC'.
+ * @param bool $preserve_keys Optional. Whether to preserve keys. Default false.
+ */
+ public function test_wp_list_util_sort( $expected, $target_array, $orderby = array(), $order = 'ASC', $preserve_keys = false ) {
+ $util = new WP_List_Util( $target_array );
+ $actual = $util->sort( $orderby, $order, $preserve_keys );
+
+ $this->assertEqualSetsWithIndex(
+ $expected,
+ $actual,
+ 'The sorted value did not match the expected value.'
+ );
+
+ $this->assertEqualSetsWithIndex(
+ $expected,
+ $util->get_output(),
+ '::get_output() did not return the expected value.'
+ );
+ }
+
+ /**
+ * Data provider that provides string arrays to test_wp_list_util_sort().
+ *
+ * @return array[]
+ */
+ public function data_wp_list_util_sort_string_arrays() {
+ return array(
+ 'string[], no keys, no ordering' => array(
+ 'expected' => array( 'four', 'two', 'three', 'one' ),
+ 'target_array' => array( 'four', 'two', 'three', 'one' ),
+ ),
+ 'string[], int keys, no ordering' => array(
+ 'expected' => array(
+ 4 => 'four',
+ 2 => 'two',
+ 3 => 'three',
+ 1 => 'one',
+ ),
+ 'target_array' => array(
+ 4 => 'four',
+ 2 => 'two',
+ 3 => 'three',
+ 1 => 'one',
+ ),
+ ),
+ 'string[], int keys, $orderby a non-existent field, $order = DESC and $preserve_keys = true' => array(
+ 'expected' => array(
+ 4 => 'four',
+ 2 => 'two',
+ 3 => 'three',
+ 1 => 'one',
+ ),
+ 'target_array' => array(
+ 4 => 'four',
+ 2 => 'two',
+ 3 => 'three',
+ 1 => 'one',
+ ),
+ 'orderby' => 'id',
+ 'order' => 'DESC',
+ 'preserve_keys' => true,
+ ),
+ 'string[], string keys, no ordering' => array(
+ 'expected' => array(
+ 'four' => 'four',
+ 'two' => 'two',
+ 'three' => 'three',
+ 'one' => 'one',
+ ),
+ 'target_array' => array(
+ 'four' => 'four',
+ 'two' => 'two',
+ 'three' => 'three',
+ 'one' => 'one',
+ ),
+ ),
+ 'string[], string keys, $orderby a non-existent field, $order = DESC and $preserve_keys = true' => array(
+ 'expected' => array(
+ 'four' => 'four',
+ 'two' => 'two',
+ 'three' => 'three',
+ 'one' => 'one',
+ ),
+ 'target_array' => array(
+ 'four' => 'four',
+ 'two' => 'two',
+ 'three' => 'three',
+ 'one' => 'one',
+ ),
+ 'orderby' => 'id',
+ 'order' => 'DESC',
+ 'preserve_keys' => true,
+ ),
+ );
+ }
+
+ /**
+ * Data provider that provides int arrays for test_wp_list_util_sort().
+ *
+ * @return array[]
+ */
+ public function data_wp_list_util_sort_int_arrays() {
+ return array(
+ 'int[], no keys, no ordering' => array(
+ 'expected' => array( 4, 2, 3, 1 ),
+ 'target_array' => array( 4, 2, 3, 1 ),
+ ),
+ 'int[], int keys, no ordering' => array(
+ 'expected' => array(
+ 4 => 4,
+ 2 => 2,
+ 3 => 3,
+ 1 => 1,
+ ),
+ 'target_array' => array(
+ 4 => 4,
+ 2 => 2,
+ 3 => 3,
+ 1 => 1,
+ ),
+ ),
+ 'int[], int keys, $orderby a non-existent field, $order = DESC and $preserve_keys = true' => array(
+ 'expected' => array(
+ 4 => 4,
+ 2 => 2,
+ 3 => 3,
+ 1 => 1,
+ ),
+ 'target_array' => array(
+ 4 => 4,
+ 2 => 2,
+ 3 => 3,
+ 1 => 1,
+ ),
+ 'orderby' => 'id',
+ 'order' => 'DESC',
+ 'preserve_keys' => true,
+ ),
+ 'int[], string keys, no ordering' => array(
+ 'expected' => array(
+ 'four' => 4,
+ 'two' => 2,
+ 'three' => 3,
+ 'one' => 1,
+ ),
+ 'target_array' => array(
+ 'four' => 4,
+ 'two' => 2,
+ 'three' => 3,
+ 'one' => 1,
+ ),
+ ),
+ 'int[], string keys, $orderby a non-existent field, $order = DESC and $preserve_keys = true' => array(
+ 'expected' => array(
+ 'four' => 4,
+ 'two' => 2,
+ 'three' => 3,
+ 'one' => 1,
+ ),
+ 'target_array' => array(
+ 'four' => 4,
+ 'two' => 2,
+ 'three' => 3,
+ 'one' => 1,
+ ),
+ 'orderby' => 'id',
+ 'order' => 'DESC',
+ 'preserve_keys' => true,
+ ),
+ );
+ }
+
+ /**
+ * Data provider that provides arrays of arrays for test_wp_list_util_sort().
+ *
+ * @return array[]
+ */
+ public function data_wp_list_util_sort_arrays_of_arrays() {
+ return array(
+ 'array[], no keys, no ordering' => array(
+ 'expected' => array(
+ array( 'four' ),
+ array( 'two' ),
+ array( 'three' ),
+ array( 'one' ),
+ ),
+ 'target_array' => array(
+ array( 'four' ),
+ array( 'two' ),
+ array( 'three' ),
+ array( 'one' ),
+ ),
+ ),
+ 'array[], int keys, no ordering' => array(
+ 'expected' => array(
+ 4 => array( 'four' ),
+ 2 => array( 'two' ),
+ 3 => array( 'three' ),
+ 1 => array( 'one' ),
+ ),
+ 'target_array' => array(
+ 4 => array( 'four' ),
+ 2 => array( 'two' ),
+ 3 => array( 'three' ),
+ 1 => array( 'one' ),
+ ),
+ ),
+ 'array[], int keys, $orderby a non-existent field, $order = DESC and $preserve_keys = true' => array(
+ 'expected' => array(
+ 4 => array( 'value' => 'four' ),
+ 2 => array( 'value' => 'two' ),
+ 3 => array( 'value' => 'three' ),
+ 1 => array( 'value' => 'one' ),
+ ),
+ 'target_array' => array(
+ 4 => array( 'value' => 'four' ),
+ 2 => array( 'value' => 'two' ),
+ 3 => array( 'value' => 'three' ),
+ 1 => array( 'value' => 'one' ),
+ ),
+ 'orderby' => 'id',
+ 'order' => 'DESC',
+ 'preserve_keys' => true,
+ ),
+ 'array[], int keys, $orderby an existing field, $order = ASC and $preserve_keys = false' => array(
+ 'expected' => array(
+ array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ ),
+ 'target_array' => array(
+ 4 => array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ 2 => array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ 3 => array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ 1 => array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ ),
+ 'orderby' => 'id',
+ 'order' => 'ASC',
+ 'preserve_keys' => false,
+ ),
+ 'array[], int keys, $orderby an existing field, $order = DESC and $preserve_keys = true' => array(
+ 'expected' => array(
+ 3 => array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ 2 => array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ 1 => array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ 0 => array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ ),
+ 'target_array' => array(
+ array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ ),
+ 'orderby' => 'id',
+ 'order' => 'DESC',
+ 'preserve_keys' => true,
+ ),
+ 'array[], string keys, no ordering' => array(
+ 'expected' => array(
+ 'four' => array( 'value' => 'four' ),
+ 'two' => array( 'value' => 'two' ),
+ 'three' => array( 'value' => 'three' ),
+ 'one' => array( 'value' => 'one' ),
+ ),
+ 'target_array' => array(
+ 'four' => array( 'value' => 'four' ),
+ 'two' => array( 'value' => 'two' ),
+ 'three' => array( 'value' => 'three' ),
+ 'one' => array( 'value' => 'one' ),
+ ),
+ ),
+ 'array[], string keys, $orderby a non-existent field, $order = DESC and $preserve_keys = true' => array(
+ 'expected' => array(
+ 'four' => array( 'value' => 'four' ),
+ 'two' => array( 'value' => 'two' ),
+ 'three' => array( 'value' => 'three' ),
+ 'one' => array( 'value' => 'one' ),
+ ),
+ 'target_array' => array(
+ 'four' => array( 'value' => 'four' ),
+ 'two' => array( 'value' => 'two' ),
+ 'three' => array( 'value' => 'three' ),
+ 'one' => array( 'value' => 'one' ),
+ ),
+ 'orderby' => 'id',
+ 'order' => 'DESC',
+ 'preserve_keys' => true,
+ ),
+ 'array[], string keys, $orderby an existing field, $order = ASC and $preserve_keys = false' => array(
+ 'expected' => array(
+ array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ ),
+ 'target_array' => array(
+ 'four' => array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ 'two' => array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ 'three' => array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ 'one' => array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ ),
+ 'orderby' => 'id',
+ 'order' => 'ASC',
+ 'preserve_keys' => false,
+ ),
+ 'array[], string keys, $orderby an existing field, $order = DESC and $preserve_keys = true' => array(
+ 'expected' => array(
+ 'four' => array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ 'three' => array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ 'two' => array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ 'one' => array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ ),
+ 'target_array' => array(
+ 'one' => array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ 'two' => array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ 'three' => array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ 'four' => array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ ),
+ 'orderby' => 'id',
+ 'order' => 'DESC',
+ 'preserve_keys' => true,
+ ),
+ 'array[], string keys, $orderby an existing field, $order = asc (lowercase) and $preserve_keys = false' => array(
+ 'expected' => array(
+ array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ ),
+ 'target_array' => array(
+ 'four' => array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ 'two' => array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ 'three' => array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ 'one' => array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ ),
+ 'orderby' => 'id',
+ 'order' => 'asc',
+ 'preserve_keys' => false,
+ ),
+ 'array[], string keys, $orderby an existing field, no order and $preserve_keys = false' => array(
+ 'expected' => array(
+ 'four' => array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ 'three' => array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ 'two' => array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ 'one' => array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ ),
+ 'target_array' => array(
+ 'one' => array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ 'two' => array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ 'three' => array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ 'four' => array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ ),
+ 'orderby' => array( 'id' ),
+ 'order' => null,
+ 'preserve_keys' => true,
+ ),
+ 'array[], string keys, $orderby two existing fields, differing orders and $preserve_keys = false' => array(
+ 'expected' => array(
+ array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ ),
+ 'target_array' => array(
+ 'four' => array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ 'two' => array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ 'three' => array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ 'one' => array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ ),
+ 'orderby' => array(
+ 'id' => 'asc',
+ 'value' => 'DESC',
+ ),
+ 'order' => null,
+ 'preserve_keys' => false,
+ ),
+ );
+ }
+
+ /**
+ * Data provider that provides object arrays for test_wp_list_util_sort().
+ *
+ * @return array[]
+ */
+ public function data_wp_list_util_sort_object_arrays() {
+ return array(
+ 'object[], no keys, no ordering' => array(
+ 'expected' => array(
+ (object) array( 'four' ),
+ (object) array( 'two' ),
+ (object) array( 'three' ),
+ (object) array( 'one' ),
+ ),
+ 'target_array' => array(
+ (object) array( 'four' ),
+ (object) array( 'two' ),
+ (object) array( 'three' ),
+ (object) array( 'one' ),
+ ),
+ ),
+ 'object[], int keys, no ordering' => array(
+ 'expected' => array(
+ 4 => (object) array( 'four' ),
+ 2 => (object) array( 'two' ),
+ 3 => (object) array( 'three' ),
+ 1 => (object) array( 'one' ),
+ ),
+ 'target_array' => array(
+ 4 => (object) array( 'four' ),
+ 2 => (object) array( 'two' ),
+ 3 => (object) array( 'three' ),
+ 1 => (object) array( 'one' ),
+ ),
+ ),
+ 'object[], int keys, $orderby an existing field, $order = ASC and $preserve_keys = false' => array(
+ 'expected' => array(
+ (object) array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ (object) array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ (object) array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ (object) array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ ),
+ 'target_array' => array(
+ 4 => (object) array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ 2 => (object) array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ 3 => (object) array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ 1 => (object) array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ ),
+ 'orderby' => 'id',
+ 'order' => 'ASC',
+ 'preserve_keys' => false,
+ ),
+ 'object[], int keys, $orderby an existing field, $order = DESC and $preserve_keys = true' => array(
+ 'expected' => array(
+ 3 => (object) array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ 2 => (object) array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ 1 => (object) array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ 0 => (object) array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ ),
+ 'target_array' => array(
+ (object) array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ (object) array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ (object) array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ (object) array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ ),
+ 'orderby' => 'id',
+ 'order' => 'DESC',
+ 'preserve_keys' => true,
+ ),
+ 'object[], string keys, no ordering' => array(
+ 'expected' => array(
+ 'four' => (object) array( 'value' => 'four' ),
+ 'two' => (object) array( 'value' => 'two' ),
+ 'three' => (object) array( 'value' => 'three' ),
+ 'one' => (object) array( 'value' => 'one' ),
+ ),
+ 'target_array' => array(
+ 'four' => (object) array( 'value' => 'four' ),
+ 'two' => (object) array( 'value' => 'two' ),
+ 'three' => (object) array( 'value' => 'three' ),
+ 'one' => (object) array( 'value' => 'one' ),
+ ),
+ ),
+ 'object[], string keys, $orderby a non-existent field, $order = DESC and $preserve_keys = true' => array(
+ 'expected' => array(
+ 'four' => (object) array( 'value' => 'four' ),
+ 'two' => (object) array( 'value' => 'two' ),
+ 'three' => (object) array( 'value' => 'three' ),
+ 'one' => (object) array( 'value' => 'one' ),
+ ),
+ 'target_array' => array(
+ 'four' => (object) array( 'value' => 'four' ),
+ 'two' => (object) array( 'value' => 'two' ),
+ 'three' => (object) array( 'value' => 'three' ),
+ 'one' => (object) array( 'value' => 'one' ),
+ ),
+ 'orderby' => 'id',
+ 'order' => 'DESC',
+ 'preserve_keys' => true,
+ ),
+ 'object[], string keys, $orderby an existing field, $order = ASC and $preserve_keys = false' => array(
+ 'expected' => array(
+ (object) array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ (object) array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ (object) array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ (object) array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ ),
+ 'target_array' => array(
+ 'four' => (object) array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ 'two' => (object) array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ 'three' => (object) array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ 'one' => (object) array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ ),
+ 'orderby' => 'id',
+ 'order' => 'ASC',
+ 'preserve_keys' => false,
+ ),
+ 'object[], string keys, $orderby an existing field, $order = DESC and $preserve_keys = true' => array(
+ 'expected' => array(
+ 'four' => (object) array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ 'three' => (object) array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ 'two' => (object) array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ 'one' => (object) array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ ),
+ 'target_array' => array(
+ 'one' => (object) array(
+ 'id' => 1,
+ 'value' => 'one',
+ ),
+ 'two' => (object) array(
+ 'id' => 2,
+ 'value' => 'two',
+ ),
+ 'three' => (object) array(
+ 'id' => 3,
+ 'value' => 'three',
+ ),
+ 'four' => (object) array(
+ 'id' => 4,
+ 'value' => 'four',
+ ),
+ ),
+ 'orderby' => 'id',
+ 'order' => 'DESC',
+ 'preserve_keys' => true,
+ ),
+ );
+ }
+
+ /**
+ * Tests non-existent '$orderby' fields.
+ *
+ * In PHP < 7.0.0, the sorting behaviour is different, which Core does not
+ * currently handle. Until this is fixed, or the minimum PHP version is
+ * raised to PHP 7.0.0+, these tests will be skipped on PHP < 7.0.0.
+ *
+ * @ticket 55300
+ *
+ * @dataProvider data_wp_list_util_sort_php_7_or_greater
+ *
+ * @covers WP_List_Util::sort
+ * @covers ::wp_list_sort
+ *
+ * @param array $expected The expected array.
+ * @param array $target_array The array to create a list from.
+ * @param array $orderby Optional. Either the field name to order by or an array of multiple orderby fields as $orderby => $order.
+ * Default empty array.
+ * @param string $order Optional. Either 'ASC' or 'DESC'. Only used if $orderby is a string. Default 'ASC'.
+ * @param bool $preserve_keys Optional. Whether to preserve keys. Default false.
+ */
+ public function test_wp_list_util_sort_php_7_or_greater( $expected, $target_array, $orderby = array(), $order = 'ASC', $preserve_keys = false ) {
+ if ( version_compare( PHP_VERSION, '7.0.0', '<' ) ) {
+ $this->markTestSkipped( 'This test can only run on PHP 7.0 or greater due to an unstable sort order.' );
+ }
+
+ $util = new WP_List_Util( $target_array );
+ $actual = $util->sort( $orderby, $order, $preserve_keys );
+
+ $this->assertEqualSetsWithIndex(
+ $expected,
+ $actual,
+ 'The sorted value did not match the expected value.'
+ );
+ $this->assertEqualSetsWithIndex(
+ $expected,
+ $util->get_output(),
+ '::get_output() did not return the expected value.'
+ );
+ }
+
+ /**
+ * Data provider for test_wp_list_util_sort_php_7_or_greater().
+ *
+ * @return array[]
+ */
+ public function data_wp_list_util_sort_php_7_or_greater() {
+ return array(
+ 'int[], int keys, $orderby a non-existent field, $order = ASC and $preserve_keys = false' => array(
+ 'expected' => array( 4, 2, 3, 1 ),
+ 'target_array' => array(
+ 4 => 4,
+ 2 => 2,
+ 3 => 3,
+ 1 => 1,
+ ),
+ 'orderby' => 'id',
+ 'order' => 'ASC',
+ 'preserve_keys' => false,
+ ),
+ 'int[], string keys, $orderby a non-existent field, $order = ASC and $preserve_keys = false' => array(
+ 'expected' => array( 4, 2, 3, 1 ),
+ 'target_array' => array(
+ 'four' => 4,
+ 'two' => 2,
+ 'three' => 3,
+ 'one' => 1,
+ ),
+ 'orderby' => 'id',
+ 'order' => 'ASC',
+ 'preserve_keys' => false,
+ ),
+ 'string[], int keys, $orderby a non-existent field, $order = ASC and $preserve_keys = false' => array(
+ 'expected' => array( 'four', 'two', 'three', 'one' ),
+ 'target_array' => array(
+ 4 => 'four',
+ 2 => 'two',
+ 3 => 'three',
+ 1 => 'one',
+ ),
+ 'orderby' => 'id',
+ 'order' => 'ASC',
+ 'preserve_keys' => false,
+ ),
+ 'string[], string keys, $orderby a non-existent field, $order = ASC and $preserve_keys = false' => array(
+ 'expected' => array( 'four', 'two', 'three', 'one' ),
+ 'target_array' => array(
+ 'four' => 'four',
+ 'two' => 'two',
+ 'three' => 'three',
+ 'one' => 'one',
+ ),
+ 'orderby' => 'id',
+ 'order' => 'ASC',
+ 'preserve_keys' => false,
+ ),
+ 'array[], int keys, $orderby a non-existent field, $order = ASC and $preserve_keys = false' => array(
+ 'expected' => array(
+ array( 'value' => 'four' ),
+ array( 'value' => 'two' ),
+ array( 'value' => 'three' ),
+ array( 'value' => 'one' ),
+ ),
+ 'target_array' => array(
+ 4 => array( 'value' => 'four' ),
+ 2 => array( 'value' => 'two' ),
+ 3 => array( 'value' => 'three' ),
+ 1 => array( 'value' => 'one' ),
+ ),
+ 'orderby' => 'id',
+ 'order' => 'ASC',
+ 'preserve_keys' => false,
+ ),
+ 'array[], string keys, $orderby a non-existent field, $order = ASC and $preserve_keys = false' => array(
+ 'expected' => array(
+ array( 'value' => 'four' ),
+ array( 'value' => 'two' ),
+ array( 'value' => 'three' ),
+ array( 'value' => 'one' ),
+ ),
+ 'target_array' => array(
+ 'four' => array( 'value' => 'four' ),
+ 'two' => array( 'value' => 'two' ),
+ 'three' => array( 'value' => 'three' ),
+ 'one' => array( 'value' => 'one' ),
+ ),
+ 'orderby' => 'id',
+ 'order' => 'ASC',
+ 'preserve_keys' => false,
+ ),
+ 'object[], int keys, $orderby a non-existent field, $order = ASC and $preserve_keys = false' => array(
+ 'expected' => array(
+ (object) array( 'value' => 'four' ),
+ (object) array( 'value' => 'two' ),
+ (object) array( 'value' => 'three' ),
+ (object) array( 'value' => 'one' ),
+ ),
+ 'target_array' => array(
+ 4 => (object) array( 'value' => 'four' ),
+ 2 => (object) array( 'value' => 'two' ),
+ 3 => (object) array( 'value' => 'three' ),
+ 1 => (object) array( 'value' => 'one' ),
+ ),
+ 'orderby' => 'id',
+ 'order' => 'ASC',
+ 'preserve_keys' => false,
+ ),
+ 'object[], int keys, $orderby a non-existent field, $order = DESC and $preserve_keys = true' => array(
+ 'expected' => array(
+ 4 => (object) array( 'value' => 'four' ),
+ 2 => (object) array( 'value' => 'two' ),
+ 3 => (object) array( 'value' => 'three' ),
+ 1 => (object) array( 'value' => 'one' ),
+ ),
+ 'target_array' => array(
+ 4 => (object) array( 'value' => 'four' ),
+ 2 => (object) array( 'value' => 'two' ),
+ 3 => (object) array( 'value' => 'three' ),
+ 1 => (object) array( 'value' => 'one' ),
+ ),
+ 'orderby' => 'id',
+ 'order' => 'DESC',
+ 'preserve_keys' => true,
+ ),
+ 'object[], string keys, $orderby a non-existent field, $order = ASC and $preserve_keys = false' => array(
+ 'expected' => array(
+ (object) array( 'value' => 'four' ),
+ (object) array( 'value' => 'two' ),
+ (object) array( 'value' => 'three' ),
+ (object) array( 'value' => 'one' ),
+ ),
+ 'target_array' => array(
+ 'four' => (object) array( 'value' => 'four' ),
+ 'two' => (object) array( 'value' => 'two' ),
+ 'three' => (object) array( 'value' => 'three' ),
+ 'one' => (object) array( 'value' => 'one' ),
+ ),
+ 'orderby' => 'id',
+ 'order' => 'ASC',
+ 'preserve_keys' => false,
+ ),
+ 'object[], string keys, $orderby a non-existent field, $order = DESC and $preserve_keys = true' => array(
+ 'expected' => array(
+ 'four' => (object) array( 'value' => 'four' ),
+ 'two' => (object) array( 'value' => 'two' ),
+ 'three' => (object) array( 'value' => 'three' ),
+ 'one' => (object) array( 'value' => 'one' ),
+ ),
+ 'target_array' => array(
+ 'four' => (object) array( 'value' => 'four' ),
+ 'two' => (object) array( 'value' => 'two' ),
+ 'three' => (object) array( 'value' => 'three' ),
+ 'one' => (object) array( 'value' => 'one' ),
+ ),
+ 'orderby' => 'id',
+ 'order' => 'DESC',
+ 'preserve_keys' => true,
+ ),
+ );
+ }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>