Skip to content
Snippets Groups Projects
Commit 169ab35b authored by Giovannoni Laurent's avatar Giovannoni Laurent
Browse files

FEAT #6490 add testU and MVC slim for convert

parent 74cd7959
No related branches found
No related tags found
No related merge requests found
......@@ -70,7 +70,7 @@ class DocserverController
ValidatorModel::stringType($aArgs, ['path']);
if (!is_dir($aArgs['path'])) {
return ['errors' => '[createPathOnDocServer] Path does not exist'];
return ['errors' => '[createPathOnDocServer] Path does not exist ' . $aArgs['path']];
}
error_reporting(0);
......
......@@ -63,6 +63,10 @@ class ResControllerTest extends TestCase
$fileSource = 'test_source.txt';
if (file_exists($path . $fileSource)) {
unlink($path . $fileSource);
}
$fp = fopen($path . $fileSource, 'a');
fwrite($fp, 'a unit test');
fclose($fp);
......@@ -163,6 +167,10 @@ class ResControllerTest extends TestCase
$fileSource = 'test_source.txt';
if (file_exists($path . $fileSource)) {
unlink($path . $fileSource);
}
$fp = fopen($path . $fileSource, 'a');
fwrite($fp, 'a unit test');
fclose($fp);
......
......@@ -387,7 +387,9 @@
if($line->is_multi_docservers == "Y") {
if (
$adrTable == 'adr_x' ||
$adrTable == 'adr_attachments'
$adrTable == 'adr_letterbox' ||
$adrTable == 'adr_attachments' ||
$adrTable == 'adr_attachments_version'
) {
$query = "select res_id, docserver_id, path, filename, offset_doc, fingerprint, adr_priority from "
......
......@@ -25,6 +25,7 @@ namespace Convert\Controllers;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;
use Respect\Validation\Validator;
use Convert\Models\ProcessConvertModel;
use Core\Models\CoreConfigModel;
use Core\Models\DocserverModel;
......@@ -44,6 +45,28 @@ class ProcessConvertController
$this->libreOfficeExecutable = $libreOfficeExecutable;
}
public function create(RequestInterface $request, ResponseInterface $response)
{
$data = $request->getParams();
$check = Validator::notEmpty()->validate($data['collId']);
$check = $check && Validator::stringType()->notEmpty()->validate($data['resTable']);
$check = $check && Validator::stringType()->notEmpty()->validate($data['adrTable']);
$check = $check && Validator::intType()->notEmpty()->validate($data['resId']);
$check = $check && Validator::stringType()->notEmpty()->validate($data['tmpDir']);
if (!$check) {
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
}
$return = ProcessConvertController::convert($data);
if (empty($return) || !empty($return['errors'])) {
return $response->withStatus(500)->withJson(['errors' => '[ProcessConvertController create] ' . $return['errors']]);
}
return $response->withJson($return);
}
/**
* Ask for conversion
*
......@@ -125,7 +148,9 @@ class ProcessConvertController
'value' => '',
'error' => 'file not exists : ' . $resourcePath,
);
ProcessConvertModel::manageErrorOnDb($resTable, $resId, '-1');
ProcessConvertModel::manageErrorOnDb(
['resTable' => $resTable, 'resId' => $resId, 'result' => '-1']
);
return $returnArray;
}
//copy the resource on tmp directory
......@@ -136,7 +161,9 @@ class ProcessConvertController
'value' => '',
'error' => 'copy on tmp failed',
);
ProcessConvertModel::manageErrorOnDb($resTable, $resId, '-1');
ProcessConvertModel::manageErrorOnDb(
['resTable' => $resTable, 'resId' => $resId, 'result' => '-1']
);
return $returnArray;
}
//now do the conversion !
......@@ -157,7 +184,9 @@ class ProcessConvertController
}
if ($resultOfConversion['status'] <> '0') {
ProcessConvertModel::manageErrorOnDb($resTable, $resId, '-1');
ProcessConvertModel::manageErrorOnDb(
['resTable' => $resTable, 'resId' => $resId, 'result' => '-1']
);
return $resultOfConversion;
}
//copy the result on docserver
......@@ -173,6 +202,7 @@ class ProcessConvertController
'docserverTypeId' => 'CONVERT'
]);
if (empty($storeResult)) {
$returnArray = array(
'status' => '1',
......@@ -180,7 +210,9 @@ class ProcessConvertController
'error' => 'Ds of collection and ds type not found for convert:'
. $collId . ' CONVERT',
);
ProcessConvertModel::manageErrorOnDb($resTable, $resId, '-1');
ProcessConvertModel::manageErrorOnDb(
['resTable' => $resTable, 'resId' => $resId, 'result' => '-1']
);
return $returnArray;
}
......@@ -209,7 +241,9 @@ class ProcessConvertController
// LogsController::info(['message'=>var_export($resultCopyDs, true), 'code'=>7, ]);
if ($resultOfUpDb['status'] <> '0') {
ProcessConvertModel::manageErrorOnDb($resTable, $resId, '-1');
ProcessConvertModel::manageErrorOnDb(
['resTable' => $resTable, 'resId' => $resId, 'result' => '-1']
);
return $resultOfUpDb;
}
......
This diff is collapsed.
......@@ -155,20 +155,20 @@ class ProcessConvertModelAbstract
]);
$queryCpt = DatabaseModel::select([
'select' => ["coalesce(custom_t9, '0') as custom_t9"],
'select' => ["convert_attempts"],
'table' => [$aArgs['resTable']],
'where' => ['res_id = ?'],
'data' => [$aArgs['resId']],
]);
$cptConvert = $queryCpt[0]['custom_t9'] + 1;
$cptConvert = $queryCpt[0]['convert_attempts'] + 1;
DatabaseModel::update([
'table' => $aArgs['resTable'],
'set' => [
'convert_result' => '1',
'is_multi_docservers' => 'Y',
'custom_t9' => $cptConvert,
'convert_attempts' => $cptConvert,
],
'where' => ['res_id = ?'],
'data' => [$aArgs['resId']]
......@@ -197,15 +197,26 @@ class ProcessConvertModelAbstract
* @param string $result error code
* @return nothing
*/
public static function manageErrorOnDb(
$resTable,
$resId,
$result
) {
public static function manageErrorOnDb(array $aArgs = [])
{
$attemptsRecord = DatabaseModel::select([
'select' => ['convert_attempts'],
'table' => [$aArgs['resTable']],
'where' => ['res_id = ?'],
'data' => [$aArgs['resId']],
]);
if (empty($attemptsRecord)) {
$attempts = 0;
} else {
$attempts = $attemptsRecord[0]['convert_attempts'] + 1;
}
DatabaseModel::update([
'table' => $aArgs['resTable'],
'set' => [
'convert_result' => $result,
'convert_result' => $aArgs['result'],
'convert_attempts' => $attempts,
],
'where' => ['res_id = ?'],
'data' => [$aArgs['resId']]
......
......@@ -15,8 +15,190 @@
namespace Convert\Models;
use Core\Models\DatabaseModel;
use Core\Models\ValidatorModel;
use Core\Controllers\HistoryController;
class ProcessFulltextModelAbstract
{
/**
* Updating the database with the location information of the document on the
* new docserver
* @param string $collId collection
* @param string $resTable res table
* @param string $adrTable adr table
* @param bigint $resId Id of the resource to process
* @param docserver $docserver docserver object
* @param string $path location of the resource on the docserver
* @param string $fileName file name of the resource on the docserver
* @param complex $zendIndex zend index object
* @return array $returnArray the result
*/
public static function updateDatabase(array $aArgs = [])
{
try {
ValidatorModel::notEmpty($aArgs, ['collId']);
ValidatorModel::notEmpty($aArgs, ['resTable']);
ValidatorModel::notEmpty($aArgs, ['adrTable']);
ValidatorModel::intVal($aArgs, ['resId']);
ValidatorModel::notEmpty($aArgs, ['docserver']);
ValidatorModel::notEmpty($aArgs, ['path']);
ValidatorModel::notEmpty($aArgs, ['fileName']);
$aArgs['docserver']['path_template'] = str_replace(
DIRECTORY_SEPARATOR,
'#',
$aArgs['docserver']['path_template']
);
$aArgs['path'] = str_replace(
$aArgs['docserver']['path_template'],
'',
$aArgs['path']
);
DatabaseModel::update([
'table' => 'convert_stack',
'set' => [
'status' => 'P'
],
'where' => ['coll_id = ?', 'res_id = ?'],
'data' => [$aArgs['collId'], $aArgs['resId']]
]);
$returnAdr = DatabaseModel::select([
'select' => ['*'],
'table' => [$aArgs['adrTable']],
'where' => ['res_id = ?'],
'data' => [$aArgs['resId']],
'order' => ['adr_priority'],
]);
if (empty($returnAdr)) {
$returnRes = DatabaseModel::select([
'select' => ['docserver_id, path, filename, offset_doc, fingerprint'],
'table' => [$aArgs['resTable']],
'where' => ['res_id = ?'],
'data' => [$aArgs['resId']]
]);
$returnRes = $returnRes[0];
// LogsController::info(['message'=>$returnRes, 'code'=>8, ]);
$resDocserverId = $returnRes['docserver_id'];
$resPath = $returnRes['path'];
$resFilename = $returnRes['filename'];
$resOffsetDoc = $returnRes['offset_doc'];
$fingerprintInit = $returnRes['fingerprint'];
$returnDs = DatabaseModel::select([
'select' => ['adr_priority_number'],
'table' => ['docservers'],
'where' => ['docserver_id = ?'],
'data' => [$resDocserverId]
]);
DatabaseModel::insert([
'table' => $aArgs['adrTable'],
'columnsValues' => [
'res_id' => $aArgs['resId'],
'docserver_id' => $resDocserverId,
'path' => $resPath,
'filename' => $resFilename,
'offset_doc' => $resOffsetDoc,
'fingerprint' => $fingerprintInit,
'adr_priority' => $returnDs[0]['adr_priority_number'],
]
]);
}
$returnAdr = DatabaseModel::select([
'select' => ['*'],
'table' => [$aArgs['adrTable']],
'where' => ['res_id = ?', 'adr_type= ?'],
'data' => [$aArgs['resId'], 'TXT'],
]);
if (empty($returnAdr)) {
DatabaseModel::insert([
'table' => $aArgs['adrTable'],
'columnsValues' => [
'res_id' => $aArgs['resId'],
'docserver_id' => $aArgs['docserver']['docserver_id'],
'path' => $aArgs['path'],
'filename' => $aArgs['fileName'],
'offset_doc' => $offsetDoc,
'fingerprint' => $fingerprint,
'adr_priority' => $aArgs['docserver']['adr_priority_number'],
'adr_type' => 'TXT',
]
]);
} else {
DatabaseModel::update([
'table' => $aArgs['adrTable'],
'set' => [
'docserver_id' => $aArgs['docserver']['docserver_id'],
'path' => $aArgs['path'],
'filename' => $aArgs['fileName'],
'offset_doc' => $offsetDoc,
'fingerprint' => $fingerprint,
'adr_priority' => $aArgs['docserver']['adr_priority_number'],
],
'where' => ['res_id = ?', "adr_type = ?"],
'data' => [$aArgs['resId'], 'TXT']
]);
}
HistoryController::add([
'tableName' => $aArgs['resTable'],
'recordId' => (string) $aArgs['resId'],
'eventType' => 'ADD',
'info' => 'process fulltext done',
'moduleId' => 'convert',
'eventId' => 'fulltext',
]);
$queryCpt = DatabaseModel::select([
'select' => ["fulltext_attempts"],
'table' => [$aArgs['resTable']],
'where' => ['res_id = ?'],
'data' => [$aArgs['resId']],
]);
$cptFulltext = $queryCpt[0]['fulltext_attempts'] + 1;
if (!empty($aArgs['zendIndex'])) {
$fResult = 1;
} else {
$fResult = 0;
}
DatabaseModel::update([
'table' => $aArgs['resTable'],
'set' => [
'fulltext_result' => $fResult,
'is_multi_docservers' => 'Y',
'fulltext_attempts' => $cptFulltext,
],
'where' => ['res_id = ?'],
'data' => [$aArgs['resId']]
]);
$returnArray = array(
'status' => '0',
'value' => '',
'error' => '',
);
return $returnArray;
} catch (Exception $e) {
$returnArray = array(
'status' => '1',
'value' => '',
'error' => $e->getMessage(),
);
return $returnArray;
}
}
public static function getById(array $aArgs = [])
{
ValidatorModel::notEmpty($aArgs, ['resId']);
......@@ -35,4 +217,37 @@ class ProcessFulltextModelAbstract
return $aReturn[0];
}
/**
* Updating the database with the error code
* @param string $resTable res table
* @param bigint $resId Id of the resource to process
* @param string $result error code
* @return nothing
*/
public static function manageErrorOnDb(array $aArgs = [])
{
$attemptsRecord = DatabaseModel::select([
'select' => ['fulltext_attempts'],
'table' => [$aArgs['resTable']],
'where' => ['res_id = ?'],
'data' => [$aArgs['resId']],
]);
if (empty($attemptsRecord)) {
$attempts = 0;
} else {
$attempts = $attemptsRecord[0]['fulltext_attempts'] + 1;
}
DatabaseModel::update([
'table' => $aArgs['resTable'],
'set' => [
'fulltext_result' => $aArgs['result'],
'fulltext_attempts' => $attempts,
],
'where' => ['res_id = ?'],
'data' => [$aArgs['resId']]
]);
}
}
......@@ -9,24 +9,119 @@
namespace MaarchTest;
use PHPUnit\Framework\TestCase;
use MaarchTest\DocserverControllerTest;
class ProcessConvertTest extends TestCase
{
public function testconvert ()
{
$action = new \Core\Controllers\ResController();
$environment = \Slim\Http\Environment::mock(
[
'REQUEST_METHOD' => 'POST',
]
);
$path = $_SESSION['config']['tmppath'] . '/test/';
if (!is_dir($path)) {
mkdir($path);
}
$fileSource = 'test_source.txt';
if (file_exists($path . $fileSource)) {
unlink($path . $fileSource);
}
$fp = fopen($path . $fileSource, 'a');
fwrite($fp, 'a unit test for PHP CONVERSION lorem ipsum...');
fclose($fp);
$fileContent = file_get_contents($path . $fileSource, FILE_BINARY);
$encodedFile = base64_encode($fileContent);
//echo $encodedFile . PHP_EOL;exit;
$data = [];
array_push(
$data,
array(
'column' => 'subject',
'value' => 'UNIT TEST from slim',
'type' => 'string',
)
);
array_push(
$data,
array(
'column' => 'type_id',
'value' => 110,
'type' => 'integer',
)
);
array_push(
$data,
array(
'column' => 'custom_t1',
'value' => 'TEST',
'type' => 'string',
)
);
array_push(
$data,
array(
'column' => 'custom_t10',
'value' => 'lgi@maarch.org',
'type' => 'string',
)
);
$aArgs = [
'encodedFile' => $encodedFile,
'data' => $data,
'collId' => 'letterbox_coll',
'table' => 'res_letterbox',
'fileFormat' => 'txt',
'status' => 'new',
];
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = new \Slim\Http\Response();
$fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
$response = $action->create($fullRequest, $response);
$responseBody = json_decode((string)$response->getBody());
$resId = $responseBody->resId;
if (!defined("_RES_ID_TEST_CONVERT")) {
define("_RES_ID_TEST_CONVERT", $resId);
}
//real test
$request = \Slim\Http\Request::createFromEnvironment($environment);
$action = new \Convert\Controllers\ProcessConvertController();
$aArgs = [
'collId' => 'letterbox_coll',
'resTable' => 'res_letterbox',
'adrTable' => 'adr_letterbox',
'resId' => 137,
'resId' => _RES_ID_TEST_CONVERT,
'tmpDir' => $_SESSION['config']['tmppath']
];
$response = $action->convert($aArgs);
$response = new \Slim\Http\Response();
$fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
$response = $action->create($fullRequest, $response);
//var_dump($response);
$responseBody = json_decode((string)$response->getBody());
$status = $responseBody->status;
$this->assertArrayHasKey('status', $response);
$this->assertEquals('0', $status);
}
}
......@@ -9,25 +9,45 @@
namespace MaarchTest;
use PHPUnit\Framework\TestCase;
use MaarchTest\DocserverControllerTest;
class ProcessFulltextTest extends TestCase
{
public function testfulltext ()
{
if (!defined("_RES_ID_TEST_CONVERT")) {
define("_RES_ID_TEST_CONVERT", 100);
}
$action = new \Convert\Controllers\ProcessFulltextController();
$environment = \Slim\Http\Environment::mock(
[
'REQUEST_METHOD' => 'POST',
]
);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$aArgs = [
'collId' => 'letterbox_coll',
'resTable' => 'res_letterbox',
'adrTable' => 'adr_letterbox',
'resId' => 100,
'tmpDir' => $_SESSION['config']['tmppath']
'resId' => _RES_ID_TEST_CONVERT,
'tmpDir' => $_SESSION['config']['tmppath'],
'createZendIndex' => true
];
$response = $action->fulltext($aArgs);
$response = new \Slim\Http\Response();
$fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
$this->assertArrayHasKey('status', $response);
$response = $action->create($fullRequest, $response);
//var_dump($response);
$responseBody = json_decode((string)$response->getBody());
//var_dump($responseBody);
$status = $responseBody->status;
$this->assertEquals('0', $status);
}
}
......@@ -17,6 +17,38 @@ ALTER TABLE res_version_attachments DROP COLUMN IF EXISTS convert_result;
ALTER TABLE res_version_attachments ADD COLUMN convert_result character varying(10) DEFAULT NULL::character varying;
--convert attempts
ALTER TABLE res_attachments DROP COLUMN IF EXISTS convert_attempts;
ALTER TABLE res_attachments ADD COLUMN convert_attempts integer DEFAULT NULL::integer;
ALTER TABLE res_letterbox DROP COLUMN IF EXISTS convert_attempts;
ALTER TABLE res_letterbox ADD COLUMN convert_attempts integer DEFAULT NULL::integer;
ALTER TABLE res_x DROP COLUMN IF EXISTS convert_attempts;
ALTER TABLE res_x ADD COLUMN convert_attempts integer DEFAULT NULL::integer;
ALTER TABLE res_version_attachments DROP COLUMN IF EXISTS convert_attempts;
ALTER TABLE res_version_attachments ADD COLUMN convert_attempts integer DEFAULT NULL::integer;
--fulltext attempts
ALTER TABLE res_attachments DROP COLUMN IF EXISTS fulltext_attempts;
ALTER TABLE res_attachments ADD COLUMN fulltext_attempts integer DEFAULT NULL::integer;
ALTER TABLE res_letterbox DROP COLUMN IF EXISTS fulltext_attempts;
ALTER TABLE res_letterbox ADD COLUMN fulltext_attempts integer DEFAULT NULL::integer;
ALTER TABLE res_x DROP COLUMN IF EXISTS fulltext_attempts;
ALTER TABLE res_x ADD COLUMN fulltext_attempts integer DEFAULT NULL::integer;
ALTER TABLE res_version_attachments DROP COLUMN IF EXISTS fulltext_attempts;
ALTER TABLE res_version_attachments ADD COLUMN fulltext_attempts integer DEFAULT NULL::integer;
--tnl attempts
ALTER TABLE res_attachments DROP COLUMN IF EXISTS tnl_attempts;
ALTER TABLE res_attachments ADD COLUMN tnl_attempts integer DEFAULT NULL::integer;
ALTER TABLE res_letterbox DROP COLUMN IF EXISTS tnl_attempts;
ALTER TABLE res_letterbox ADD COLUMN tnl_attempts integer DEFAULT NULL::integer;
ALTER TABLE res_x DROP COLUMN IF EXISTS tnl_attempts;
ALTER TABLE res_x ADD COLUMN tnl_attempts integer DEFAULT NULL::integer;
ALTER TABLE res_version_attachments DROP COLUMN IF EXISTS tnl_attempts;
ALTER TABLE res_version_attachments ADD COLUMN tnl_attempts integer DEFAULT NULL::integer;
--thumbnails result
ALTER TABLE res_attachments DROP COLUMN IF EXISTS tnl_result;
ALTER TABLE res_attachments ADD COLUMN tnl_result character varying(10) DEFAULT NULL::character varying;
......@@ -99,9 +131,13 @@ WITH (OIDS=FALSE);
UPDATE docservers set docserver_type_id = 'DOC' where docserver_type_id <> 'TEMPLATES' and docserver_type_id <> 'TNL';
DELETE FROM docserver_types where docserver_type_id = 'DOC';
INSERT INTO docserver_types (docserver_type_id, docserver_type_label, enabled, is_container, container_max_number, is_compressed, compression_mode, is_meta, meta_template, is_logged, log_template, is_signed, fingerprint_mode)
VALUES ('DOC', 'Documents', 'Y', 'N', 0, 'N', 'NONE', 'N', 'NONE', 'N', 'NONE', 'Y', 'SHA512');
DELETE FROM docserver_types where docserver_type_id = 'CONVERT';
INSERT INTO docserver_types (docserver_type_id, docserver_type_label, enabled, is_container, container_max_number, is_compressed, compression_mode, is_meta, meta_template, is_logged, log_template, is_signed, fingerprint_mode)
VALUES ('CONVERT', 'Conversions', 'Y', 'N', 0, 'N', 'NONE', 'N', 'NONE', 'N', 'NONE', 'Y', 'NONE');
VALUES ('CONVERT', 'Conversions', 'Y', 'N', 0, 'N', 'NONE', 'N', 'NONE', 'N', 'NONE', 'Y', 'SHA256');
DELETE FROM docservers where docserver_id = 'CONVERT_MLB';
INSERT INTO docservers (docserver_id, docserver_type_id, device_label, is_readonly, enabled, size_limit_number, actual_size_number, path_template, ext_docserver_info, chain_before, chain_after, creation_date, closing_date, coll_id, priority_number, docserver_location_id, adr_priority_number)
......@@ -136,7 +172,7 @@ update docservers set docserver_id = 'TNL_MLB', priority_number = 12 where docse
DELETE FROM docserver_types where docserver_type_id = 'FULLTEXT';
INSERT INTO docserver_types (docserver_type_id, docserver_type_label, enabled, is_container, container_max_number, is_compressed, compression_mode, is_meta, meta_template, is_logged, log_template, is_signed, fingerprint_mode)
VALUES ('FULLTEXT', 'FULLTEXT', 'Y', 'N', 0, 'N', 'NONE', 'N', 'NONE', 'N', 'NONE', 'Y', 'NONE');
VALUES ('FULLTEXT', 'FULLTEXT', 'Y', 'N', 0, 'N', 'NONE', 'N', 'NONE', 'N', 'NONE', 'Y', 'SHA256');
DELETE FROM docservers where docserver_id = 'FULLTEXT_MLB';
INSERT INTO docservers (docserver_id, docserver_type_id, device_label, is_readonly, enabled, size_limit_number, actual_size_number, path_template, ext_docserver_info, chain_before, chain_after, creation_date, closing_date, coll_id, priority_number, docserver_location_id, adr_priority_number)
......
......@@ -10,8 +10,10 @@
<file>core/Test/StatusControllerTest.php</file>
<file>core/Test/UserControllerTest.php</file>
<file>core/Test/BasketControllerTest.php</file>
<!--file>modules/convert/Test/ProcessConvertTest.php</file-->
<!--file>modules/convert/Test/ProcessFulltextTest.php</file-->
<!-- <file>modules/convert/Test/ProcessConvertTest.php</file>
<file>modules/convert/Test/ProcessFulltextTest.php</file>
<file>modules/convert/Test/ProcessThumbnailsTest.php</file>
<file>modules/convert/Test/ProcessManageConvertTest.php</file> -->
</testsuite>
</testsuites>
<filter>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment