Changeset 50965
- Timestamp:
- 05/24/2021 12:24:14 PM (4 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
- 1 moved
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/functions.php
r50958 r50965 4637 4637 * $array = array(); 4638 4638 * _wp_array_set( $array, array( 'a', 'b', 'c', 1 ); 4639 * 4639 4640 * $array becomes: 4640 4641 * array( … … 4646 4647 * ); 4647 4648 * 4648 * @param array $array An array that we want to mutate to include a specific value in a path. 4649 * @param array $path An array of keys describing the path that we want to mutate. 4650 * @param mixed $value The value that will be set. 4649 * @internal 4650 * 4651 * @since 5.8.0 4652 * @access private 4653 * 4654 * @param array $array An array that we want to mutate to include a specific value in a path. 4655 * @param array $path An array of keys describing the path that we want to mutate. 4656 * @param mixed $value The value that will be set. 4651 4657 */ 4652 4658 function _wp_array_set( &$array, $path, $value = null ) { … … 4660 4666 return; 4661 4667 } 4668 4662 4669 $path_length = count( $path ); 4670 4663 4671 if ( 0 === $path_length ) { 4664 4672 return; 4665 4673 } 4674 4666 4675 foreach ( $path as $path_element ) { 4667 4676 if ( … … 4683 4692 $array = &$array[ $path_element ]; // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.VariableRedeclaration 4684 4693 } 4694 4685 4695 $array[ $path[ $i ] ] = $value; 4686 4696 } -
trunk/tests/phpunit/tests/functions/wpArrayGet.php
r50964 r50965 58 58 59 59 /** 60 * Test _wp_array_get() with non 60 * Test _wp_array_get() with non-subtree paths. 61 61 * 62 62 * @ticket 51720 63 63 */ 64 64 public function test_wp_array_get_simple_non_subtree() { 65 // Simple non subtree test.65 // Simple non-subtree test. 66 66 $this->assertSame( 67 67 _wp_array_get( … … 74 74 ); 75 75 76 // Simple non subtree not found.76 // Simple non-subtree not found. 77 77 $this->assertSame( 78 78 _wp_array_get( … … 85 85 ); 86 86 87 // Simple non subtree not found with a default.87 // Simple non-subtree not found with a default. 88 88 $this->assertSame( 89 89 _wp_array_get( … … 97 97 ); 98 98 99 // Simple non subtree integer path.99 // Simple non-subtree integer path. 100 100 $this->assertSame( 101 101 _wp_array_get( … … 112 112 113 113 /** 114 * Test _wp_array_get() with subtrees. 115 * 116 * @ticket 51720 117 */ 118 public function test_wp_array_get_subtree() { 119 $this->assertSame( 120 _wp_array_get( 121 array( 122 'a' => array( 123 'b' => array( 124 'c' => 1, 125 ), 126 ), 127 ), 128 array( 'a', 'b' ) 129 ), 130 array( 'c' => 1 ) 131 ); 132 133 $this->assertSame( 134 _wp_array_get( 135 array( 136 'a' => array( 137 'b' => array( 138 'c' => 1, 139 ), 140 ), 141 ), 142 array( 'a', 'b', 'c' ) 143 ), 144 1 145 ); 146 147 $this->assertSame( 148 _wp_array_get( 149 array( 150 'a' => array( 151 'b' => array( 152 'c' => 1, 153 ), 154 ), 155 ), 156 array( 'a', 'b', 'c', 'd' ) 157 ), 158 null 159 ); 160 } 161 162 /** 114 163 * Test _wp_array_get() with zero strings. 115 164 * … … 159 208 ), 160 209 'b' 161 );162 }163 164 165 /**166 * Test _wp_array_get() with subtrees.167 *168 * @ticket 51720169 */170 public function test_wp_array_get_subtree() {171 $this->assertSame(172 _wp_array_get(173 array(174 'a' => array(175 'b' => array(176 'c' => 1,177 ),178 ),179 ),180 array( 'a', 'b' )181 ),182 array( 'c' => 1 )183 );184 185 $this->assertSame(186 _wp_array_get(187 array(188 'a' => array(189 'b' => array(190 'c' => 1,191 ),192 ),193 ),194 array( 'a', 'b', 'c' )195 ),196 1197 );198 199 $this->assertSame(200 _wp_array_get(201 array(202 'a' => array(203 'b' => array(204 'c' => 1,205 ),206 ),207 ),208 array( 'a', 'b', 'c', 'd' )209 ),210 null211 210 ); 212 211 } -
trunk/tests/phpunit/tests/functions/wpArraySet.php
r50964 r50965 1 1 <?php 2 /**3 * _wp_array_set.4 *5 * @package WordPress6 */7 2 8 3 /** 9 * Test _wp_array_set function.4 * Tests for the _wp_array_get() function 10 5 * 11 * @package WordPress 6 * @since 5.8.0 7 * 8 * @group functions.php 9 * @covers ::_wp_array_get 12 10 */ 13 class WP_Array_Set_Test extends WP_UnitTestCase { 11 class Tests_Functions_wpArraySet extends WP_UnitTestCase { 12 14 13 /** 15 * Test _wp_array_set() with simple non subtree path.14 * Test _wp_array_set() with invalid parameters. 16 15 * 17 16 * @ticket 53175 18 17 */ 19 public function test_simple_not_subtree_set() { 18 public function test_wp_array_set_invalid_parameters() { 19 $test = 3; 20 _wp_array_set( $test, array( 'a' ), 1 ); 21 $this->assertSame( 22 $test, 23 3 24 ); 25 26 $test_array = array( 'a' => 2 ); 27 _wp_array_set( $test_array, 'a', 3 ); 28 $this->assertSame( 29 $test_array, 30 array( 'a' => 2 ) 31 ); 32 33 $test_array = array( 'a' => 2 ); 34 _wp_array_set( $test_array, null, 3 ); 35 $this->assertSame( 36 $test_array, 37 array( 'a' => 2 ) 38 ); 39 40 $test_array = array( 'a' => 2 ); 41 _wp_array_set( $test_array, array(), 3 ); 42 $this->assertSame( 43 $test_array, 44 array( 'a' => 2 ) 45 ); 46 47 $test_array = array( 'a' => 2 ); 48 _wp_array_set( $test_array, array( 'a', array() ), 3 ); 49 $this->assertSame( 50 $test_array, 51 array( 'a' => 2 ) 52 ); 53 } 54 55 /** 56 * Test _wp_array_set() with simple non-subtree path. 57 * 58 * @ticket 53175 59 */ 60 public function test_wp_array_set_simple_non_subtree() { 20 61 $test_array = array(); 21 62 _wp_array_set( $test_array, array( 'a' ), 1 ); … … 48 89 * @ticket 53175 49 90 */ 50 public function test_ subtree_set() {91 public function test_wp_array_set_subtree() { 51 92 $test_array = array(); 52 93 _wp_array_set( $test_array, array( 'a', 'b', 'c' ), 1 ); … … 92 133 ); 93 134 } 94 95 /**96 * Test _wp_array_set() with invalid parameters.97 *98 * @ticket 5317599 */100 public function test_invalid_parameters_set() {101 $test = 3;102 _wp_array_set( $test, array( 'a' ), 1 );103 $this->assertSame(104 $test,105 3106 );107 108 $test_array = array( 'a' => 2 );109 _wp_array_set( $test_array, 'a', 3 );110 $this->assertSame(111 $test_array,112 array( 'a' => 2 )113 );114 115 $test_array = array( 'a' => 2 );116 _wp_array_set( $test_array, null, 3 );117 $this->assertSame(118 $test_array,119 array( 'a' => 2 )120 );121 122 $test_array = array( 'a' => 2 );123 _wp_array_set( $test_array, array(), 3 );124 $this->assertSame(125 $test_array,126 array( 'a' => 2 )127 );128 129 $test_array = array( 'a' => 2 );130 _wp_array_set( $test_array, array( 'a', array() ), 3 );131 $this->assertSame(132 $test_array,133 array( 'a' => 2 )134 );135 }136 135 }
Note: See TracChangeset
for help on using the changeset viewer.