Compare commits

..

No commits in common. '7b1cd1101719994afee58d43b1ea8e481210d3b9' and '1f89a43499aa6209399468407f023ad4350a5fe3' have entirely different histories.

  1. 21
      SeacmsAppPlugin.php
  2. 21
      src/TestBaseUrl.php
  3. 17
      src/TestBaseUrlException.php
  4. 24
      src/TestOnPageRenderingInterface.php
  5. 161
      tests/AppTest.php

@ -6,7 +6,6 @@ use SeaCMS\Api\LateApiAware;
use SeaCMS\Api\JsonResponse;
use SeaCMS\App\MdResponse;
use SeaCMS\App\TestInterface;
use SeaCMS\App\TestOnPageRenderingInterface;
/**
* A plugin for SeaCMS-app.
@ -25,12 +24,6 @@ class SeacmsAppPlugin extends AbstractPicoPlugin implements LateApiAware
*/
protected $triggerTest;
/**
* define if test output should be defined onPageRendering
* @var bool
*/
protected $triggerTestOnPageRendering;
/**
* define test
* @var TestInterface
@ -46,7 +39,6 @@ class SeacmsAppPlugin extends AbstractPicoPlugin implements LateApiAware
{
parent::__construct($pico);
$this->triggerTest = !empty($testRunner) && ($testRunner instanceof TestInterface);
$this->triggerTestOnPageRendering = !empty($testRunner) && ($testRunner instanceof TestOnPageRenderingInterface);
$this->testRunner = ($this->triggerTest) ? $testRunner : null;
}
@ -181,17 +173,4 @@ class SeacmsAppPlugin extends AbstractPicoPlugin implements LateApiAware
}
}
/**
* Triggered before Pico renders the page
*
* @param string &$templateName file name of the template
* @param array &$twigVariables template variables
*/
public function onPageRendering(&$templateName, array &$twigVariables)
{
if ($this->triggerTestOnPageRendering){
$this->testRunner->runOnPageRendering($this,$templateName,$twigVariables);
}
}
}

@ -6,23 +6,21 @@ namespace SeaCMS\App;
use SeacmsAppPlugin;
use SeaCMS\App\TestException;
use SeaCMS\App\TestOnPageRenderingInterface;
use SeaCMS\App\TestInterface;
use SeaCMS\App\TestBaseUrlException;
/**
* test base Url
*/
class TestBaseUrl implements TestOnPageRenderingInterface
class TestBaseUrl implements TestInterface
{
/**
* run tests
* @param SeacmsAppPlugin $plugin
* @param string $templateName file name of the template
* @param array $twigVariables template variables
* @param string $output
* @throws TestException
*/
public function runOnPageRendering(SeacmsAppPlugin $plugin, string $templateName, array $twigVariables)
{
public function run(SeacmsAppPlugin $plugin, string $output){
$pico = $plugin->getPico();
throw new TestBaseUrlException(
$pico->getBaseUrl(),
@ -30,17 +28,8 @@ class TestBaseUrl implements TestOnPageRenderingInterface
$pico->getPluginsDir(),
$pico->getThemesDir(),
$pico->getCurrentPage(),
$twigVariables,
"Testing base url"
);
}
/**
* run tests
* @param SeacmsAppPlugin $plugin
* @param string $output
* @throws TestException
*/
public function run(SeacmsAppPlugin $plugin, string $output){
}
}

@ -42,19 +42,12 @@ class TestBaseUrlException extends TestException
*/
protected $currentPage;
/**
* twigVariables
* @var null|array
*/
protected $twigVariables;
public function __construct(
string $baseUrl,
string $rootDir,
string $pluginDir,
string $themeDir,
?array $currentPage,
?array $twigVariables,
string $message = "",
int $code=0,
Throwable $th = null
@ -66,7 +59,6 @@ class TestBaseUrlException extends TestException
$this->pluginDir = $pluginDir;
$this->themeDir = $themeDir;
$this->currentPage = $currentPage;
$this->twigVariables = $twigVariables;
}
/**
@ -114,13 +106,4 @@ class TestBaseUrlException extends TestException
return $this->currentPage;
}
/**
* get twigVariables
* @return null|array
*/
public function getTwigVariables(): ?array
{
return $this->twigVariables;
}
}

@ -1,24 +0,0 @@
<?php
// SPDX-License-Identifier: EUPL-1.2
// Authors: see README.md
namespace SeaCMS\App;
use SeacmsAppPlugin;
use SeaCMS\App\TestException;
use SeaCMS\App\TestInterface;
/**
* define interface for tests
*/
interface TestOnPageRenderingInterface extends TestInterface
{
/**
* run tests
* @param SeacmsAppPlugin $plugin
* @param string $templateName file name of the template
* @param array $twigVariables template variables
* @throws TestException
*/
public function runOnPageRendering(SeacmsAppPlugin $plugin, string $templateName, array $twigVariables);
}

@ -16,44 +16,12 @@ 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,
@ -61,10 +29,6 @@ final class AppTest extends TestCase {
string $shortScriptName,
string $queryString,
string $waitedBaseUrl,
string $waitedPageId,
string $waitedPageUrl,
string $waitedThemesUrl,
string $waitedPluginsUrl,
): void
{
$this->saveSERVER();
@ -85,105 +49,20 @@ 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->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";
// }
$this->prepareData($data,'content/*','content','index.php','/','','http://localhost/');
$this->prepareData($data,'content/index.php*','content','index.php','/index.php','','http://localhost/');
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);
}
$formattedRootMiddle = empty($pageIdInUrl) ? '' : "/$pageIdInUrl";
$this->prepareData($data,$name,$rootFolder,"$rootFolder$formattedRootMiddle/index.php",$scriptName,$queryData['q'],$waitedUrl,$pageId,$pageUrl);
}
}
}
}
protected function prepareData(
public function prepareData(
array &$data,
string $name,
string $rootFolder,
@ -191,29 +70,15 @@ final class AppTest extends TestCase {
string $shortScriptName,
string $queryString,
string $waitedBaseUrl,
string $waitedPageId,
string $waitedPageEndUrl
)
{
$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'
]);
$data[$name] = [
'rootFolder' => $rootFolder,
'filePath' => $filePath,
'shortScriptName' => $shortScriptName,
'queryString' => $queryString,
'waitedBaseUrl' => $waitedBaseUrl,
];
}
/**

Loading…
Cancel
Save