From 4e2fe1529e771e23577eeefaf02bd34b4bfeb5b5 Mon Sep 17 00:00:00 2001 From: Guillaume Heurtier <guillaume.heurtier@maarch.org> Date: Fri, 24 Apr 2020 18:11:05 +0200 Subject: [PATCH] FEAT #13676 TIME 8:45 Start improving resources tests --- .../resource/controllers/ResController.php | 4 +- .../app/indexing/IndexingControllerTest.php | 82 ++- .../resource/FolderPrintControllerTest.php | 430 ++++++++++- .../app/resource/ResControllerTest.php | 685 ++++++++++++++++-- 4 files changed, 1135 insertions(+), 66 deletions(-) diff --git a/src/app/resource/controllers/ResController.php b/src/app/resource/controllers/ResController.php index 49e15dd85d5..84d8ecb96ea 100755 --- a/src/app/resource/controllers/ResController.php +++ b/src/app/resource/controllers/ResController.php @@ -242,7 +242,7 @@ class ResController extends ResourceControlController public function update(Request $request, Response $response, array $args) { if (!Validator::intVal()->validate($args['resId'])) { - return ['errors' => 'Route resId is not an integer']; + return $response->withStatus(400)->withJson(['errors' => 'Route resId is not an integer']); } elseif (!PrivilegeController::canUpdateResource(['userId' => $GLOBALS['id'], 'resId' => $args['resId']])) { return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); } @@ -1236,7 +1236,7 @@ class ResController extends ResourceControlController public function getProcessingData(Request $request, Response $response, array $args) { if (!Validator::intVal()->validate($args['groupId'])) { - return $response->withStatus(403)->withJson(['errors' => 'resId param is not an integer']); + return $response->withStatus(403)->withJson(['errors' => 'groupId param is not an integer']); } if (!Validator::intVal()->validate($args['userId'])) { return $response->withStatus(403)->withJson(['errors' => 'userId param is not an integer']); diff --git a/test/unitTests/app/indexing/IndexingControllerTest.php b/test/unitTests/app/indexing/IndexingControllerTest.php index c787ca242ed..d9c5cb59ce5 100755 --- a/test/unitTests/app/indexing/IndexingControllerTest.php +++ b/test/unitTests/app/indexing/IndexingControllerTest.php @@ -39,9 +39,24 @@ class IndexingControllerTest extends TestCase //ERROR $response = $indexingController->getIndexingActions($request, new \Slim\Http\Response(), ['groupId' => 99999]); + $this->assertSame(403, $response->getStatusCode()); $responseBody = json_decode((string)$response->getBody()); $this->assertSame('This user is not in this group', $responseBody->errors); + $response = $indexingController->getIndexingActions($request, new \Slim\Http\Response(), ['groupId' => 'wrong format']); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody()); + $this->assertSame('Param groupId must be an integer val', $responseBody->errors); + + $GLOBALS['login'] = 'ddur'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $indexingController->getIndexingActions($request, new \Slim\Http\Response(), ['groupId' => 8]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody()); + $this->assertSame('This group can not index document', $responseBody->errors); + $GLOBALS['login'] = 'superadmin'; $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); $GLOBALS['id'] = $userInfo['id']; @@ -73,10 +88,15 @@ class IndexingControllerTest extends TestCase } //ERROR - $response = $indexingController->getIndexingActions($request, new \Slim\Http\Response(), ['groupId' => 99999]); + $response = $indexingController->getIndexingEntities($request, new \Slim\Http\Response(), ['groupId' => 99999]); $responseBody = json_decode((string)$response->getBody()); $this->assertSame('This user is not in this group', $responseBody->errors); + $response = $indexingController->getIndexingEntities($request, new \Slim\Http\Response(), ['groupId' => 'wrong format']); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody()); + $this->assertSame('Param groupId must be an integer val', $responseBody->errors); + $GLOBALS['login'] = 'superadmin'; $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); $GLOBALS['id'] = $userInfo['id']; @@ -200,4 +220,64 @@ class IndexingControllerTest extends TestCase $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); $GLOBALS['id'] = $userInfo['id']; } + + public function testSetAction() + { + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $indexingController = new \Resource\controllers\IndexingController(); + + // GET + // ERROR + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $response = $indexingController->setAction($request, new \Slim\Http\Response(), []); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Body resource is empty or not an integer', $responseBody['errors']); + + + $body = [ + 'resource' => 1 + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + $response = $indexingController->setAction($fullRequest, new \Slim\Http\Response(), ['groupId' => 10000 ]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody()); + $this->assertSame('Route groupId does not exist', $responseBody->errors); + + $body = [ + 'resource' => 1 + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + $response = $indexingController->setAction($fullRequest, new \Slim\Http\Response(), ['groupId' => 1 ]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody()); + $this->assertSame('Group is not linked to this user', $responseBody->errors); + + $body = [ + 'resource' => 1 + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + $response = $indexingController->setAction($fullRequest, new \Slim\Http\Response(), ['groupId' => 2, 'actionId' => 2]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody()); + $this->assertSame('Action is not linked to this group', $responseBody->errors); + + $body = [ + 'resource' => 1 + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + $response = $indexingController->setAction($fullRequest, new \Slim\Http\Response(), ['groupId' => 2, 'actionId' => 22]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody()); + $this->assertSame('Resource does not exist', $responseBody->errors); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + } } diff --git a/test/unitTests/app/resource/FolderPrintControllerTest.php b/test/unitTests/app/resource/FolderPrintControllerTest.php index 507394dd6db..3169adcc62c 100755 --- a/test/unitTests/app/resource/FolderPrintControllerTest.php +++ b/test/unitTests/app/resource/FolderPrintControllerTest.php @@ -25,13 +25,13 @@ class FolderPrintControllerTest extends TestCase $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $aArgs = [ + $body = [ 'value' => "Test d'ajout d'une note par php unit", 'entities' => ['COU', 'CAB'], 'resId' => $GLOBALS['resources'][0] ]; - $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); $response = $noteController->create($fullRequest, new \Slim\Http\Response()); $responseBody = json_decode((string)$response->getBody()); @@ -47,7 +47,7 @@ class FolderPrintControllerTest extends TestCase $fileContent = file_get_contents('test/unitTests/samples/test.txt'); $encodedFile = base64_encode($fileContent); - $aArgs = [ + $body = [ 'title' => 'Nulle pierre ne peut être polie sans friction, nul homme ne peut parfaire son expérience sans épreuve.', 'type' => 'response_project', 'chrono' => 'MAARCH/2019D/14', @@ -58,7 +58,7 @@ class FolderPrintControllerTest extends TestCase 'recipientType' => 'contact' ]; - $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); $response = $attachmentController->create($fullRequest, new \Slim\Http\Response()); $responseBody = json_decode((string)$response->getBody()); @@ -67,7 +67,7 @@ class FolderPrintControllerTest extends TestCase // CREATE LINK - \Resource\models\ResModel::update(['set' => ['linked_resources' => json_encode([$GLOBALS['resources'][1]])], 'where' => ['res_id = ?'], 'data' => [$GLOBALS['resources'][0]]]); + \Resource\models\ResModel::update(['set' => ['linked_resources' => json_encode([$GLOBALS['resources'][1], $GLOBALS['resources'][1] * 1000])], 'where' => ['res_id = ?'], 'data' => [$GLOBALS['resources'][0]]]); // GENERATE FOLDER PRINT @@ -76,7 +76,406 @@ class FolderPrintControllerTest extends TestCase $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $aArgs = [ + // Errors + $body = [ + "resources" => [ ] + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $folderPrintController->generateFile($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Body resources is empty', $responseBody['errors']); + + $body = [ + "resources" => [ + [ + "resId" => $GLOBALS['resources'][0], + "document" => true, + "attachments" => [self::$attachmentId], + "notes" => [self::$noteId], + "acknowledgementReceipts" => [], + "emails" => [], + "linkedResources" => [ + [ + 'resId' => $GLOBALS['resources'][1], + 'document' => true + ] + ], + ], [ + "resId" => $GLOBALS['resources'][0] * 1000, + "document" => true, + "attachments" => [self::$attachmentId], + "notes" => [self::$noteId], + "acknowledgementReceipts" => [], + "emails" => [], + "linkedResources" => [ + [ + 'resId' => $GLOBALS['resources'][1], + 'document' => true + ] + ], + ] + ] + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $folderPrintController->generateFile($fullRequest, new \Slim\Http\Response()); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Document out of perimeter', $responseBody['errors']); + + $body = [ + "resources" => [ + [ + "resId" => $GLOBALS['resources'][0], + "document" => false, + "attachments" => [], + "notes" => [], + "acknowledgementReceipts" => [], + "emails" => [], + ] + ] + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $folderPrintController->generateFile($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('No document to merge', $responseBody['errors']); + + // Attachment errors + $body = [ + "resources" => [ + [ + "resId" => $GLOBALS['resources'][0], + "document" => true, + "attachments" => [self::$attachmentId, 'wrong format'], + "notes" => [self::$noteId], + "acknowledgementReceipts" => [], + "emails" => [], + "linkedResources" => [ + [ + 'resId' => $GLOBALS['resources'][1], + 'document' => true + ] + ], + ] + ] + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $folderPrintController->generateFile($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Attachment id is not an integer', $responseBody['errors']); + + $body = [ + "resources" => [ + [ + "resId" => $GLOBALS['resources'][0], + "document" => true, + "attachments" => [self::$attachmentId * 1000], + "notes" => [self::$noteId], + "acknowledgementReceipts" => [], + "emails" => [], + "linkedResources" => [ + [ + 'resId' => $GLOBALS['resources'][1], + 'document' => true + ] + ], + ] + ] + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $folderPrintController->generateFile($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Attachment(s) not found', $responseBody['errors']); + + $body = [ + "resources" => [ + [ + "resId" => $GLOBALS['resources'][1], + "document" => true, + "attachments" => [self::$attachmentId], + "notes" => [self::$noteId], + "acknowledgementReceipts" => [], + "emails" => [], + "linkedResources" => [ + [ + 'resId' => $GLOBALS['resources'][1], + 'document' => true + ] + ], + ] + ] + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $folderPrintController->generateFile($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Attachment not linked to resource', $responseBody['errors']); + + // Note errors + $body = [ + "resources" => [ + [ + "resId" => $GLOBALS['resources'][0], + "document" => true, + "attachments" => [self::$attachmentId], + "notes" => [self::$noteId, 'wrong format'], + "acknowledgementReceipts" => [], + "emails" => [], + "linkedResources" => [ + [ + 'resId' => $GLOBALS['resources'][1], + 'document' => true + ] + ], + ] + ] + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $folderPrintController->generateFile($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Note id is not an integer', $responseBody['errors']); + + $body = [ + "resources" => [ + [ + "resId" => $GLOBALS['resources'][0], + "document" => true, + "attachments" => [self::$attachmentId], + "notes" => [self::$noteId, self::$noteId * 1000], + "acknowledgementReceipts" => [], + "emails" => [], + "linkedResources" => [ + [ + 'resId' => $GLOBALS['resources'][1], + 'document' => true + ] + ], + ] + ] + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $folderPrintController->generateFile($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Note(s) not found', $responseBody['errors']); + + // Linked resources errors + $body = [ + "resources" => [ + [ + "resId" => $GLOBALS['resources'][0], + "document" => true, + "attachments" => [self::$attachmentId], + "notes" => [self::$noteId], + "acknowledgementReceipts" => [], + "emails" => [], + "linkedResources" => [ + [ + 'resId' => 'wrong format', + 'document' => true + ] + ], + ] + ] + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $folderPrintController->generateFile($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('LinkedResources resId is not an integer', $responseBody['errors']); + + $body = [ + "resources" => [ + [ + "resId" => $GLOBALS['resources'][0], + "document" => true, + "attachments" => [self::$attachmentId], + "notes" => [self::$noteId], + "acknowledgementReceipts" => [], + "emails" => [], + "linkedResources" => [ + [ + 'resId' => $GLOBALS['resources'][2], + 'document' => true + ] + ], + ] + ] + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $folderPrintController->generateFile($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('LinkedResources resId is not linked to resource', $responseBody['errors']); + + $body = [ + "resources" => [ + [ + "resId" => $GLOBALS['resources'][0], + "document" => false, + "attachments" => [], + "notes" => [], + "acknowledgementReceipts" => [], + "emails" => [], + "linkedResources" => [ + [ + 'resId' => $GLOBALS['resources'][1] * 1000, + 'document' => true + ] + ], + ] + ] + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $folderPrintController->generateFile($fullRequest, new \Slim\Http\Response()); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('LinkedResources out of perimeter', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $body = [ + "resources" => [ + [ + "resId" => $GLOBALS['resources'][0], + "document" => true, + "attachments" => [], + "notes" => [], + "acknowledgementReceipts" => [], + "emails" => [], + "linkedResources" => [ + [ + 'resId' => $GLOBALS['resources'][1] * 1000, + 'document' => true + ] + ], + ] + ] + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $folderPrintController->generateFile($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('LinkedResources Document does not exist', $responseBody['errors']); + + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + // Linked resources attachments errors + $body = [ + "resources" => [ + [ + "resId" => $GLOBALS['resources'][0], + "document" => true, + "attachments" => [], + "notes" => [], + "acknowledgementReceipts" => [], + "emails" => [], + "linkedResources" => [ + [ + 'resId' => $GLOBALS['resources'][1], + 'document' => true, + 'attachments' => ['wrong format'] + ] + ], + ] + ] + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $folderPrintController->generateFile($fullRequest, new \Slim\Http\Response()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame(400, $response->getStatusCode()); + $this->assertSame('LinkedResources attachment id is not an integer', $responseBody['errors']); + + $body = [ + "resources" => [ + [ + "resId" => $GLOBALS['resources'][0], + "document" => true, + "attachments" => [], + "notes" => [], + "acknowledgementReceipts" => [], + "emails" => [], + "linkedResources" => [ + [ + 'resId' => $GLOBALS['resources'][1], + 'document' => true, + 'attachments' => [self::$attachmentId * 1000] + ] + ], + ] + ] + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $folderPrintController->generateFile($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('LinkedResources attachments not found', $responseBody['errors']); + + $body = [ + "resources" => [ + [ + "resId" => $GLOBALS['resources'][0], + "document" => true, + "attachments" => [], + "notes" => [], + "acknowledgementReceipts" => [], + "emails" => [], + "linkedResources" => [ + [ + 'resId' => $GLOBALS['resources'][1], + 'document' => true, + 'attachments' => [self::$attachmentId] + ] + ], + ] + ] + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $folderPrintController->generateFile($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('LinkedResources attachment is not linked to resource', $responseBody['errors']); + + // Success + $body = [ "resources" => [[ "resId" => $GLOBALS['resources'][0], "document" => true, @@ -123,11 +522,13 @@ class FolderPrintControllerTest extends TestCase ], "withSeparator" => true, ]; - - $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); $response = $folderPrintController->generateFile($fullRequest, new \Slim\Http\Response()); $responseBody = json_decode((string)$response->getBody()); + print_r($responseBody); + $this->assertSame(200, $response->getStatusCode()); $this->assertSame(null, $responseBody); @@ -138,7 +539,7 @@ class FolderPrintControllerTest extends TestCase $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $aArgs = [ + $body = [ "resources" => [[ "resId" => $GLOBALS['resources'][0], "document" => true, @@ -180,9 +581,10 @@ class FolderPrintControllerTest extends TestCase "withSeparator" => true, ]; - $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); $response = $folderPrintController->generateFile($fullRequest, new \Slim\Http\Response()); + $this->assertSame(200, $response->getStatusCode()); $responseBody = json_decode((string)$response->getBody()); $this->assertSame(null, $responseBody); @@ -207,10 +609,14 @@ class FolderPrintControllerTest extends TestCase // $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); // $request = \Slim\Http\Request::createFromEnvironment($environment); - // unset($aArgs['data'][2]['label']); - // $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + // unset($body['data'][2]['label']); + // $fullRequest = \httpRequestCustom::addContentInBody($body, $request); // $response = $ExportController->updateExport($fullRequest, new \Slim\Http\Response(), ['userId' => 19, 'groupId' => 2, 'basketId' => $myBasket['id']]); // $responseBody = json_decode((string)$response->getBody()); // $this->assertSame('One data is not set well', $responseBody->errors); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; } } diff --git a/test/unitTests/app/resource/ResControllerTest.php b/test/unitTests/app/resource/ResControllerTest.php index 5d7c121384e..fdd0a7d7735 100755 --- a/test/unitTests/app/resource/ResControllerTest.php +++ b/test/unitTests/app/resource/ResControllerTest.php @@ -12,6 +12,17 @@ use PHPUnit\Framework\TestCase; class ResControllerTest extends TestCase { private static $id = null; + private static $id2 = null; + + public function testGetDepartmentById() + { + $department = \Resource\controllers\DepartmentController::getById(['id' => '75']); + $this->assertSame('Paris', $department); + + $department = \Resource\controllers\DepartmentController::getById(['id' => 'not a french department']); + $this->assertIsString($department); + $this->assertEmpty($department); + } public function testCreate() { @@ -53,6 +64,30 @@ class ResControllerTest extends TestCase self::$id = $responseBody->resId; $this->assertIsInt(self::$id); + $aArgs = [ + 'modelId' => 2, + 'status' => 'NEW', + 'confidentiality' => false, + 'documentDate' => '2019-01-01 17:18:47', + 'arrivalDate' => '2019-01-01 17:18:47', + 'processLimitDate' => '2029-01-01', + 'doctype' => 102, + 'destination' => 15, + 'initiator' => 15, + 'subject' => 'Breaking News : Superman is alive - PHP unit', + 'typist' => 19, + 'priority' => 'poiuytre1357nbvc', + 'senders' => [['type' => 'contact', 'id' => 1], ['type' => 'user', 'id' => 21], ['type' => 'entity', 'id' => 1]], + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + + $response = $resController->create($fullRequest, new \Slim\Http\Response()); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertIsInt($responseBody['resId']); + self::$id2 = $responseBody['resId']; + // READ $res = \Resource\models\ResModel::getById(['resId' => self::$id, 'select' => ['*']]); @@ -98,6 +133,15 @@ class ResControllerTest extends TestCase $this->assertSame('Body modelId is empty or not an integer', $responseBody->errors); + $GLOBALS['login'] = 'ddur'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $resController->create($fullRequest, new \Slim\Http\Response()); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody()); + $this->assertSame('Service forbidden', $responseBody->errors); + $GLOBALS['login'] = 'superadmin'; $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); $GLOBALS['id'] = $userInfo['id']; @@ -110,12 +154,12 @@ class ResControllerTest extends TestCase $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $response = $resController->getById($request, new \Slim\Http\Response(), ['resId' => self::$id]); + $response = $resController->getById($request, new \Slim\Http\Response(), ['resId' => self::$id2]); $responseBody = json_decode((string)$response->getBody()); - $this->assertSame(self::$id, $responseBody->resId); - $this->assertSame(1, $responseBody->modelId); - $this->assertSame('incoming', $responseBody->categoryId); + $this->assertSame(self::$id2, $responseBody->resId); + $this->assertSame(2, $responseBody->modelId); + $this->assertSame('outgoing', $responseBody->categoryId); $this->assertEmpty($responseBody->chrono); $this->assertSame('NEW', $responseBody->status); $this->assertEmpty($responseBody->closingDate); @@ -129,7 +173,7 @@ class ResControllerTest extends TestCase $this->assertSame(102, $responseBody->doctype); $this->assertSame(15, $responseBody->destination); $this->assertSame('2019-01-01 17:18:47', $responseBody->documentDate); - $this->assertSame('2019-01-01 17:18:47', $responseBody->arrivalDate); + $this->assertEmpty($responseBody->arrivalDate); $this->assertNotEmpty($responseBody->destinationLabel); $this->assertSame("Nouveau courrier pour le service", $responseBody->statusLabel); $this->assertIsBool($responseBody->statusAlterable); @@ -190,22 +234,65 @@ class ResControllerTest extends TestCase $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); $request = \Slim\Http\Request::createFromEnvironment($environment); + $fileContent = file_get_contents('test/unitTests/samples/test.txt'); + $encodedFile = base64_encode($fileContent); + + $tag = \Tag\models\TagModel::get([ + 'select' => ['id'], + 'limit' => 1 + ]); + $tag = $tag[0]['id']; + + $folder = \Folder\models\FolderModel::create([ + 'label' => 'FOLDER TEST', + 'public' => false, + 'user_id' => $GLOBALS['id'], + 'parent_id' => null, + 'level' => 0 + ]); + $aArgs = [ - 'modelId' => 1, - 'status' => 'NEW', - 'confidentiality' => true, - 'documentDate' => '2019-01-01 17:18:47', - 'arrivalDate' => '2019-01-01 17:18:47', - 'processLimitDate' => '2030-01-01', - 'doctype' => 102, - 'destination' => 15, - 'initiator' => 15, - 'subject' => 'Breaking News : Superman is alive - PHP unit', - 'typist' => 19, - 'priority' => 'poiuytre1357nbvc', - 'senders' => [['type' => 'contact', 'id' => 1], ['type' => 'user', 'id' => 21], ['type' => 'entity', 'id' => 1]], + 'modelId' => 1, + 'status' => 'NEW', + 'encodedFile' => $encodedFile, + 'format' => 'txt', + 'confidentiality' => true, + 'documentDate' => '2019-01-01 17:18:47', + 'arrivalDate' => '2019-01-01 17:18:47', + 'processLimitDate' => '2030-01-01', + 'doctype' => 102, + 'destination' => 15, + 'initiator' => 15, + 'subject' => 'Breaking News : Superman is alive - PHP unit', + 'typist' => 19, + 'priority' => 'poiuytre1357nbvc', + 'senders' => [['type' => 'contact', 'id' => 1], ['type' => 'user', 'id' => 21], ['type' => 'entity', 'id' => 1]], + 'recipients' => [['type' => 'contact', 'id' => 2]], + 'tags' => [$tag], + 'folders' => [$folder], ]; + $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + $response = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]); + $this->assertSame(204, $response->getStatusCode()); + + $aArgs = [ + 'modelId' => 1, + 'status' => 'NEW', + 'encodedFile' => $encodedFile, + 'format' => 'txt', + 'confidentiality' => true, + 'documentDate' => '2019-01-01 17:18:47', + 'arrivalDate' => '2019-01-01 17:18:47', + 'processLimitDate' => '2030-01-01', + 'doctype' => 102, + 'destination' => 15, + 'initiator' => 15, + 'subject' => 'Breaking News : Superman is alive - PHP unit', + 'typist' => 19, + 'priority' => 'poiuytre1357nbvc', + 'senders' => [['type' => 'contact', 'id' => 1], ['type' => 'user', 'id' => 21], ['type' => 'entity', 'id' => 1]] + ]; $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); $response = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]); @@ -230,32 +317,75 @@ class ResControllerTest extends TestCase $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $fileContent = file_get_contents('test/unitTests/samples/test.txt'); - $encodedFile = base64_encode($fileContent); + $aArgs = [ + 'modelId' => 1, + 'status' => 'NEW', + 'encodedFile' => $encodedFile, + 'confidentiality' => true, + 'documentDate' => '2019-01-01 17:18:47', + 'arrivalDate' => '2019-01-01 17:18:47', + 'processLimitDate' => '2030-01-01', + 'doctype' => 102, + 'destination' => 15, + 'initiator' => 15, + 'subject' => 'Breaking News : Superman is alive - PHP unit', + 'typist' => 19, + 'priority' => 'poiuytre1357nbvc', + 'senders' => [['type' => 'contact', 'id' => 1], ['type' => 'user', 'id' => 21], ['type' => 'entity', 'id' => 1]], + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + + $response = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Body format is empty or not a string', $responseBody['errors']); $aArgs = [ - 'status' => 'NEW', - 'encodedFile' => $encodedFile, - 'format' => 'txt', - 'confidentiality' => false, - 'documentDate' => '2019-01-01 17:18:47', - 'arrivalDate' => '2019-01-01 17:18:47', - 'processLimitDate' => '2029-01-01', - 'destination' => 15, - 'initiator' => 15, - 'subject' => 'Breaking News : Superman is alive - PHP unit', - 'typist' => 19, - 'priority' => 'poiuytre1357nbvc', - 'tags' => [1, 2], - 'folders' => [1, 2], + 'status' => 'NEW', + 'encodedFile' => $encodedFile, + 'format' => 'txt', + 'confidentiality' => false, + 'documentDate' => '2019-01-01 17:18:47', + 'arrivalDate' => '2019-01-01 17:18:47', + 'processLimitDate' => '2029-01-01', + 'destination' => 15, + 'initiator' => 15, + 'subject' => 'Breaking News : Superman is alive - PHP unit', + 'typist' => 19, + 'priority' => 'poiuytre1357nbvc', + 'tags' => [1, 2], + 'folders' => [1, 2], ]; $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); $response = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]); - $responseBody = json_decode((string)$response->getBody()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Body doctype is empty or not an integer', $responseBody['errors']); + + $response = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => 'wrong format']); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Route resId is not an integer', $responseBody['errors']); + + $GLOBALS['login'] = 'ddur'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Service forbidden', $responseBody['errors']); - $this->assertSame('Body doctype is empty or not an integer', $responseBody->errors); + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + \Folder\models\FolderModel::delete([ + 'where' => ['id = ?'], + 'data' => [$folder] + ]); } public function testGetOriginalContent() @@ -288,11 +418,64 @@ class ResControllerTest extends TestCase $this->assertSame('txt', $responseBody->originalFormat); $this->assertNotEmpty($responseBody->originalCreatorId); + $aArgs = [ + 'mode' => 'base64' + ]; + $fullRequest = $request->withQueryParams($aArgs); + $response = $resController->getOriginalFileContent($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertNotEmpty($responseBody['encodedDocument']); + $this->assertIsString($responseBody['encodedDocument']); + $this->assertSame('txt', $responseBody['extension']); + $this->assertNotEmpty($responseBody['mimeType']); + $this->assertIsString($responseBody['mimeType']); + // ERROR $response = $resController->getFileContent($request, new \Slim\Http\Response(), ['resId' => -2]); - $responseBody = json_decode((string)$response->getBody()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Document does not exist', $responseBody['errors']); - $this->assertSame('Document does not exist', $responseBody->errors); + $response = $resController->getFileContent($request, new \Slim\Http\Response(), ['resId' => self::$id2]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Document has no file', $responseBody['errors']); + + $GLOBALS['login'] = 'ddur'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $resController->getFileContent($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Document out of perimeter', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $resController->getOriginalFileContent($request, new \Slim\Http\Response(), ['resId' => -2]); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Document does not exist', $responseBody['errors']); + + $response = $resController->getOriginalFileContent($request, new \Slim\Http\Response(), ['resId' => self::$id2]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Document has no file', $responseBody['errors']); + + $GLOBALS['login'] = 'ddur'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $resController->getOriginalFileContent($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Document out of perimeter', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; } public function testGetThumbnailContent() @@ -303,14 +486,109 @@ class ResControllerTest extends TestCase $request = \Slim\Http\Request::createFromEnvironment($environment); $response = $resController->getThumbnailContent($request, new \Slim\Http\Response(), ['resId' => self::$id]); - $responseBody = json_decode((string)$response->getBody()); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); - $this->assertSame(null, $responseBody); + $this->assertEmpty($responseBody); $response = $resController->getThumbnailContent($request, new \Slim\Http\Response(), ['resId' => -2]); - $responseBody = json_decode((string)$response->getBody()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Document does not exist', $responseBody['errors']); + + $response = $resController->getThumbnailContent($request, new \Slim\Http\Response(), ['resId' => 'wrong format']); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('resId param is not an integer', $responseBody['errors']); + } + + public function testGetItems() + { + $resController = new \Resource\controllers\ResController(); + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); - $this->assertSame('Document does not exist', $responseBody->errors); + // Errors + $response = $resController->getItems($request, new \Slim\Http\Response(), ['resId' => 'wrong format']); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Document out of perimeter', $responseBody['errors']); + + $response = $resController->getItems($request, new \Slim\Http\Response(), ['resId' => -2]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Document does not exist', $responseBody['errors']); + + // Success + $response = $resController->getItems($request, new \Slim\Http\Response(), ['resId' => self::$id]); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertSame(0, $responseBody['linkedResources']); + $this->assertSame(0, $responseBody['attachments']); + $this->assertSame(0, $responseBody['diffusionList']); + $this->assertSame(0, $responseBody['visaCircuit']); + $this->assertSame(0, $responseBody['opinionCircuit']); + $this->assertSame(0, $responseBody['notes']); + $this->assertSame(0, $responseBody['emails']); + } + + public function testGetField() + { + $resController = new \Resource\controllers\ResController(); + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + // Errors + $GLOBALS['login'] = 'ddur'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $resController->getField($request, new \Slim\Http\Response(), ['resId' => self::$id]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Document out of perimeter', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $resController->getField($request, new \Slim\Http\Response(), ['resId' => self::$id, 'fieldId' => 'initiator']); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Field out of perimeter', $responseBody['errors']); + + $response = $resController->getField($request, new \Slim\Http\Response(), ['resId' => self::$id * 1000, 'fieldId' => 'destination']); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Document does not exist', $responseBody['errors']); + + // Success + $response = $resController->getField($request, new \Slim\Http\Response(), ['resId' => self::$id2, 'fieldId' => 'externalId']); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertIsArray($responseBody['field']); + $this->assertEmpty($responseBody['field']); + + $fullRequest = $request->withQueryParams(['alt' => true]); + $response = $resController->getField($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id2, 'fieldId' => 'destination']); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame(15, $responseBody['field']); + } + + public function testGetEncodedDocument() + { + $resController = new \Resource\controllers\ResController(); + + $response = $resController::getEncodedDocument(['resId' => self::$id, 'original' => false]); + + $this->assertIsString($response['encodedDocument']); + $this->assertNotEmpty($response['encodedDocument']); + + $this->assertSame('Breaking News _ Superman is al.pdf', $response['fileName']); } public function testGetCategories() @@ -402,11 +680,11 @@ class ResControllerTest extends TestCase $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $aArgs = [ + $body = [ 'resId' => [self::$id], 'status' => 'EVIS' ]; - $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); $response = $resController->updateStatus($fullRequest, new \Slim\Http\Response()); $responseBody = json_decode((string)$response->getBody()); @@ -421,11 +699,11 @@ class ResControllerTest extends TestCase $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $aArgs = [ + $body = [ 'resId' => [self::$id] ]; - $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); $response = $resController->updateStatus($fullRequest, new \Slim\Http\Response()); $responseBody = json_decode((string)$response->getBody()); @@ -436,6 +714,60 @@ class ResControllerTest extends TestCase $res = \Resource\models\ResModel::getById(['resId' => self::$id, 'select' => ['*']]); $this->assertIsArray($res); $this->assertSame('COU', $res['status']); + + $body = [ + 'status' => 'STATUS_THAT_DOES_NOT_EXIST' + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $resController->updateStatus($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame(_STATUS_NOT_FOUND, $responseBody['errors']); + + $body = [ + 'status' => 'EVIS', + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $resController->updateStatus($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Bad Request', $responseBody['errors']); + + $body = [ + 'status' => 'EVIS', + 'resId' => [self::$id * 1000] + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $resController->updateStatus($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame(_DOCUMENT_NOT_FOUND, $responseBody['errors']); + + $GLOBALS['login'] = 'ddur'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $body = [ + 'status' => 'EVIS', + 'resId' => [self::$id] + ]; + + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $resController->updateStatus($fullRequest, new \Slim\Http\Response()); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Document out of perimeter', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; } public function testUpdateExternalInfos() @@ -550,7 +882,7 @@ class ResControllerTest extends TestCase $request = \Slim\Http\Request::createFromEnvironment($environment); $aArgs = [ - 'select' => 'res_id', + 'select' => 'sve_start_date', 'clause' => '1=1', 'withFile' => true, 'orderBy' => ['res_id'], @@ -579,6 +911,7 @@ class ResControllerTest extends TestCase $this->assertSame(null, $arr_res[0]->fileBase64Content); $this->assertIsInt($arr_res[0]->res_id); + // Errors $aArgs = [ 'select' => '', 'clause' => '1=1', @@ -589,8 +922,8 @@ class ResControllerTest extends TestCase $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); $response = $resController->getList($fullRequest, new \Slim\Http\Response()); - $responseBody = json_decode((string)$response->getBody()); - $this->assertSame("Bad Request: select is not valid", $responseBody->errors); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Bad Request: select is not valid', $responseBody['errors']); $aArgs = [ 'select' => 'res_id', @@ -602,8 +935,249 @@ class ResControllerTest extends TestCase $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); $response = $resController->getList($fullRequest, new \Slim\Http\Response()); - $responseBody = json_decode((string)$response->getBody()); - $this->assertSame("Bad Request: clause is not valid", $responseBody->errors); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Bad Request: clause is not valid', $responseBody['errors']); + + $aArgs = [ + 'select' => 'res_id', + 'clause' => '1=1', + 'withFile' => 'wrong format', + 'orderBy' => ['res_id'], + 'limit' => 1 + ]; + $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + + $response = $resController->getList($fullRequest, new \Slim\Http\Response()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Bad Request: withFile parameter is not a boolean', $responseBody['errors']); + + $aArgs = [ + 'select' => 'res_id', + 'clause' => '1=1', + 'withFile' => false, + 'orderBy' => 'wrong format', + 'limit' => 1 + ]; + $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + + $response = $resController->getList($fullRequest, new \Slim\Http\Response()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Bad Request: orderBy parameter not valid', $responseBody['errors']); + + $aArgs = [ + 'select' => 'res_id', + 'clause' => '1=1', + 'withFile' => false, + 'orderBy' => ['res_id'], + 'limit' => 'wrong format' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + + $response = $resController->getList($fullRequest, new \Slim\Http\Response()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Bad Request: limit parameter not valid', $responseBody['errors']); + + $aArgs = [ + 'select' => 'res_id', + 'clause' => 'dundermifflin_clients.branch', + 'withFile' => false, + 'orderBy' => ['res_id'], + 'limit' => 1 + ]; + $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + + $response = $resController->getList($fullRequest, new \Slim\Http\Response()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame(_INVALID_REQUEST, $responseBody['errors']); + } + + public function testGetProcessingData() + { + $resController = new \Resource\controllers\ResController(); + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $response = $resController->getProcessingData($request, new \Slim\Http\Response(), ['groupId' => 'wrong format']); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('groupId param is not an integer', $responseBody['errors']); + + $response = $resController->getProcessingData($request, new \Slim\Http\Response(), ['groupId' => 2, 'userId' => 'wrong format']); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('userId param is not an integer', $responseBody['errors']); + + $response = $resController->getProcessingData($request, new \Slim\Http\Response(), ['groupId' => 2, 'userId' => $GLOBALS['id'], 'basketId' => 'wrong format']); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('basketId param is not an integer', $responseBody['errors']); + + $response = $resController->getProcessingData($request, new \Slim\Http\Response(), ['groupId' => 2, 'userId' => $GLOBALS['id'], 'basketId' => 2, 'resId' => 'wrong format']); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('resId param is not an integer', $responseBody['errors']); + + $response = $resController->getProcessingData($request, new \Slim\Http\Response(), ['groupId' => 2, 'userId' => $GLOBALS['id'], 'basketId' => 2, 'resId' => self::$id]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Group is not linked to this user', $responseBody['errors']); + + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $resController->getProcessingData($request, new \Slim\Http\Response(), ['groupId' => 2, 'userId' => $GLOBALS['id'], 'basketId' => 2, 'resId' => self::$id]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Group is not linked to this basket', $responseBody['errors']); + + // Success + $response = $resController->getProcessingData($request, new \Slim\Http\Response(), ['groupId' => 2, 'userId' => $GLOBALS['id'], 'basketId' => 4, 'resId' => self::$id]); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertIsArray($responseBody['listEventData']); + $this->assertNotEmpty($responseBody['listEventData']); + $this->assertSame('dashboard', $responseBody['listEventData']['defaultTab']); + $this->assertSame(false, $responseBody['listEventData']['canUpdate']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + } + + public function testGetResourceFileInformation() + { + $resController = new \Resource\controllers\ResController(); + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $resController->getResourceFileInformation($request, new \Slim\Http\Response(), ['resId' => self::$id]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Document out of perimeter', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + // Success + $response = $resController->getResourceFileInformation($request, new \Slim\Http\Response(), ['resId' => self::$id]); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertIsArray($responseBody['information']); + $this->assertNotEmpty($responseBody['information']); + $this->assertSame('txt', $responseBody['information']['format']); + $this->assertIsString($responseBody['information']['fingerprint']); + $this->assertNotEmpty($responseBody['information']['fingerprint']); + $this->assertSame(46, $responseBody['information']['filesize']); + $this->assertSame('ERROR', $responseBody['information']['fulltext_result']); + $this->assertSame(true, $responseBody['information']['canConvert']); + } + + public function testGetVersionsInformations() + { + $resController = new \Resource\controllers\ResController(); + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $resController->getVersionsInformations($request, new \Slim\Http\Response(), ['resId' => self::$id]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Document out of perimeter', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + // Success + $response = $resController->getVersionsInformations($request, new \Slim\Http\Response(), ['resId' => self::$id]); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertIsArray($responseBody['DOC']); + $this->assertNotEmpty($responseBody['DOC']); + $this->assertSame(1, $responseBody['DOC'][0]); + $this->assertSame(2, $responseBody['DOC'][1]); + $this->assertSame(3, $responseBody['DOC'][2]); + + $this->assertIsArray($responseBody['PDF']); + $this->assertNotEmpty($responseBody['PDF']); + $this->assertSame(1, $responseBody['PDF'][0]); + $this->assertSame(2, $responseBody['PDF'][1]); + $this->assertSame(3, $responseBody['PDF'][2]); + + $this->assertIsArray($responseBody['SIGN']); + $this->assertEmpty($responseBody['SIGN']); + + $this->assertIsArray($responseBody['NOTE']); + $this->assertEmpty($responseBody['NOTE']); + + $this->assertSame(true, $responseBody['convert']); + + $response = $resController->getVersionsInformations($request, new \Slim\Http\Response(), ['resId' => self::$id2]); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertIsArray($responseBody['DOC']); + $this->assertEmpty($responseBody['DOC']); + $this->assertIsArray($responseBody['PDF']); + $this->assertEmpty($responseBody['PDF']); + $this->assertIsArray($responseBody['SIGN']); + $this->assertEmpty($responseBody['SIGN']); + $this->assertIsArray($responseBody['NOTE']); + $this->assertEmpty($responseBody['NOTE']); + } + + public function testGetVersionFileContent() + { + $resController = new \Resource\controllers\ResController(); + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $resController->getVersionFileContent($request, new \Slim\Http\Response(), ['resId' => self::$id]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Document out of perimeter', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $resController->getVersionFileContent($request, new \Slim\Http\Response(), ['resId' => self::$id, 'version' => 1000]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Incorrect version', $responseBody['errors']); + + $response = $resController->getVersionFileContent($request, new \Slim\Http\Response(), ['resId' => self::$id2, 'version' => 1]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Document has no file', $responseBody['errors']); + + // Success + $response = $resController->getVersionFileContent($request, new \Slim\Http\Response(), ['resId' => self::$id, 'version' => 1]); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertIsString($responseBody['encodedDocument']); + $this->assertNotEmpty($responseBody['encodedDocument']); } public function testDelete() @@ -615,6 +1189,15 @@ class ResControllerTest extends TestCase $res = \Resource\models\ResModel::getById(['resId' => self::$id, 'select' => ['*']]); $this->assertIsArray($res); $this->assertSame('DEL', $res['status']); + + \Resource\models\ResModel::delete([ + 'where' => ['res_id = ?'], + 'data' => [self::$id2] + ]); + + $res = \Resource\models\ResModel::getById(['resId' => self::$id2, 'select' => ['*']]); + $this->assertIsArray($res); + $this->assertEmpty($res); } public function testCreateMultipleDocument() -- GitLab