feat(AppTest): test themes and plugins url

This commit is contained in:
Jérémy Dufraisse 2023-03-21 12:40:54 +01:00
parent 02601c54a3
commit 7b1cd11017
5 changed files with 109 additions and 15 deletions

View File

@ -6,6 +6,7 @@ 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.
@ -24,6 +25,12 @@ class SeacmsAppPlugin extends AbstractPicoPlugin implements LateApiAware
*/
protected $triggerTest;
/**
* define if test output should be defined onPageRendering
* @var bool
*/
protected $triggerTestOnPageRendering;
/**
* define test
* @var TestInterface
@ -39,6 +46,7 @@ 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;
}
@ -173,4 +181,17 @@ 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);
}
}
}

View File

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

View File

@ -42,12 +42,19 @@ 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
@ -59,6 +66,7 @@ class TestBaseUrlException extends TestException
$this->pluginDir = $pluginDir;
$this->themeDir = $themeDir;
$this->currentPage = $currentPage;
$this->twigVariables = $twigVariables;
}
/**
@ -106,4 +114,13 @@ class TestBaseUrlException extends TestException
return $this->currentPage;
}
/**
* get twigVariables
* @return null|array
*/
public function getTwigVariables(): ?array
{
return $this->twigVariables;
}
}

View File

@ -0,0 +1,24 @@
<?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);
}

View File

@ -45,13 +45,15 @@ final class AppTest extends TestCase {
* @depends testInit
* @dataProvider apiRewriteProvider
* @covers App::update_SERVERIfNeeded
* @param string $rootFolder,
* @param string $filePath,
* @param string $shortScriptName,
* @param string $queryString,
* @param string $waitedBaseUrl,
* @param string $waitedPageId,
* @param string $waitedPageUrl,
* @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,6 +63,8 @@ final class AppTest extends TestCase {
string $waitedBaseUrl,
string $waitedPageId,
string $waitedPageUrl,
string $waitedThemesUrl,
string $waitedPluginsUrl,
): void
{
$this->saveSERVER();
@ -94,6 +98,13 @@ final class AppTest extends TestCase {
$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()
@ -165,7 +176,8 @@ final class AppTest extends TestCase {
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);
$formattedRootMiddle = empty($pageIdInUrl) ? '' : "/$pageIdInUrl";
$this->prepareData($data,$name,$rootFolder,"$rootFolder$formattedRootMiddle/index.php",$scriptName,$queryData['q'],$waitedUrl,$pageId,$pageUrl);
}
}
}
@ -180,10 +192,17 @@ final class AppTest extends TestCase {
string $queryString,
string $waitedBaseUrl,
string $waitedPageId,
string $waitedPageEndUrl,
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',
@ -191,7 +210,9 @@ final class AppTest extends TestCase {
'queryString',
'waitedBaseUrl',
'waitedPageId',
'waitedPageUrl'
'waitedPageUrl',
'waitedThemesUrl',
'waitedPluginsUrl'
]);
}