utils::GetDefaultUrlAppRoot : make comparison case insentitive, add a test

This commit is contained in:
Pierre Goiffon
2019-12-23 11:06:29 +01:00
parent 57ae29cf2f
commit 13d31ac211
2 changed files with 40 additions and 5 deletions

View File

@@ -846,10 +846,25 @@ class utils
$sAbsoluteUrl = "$sProtocol://{$sServerName}{$sPort}{$sPath}";
$sCurrentScript = realpath($_SERVER['SCRIPT_FILENAME']);
$sAppRoot = realpath(APPROOT);
return self::GetAppRootUrl($sCurrentScript, $sAppRoot, $sAbsoluteUrl);
}
/**
* @param $sCurrentScript
* @param $sAppRoot
* @param $sAbsoluteUrl
*
* @return false|string
* @throws \Exception
*/
public static function GetAppRootUrl($sCurrentScript, $sAppRoot, $sAbsoluteUrl)
{
$sCurrentScript = str_replace('\\', '/', $sCurrentScript); // canonical path
$sAppRoot = str_replace('\\', '/', realpath(APPROOT)).'/'; // canonical path with the trailing '/' appended
$sCurrentRelativePath = str_replace($sAppRoot, '', $sCurrentScript);
$sAppRoot = str_replace('\\', '/', $sAppRoot).'/'; // canonical path with the trailing '/' appended
$sCurrentRelativePath = str_ireplace($sAppRoot, '', $sCurrentScript);
$sAppRootPos = strpos($sAbsoluteUrl, $sCurrentRelativePath);
if ($sAppRootPos !== false)
{
@@ -858,7 +873,7 @@ class utils
else
{
// Second attempt without index.php at the end...
$sCurrentRelativePath = str_replace('index.php', '', $sCurrentRelativePath);
$sCurrentRelativePath = str_ireplace('index.php', '', $sCurrentRelativePath);
$sAppRootPos = strpos($sAbsoluteUrl, $sCurrentRelativePath);
if ($sAppRootPos !== false)
{
@@ -868,8 +883,9 @@ class utils
{
// No luck...
throw new Exception("Failed to determine application root path $sAbsoluteUrl ($sCurrentRelativePath) APPROOT:'$sAppRoot'");
}
}
}
return $sAppRootUrl;
}

View File

@@ -102,4 +102,23 @@ class UtilsTest extends \Combodo\iTop\Test\UnitTest\ItopTestCase
],
];
}
/**
* @dataProvider appRootUrlProvider
* @covers utils::GetAppRootUrl
*/
public function testGetAppRootUrl($sReturnValue, $sCurrentScript, $sAppRoot, $sAbsoluteUrl)
{
$this->assertEquals($sReturnValue, utils::GetAppRootUrl($sCurrentScript, $sAppRoot, $sAbsoluteUrl));
}
public function appRootUrlProvider()
{
return array(
'Setup index (windows antislash)' => array('http://localhost/', 'C:\Dev\wamp64\www\itop-dev\setup\index.php', 'C:\Dev\wamp64\www\itop-dev', 'http://localhost/setup/'),
'Setup index (windows slash)' => array('http://127.0.0.1/', 'C:/web/setup/index.php', 'C:/web', 'http://127.0.0.1/setup/'),
'Setup index (windows slash, drive letter case difference)' => array('http://127.0.0.1/', 'c:/web/setup/index.php', 'C:/web', 'http://127.0.0.1/setup/'),
);
}
}