Skip to content
Snippets Groups Projects
Commit a64322d5 authored by Guillaume Heurtier's avatar Guillaume Heurtier
Browse files

FEAT #11931 TIME 1:20 update syntax on get tags + update tags + unit tests

parent 36afb2cb
No related branches found
No related tags found
No related merge requests found
...@@ -363,7 +363,8 @@ $app->get('/administration/statuses/new', \Status\controllers\StatusController:: ...@@ -363,7 +363,8 @@ $app->get('/administration/statuses/new', \Status\controllers\StatusController::
$app->post('/tags', \Tag\controllers\TagController::class . ':create'); $app->post('/tags', \Tag\controllers\TagController::class . ':create');
$app->delete('/tags/{id}', \Tag\controllers\TagController::class . ':delete'); $app->delete('/tags/{id}', \Tag\controllers\TagController::class . ':delete');
$app->get('/tags/{id}', \Tag\controllers\TagController::class . ':getById'); $app->get('/tags/{id}', \Tag\controllers\TagController::class . ':getById');
$app->get('/tags', \Tag\controllers\TagController::class . ':getList'); $app->get('/tags', \Tag\controllers\TagController::class . ':get');
$app->put('/tags/{id}', \Tag\controllers\TagController::class . ':update');
//Templates //Templates
$app->get('/templates', \Template\controllers\TemplateController::class . ':get'); $app->get('/templates', \Template\controllers\TemplateController::class . ':get');
......
...@@ -90,14 +90,45 @@ class TagController ...@@ -90,14 +90,45 @@ class TagController
return $response->withJson($tag); return $response->withJson($tag);
} }
public function getList(Request $request, Response $response, array $args) public function get(Request $request, Response $response)
{ {
if (!ServiceModel::hasService(['id' => 'admin_tag', 'userId' => $GLOBALS['userId'], 'location' => 'tags', 'type' => 'admin'])) { if (!ServiceModel::hasService(['id' => 'admin_tag', 'userId' => $GLOBALS['userId'], 'location' => 'tags', 'type' => 'admin'])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
} }
$tag = TagModel::getList(['select' => []]); $tags = TagModel::get();
return $response->withJson($tag); return $response->withJson(["tags" => $tags]);
}
public function update(Request $request, Response $response, array $args)
{
if (!ServiceModel::hasService(['id' => 'admin_tag', 'userId' => $GLOBALS['userId'], 'location' => 'tags', 'type' => 'admin'])) {
return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
}
$body = $request->getParsedBody();
if (!Validator::stringType()->notEmpty()->validate($body['label'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body label is empty or not a string']);
}
TagModel::update([
'set' => [
'label' => $body['label']
],
'where' => ['id = ?'],
'data' => [$args['id']]
]);
HistoryController::add([
'tableName' => 'tags',
'recordId' => $args['id'],
'eventType' => 'UP',
'info' => _TAG_UPDATED . " : {$body['label']}",
'eventId' => 'tagModification',
]);
return $response->withStatus(200);
} }
} }
...@@ -105,15 +105,18 @@ class TagModel ...@@ -105,15 +105,18 @@ class TagModel
return $tags; return $tags;
} }
public static function getList(array $args) public static function update(array $args)
{ {
ValidatorModel::arrayType($args, ['select']); ValidatorModel::notEmpty($args, ['where']);
ValidatorModel::arrayType($args, ['set', 'where', 'data']);
$tags = DatabaseModel::select([
'select' => empty($args['select']) ? ['*'] : $args['select'], DatabaseModel::update([
'table' => ['tags'] 'table' => 'tags',
'set' => empty($args['set']) ? [] : $args['set'],
'where' => $args['where'],
'data' => empty($args['data']) ? [] : $args['data']
]); ]);
return $tags; return true;
} }
} }
...@@ -70,6 +70,59 @@ class TagControllerTest extends TestCase ...@@ -70,6 +70,59 @@ class TagControllerTest extends TestCase
$this->assertSame('Body label is empty or not a string', $responseBody->errors); $this->assertSame('Body label is empty or not a string', $responseBody->errors);
} }
public function testGet()
{
$tagController = new \Tag\controllers\TagController();
// READ
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = $tagController->getById($request, new \Slim\Http\Response(), ['id' => self::$id]);
$this->assertSame(200, $response->getStatusCode());
$responseBody = json_decode((string)$response->getBody());
$this->assertInternalType('int', $responseBody->id);
$this->assertInternalType('string', $responseBody->label);
}
public function testUpdate()
{
$tagController = new \Tag\controllers\TagController();
// Update working
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$aArgs = [
'label' => 'TEST_LABEL_2'
];
$fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
$response = $tagController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]);
$this->assertSame(200, $response->getStatusCode());
// Update fail
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$aArgs = [
'label' => ''
];
$fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
$response = $tagController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]);
$this->assertSame(400, $response->getStatusCode());
$responseBody = json_decode((string)$response->getBody());
$this->assertInternalType('string', $responseBody->errors);
$this->assertSame('Body label is empty or not a string', $responseBody->errors);
}
public function testDelete() public function testDelete()
{ {
// DELETE // DELETE
...@@ -102,37 +155,25 @@ class TagControllerTest extends TestCase ...@@ -102,37 +155,25 @@ class TagControllerTest extends TestCase
$this->assertSame(400, $response->getStatusCode()); $this->assertSame(400, $response->getStatusCode());
} }
public function testGet() public function testGetList()
{ {
$tagController = new \Tag\controllers\TagController(); $tagController = new \Tag\controllers\TagController();
// READ // READ
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
$request = \Slim\Http\Request::createFromEnvironment($environment); $request = \Slim\Http\Request::createFromEnvironment($environment);
$response = $tagController->getById($request, new \Slim\Http\Response(), ['id' => 1]); $response = $tagController->get($request, new \Slim\Http\Response(), ['id' => self::$id]);
$this->assertSame(200, $response->getStatusCode()); $this->assertSame(200, $response->getStatusCode());
$responseBody = json_decode((string)$response->getBody()); $responseBody = json_decode((string)$response->getBody());
$this->assertInternalType('int', $responseBody->id); $this->assertInternalType('array', $responseBody->tags);
$this->assertInternalType('string', $responseBody->label); $this->assertNotEmpty($responseBody->tags);
}
public function testGetList()
{
$tagController = new \Tag\controllers\TagController();
// READ $tags = $responseBody->tags;
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = $tagController->getList($request, new \Slim\Http\Response(), ['id' => self::$id]);
$this->assertSame(200, $response->getStatusCode());
$responseBody = json_decode((string)$response->getBody());
foreach ($responseBody as $value) { foreach ($tags as $value) {
$this->assertInternalType('int', $value->id); $this->assertInternalType('int', $value->id);
$this->assertInternalType('string', $value->label); $this->assertInternalType('string', $value->label);
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment