From d59fa2b36cc8e232dfd7d5d374611cc39fb8d4c9 Mon Sep 17 00:00:00 2001
From: Vinciane <vinciane.bizet@maarch.org>
Date: Thu, 27 Dec 2018 18:17:08 +0100
Subject: [PATCH] FEAT #9207 correction NoteController

---
 src/app/note/controllers/NoteController.php   |  8 +++++---
 .../unitTests/app/note/NoteControllerTest.php | 19 ++++++++-----------
 2 files changed, 13 insertions(+), 14 deletions(-)

diff --git a/src/app/note/controllers/NoteController.php b/src/app/note/controllers/NoteController.php
index f4fb1ddfe28..debd920886d 100755
--- a/src/app/note/controllers/NoteController.php
+++ b/src/app/note/controllers/NoteController.php
@@ -37,13 +37,13 @@ class NoteController
         return $response->withJson($aNotes);
     }
 
-    public function create(Request $request, Response $response)
+    public function create(Request $request, Response $response, $aArgs)
     {
         $data = $request->getParams();
 
         //Check data
         $check = Validator::stringType()->notEmpty()->validate($data['note_text']);
-        $check = $check && Validator::intVal()->notEmpty()->validate($data['identifier']); //correspond to res_id
+        $check = $check && Validator::intVal()->notEmpty()->validate($aArgs['resId']); //correspond to res_id
         $check = $check && Validator::stringType()->notEmpty()->validate($GLOBALS['userId']);
         
         if (isset($data['entities_chosen'])) {
@@ -54,10 +54,12 @@ class NoteController
             return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
         }
 
-        if (!ResController::hasRightByResId(['resId' => $data['identifier'], 'userId' => $GLOBALS['userId']])) {
+        if (!ResController::hasRightByResId(['resId' => $aArgs['resId'], 'userId' => $GLOBALS['userId']])) {
             return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
         }
         
+        $data['identifier'] = $aArgs['resId'];
+        
         //Insert note in notes table and recover last insert ID
         $noteId = NoteModel::create($data);
     
diff --git a/test/unitTests/app/note/NoteControllerTest.php b/test/unitTests/app/note/NoteControllerTest.php
index 00891e4f1e0..4b4269eb1dd 100644
--- a/test/unitTests/app/note/NoteControllerTest.php
+++ b/test/unitTests/app/note/NoteControllerTest.php
@@ -23,7 +23,7 @@ class NoteControllerTest extends TestCase
             'limit'     => 1,
         ]);
 
-        $resID = $getResId[0]['res_id'];
+        $resID['resId'] = $getResId[0]['res_id'];
         $noteController = new \Note\controllers\NoteController();
 
         // CREATE WITH ALL DATA -> OK
@@ -32,13 +32,12 @@ class NoteControllerTest extends TestCase
 
         $aArgs = [
             'note_text'         => "Test d'ajout d'une note par php unit",
-            'identifier'        => $resID,
             'entities_chosen'   => ['COU', 'CAB']
         ];
 
         $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
 
-        $response     = $noteController->create($fullRequest, new \Slim\Http\Response());
+        $response     = $noteController->create($fullRequest, new \Slim\Http\Response(), $resID);
         $responseBody = json_decode((string)$response->getBody());
 
         self::$noteId = $responseBody->noteId;
@@ -73,7 +72,7 @@ class NoteControllerTest extends TestCase
         $this->assertSame(self::$noteId, $responseBody['id']);
         $this->assertSame($GLOBALS['userId'], $responseBody['user_id']);
         $this->assertSame("Test d'ajout d'une note par php unit", $responseBody['note_text']);
-        $this->assertSame($resID, $responseBody['identifier']);
+        $this->assertSame($resID['resId'], $responseBody['identifier']);
         $this->assertInternalType('array', $responseBody['entities']);
         $this->assertSame('COU', $responseBody['entities'][0]);
         $this->assertSame('CAB', $responseBody['entities'][1]);
@@ -84,13 +83,12 @@ class NoteControllerTest extends TestCase
         $request        = \Slim\Http\Request::createFromEnvironment($environment);
 
         $aArgs = [
-            'note_text'         => "Test d'ajout d'une note par php unit",
-            'identifier'        => $resID,
+            'note_text'         => "Test d'ajout d'une note par php unit"
         ];
 
         $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
 
-        $response     = $noteController->create($fullRequest, new \Slim\Http\Response());
+        $response     = $noteController->create($fullRequest, new \Slim\Http\Response(), $resID);
         $responseBody = json_decode((string)$response->getBody());
 
         self::$noteId = $responseBody->noteId;
@@ -125,22 +123,21 @@ class NoteControllerTest extends TestCase
         $this->assertSame(self::$noteId, $responseBody['id']);
         $this->assertSame($GLOBALS['userId'], $responseBody['user_id']);
         $this->assertSame("Test d'ajout d'une note par php unit", $responseBody['note_text']);
-        $this->assertSame($resID, $responseBody['identifier']);
+        $this->assertSame($resID['resId'], $responseBody['identifier']);
         $this->assertInternalType('string', $responseBody['entities']);
         $this->assertSame('', $responseBody['entities']);
 
-        // CREATE WITH A REQUERY MISSING DATA -> NOT OK
+        // CREATE WITH NOTE_TEXT MISSING -> NOT OK
         $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']);
         $request        = \Slim\Http\Request::createFromEnvironment($environment);
 
         $aArgs = [
-            'identifier'        => $resID,
             'entities_chosen' => ["COU", "CAB"]
         ];
 
         $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
 
-        $response     = $noteController->create($fullRequest, new \Slim\Http\Response());
+        $response     = $noteController->create($fullRequest, new \Slim\Http\Response(), $resID);
         $responseBody = json_decode((string)$response->getBody());
 
         $this->assertSame('Bad Request', $responseBody->errors);
-- 
GitLab