From 0e5b75b8be288bab7d4983703c14becd62e0e859 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Wed, 11 Oct 2017 00:09:47 +0000 Subject: [PATCH] Database: When parsing the host, leave the port and socket as `null` if they're not defined. This fixes a change in behaviour introduced by [41629]. The host is set to an empty string when it isn't defined, this continues existing behaviour. In particular, the mysqli library treats a `null` host as being the same as `localhost`, which is not always the intended behaviour. Props birgire, markjaquith, pento. Fixes #41722. git-svn-id: https://develop.svn.wordpress.org/trunk@41820 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/wp-db.php | 7 ++++--- tests/phpunit/tests/db.php | 14 +++++++------- 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/wp-includes/wp-db.php b/src/wp-includes/wp-db.php index 9759438a0c..a5903b4187 100644 --- a/src/wp-includes/wp-db.php +++ b/src/wp-includes/wp-db.php @@ -1479,7 +1479,7 @@ class wpdb { $port = null; $socket = null; $is_ipv6 = false; - + if ( $host_data = $this->parse_db_host( $this->dbhost ) ) { list( $host, $port, $socket, $is_ipv6 ) = $host_data; } @@ -1621,9 +1621,10 @@ class wpdb { return false; } + $host = ''; foreach ( array( 'host', 'port', 'socket' ) as $component ) { - if ( array_key_exists( $component, $matches ) ) { - $$component = $matches[$component]; + if ( ! empty( $matches[ $component ] ) ) { + $$component = $matches[ $component ]; } } diff --git a/tests/phpunit/tests/db.php b/tests/phpunit/tests/db.php index 208c70ed5b..23fc87f8c4 100644 --- a/tests/phpunit/tests/db.php +++ b/tests/phpunit/tests/db.php @@ -1208,10 +1208,10 @@ class Tests_DB extends WP_UnitTestCase { list( $parsed_host, $parsed_port, $parsed_socket, $parsed_is_ipv6 ) = $data; - $this->assertEquals( $host, $parsed_host ); - $this->assertEquals( $port, $parsed_port ); - $this->assertEquals( $socket, $parsed_socket ); - $this->assertEquals( $is_ipv6, $parsed_is_ipv6 ); + $this->assertSame( $host, $parsed_host ); + $this->assertSame( $port, $parsed_port ); + $this->assertSame( $socket, $parsed_socket ); + $this->assertSame( $is_ipv6, $parsed_is_ipv6 ); } } @@ -1220,7 +1220,7 @@ class Tests_DB extends WP_UnitTestCase { array( '', // DB_HOST false, // Expect parse_db_host to bail for this hostname - null, // Parsed host + '', // Parsed host null, // Parsed port null, // Parsed socket false, // is_ipv6 @@ -1228,7 +1228,7 @@ class Tests_DB extends WP_UnitTestCase { array( ':3306', false, - null, + '', '3306', null, false, @@ -1236,7 +1236,7 @@ class Tests_DB extends WP_UnitTestCase { array( ':/tmp/mysql.sock', false, - null, + '', null, '/tmp/mysql.sock', false,