Skip to content

Commit

Permalink
fix(Module/Support/DbDump.php) handling of locahost:port URLs in repl…
Browse files Browse the repository at this point in the history
…acement

fixes #430
  • Loading branch information
lucatume committed Aug 4, 2020
1 parent a14506d commit d103370
Show file tree
Hide file tree
Showing 5 changed files with 624 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ This project adheres to [Semantic Versioning](http://semver.org/).

## [unreleased] Unreleased

### Fixed

- URL replacement function in `WPDb` module that would incorrectly handling the replacement of `locahost:port` URLs, fixes #430

## [2.6.5] 2020-07-16;

### Fixed
Expand Down
3 changes: 2 additions & 1 deletion src/Codeception/Module/WPLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,8 @@ public function _loadPlugins()
* $userId = $I->factory()->user->create(['role' => 'administrator']);
* ```
*
* @return \tad\WPBrowser\Module\WPLoader\FactoryStore A factory store, proxy to get hold of the Core suite object factories.
* @return \tad\WPBrowser\Module\WPLoader\FactoryStore A factory store, proxy to get hold of the Core suite object
* factories.
*
* @link https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/
*/
Expand Down
10 changes: 8 additions & 2 deletions src/tad/WPBrowser/Module/Support/DbDump.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,14 @@ public function replaceSiteDomainInSqlString($sql, $debug = false)
}

$originalFrags = parse_url($this->originalUrl);
$originalFrags = array_intersect_key($originalFrags, array_flip(['host', 'path']));
$originalHostAndPath = rtrim(implode('', array_merge(['host' => '', 'path' => ''], $originalFrags)), '/');
$originalFrags = array_intersect_key($originalFrags, array_flip(['host', 'path','port']));
if (!empty($originalFrags['port'])) {
$originalFrags['port'] = ':' . $originalFrags['port'];
}
$originalHostAndPath = rtrim(
implode('', array_merge(['host' => '', 'path' => '', 'port' => ''], $originalFrags)),
'/'
);
$replaceScheme = parse_url($this->url, PHP_URL_SCHEME);
$replaceHost = parse_url($this->url, PHP_URL_HOST);

Expand Down
590 changes: 590 additions & 0 deletions tests/_data/dump-test/url-replacement-test-01.sql

Large diffs are not rendered by default.

20 changes: 20 additions & 0 deletions tests/unit/tad/WPBrowser/Module/Support/DbDumpTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -303,4 +303,24 @@ public function should_correctly_replace_subdomain_urls_in_multisite_installatio
$this->assertEquals($expectedLine, $replaced, 'Error at line number ' . $lineNumber);
}
}

/**
* It should correctly replace localhost host address with pretty URL
*
* @test
*/
public function should_correctly_replace_localhost_host_address_with_pretty_url()
{
$inputFile = codecept_data_dir('dump-test/url-replacement-test-01.sql');
$sql = file_get_contents($inputFile);

$dbDump = $this->make_instance();
$dbDump->setUrl('http://some-nice-host-name');
$originalUrl = $dbDump->getOriginalUrlFromSqlString($sql);

$this->assertEquals('http://localhost:5100', $originalUrl);

$replacedSql = $dbDump->replaceSiteDomainInSqlString($sql);
$this->assertEquals('http://some-nice-host-name', $dbDump->getOriginalUrlFromSqlString($replacedSql));
}
}

0 comments on commit d103370

Please sign in to comment.