mirror of
https://github.com/Karaka-Management/oms-Exchange.git
synced 2026-02-16 09:48:41 +00:00
create tests
This commit is contained in:
parent
56010f1f3d
commit
5fbde09e26
|
|
@ -26,6 +26,9 @@ use phpOMS\Message\ResponseAbstract;
|
||||||
use phpOMS\Model\Message\FormValidation;
|
use phpOMS\Model\Message\FormValidation;
|
||||||
use phpOMS\System\File\Local\Directory;
|
use phpOMS\System\File\Local\Directory;
|
||||||
use phpOMS\System\MimeType;
|
use phpOMS\System\MimeType;
|
||||||
|
use phpOMS\Autoloader;
|
||||||
|
use phpOMS\Localization\L11nManager;
|
||||||
|
use phpOMS\DataStorage\Database\Connection\NullConnection;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Exchange controller class.
|
* Exchange controller class.
|
||||||
|
|
@ -85,8 +88,14 @@ final class ApiController extends Controller
|
||||||
{
|
{
|
||||||
/** @var \Modules\Exchange\Models\InterfaceManager $interface */
|
/** @var \Modules\Exchange\Models\InterfaceManager $interface */
|
||||||
$interface = InterfaceManagerMapper::get($request->getData('id'));
|
$interface = InterfaceManagerMapper::get($request->getData('id'));
|
||||||
$class = '\\Modules\\Exchange\\Interfaces\\' . $interface->getInterfacePath() . '\\Importer';
|
$dirname = \basename(\dirname($interface->getPath()));
|
||||||
$importer = new $class($this->app->dbPool->get());
|
|
||||||
|
if (!Autoloader::exists($class = '\\Modules\\Exchange\\Interfaces\\' . $dirname . '\\Importer')) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// @todo: implement real remote connection if available
|
||||||
|
$importer = new $class($this->app->dbPool->get(), new NullConnection(), new L11nManager($this->app->appName));
|
||||||
|
|
||||||
return $importer->importFromRequest($request);
|
return $importer->importFromRequest($request);
|
||||||
}
|
}
|
||||||
|
|
@ -135,7 +144,7 @@ final class ApiController extends Controller
|
||||||
}
|
}
|
||||||
|
|
||||||
$interface = new InterfaceManager(
|
$interface = new InterfaceManager(
|
||||||
__DIR__ . '/../Interfaces/' . $request->getData('interface') . '/interface.json'
|
\realpath(__DIR__ . '/../Interfaces/' . $request->getData('interface') . '/interface.json')
|
||||||
);
|
);
|
||||||
$interface->load();
|
$interface->load();
|
||||||
|
|
||||||
|
|
@ -160,11 +169,27 @@ final class ApiController extends Controller
|
||||||
public function apiExchangeExport(RequestAbstract $request, HttpResponse $response, $data = null) : void
|
public function apiExchangeExport(RequestAbstract $request, HttpResponse $response, $data = null) : void
|
||||||
{
|
{
|
||||||
$export = $this->exportDataFromRequest($request);
|
$export = $this->exportDataFromRequest($request);
|
||||||
foreach ($export['logs'] as $log) {
|
|
||||||
$this->createModel($request->header->account, $log, ExchangeLogMapper::class, 'export', $request->getOrigin());
|
if (!isset($export['type'], $export['logs'])) {
|
||||||
|
$response->header->status = RequestStatusCode::R_400;
|
||||||
|
|
||||||
|
$status = NotificationLevel::ERROR;
|
||||||
|
$message = 'Export failed.';
|
||||||
|
|
||||||
|
$response->set($request->uri->__toString(), [
|
||||||
|
'status' => $status,
|
||||||
|
'title' => 'Exchange',
|
||||||
|
'message' => $message,
|
||||||
|
]);
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($export['type'] === 'file') {
|
if ($export['type'] === 'file') {
|
||||||
|
foreach ($export['logs'] as $log) {
|
||||||
|
$this->createModel($request->header->account, $log, ExchangeLogMapper::class, 'export', $request->getOrigin());
|
||||||
|
}
|
||||||
|
|
||||||
$file = \explode('.', $export['name']);
|
$file = \explode('.', $export['name']);
|
||||||
|
|
||||||
$response->header->setDownloadable($file[0], $file[1]);
|
$response->header->setDownloadable($file[0], $file[1]);
|
||||||
|
|
@ -179,21 +204,7 @@ final class ApiController extends Controller
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
$response->set('export', $export['content']);
|
$response->set($request->uri->__toString(), $export['content']);
|
||||||
} else {
|
|
||||||
$status = NotificationLevel::ERROR;
|
|
||||||
$message = 'Export failed.';
|
|
||||||
|
|
||||||
if ($export['status']) {
|
|
||||||
$status = NotificationLevel::OK;
|
|
||||||
$message = 'Export succeeded.';
|
|
||||||
}
|
|
||||||
|
|
||||||
$response->set($request->uri->__toString(), [
|
|
||||||
'status' => $status,
|
|
||||||
'title' => 'Exchange',
|
|
||||||
'message' => $message,
|
|
||||||
]);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -210,8 +221,13 @@ final class ApiController extends Controller
|
||||||
{
|
{
|
||||||
/** @var \Modules\Exchange\Models\InterfaceManager $interface */
|
/** @var \Modules\Exchange\Models\InterfaceManager $interface */
|
||||||
$interface = InterfaceManagerMapper::get($request->getData('id'));
|
$interface = InterfaceManagerMapper::get($request->getData('id'));
|
||||||
$class = '\\Modules\\Exchange\\Interfaces\\' . $interface->getInterfacePath() . '\\Exporter';
|
$dirname = \basename(\dirname($interface->getPath()));
|
||||||
$exporter = new $class($this->app->dbPool->get());
|
|
||||||
|
if (!Autoloader::exists($class = '\\Modules\\Exchange\\Interfaces\\' . $dirname . '\\Exporter')) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
$exporter = new $class($this->app->dbPool->get(), new L11nManager($this->app->appName));
|
||||||
|
|
||||||
return $exporter->exportFromRequest($request);
|
return $exporter->exportFromRequest($request);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -114,7 +114,7 @@ final class Exporter extends ExporterAbstract
|
||||||
$modules = \scandir($basePath);
|
$modules = \scandir($basePath);
|
||||||
|
|
||||||
if ($modules === false) {
|
if ($modules === false) {
|
||||||
return [];
|
return []; // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($modules as $module) {
|
foreach ($modules as $module) {
|
||||||
|
|
@ -168,7 +168,7 @@ final class Exporter extends ExporterAbstract
|
||||||
$array = \reset($array);
|
$array = \reset($array);
|
||||||
|
|
||||||
if ($array === false) {
|
if ($array === false) {
|
||||||
continue;
|
continue; // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($len === 3) {
|
if ($len === 3) {
|
||||||
|
|
|
||||||
|
|
@ -131,7 +131,7 @@ final class Importer extends ImporterAbstract
|
||||||
|
|
||||||
$fp = \fopen($upload['file0']['path'] . '/' . $upload['file0']['filename'], 'r');
|
$fp = \fopen($upload['file0']['path'] . '/' . $upload['file0']['filename'], 'r');
|
||||||
if ($fp === false) {
|
if ($fp === false) {
|
||||||
return;
|
return; // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
$header = \fgetcsv($fp, 0, ';', '"');
|
$header = \fgetcsv($fp, 0, ';', '"');
|
||||||
|
|
@ -158,7 +158,7 @@ final class Importer extends ImporterAbstract
|
||||||
|
|
||||||
foreach ($supportedLanguages as $index => $language) {
|
foreach ($supportedLanguages as $index => $language) {
|
||||||
if (empty(\trim($language))) {
|
if (empty(\trim($language))) {
|
||||||
continue;
|
continue; // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
$languageArray[$line[0]][$line[1]][$line[2]][$line[3]][\trim($language)] = $translations[$index];
|
$languageArray[$line[0]][$line[1]][$line[2]][$line[3]][\trim($language)] = $translations[$index];
|
||||||
|
|
@ -185,7 +185,7 @@ final class Importer extends ImporterAbstract
|
||||||
|
|
||||||
$fp = \fopen($langFile, 'w+');
|
$fp = \fopen($langFile, 'w+');
|
||||||
if ($fp === false) {
|
if ($fp === false) {
|
||||||
continue;
|
continue; // @codeCoverageIgnore
|
||||||
}
|
}
|
||||||
|
|
||||||
\fwrite($fp,
|
\fwrite($fp,
|
||||||
|
|
|
||||||
|
|
@ -35,7 +35,7 @@ final class InterfaceManagerMapper extends DataMapperAbstract
|
||||||
protected static array $columns = [
|
protected static array $columns = [
|
||||||
'exchange_id' => ['name' => 'exchange_id', 'type' => 'int', 'internal' => 'id'],
|
'exchange_id' => ['name' => 'exchange_id', 'type' => 'int', 'internal' => 'id'],
|
||||||
'exchange_title' => ['name' => 'exchange_title', 'type' => 'string', 'internal' => 'info/name'],
|
'exchange_title' => ['name' => 'exchange_title', 'type' => 'string', 'internal' => 'info/name'],
|
||||||
'exchange_path' => ['name' => 'exchange_path', 'type' => 'string', 'internal' => 'info/path'],
|
'exchange_path' => ['name' => 'exchange_path', 'type' => 'string', 'internal' => 'path'],
|
||||||
'exchange_version' => ['name' => 'exchange_version', 'type' => 'string', 'internal' => 'info/version'],
|
'exchange_version' => ['name' => 'exchange_version', 'type' => 'string', 'internal' => 'info/version'],
|
||||||
'exchange_export' => ['name' => 'exchange_export', 'type' => 'bool', 'internal' => 'info/export'],
|
'exchange_export' => ['name' => 'exchange_export', 'type' => 'bool', 'internal' => 'info/export'],
|
||||||
'exchange_import' => ['name' => 'exchange_import', 'type' => 'bool', 'internal' => 'info/import'],
|
'exchange_import' => ['name' => 'exchange_import', 'type' => 'bool', 'internal' => 'info/import'],
|
||||||
|
|
|
||||||
209
tests/Controller/ApiControllerTest.php
Normal file
209
tests/Controller/ApiControllerTest.php
Normal file
|
|
@ -0,0 +1,209 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Orange Management
|
||||||
|
*
|
||||||
|
* PHP Version 8.0
|
||||||
|
*
|
||||||
|
* @package tests
|
||||||
|
* @copyright Dennis Eichhorn
|
||||||
|
* @license OMS License 1.0
|
||||||
|
* @version 1.0.0
|
||||||
|
* @link https://orange-management.org
|
||||||
|
*/
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Modules\Exchange\tests\Controller;
|
||||||
|
|
||||||
|
use Model\CoreSettings;
|
||||||
|
use Modules\Admin\Models\AccountPermission;
|
||||||
|
use phpOMS\DataStorage\Session\HttpSession;
|
||||||
|
use phpOMS\Account\Account;
|
||||||
|
use phpOMS\Account\AccountManager;
|
||||||
|
use phpOMS\Account\PermissionType;
|
||||||
|
use phpOMS\Application\ApplicationAbstract;
|
||||||
|
use phpOMS\Dispatcher\Dispatcher;
|
||||||
|
use phpOMS\Event\EventManager;
|
||||||
|
use phpOMS\Module\ModuleAbstract;
|
||||||
|
use phpOMS\Module\ModuleManager;
|
||||||
|
use phpOMS\Router\WebRouter;
|
||||||
|
use phpOMS\Utils\TestUtils;
|
||||||
|
use phpOMS\Message\Http\HttpRequest;
|
||||||
|
use phpOMS\Message\Http\HttpResponse;
|
||||||
|
use phpOMS\Message\Http\RequestStatusCode;
|
||||||
|
use phpOMS\Uri\HttpUri;
|
||||||
|
use phpOMS\System\MimeType;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @testdox Modules\Exchange\tests\Controller\ApiControllerTest: Exchange api controller
|
||||||
|
*
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
final class ApiControllerTest extends \PHPUnit\Framework\TestCase
|
||||||
|
{
|
||||||
|
protected ApplicationAbstract $app;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @var \Modules\Exchange\Controller\ApiController
|
||||||
|
*/
|
||||||
|
protected ModuleAbstract $module;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function setUp() : void
|
||||||
|
{
|
||||||
|
$this->app = new class() extends ApplicationAbstract
|
||||||
|
{
|
||||||
|
protected string $appName = 'Api';
|
||||||
|
};
|
||||||
|
|
||||||
|
$this->app->dbPool = $GLOBALS['dbpool'];
|
||||||
|
$this->app->orgId = 1;
|
||||||
|
$this->app->accountManager = new AccountManager($GLOBALS['session']);
|
||||||
|
$this->app->appSettings = new CoreSettings();
|
||||||
|
$this->app->moduleManager = new ModuleManager($this->app, __DIR__ . '/../../../../Modules/');
|
||||||
|
$this->app->dispatcher = new Dispatcher($this->app);
|
||||||
|
$this->app->eventManager = new EventManager($this->app->dispatcher);
|
||||||
|
$this->app->eventManager->importFromFile(__DIR__ . '/../../../../Web/Api/Hooks.php');
|
||||||
|
$this->app->sessionManager = new HttpSession(36000);
|
||||||
|
|
||||||
|
$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('Exchange');
|
||||||
|
|
||||||
|
TestUtils::setMember($this->module, 'app', $this->app);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers Modules\Exchange\Controller\ApiController
|
||||||
|
* @group module
|
||||||
|
*/
|
||||||
|
public function testInterfaceInstall() : void
|
||||||
|
{
|
||||||
|
$response = new HttpResponse();
|
||||||
|
$request = new HttpRequest(new HttpUri(''));
|
||||||
|
|
||||||
|
$request->header->account = 1;
|
||||||
|
$request->setData('interface', 'OMS');
|
||||||
|
|
||||||
|
$this->module->apiInterfaceInstall($request, $response);
|
||||||
|
self::assertEquals(1, $response->get('')['response']->getId());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers Modules\Exchange\Controller\ApiController
|
||||||
|
* @group module
|
||||||
|
*/
|
||||||
|
public function testInterfaceInstallInvalidData() : void
|
||||||
|
{
|
||||||
|
$response = new HttpResponse();
|
||||||
|
$request = new HttpRequest(new HttpUri(''));
|
||||||
|
|
||||||
|
$request->header->account = 1;
|
||||||
|
$request->setData('invalid', '1');
|
||||||
|
|
||||||
|
$this->module->apiInterfaceInstall($request, $response);
|
||||||
|
self::assertEquals(RequestStatusCode::R_400, $response->header->status);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers Modules\Exchange\Controller\ApiController
|
||||||
|
* @group module
|
||||||
|
*/
|
||||||
|
public function testExport() : void
|
||||||
|
{
|
||||||
|
$response = new HttpResponse();
|
||||||
|
$request = new HttpRequest(new HttpUri(''));
|
||||||
|
|
||||||
|
$request->header->account = 1;
|
||||||
|
$request->setData('id', '1');
|
||||||
|
$request->setData('type', 'language');
|
||||||
|
|
||||||
|
$this->module->apiExchangeExport($request, $response);
|
||||||
|
self::assertTrue(\strlen($response->get('')) > 500);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers Modules\Exchange\Controller\ApiController
|
||||||
|
* @group module
|
||||||
|
*/
|
||||||
|
public function testExportInvalidInterface() : void
|
||||||
|
{
|
||||||
|
$response = new HttpResponse();
|
||||||
|
$request = new HttpRequest(new HttpUri(''));
|
||||||
|
|
||||||
|
$request->header->account = 1;
|
||||||
|
$request->setData('id', '9999');
|
||||||
|
$request->setData('type', 'language');
|
||||||
|
|
||||||
|
$this->module->apiExchangeExport($request, $response);
|
||||||
|
self::assertEquals(RequestStatusCode::R_400, $response->header->status);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers Modules\Exchange\Controller\ApiController
|
||||||
|
* @group module
|
||||||
|
*/
|
||||||
|
public function testLanguageImport() : void
|
||||||
|
{
|
||||||
|
$response = new HttpResponse();
|
||||||
|
$request = new HttpRequest(new HttpUri(''));
|
||||||
|
|
||||||
|
$request->header->account = 1;
|
||||||
|
$request->setData('id', '1');
|
||||||
|
$request->setData('type', 'language');
|
||||||
|
|
||||||
|
if (!\is_file(__DIR__ . '/test_tmp.csv')) {
|
||||||
|
\copy(__DIR__ . '/../Interfaces/OMS/test.csv', __DIR__ . '/../Interfaces/OMS/test_tmp.csv');
|
||||||
|
}
|
||||||
|
|
||||||
|
TestUtils::setMember($request, 'files', [
|
||||||
|
'file0' => [
|
||||||
|
'name' => 'test.csv',
|
||||||
|
'type' => 'csv',
|
||||||
|
'tmp_name' => __DIR__ . '/../Interfaces/OMS/test_tmp.csv',
|
||||||
|
'error' => \UPLOAD_ERR_OK,
|
||||||
|
'size' => \filesize(__DIR__ . '/../Interfaces/OMS/test_tmp.csv'),
|
||||||
|
],
|
||||||
|
]);
|
||||||
|
|
||||||
|
$this->module->apiExchangeImport($request, $response);
|
||||||
|
self::assertEquals(
|
||||||
|
\date('Y-m-d'),
|
||||||
|
\date('Y-m-d', \filemtime(__DIR__ . '/../../../TestModule/Theme/Backend/Lang/en.lang.php'))
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @covers Modules\Exchange\Controller\ApiController
|
||||||
|
* @group module
|
||||||
|
*/
|
||||||
|
public function testImportInvalidInterface() : void
|
||||||
|
{
|
||||||
|
$response = new HttpResponse();
|
||||||
|
$request = new HttpRequest(new HttpUri(''));
|
||||||
|
|
||||||
|
$request->header->account = 1;
|
||||||
|
$request->setData('id', '9999');
|
||||||
|
|
||||||
|
$this->module->apiExchangeExport($request, $response);
|
||||||
|
self::assertEquals(RequestStatusCode::R_400, $response->header->status);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -19,6 +19,7 @@ use phpOMS\Localization\L11nManager;
|
||||||
use phpOMS\DataStorage\Database\Connection\NullConnection;
|
use phpOMS\DataStorage\Database\Connection\NullConnection;
|
||||||
use phpOMS\Uri\HttpUri;
|
use phpOMS\Uri\HttpUri;
|
||||||
use phpOMS\Message\Http\HttpRequest;
|
use phpOMS\Message\Http\HttpRequest;
|
||||||
|
use phpOMS\Utils\TestUtils;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
|
|
@ -47,7 +48,7 @@ final class ImporterTest extends \PHPUnit\Framework\TestCase
|
||||||
$request->setData('id', '123');
|
$request->setData('id', '123');
|
||||||
$request->setData('type', 'language');
|
$request->setData('type', 'language');
|
||||||
|
|
||||||
if (!\is_file()) {
|
if (!\is_file(__DIR__ . '/test_tmp.csv')) {
|
||||||
\copy(__DIR__ . '/test.csv', __DIR__ . '/test_tmp.csv');
|
\copy(__DIR__ . '/test.csv', __DIR__ . '/test_tmp.csv');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user