From 8f143193b3b37d789fb3b7d49b0734d96e9720d4 Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 10 Oct 2020 21:28:37 +0200 Subject: [PATCH] add tests --- Admin/Installer.php | 8 +- Controller/SearchController.php | 15 ++- Models/Navigation.php | 18 ++-- tests/Admin/AdminTest.php | 2 + tests/Controller/SearchControllerTest.php | 126 ++++++++++++++++++++++ 5 files changed, 153 insertions(+), 16 deletions(-) create mode 100644 tests/Controller/SearchControllerTest.php diff --git a/Admin/Installer.php b/Admin/Installer.php index f6d60c3..d14e337 100755 --- a/Admin/Installer.php +++ b/Admin/Installer.php @@ -48,19 +48,17 @@ final class Installer extends InstallerAbstract try { $dbPool->get()->con->query('select 1 from `nav`'); } catch (\Exception $e) { - return; + return; // @codeCoverageIgnore } $navFile = \file_get_contents($data['path'] ?? ''); - if ($navFile === false) { - throw new PathException($data['path'] ?? ''); + throw new PathException($data['path'] ?? ''); // @codeCoverageIgnore } $navData = \json_decode($navFile, true); - if ($navData === false) { - throw new \Exception(); + throw new \Exception(); // @codeCoverageIgnore } foreach ($navData as $link) { diff --git a/Controller/SearchController.php b/Controller/SearchController.php index ed0b633..53bb3e3 100755 --- a/Controller/SearchController.php +++ b/Controller/SearchController.php @@ -21,6 +21,7 @@ use phpOMS\Message\ResponseAbstract; use phpOMS\Model\Message\Redirect; use phpOMS\System\MimeType; use phpOMS\Uri\UriFactory; +use phpOMS\Message\Http\RequestStatusCode; /** * Search class. @@ -51,7 +52,14 @@ final class SearchController extends Controller /** @var \Modules\Navigation\Models\NavElement[] $elements */ $elements = NavElementMapper::getAll(); - $search = \strtolower(\explode(' ', $request->getData('search'))[1]); + $search = \mb_strtolower(\substr( + $request->getData('search'), + \stripos( + $request->getData('search'), + ' ', + \stripos($request->getData('search'), ':') + ) + 1 + )); $found = null; foreach ($elements as $element) { @@ -59,7 +67,7 @@ final class SearchController extends Controller continue; } - $name = \strtolower($this->app->l11nManager->getText( + $name = \mb_strtolower($this->app->l11nManager->getText( $response->getHeader()->getL11n()->getLanguage() ?? 'en', 'Navigation', '0', $element->name, @@ -75,6 +83,8 @@ final class SearchController extends Controller if ($found === null) { $this->fillJsonResponse($request, $response, NotificationLevel::WARNING, 'Command', 'Unknown command "' . $search . '"', $search); + $response->getHeader()->setStatusCode(RequestStatusCode::R_400); + return; } @@ -95,7 +105,6 @@ final class SearchController extends Controller * This should either get cached per user or maybe put into one large language file per language (like the routes). * * @since 1.0.0 - * @codeCoverageIgnore */ private function loadLanguage(RequestAbstract $request, ResponseAbstract $response, string $app) : void { diff --git a/Models/Navigation.php b/Models/Navigation.php index fde0af3..7ed427a 100755 --- a/Models/Navigation.php +++ b/Models/Navigation.php @@ -135,15 +135,17 @@ class Navigation */ private function setReadable(array &$nav, int $parent) : void { - if (isset($nav[$parent])) { - $nav[$parent][0]['readable'] = true; + if (!isset($nav[$parent])) { + return; + } - if (isset($nav[$nav[$parent][0]['nav_parent']]) - && (!isset($nav[$nav[$parent][0]['nav_parent']][0]['readable']) - || !$nav[$nav[$parent][0]['nav_parent']][0]['readable']) - ) { - $this->setReadable($nav, $nav[$parent][0]['nav_parent']); - } + $nav[$parent][0]['readable'] = true; + + if (isset($nav[$nav[$parent][0]['nav_parent']]) + && (!isset($nav[$nav[$parent][0]['nav_parent']][0]['readable']) + || !$nav[$nav[$parent][0]['nav_parent']][0]['readable']) + ) { + $this->setReadable($nav, $nav[$parent][0]['nav_parent']); } } diff --git a/tests/Admin/AdminTest.php b/tests/Admin/AdminTest.php index a4f1c05..980c90c 100755 --- a/tests/Admin/AdminTest.php +++ b/tests/Admin/AdminTest.php @@ -34,6 +34,8 @@ class AdminTest extends \PHPUnit\Framework\TestCase /** * Test if navigation model works correct * + * @covers Modules\Navigation\Models\Navigation + * * @group final * @group module */ diff --git a/tests/Controller/SearchControllerTest.php b/tests/Controller/SearchControllerTest.php new file mode 100644 index 0000000..3391d5a --- /dev/null +++ b/tests/Controller/SearchControllerTest.php @@ -0,0 +1,126 @@ +app = new class() extends ApplicationAbstract + { + protected string $appName = 'Search'; + }; + + $this->app->dbPool = $GLOBALS['dbpool']; + $this->app->orgId = 1; + $this->app->accountManager = new AccountManager($GLOBALS['session']); + $this->app->appSettings = new CoreSettings($this->app->dbPool->get()); + $this->app->moduleManager = new ModuleManager($this->app, __DIR__ . '/../../../../Modules'); + $this->app->dispatcher = new Dispatcher($this->app); + $this->app->l11nManager = new L11nManager($this->app->appName); + $this->app->eventManager = new EventManager($this->app->dispatcher); + $this->app->eventManager->importFromFile(__DIR__ . '/../../../../Web/Api/Hooks.php'); + + $account = new Account(); + TestUtils::setMember($account, 'id', 1); + + $permission = new AccountPermission(); + $permission->setUnit(1); + $permission->setApp('backend'); + $permission->setPermission( + PermissionType::READ + | PermissionType::CREATE + | PermissionType::MODIFY + | PermissionType::DELETE + | PermissionType::PERMISSION + ); + + $account->addPermission($permission); + + $this->app->accountManager->add($account); + $this->app->router = new WebRouter(); + + $this->module = $this->app->moduleManager->get('Navigation'); + + TestUtils::setMember($this->module, 'app', $this->app); + } + + /** + * @covers Modules\Navigation\Controller\SearchController + * @group module + */ + public function testGotoSearch() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('https://127.0.0.1/en/backend')); + $request->createRequestHashs(2); + + $request->getHeader()->setAccount(1); + $request->setData('search', ':goto General'); + $request->setData('app', 'Backend'); + + $this->module->searchGoto($request, $response); + self::assertInstanceOf(Redirect::class, $response->get('https://127.0.0.1/en/backend')); + } + + /** + * @covers Modules\Navigation\Controller\SearchController + * @group module + */ + public function testInvalidGotoSearch() : void + { + $response = new HttpResponse(); + $request = new HttpRequest(new HttpUri('https://127.0.0.1/en/backend')); + $request->createRequestHashs(0); + + $request->getHeader()->setAccount(1); + $request->setData('search', ':goto Invalid'); + $request->setData('app', 'Backend'); + + $this->module->searchGoto($request, $response); + self::assertEquals(RequestStatusCode::R_400, $response->getHeader()->getStatusCode()); + } +}