diff --git a/composer.json b/composer.json index 1ceae779fb41e6b986e434d7c28499b28854160e..cf0383ac21fc617694f27c4fe4d7c6a12d3bca71 100644 --- a/composer.json +++ b/composer.json @@ -10,6 +10,7 @@ }, "require": { "slim/slim": "^3", - "respect/validation": "^1.1" + "respect/validation": "^1.1", + "guzzlehttp/guzzle": "~6.0" } } diff --git a/core/Controllers/ParametersController.php b/core/Controllers/ParametersController.php new file mode 100644 index 0000000000000000000000000000000000000000..48d220ff5ae956a660df469bd3053d2b689626b6 --- /dev/null +++ b/core/Controllers/ParametersController.php @@ -0,0 +1,146 @@ +<?php + + namespace Core\Controllers; + +use Psr\Http\Message\RequestInterface; +use Psr\Http\Message\ResponseInterface; +use Respect\Validation\Validator; +use Core\Models\ParametersModel; + + require_once 'core/class/class_db_pdo.php'; + require_once 'modules/notes/Models/NotesModel.php'; + + class ParametersController + { + + public function getList(RequestInterface $request, ResponseInterface $response) + { + $obj = ParametersModel::getList(); + + return $response->withJson($obj); + } + + public function getById(RequestInterface $request, ResponseInterface $response, $aArgs) + { + + $obj = ParametersModel::getById(['id' => $aArgs['id']]); + return $response->withJson($obj); + } + public function create(RequestInterface $request, ResponseInterface $response) + { + $datas = $request->getParams(); + + $errors = $this->control($request, 'create',$datas); + + if (!empty($errors)) { + return $response + ->withJson(['errors' => $errors]); + } + + $return = ParametersModel::create($datas); + + if ($return) { + $obj = ParametersModel::getById([ + 'id' => $datas['id'] + ]); + } else { + return $response + ->withStatus(500) + ->withJson(['errors' => _NOT_CREATE]); + } + + return $response->withJson($obj); + } + + public function update(RequestInterface $request, ResponseInterface $response, $aArgs) + { + $datas = $request->getParams(); + + $errors = $this->control($request, 'update',$datas); + + if (!empty($errors)) { + return $response + ->withJson(['errors' => $errors]); + } + + $return = ParametersModel::update($datas); + + if ($return) { + $obj = ParametersModel::getById([ + 'id' => $aArgs['id'] + ]); + } else { + return $response + ->withStatus(500) + ->withJson(['errors' => _NOT_UPDATE]); + } + + return $response->withJson($obj); + } + + public function delete(RequestInterface $request, ResponseInterface $response, $aArgs) + { + $obj = ParametersModel::delete(['id' => $aArgs['id']]); + return $response->withJson($obj); + } + + protected function control($request, $mode, $aArgs) + { + $errors = []; + + if ($mode == 'update') { + $obj = ParametersModel::getById([ + 'id' => $aArgs['id'], + 'param_value_int' => $aArgs['param_value_int'] + ]); + if (empty($obj)) { + array_push( + $errors, + _ID . ' '. _NOT_EXISTS + ); + } + + } + if (!Validator::notEmpty()->validate($aArgs['id'])) { + array_push($errors, '_ID_IS_EMPTY_CONTROLLER'); + } elseif ($mode == 'create') { + if(!Validator::regex('/^[\w.-]*$/')->validate($request->getParam('id'))){ + array_push($errors,'ID INVALIDE'); + } + if(!Validator::regex('/^[\w.-]*$/')->validate($request->getParam('description'))&&$request->getParam('description')!=null){ + array_push($errors,'DESCRIPTION INVALIDE'); + } + if (!Validator::regex('/^[\w.-]*$/')->validate($request->getParam('param_value_string'))&&$request->getParam('param_value_string')!=null) { + array_push($errors,'PARAM STRING INVALIDE'); + } + $obj = ParametersModel::getById([ + 'id' => $aArgs['id'] + ]); + if (!empty($obj)) { + array_push( + $errors, + _ID . ' ' . $obj[0]['id'] . ' ' . _ALREADY_EXISTS + ); + } + } + if ($aArgs['param_value_date']!=null) { + if (date('Y-m-d H:i:s', strtotime($aArgs['param_value_date'])) != $aArgs['param_value_date']) { + array_push( + $errors, + 'PARAMETRE DATE INVALIDE.' + ); + } + } + if ($mode=='create'&&!Validator::notEmpty()->validate($aArgs['param_value_int'])&& + !Validator::notEmpty()->validate($aArgs['param_value_string'])&& + !Validator::notEmpty()->validate($aArgs['param_value_date']) + ) { + array_push($errors, '_PARAM_VALUE_IS_EMPTY'); + } + + return $errors; + } + + } + +?> \ No newline at end of file diff --git a/core/Models/ParametersModel.php b/core/Models/ParametersModel.php new file mode 100644 index 0000000000000000000000000000000000000000..eecad051d544b958edb84bf18f7827ed3863f935 --- /dev/null +++ b/core/Models/ParametersModel.php @@ -0,0 +1,26 @@ +<?php + + namespace Core\Models; +/* + use Psr\Http\Message\RequestInterface; + use Psr\Http\Message\ResponseInterface; + use Respect\Validation\Validator; + use Core\Models\DocserverModel; + use Core\Models\DocserverTypeModel; + use Core\Models\UserModel; + use Core\Models\ParameterModel; + use Entities\Models\EntitiesModel; + use Core\Controllers\DocserverController; + use Core\Controllers\DocserverToolsController; +*/ + use Core\Models\ParametersModelAbstract; + + require_once 'core/class/class_db_pdo.php'; + require_once 'modules/notes/Models/NotesModel.php'; + + class ParametersModel extends ParametersModelAbstract + { + + } + +?> \ No newline at end of file diff --git a/core/Models/ParametersModelAbstract.php b/core/Models/ParametersModelAbstract.php new file mode 100644 index 0000000000000000000000000000000000000000..15bde6264a92555e967706f2548d2f7350b64f70 --- /dev/null +++ b/core/Models/ParametersModelAbstract.php @@ -0,0 +1,76 @@ +<?php + namespace Core\Models; + + require_once 'apps/maarch_entreprise/services/Table.php'; + + class ParametersModelAbstract extends \Apps_Table_Service + { + public static function getList() + { + $aReturn = static::select([ + 'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'], + 'table' => ['parameters'], + ]); + + return $aReturn; + } + + + public static function getById(array $aArgs = []) + { + static::checkRequired($aArgs, ['id']); + static::checkString($aArgs,['id']); + + $aReturn = static::select([ + 'select' => empty($aArgs['select']) ? ['*'] : $aArgs['select'], + 'table' =>['parameters'], + 'where' => ['id = ?'], + 'data' => [$aArgs['id']] + ]); + + return $aReturn; + + } + + public static function create(array $aArgs = []) + { + static::checkRequired($aArgs, ['id']); + static::checkString($aArgs, ['id']); + + $aReturn = static::insertInto($aArgs, 'parameters'); + + return $aReturn; + } + + public static function update(array $aArgs = []) + { + static::checkRequired($aArgs, ['id']); + static::checkString($aArgs, ['id']); + + $where['id'] = $aArgs['id']; + + $aReturn = static::updateTable( + $aArgs, + 'parameters', + $where + ); + + return $aReturn; + } + + public static function delete(array $aArgs = []) + { + static::checkRequired($aArgs, ['id']); + static::checkString($aArgs, ['id']); + + $aReturn = static::deleteFrom([ + 'table' => 'parameters', + 'where' => ['id = ?'], + 'data' => [$aArgs['id']] + ]); + + return $aReturn; + } + + } +?> \ No newline at end of file diff --git a/core/Test/ParametersControllerTest.php b/core/Test/ParametersControllerTest.php new file mode 100644 index 0000000000000000000000000000000000000000..a60a81d1ec099ca801154f2928a2f1fb72740e38 --- /dev/null +++ b/core/Test/ParametersControllerTest.php @@ -0,0 +1,248 @@ +<?php + + namespace MaarchTest; + + require_once __DIR__.'/define.php'; + + class ParametersControllerTest extends \PHPUnit_Framework_TestCase + { + + public function testCreate() + { + + $aArgs = [ + 'id'=> 'TEST', + 'description' => null, + 'param_value_string' => 'abcd', + 'param_value_int' => null, + 'param_value_date' => null + ]; + + $client = new \GuzzleHttp\Client([ + 'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', + // You can set any number of default request options. + 'timeout' => 2.0, + ]); + $response = $client->request('POST', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', [ + 'auth'=> ['superadmin','superadmin'], + 'form_params' => $aArgs + ]); + + $compare = '[{"id":"TEST","description":null,' + . '"param_value_string":"abcd","param_value_int":null,"param_value_date":null}]'; + $this->assertSame($compare,(string)$response->getBody()); + + //TEST EXISTE DEJA + $aArgs = [ + 'id'=> 'TEST', + 'description' => null, + 'param_value_string' => null, + 'param_value_int' => 1234, + 'param_value_date' => null + ]; + + $client = new \GuzzleHttp\Client([ + 'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', + // You can set any number of default request options. + 'timeout' => 2.0, + ]); + $response = $client->request('POST', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', [ + 'auth'=> ['superadmin','superadmin'], + 'form_params' => $aArgs + ]); + + $compare = '{"errors":["Identifiant TEST existe d\u00e9j\u00e0 !"]}'; + + $this->assertSame($compare,(string)$response->getBody()); + + //AUCUN PARAMETRE + + $aArgs = [ + 'id'=> 'TEST3', + 'description' => null, + 'param_value_string' => null, + 'param_value_int' => null, + 'param_value_date' => null + ]; + + $client = new \GuzzleHttp\Client([ + 'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', + // You can set any number of default request options. + 'timeout' => 2.0, + ]); + $response = $client->request('POST', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', [ + 'auth'=> ['superadmin','superadmin'], + 'form_params' => $aArgs + ]); + + $compare = '{"errors":["_PARAM_VALUE_IS_EMPTY"]}'; + + $this->assertSame($compare,(string)$response->getBody()); + + //AUCUN ARGUMENTS + $aArgs = [ ]; + + $client = new \GuzzleHttp\Client([ + 'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', + // You can set any number of default request options. + 'timeout' => 2.0, + ]); + $response = $client->request('POST', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', [ + 'auth'=> ['superadmin','superadmin'], + 'form_params' => $aArgs + ]); + + $compare = '{"errors":["_ID_IS_EMPTY_CONTROLLER","_PARAM_VALUE_IS_EMPTY"]}'; + + $this->assertSame($compare,(string)$response->getBody()); + + //DATE MAUVAIS FORMAT + + $aArgs = [ + 'id'=> 'TEST4', + 'description' => null, + 'param_value_string' => null, + 'param_value_int' => null, + 'param_value_date' => '123456' + ]; + + $client = new \GuzzleHttp\Client([ + 'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', + // You can set any number of default request options. + 'timeout' => 2.0, + ]); + $response = $client->request('POST', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', [ + 'auth'=> ['superadmin','superadmin'], + 'form_params' => $aArgs + ]); + + $compare ='{"errors":["PARAMETRE DATE INVALIDE."]}'; + $this->assertSame($compare,(string)$response->getBody()); + + //TEST ID MAUVAIS FORMAT (REGEX) + + $aArgs = [ + 'id'=> 'A*-#==', + 'description' => "*///*//", + 'param_value_string' => "//-//**", + 'param_value_int' => null, + 'param_value_date' => null + ]; + + $response = $client->request('POST', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', [ + 'auth'=> ['superadmin','superadmin'], + 'form_params' => $aArgs + ]); + + $compare ='{"errors":["ID INVALIDE","DESCRIPTION INVALIDE","PARAM STRING INVALIDE"]}'; + $this->assertSame($compare,(string)$response->getBody()); + } + + public function testGetList() + { + $client = new \GuzzleHttp\Client([ + 'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', + // You can set any number of default request options. + 'timeout' => 2.0, + ]); + $response = $client->request('GET', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', [ + 'auth'=> ['superadmin','superadmin'] + ]); + + $this->assertNotNull($response->getBody()); + } + + public function testGetById() + { + + $aArgs = [ + 'id'=> 'TEST' + ]; + + $client = new \GuzzleHttp\Client([ + 'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', + // You can set any number of default request options. + 'timeout' => 2.0, + ]); + $response = $client->request('GET', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters/'.$aArgs['id'], [ + 'auth'=> ['superadmin','superadmin'] + ]); + $compare = '[[{"id":"TEST",' + .'"description":null,' + .'"param_value_string":"abcd",' + .'"param_value_int":null,' + .'"param_value_date":null}]]'; + + $this->assertNotNull((string)$response->getBody()); + } + + public function testUpdate() + { + + $aArgs = [ + 'id'=> 'TEST', + 'description' =>'TEST AFTER UP' + ]; + + $client = new \GuzzleHttp\Client([ + 'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', + // You can set any number of default request options. + 'timeout' => 2.0, + ]); + $response = $client->request('PUT', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters/'.$aArgs['id'], [ + 'auth'=> ['superadmin','superadmin'], + 'form_params' => $aArgs + ]); + + $compare = '[{"id":"TEST",' + .'"description":"TEST AFTER UP",' + .'"param_value_string":"abcd",' + .'"param_value_int":null,' + .'"param_value_date":null}]'; + + $this->assertSame($compare,(string)$response->getBody()); + + //TEST ID NULL + $aArgs = [ + 'id'=> 'NEW' + ]; + + $client = new \GuzzleHttp\Client([ + 'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', + // You can set any number of default request options. + 'timeout' => 2.0, + ]); + $response = $client->request('PUT', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters/'.$aArgs['id'], [ + 'auth'=> ['superadmin','superadmin'], + 'form_params' => $aArgs + ]); + + $compare = '{"errors":["Identifiant n\'existe pas"]}'; + + $this->assertSame($compare,(string)$response->getBody()); + + + } + + public function testDelete() + { + $aArgs = [ + 'id'=> 'TEST' + ]; + + $client = new \GuzzleHttp\Client([ + 'base_uri' => 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters', + // You can set any number of default request options. + 'timeout' => 2.0, + ]); + $response = $client->request('DELETE', 'http://127.0.0.1/maarch_courrier/cs_Maarch/rest/parameters/'.$aArgs['id'], [ + 'auth'=> ['superadmin','superadmin'], + 'form_params' => $aArgs + ]); + $compare='true'; + $this->assertSame($compare,(string)$response->getBody()); + } + + } + +?> \ No newline at end of file diff --git a/rest/index.php b/rest/index.php index fdbfa80bcd0f921ba680f5aa5fa159dbe9c3df45..e3800470ed7d72a00f5f06f9fb3112f286ab7292 100644 --- a/rest/index.php +++ b/rest/index.php @@ -134,4 +134,11 @@ $app->post('/resExt', \Core\Controllers\ResExtController::class . ':create'); //Users $app->get('/user/profile', \Core\Controllers\UserController::class . ':getCurrentUserInfos'); +//parameters +$app->get('/parameters', \Core\Controllers\ParametersController::class . ':getList'); +$app->get('/parameters/{id}', \Core\Controllers\ParametersController::class . ':getById'); +$app->post('/parameters', \Core\Controllers\ParametersController::class . ':create'); +$app->put('/parameters/{id}', \Core\Controllers\ParametersController::class . ':update'); +$app->delete('/parameters/{id}', \Core\Controllers\ParametersController::class . ':delete'); + $app->run();