Skip to content
Snippets Groups Projects
Verified Commit c9752045 authored by Damien's avatar Damien
Browse files

FEAT #11269 TIME 2:00 Indexing model unit tests + fixes

parent bffce01d
No related branches found
No related tags found
No related merge requests found
......@@ -11,12 +11,14 @@
<file>test/unitTests/app/contact/ContactControllerTest.php</file>
<file>test/unitTests/app/contact/ContactGroupControllerTest.php</file>
<file>test/unitTests/app/contact/ContactTypeControllerTest.php</file>
<file>test/unitTests/app/customField/CustomFieldControllerTest.php</file>
<file>test/unitTests/core/CoreControllerTest.php</file>
<file>test/unitTests/app/docserver/DocserverControllerTest.php</file>
<file>test/unitTests/app/doctype/DoctypeControllerTest.php</file>
<file>test/unitTests/app/entity/EntityControllerTest.php</file>
<file>test/unitTests/app/group/GroupControllerTest.php</file>
<file>test/unitTests/app/entity/ListTemplateControllerTest.php</file>
<file>test/unitTests/app/indexingModel/IndexingModelControllerTest.php</file>
<file>test/unitTests/app/notification/NotificationControllerTest.php</file>
<file>test/unitTests/app/notification/NotificationScheduleControllerTest.php</file>
<file>test/unitTests/app/parameter/ParameterControllerTest.php</file>
......
......@@ -26,12 +26,14 @@ use Slim\Http\Response;
class IndexingModelController
{
public function get(Request $request, Response $response, array $args)
const FIELDS_TYPES = ['standard', 'custom'];
public function get(Request $request, Response $response)
{
$models = IndexingModelModel::get(['where' => ['owner = ? OR private = ?'], 'data' => [$GLOBALS['id'], false]]);
$models = IndexingModelModel::get(['where' => ['owner = ? OR private = ?'], 'data' => [$GLOBALS['id'], 'false']]);
foreach ($models as $key => $model) {
$fields = IndexingModelFieldModel::get(['select' => ['label', 'mandatory', 'value', 'unit'], 'where' => ['model_id = ?'], 'data' => [$model['id']]]);
$fields = IndexingModelFieldModel::get(['select' => ['type', 'identifier', 'mandatory', 'value', 'unit'], 'where' => ['model_id = ?'], 'data' => [$model['id']]]);
$models[$key]['fields'] = $fields;
}
......@@ -47,7 +49,7 @@ class IndexingModelController
return $response->withStatus(400)->withJson(['errors' => 'Model out of perimeter']);
}
$fields = IndexingModelFieldModel::get(['select' => ['label', 'mandatory', 'value', 'unit'], 'where' => ['model_id = ?'], 'data' => [$args['id']]]);
$fields = IndexingModelFieldModel::get(['select' => ['type', 'identifier', 'mandatory', 'value', 'unit'], 'where' => ['model_id = ?'], 'data' => [$args['id']]]);
$model['fields'] = $fields;
return $response->withJson(['indexingModel' => $model]);
......@@ -60,6 +62,14 @@ class IndexingModelController
if (!Validator::stringType()->notEmpty()->validate($body['label'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body label is empty or not a string']);
}
foreach ($body['fields'] as $key => $field) {
if (!Validator::stringType()->notEmpty()->validate($field['type']) || !in_array($field['type'], IndexingModelController::FIELDS_TYPES)) {
return $response->withStatus(400)->withJson(['errors' => "Body fields[{$key}] type is empty or not a validate type"]);
} elseif (!Validator::intVal()->notEmpty()->validate($field['identifier'])) {
return $response->withStatus(400)->withJson(['errors' => "Body fields[{$key}] identifier is empty or not an integer"]);
}
}
if (ServiceModel::hasService(['id' => 'admin_indexing_models', 'userId' => $GLOBALS['userId'], 'location' => 'apps', 'type' => 'admin'])) {
$body['private'] = empty($body['private']) ? 'false' : 'true';
} else {
......@@ -74,16 +84,22 @@ class IndexingModelController
]);
foreach ($body['fields'] as $field) {
if ($field['type'] == 'custom') {
$unit = $field['unit'] ?? null;
} else {
$unit = null;
}
IndexingModelFieldModel::create([
'model_id' => $modelId,
'label' => $field['label'],
'mandatory' => empty($field['mandatory']) ? 'false' : 'true',
'value' => $field['value'] ?? null,
'unit' => empty($field['unit']) ? null : $field['unit']
'model_id' => $modelId,
'type' => $field['type'],
'identifier' => $field['identifier'],
'mandatory' => empty($field['mandatory']) ? 'false' : 'true',
'value' => $field['value'] ?? null,
'unit' => $unit
]);
}
return $response->withStatus(204);
return $response->withJson(['id' => $modelId]);
}
public function update(Request $request, Response $response, array $args)
......@@ -93,6 +109,13 @@ class IndexingModelController
if (!Validator::stringType()->notEmpty()->validate($body['label'])) {
return $response->withStatus(400)->withJson(['errors' => 'Body label is empty or not a string']);
}
foreach ($body['fields'] as $key => $field) {
if (!Validator::stringType()->notEmpty()->validate($field['type']) || !in_array($field['type'], IndexingModelController::FIELDS_TYPES)) {
return $response->withStatus(400)->withJson(['errors' => "Body fields[{$key}] type is empty or not a validate type"]);
} elseif (!Validator::intVal()->notEmpty()->validate($field['identifier'])) {
return $response->withStatus(400)->withJson(['errors' => "Body fields[{$key}] identifier is empty or not an integer"]);
}
}
$model = IndexingModelModel::getById(['select' => ['owner', 'private'], 'id' => $args['id']]);
if (empty($model)) {
......@@ -105,7 +128,7 @@ class IndexingModelController
IndexingModelModel::update([
'set' => [
'label' => $body['label']
'label' => $body['label']
],
'where' => ['id = ?'],
'data' => [$args['id']]
......@@ -114,12 +137,18 @@ class IndexingModelController
IndexingModelFieldModel::delete(['where' => ['model_id = ?'], 'data' => [$args['id']]]);
foreach ($body['fields'] as $field) {
if ($field['type'] == 'custom') {
$unit = $field['unit'] ?? null;
} else {
$unit = null;
}
IndexingModelFieldModel::create([
'model_id' => $args['id'],
'label' => $field['label'],
'mandatory' => empty($field['mandatory']) ? 'false' : 'true',
'value' => $field['value'] ?? null,
'unit' => empty($field['unit']) ? null : $field['unit']
'model_id' => $args['id'],
'type' => $field['type'],
'identifier' => $field['identifier'],
'mandatory' => empty($field['mandatory']) ? 'false' : 'true',
'value' => $field['value'] ?? null,
'unit' => $unit
]);
}
......
......@@ -38,18 +38,19 @@ class IndexingModelFieldModel
public static function create(array $args)
{
ValidatorModel::notEmpty($args, ['model_id', 'label', 'mandatory']);
ValidatorModel::stringType($args, ['label', 'mandatory', 'value']);
ValidatorModel::intVal($args, ['model_id', 'unit']);
ValidatorModel::notEmpty($args, ['model_id', 'type', 'identifier', 'mandatory']);
ValidatorModel::stringType($args, ['type', 'mandatory', 'value']);
ValidatorModel::intVal($args, ['model_id', 'identifier', 'unit']);
DatabaseModel::insert([
'table' => 'indexing_models_fields',
'columnsValues' => [
'model_id' => $args['model_id'],
'label' => $args['label'],
'mandatory' => $args['mandatory'],
'value' => $args['value'],
'unit' => $args['unit']
'model_id' => $args['model_id'],
'type' => $args['type'],
'identifier' => $args['identifier'],
'mandatory' => $args['mandatory'],
'value' => $args['value'],
'unit' => $args['unit']
]
]);
......
......@@ -69,7 +69,7 @@ class IndexingModelModel
'columnsValues' => [
'id' => $nextSequenceId,
'label' => $args['label'],
'default' => $args['default'],
'"default"' => $args['default'],
'owner' => $args['owner'],
'private' => $args['private']
]
......
<?php
/**
* Copyright Maarch since 2008 under licence GPLv3.
* See LICENCE.txt file at the root folder for more details.
* This file is part of Maarch software.
*
*/
use PHPUnit\Framework\TestCase;
class IndexingModelControllerTest extends TestCase
{
private static $id = null;
public function testCreate()
{
$indexingModelController = new \IndexingModel\controllers\IndexingModelController();
// CREATE
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$args = [
'label' => 'mon model d indexation',
'private' => true,
'fields' => [
[
'type' => 'standard',
'identifier' => 1,
'mandatory' => true,
'value' => 'tika',
],
[
'type' => 'standard',
'identifier' => 2,
'mandatory' => true,
'value' => 'massala',
]
]
];
$fullRequest = \httpRequestCustom::addContentInBody($args, $request);
$response = $indexingModelController->create($fullRequest, new \Slim\Http\Response());
$this->assertSame(200, $response->getStatusCode());
$responseBody = json_decode((string)$response->getBody());
self::$id = $responseBody->id;
// GET BY ID
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = $indexingModelController->getById($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]);
$this->assertSame(200, $response->getStatusCode());
$responseBody = json_decode((string)$response->getBody());
$this->assertSame('mon model d indexation', $responseBody->indexingModel->label);
$this->assertSame(true, $responseBody->indexingModel->private);
$this->assertSame('standard', $responseBody->indexingModel->fields[0]->type);
$this->assertSame(1, $responseBody->indexingModel->fields[0]->identifier);
$this->assertSame(true, $responseBody->indexingModel->fields[0]->mandatory);
$this->assertSame('tika', $responseBody->indexingModel->fields[0]->value);
$this->assertSame('standard', $responseBody->indexingModel->fields[1]->type);
$this->assertSame(2, $responseBody->indexingModel->fields[1]->identifier);
$this->assertSame(true, $responseBody->indexingModel->fields[1]->mandatory);
$this->assertSame('massala', $responseBody->indexingModel->fields[1]->value);
// Errors
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
unset($args['label']);
$fullRequest = \httpRequestCustom::addContentInBody($args, $request);
$response = $indexingModelController->create($fullRequest, new \Slim\Http\Response());
$this->assertSame(400, $response->getStatusCode());
$responseBody = json_decode((string)$response->getBody());
$this->assertSame('Body label is empty or not a string', $responseBody->errors);
}
public function testUpdate()
{
$indexingModelController = new \IndexingModel\controllers\IndexingModelController();
// UPDATE
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$args = [
'label' => 'mon model d indexation modifié',
'fields' => [
[
'type' => 'standard',
'identifier' => 4,
'mandatory' => true,
'value' => 'butter',
],
[
'type' => 'custom',
'identifier' => 8,
'mandatory' => false,
'value' => 'chicken',
]
]
];
$fullRequest = \httpRequestCustom::addContentInBody($args, $request);
$response = $indexingModelController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]);
$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($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]);
$this->assertSame(200, $response->getStatusCode());
$responseBody = json_decode((string)$response->getBody());
$this->assertSame('mon model d indexation modifié', $responseBody->indexingModel->label);
$this->assertSame(true, $responseBody->indexingModel->private);
$this->assertSame('standard', $responseBody->indexingModel->fields[0]->type);
$this->assertSame(4, $responseBody->indexingModel->fields[0]->identifier);
$this->assertSame(true, $responseBody->indexingModel->fields[0]->mandatory);
$this->assertSame('butter', $responseBody->indexingModel->fields[0]->value);
$this->assertSame('custom', $responseBody->indexingModel->fields[1]->type);
$this->assertSame(8, $responseBody->indexingModel->fields[1]->identifier);
$this->assertSame(false, $responseBody->indexingModel->fields[1]->mandatory);
$this->assertSame('chicken', $responseBody->indexingModel->fields[1]->value);
// Errors
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
unset($args['label']);
$fullRequest = \httpRequestCustom::addContentInBody($args, $request);
$response = $indexingModelController->update($fullRequest, new \Slim\Http\Response(), ['id' => self::$id]);
$this->assertSame(400, $response->getStatusCode());
$responseBody = json_decode((string)$response->getBody());
$this->assertSame('Body label is empty or not a string', $responseBody->errors);
}
public function testGet()
{
$indexingModelController = new \IndexingModel\controllers\IndexingModelController();
// GET
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = $indexingModelController->get($request, new \Slim\Http\Response());
$this->assertSame(200, $response->getStatusCode());
$responseBody = json_decode((string)$response->getBody());
$this->assertNotEmpty($responseBody->indexingModels);
}
public function testDelete()
{
$indexingModelController = new \IndexingModel\controllers\IndexingModelController();
// DELETE
$environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']);
$request = \Slim\Http\Request::createFromEnvironment($environment);
$response = $indexingModelController->delete($request, new \Slim\Http\Response(), ['id' => self::$id]);
$this->assertSame(204, $response->getStatusCode());
// Errors
$response = $indexingModelController->delete($request, new \Slim\Http\Response(), ['id' => 99999]);
$this->assertSame(400, $response->getStatusCode());
$responseBody = json_decode((string)$response->getBody());
$this->assertSame('Model not found', $responseBody->errors);
}
}
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