feat(AppTest): define test on url rewrite

This commit is contained in:
Jérémy Dufraisse 2023-03-21 12:04:41 +01:00
parent d4711dddd1
commit 02601c54a3

View File

@ -48,7 +48,10 @@ final class AppTest extends TestCase {
* @param string $rootFolder,
* @param string $filePath,
* @param string $shortScriptName,
* @param string $queryString
* @param string $queryString,
* @param string $waitedBaseUrl,
* @param string $waitedPageId,
* @param string $waitedPageUrl,
*/
public function testApiRewrite(
string $rootFolder,
@ -56,6 +59,8 @@ final class AppTest extends TestCase {
string $shortScriptName,
string $queryString,
string $waitedBaseUrl,
string $waitedPageId,
string $waitedPageUrl,
): void
{
$this->saveSERVER();
@ -76,19 +81,96 @@ 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");
}
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;
}
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);
}
$this->prepareData($data,$name,$rootFolder,"$rootFolder/$pageIdInUrl/index.php",$scriptName,$queryData['q'],$waitedUrl,$pageId,$pageUrl);
}
}
}
}
protected function prepareData(
array &$data,
string $name,
@ -97,15 +179,20 @@ 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;
$data[$name] = compact([
'rootFolder',
'filePath',
'shortScriptName',
'queryString',
'waitedBaseUrl',
'waitedPageId',
'waitedPageUrl'
]);
}
/**