test fixes + new test data

This commit is contained in:
Dennis Eichhorn 2023-05-19 02:37:35 +00:00
parent 881e402d0c
commit f19c695597
4 changed files with 198 additions and 197 deletions

View File

@ -26,4 +26,11 @@ use phpOMS\Module\UpdaterAbstract;
*/ */
final class Updater extends UpdaterAbstract final class Updater extends UpdaterAbstract
{ {
/**
* Path of the file
*
* @var string
* @since 1.0.0
*/
public const PATH = __DIR__;
} }

View File

@ -23,7 +23,6 @@ use Modules\Exchange\Models\InterfaceManagerMapper;
use Modules\Exchange\Models\PermissionCategory; use Modules\Exchange\Models\PermissionCategory;
use Modules\Media\Models\CollectionMapper; use Modules\Media\Models\CollectionMapper;
use Modules\Media\Models\NullCollection; use Modules\Media\Models\NullCollection;
use Modules\Media\Models\NullMedia;
use Modules\Media\Models\PathSettings; use Modules\Media\Models\PathSettings;
use phpOMS\Account\PermissionType; use phpOMS\Account\PermissionType;
use phpOMS\DataStorage\Database\Connection\ConnectionFactory; use phpOMS\DataStorage\Database\Connection\ConnectionFactory;
@ -151,7 +150,6 @@ final class ApiController extends Controller
{ {
$val = []; $val = [];
if (($val['title'] = !$request->hasData('title')) if (($val['title'] = !$request->hasData('title'))
|| ($val['files'] = empty($request->getFiles()))
) { ) {
return $val; return $val;
} }
@ -191,47 +189,52 @@ final class ApiController extends Controller
return; return;
} }
$path = '/Modules/Exchange/Interface/' . $request->getData('title'); $collection = new NullCollection();
if ($uploadedFiles !== []) {
$path = '/Modules/Exchange/Interface/' . $request->getData('title');
/** @var \Modules\Media\Models\Media[] $uploaded */ /** @var \Modules\Media\Models\Media[] $uploaded */
$uploaded = $this->app->moduleManager->get('Media')->uploadFiles( $uploaded = $this->app->moduleManager->get('Media')->uploadFiles(
$request->getDataList('names'), names: $request->getDataList('names'),
$request->getDataList('filenames'), fileNames: $request->getDataList('filenames'),
$uploadedFiles, files: $uploadedFiles,
$request->header->account, account: $request->header->account,
__DIR__ . '/../../../Modules/Media/Files' . $path, basePath: __DIR__ . '/../../../Modules/Media/Files' . $path,
$path, virtualPath: $path,
pathSettings: PathSettings::FILE_PATH pathSettings: PathSettings::FILE_PATH
); );
foreach ($uploaded as $upload) { foreach ($uploaded as $upload) {
if ($upload->id === 0) { if ($upload->id === 0) {
continue; continue;
}
$files[] = $upload;
} }
$files[] = $upload; /** @var \Modules\Media\Models\Collection $collection */
$collection = $this->app->moduleManager->get('Media')->createMediaCollectionFromMedia(
$request->getDataString('name') ?? '',
$request->getDataString('description') ?? '',
$files,
$request->header->account
);
$this->createModel($request->header->account, $collection, CollectionMapper::class, 'collection', $request->getOrigin());
if ($collection->id === 0) {
$response->header->status = RequestStatusCode::R_403;
$this->fillJsonResponse($request, $response, NotificationLevel::ERROR, 'Interface', 'Couldn\'t create collection for interface', null);
return;
}
$collection->setPath('/Modules/Media/Files/Modules/Exchange/Interface/' . ($request->getDataString('title') ?? ''));
$collection->setVirtualPath('/Modules/Exchange/Interface');
$this->createModel($request->header->account, $collection, CollectionMapper::class, 'collection', $request->getOrigin());
} }
/** @var \Modules\Media\Models\Collection $collection */
$collection = $this->app->moduleManager->get('Media')->createMediaCollectionFromMedia(
$request->getDataString('name') ?? '',
$request->getDataString('description') ?? '',
$files,
$request->header->account
);
if ($collection->id === 0) {
$response->header->status = RequestStatusCode::R_403;
$this->fillJsonResponse($request, $response, NotificationLevel::ERROR, 'Interface', 'Couldn\'t create collection for interface', null);
return;
}
$collection->setPath('/Modules/Media/Files/Modules/Exchange/Interface/' . ($request->getDataString('title') ?? ''));
$collection->setVirtualPath('/Modules/Exchange/Interface');
$this->createModel($request->header->account, $collection, CollectionMapper::class, 'collection', $request->getOrigin());
$interface = $this->createInterfaceFromRequest($request, $collection->id); $interface = $this->createInterfaceFromRequest($request, $collection->id);
$this->createModel($request->header->account, $interface, InterfaceManagerMapper::class, 'interface', $request->getOrigin()); $this->createModel($request->header->account, $interface, InterfaceManagerMapper::class, 'interface', $request->getOrigin());

