From 7b1cd1101719994afee58d43b1ea8e481210d3b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9my=20Dufraisse?= Date: Tue, 21 Mar 2023 12:40:54 +0100 Subject: [PATCH] feat(AppTest): test themes and plugins url --- SeacmsAppPlugin.php | 21 ++++++++++++++ src/TestBaseUrl.php | 21 ++++++++++---- src/TestBaseUrlException.php | 17 ++++++++++++ src/TestOnPageRenderingInterface.php | 24 ++++++++++++++++ tests/AppTest.php | 41 +++++++++++++++++++++------- 5 files changed, 109 insertions(+), 15 deletions(-) create mode 100644 src/TestOnPageRenderingInterface.php diff --git a/SeacmsAppPlugin.php b/SeacmsAppPlugin.php index fb8b36d..92b5e4c 100644 --- a/SeacmsAppPlugin.php +++ b/SeacmsAppPlugin.php @@ -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); + } + } + } \ No newline at end of file diff --git a/src/TestBaseUrl.php b/src/TestBaseUrl.php index 331f967..7e2d9f6 100644 --- a/src/TestBaseUrl.php +++ b/src/TestBaseUrl.php @@ -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){ } } diff --git a/src/TestBaseUrlException.php b/src/TestBaseUrlException.php index e58c1aa..2572a6f 100644 --- a/src/TestBaseUrlException.php +++ b/src/TestBaseUrlException.php @@ -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; + } + } diff --git a/src/TestOnPageRenderingInterface.php b/src/TestOnPageRenderingInterface.php new file mode 100644 index 0000000..ed5764e --- /dev/null +++ b/src/TestOnPageRenderingInterface.php @@ -0,0 +1,24 @@ +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' ]); }