From 7cbe266cf6dceb145f55f2d902f453781bb5c009 Mon Sep 17 00:00:00 2001
From: Guillaume Heurtier <guillaume.heurtier@maarch.org>
Date: Tue, 28 Apr 2020 14:36:02 +0200
Subject: [PATCH] FEAT #13676 TIME 2:30 improved summary sheets tests

---
 rest/index.php                                |   2 -
 .../controllers/SummarySheetController.php    | 135 ------------------
 .../resource/models/ResourceContactModel.php  |  15 --
 .../models/UserFollowedResourceModel.php      |  31 ----
 .../app/resource/ResControllerTest.php        |   2 +-
 .../resource/SummarySheetControllerTest.php   | 112 ++++++++++++++-
 6 files changed, 107 insertions(+), 190 deletions(-)

diff --git a/rest/index.php b/rest/index.php
index 295047b6010..f7123c8782f 100755
--- a/rest/index.php
+++ b/rest/index.php
@@ -136,7 +136,6 @@ $app->delete('/contacts/{id}', \Contact\controllers\ContactController::class . '
 $app->put('/contacts/{id}/activation', \Contact\controllers\ContactController::class . ':updateActivation');
 $app->get('/formattedContacts/{id}/types/{type}', \Contact\controllers\ContactController::class . ':getLightFormattedContact');
 $app->get('/ban/availableDepartments', \Contact\controllers\ContactController::class . ':getAvailableDepartments');
-$app->post('/contacts/formatV1', \Contact\controllers\ContactController::class . ':getFormattedContactsForSearchV1');
 
 //ContactsCustomFields
 $app->get('/contactsCustomFields', \Contact\controllers\ContactCustomFieldController::class . ':get');
@@ -401,7 +400,6 @@ $app->put('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/exp
 $app->post('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/summarySheets', \Resource\controllers\SummarySheetController::class . ':createList');
 $app->put('/resourcesList/users/{userId}/groups/{groupId}/baskets/{basketId}/actions/{actionId}', \Resource\controllers\ResourceListController::class . ':setAction');
 $app->get('/resourcesList/exportTemplate', \Resource\controllers\ExportController::class . ':getExportTemplates');
-$app->get('/resourcesList/summarySheets', \Resource\controllers\SummarySheetController::class . ':createListWithAll');
 $app->put('/resourcesList/integrations', \Resource\controllers\ResController::class . ':setInIntegrations');
 
 //PreProcess
diff --git a/src/app/resource/controllers/SummarySheetController.php b/src/app/resource/controllers/SummarySheetController.php
index 6ef6e0fb03a..ac89194e713 100755
--- a/src/app/resource/controllers/SummarySheetController.php
+++ b/src/app/resource/controllers/SummarySheetController.php
@@ -171,141 +171,6 @@ class SummarySheetController
         return $response->withHeader('Content-Type', $mimeType);
     }
 
-    public function createListWithAll(Request $request, Response $response)
-    {
-        set_time_limit(240);
-
-        $bodyData = $request->getQueryParams();
-        $units    = [
-            [
-                "unit" => "qrcode",
-                "label" => ""
-            ],
-            [
-                "unit" => "primaryInformations",
-                "label" => "Informations pricipales"
-            ],
-            [
-                "unit" => "senderRecipientInformations",
-                "label" => "Informations de destination"
-            ],
-            [
-                "unit" => "secondaryInformations",
-                "label" => "Informations secondaires"
-            ],
-            [
-                "unit" => "diffusionList",
-                "label" => "Liste de diffusion"
-            ],
-            [
-                "unit" => "opinionWorkflow",
-                "label" => "Circuit d'avis"
-            ],
-            [
-                "unit" => "visaWorkflow",
-                "label" => "Circuit de visa"
-            ],
-            [
-                "unit" => "notes",
-                "label" => "Annotation(s)"
-            ]
-        ];
-
-        $resourcesData = json_decode($bodyData['resources']);
-
-        if (!Validator::arrayType()->notEmpty()->validate($resourcesData)) {
-            return $response->withStatus(403)->withJson(['errors' => 'Resources is not set or empty']);
-        }
-
-        $resourcesData = array_slice($resourcesData, 0, 500);
-
-        $rawResourcesInBasket = ResModel::getOnView([
-            'select'    => ['res_id'],
-            'where'     => ['res_view_letterbox.res_id in (?)'],
-            'data'      => [$resourcesData]
-        ]);
-        $allResourcesInBasket = array_column($rawResourcesInBasket, 'res_id');
-
-        $resourcesByModelIds = ResModel::get([
-            'select'  => ["string_agg(cast(res_id as text), ',') as res_ids", 'model_id'],
-            'where'   => ['res_id in (?)'],
-            'data'    => [$resourcesData],
-            'groupBy' => ['model_id']
-        ]);
-
-        $order = 'CASE res_view_letterbox.res_id ';
-        foreach ($resourcesData as $key => $resId) {
-            if (!in_array($resId, $allResourcesInBasket)) {
-                return $response->withStatus(403)->withJson(['errors' => 'Resources out of perimeter']);
-            }
-            $order .= "WHEN {$resId} THEN {$key} ";
-        }
-        $order .= 'END';
-
-        $select = [
-            'res_id',
-            'subject',
-            'alt_identifier',
-            'admission_date',
-            'creation_date',
-            'doc_date',
-            'type_label',
-            'initiator',
-            'typist',
-            'category_id',
-            'priority',
-            'process_limit_date',
-            'status',
-            'destination'
-        ];
-
-        $pdf = new Fpdi('P', 'pt');
-        $pdf->setPrintHeader(false);
-
-        foreach ($resourcesByModelIds as $resourcesByModelId) {
-            $resourcesIdsByModel = $resourcesByModelId['res_ids'];
-            $resourcesIdsByModel = explode(',', $resourcesIdsByModel);
-
-            $indexingFields = IndexingModelFieldModel::get([
-                'select' => ['identifier', 'unit'],
-                'where'  => ['model_id = ?'],
-                'data'   => [$resourcesByModelId['model_id']]
-            ]);
-            $fieldsIdentifier = array_column($indexingFields, 'identifier');
-
-            $resources = ResModel::getOnView([
-                'select'  => $select,
-                'where'   => ['res_view_letterbox.res_id in (?)'],
-                'data'    => [$resourcesIdsByModel],
-                'orderBy' => [$order]
-            ]);
-
-            $resourcesIds = array_column($resources, 'res_id');
-
-            // Data for resources
-            $data = SummarySheetController::prepareData(['units' => $units, 'resourcesIds' => $resourcesIds]);
-
-            foreach ($resources as $resource) {
-                SummarySheetController::createSummarySheet($pdf, [
-                    'resource'         => $resource,
-                    'units'            => $units,
-                    'login'            => $GLOBALS['login'],
-                    'data'             => $data,
-                    'fieldsIdentifier' => $fieldsIdentifier
-                ]);
-            }
-        }
-
-        $fileContent = $pdf->Output('', 'S');
-        $finfo    = new \finfo(FILEINFO_MIME_TYPE);
-        $mimeType = $finfo->buffer($fileContent);
-
-        $response->write($fileContent);
-        $response = $response->withAddedHeader('Content-Disposition', "inline; filename=maarch.pdf");
-
-        return $response->withHeader('Content-Type', $mimeType);
-    }
-
     public static function createSummarySheet(Fpdi $pdf, array $args)
     {
         ValidatorModel::notEmpty($args, ['resource', 'login']);
diff --git a/src/app/resource/models/ResourceContactModel.php b/src/app/resource/models/ResourceContactModel.php
index 49bcac80894..0d5395eee6e 100755
--- a/src/app/resource/models/ResourceContactModel.php
+++ b/src/app/resource/models/ResourceContactModel.php
@@ -19,21 +19,6 @@ use SrcCore\models\ValidatorModel;
 
 class ResourceContactModel
 {
-    public static function getByResId(array $aArgs)
-    {
-        ValidatorModel::notEmpty($aArgs, ['resId']);
-        ValidatorModel::intVal($aArgs, ['resId']);
-
-        $aContacts = DatabaseModel::select([
-            'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
-            'table'     => ['resource_contacts'],
-            'where'     => ['res_id = ?'],
-            'data'      => [$aArgs['resId']],
-        ]);
-
-        return $aContacts;
-    }
-
     public static function get(array $args)
     {
         ValidatorModel::arrayType($args, ['select', 'where', 'data', 'orderBy']);
diff --git a/src/app/resource/models/UserFollowedResourceModel.php b/src/app/resource/models/UserFollowedResourceModel.php
index aeba5c9cc5f..5311b5da634 100644
--- a/src/app/resource/models/UserFollowedResourceModel.php
+++ b/src/app/resource/models/UserFollowedResourceModel.php
@@ -36,22 +36,6 @@ class UserFollowedResourceModel
         return $followedResources;
     }
 
-    public static function getByUserId(array $args)
-    {
-        ValidatorModel::notEmpty($args, ['userId']);
-        ValidatorModel::intVal($args, ['userId']);
-        ValidatorModel::arrayType($args, ['select']);
-
-        $followedResources = DatabaseModel::select([
-            'select'    => empty($args['select']) ? ['*'] : $args['select'],
-            'table'     => ['users_followed_resources'],
-            'where'     => ['user_id = ?'],
-            'data'      => [$args['userId']]
-        ]);
-
-        return $followedResources;
-    }
-
     public static function create(array $args)
     {
         ValidatorModel::notEmpty($args, ['resId', 'userId']);
@@ -68,21 +52,6 @@ class UserFollowedResourceModel
         return true;
     }
 
-    public static function update(array $args)
-    {
-        ValidatorModel::notEmpty($args, ['set', 'where', 'data']);
-        ValidatorModel::arrayType($args, ['set', 'where', 'data']);
-
-        DatabaseModel::update([
-            'table' => 'users_followed_resources',
-            'set'   => $args['set'],
-            'where' => $args['where'],
-            'data'  => $args['data']
-        ]);
-
-        return true;
-    }
-
     public static function delete(array $args)
     {
         ValidatorModel::notEmpty($args, ['resId', 'userId']);
diff --git a/test/unitTests/app/resource/ResControllerTest.php b/test/unitTests/app/resource/ResControllerTest.php
index 94f319c674b..2905807bcd9 100755
--- a/test/unitTests/app/resource/ResControllerTest.php
+++ b/test/unitTests/app/resource/ResControllerTest.php
@@ -1078,7 +1078,7 @@ class ResControllerTest extends TestCase
         $this->assertIsString($responseBody['information']['fingerprint']);
         $this->assertNotEmpty($responseBody['information']['fingerprint']);
         $this->assertSame(46, $responseBody['information']['filesize']);
-        $this->assertIsString($responseBody['information']['fulltext_result']);
+        $this->assertSame('SUCCESS', $responseBody['information']['fulltext_result']);
         $this->assertSame(true, $responseBody['information']['canConvert']);
     }
 
diff --git a/test/unitTests/app/resource/SummarySheetControllerTest.php b/test/unitTests/app/resource/SummarySheetControllerTest.php
index c2778633be5..5b28bd833b6 100644
--- a/test/unitTests/app/resource/SummarySheetControllerTest.php
+++ b/test/unitTests/app/resource/SummarySheetControllerTest.php
@@ -11,8 +11,74 @@ use PHPUnit\Framework\TestCase;
 
 class SummarySheetControllerTest extends TestCase
 {
+    private static $noteId = null;
+
     public function testCreateList()
     {
+        $GLOBALS['login'] = 'ddur';
+        $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]);
+        $GLOBALS['id'] = $userInfo['id'];
+
+        $noteController = new \Note\controllers\NoteController();
+
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+
+        $body = [
+            'value'     => "Test d'ajout d'une note par php unit",
+            'entities'  => ['COU', 'CAB', 'PJS'],
+            'resId'     => $GLOBALS['resources'][0]
+        ];
+
+        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
+
+        $response     = $noteController->create($fullRequest, new \Slim\Http\Response());
+        $responseBody = json_decode((string)$response->getBody(), true);
+
+        $this->assertIsInt($responseBody['noteId']);
+        self::$noteId = $responseBody['noteId'];
+
+        $GLOBALS['login'] = 'superadmin';
+        $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]);
+        $GLOBALS['id'] = $userInfo['id'];
+        $userInfo = \User\models\UserModel::getByLogin(['login' => 'bbain', 'select' => ['id']]);
+
+        \IndexingModel\models\IndexingModelFieldModel::create([
+            'model_id'   => 1,
+            'identifier' => 'indexingCustomField_4',
+            'mandatory'  => 'false',
+            'unit'       => 'mail'
+        ]);
+
+        \IndexingModel\models\IndexingModelFieldModel::create([
+            'model_id'   => 1,
+            'identifier' => 'recipients',
+            'mandatory'  => 'false',
+            'unit'       => 'mail'
+        ]);
+
+        \Entity\models\ListInstanceModel::create([
+            'res_id'          => $GLOBALS['resources'][0],
+            'sequence'        => 0,
+            'item_id'         => $userInfo['id'],
+            'item_type'       => 'user_id',
+            'item_mode'       => 'dest',
+            'added_by_user'   => $GLOBALS['id'],
+            'viewed'          => 0,
+            'difflist_type'   => 'VISA_CIRCUIT'
+        ]);
+
+        \Entity\models\ListInstanceModel::create([
+            'res_id'          => $GLOBALS['resources'][0],
+            'sequence'        => 0,
+            'item_id'         => $userInfo['id'],
+            'item_type'       => 'user_id',
+            'item_mode'       => 'dest',
+            'added_by_user'   => $GLOBALS['id'],
+            'viewed'          => 0,
+            'difflist_type'   => 'AVIS_CIRCUIT'
+        ]);
+
         $GLOBALS['login'] = 'bbain';
         $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]);
         $GLOBALS['id'] = $userInfo['id'];
