diff --git a/core/Controllers/StatusController.php b/core/Controllers/StatusController.php index 2dcd8588da25141c3bf64315073a30ef74005107..6781d69b8425cd8befefea46b515a305a50be9e8 100644 --- a/core/Controllers/StatusController.php +++ b/core/Controllers/StatusController.php @@ -39,17 +39,24 @@ class StatusController public function getNewInformations(RequestInterface $request, ResponseInterface $response) { + if (!ServiceModel::hasService(['id' => 'admin_status', 'userId' => $_SESSION['user']['UserId'], 'location' => 'apps', 'type' => 'admin'])) { + return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); + } + $datas = [ 'statusImages' => StatusModel::getStatusImages(), 'lang' => StatusModel::getStatusLang() ]; return $response->withJson($datas); - } public function getByIdentifier(RequestInterface $request, ResponseInterface $response, $aArgs) { + if (!ServiceModel::hasService(['id' => 'admin_status', 'userId' => $_SESSION['user']['UserId'], 'location' => 'apps', 'type' => 'admin'])) { + return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); + } + if (!empty($aArgs['identifier']) && Validator::numeric()->validate($aArgs['identifier'])) { $obj = StatusModel::getByIdentifier([ 'identifier' => $aArgs['identifier'] @@ -64,11 +71,9 @@ class StatusController 'lang' => StatusModel::getStatusLang(), 'statusImages' => StatusModel::getStatusImages(), ]); - } else { return $response->withStatus(400)->withJson(['errors' => 'identifier not valid']); } - } public function create(RequestInterface $request, ResponseInterface $response) @@ -93,7 +98,6 @@ class StatusController 'id' => $id ]); return $response->withJson([$obj]); - } else { return $response->withStatus(500)->withJson(['errors' => _NOT_CREATE]); } @@ -121,7 +125,6 @@ class StatusController $obj = StatusModel::getByIdentifier([ 'identifier' => $aArgs['identifier'] ]); - } else { return $response ->withStatus(500) @@ -150,10 +153,11 @@ class StatusController return $response->withJson([$obj]); } - protected function manageValue($request){ + protected function manageValue($request) + { foreach ($request as $key => $value) { - if(in_array($key, ['is_system', 'is_folder_status', 'can_be_searched', 'can_be_modified'])){ - if(empty($value)){ + if (in_array($key, ['is_system', 'is_folder_status', 'can_be_searched', 'can_be_modified'])) { + if (empty($value)) { $request[$key] = 'N'; } else { $request[$key] = 'Y'; @@ -180,7 +184,7 @@ class StatusController _ID . ' ' . $obj[0]['id'] . ' ' . _ALREADY_EXISTS ); } - } else if ($mode == 'update') { + } elseif ($mode == 'update') { $obj = StatusModel::getByIdentifier([ 'identifier' => $request['identifier'] ]); @@ -203,41 +207,41 @@ class StatusController array_push($errors, 'label_status not valid'); } - if ( Validator::notEmpty()->validate($request['is_system']) && + if (Validator::notEmpty()->validate($request['is_system']) && !Validator::contains('Y')->validate($request['is_system']) && !Validator::contains('N')->validate($request['is_system']) ) { array_push($errors, 'is_system not valid'); } - if ( Validator::notEmpty()->validate($request['is_folder_status']) && + if (Validator::notEmpty()->validate($request['is_folder_status']) && !Validator::contains('Y')->validate($request['is_folder_status']) && !Validator::contains('N')->validate($request['is_folder_status']) ) { array_push($errors, 'is_folder_status not valid'); } - if ( Validator::notEmpty()->validate($request['img_filename']) && + if (Validator::notEmpty()->validate($request['img_filename']) && (!Validator::regex('/^[\w-.]+$/')->validate($request['img_filename']) || !Validator::length(1, 255)->validate($request['img_filename'])) ) { array_push($errors, 'img_filename not valid'); } - if ( Validator::notEmpty()->validate($request['maarch_module']) && + if (Validator::notEmpty()->validate($request['maarch_module']) && !Validator::length(null, 255)->validate($request['maarch_module']) ) { array_push($errors, 'maarch_module not valid'); } - if ( Validator::notEmpty()->validate($request['can_be_searched']) && + if (Validator::notEmpty()->validate($request['can_be_searched']) && !Validator::contains('Y')->validate($request['can_be_searched']) && !Validator::contains('N')->validate($request['can_be_searched']) ) { array_push($errors, 'can_be_searched not valid'); } - if ( Validator::notEmpty()->validate($request['can_be_modified']) && + if (Validator::notEmpty()->validate($request['can_be_modified']) && !Validator::contains('Y')->validate($request['can_be_modified']) && !Validator::contains('N')->validate($request['can_be_modified']) ) { @@ -245,6 +249,5 @@ class StatusController } return $errors; - } } diff --git a/core/Models/StatusModelAbstract.php b/core/Models/StatusModelAbstract.php index c877b7d8ce8473db5e12ddd294d0ef97557df370..9b5e8383a910b79be7a64cc66b588eb506f40b0d 100644 --- a/core/Models/StatusModelAbstract.php +++ b/core/Models/StatusModelAbstract.php @@ -30,7 +30,8 @@ class StatusModelAbstract extends \Apps_Table_Service return $aReturn; } - public static function getStatusLang(){ + public static function getStatusLang() + { $aLang = LangModel::getStatusLang(); return $aLang; } @@ -109,7 +110,6 @@ class StatusModelAbstract extends \Apps_Table_Service public static function getStatusImages(array $aArgs = []) { - $aReturn = static::select([ 'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'], 'table' => ['status_images'], @@ -118,5 +118,4 @@ class StatusModelAbstract extends \Apps_Table_Service return $aReturn; } - -} \ No newline at end of file +} diff --git a/core/Test/StatusControllerTest.php b/core/Test/StatusControllerTest.php index 664dd782904e87ec1306bcd394631537d34f408a..4b6ee449b0d635c512f8bbb016788f5bf44b400e 100644 --- a/core/Test/StatusControllerTest.php +++ b/core/Test/StatusControllerTest.php @@ -20,26 +20,67 @@ class StatusControllerTest extends \PHPUnit_Framework_TestCase $status = new \Core\Controllers\StatusController(); $aArgs = [ - 'id' => 'TEST', - 'label_status' => 'TEST', - 'img_filename' => 'fm-letter-end' + 'id' => 'TEST', + 'label_status' => 'TEST', + 'img_filename' => 'fm-letter-end', + 'is_folder_status' => '', + 'can_be_searched' => 'true', + 'can_be_modified' => '', ]; $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); $response = $status->create($fullRequest, new \Slim\Http\Response()); - $compare = '[[{"id":"TEST","label_status":"TEST",' - . '"is_system":"N","is_folder_status":"N","img_filename":"fm-letter-end",' - . '"maarch_module":"apps","can_be_searched":"Y",' - . '"can_be_modified":"Y"}]]'; + $responseBody = json_decode((string)$response->getBody()); + + $this->assertInternalType("int", $responseBody[0][0]->identifier); + + unset($responseBody[0][0]->identifier); + + $compare = [ + 'id' => 'TEST', + 'label_status' => 'TEST', + 'is_system' => 'N', + 'is_folder_status' => 'N', + 'img_filename' => 'fm-letter-end', + 'maarch_module' => 'apps', + 'can_be_searched' => 'Y', + 'can_be_modified' => 'N', + ]; + + $aCompare = json_decode(json_encode($compare), false); + $this->assertEquals($aCompare, $responseBody[0][0], "\$canonicalize = true", 0.0, 10, true); - $this->assertNotNull((string)$response->getBody()); - // $this->assertSame((string)$response->getBody(), $compare); + ########## CREATE FAIL ########## + $request = \Slim\Http\Request::createFromEnvironment($environment); + $aArgs = [ + 'id' => 'TEST', + 'label_status' => 'TEST', + 'img_filename' => 'fm-letter-end', + 'is_folder_status' => '' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + $response = $status->create($fullRequest, new \Slim\Http\Response()); + $responseBody = json_decode((string)$response->getBody()); + $this->assertSame(_ID . ' TEST ' . _ALREADY_EXISTS, $responseBody->errors[0]); + + ########## CREATE FAIL 2 ########## + $request = \Slim\Http\Request::createFromEnvironment($environment); + $aArgs = [ + 'id' => 'papa', + 'label_status' => '', + 'img_filename' => 'fm-letter-end', + 'is_folder_status' => '' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + $response = $status->create($fullRequest, new \Slim\Http\Response()); + $responseBody = json_decode((string)$response->getBody()); + $this->assertSame('label_status not valid', $responseBody->errors[0]); } public function testGetListUpdateDelete() { - #####GET LIST##### + ########## GET LIST ########## $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); $request = \Slim\Http\Request::createFromEnvironment($environment); $status = new \Core\Controllers\StatusController(); @@ -48,6 +89,11 @@ class StatusControllerTest extends \PHPUnit_Framework_TestCase $responseBody = json_decode((string)$response->getBody()); $this->assertNotNull($responseBody->statusList); + + foreach ($responseBody->statusList as $value) { + $this->assertInternalType("int", $value->identifier); + } + $this->assertNotNull($responseBody->lang); $elem = $responseBody->statusList; @@ -55,11 +101,39 @@ class StatusControllerTest extends \PHPUnit_Framework_TestCase $key = key($elem); $lastIdentifier = $elem[$key]->identifier; + ########## GETBYIDENTIFIER ########## + $response = $status->getByIdentifier($request, new \Slim\Http\Response(), ['identifier' => $lastIdentifier]); + $responseBody = json_decode((string)$response->getBody()); + + $this->assertNotNull($responseBody->status); + $this->assertNotNull($responseBody->statusImages); + $this->assertNotNull($responseBody->lang); + + $compare = [ + 'identifier' => $lastIdentifier, + 'id' => 'TEST', + 'label_status' => 'TEST', + 'is_system' => 'N', + 'is_folder_status' => 'N', + 'img_filename' => 'fm-letter-end', + 'maarch_module' => 'apps', + 'can_be_searched' => 'Y', + 'can_be_modified' => 'N', + ]; + + $aCompare = json_decode(json_encode($compare), false); + $this->assertEquals($aCompare, $responseBody->status[0], "\$canonicalize = true", 0.0, 10, true); + + ########## GETBYIDENTIFIER FAIL ########## + $response = $status->getByIdentifier($request, new \Slim\Http\Response(), ['identifier' => -1]); + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame('identifier not found', $responseBody->errors); + - #####UPDATE##### + ########## UPDATE ########## $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $status = new \Core\Controllers\StatusController(); $aArgs = [ 'id' => 'TEST', @@ -69,21 +143,57 @@ class StatusControllerTest extends \PHPUnit_Framework_TestCase $response = $status->update($fullRequest, new \Slim\Http\Response(), ['identifier' => $lastIdentifier]); - $compare = '[[{"id":"TEST","label_status":"TEST AFTER UP",' - . '"is_system":"N","is_folder_status":"N","img_filename":"fm-letter-end",' - . '"maarch_module":"apps","can_be_searched":"Y",' - . '"can_be_modified":"Y","identifier":'.$lastIdentifier.'}]]'; - - $this->assertSame((string)$response->getBody(), $compare); + $responseBody = json_decode((string)$response->getBody()); + + $compare = [ + 'identifier' => $lastIdentifier, + 'id' => 'TEST', + 'label_status' => 'TEST AFTER UP', + 'is_system' => 'N', + 'is_folder_status' => 'N', + 'img_filename' => 'fm-letter-end', + 'maarch_module' => 'apps', + 'can_be_searched' => 'Y', + 'can_be_modified' => 'N', + ]; + + $aCompare = json_decode(json_encode($compare), false); + $this->assertEquals($aCompare, $responseBody[0][0], "\$canonicalize = true", 0.0, 10, true); + + ########## UPDATE FAIL ########## + $request = \Slim\Http\Request::createFromEnvironment($environment); + $aArgs = [ + 'id' => 'PZOEIRUTY', + 'label_status' => 'TEST AFTER UP' + ]; + $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request); + + $response = $status->update($fullRequest, new \Slim\Http\Response(), ['identifier' => -1]); + + $responseBody = json_decode((string)$response->getBody()); + $this->assertSame('-1 ' . _NOT_EXISTS, $responseBody->errors[0]); - #####DELETE##### + ########## DELETE ########## $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']); $request = \Slim\Http\Request::createFromEnvironment($environment); - $status = new \Core\Controllers\StatusController(); $response = $status->delete($request, new \Slim\Http\Response(), ['identifier'=> $lastIdentifier]); $this->assertSame((string)$response->getBody(), '[true]'); } + + public function testGetNewInformations() + { + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + $status = new \Core\Controllers\StatusController(); + + $response = $status->getNewInformations($request, new \Slim\Http\Response()); + + $responseBody = json_decode((string)$response->getBody()); + + $this->assertNotNull($responseBody->statusImages); + $this->assertNotNull($responseBody->lang); + } }