|
|
|
@ -16,12 +16,44 @@ use Throwable; |
|
|
|
|
final class AppTest extends TestCase { |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* register error handlers |
|
|
|
|
*/ |
|
|
|
|
public function testInit() |
|
|
|
|
{ |
|
|
|
|
$displayErrors = true; |
|
|
|
|
set_error_handler(function( |
|
|
|
|
int $errno, |
|
|
|
|
string $errstr, |
|
|
|
|
?string $errfile = null, |
|
|
|
|
?int $errline = null, |
|
|
|
|
?array $errcontext = null |
|
|
|
|
) use (&$displayErrors){ |
|
|
|
|
if ($displayErrors){ |
|
|
|
|
echo "\nError $errstr ($errno)\nIn file $errfile\nLine : $errline\n"; |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
register_shutdown_function(function (){ |
|
|
|
|
$lastErr = error_get_last(); |
|
|
|
|
if (!is_null($lastErr)){ |
|
|
|
|
echo "\nAn error occured\n".json_encode($lastErr)."\n"; |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
$this->assertTrue($displayErrors); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* @depends testInit |
|
|
|
|
* @dataProvider apiRewriteProvider |
|
|
|
|
* @covers App::update_SERVERIfNeeded |
|
|
|
|
* @param string $rootFolder, |
|
|
|
|
* @param string $filePath, |
|
|
|
|
* @param string $shortScriptName, |
|
|
|
|
* @param string $rootFolder |
|
|
|
|
* @param string $filePath |
|
|
|
|
* @param string $shortScriptName |
|
|
|
|
* @param string $queryString |
|
|
|
|
* @param string $waitedBaseUrl |
|
|
|
|
* @param string $waitedPageId |
|
|
|
|
* @param string $waitedPageUrl |
|
|
|
|
* @param string $waitedThemesUrl |
|
|
|
|
* @param string $waitedPluginsUrl |
|
|
|
|
*/ |
|
|
|
|
public function testApiRewrite( |
|
|
|
|
string $rootFolder, |
|
|
|
@ -29,6 +61,10 @@ final class AppTest extends TestCase { |
|
|
|
|
string $shortScriptName, |
|
|
|
|
string $queryString, |
|
|
|
|
string $waitedBaseUrl, |
|
|
|
|
string $waitedPageId, |
|
|
|
|
string $waitedPageUrl, |
|
|
|
|
string $waitedThemesUrl, |
|
|
|
|
string $waitedPluginsUrl, |
|
|
|
|
): void |
|
|
|
|
{ |
|
|
|
|
$this->saveSERVER(); |
|
|
|
@ -49,20 +85,105 @@ final class AppTest extends TestCase { |
|
|
|
|
} catch (Throwable $th){ |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
$cwd = getcwd(); |
|
|
|
|
$this->revertSERVER(); |
|
|
|
|
$this->assertTrue($thrown,"TestBaseUrlException not found"); |
|
|
|
|
$this->assertEquals($waitedBaseUrl,$foundTh->getBaseUrl(),"Not same baseUrl"); |
|
|
|
|
$this->assertEquals("$cwd/vendor/picocms/plugins/",$foundTh->getPluginDir(),"Not same pluginDir"); |
|
|
|
|
$this->assertEquals("$cwd/vendor/picocms/themes/",$foundTh->getThemeDir(),"Not same themeDir"); |
|
|
|
|
$this->assertEquals("$cwd/",$foundTh->getRootDir(),"Not same rootDir"); |
|
|
|
|
$currentPage = $foundTh->getcurrentPage(); |
|
|
|
|
$this->assertIsArray($currentPage,"Current Page should be an array"); |
|
|
|
|
$this->assertArrayHasKey('id',$currentPage,"Current Page should be an array with key 'id'"); |
|
|
|
|
$this->assertEquals($waitedPageId,$currentPage['id'],"Not waited page's id"); |
|
|
|
|
$this->assertArrayHasKey('url',$currentPage,"Current Page should be an array with key 'url'"); |
|
|
|
|
$this->assertEquals($waitedPageUrl,$currentPage['url'],"Not waited page's url"); |
|
|
|
|
$twigVariables = $foundTh->getTwigVariables(); |
|
|
|
|
$this->assertIsArray($twigVariables,"Twigvariables should be an array"); |
|
|
|
|
$this->assertArrayHasKey('themes_url',$twigVariables,"Twigvariables should be an array with key 'themes_url'"); |
|
|
|
|
$this->assertEquals($waitedThemesUrl,$twigVariables['themes_url'],"Not waited themes_url"); |
|
|
|
|
$this->assertArrayHasKey('plugins_url',$twigVariables,"Twigvariables should be an array with key 'plugins_url'"); |
|
|
|
|
$this->assertEquals($waitedPluginsUrl,$twigVariables['plugins_url'],"Not waited plugins_url"); |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function apiRewriteProvider() |
|
|
|
|
{ |
|
|
|
|
$data = []; |
|
|
|
|
$this->prepareData($data,'content/*','content','index.php','/','','http://localhost/'); |
|
|
|
|
$this->prepareData($data,'content/index.php*','content','index.php','/index.php','','http://localhost/'); |
|
|
|
|
$this->prepareAPage($data,'','content','index'); |
|
|
|
|
$this->prepareAPage($data,'','content','sub/index'); |
|
|
|
|
$this->prepareAPage($data,'','content','theme'); |
|
|
|
|
|
|
|
|
|
$this->prepareAPage($data,'','sites/default','index'); |
|
|
|
|
$this->prepareAPage($data,'','sites/default','sub/index'); |
|
|
|
|
$this->prepareAPage($data,'','sites/default','theme'); |
|
|
|
|
|
|
|
|
|
$this->prepareAPage($data,'/sea','content','index'); |
|
|
|
|
$this->prepareAPage($data,'/sea','content','sub/index'); |
|
|
|
|
$this->prepareAPage($data,'/sea','content','theme'); |
|
|
|
|
|
|
|
|
|
$this->prepareAPage($data,'/sea','sites/default','index'); |
|
|
|
|
$this->prepareAPage($data,'/sea','sites/default','sub/index'); |
|
|
|
|
$this->prepareAPage($data,'/sea','sites/default','theme'); |
|
|
|
|
|
|
|
|
|
// echo "\n"; |
|
|
|
|
// foreach($data as $name => $line){ |
|
|
|
|
// echo "$name => ".implode(',',$line)."\n"; |
|
|
|
|
// } |
|
|
|
|
return $data; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
public function prepareData( |
|
|
|
|
protected function prepareAPage( |
|
|
|
|
array &$data, |
|
|
|
|
string $baseScriptName, |
|
|
|
|
string $rootFolder, |
|
|
|
|
string $pageId |
|
|
|
|
) |
|
|
|
|
{ |
|
|
|
|
$baseUrl = 'http://localhost'.$baseScriptName.'/'; |
|
|
|
|
$isIndex = ($pageId == 'index'); |
|
|
|
|
$isSubIndex = (substr($pageId,-strlen('/index')) == '/index'); |
|
|
|
|
$pageIdInUrl = $isIndex ? '' : ($isSubIndex ? substr($pageId,0,-strlen('/index')) : $pageId) ; |
|
|
|
|
$queriesStrings = $isIndex ? [['q'=>'','s'=>'','f'=>'']] : [ |
|
|
|
|
['q'=>$pageIdInUrl,'s'=>'','f'=>''], |
|
|
|
|
['q'=>'','s'=>$pageIdInUrl,'f'=>''], |
|
|
|
|
['q'=>'','s'=>$pageIdInUrl,'f'=>$pageIdInUrl], |
|
|
|
|
['q'=>$pageIdInUrl,'s'=>$pageIdInUrl,'f'=>''], |
|
|
|
|
['q'=>$pageIdInUrl,'s'=>$pageIdInUrl,'f'=>$pageIdInUrl] |
|
|
|
|
]; |
|
|
|
|
foreach ($queriesStrings as $queryData) { |
|
|
|
|
$queryString = empty($queryData['q']) ? '' :'?'.$queryData['q']; |
|
|
|
|
$scriptNameMiddle = empty($queryData['s']) ? '' : $queryData['s'].'/'; |
|
|
|
|
foreach (['','index.php'] as $endScriptName) { |
|
|
|
|
$formattedQueryString = str_replace('/','%2F',$queryString); |
|
|
|
|
$pageUrl = empty($queryData['s']) ? (empty($queryData['q']) ? '' : $formattedQueryString) : $queryData['s']; |
|
|
|
|
$canRewriteFromRoot = empty($queryData['s']) && empty($queryData['f']); |
|
|
|
|
if ($rootFolder == 'content' && $canRewriteFromRoot){ |
|
|
|
|
$scriptName = "$baseScriptName/$scriptNameMiddle$endScriptName"; |
|
|
|
|
$name = $rootFolder.$scriptName.$queryString.'*'; |
|
|
|
|
$this->prepareData($data,$name,$rootFolder,'index.php',$scriptName,$queryData['q'],$baseUrl,$pageId,$pageUrl); |
|
|
|
|
} |
|
|
|
|
foreach ([false,true] as $withRewrite) { |
|
|
|
|
if ($withRewrite){ |
|
|
|
|
$scriptName = "$baseScriptName/$scriptNameMiddle$endScriptName"; |
|
|
|
|
$name = $rootFolder.$scriptName.$queryString.' R'; |
|
|
|
|
$waitedUrl = $baseUrl; |
|
|
|
|
} else { |
|
|
|
|
$scriptName = "$baseScriptName/$rootFolder/$scriptNameMiddle$endScriptName"; |
|
|
|
|
$name = $rootFolder.$scriptName.$queryString; |
|
|
|
|
$waitedUrl = $baseUrl.$rootFolder.'/'; |
|
|
|
|
} |
|
|
|
|
if ($canRewriteFromRoot){ |
|
|
|
|
$this->prepareData($data,$name."*",$rootFolder,"index.php",$scriptName,$queryData['q'],$waitedUrl,$pageId,$pageUrl); |
|
|
|
|
} |
|
|
|
|
$formattedRootMiddle = empty($pageIdInUrl) ? '' : "/$pageIdInUrl"; |
|
|
|
|
$this->prepareData($data,$name,$rootFolder,"$rootFolder$formattedRootMiddle/index.php",$scriptName,$queryData['q'],$waitedUrl,$pageId,$pageUrl); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
protected function prepareData( |
|
|
|
|
array &$data, |
|
|
|
|
string $name, |
|
|
|
|
string $rootFolder, |
|
|
|
@ -70,15 +191,29 @@ final class AppTest extends TestCase { |
|
|
|
|
string $shortScriptName, |
|
|
|
|
string $queryString, |
|
|
|
|
string $waitedBaseUrl, |
|
|
|
|
string $waitedPageId, |
|
|
|
|
string $waitedPageEndUrl |
|
|
|
|
) |
|
|
|
|
{ |
|
|
|
|
$data[$name] = [ |
|
|
|
|
'rootFolder' => $rootFolder, |
|
|
|
|
'filePath' => $filePath, |
|
|
|
|
'shortScriptName' => $shortScriptName, |
|
|
|
|
'queryString' => $queryString, |
|
|
|
|
'waitedBaseUrl' => $waitedBaseUrl, |
|
|
|
|
]; |
|
|
|
|
$waitedPageUrl = $waitedBaseUrl.$waitedPageEndUrl; |
|
|
|
|
$nbLevels = in_array($shortScriptName,['/','/index.php']) ? 0 : count(explode('/',$rootFolder)); |
|
|
|
|
$prefix = implode('/',array_fill(0,$nbLevels,'..')); |
|
|
|
|
if (!empty($prefix)){ |
|
|
|
|
$prefix .= '/'; |
|
|
|
|
} |
|
|
|
|
$waitedThemesUrl = $waitedBaseUrl.$prefix.'vendor/picocms/themes'; |
|
|
|
|
$waitedPluginsUrl = $waitedBaseUrl.$prefix.'vendor/picocms/plugins'; |
|
|
|
|
$data[$name] = compact([ |
|
|
|
|
'rootFolder', |
|
|
|
|
'filePath', |
|
|
|
|
'shortScriptName', |
|
|
|
|
'queryString', |
|
|
|
|
'waitedBaseUrl', |
|
|
|
|
'waitedPageId', |
|
|
|
|
'waitedPageUrl', |
|
|
|
|
'waitedThemesUrl', |
|
|
|
|
'waitedPluginsUrl' |
|
|
|
|
]); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|