@@ -24,11 +90,12 @@ class SummarySheetControllerTest extends TestCase
         $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']);
         $request        = \Slim\Http\Request::createFromEnvironment($environment);
 
-        $aArgs = [
+        $body = [
             "resources" => $GLOBALS['resources'],
             "units" => [
                 ['label' => 'Informations', 'unit' => 'primaryInformations'],
                 ['label' => 'Informations Secondaires', 'unit' => 'secondaryInformations'],
+                ["label" => "Informations de destination", "unit" => "senderRecipientInformations"],
                 ['label' => 'Liste de diffusion', 'unit' => 'diffusionList'],
                 ['label' => 'Ptit avis les potos.', 'unit' => 'freeField'],
                 ['label' => 'Annotation(s)', 'unit' => 'notes'],
@@ -38,7 +105,7 @@ class SummarySheetControllerTest extends TestCase
                 ['unit' => 'qrcode']
             ],
         ];
-        $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
+        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
 
         $response     = $summarySheetController->createList($fullRequest, new \Slim\Http\Response(), ['userId' => 19, 'groupId' => 2, 'basketId' => $myBasket['id']]);
         $responseBody = json_decode((string)$response->getBody());
@@ -50,14 +117,47 @@ class SummarySheetControllerTest extends TestCase
         $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']);
         $request        = \Slim\Http\Request::createFromEnvironment($environment);
 
-        unset($aArgs['resources']);
-        $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
+        unset($body['resources']);
+        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
         $response = $summarySheetController->createList($fullRequest, new \Slim\Http\Response(), ['userId' => 19, 'groupId' => 2, 'basketId' => $myBasket['id']]);
-        $responseBody = json_decode((string)$response->getBody());
-        $this->assertSame('Resources is not set or empty', $responseBody->errors);
+        $this->assertSame(403, $response->getStatusCode());
+        $responseBody = json_decode((string)$response->getBody(), true);
+        $this->assertSame('Resources is not set or empty', $responseBody['errors']);
+
+        $body = [
+            "resources" => $GLOBALS['resources'],
+            "units" => [
+                ['label' => 'Informations', 'unit' => 'primaryInformations']
+            ],
+        ];
+
+        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
+        $response = $summarySheetController->createList($fullRequest, new \Slim\Http\Response(), ['userId' => 19, 'groupId' => 2, 'basketId' => $myBasket['id'] * 1000]);
+        $this->assertSame(403, $response->getStatusCode());
+        $responseBody = json_decode((string)$response->getBody(), true);
+        $this->assertSame('Group or basket does not exist', $responseBody['errors']);
+
+        $GLOBALS['login'] = 'ddur';
+        $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]);
+        $GLOBALS['id'] = $userInfo['id'];
+
+        $response = $summarySheetController->createList($fullRequest, new \Slim\Http\Response(), ['userId' => $GLOBALS['id'], 'groupId' => 8, 'basketId' => $myBasket['id']]);
+        $this->assertSame(403, $response->getStatusCode());
+        $responseBody = json_decode((string)$response->getBody(), true);
+        $this->assertSame('Resources out of perimeter', $responseBody['errors']);
 
         $GLOBALS['login'] = 'superadmin';
         $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]);
         $GLOBALS['id'] = $userInfo['id'];
+
+        \IndexingModel\models\IndexingModelFieldModel::delete([
+            'where' => ['identifier in (?)', 'model_id = ?'],
+            'data'  => [['indexingCustomField_4', 'recipients'], 1]
+        ]);
+
+        \Entity\models\ListInstanceModel::delete([
+            'where' => ['res_id = ?', 'difflist_type in (?)'],
+            'data'  => [$GLOBALS['resources'][0], ['AVIS_CIRCUIT', 'VISA_CIRCUIT']]
+        ]);
     }
 }
-- 
GitLab