From b76a15250f1daa9422bdcc12e152d9e4c00195ba Mon Sep 17 00:00:00 2001
From: Nestor <npegane@hotmail.com>
Date: Fri, 6 Apr 2018 15:07:46 +0200
Subject: [PATCH] FEAT #7345 check then update + modified sql + test adaptation

---
 core/Test/ResControllerTest.php               | 73 ++++++++++++++++++-
 sql/develop.sql                               |  7 ++
 sql/structure.sql                             |  2 +
 .../resource/controllers/ResController.php    | 52 +++++++------
 4 files changed, 109 insertions(+), 25 deletions(-)

diff --git a/core/Test/ResControllerTest.php b/core/Test/ResControllerTest.php
index 1e8d81a2708..e15f2704fa7 100755
--- a/core/Test/ResControllerTest.php
+++ b/core/Test/ResControllerTest.php
@@ -163,7 +163,7 @@ class ResControllerTest extends TestCase
         
         $responseBody = json_decode((string) $response->getBody());
 
-        $this->assertSame('Bad Request', $responseBody->errors);
+        $this->assertSame('Bad Request: invalid res_id', $responseBody->errors);
 
         // DOCUMENT DOES NOT EXIST
         $aArgs = [
@@ -185,11 +185,11 @@ class ResControllerTest extends TestCase
 
         $this->assertSame(_DOCUMENT_NOT_FOUND, $responseBody->errors);
 
-        //MISSING EXTERNAL INFO
+        //MISSING STATUS
         $aArgs = [
                 'externalInfos' => [
                     [
-                        'res_id'        => 123456789,
+                        'res_id'        => self::$id,
                         'external_id'   => "BB981212IIYZ",
                         'external_link' => "https://publik.nancy.fr/res/BB981212BB65"
                     ]
@@ -205,7 +205,7 @@ class ResControllerTest extends TestCase
 
         $this->assertSame('Bad Request', $responseBody->errors);
         
-        //MISSING STATUS
+        //MISSING EXTERNAL INFOS
         $aArgs = [
             'externalInfos' => NULL,
             'status'        => "GRCSENT"
@@ -242,4 +242,69 @@ class ResControllerTest extends TestCase
         $this->assertSame(null, $res);
     }
 
+    public function testGetList(){
+        $resController = new \Resource\controllers\ResController();
+
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+
+        $aArgs = [
+            'select'        => 'res_id',
+            'clause'        => '1=1',
+            'withFile'      => true,
+            'orderBy'       => ['res_id'],
+            'limit'         => 1
+        ];
+        $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
+
+        $response     = $resController->getList($fullRequest, new \Slim\Http\Response());
+        $responseBody = json_decode((string)$response->getBody());
+        $arr_res = $responseBody->resources;
+        $this->assertNotNull($arr_res[0]->fileBase64Content);
+        $this->assertSame(100,$arr_res[0]->res_id);
+
+        $aArgs = [
+            'select'        => 'res_id',
+            'clause'        => '1=1',
+            '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());
+        $arr_res = $responseBody->resources;
+        $this->assertSame(null,$arr_res[0]->fileBase64Content);
+        $this->assertSame(100,$arr_res[0]->res_id);
+
+        $aArgs = [
+            'select'        => '',
+            'clause'        => '1=1',
+            '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());
+        $arr_res = $responseBody->resources;
+        $this->assertSame("Bad Request: select parameter not valid",$responseBody->errors);
+
+        $aArgs = [
+            'select'        => 'res_id',
+            'clause'        => '',
+            '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());
+        $arr_res = $responseBody->resources;
+        $this->assertSame("Bad Request: clause parameter not valid",$responseBody->errors);
+    }
+
 }
diff --git a/sql/develop.sql b/sql/develop.sql
index dcc75835c48..75e445bdfaa 100755
--- a/sql/develop.sql
+++ b/sql/develop.sql
@@ -140,6 +140,13 @@ DROP VIEW IF EXISTS res_view_letterbox;
 /* Alter table here because view depends on it*/
 ALTER TABLE res_letterbox ALTER COLUMN priority TYPE character varying(16);
 
+--ALTER TABLE for external infos webservice
+ALTER TABLE res_letterbox DROP COLUMN IF EXISTS external_id;
+ALTER TABLE res_letterbox ADD COLUMN external_id character varying(255);
+
+ALTER TABLE res_letterbox DROP COLUMN IF EXISTS external_link;
+ALTER TABLE res_letterbox ADD COLUMN external_link character varying(255);
+
 CREATE OR REPLACE VIEW res_view_letterbox AS 
  SELECT r.tablename,
     r.is_multi_docservers,
diff --git a/sql/structure.sql b/sql/structure.sql
index 8a3a4aa827b..21b8e0f8e7d 100755
--- a/sql/structure.sql
+++ b/sql/structure.sql
@@ -1619,6 +1619,8 @@ CREATE TABLE res_letterbox
   tnl_result character varying(10) DEFAULT NULL::character varying,
   tnl_attempts integer DEFAULT NULL::integer,
   ocr_result character varying(10) DEFAULT NULL::character varying,
+  external_id character varying(255) DEFAULT NULL::character varying,
+  external_link character varying(255) DEFAULT NULL::character varying,
   CONSTRAINT res_letterbox_pkey PRIMARY KEY  (res_id)
 )
 WITH (OIDS=FALSE);
diff --git a/src/app/resource/controllers/ResController.php b/src/app/resource/controllers/ResController.php
index aab6d3506c2..a41c4b9fc77 100755
--- a/src/app/resource/controllers/ResController.php
+++ b/src/app/resource/controllers/ResController.php
@@ -173,14 +173,19 @@ class ResController
         }
 
         $externalInfos = $data['externalInfos'];
-        foreach($externalInfos as $mail){            
-            $check = Validator::intType()->validate($mail['res_id']);
-            $check = $check && Validator::StringType()->notEmpty()->validate($mail['external_id']);
-            $check = $check && Validator::StringType()->notEmpty()->validate($mail['external_link']);
-            if(!$check){
-                return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
+        foreach($externalInfos as $mail){
+            if(!Validator::intType()->validate($mail['res_id'])){
+                return $response->withStatus(400)->withJson(['errors' => 'Bad Request: invalid res_id']);
             }
-            
+            if(!Validator::StringType()->notEmpty()->validate($mail['external_id'])){
+                return $response->withStatus(400)->withJson(['errors' => 'Bad Request: invalid external_id for element : '.$mail['res_id']]);
+            }
+            if(!Validator::StringType()->notEmpty()->validate($mail['external_link'])){
+                return $response->withStatus(400)->withJson(['errors' => 'Bad Request:  invalid external_link for element'.$mail['res_id']]);
+            }          
+        }
+
+        foreach($externalInfos as $mail){
             $document = ResModel::getById(['resId' => $mail['res_id'], 'select' => ['res_id']]);
             if (empty($document)) {
                 return $response->withStatus(400)->withJson(['errors' => _DOCUMENT_NOT_FOUND]);
@@ -189,8 +194,8 @@ class ResController
                 return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
             }
             ResModel::update(['set' => ['external_id' => $mail['external_id'] , 'external_link' => $mail['external_link'], 'status' => $data['status']], 'where' => ['res_id = ?'], 'data' => [$document['res_id']]]);
-            
-        }
+        }        
+
         return $response->withJson(['success' => 'success']);
     }
 
@@ -257,26 +262,31 @@ class ResController
     public function getList(Request $request, Response $response)
     {
         $data = $request->getParams();
-
-        $check = Validator::stringType()->notEmpty()->validate($data['clause']);
-        $check = $check && Validator::stringType()->notEmpty()->validate($data['select']);
+        if(!Validator::stringType()->notEmpty()->validate($data['select'])){
+            return $response->withStatus(400)->withJson(['errors' => 'Bad Request: select parameter not valid']);
+        }
+        if(!Validator::stringType()->notEmpty()->validate($data['clause'])){
+            return $response->withStatus(400)->withJson(['errors' => 'Bad Request: clause parameter not valid']);
+        }
         if(!empty($data['withFile'])){
-            $check = $check && Validator::boolType()->validate($data['withFile']);
+            if(!Validator::boolType()->validate($data['withFile'])){
+                return $response->withStatus(400)->withJson(['errors' => 'Bad Request: withFile parameter is not a boolean']);
+            }            
         }
 
         if(!empty($data['orderBy'])){
-            $check = $check && Validator::arrayType()->notEmpty()->validate($data['orderBy']);
-            $orderBy = $data['orderBy'];
+            if(!Validator::arrayType()->notEmpty()->validate($data['orderBy'])){
+                return $response->withStatus(400)->withJson(['errors' => 'Bad Request: orderBy parameter not valid']);
+            }            
         }
 
         if(!empty($data['limit'])){
-            $limit = (int) $data['limit'];
-            $check = $check && Validator::intType()->validate($limit);
-        }
-
-        if (!$check) {
-            return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
+            if(!Validator::intType()->validate($data['limit'])){
+                return $response->withStatus(400)->withJson(['errors' => 'Bad Request: limit parameter not valid']);
+            }
         }
+        $orderBy = $data['orderBy'];
+        $limit = $data['limit'];
         $select = explode(',', $data['select']);
         
         if (!PreparedClauseController::isRequestValid(['select' => $select,'clause' => $data['clause'], 'orderBy' => $orderBy, 'limit' => $limit, 'userId' => $GLOBALS['userId']])) {
-- 
GitLab