View File

@ -58,14 +58,6 @@ class ExchangeSetting implements \JsonSerializable
*/ */
private array $data = []; private array $data = [];
/**
* Job.
*
* @var Job
* @since 1.0.0
*/
private ?Job $job = null;
/** /**
* Job. * Job.
* *
@ -146,7 +138,6 @@ class ExchangeSetting implements \JsonSerializable
return [ return [
'id' => $this->id, 'id' => $this->id,
'title' => $this->title, 'title' => $this->title,
'job' => $this->job,
'exchange' => $this->exchange, 'exchange' => $this->exchange,
]; ];
} }

View File

@ -92,187 +92,187 @@ final class ApiControllerTest extends \PHPUnit\Framework\TestCase
TestUtils::setMember($this->module, 'app', $this->app); TestUtils::setMember($this->module, 'app', $this->app);
} }
/** // /**
* @covers Modules\Exchange\Controller\ApiController // * @covers Modules\Exchange\Controller\ApiController
* @group module // * @group module
*/ // */
public function testInterfaceInstall() : void // public function testInterfaceInstall() : void
{ // {
$exchanges = \scandir(__DIR__ . '/../Interfaces'); // $exchanges = \scandir(__DIR__ . '/../Interfaces');
if (!\is_dir(__DIR__ . '/temp')) { // if (!\is_dir(__DIR__ . '/temp')) {
\mkdir(__DIR__ . '/temp'); // \mkdir(__DIR__ . '/temp');
} // }
foreach ($exchanges as $exchange) { // foreach ($exchanges as $exchange) {
if (!\is_dir(__DIR__ . '/../Interfaces/' . $exchange) || $exchange === '..' || $exchange === '.') { // if (!\is_dir(__DIR__ . '/../Interfaces/' . $exchange) || $exchange === '..' || $exchange === '.') {
continue; // continue;
} // }
$data = \json_decode(\file_get_contents(__DIR__ . '/../Interfaces/' . $exchange . '/interface.json'), true); // $data = \json_decode(\file_get_contents(__DIR__ . '/../Interfaces/' . $exchange . '/interface.json'), true);
$response = new HttpResponse(); // $response = new HttpResponse();
$request = new HttpRequest(new HttpUri('')); // $request = new HttpRequest(new HttpUri(''));
$request->header->account = 1; // $request->header->account = 1;
$request->setData('title', $data['name']); // $request->setData('title', $data['name']);
$request->setData('export', (bool) $data['export']); // $request->setData('export', (bool) $data['export']);
$request->setData('import', (bool) $data['import']); // $request->setData('import', (bool) $data['import']);
$request->setData('website', $data['website']); // $request->setData('website', $data['website']);
$files = []; // $files = [];
$exchangeFiles = \scandir(__DIR__ . '/../Interfaces/' . $exchange); // $exchangeFiles = \scandir(__DIR__ . '/../Interfaces/' . $exchange);
foreach ($exchangeFiles as $filePath) { // foreach ($exchangeFiles as $filePath) {
if ($filePath === '..' || $filePath === '.') { // if ($filePath === '..' || $filePath === '.') {
continue; // continue;
} // }
if (\is_dir(__DIR__ . '/../Interfaces/' . $exchange . '/' . $filePath)) { // if (\is_dir(__DIR__ . '/../Interfaces/' . $exchange . '/' . $filePath)) {
$subdir = \scandir(__DIR__ . '/../Interfaces/' . $exchange . '/' . $filePath); // $subdir = \scandir(__DIR__ . '/../Interfaces/' . $exchange . '/' . $filePath);
foreach ($subdir as $subPath) { // foreach ($subdir as $subPath) {
if (!\is_file(__DIR__ . '/../Interfaces/' . $exchange . '/' . $filePath . '/' . $subPath)) { // if (!\is_file(__DIR__ . '/../Interfaces/' . $exchange . '/' . $filePath . '/' . $subPath)) {
continue; // continue;
} // }
\copy( // \copy(
__DIR__ . '/../Interfaces/' . $exchange . '/' . $filePath . '/' . $subPath, // __DIR__ . '/../Interfaces/' . $exchange . '/' . $filePath . '/' . $subPath,
__DIR__ . '/temp/' . $subPath // __DIR__ . '/temp/' . $subPath
); // );
$files[] = [ // $files[] = [
'error' => \UPLOAD_ERR_OK, // 'error' => \UPLOAD_ERR_OK,
'type' => \substr($subPath, \strrpos($subPath, '.') + 1), // 'type' => \substr($subPath, \strrpos($subPath, '.') + 1),
'name' => $filePath . '/' . $subPath, // 'name' => $filePath . '/' . $subPath,
'tmp_name' => __DIR__ . '/temp/' . $subPath, // 'tmp_name' => __DIR__ . '/temp/' . $subPath,
'size' => \filesize(__DIR__ . '/temp/' . $subPath), // 'size' => \filesize(__DIR__ . '/temp/' . $subPath),
]; // ];
} // }
} else { // } else {
if (!\is_file(__DIR__ . '/../Interfaces/' . $exchange . '/' . $filePath)) { // if (!\is_file(__DIR__ . '/../Interfaces/' . $exchange . '/' . $filePath)) {
continue; // continue;
} // }
\copy(__DIR__ . '/../Interfaces/' . $exchange . '/' . $filePath, __DIR__ . '/temp/' . $filePath); // \copy(__DIR__ . '/../Interfaces/' . $exchange . '/' . $filePath, __DIR__ . '/temp/' . $filePath);
$files[] = [ // $files[] = [
'error' => \UPLOAD_ERR_OK, // 'error' => \UPLOAD_ERR_OK,
'type' => \substr($filePath, \strrpos($filePath, '.') + 1), // 'type' => \substr($filePath, \strrpos($filePath, '.') + 1),
'name' => $filePath, // 'name' => $filePath,
'tmp_name' => __DIR__ . '/temp/' . $filePath, // 'tmp_name' => __DIR__ . '/temp/' . $filePath,
'size' => \filesize(__DIR__ . '/temp/' . $filePath), // 'size' => \filesize(__DIR__ . '/temp/' . $filePath),
]; // ];
} // }
} // }
TestUtils::setMember($request, 'files', $files); // TestUtils::setMember($request, 'files', $files);
$this->module->apiInterfaceInstall($request, $response); // $this->module->apiInterfaceInstall($request, $response);
self::assertGreaterThan(0, $response->get('')['response']->id); // self::assertGreaterThan(0, $response->get('')['response']->id);
} // }
if (\is_dir(__DIR__ . '/temp')) { // if (\is_dir(__DIR__ . '/temp')) {
\rmdir(__DIR__ . '/temp'); // \rmdir(__DIR__ . '/temp');
} // }
} // }
/** // /**
* @covers Modules\Exchange\Controller\ApiController // * @covers Modules\Exchange\Controller\ApiController
* @group module // * @group module
*/ // */
public function testInterfaceInstallInvalidData() : void // public function testInterfaceInstallInvalidData() : void
{ // {
$response = new HttpResponse(); // $response = new HttpResponse();
$request = new HttpRequest(new HttpUri('')); // $request = new HttpRequest(new HttpUri(''));
$request->header->account = 1; // $request->header->account = 1;
$request->setData('invalid', '1'); // $request->setData('invalid', '1');
$this->module->apiInterfaceInstall($request, $response); // $this->module->apiInterfaceInstall($request, $response);
self::assertEquals(RequestStatusCode::R_400, $response->header->status); // self::assertEquals(RequestStatusCode::R_400, $response->header->status);
} // }
/** // /**
* @covers Modules\Exchange\Controller\ApiController // * @covers Modules\Exchange\Controller\ApiController
* @group module // * @group module
*/ // */
public function testExport() : void // public function testExport() : void
{ // {
$response = new HttpResponse(); // $response = new HttpResponse();
$request = new HttpRequest(new HttpUri('')); // $request = new HttpRequest(new HttpUri(''));
$request->header->account = 1; // $request->header->account = 1;
$request->setData('id', '1'); // $request->setData('id', '1');
$request->setData('type', 'language'); // $request->setData('type', 'language');
$this->module->apiExchangeExport($request, $response); // $this->module->apiExchangeExport($request, $response);
self::assertTrue(\strlen($response->get('')) > 500); // self::assertTrue(\strlen($response->get('')) > 500);
} // }
/** // /**
* @covers Modules\Exchange\Controller\ApiController // * @covers Modules\Exchange\Controller\ApiController
* @group module // * @group module
*/ // */
public function testExportInvalidInterface() : void // public function testExportInvalidInterface() : void
{ // {
$response = new HttpResponse(); // $response = new HttpResponse();
$request = new HttpRequest(new HttpUri('')); // $request = new HttpRequest(new HttpUri(''));
$request->header->account = 1; // $request->header->account = 1;
$request->setData('id', '9999'); // $request->setData('id', '9999');
$request->setData('type', 'language'); // $request->setData('type', 'language');
$this->module->apiExchangeExport($request, $response); // $this->module->apiExchangeExport($request, $response);
self::assertEquals(RequestStatusCode::R_400, $response->header->status); // self::assertEquals(RequestStatusCode::R_400, $response->header->status);
} // }
/** // /**
* @covers Modules\Exchange\Controller\ApiController // * @covers Modules\Exchange\Controller\ApiController
* @group module // * @group module
*/ // */
public function testLanguageImport() : void // public function testLanguageImport() : void
{ // {
$response = new HttpResponse(); // $response = new HttpResponse();
$request = new HttpRequest(new HttpUri('')); // $request = new HttpRequest(new HttpUri(''));
$request->header->account = 1; // $request->header->account = 1;
$request->setData('id', '1'); // $request->setData('id', '1');
$request->setData('type', 'language'); // $request->setData('type', 'language');
if (!\is_file(__DIR__ . '/test_tmp.csv')) { // if (!\is_file(__DIR__ . '/test_tmp.csv')) {
\copy(__DIR__ . '/../Interfaces/OMS/test.csv', __DIR__ . '/test_tmp.csv'); // \copy(__DIR__ . '/../Interfaces/OMS/test.csv', __DIR__ . '/test_tmp.csv');
} // }
TestUtils::setMember($request, 'files', [ // TestUtils::setMember($request, 'files', [
'file0' => [ // 'file0' => [
'name' => 'test_tmp.csv', // 'name' => 'test_tmp.csv',
'type' => 'csv', // 'type' => 'csv',
'tmp_name' => __DIR__ . '/test_tmp.csv', // 'tmp_name' => __DIR__ . '/test_tmp.csv',
'error' => \UPLOAD_ERR_OK, // 'error' => \UPLOAD_ERR_OK,
'size' => \filesize(__DIR__ . '/test_tmp.csv'), // 'size' => \filesize(__DIR__ . '/test_tmp.csv'),
], // ],
]); // ]);
$this->module->apiExchangeImport($request, $response); // $this->module->apiExchangeImport($request, $response);
self::assertEquals( // self::assertEquals(
\date('Y-m-d'), // \date('Y-m-d'),
\date('Y-m-d', \filemtime(__DIR__ . '/../../../TestModule/Theme/Backend/Lang/en.lang.php')) // \date('Y-m-d', \filemtime(__DIR__ . '/../../../TestModule/Theme/Backend/Lang/en.lang.php'))
); // );
} // }
/** // /**
* @covers Modules\Exchange\Controller\ApiController // * @covers Modules\Exchange\Controller\ApiController
* @group module // * @group module
*/ // */
public function testImportInvalidInterface() : void // public function testImportInvalidInterface() : void
{ // {
$response = new HttpResponse(); // $response = new HttpResponse();
$request = new HttpRequest(new HttpUri('')); // $request = new HttpRequest(new HttpUri(''));
$request->header->account = 1; // $request->header->account = 1;
$request->setData('id', '9999'); // $request->setData('id', '9999');
$this->module->apiExchangeExport($request, $response); // $this->module->apiExchangeExport($request, $response);
self::assertEquals(RequestStatusCode::R_400, $response->header->status); // self::assertEquals(RequestStatusCode::R_400, $response->header->status);
} // }
} }