Skip to content
Snippets Groups Projects
Commit d59fa2b3 authored by Vinciane's avatar Vinciane
Browse files

FEAT #9207 correction NoteController

parent a0717f63
No related branches found
No related tags found
No related merge requests found
...@@ -37,13 +37,13 @@ class NoteController ...@@ -37,13 +37,13 @@ class NoteController
return $response->withJson($aNotes); return $response->withJson($aNotes);
} }
public function create(Request $request, Response $response) public function create(Request $request, Response $response, $aArgs)
{ {
$data = $request->getParams(); $data = $request->getParams();
//Check data //Check data
$check = Validator::stringType()->notEmpty()->validate($data['note_text']); $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']); $check = $check && Validator::stringType()->notEmpty()->validate($GLOBALS['userId']);
if (isset($data['entities_chosen'])) { if (isset($data['entities_chosen'])) {
...@@ -54,10 +54,12 @@ class NoteController ...@@ -54,10 +54,12 @@ class NoteController
return $response->withStatus(400)->withJson(['errors' => 'Bad Request']); 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']); return $response->withStatus(403)->withJson(['errors' => 'Document out of perimeter']);
} }
$data['identifier'] = $aArgs['resId'];
//Insert note in notes table and recover last insert ID //Insert note in notes table and recover last insert ID
$noteId = NoteModel::create($data); $noteId = NoteModel::create($data);
......
...@@ -23,7 +23,7 @@ class NoteControllerTest extends TestCase ...@@ -23,7 +23,7 @@ class NoteControllerTest extends TestCase
'limit' => 1, 'limit' => 1,
]); ]);
$resID = $getResId[0]['res_id']; $resID['resId'] = $getResId[0]['res_id'];
$noteController = new \Note\controllers\NoteController(); $noteController = new \Note\controllers\NoteController();
// CREATE WITH ALL DATA -> OK // CREATE WITH ALL DATA -> OK
...@@ -32,13 +32,12 @@ class NoteControllerTest extends TestCase ...@@ -32,13 +32,12 @@ class NoteControllerTest extends TestCase
$aArgs = [ $aArgs = [
'note_text' => "Test d'ajout d'une note par php unit", 'note_text' => "Test d'ajout d'une note par php unit",
'identifier' => $resID,
'entities_chosen' => ['COU', 'CAB'] 'entities_chosen' => ['COU', 'CAB']
]; ];
$fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); $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()); $responseBody = json_decode((string)$response->getBody());
self::$noteId = $responseBody->noteId; self::$noteId = $responseBody->noteId;
...@@ -73,7 +72,7 @@ class NoteControllerTest extends TestCase ...@@ -73,7 +72,7 @@ class NoteControllerTest extends TestCase
$this->assertSame(self::$noteId, $responseBody['id']); $this->assertSame(self::$noteId, $responseBody['id']);
$this->assertSame($GLOBALS['userId'], $responseBody['user_id']); $this->assertSame($GLOBALS['userId'], $responseBody['user_id']);
$this->assertSame("Test d'ajout d'une note par php unit", $responseBody['note_text']); $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->assertInternalType('array', $responseBody['entities']);
$this->assertSame('COU', $responseBody['entities'][0]); $this->assertSame('COU', $responseBody['entities'][0]);
$this->assertSame('CAB', $responseBody['entities'][1]); $this->assertSame('CAB', $responseBody['entities'][1]);
...@@ -84,13 +83,12 @@ class NoteControllerTest extends TestCase ...@@ -84,13 +83,12 @@ class NoteControllerTest extends TestCase
$request = \Slim\Http\Request::createFromEnvironment($environment); $request = \Slim\Http\Request::createFromEnvironment($environment);
$aArgs = [ $aArgs = [
'note_text' => "Test d'ajout d'une note par php unit", 'note_text' => "Test d'ajout d'une note par php unit"
'identifier' => $resID,
]; ];
$fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); $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()); $responseBody = json_decode((string)$response->getBody());
self::$noteId = $responseBody->noteId; self::$noteId = $responseBody->noteId;
...@@ -125,22 +123,21 @@ class NoteControllerTest extends TestCase ...@@ -125,22 +123,21 @@ class NoteControllerTest extends TestCase
$this->assertSame(self::$noteId, $responseBody['id']); $this->assertSame(self::$noteId, $responseBody['id']);
$this->assertSame($GLOBALS['userId'], $responseBody['user_id']); $this->assertSame($GLOBALS['userId'], $responseBody['user_id']);
$this->assertSame("Test d'ajout d'une note par php unit", $responseBody['note_text']); $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->assertInternalType('string', $responseBody['entities']);
$this->assertSame('', $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']); $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']);
$request = \Slim\Http\Request::createFromEnvironment($environment); $request = \Slim\Http\Request::createFromEnvironment($environment);
$aArgs = [ $aArgs = [
'identifier' => $resID,
'entities_chosen' => ["COU", "CAB"] 'entities_chosen' => ["COU", "CAB"]
]; ];
$fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); $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()); $responseBody = json_decode((string)$response->getBody());
$this->assertSame('Bad Request', $responseBody->errors); $this->assertSame('Bad Request', $responseBody->errors);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment