diff --git a/tests/phpunit/tests/functions/wpArrayGet.php b/tests/phpunit/tests/functions/wpArrayGet.php new file mode 100644 index 0000000000..8ebd9c4386 --- /dev/null +++ b/tests/phpunit/tests/functions/wpArrayGet.php @@ -0,0 +1,286 @@ +assertSame( + _wp_array_get( + null, + array( 'a' ) + ), + null + ); + + $this->assertSame( + _wp_array_get( + array( + 'key' => 4, + ), + null + ), + null + ); + + $this->assertSame( + _wp_array_get( + array( + 'key' => 4, + ), + array() + ), + null + ); + + $this->assertSame( + _wp_array_get( + array( + 'key' => 4, + ), + array(), + true + ), + true + ); + } + + /** + * Test _wp_array_get() with non subtree paths. + * + * @ticket 51720 + */ + public function test_wp_array_get_simple_non_subtree() { + // Simple non sub tree test. + $this->assertSame( + _wp_array_get( + array( + 'key' => 4, + ), + array( 'key' ), + ), + 4 + ); + + // Simple non sub tree not found. + $this->assertSame( + _wp_array_get( + array( + 'key' => 4, + ), + array( 'invalid' ), + ), + null + ); + + // Simple non sub tree not found with a default. + $this->assertSame( + _wp_array_get( + array( + 'key' => 4, + ), + array( 'invalid' ), + 1 + ), + 1 + ); + + // Simple non sub tree integer path. + $this->assertSame( + _wp_array_get( + array( + 'a', + 'b', + 'c', + ), + array( 1 ) + ), + 'b' + ); + } + + /** + * Test _wp_array_get() with zero strings. + * + * @ticket 51720 + */ + public function test_wp_array_get_handle_zeros() { + $this->assertSame( + _wp_array_get( + array( + '-0' => 'a', + '0' => 'b', + ), + array( 0 ) + ), + 'b' + ); + + $this->assertSame( + _wp_array_get( + array( + '-0' => 'a', + '0' => 'b', + ), + array( -0 ) + ), + 'b' + ); + + $this->assertSame( + _wp_array_get( + array( + '-0' => 'a', + '0' => 'b', + ), + array( '-0' ) + ), + 'a' + ); + + $this->assertSame( + _wp_array_get( + array( + '-0' => 'a', + '0' => 'b', + ), + array( '0' ) + ), + 'b' + ); + } + + + /** + * Test _wp_array_get() with subtrees. + * + * @ticket 51720 + */ + public function test_wp_array_get_subtree() { + $this->assertSame( + _wp_array_get( + array( + 'a' => array( + 'b' => array( + 'c' => 1, + ), + ), + ), + array( 'a', 'b' ) + ), + array( 'c' => 1 ) + ); + + $this->assertSame( + _wp_array_get( + array( + 'a' => array( + 'b' => array( + 'c' => 1, + ), + ), + ), + array( 'a', 'b', 'c' ) + ), + 1 + ); + + $this->assertSame( + _wp_array_get( + array( + 'a' => array( + 'b' => array( + 'c' => 1, + ), + ), + ), + array( 'a', 'b', 'c', 'd' ) + ), + null + ); + } + + /** + * Test _wp_array_get() with null values. + * + * @ticket 51720 + */ + public function test_wp_array_get_null() { + $this->assertSame( + _wp_array_get( + array( + 'key' => null, + ), + array( 'key' ), + true + ), + null + ); + + $this->assertSame( + _wp_array_get( + array( + 'key' => null, + ), + array( 'key', 'subkey' ), + true + ), + true + ); + + $this->assertSame( + _wp_array_get( + array( + 'key' => array( + null => 4, + ), + ), + array( 'key', null ), + true + ), + 4 + ); + } + + /** + * Test _wp_array_get() with empty paths. + * + * @ticket 51720 + */ + public function test_wp_array_get_empty_paths() { + $this->assertSame( + _wp_array_get( + array( + 'a' => 4, + ), + array() + ), + null + ); + + $this->assertSame( + _wp_array_get( + array( + 'a' => array( + 'b' => array( + 'c' => 1, + ), + ), + ), + array( 'a', 'b', array() ) + ), + null + ); + } +}