From 9fd7b6f1d976bef346a9942db88de4fcd192239a Mon Sep 17 00:00:00 2001 From: Guillaume Heurtier <guillaume.heurtier@maarch.org> Date: Wed, 9 Oct 2019 11:12:01 +0200 Subject: [PATCH] FEAT #11996 TIME 2:00 added enable/disable routes --- rest/index.php | 2 + .../controllers/IndexingModelController.php | 58 +++++++++++- .../IndexingModelControllerTest.php | 91 +++++++++++++++++++ 3 files changed, 150 insertions(+), 1 deletion(-) diff --git a/rest/index.php b/rest/index.php index 656852b8366..60eff54725e 100755 --- a/rest/index.php +++ b/rest/index.php @@ -237,6 +237,8 @@ $app->get('/indexingModels/entities', \IndexingModel\controllers\IndexingModelCo $app->get('/indexingModels/{id}', \IndexingModel\controllers\IndexingModelController::class . ':getById'); $app->post('/indexingModels', \IndexingModel\controllers\IndexingModelController::class . ':create'); $app->put('/indexingModels/{id}', \IndexingModel\controllers\IndexingModelController::class . ':update'); +$app->put('/indexingModels/{id}/disable', \IndexingModel\controllers\IndexingModelController::class . ':disable'); +$app->put('/indexingModels/{id}/enable', \IndexingModel\controllers\IndexingModelController::class . ':enable'); $app->delete('/indexingModels/{id}', \IndexingModel\controllers\IndexingModelController::class . ':delete'); //Jnlp diff --git a/src/app/indexingModel/controllers/IndexingModelController.php b/src/app/indexingModel/controllers/IndexingModelController.php index df7748c64f6..8ef37d4a0d8 100644 --- a/src/app/indexingModel/controllers/IndexingModelController.php +++ b/src/app/indexingModel/controllers/IndexingModelController.php @@ -32,7 +32,13 @@ class IndexingModelController { public function get(Request $request, Response $response) { - $models = IndexingModelModel::get(['where' => ['owner = ? OR private = ?'], 'data' => [$GLOBALS['id'], 'false']]); + $where = ['(owner = ? OR private = ?)']; + + if (!ServiceModel::hasService(['id' => 'admin_indexing_models', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin'])) { + $where[] = 'enabled = TRUE'; + } + + $models = IndexingModelModel::get(['where' => $where, 'data' => [$GLOBALS['id'], 'false']]); return $response->withJson(['indexingModels' => $models]); } @@ -359,6 +365,56 @@ class IndexingModelController return $response->withStatus(204); } + public function disable(Request $request, Response $response, array $args) { + if (!ServiceModel::hasService(['id' => 'admin_indexing_models', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin'])) { + return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); + } + + if (!Validator::intVal()->notEmpty()->validate($args['id'])) { + return $response->withStatus(400)->withJson(['errors' => 'Route id is empty or not an integer']); + } + + $model = IndexingModelModel::getById(['select' => ['enabled'], 'id' => $args['id']]); + if (empty($model)) { + return $response->withStatus(400)->withJson(['errors' => 'Model not found']); + } + + IndexingModelModel::update([ + 'set' => [ + 'enabled' => 'false' + ], + 'where' => ['id = ? or master = ?'], + 'data' => [$args['id'], $args['id']] + ]); + + return $response->withStatus(204); + } + + public function enable(Request $request, Response $response, array $args) { + if (!ServiceModel::hasService(['id' => 'admin_indexing_models', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin'])) { + return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']); + } + + if (!Validator::intVal()->notEmpty()->validate($args['id'])) { + return $response->withStatus(400)->withJson(['errors' => 'Route id is empty or not an integer']); + } + + $model = IndexingModelModel::getById(['select' => ['enabled'], 'id' => $args['id']]); + if (empty($model)) { + return $response->withStatus(400)->withJson(['errors' => 'Model not found']); + } + + IndexingModelModel::update([ + 'set' => [ + 'enabled' => 'true' + ], + 'where' => ['id = ? or master = ?'], + 'data' => [$args['id'], $args['id']] + ]); + + return $response->withStatus(204); + } + public function getEntities(Request $request, Response $response, array $aArgs) { $entitiesTmp = EntityModel::get([ diff --git a/test/unitTests/app/indexingModel/IndexingModelControllerTest.php b/test/unitTests/app/indexingModel/IndexingModelControllerTest.php index 7509c3f80a1..61d11fd6d07 100644 --- a/test/unitTests/app/indexingModel/IndexingModelControllerTest.php +++ b/test/unitTests/app/indexingModel/IndexingModelControllerTest.php @@ -354,6 +354,97 @@ class IndexingModelControllerTest extends TestCase $this->assertNotEmpty($responseBody->indexingModels); } + public function testEnabled() { + $indexingModelController = new \IndexingModel\controllers\IndexingModelController(); + + // GET BY ID + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $response = $indexingModelController->getById($request, new \Slim\Http\Response(), ['id' => self::$masterId]); + $this->assertSame(200, $response->getStatusCode()); + + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame(true, $responseBody->indexingModel->enabled); + + $response = $indexingModelController->getById($request, new \Slim\Http\Response(), ['id' => self::$childId]); + $this->assertSame(200, $response->getStatusCode()); + + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame(true, $responseBody->indexingModel->enabled); + + $response = $indexingModelController->getById($request, new \Slim\Http\Response(), ['id' => self::$childId2]); + $this->assertSame(200, $response->getStatusCode()); + + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame(true, $responseBody->indexingModel->enabled); + + // Disable + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + $response = $indexingModelController->disable($request, new \Slim\Http\Response(), ['id' => self::$masterId]); + $this->assertSame(204, $response->getStatusCode()); + + // GET BY ID + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $response = $indexingModelController->getById($request, new \Slim\Http\Response(), ['id' => self::$masterId]); + $this->assertSame(200, $response->getStatusCode()); + + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame(false, $responseBody->indexingModel->enabled); + + $response = $indexingModelController->getById($request, new \Slim\Http\Response(), ['id' => self::$childId]); + $this->assertSame(200, $response->getStatusCode()); + + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame(false, $responseBody->indexingModel->enabled); + + $response = $indexingModelController->getById($request, new \Slim\Http\Response(), ['id' => self::$childId2]); + $this->assertSame(200, $response->getStatusCode()); + + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame(false, $responseBody->indexingModel->enabled); + + // Enable + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + $response = $indexingModelController->enable($request, new \Slim\Http\Response(), ['id' => self::$masterId]); + $this->assertSame(204, $response->getStatusCode()); + + // GET BY ID + $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']); + $request = \Slim\Http\Request::createFromEnvironment($environment); + + $response = $indexingModelController->getById($request, new \Slim\Http\Response(), ['id' => self::$masterId]); + $this->assertSame(200, $response->getStatusCode()); + + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame(true, $responseBody->indexingModel->enabled); + + $response = $indexingModelController->getById($request, new \Slim\Http\Response(), ['id' => self::$childId]); + $this->assertSame(200, $response->getStatusCode()); + + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame(true, $responseBody->indexingModel->enabled); + + $response = $indexingModelController->getById($request, new \Slim\Http\Response(), ['id' => self::$childId2]); + $this->assertSame(200, $response->getStatusCode()); + + $responseBody = json_decode((string)$response->getBody()); + + $this->assertSame(true, $responseBody->indexingModel->enabled); + } + public function testDelete() { $indexingModelController = new \IndexingModel\controllers\IndexingModelController(); -- GitLab