From a8b3bcc0855c010e8b941d601223fb83e87d6a51 Mon Sep 17 00:00:00 2001 From: Guillaume Heurtier <guillaume.heurtier@maarch.org> Date: Thu, 23 Apr 2020 15:47:02 +0200 Subject: [PATCH] FEAT #13676 TIME 9:00 improved contacts unit tests --- .../contact/controllers/ContactController.php | 77 +- .../controllers/ContactGroupController.php | 2 +- src/app/contact/models/ContactModel.php | 20 - .../app/contact/ContactControllerTest.php | 848 +++++++++++++++++- .../ContactCustomFieldControllerTest.php | 155 +++- .../contact/ContactGroupControllerTest.php | 218 ++++- 6 files changed, 1183 insertions(+), 137 deletions(-) diff --git a/src/app/contact/controllers/ContactController.php b/src/app/contact/controllers/ContactController.php index d3f344c9739..255a94b4793 100755 --- a/src/app/contact/controllers/ContactController.php +++ b/src/app/contact/controllers/ContactController.php @@ -633,7 +633,7 @@ class ContactController return $response->withJson(['contacts' => $contacts]); } - public static function getLightFormattedContact(Request $request, Response $response, array $args) + public function getLightFormattedContact(Request $request, Response $response, array $args) { if (!Validator::intVal()->notEmpty()->validate($args['id'])) { return $response->withStatus(400)->withJson(['errors' => 'Query params id is not an integer']); @@ -659,86 +659,13 @@ class ContactController return $response->withJson(['contact' => $contact]); } - public static function getCivilities(Request $request, Response $response) + public function getCivilities(Request $request, Response $response) { $civilities = ContactModel::getCivilities(); return $response->withJson(['civilities' => $civilities]); } - public static function getFormattedContactsForSearchV1(Request $request, Response $response) - { - $data = $request->getParsedBody(); - - $return = ''; - - if (!isset($data['resId']) && !isset($data['mode'])) { - $status = 1; - $return .= '<td colspan="6" style="background-color: red;">'; - $return .= '<p style="padding: 10px; color: black;">'; - $return .= 'Erreur lors du chargement des contacts'; - $return .= '</p>'; - $return .= '</td>'; - - return $response->withJson(['status' => $status, 'toShow' => $return]); - } - - $status = 0; - $return .= '<td>'; - $return .= '<div align="center">'; - $return .= '<table width="100%">'; - - $resourceContacts = ResourceContactModel::get([ - 'where' => ['res_id = ?', 'mode = ?'], - 'data' => [$data['resId'], $data['mode']] - ]); - - $mode = ''; - if ($data['mode'] == 'sender') { - $mode = _SENDER; - } elseif ($data['mode'] == 'recipient') { - $mode = _RECIPIENT; - } - - foreach ($resourceContacts as $resourceContact) { - $return .= '<tr>'; - $return .= '<td style="background: transparent; border: 0px dashed rgb(200, 200, 200);">'; - - $return .= '<div style="text-align: left; background-color: rgb(230, 230, 230); padding: 3px; margin-left: 20px; margin-top: -6px;">'; - - if ($resourceContact['type'] == 'contact') { - $contactRaw = ContactModel::getById([ - 'select' => ['*'], - 'id' => $resourceContact['item_id'] - ]); - - $contactToDisplay = ContactController::getFormattedContactWithAddress(['contact' => $contactRaw]); - - $return .= '<span style="font-size:10px;color:#135F7F;">' . $mode . '</span> - '; - $return .= $contactToDisplay['contact']['otherInfo']; - } elseif ($resourceContact['type'] == 'user') { - $return .= '<span style="font-size:10px;color:#135F7F;">' . $mode . ' (interne)</span> - '; - $return .= UserModel::getLabelledUserById(['id' => $resourceContact['item_id']]); - } elseif ($resourceContact['type'] == 'entity') { - $return .= '<span style="font-size:10px;color:#135F7F;">' . $mode . ' (interne)</span> - '; - $entity = EntityModel::getById(['id' => $resourceContact['item_id'], 'select' => ['entity_label']]); - $return .= $entity['entity_label']; - } - - $return .= '</div>'; - - $return .= '</td>'; - $return .= '</tr>'; - } - - $return .= '</table>'; - $return .= '<br />'; - $return .= '</div>'; - $return .= '</td>'; - - return $response->withJson(['status' => $status, 'toShow' => $return]); - } - public static function getFillingRate(array $aArgs) { ValidatorModel::notEmpty($aArgs, ['contactId']); diff --git a/src/app/contact/controllers/ContactGroupController.php b/src/app/contact/controllers/ContactGroupController.php index bacdf399687..08ee61165ca 100755 --- a/src/app/contact/controllers/ContactGroupController.php +++ b/src/app/contact/controllers/ContactGroupController.php @@ -74,7 +74,7 @@ class ContactGroupController return $response->withStatus(400)->withJson(['errors' => _CONTACTS_GROUP_LABEL_ALREADY_EXISTS]); } - if ($GLOBALS['login'] = 'superadmin') { + if ($GLOBALS['login'] == 'superadmin') { $entityOwner = 'superadmin'; } else { $primaryEntity = UserModel::getPrimaryEntityById(['id' => $GLOBALS['id'], 'select' => ['entities.entity_id']]); diff --git a/src/app/contact/models/ContactModel.php b/src/app/contact/models/ContactModel.php index c8c76b58c48..f84ecb7ee80 100755 --- a/src/app/contact/models/ContactModel.php +++ b/src/app/contact/models/ContactModel.php @@ -106,26 +106,6 @@ class ContactModel return true; } - public static function getContactCommunication(array $aArgs) - { - ValidatorModel::notEmpty($aArgs, ['contactId']); - ValidatorModel::intVal($aArgs, ['contactId']); - - $aReturn = DatabaseModel::select([ - 'select' => ['*'], - 'table' => ['contact_communication'], - 'where' => ['contact_id = ?'], - 'data' => [$aArgs['contactId']], - ]); - - if (empty($aReturn)) { - return ""; - } else { - $aReturn[0]['value'] = trim(trim($aReturn[0]['value']), '/'); - return $aReturn[0]; - } - } - public static function purgeContact($aArgs) { ValidatorModel::notEmpty($aArgs, ['id']); diff --git a/test/unitTests/app/contact/ContactControllerTest.php b/test/unitTests/app/contact/ContactControllerTest.php index f2cad4c3afb..7779ee86431 100755 --- a/test/unitTests/app/contact/ContactControllerTest.php +++ b/test/unitTests/app/contact/ContactControllerTest.php @@ -12,6 +12,8 @@ use PHPUnit\Framework\TestCase; class ContactControllerTest extends TestCase { private static $id = null; + private static $id2 = null; + private static $resId = null; public function testCreate() { @@ -36,6 +38,9 @@ class ContactControllerTest extends TestCase 'email' => 'hal.jordan@glc.com', 'phone' => '911', 'notes' => 'In brightest day', + 'externalId' => [ + 'm2m' => '45239273100025/PJS' + ] ]; $fullRequest = \httpRequestCustom::addContentInBody($args, $request); @@ -46,6 +51,57 @@ class ContactControllerTest extends TestCase $this->assertIsInt($responseBody['id']); self::$id = $responseBody['id']; + $args = [ + 'civility' => 'title1', + 'firstname' => 'Hal', + 'lastname' => 'Jordan', + 'company' => 'Green Lantern Corps', + 'department' => 'Sector 2814', + 'function' => 'member', + 'addressNumber' => '1', + 'addressStreet' => 'somewhere', + 'addressPostcode' => '99000', + 'addressTown' => 'Bluehaven', + 'addressCountry' => 'USA', + 'email' => 'hal.jordan@glc.com', + 'phone' => '911', + 'notes' => 'In brightest day' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactController->create($fullRequest, new \Slim\Http\Response()); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame(self::$id, $responseBody['id']); + + $args2 = [ + 'civility' => 'title1', + 'firstname' => 'Dwight', + 'lastname' => 'Schrute', + 'company' => 'Dunder Mifflin Paper Company Inc.', + 'department' => 'Sales', + 'function' => 'Salesman', + 'addressNumber' => '1725', + 'addressStreet' => 'Slough Avenue', + 'addressPostcode' => '18505', + 'addressTown' => 'Scranton', + 'addressCountry' => 'USA', + 'email' => 'dschrute@dundermifflin.com', + 'phone' => '5705551212', + 'notes' => 'Assistant to the regional manager', + 'communicationMeans' => 'dschrute@dundermifflin.com', + 'externalId' => [ + 'schruteFarmId' => 1, + 'm2m' => '45239273100025/PJS' + ] + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args2, $request); + + $response = $contactController->create($fullRequest, new \Slim\Http\Response()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame(200, $response->getStatusCode()); + $this->assertIsInt($responseBody['id']); + self::$id2 = $responseBody['id']; // GET $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); @@ -81,6 +137,16 @@ class ContactControllerTest extends TestCase $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']); $request = \Slim\Http\Request::createFromEnvironment($environment); + $args = [ + + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactController->create($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Body is not set or empty', $responseBody['errors']); + $args = [ 'civility' => 'title1', 'firstname' => 'Hal', @@ -100,8 +166,165 @@ class ContactControllerTest extends TestCase $response = $contactController->create($fullRequest, new \Slim\Http\Response()); $this->assertSame(400, $response->getStatusCode()); $responseBody = json_decode((string)$response->getBody(), true); - $this->assertSame('Body lastname or company is mandatory', $responseBody['errors']); + + $args = [ + 'civility' => 'title1', + 'firstname' => 'Dwight', + 'lastname' => 'Schrute', + 'company' => 'Dunder Mifflin Paper Company Inc.', + 'department' => 'Sales', + 'function' => 'Salesman', + 'addressNumber' => '1725', + 'addressStreet' => 'Slough Avenue', + 'addressPostcode' => '18505', + 'addressTown' => 'Scranton', + 'addressCountry' => 'USA', + 'email' => 'dschrute@schrutefarms.com', + 'phone' => '5705551212', + 'notes' => 'Assistant to the regional manager', + 'communicationMeans' => 'wrong format' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactController->create($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame(_COMMUNICATION_MEANS_VALIDATOR, $responseBody['errors']); + + + $args = [ + 'civility' => 'title1', + 'firstname' => 'Dwight', + 'lastname' => 'Schrute', + 'company' => 'Dunder Mifflin Paper Company Inc.', + 'department' => 'Sales', + 'function' => 'Salesman', + 'addressNumber' => '1725', + 'addressStreet' => 'Slough Avenue', + 'addressPostcode' => '18505', + 'addressTown' => 'Scranton', + 'addressCountry' => 'USA', + 'email' => 'wrong email format', + 'phone' => '5705551212', + 'notes' => 'Assistant to the regional manager', + 'customFields' => 'wrong format' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactController->create($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Body email is not valid', $responseBody['errors']); + + $args = [ + 'civility' => 'title1', + 'firstname' => 'Dwight', + 'lastname' => 'Schrute', + 'company' => 'Dunder Mifflin Paper Company Inc.', + 'department' => 'Sales', + 'function' => 'Salesman', + 'addressNumber' => '1725', + 'addressStreet' => 'Slough Avenue', + 'addressPostcode' => '18505', + 'addressTown' => 'Scranton', + 'addressCountry' => 'USA', + 'email' => 'dschrute@schrutefarms.com', + 'phone' => 'wrong format', + 'notes' => 'Assistant to the regional manager', + 'communicationMeans' => 'wrong format' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactController->create($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Body phone is not valid', $responseBody['errors']); + + $args = [ + 'civility' => 'title1', + 'firstname' => 'Dwight', + 'lastname' => 'Schrute', + 'company' => 'Dunder Mifflin Paper Company Inc.', + 'department' => 'Sales', + 'function' => 'Salesman', + 'addressNumber' => '1725', + 'addressStreet' => 'Slough Avenue', + 'addressPostcode' => '18505', + 'addressTown' => 'Scranton', + 'addressCountry' => 'USA', + 'email' => 'dschrute@schrutefarms.com', + 'phone' => '5705551212', + 'notes' => 'Assistant to the regional manager', + 'customFields' => [1 => 'wrong field'] + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactController->create($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Body customFields : One or more custom fields do not exist', $responseBody['errors']); + + $args = [ + 'civility' => 'title1', + 'firstname' => 'Dwight', + 'lastname' => 'Schrute', + 'company' => 'Dunder Mifflin Paper Company Inc.', + 'department' => 'Sales', + 'function' => 'Salesman', + 'addressNumber' => '1725', + 'addressStreet' => 'Slough Avenue', + 'addressPostcode' => '18505', + 'addressTown' => 'Scranton', + 'addressCountry' => 'USA', + 'email' => 'dschrute@schrutefarms.com', + 'phone' => '5705551212', + 'notes' => 'Assistant to the regional manager', + 'customFields' => 'wrong format' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactController->create($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Body customFields is not an array', $responseBody['errors']); + + $args = [ + 'civility' => 'title1', + 'firstname' => 'Dwight', + 'lastname' => 'Schrute', + 'company' => 'Dunder Mifflin Paper Company Inc. blablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablablalablablablablablablablablablabla', + 'department' => 'Sales', + 'function' => 'Salesman', + 'addressNumber' => '1725', + 'addressStreet' => 'Slough Avenue', + 'addressPostcode' => '18505', + 'addressTown' => 'Scranton', + 'addressCountry' => 'USA', + 'email' => 'dschrute@schrutefarms.com', + 'phone' => '5705551212', + 'notes' => 'Assistant to the regional manager', + 'customFields' => 'wrong format' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactController->create($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Body company length is not valid (1..256)', $responseBody['errors']); + + $GLOBALS['login'] = 'ddur'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $contactController->create($fullRequest, new \Slim\Http\Response()); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Service forbidden', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; } public function testUpdate() @@ -162,6 +385,28 @@ class ContactControllerTest extends TestCase $this->assertNotNull($responseBody['creationDate']); $this->assertNotNull($responseBody['modificationDate']); + $args = [ + 'civility' => 'title1', + 'firstname' => 'Dwight', + 'lastname' => 'Schrute', + 'company' => 'Dunder Mifflin Paper Company Inc.', + 'department' => 'Sales', + 'function' => 'Salesman', + 'addressNumber' => '1725', + 'addressStreet' => 'Slough Avenue', + 'addressPostcode' => '18505', + 'addressTown' => 'Scranton', + 'addressCountry' => 'USA', + 'email' => 'dschrute@schrutefarms.com', + 'phone' => '5705551212', + 'notes' => 'Assistant to the regional manager', + 'externalId' => ['schruteFarmId' => 2] + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id2]); + $this->assertSame(204, $response->getStatusCode()); + // ERRORS $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); @@ -186,8 +431,48 @@ class ContactControllerTest extends TestCase $response = $contactController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); $this->assertSame(400, $response->getStatusCode()); $responseBody = json_decode((string)$response->getBody(), true); - $this->assertSame('Body lastname or company is mandatory', $responseBody['errors']); + + $args = [ + 'civility' => 'title1', + 'firstname' => 'Dwight', + 'lastname' => 'Schrute', + 'company' => 'Dunder Mifflin Paper Company Inc.', + 'department' => 'Sales', + 'function' => 'Salesman', + 'addressNumber' => '1725', + 'addressStreet' => 'Slough Avenue', + 'addressPostcode' => '18505', + 'addressTown' => 'Scranton', + 'addressCountry' => 'USA', + 'email' => 'dschrute@schrutefarms.com', + 'phone' => '5705551212', + 'notes' => 'Assistant to the regional manager' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id2 * 1000]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Contact does not exist', $responseBody['errors']); + + $response = $contactController->update($fullRequest, new \Slim\Http\Response(), ['id' => 'wrong format']); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Route id is not an integer', $responseBody['errors']); + + $GLOBALS['login'] = 'ddur'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $contactController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id2]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Service forbidden', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; } public function testGet() @@ -198,59 +483,311 @@ class ContactControllerTest extends TestCase $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $response = $contactController->get($request, new \Slim\Http\Response()); + $queryParams = [ + 'orderBy' => 'filling' + ]; + $fullRequest = $request->withQueryParams($queryParams); + + $response = $contactController->get($fullRequest, new \Slim\Http\Response()); $this->assertSame(200, $response->getStatusCode()); $responseBody = json_decode((string)$response->getBody(), true); + $this->assertIsArray($responseBody['contacts']); + $this->assertNotEmpty($responseBody['contacts']); + $this->assertNotNull($responseBody['contacts'][0]['id']); $this->assertNotNull($responseBody['contacts'][0]['lastname']); $this->assertNotNull($responseBody['contacts'][0]['company']); + + $queryParams = [ + 'search' => 'Michael Scott', + 'orderBy' => 'filling' + ]; + $fullRequest = $request->withQueryParams($queryParams); + + $response = $contactController->get($fullRequest, new \Slim\Http\Response()); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertIsArray($responseBody['contacts']); + $this->assertEmpty($responseBody['contacts']); + $this->assertSame(0, $responseBody['count']); + + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $contactController->get($request, new \Slim\Http\Response()); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Service forbidden', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; } - public function testDelete() + public function testControlLengthNameAfnor() + { + $name = \Contact\controllers\ContactController::controlLengthNameAfnor(['civility' => 'title1', 'fullName' => 'Prénom NOM', 'strMaxLength' => 38]); + + $this->assertSame('Monsieur Prénom NOM', $name); + + $name = \Contact\controllers\ContactController::controlLengthNameAfnor(['civility' => 'title3', 'fullName' => 'Prénom NOM TROP LOOOOOOOOOOOOONG', 'strMaxLength' => 38]); + + $this->assertSame('Mlle Prénom NOM TROP LOOOOOOOOOOOOONG', $name); + } + + public function testGetContactAfnor() { $contactController = new \Contact\controllers\ContactController(); - // DELETE - $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']); + // GET + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $response = $contactController->delete($request, new \Slim\Http\Response(), ['id' => self::$id]); - $this->assertSame(204, $response->getStatusCode()); + $queryParams = [ + 'orderBy' => 'filling' + ]; + $fullRequest = $request->withQueryParams($queryParams); + + $contact = \Contact\models\ContactModel::getById([ + 'select' => [ + 'company', 'firstname', 'lastname', 'civility', 'address_additional1', 'address_additional2', 'address_number', + 'address_street', 'address_postcode', 'address_town' + ], + 'id' => self::$id2 + ]); + + $response = $contactController::getContactAfnor($contact); + + $this->assertIsArray($response); + $this->assertNotEmpty($response); + $this->assertSame('Afnor', $response[0]); + $this->assertSame('Dunder Mifflin Paper Company Inc.', $response[1]); + $this->assertSame('Monsieur Dwight Schrute', $response[2]); + $this->assertEmpty($response[3]); + $this->assertSame('1725 SLOUGH AVENUE', $response[4]); + $this->assertEmpty($response[5]); + $this->assertSame('18505 SCRANTON', $response[6]); + } + + public function testGetByResId() + { + $resController = new \Resource\controllers\ResController(); + + // CREATE + $GLOBALS['login'] = 'cchaplin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $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); + + $argsMailNew = [ + 'modelId' => 1, + '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', + 'doctype' => 102, + 'destination' => 15, + 'initiator' => 15, + 'subject' => 'Breaking News : Superman is dead again - PHP unit', + 'typist' => 19, + 'priority' => 'poiuytre1357nbvc', + 'followed' => true, + 'diffusionList' => [ + [ + 'id' => 11, + 'type' => 'user', + 'mode' => 'dest' + ] + ], + 'senders' => [ + [ + 'id' => self::$id2, + 'type' => 'contact' + ] + ], + 'recipients' => [ + [ + 'id' => $GLOBALS['id'], + 'type' => 'user' + ], + [ + 'id' => 1, + 'type' => 'entity' + ], + + ] + ]; + + $fullRequest = httpRequestCustom::addContentInBody($argsMailNew, $request); + + $response = $resController->create($fullRequest, new \Slim\Http\Response()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertIsInt($responseBody['resId']); + self::$resId = $responseBody['resId']; + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $contactController = new \Contact\controllers\ContactController(); // GET $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $response = $contactController->getById($request, new \Slim\Http\Response(), ['id' => self::$id]); - $this->assertSame(400, $response->getStatusCode()); + // Senders + $queryParams = [ + 'type' => 'senders' + ]; + $fullRequest = $request->withQueryParams($queryParams); + $response = $contactController->getByResId($fullRequest, new \Slim\Http\Response(), ['resId' => self::$resId]); + $this->assertSame(200, $response->getStatusCode()); $responseBody = json_decode((string)$response->getBody(), true); - $this->assertSame('Contact does not exist', $responseBody['errors']); + $this->assertIsArray($responseBody['contacts']); + $this->assertNotEmpty($responseBody['contacts']); + $this->assertSame('contact', $responseBody['contacts'][0]['type']); + $this->assertSame('Dwight', $responseBody['contacts'][0]['firstname']); + $this->assertSame('Schrute', $responseBody['contacts'][0]['lastname']); + $this->assertSame('Dunder Mifflin Paper Company Inc.', $responseBody['contacts'][0]['company']); + $this->assertSame('Sales', $responseBody['contacts'][0]['department']); + $this->assertSame('Salesman', $responseBody['contacts'][0]['function']); + $this->assertSame('1725', $responseBody['contacts'][0]['addressNumber']); + $this->assertSame('Slough Avenue', $responseBody['contacts'][0]['addressStreet']); + $this->assertEmpty($responseBody['contacts'][0]['addressAdditional1']); + $this->assertEmpty($responseBody['contacts'][0]['addressAdditional2']); + $this->assertSame('18505', $responseBody['contacts'][0]['addressPostcode']); + $this->assertSame('Scranton', $responseBody['contacts'][0]['addressTown']); + $this->assertSame('USA', $responseBody['contacts'][0]['addressCountry']); + $this->assertSame('dschrute@schrutefarms.com', $responseBody['contacts'][0]['email']); + $this->assertSame('5705551212', $responseBody['contacts'][0]['phone']); + $this->assertEmpty($responseBody['contacts'][0]['communicationMeans']); + $this->assertSame('Assistant to the regional manager', $responseBody['contacts'][0]['notes']); + $this->assertSame($GLOBALS['id'], $responseBody['contacts'][0]['creator']); + $this->assertSame(true, $responseBody['contacts'][0]['enabled']); + $this->assertEmpty($responseBody['contacts'][0]['customFields']); + $this->assertIsArray($responseBody['contacts'][0]['externalId']); + $this->assertSame(2, $responseBody['contacts'][0]['externalId']['schruteFarmId']); + $this->assertIsArray($responseBody['contacts'][0]['fillingRate']); + $this->assertSame(100, $responseBody['contacts'][0]['fillingRate']['rate']); + $this->assertSame('third', $responseBody['contacts'][0]['fillingRate']['thresholdLevel']); - // ERRORS - $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']); - $request = \Slim\Http\Request::createFromEnvironment($environment); + // Recipients + $queryParams = [ + 'type' => 'recipients' + ]; + $fullRequest = $request->withQueryParams($queryParams); + $response = $contactController->getByResId($fullRequest, new \Slim\Http\Response(), ['resId' => self::$resId]); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); - $response = $contactController->delete($request, new \Slim\Http\Response(), ['id' => self::$id]); - $this->assertSame(400, $response->getStatusCode()); + $this->assertIsArray($responseBody['contacts']); + $this->assertNotEmpty($responseBody['contacts']); + + $this->assertSame('user', $responseBody['contacts'][0]['type']); + $this->assertSame('Charlie', $responseBody['contacts'][0]['firstname']); + $this->assertSame('CHAPLIN', $responseBody['contacts'][0]['lastname']); + $this->assertEmpty($responseBody['contacts'][0]['company']); + $this->assertSame('Ville de Maarch-les-bains', $responseBody['contacts'][0]['department']); + $this->assertEmpty($responseBody['contacts'][0]['function']); + $this->assertEmpty($responseBody['contacts'][0]['addressNumber']); + $this->assertEmpty($responseBody['contacts'][0]['addressStreet']); + $this->assertEmpty($responseBody['contacts'][0]['addressAdditional1']); + $this->assertEmpty($responseBody['contacts'][0]['addressAdditional2']); + $this->assertEmpty($responseBody['contacts'][0]['addressPostcode']); + $this->assertEmpty($responseBody['contacts'][0]['addressTown']); + $this->assertEmpty($responseBody['contacts'][0]['addressCountry']); + $this->assertSame('support@maarch.fr', $responseBody['contacts'][0]['email']); + $this->assertEmpty($responseBody['contacts'][0]['phone']); + $this->assertEmpty($responseBody['contacts'][0]['communicationMeans']); + $this->assertEmpty($responseBody['contacts'][0]['notes']); + $this->assertEmpty($responseBody['contacts'][0]['creator']); + $this->assertEmpty($responseBody['contacts'][0]['enabled']); + $this->assertEmpty($responseBody['contacts'][0]['customFields']); + $this->assertEmpty($responseBody['contacts'][0]['externalId']); + $this->assertEmpty($responseBody['contacts'][0]['fillingRate']); + + $this->assertSame('entity', $responseBody['contacts'][1]['type']); + $this->assertEmpty($responseBody['contacts'][1]['firstname']); + $this->assertSame('Ville de Maarch-les-bains', $responseBody['contacts'][1]['lastname']); + $this->assertEmpty($responseBody['contacts'][1]['company']); + $this->assertEmpty($responseBody['contacts'][1]['department']); + $this->assertEmpty($responseBody['contacts'][1]['function']); + $this->assertEmpty($responseBody['contacts'][1]['addressNumber']); + $this->assertEmpty($responseBody['contacts'][1]['addressStreet']); + $this->assertEmpty($responseBody['contacts'][1]['addressAdditional1']); + $this->assertEmpty($responseBody['contacts'][1]['addressAdditional2']); + $this->assertEmpty($responseBody['contacts'][1]['addressPostcode']); + $this->assertEmpty($responseBody['contacts'][1]['addressTown']); + $this->assertEmpty($responseBody['contacts'][1]['addressCountry']); + $this->assertSame('support@maarch.fr', $responseBody['contacts'][1]['email']); + $this->assertEmpty($responseBody['contacts'][1]['phone']); + $this->assertEmpty($responseBody['contacts'][1]['communicationMeans']); + $this->assertEmpty($responseBody['contacts'][1]['notes']); + $this->assertEmpty($responseBody['contacts'][1]['creator']); + $this->assertEmpty($responseBody['contacts'][1]['enabled']); + $this->assertEmpty($responseBody['contacts'][1]['customFields']); + $this->assertEmpty($responseBody['contacts'][1]['externalId']); + $this->assertEmpty($responseBody['contacts'][1]['fillingRate']); + + // No contact type provided + $response = $contactController->getByResId($request, new \Slim\Http\Response(), ['resId' => self::$resId]); + $this->assertSame(200, $response->getStatusCode()); $responseBody = json_decode((string)$response->getBody(), true); + $this->assertIsArray($responseBody['contacts']); + $this->assertEmpty($responseBody['contacts']); - $this->assertSame('Contact does not exist', $responseBody['errors']); - } + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + $response = $contactController->getByResId($request, new \Slim\Http\Response(), ['resId' => self::$resId]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Document out of perimeter', $responseBody['errors']); - public function testControlLengthNameAfnor() + // Delete resource + \Resource\models\ResourceContactModel::delete(['where' => ['res_id = ?'], 'data' => [self::$resId]]); + \Resource\models\ResModel::delete([ + 'where' => ['res_id = ?'], + 'data' => [self::$resId] + ]); + + $res = \Resource\models\ResModel::getById(['resId' => self::$resId, 'select' => ['*']]); + $this->assertIsArray($res); + $this->assertEmpty($res); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + } + + public function testGetCivilities() { - $name = \Contact\controllers\ContactController::controlLengthNameAfnor(['civility' => 'title1', 'fullName' => 'Prénom NOM', 'strMaxLength' => 38]); + $contactController = new \Contact\controllers\ContactController(); - $this->assertSame('Monsieur Prénom NOM', $name); + // GET + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); - $name = \Contact\controllers\ContactController::controlLengthNameAfnor(['civility' => 'title3', 'fullName' => 'Prénom NOM TROP LOOOOOOOOOOOOONG', 'strMaxLength' => 38]); + $response = $contactController->getCivilities($request, new \Slim\Http\Response()); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); - $this->assertSame('Mlle Prénom NOM TROP LOOOOOOOOOOOOONG', $name); + $this->assertIsArray($responseBody['civilities']); + $this->assertNotEmpty($responseBody['civilities']); } public function testGetAvailableDepartments() @@ -267,16 +804,279 @@ class ContactControllerTest extends TestCase $this->assertNotEmpty($responseBody['departments']); } + public function testGetLightFormattedContact() + { + $contactController = new \Contact\controllers\ContactController(); + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $response = $contactController->getLightFormattedContact($request, new \Slim\Http\Response(), ['id' => self::$id2, 'type' => 'contact']); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertSame('Dwight', $responseBody['contact']['firstname']); + $this->assertSame('Schrute', $responseBody['contact']['lastname']); + $this->assertSame('Dunder Mifflin Paper Company Inc.', $responseBody['contact']['company']); + $this->assertSame('1725', $responseBody['contact']['addressNumber']); + $this->assertSame('Slough Avenue', $responseBody['contact']['addressStreet']); + $this->assertSame('18505', $responseBody['contact']['addressPostcode']); + $this->assertSame('Scranton', $responseBody['contact']['addressTown']); + $this->assertSame('USA', $responseBody['contact']['addressCountry']); + + $response = $contactController->getLightFormattedContact($request, new \Slim\Http\Response(), ['id' => 1, 'type' => 'user']); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertIsString($responseBody['contact']['firstname']); + $this->assertIsString($responseBody['contact']['lastname']); + + $response = $contactController->getLightFormattedContact($request, new \Slim\Http\Response(), ['id' => 1, 'type' => 'entity']); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertIsString($responseBody['contact']['label']); + + // Fail + $response = $contactController->getLightFormattedContact($request, new \Slim\Http\Response(), ['id' => 'wrong format']); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Query params id is not an integer', $responseBody['errors']); + + $response = $contactController->getLightFormattedContact($request, new \Slim\Http\Response(), ['id' => self::$id * 1000, 'type' => 'contact']); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Contact does not exist', $responseBody['errors']); + + $response = $contactController->getLightFormattedContact($request, new \Slim\Http\Response(), ['id' => self::$id * 1000, 'type' => 'user']); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Contact does not exist', $responseBody['errors']); + + $response = $contactController->getLightFormattedContact($request, new \Slim\Http\Response(), ['id' => self::$id * 1000, 'type' => 'entity']); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Contact does not exist', $responseBody['errors']); + + } + + public function testUpdateContactsParameters() + { + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $contactController = new \Contact\controllers\ContactController(); + + // Success + $body = [ + 'contactsParameters' => [ + 'id' => 5, // department + 'label' => null, + 'mandatory' => true, + 'filling' => true, + 'searchable' => true, + 'displayable' => true + ], + 'contactsFilling' => [ + 'enable' => true, + 'first_threshold' => 50, + 'second_threshold' => 80, + ] + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $contactController->updateContactsParameters($fullRequest, new \Slim\Http\Response()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame(200, $response->getStatusCode()); + $this->assertSame('success', $responseBody['success']); + + // Fail + $body = [ ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $contactController->updateContactsParameters($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Bad Request', $responseBody['errors']); + + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $contactController->updateContactsParameters($request, new \Slim\Http\Response()); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Service forbidden', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + } + public function testGetContactsParameters() { $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); $request = \Slim\Http\Request::createFromEnvironment($environment); + $customParameterId = \Contact\models\ContactParameterModel::create(['identifier' => 'contactCustomField_1000']); + $contactController = new \Contact\controllers\ContactController(); $response = $contactController->getContactsParameters($request, new \Slim\Http\Response()); - $responseBody = json_decode((string)$response->getBody()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertIsArray($responseBody['contactsFilling']); + $this->assertNotEmpty($responseBody['contactsFilling']); + $this->assertNotEmpty($responseBody['contactsParameters']); + $this->assertNotEmpty($responseBody['contactsParameters']); + $this->assertSame(false, $responseBody['annuaryEnabled']); + + \Contact\models\ContactParameterModel::delete([ + 'where' => ['id = ?'], + 'data' => [$customParameterId] + ]); + } + + public function testUpdateActivation() + { + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $contactController = new \Contact\controllers\ContactController(); + + // Success + $response = $contactController->updateActivation($request, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(204, $response->getStatusCode()); + + $contact = \Contact\models\ContactModel::getById([ + 'select' => ['enabled'], + 'id' => self::$id + ]); + $this->assertSame(false, $contact['enabled']); + + $body = [ + 'enabled' => true + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + $response = $contactController->updateActivation($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(204, $response->getStatusCode()); + + $contact = \Contact\models\ContactModel::getById([ + 'select' => ['enabled'], + 'id' => self::$id + ]); + $this->assertSame(true, $contact['enabled']); + + // Fail + $response = $contactController->updateActivation($request, new \Slim\Http\Response(), ['id' => 'wrong format']); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Route id is not an integer', $responseBody['errors']); + + $response = $contactController->updateActivation($request, new \Slim\Http\Response(), ['id' => self::$id * 1000]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Contact does not exist', $responseBody['errors']); $this->assertIsArray((array)$responseBody->contactsFilling); + + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $contactController->updateActivation($request, new \Slim\Http\Response(), ['id' => self::$id * 1000]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Service forbidden', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + } + + public function testDelete() + { + $contactController = new \Contact\controllers\ContactController(); + + // DELETE + + // ERRORS + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $response = $contactController->delete($request, new \Slim\Http\Response(), ['id' => self::$id * 1000]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Contact does not exist', $responseBody['errors']); + + $response = $contactController->delete($request, new \Slim\Http\Response(), ['id' => ['wrong format']]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Route id is not an integer', $responseBody['errors']); + + $queryParams = [ + 'redirect' => 'wrong format' + ]; + $fullRequest = $request->withQueryParams($queryParams); + + $response = $contactController->delete($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Query param redirect is not an integer', $responseBody['errors']); + + $queryParams = [ + 'redirect' => self::$id + ]; + $fullRequest = $request->withQueryParams($queryParams); + + $response = $contactController->delete($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Cannot redirect to contact you are deleting', $responseBody['errors']); + + $queryParams = [ + 'redirect' => self::$id * 1000 + ]; + $fullRequest = $request->withQueryParams($queryParams); + + $response = $contactController->delete($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Contact does not exist', $responseBody['errors']); + + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $contactController->delete($fullRequest, new \Slim\Http\Response(), ['id' => self::$id2]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Service forbidden', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + // Success + $queryParams = [ + 'redirect' => self::$id + ]; + $fullRequest = $request->withQueryParams($queryParams); + + $response = $contactController->delete($fullRequest, new \Slim\Http\Response(), ['id' => self::$id2]); + $this->assertSame(204, $response->getStatusCode()); + + $response = $contactController->delete($request, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(204, $response->getStatusCode()); + + // GET + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $response = $contactController->getById($request, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertSame('Contact does not exist', $responseBody['errors']); } // public function testUpdateFilling() diff --git a/test/unitTests/app/contact/ContactCustomFieldControllerTest.php b/test/unitTests/app/contact/ContactCustomFieldControllerTest.php index a4adc7871f6..a76970ade64 100644 --- a/test/unitTests/app/contact/ContactCustomFieldControllerTest.php +++ b/test/unitTests/app/contact/ContactCustomFieldControllerTest.php @@ -12,6 +12,7 @@ use PHPUnit\Framework\TestCase; class ContactCustomFieldControllerTest extends TestCase { private static $id = null; + private static $id2 = null; public function testCreate() { @@ -36,14 +37,29 @@ class ContactCustomFieldControllerTest extends TestCase self::$id = $responseBody['id']; + $args = [ + 'label' => 'my second custom', + 'type' => 'select', + 'values' => ['one', 'two'] + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactCustomFieldController->create($fullRequest, new \Slim\Http\Response()); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertIsInt($responseBody['id']); + + self::$id2 = $responseBody['id']; + // Errors $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']); $request = \Slim\Http\Request::createFromEnvironment($environment); $args = [ - 'label' => 'mon custom', - 'type' => 'select', - 'values' => ['one', 'two'] + 'label' => 'mon custom', + 'type' => 'select', + 'values' => ['one', 'two'] ]; $fullRequest = \httpRequestCustom::addContentInBody($args, $request); @@ -52,6 +68,51 @@ class ContactCustomFieldControllerTest extends TestCase $responseBody = json_decode((string)$response->getBody(), true); $this->assertSame('Custom field with this label already exists', $responseBody['errors']); + + $args = [ + + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactCustomFieldController->create($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Body label is empty or not a string', $responseBody['errors']); + + $args = [ + 'label' => 'mon custom' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactCustomFieldController->create($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Body type is empty or not a string', $responseBody['errors']); + + $args = [ + 'label' => 'mon custom', + 'type' => 'select', + 'values' => 'wrong format' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactCustomFieldController->create($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Body values is not an array', $responseBody['errors']); + + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $contactCustomFieldController->create($fullRequest, new \Slim\Http\Response()); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Service forbidden', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; } public function testReadList() @@ -77,7 +138,7 @@ class ContactCustomFieldControllerTest extends TestCase $args = [ 'label' => 'mon custom22', - 'values' => ['one', 'two', 'trois'] + 'values' => ['un', 'deux', 'trois'] ]; $fullRequest = \httpRequestCustom::addContentInBody($args, $request); @@ -93,6 +154,73 @@ class ContactCustomFieldControllerTest extends TestCase $responseBody = json_decode((string)$response->getBody(), true); $this->assertSame('Body label is empty or not a string', $responseBody['errors']); + + // Fail + $response = $contactCustomFieldController->update($request, new \Slim\Http\Response(), ['id' => 'wrong format']); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Param id is empty or not an integer', $responseBody['errors']); + + $args = [ + 'label' => 'mon custom', + 'type' => 'select', + 'values' => 'wrong format' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactCustomFieldController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Body values is not an array', $responseBody['errors']); + + $args = [ + 'label' => 'mon custom', + 'type' => 'select', + 'values' => ['one', 'one', 'two'] + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactCustomFieldController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Some values have the same name', $responseBody['errors']); + + $args = [ + 'label' => 'mon custom', + 'type' => 'select', + 'values' => ['one', 'two'] + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactCustomFieldController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id * 1000]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Custom field not found', $responseBody['errors']); + + $args = [ + 'label' => 'my second custom', + 'type' => 'select', + 'values' => ['one', 'two'] + ]; + $fullRequest = \httpRequestCustom::addContentInBody($args, $request); + + $response = $contactCustomFieldController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Custom field with this label already exists', $responseBody['errors']); + + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $contactCustomFieldController->update($request, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Service forbidden', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id'];; } public function testDelete() @@ -106,5 +234,24 @@ class ContactCustomFieldControllerTest extends TestCase $response = $contactCustomFieldController->delete($request, new \Slim\Http\Response(), ['id' => self::$id]); $this->assertSame(204, $response->getStatusCode()); + + // Fail + $response = $contactCustomFieldController->delete($request, new \Slim\Http\Response(), ['id' => 'wrong format']); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Param id is empty or not an integer', $responseBody['errors']); + + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $contactCustomFieldController->delete($request, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Service forbidden', $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/contact/ContactGroupControllerTest.php b/test/unitTests/app/contact/ContactGroupControllerTest.php index 8410e2b0582..2427f3a7a96 100755 --- a/test/unitTests/app/contact/ContactGroupControllerTest.php +++ b/test/unitTests/app/contact/ContactGroupControllerTest.php @@ -12,6 +12,7 @@ use PHPUnit\Framework\TestCase; class ContactGroupControllerTest extends TestCase { private static $id = null; + private static $id2 = null; public function testCreate() @@ -22,23 +23,51 @@ class ContactGroupControllerTest extends TestCase $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $aArgs = [ + $body = [ 'label' => 'Groupe petition', 'description' => 'Groupe de petition', - 'public' => true + 'public' => false ]; - $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); $response = $contactGroupController->create($fullRequest, new \Slim\Http\Response()); - $responseBody = json_decode((string)$response->getBody()); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + + $this->assertIsInt($responseBody['contactsGroup']); + self::$id = $responseBody['contactsGroup']; + + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $body = [ + 'label' => 'Groupe petition 2', + 'description' => 'Groupe de petition', + 'public' => false + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $contactGroupController->create($fullRequest, new \Slim\Http\Response()); + $this->assertSame(200, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); - self::$id = $responseBody->contactsGroup; + $this->assertIsInt($responseBody['contactsGroup']); + self::$id2 = $responseBody['contactsGroup']; - $this->assertIsInt(self::$id); // READ $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); $request = \Slim\Http\Request::createFromEnvironment($environment); + $response = $contactGroupController->getById($request, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Contacts group out of perimeter', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + $response = $contactGroupController->getById($request, new \Slim\Http\Response(), ['id' => self::$id]); $responseBody = json_decode((string)$response->getBody()); @@ -46,15 +75,46 @@ class ContactGroupControllerTest extends TestCase $this->assertSame(self::$id, $responseBody->contactsGroup->id); $this->assertSame('Groupe petition', $responseBody->contactsGroup->label); $this->assertSame('Groupe de petition', $responseBody->contactsGroup->description); - $this->assertSame(true, $responseBody->contactsGroup->public); + $this->assertSame(false, $responseBody->contactsGroup->public); $this->assertSame($user['id'], $responseBody->contactsGroup->owner); $this->assertSame('superadmin', $responseBody->contactsGroup->entity_owner); $this->assertIsString($responseBody->contactsGroup->labelledOwner); $this->assertIsArray($responseBody->contactsGroup->contacts); + + // Fail + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $body = [ + 'label' => 'Groupe petition', + 'public' => true + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $contactGroupController->create($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Bad Request', $responseBody['errors']); + + $body = [ + 'label' => 'Groupe petition', + 'description' => 'Groupe de petition', + 'public' => true + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $contactGroupController->create($fullRequest, new \Slim\Http\Response()); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame(_CONTACTS_GROUP_LABEL_ALREADY_EXISTS, $responseBody['errors']); } public function testGet() { + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + $contactGroupController = new \Contact\controllers\ContactGroupController(); // GET @@ -65,6 +125,10 @@ class ContactGroupControllerTest extends TestCase $this->assertIsArray($responseBody->contactsGroups); $this->assertNotNull($responseBody->contactsGroups); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; } public function testUpdate() @@ -75,12 +139,12 @@ class ContactGroupControllerTest extends TestCase $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $aArgs = [ + $body = [ 'label' => 'Groupe petition updated', 'description' => 'Groupe de petition updated', - 'public' => false + 'public' => true ]; - $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); $response = $contactGroupController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); $responseBody = json_decode((string)$response->getBody()); @@ -96,10 +160,62 @@ class ContactGroupControllerTest extends TestCase $this->assertSame(self::$id, $responseBody->contactsGroup->id); $this->assertSame('Groupe petition updated', $responseBody->contactsGroup->label); $this->assertSame('Groupe de petition updated', $responseBody->contactsGroup->description); - $this->assertSame(false, $responseBody->contactsGroup->public); + $this->assertSame(true, $responseBody->contactsGroup->public); $this->assertSame('superadmin', $responseBody->contactsGroup->entity_owner); $this->assertIsString($responseBody->contactsGroup->labelledOwner); $this->assertIsArray($responseBody->contactsGroup->contacts); + + // Fail + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $body = [ + 'label' => 'Groupe petition updated', + 'description' => 'Groupe de petition updated', + 'public' => true + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $contactGroupController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id * 1000]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Contacts Group does not exist', $responseBody['errors']); + + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $contactGroupController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Service forbidden', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $body = [ + 'label' => 'Groupe petition updated', + 'public' => true + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $contactGroupController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Bad Request', $responseBody['errors']); + + $body = [ + 'label' => 'Groupe petition updated', + 'description' => 'Groupe de petition 2 updated', + 'public' => true + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $contactGroupController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id2]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame(_CONTACTS_GROUP_LABEL_ALREADY_EXISTS, $responseBody['errors']); } public function testAddContacts() @@ -111,10 +227,11 @@ class ContactGroupControllerTest extends TestCase 'limit' => 1 ]); + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + if (!empty($contacts[0])) { // UPDATE - $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']); - $request = \Slim\Http\Request::createFromEnvironment($environment); $aArgs = [ 'contacts' => [$contacts[0]['id']] @@ -132,6 +249,34 @@ class ContactGroupControllerTest extends TestCase $this->assertIsString($responseBody->contactsGroup->contacts[0]->contact); $this->assertIsString($responseBody->contactsGroup->contacts[0]->address); } + + $body = [ + + ]; + $fullRequest = \httpRequestCustom::addContentInBody($body, $request); + + $response = $contactGroupController->addContacts($fullRequest, new \Slim\Http\Response(), ['id' => self::$id * 1000]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Contacts Group does not exist', $responseBody['errors']); + + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $contactGroupController->addContacts($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Service forbidden', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $contactGroupController->addContacts($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Bad Request', $responseBody['errors']); } public function testDeleteContacts() @@ -167,6 +312,27 @@ class ContactGroupControllerTest extends TestCase $this->assertIsString($responseBody->contactsGroup->labelledOwner); $this->assertIsArray($responseBody->contactsGroup->contacts); $this->assertEmpty($responseBody->contactsGroup->contacts); + + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $response = $contactGroupController->deleteContact($request, new \Slim\Http\Response(), ['id' => self::$id * 1000]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Contacts Group does not exist', $responseBody['errors']); + + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $contactGroupController->deleteContact($request, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Service forbidden', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; } public function testDelete() @@ -176,11 +342,37 @@ class ContactGroupControllerTest extends TestCase // DELETE $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']); $request = \Slim\Http\Request::createFromEnvironment($environment); + + // Fail + $response = $contactGroupController->delete($request, new \Slim\Http\Response(), ['id' => self::$id * 1000]); + $this->assertSame(400, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Contacts Group does not exist', $responseBody['errors']); + + $GLOBALS['login'] = 'bbain'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + $response = $contactGroupController->delete($request, new \Slim\Http\Response(), ['id' => self::$id]); + $this->assertSame(403, $response->getStatusCode()); + $responseBody = json_decode((string)$response->getBody(), true); + $this->assertSame('Service forbidden', $responseBody['errors']); + + $GLOBALS['login'] = 'superadmin'; + $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]); + $GLOBALS['id'] = $userInfo['id']; + + // Sucess $response = $contactGroupController->delete($request, new \Slim\Http\Response(), ['id' => self::$id]); $responseBody = json_decode((string)$response->getBody()); $this->assertSame('success', $responseBody->success); + $response = $contactGroupController->delete($request, new \Slim\Http\Response(), ['id' => self::$id2]); + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame('success', $responseBody->success); + // READ $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); $request = \Slim\Http\Request::createFromEnvironment($environment); -- GitLab