From d7e89a4a310ec5501b3b17f23f2b769bfe05e7f5 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Tue, 28 Sep 2021 10:19:37 +0200 Subject: [PATCH] bug fixes --- Application/ApplicationManager.php | 13 ++++--- Application/InstallerAbstract.php | 17 ++++++--- Application/StatusAbstract.php | 9 +++-- Module/ModuleManager.php | 2 +- tests/Application/ApplicationInfoTest.php | 4 +- tests/Application/ApplicationManagerTest.php | 4 +- tests/Application/Testapp/Admin/Installer.php | 30 +++++++++++++++ tests/Application/Testapp/Admin/Status.php | 30 +++++++++++++++ tests/Application/info-test.json | 4 +- .../Database/Query/BuilderTest.php | 16 +------- tests/Module/ModuleManagerTest.php | 38 +++++++++++++++++++ 11 files changed, 130 insertions(+), 37 deletions(-) create mode 100644 tests/Application/Testapp/Admin/Installer.php create mode 100644 tests/Application/Testapp/Admin/Status.php diff --git a/Application/ApplicationManager.php b/Application/ApplicationManager.php index 4e3ecab84..9398b97d6 100644 --- a/Application/ApplicationManager.php +++ b/Application/ApplicationManager.php @@ -113,7 +113,9 @@ final class ApplicationManager $this->installFiles($source, $destination); $this->replacePlaceholder($destination); - $class = '\\Web\\' . $info->getInternalName() . '\\Admin\\Installer'; + $classPath = \substr(\realpath($destination) . '/Admin/Installer', \strlen(\realpath(__DIR__ . '/../../'))); + + $class = \str_replace('/', '\\', $classPath); $class::install($this->app->dbPool, $info, $this->app->appSettings); return true; @@ -149,22 +151,23 @@ final class ApplicationManager * Get all installed modules. * * @param bool $useCache Use Cache + * @param string $basePath Base path for the applications * * @return array * * @since 1.0.0 */ - public function getInstalledApplications(bool $useCache = true) : array + public function getInstalledApplications(bool $useCache = true, string $basePath = __DIR__ . '/../../Web') : array { if (empty($this->installed) || !$useCache) { - $apps = \scandir(__DIR__ . '/../../Web'); + $apps = \scandir($basePath); foreach ($apps as $app) { - if ($app === '.' || $app === '..' || !\is_file(__DIR__ . '/../../Web/' . $app . '/info.json')) { + if ($app === '.' || $app === '..' || !\is_file($basePath . '/' . $app . '/info.json')) { continue; } - $this->installed[$app] = $this->loadInfo(__DIR__ . '/../../Web/' . $app . '/info.json'); + $this->installed[$app] = $this->loadInfo($basePath . '/' . $app . '/info.json'); } } diff --git a/Application/InstallerAbstract.php b/Application/InstallerAbstract.php index 70cc3b6fe..a05a8f9fd 100644 --- a/Application/InstallerAbstract.php +++ b/Application/InstallerAbstract.php @@ -30,6 +30,8 @@ use phpOMS\System\File\Local\Directory; */ abstract class InstallerAbstract { + public const PATH = ''; + /** * Install app. * @@ -46,7 +48,7 @@ abstract class InstallerAbstract self::createTables($dbPool, $info); self::installSettings($dbPool, $info, $cfgHandler); self::activate($dbPool, $info); - self::installTheme(__DIR__ . '/../../Web/' . $info->getInternalName(), 'Default'); + self::installTheme(static::PATH . '/..', 'Default'); } /** @@ -96,7 +98,7 @@ abstract class InstallerAbstract */ public static function installSettings(DatabasePool $dbPool, ApplicationInfo $info, SettingsInterface $cfgHandler) : void { - $path = __DIR__ . '/../../Web/' . $info->getInternalName() . '/Admin/Install/Settings.install.php'; + $path = static::PATH . '/Install/Settings.install.php'; if (!\is_file($path)) { return; } @@ -120,7 +122,7 @@ abstract class InstallerAbstract */ protected static function createTables(DatabasePool $dbPool, ApplicationInfo $info) : void { - $path = __DIR__ . '/../../Web/' . $info->getInternalName() . '/Admin/Install/db.json'; + $path = static::PATH . '/Install/db.json'; if (!\is_file($path)) { return; } @@ -148,8 +150,9 @@ abstract class InstallerAbstract */ protected static function activate(DatabasePool $dbPool, ApplicationInfo $info) : void { - /** @var StatusAbstract $class */ - $class = '\\Web\\' . $info->getInternalName() . '\\Admin\\Status'; + $classPath = \substr(\realpath(static::PATH) . '/Status', \strlen(\realpath(__DIR__ . '/../../'))); + + $class = \str_replace('/', '\\', $classPath); $class::activate($dbPool, $info); } @@ -164,7 +167,9 @@ abstract class InstallerAbstract */ public static function reInit(ApplicationInfo $info) : void { - $class = '\\Web\\' . $info->getInternalName() . '\\Admin\\Status'; + $classPath = \substr(\realpath(static::PATH) . '/Status', \strlen(\realpath(__DIR__ . '/../../'))); + + $class = \str_replace('/', '\\', $classPath); $class::activateRoutes($info); $class::activateHooks($info); } diff --git a/Application/StatusAbstract.php b/Application/StatusAbstract.php index 6f81e2332..2ad56c754 100644 --- a/Application/StatusAbstract.php +++ b/Application/StatusAbstract.php @@ -33,6 +33,7 @@ use phpOMS\Utils\ArrayUtils; */ abstract class StatusAbstract { + public const PATH = ''; /** * Deactivate app. * @@ -62,7 +63,7 @@ abstract class StatusAbstract */ public static function activateRoutes(ApplicationInfo $appInfo = null) : void { - self::installRoutes(__DIR__ . '/../../Web/' . $appInfo->getInternalName() . '/Routes.php', __DIR__ . '/../../Web/' . $appInfo->getInternalName() . '/Admin/Install/Application/Routes.php'); + self::installRoutes(static::PATH . '/../Routes.php', static::PATH . '/../Admin/Install/Application/Routes.php'); } /** @@ -78,7 +79,7 @@ abstract class StatusAbstract */ public static function activateHooks(ApplicationInfo $appInfo = null) : void { - self::installRoutes(__DIR__ . '/../../Web/' . $appInfo->getInternalName() . '/Hooks.php', __DIR__ . '/../../Web/' . $appInfo->getInternalName() . '/Admin/Install/Application/Hooks.php'); + self::installRoutes(static::PATH . '/../Hooks.php', static::PATH . '/../Admin/Install/Application/Hooks.php'); } /** @@ -175,7 +176,7 @@ abstract class StatusAbstract */ public static function deactivateRoutes(ApplicationInfo $appInfo) : void { - self::installRoutes(__DIR__ . '/../../Web/' . $appInfo->getInternalName() . '/Routes.php', __DIR__ . '/../../Web/' . $appInfo->getInternalName() . '/Admin/Install/Application/Routes.php'); + self::installRoutes(static::PATH . '/../Routes.php', static::PATH . '/../Admin/Install/Application/Routes.php'); } /** @@ -191,7 +192,7 @@ abstract class StatusAbstract */ public static function deactivateHooks(ApplicationInfo $appInfo) : void { - self::installRoutes(__DIR__ . '/../../Web/' . $appInfo->getInternalName() . '/Hooks.php', __DIR__ . '/../../Web/' . $appInfo->getInternalName() . '/Admin/Install/Application/Hooks.php'); + self::installRoutes(static::PATH . '/../Hooks.php', static::PATH . '/../Admin/Install/Application/Hooks.php'); } /** diff --git a/Module/ModuleManager.php b/Module/ModuleManager.php index d1d3cbbbb..28ba9b0a1 100644 --- a/Module/ModuleManager.php +++ b/Module/ModuleManager.php @@ -336,7 +336,7 @@ final class ModuleManager * * @since 1.0.0 */ - private function loadInfo(string $module) : ?ModuleInfo + public function loadInfo(string $module) : ?ModuleInfo { $path = \realpath($oldPath = $this->modulePath . $module . '/info.json'); if ($path === false) { diff --git a/tests/Application/ApplicationInfoTest.php b/tests/Application/ApplicationInfoTest.php index a4b2784dc..162109db5 100644 --- a/tests/Application/ApplicationInfoTest.php +++ b/tests/Application/ApplicationInfoTest.php @@ -60,8 +60,8 @@ class ApplicationInfoTest extends \PHPUnit\Framework\TestCase $info = new ApplicationInfo(__DIR__ . '/info-test.json'); $info->load(); - $info->set('/name/internal', 'ABC'); - self::assertEquals('ABC', $info->getInternalName()); + $info->set('/name/internal', 'Testapp'); + self::assertEquals('Testapp', $info->getInternalName()); $info->update(); $info2 = new ApplicationInfo(__DIR__ . '/info-test.json'); diff --git a/tests/Application/ApplicationManagerTest.php b/tests/Application/ApplicationManagerTest.php index 688fd6a27..f2d2e1386 100644 --- a/tests/Application/ApplicationManagerTest.php +++ b/tests/Application/ApplicationManagerTest.php @@ -101,9 +101,7 @@ class ApplicationManagerTest extends \PHPUnit\Framework\TestCase */ public function testMissingApplicationInfoFile() : void { - $this->expectException(\phpOMS\System\File\PathException::class); - - self::assertFalse($this->appManager->install(__DIR__, __DIR__ . '/newapp')); + self::assertFalse($this->appManager->install(__DIR__, __DIR__ . '/newapp', __DIR__ . '/Apps/newapp')); } /** diff --git a/tests/Application/Testapp/Admin/Installer.php b/tests/Application/Testapp/Admin/Installer.php new file mode 100644 index 000000000..1787e50aa --- /dev/null +++ b/tests/Application/Testapp/Admin/Installer.php @@ -0,0 +1,30 @@ +select('a.test')->from('a')->where('a.test', '=', 1)->orderBy('a.test', 'ASC')->toSql()); $query = new Builder($this->con); - $sql = 'SELECT `a`.`test` FROM `a` WHERE `a`.`test` = 1 ORDER BY `a`.`test`, `a`.`test2` DESC;'; + $sql = 'SELECT `a`.`test` FROM `a` WHERE `a`.`test` = 1 ORDER BY `a`.`test` DESC, `a`.`test2` DESC;'; self::assertEquals($sql, $query->select('a.test')->from('a')->where('a.test', '=', 1)->orderBy(['a.test', 'a.test2'], ['DESC', 'DESC'])->toSql()); $query = new Builder($this->con); - $sql = 'SELECT `a`.`test` FROM `a` WHERE `a`.`test` = 1 ORDER BY `a`.`test`, `a`.`test2` ASC;'; + $sql = 'SELECT `a`.`test` FROM `a` WHERE `a`.`test` = 1 ORDER BY `a`.`test` ASC, `a`.`test2` ASC;'; self::assertEquals($sql, $query->select('a.test')->from('a')->where('a.test', '=', 1)->orderBy(['a.test', 'a.test2'], 'ASC')->toSql()); } @@ -508,16 +508,4 @@ class BuilderTest extends \PHPUnit\Framework\TestCase $query = new Builder($this->con, true); $query->join('b')->on('a', 'invalid', 'b'); } - - /** - * @testdox Invalid order column types throw a InvalidArgumentException - * @group framework - */ - public function testInvalidOrderColumnType() : void - { - $this->expectException(\InvalidArgumentException::class); - - $query = new Builder($this->con, true); - $query->orderBy('valid', ['invalid']); - } } diff --git a/tests/Module/ModuleManagerTest.php b/tests/Module/ModuleManagerTest.php index c047b51b8..d6e7a705a 100644 --- a/tests/Module/ModuleManagerTest.php +++ b/tests/Module/ModuleManagerTest.php @@ -15,12 +15,16 @@ declare(strict_types=1); namespace phpOMS\tests\Module; use Model\CoreSettings; +use Modules\Admin\Models\ModuleMapper; use phpOMS\Application\ApplicationAbstract; use phpOMS\Dispatcher\Dispatcher; use phpOMS\Message\Http\HttpRequest; use phpOMS\Module\ModuleManager; use phpOMS\Router\WebRouter; use phpOMS\Uri\HttpUri; +use Modules\Admin\Models\Module; +use phpOMS\Module\ModuleStatus; +use phpOMS\DataStorage\Database\Query\Builder; require_once __DIR__ . '/../Autoloader.php'; @@ -163,10 +167,44 @@ class ModuleManagerTest extends \PHPUnit\Framework\TestCase { $this->moduleManager->install('TestModule'); + $module = new Module(); + $module->id = 'TestModule'; + $module->name = 'TestModule'; + $module->path = 'TestModule'; + ModuleMapper::create($module); + self::assertTrue($this->moduleManager->deactivate('TestModule')); self::assertFalse($this->moduleManager->isActive('TestModule')); self::assertTrue($this->moduleManager->activate('TestModule')); + + // this is normally done in the ApiController + $module->setStatus(ModuleStatus::ACTIVE); + ModuleMapper::update($module); + + $queryLoad = new Builder($this->app->dbPool->get('insert')); + $queryLoad->insert('module_load_pid', 'module_load_type', 'module_load_from', 'module_load_for', 'module_load_file') + ->into('module_load'); + + $moduleInfo = $this->moduleManager->loadInfo('TestModule'); + + $load = $moduleInfo->getLoad(); + foreach ($load as $val) { + foreach ($val['pid'] as $pid) { + $queryLoad->values( + \sha1(\str_replace('/', '', $pid)), + (int) $val['type'], + $val['from'], + $val['for'], + $val['file'] + ); + } + } + + if (!empty($queryLoad->getValues())) { + $queryLoad->execute(); + } + self::assertTrue($this->moduleManager->isActive('TestModule')); }