Skip to content
Snippets Groups Projects
ResControllerTest.php 95.9 KiB
Newer Older
<?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.
*
*/

Giovannoni Laurent's avatar
Giovannoni Laurent committed
use PHPUnit\Framework\TestCase;
Giovannoni Laurent's avatar
Giovannoni Laurent committed
class ResControllerTest extends TestCase
Damien's avatar
Damien committed
    private static $id = null;
    private static $id2 = null;
    private static $id3 = null;

    public function testGetDepartmentById()
    {
        $department = \Resource\controllers\DepartmentController::getById(['id' => '75']);
        $this->assertSame('Paris', $department);

        $department = \Resource\controllers\DepartmentController::getById(['id' => 'not a french department']);
        $this->assertIsString($department);
        $this->assertEmpty($department);
    }
Damien's avatar
Damien committed

    public function testCreate()
    {
        $GLOBALS['login'] = 'cchaplin';
        $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]);
        $GLOBALS['id'] = $userInfo['id'];

Damien's avatar
Damien committed
        $resController = new \Resource\controllers\ResController();

        //  CREATE
        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']);
        $request        = \Slim\Http\Request::createFromEnvironment($environment);

Damien's avatar
Damien committed
        $fileContent = file_get_contents('test/unitTests/samples/test.txt');
Damien's avatar
Damien committed
        $encodedFile = base64_encode($fileContent);

        $body = [
            'modelId'          => 1,
            'status'           => 'NEW',
            'encodedFile'      => $encodedFile,
            'format'           => 'txt',
            'confidentiality'  => false,
            'documentDate'     => '2019-01-01 17:18:47',
            'arrivalDate'      => '2019-01-01 17:18:47',
            'processLimitDate' => '2029-01-01',
            'doctype'          => 102,
            'destination'      => 15,
            'initiator'        => 15,
            'subject'          => 'Breaking News : Superman is alive - PHP unit',
            'typist'           => 19,
            'priority'         => 'poiuytre1357nbvc',
            'senders'          => [['type' => 'contact', 'id' => 1], ['type' => 'user', 'id' => 21], ['type' => 'entity', 'id' => 1]],
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
Damien's avatar
Damien committed
        $responseBody = json_decode((string)$response->getBody());
        self::$id = $responseBody->resId;
        $this->assertIsInt(self::$id);
        $body = [
            'modelId'          => 2,
            'status'           => 'NEW',
            'confidentiality'  => false,
            'documentDate'     => '2019-01-01 17:18:47',
            'arrivalDate'      => '2019-01-01 17:18:47',
            'processLimitDate' => '2029-01-01',
            'doctype'          => 102,
            'destination'      => 15,
            'initiator'        => 15,
            'subject'          => 'Breaking News : Superman is alive - PHP unit',
            'typist'           => 19,
            'priority'         => 'poiuytre1357nbvc',
            'senders'          => [['type' => 'contact', 'id' => 1], ['type' => 'user', 'id' => 21], ['type' => 'entity', 'id' => 1]],
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);

        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(200, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertIsInt($responseBody['resId']);
        self::$id2 = $responseBody['resId'];

        $fileContent = file_get_contents('modules/templates/templates/styles/AR_Masse_Simple.docx');
        $encodedFile = base64_encode($fileContent);

        $body = [
            'modelId'          => 2,
            'status'           => 'NEW',
            'confidentiality'  => false,
            'encodedFile'      => $encodedFile,
            'format'           => 'docx',
            'arrivalDate'      => '2019-01-01 17:18:47',
            'processLimitDate' => '2029-01-01',
            'doctype'          => 102,
            'destination'      => 15,
            'initiator'        => 15,
            'subject'          => 'Breaking News : Superman is alive (again) - PHP unit',
            'typist'           => 19,
            'priority'         => 'poiuytre1357nbvc',
            'senders'          => [['type' => 'contact', 'id' => 1], ['type' => 'user', 'id' => 21], ['type' => 'entity', 'id' => 1]],
        ];

        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);

        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(200, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertIsInt($responseBody['resId']);
        self::$id3 = $responseBody['resId'];

Damien's avatar
Damien committed
        //  READ
        $res = \Resource\models\ResModel::getById(['resId' => self::$id, 'select' => ['*']]);
        $this->assertIsArray($res);
Vinciane's avatar
Vinciane committed
        $this->assertSame('Breaking News : Superman is alive - PHP unit', $res['subject']);
Damien's avatar
Damien committed
        $this->assertSame(102, $res['type_id']);
        $this->assertSame('txt', $res['format']);
        $this->assertSame('NEW', $res['status']);
        $this->assertSame(19, $res['typist']);
        $this->assertNotNull($res['destination']);
        $this->assertNotNull($res['initiator']);
        //  ERROR
        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']);
        $request        = \Slim\Http\Request::createFromEnvironment($environment);

        $fileContent = file_get_contents('test/unitTests/samples/test.txt');
        $encodedFile = base64_encode($fileContent);

        $body = [
            'status'        => 'NEW',
            'encodedFile'   => $encodedFile,
            'format'        => 'txt',
            'confidentiality'   => false,
            'documentDate'  => '2019-01-01 17:18:47',
            'arrivalDate'   => '2019-01-01 17:18:47',
            'processLimitDate'  => '2029-01-01',
            'doctype'       => 102,
            'destination'   => 15,
            'initiator'     => 15,
            'subject'       => 'Breaking News : Superman is alive - PHP unit',
            'typist'        => 19,
            'priority'      => 'poiuytre1357nbvc',
            'tags'          => [1, 2],
            'folders'       => [1, 2],
        ];

        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);

        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body modelId is empty or not an integer', $responseBody['errors']);

        // Errors from ResourceControlController::controlResource
        $body = [];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);

        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body is not set or empty', $responseBody['errors']);

        $body = [
            'doctype' => 'wrong format'
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body doctype is empty or not an integer', $responseBody['errors']);

        $body = [
            'doctype' => 102,
            'modelId' => 1,
            'status'  => 1 // wrong format
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body status is empty or not a string', $responseBody['errors']);

        $body = [
            'doctype' => 102000000,
            'modelId' => 1,
            'status'  => 'NEW'
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body doctype does not exist', $responseBody['errors']);


        $body = [
            'doctype' => 102,
            'modelId' => 1000,
            'status'  => 'NEW'
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body modelId does not exist', $responseBody['errors']);

        $body = [
            'doctype'     => 102,
            'modelId'     => 1,
            'status'      => 'NEW',
            'encodedFile' => $encodedFile,
            'format'      => 'docx'
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Format with this mimeType is not allowed : docx text/plain', $responseBody['errors']);

        $body = [
            'doctype'      => 102,
            'modelId'      => 1,
            'status'       => 'NEW',
            'customFields' => 'wrong format'
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body customFields is not an array', $responseBody['errors']);

        $body = [
            'doctype'      => 102,
            'modelId'      => 1,
            'status'       => 'NEW',
            'customFields' => [1000]
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body customFields : One or more custom fields do not exist', $responseBody['errors']);

        $body = [
            'doctype' => 102,
            'modelId' => 1,
            'status'  => 'NEW',
            'folders' => 'wrong format'
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body folders is not an array', $responseBody['errors']);

        $body = [
            'doctype' => 102,
            'modelId' => 1,
            'status'  => 'NEW',
            'folders' => [100000]
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body folders : One or more folders do not exist or are out of perimeter', $responseBody['errors']);

        $body = [
            'doctype' => 102,
            'modelId' => 1,
            'status'  => 'NEW',
            'tags'    => 'wrong format'
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body tags is not an array', $responseBody['errors']);

        $body = [
            'doctype' => 102,
            'modelId' => 1,
            'status'  => 'NEW',
            'tags'    => [100000]
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body tags : One or more tags do not exist', $responseBody['errors']);

        $body = [
            'doctype' => 102,
            'modelId' => 1,
            'status'  => 'NEW',
            'senders' => 'wrong format'
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body senders is not an array', $responseBody['errors']);

        $body = [
            'doctype' => 102,
            'modelId' => 1,
            'status'  => 'NEW',
            'senders' => ['wrong format']
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body senders[0] is not an array', $responseBody['errors']);

        $body = [
            'doctype' => 102,
            'modelId' => 1,
            'status'  => 'NEW',
            'senders' => [['type' => 'alien']]
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body senders[0] type is not valid', $responseBody['errors']);

        $body = [
            'doctype' => 102,
            'modelId' => 1,
            'status'  => 'NEW',
            'senders' => [['type' => 'user', 'id' => 1000]]
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body senders[0] id does not exist', $responseBody['errors']);

        $body = [
            'doctype' => 102,
            'modelId' => 1,
            'status'  => 'NEW',
            'recipients' => 'wrong format'
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body recipients is not an array', $responseBody['errors']);

        $body = [
            'doctype' => 102,
            'modelId' => 1,
            'status'  => 'NEW',
            'recipients' => ['wrong format']
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body recipients[0] is not an array', $responseBody['errors']);

        $body = [
            'doctype' => 102,
            'modelId' => 1,
            'status'  => 'NEW',
            'recipients' => [['type' => 'alien']]
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body recipients[0] type is not valid', $responseBody['errors']);

        $body = [
            'doctype' => 102,
            'modelId' => 1,
            'status'  => 'NEW',
            'recipients' => [['type' => 'user', 'id' => 1000]]
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body recipients[0] id does not exist', $responseBody['errors']);

        $body = [
            'doctype'      => 102,
            'modelId'      => 1,
            'status'       => 'NEW',
            'documentDate' => 'wrong format'
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body documentDate is not a date', $responseBody['errors']);

        $dateInTheFuture = new \DateTime('tomorrow');
        $dateInTheFuture->add(new \DateInterval('P10D'));
        $dateInTheFuture = $dateInTheFuture->format('d-m-Y');
        
        $body = [
            'doctype'      => 102,
            'modelId'      => 1,
            'status'       => 'NEW',
            'documentDate' => $dateInTheFuture
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body documentDate is not a valid date', $responseBody['errors']);

        $body = [
            'doctype'     => 102,
            'modelId'     => 1,
            'status'      => 'NEW',
            'arrivalDate' => 'wrong format'
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body arrivalDate is not a date', $responseBody['errors']);

        $body = [
            'doctype'     => 102,
            'modelId'     => 1,
            'status'      => 'NEW',
            'arrivalDate' => $dateInTheFuture
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body arrivalDate is not a valid date', $responseBody['errors']);

        $body = [
            'doctype'       => 102,
            'modelId'       => 1,
            'status'        => 'NEW',
            'departureDate' => 'wrong format'
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body departureDate is not a date', $responseBody['errors']);

        $body = [
            'doctype'       => 102,
            'modelId'       => 1,
            'status'        => 'NEW',
            'documentDate'  => '2020-02-01',
            'departureDate' => '2020-01-01'
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body departureDate is not a valid date', $responseBody['errors']);

        $GLOBALS['login'] = 'bbain';
        $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]);
        $GLOBALS['id'] = $userInfo['id'];

        $body = [
            'modelId'       => 1,
            'status'        => 'NEW',
            'confidentiality'   => false,
            'documentDate'  => '2019-01-01 17:18:47',
            'arrivalDate'   => '2019-01-01 17:18:47',
            'processLimitDate'  => '2029-01-01',
            'doctype'       => 102,
            'destination'   => 15,
            'initiator'     => 15,
            'subject'       => 'Breaking News : Superman is alive - PHP unit',
            'typist'        => 19,
            'priority'      => 'poiuytre1357nbvc',
            'senders'       => [['type' => 'contact', 'id' => 1], ['type' => 'user', 'id' => 21], ['type' => 'entity', 'id' => 1]],
            'diffusionList' => [
                ['id' => 19, 'mode' => 'dest']
            ]
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);

        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body initiator does not belong to your entities', $responseBody['errors']);
        $GLOBALS['login'] = 'ddur';
        $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]);
        $GLOBALS['id'] = $userInfo['id'];

        $response     = $resController->create($fullRequest, new \Slim\Http\Response());
        $this->assertSame(403, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody());
        $this->assertSame('Service forbidden', $responseBody->errors);

        $GLOBALS['login'] = 'superadmin';
        $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]);
        $GLOBALS['id'] = $userInfo['id'];
    }

    public function testGetById()
    {
        $resController = new \Resource\controllers\ResController();

        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
        $request        = \Slim\Http\Request::createFromEnvironment($environment);

        $response     = $resController->getById($request, new \Slim\Http\Response(), ['resId' => self::$id2]);
        $responseBody = json_decode((string)$response->getBody());

        $this->assertSame(self::$id2, $responseBody->resId);
        $this->assertSame(2, $responseBody->modelId);
        $this->assertSame('outgoing', $responseBody->categoryId);
        $this->assertEmpty($responseBody->chrono);
        $this->assertSame('NEW', $responseBody->status);
        $this->assertEmpty($responseBody->closingDate);
        $this->assertNotEmpty($responseBody->creationDate);
        $this->assertNotEmpty($responseBody->modificationDate);
        $this->assertIsBool($responseBody->integrations->inShipping);
        $this->assertIsBool($responseBody->integrations->inSignatureBook);
        $this->assertSame('Breaking News : Superman is alive - PHP unit', $responseBody->subject);
        $this->assertSame('2029-01-01 00:00:00', $responseBody->processLimitDate);
        $this->assertSame('poiuytre1357nbvc', $responseBody->priority);
        $this->assertSame(102, $responseBody->doctype);
        $this->assertSame(15, $responseBody->destination);
        $this->assertSame('2019-01-01 17:18:47', $responseBody->documentDate);
        $this->assertEmpty($responseBody->arrivalDate);
        $this->assertNotEmpty($responseBody->destinationLabel);
        $this->assertSame("Nouveau courrier pour le service", $responseBody->statusLabel);
        $this->assertIsBool($responseBody->statusAlterable);
        $this->assertSame('Normal', $responseBody->priorityLabel);
        $this->assertSame('#009dc5', $responseBody->priorityColor);
        $this->assertIsArray($responseBody->senders);
        $this->assertIsArray($responseBody->customFields);
        $this->assertIsArray($responseBody->folders);
        foreach ($responseBody->folders as $value) {
            $this->assertIsInt($value);
        }
        $this->assertIsArray($responseBody->tags);
        foreach ($responseBody->tags as $value) {
            $this->assertIsInt($value);
        }
        
        // ERROR
        $GLOBALS['login'] = 'cchaplin';
        $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]);
        $GLOBALS['id'] = $userInfo['id'];

        $response     = $resController->getById($request, new \Slim\Http\Response(), ['resId' => 123748]);
        $responseBody = json_decode((string)$response->getBody());
        $this->assertSame('Document out of perimeter', $responseBody->errors);

        $GLOBALS['login'] = 'superadmin';
        $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]);
        $GLOBALS['id'] = $userInfo['id'];

        // LIGHT
        $aArgs = [
            'light'  => true
        ];
        $fullRequest = $request->withQueryParams($aArgs);
        $response     = $resController->getById($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
        $responseBody = json_decode((string)$response->getBody());

        $this->assertSame(self::$id, $responseBody->resId);
        $this->assertSame(1, $responseBody->modelId);
        $this->assertSame('incoming', $responseBody->categoryId);
        $this->assertEmpty($responseBody->chrono);
        $this->assertSame('NEW', $responseBody->status);
        $this->assertEmpty($responseBody->closingDate);
        $this->assertNotEmpty($responseBody->creationDate);
        $this->assertNotEmpty($responseBody->modificationDate);
        $this->assertIsBool($responseBody->integrations->inShipping);
        $this->assertIsBool($responseBody->integrations->inSignatureBook);
        $this->assertSame('Breaking News : Superman is alive - PHP unit', $responseBody->subject);
        $this->assertSame('2029-01-01 00:00:00', $responseBody->processLimitDate);
        $this->assertSame('poiuytre1357nbvc', $responseBody->priority);
    public function testUpdate()
    {
        $resController = new \Resource\controllers\ResController();

        // UPDATE
        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']);
        $request        = \Slim\Http\Request::createFromEnvironment($environment);

        $fileContent = file_get_contents('test/unitTests/samples/test.txt');
        $encodedFile = base64_encode($fileContent);

        $tag = \Tag\models\TagModel::get([
            'select' => ['id'],
            'limit' => 1
        ]);
        $tag = $tag[0]['id'];

        $folder = \Folder\models\FolderModel::create([
            'label'     => 'FOLDER TEST',
            'public'    => false,
            'user_id'   => $GLOBALS['id'],
            'parent_id' => null,
            'level'     => 0
        ]);

        $aArgs = [
            'status'           => 'NEW',
            'encodedFile'      => $encodedFile,
            'format'           => 'txt',
            'confidentiality'  => true,
            'documentDate'     => '2019-01-01 17:18:47',
            'arrivalDate'      => '2019-01-01 17:18:47',
            'processLimitDate' => '2030-01-01',
            'doctype'          => 102,
            'destination'      => 15,
            'initiator'        => 15,
            'subject'          => 'Breaking News : Superman is alive - PHP unit',
            'typist'           => 19,
            'priority'         => 'poiuytre1357nbvc',
            'senders'          => [['type' => 'contact', 'id' => 1], ['type' => 'user', 'id' => 21], ['type' => 'entity', 'id' => 1]],
            'recipients'       => [['type' => 'contact', 'id' => 2]],
            'tags'             => [$tag],
            'folders'          => [$folder],
        $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
        $response     = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
        $this->assertSame(204, $response->getStatusCode());

        $aArgs = [
            'status'           => 'NEW',
            'encodedFile'      => $encodedFile,
            'format'           => 'txt',
            'confidentiality'  => true,
            'documentDate'     => '2019-01-01 17:18:47',
            'arrivalDate'      => '2019-01-01 17:18:47',
            'processLimitDate' => '2030-01-01',
            'doctype'          => 102,
            'destination'      => 15,
            'initiator'        => 15,
            'subject'          => 'Breaking News : Superman is alive - PHP unit',
            'typist'           => 19,
            'priority'         => 'poiuytre1357nbvc',
            'senders'          => [['type' => 'contact', 'id' => 1], ['type' => 'user', 'id' => 21], ['type' => 'entity', 'id' => 1]]
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);

        $response     = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
        $this->assertSame(204, $response->getStatusCode());

        //  READ
        $res = \Resource\models\ResModel::getById(['resId' => self::$id, 'select' => ['*']]);

        $this->assertIsArray($res);

        $this->assertSame('Breaking News : Superman is alive - PHP unit', $res['subject']);
        $this->assertSame(102, $res['type_id']);
        $this->assertSame('txt', $res['format']);
        $this->assertSame('NEW', $res['status']);
        $this->assertSame(19, $res['typist']);
        $this->assertNotNull($res['destination']);
        $this->assertNotNull($res['initiator']);
        $this->assertSame('Y', $res['confidentiality']);
        $this->assertSame('2030-01-01 00:00:00', $res['process_limit_date']);

        //  ERROR
        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']);
        $request        = \Slim\Http\Request::createFromEnvironment($environment);

        $aArgs = [
            'status'           => 'NEW',
            'encodedFile'      => $encodedFile,
            'confidentiality'  => true,
            'documentDate'     => '2019-01-01 17:18:47',
            'arrivalDate'      => '2019-01-01 17:18:47',
            'processLimitDate' => '2030-01-01',
            'doctype'          => 102,
            'destination'      => 15,
            'initiator'        => 15,
            'subject'          => 'Breaking News : Superman is alive - PHP unit',
            'typist'           => 19,
            'priority'         => 'poiuytre1357nbvc',
            'senders'          => [['type' => 'contact', 'id' => 1], ['type' => 'user', 'id' => 21], ['type' => 'entity', 'id' => 1]],
        ];

        $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);

        $response     = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body format is empty or not a string', $responseBody['errors']);
            'status'           => 'NEW',
            'encodedFile'      => $encodedFile,
            'format'           => 'txt',
            'confidentiality'  => false,
            'documentDate'     => '2019-01-01 17:18:47',
            'arrivalDate'      => '2019-01-01 17:18:47',
            'processLimitDate' => '2029-01-01',
            'destination'      => 15,
            'initiator'        => 15,
            'subject'          => 'Breaking News : Superman is alive - PHP unit',
            'typist'           => 19,
            'priority'         => 'poiuytre1357nbvc',
            'tags'             => [1, 2],
            'folders'          => [1, 2],
        ];

        $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);

        $response     = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body doctype is empty or not an integer', $responseBody['errors']);

        $response     = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => 'wrong format']);
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Route resId is not an integer',  $responseBody['errors']);

        // Errors from ResourceControlControllers->controlUpdateResource
        $body = [];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);

        $response = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body is not set or empty', $responseBody['errors']);

        \Resource\models\ResModel::update([
            'set'   => ['status' => ''],
            'where' => ['res_id = ?'],
            'data'  => [self::$id]
        ]);

        $body = [
            'doctype'  => 102
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Resource status is empty. It can not be modified', $responseBody['errors']);

        \Resource\models\ResModel::update([
            'set'   => ['status' => 'TMP'],
            'where' => ['res_id = ?'],
            'data'  => [self::$id]
        ]);

        $body = [
            'doctype'  => 102
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Resource can not be modified because of status', $responseBody['errors']);

        \Resource\models\ResModel::update([
            'set'   => ['status' => 'NEW'],
            'where' => ['res_id = ?'],
            'data'  => [self::$id]
        ]);

        $body = [
            'doctype'  => 102000
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body doctype does not exist', $responseBody['errors']);

        $body = [
            'encodedFile' => ''
        ];
        $queryParams = ['onlyDocument' => true ];
        $fullRequest = $request->withQueryParams($queryParams);
        $fullRequest = \httpRequestCustom::addContentInBody($body, $fullRequest);
        $response = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body encodedFile is not set or empty', $responseBody['errors']);

        $externalId = ['signatureBookId' => 42];
        \Resource\models\ResModel::update([
            'set'   => ['external_id' => json_encode($externalId)],
            'where' => ['res_id = ?'],
            'data'  => [self::$id]
        ]);

        $body = [
            'encodedFile' => $encodedFile
        ];
        $queryParams = ['onlyDocument' => true ];
        $fullRequest = $request->withQueryParams($queryParams);
        $fullRequest = \httpRequestCustom::addContentInBody($body, $fullRequest);
        $response = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Resource is in external signature book, file can not be modified', $responseBody['errors']);


        $externalId = [];
        \Resource\models\ResModel::update([
            'set'   => ['external_id' => json_encode($externalId)],
            'where' => ['res_id = ?'],
            'data'  => [self::$id]
        ]);
        \Convert\models\AdrModel::createDocumentAdr([
            'resId'         => self::$id,
            'type'          => 'SIGN',
            'docserverId'   => 'docserver_id',
            'path'          => 'directory',
            'filename'      => 'file_destination_name',
            'version'       => 2,
            'fingerprint'   => '1'
        ]);

        $body = [
            'encodedFile' => $encodedFile
        ];
        $queryParams = ['onlyDocument' => true ];
        $fullRequest = $request->withQueryParams($queryParams);
        $fullRequest = \httpRequestCustom::addContentInBody($body, $fullRequest);
        $response = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Resource is signed, file can not be modified', $responseBody['errors']);

        \Convert\models\AdrModel::deleteDocumentAdr([
            'where' => ['res_id = ?', 'type = ?'],
            'data' => [self::$id, 'SIGN']
        ]);

        \Resource\models\ResModel::update([
            'set'   => ['format' => 'css'],
            'where' => ['res_id = ?'],
            'data'  => [self::$id]
        ]);

        $body = [
            'encodedFile' => $encodedFile
        ];
        $queryParams = ['onlyDocument' => true ];
        $fullRequest = $request->withQueryParams($queryParams);
        $fullRequest = \httpRequestCustom::addContentInBody($body, $fullRequest);
        $response = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Resource is not convertible, file can not be modified', $responseBody['errors']);

        \Resource\models\ResModel::update([
            'set'   => ['format' => 'txt'],
            'where' => ['res_id = ?'],
            'data'  => [self::$id]
        ]);

        $body = [
            'doctype' => 102,
            'tags'    => 'wrong format'
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body tags is not an array', $responseBody['errors']);

        $body = [
            'doctype'   => 102,
            'initiator' => 10000
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body priority is not set', $responseBody['errors']);

        $body = [
            'doctype'          => 102,
            'initiator'        => 10000,
            'priority'         => 'poiuytre1357nbvc',
            'documentDate'     => 'wrong format',
            'arrivalDate'      => 'wrong format',
            'subject'          => 'Permit to expend Slaughter house in  Schrute Farms',
            'senders'          => [['type' => 'contact', 'id' => 1]],
            'destination'      => 15,
            'processLimitDate' => '2029-01-01'
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body initiator does not exist', $responseBody['errors']);

        $body = [
            'doctype'          => 102,
            'initiator'        => 10,
            'priority'         => 'poiuytre1357nbvc',
            'documentDate'     => 'wrong format',
            'arrivalDate'      => 'wrong format',
            'subject'          => 'Permit to expend Slaughter house in  Schrute Farms',
            'senders'          => [['type' => 'contact', 'id' => 1]],
            'destination'      => 15,
            'processLimitDate' => '2029-01-01'
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body initiator does not belong to your entities', $responseBody['errors']);

        $body = [
            'doctype'          => 102,
            'priority'         => 'poiuytre1357nbvc',
            'documentDate'     => 'wrong format',
            'arrivalDate'      => 'wrong format',
            'subject'          => 'Permit to expend Slaughter house in  Schrute Farms',
            'senders'          => [['type' => 'contact', 'id' => 1]],
            'destination'      => 15,
            'processLimitDate' => '2029-01-01'
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body documentDate is not a date', $responseBody['errors']);

        // test control custom fields
        \IndexingModel\models\IndexingModelFieldModel::create([
            'model_id'   => 1,
            'identifier' => 'indexingCustomField_1',
            'mandatory'  => 'true',
            'unit'       => 'mail'
        ]);
        \IndexingModel\models\IndexingModelFieldModel::create([
            'model_id'   => 1,
            'identifier' => 'indexingCustomField_2',
            'mandatory'  => 'false',
            'unit'       => 'mail'
        ]);
        \IndexingModel\models\IndexingModelFieldModel::create([
            'model_id'   => 1,
            'identifier' => 'indexingCustomField_3',
            'mandatory'  => 'false',
            'unit'       => 'mail'
        ]);
        \IndexingModel\models\IndexingModelFieldModel::create([
            'model_id'   => 1,
            'identifier' => 'indexingCustomField_4',
            'mandatory'  => 'false',
            'unit'       => 'mail'
        ]);

        $body = [
            'doctype'          => 102,
            'priority'         => 'poiuytre1357nbvc',
            'documentDate'     => 'wrong format',
            'arrivalDate'      => 'wrong format',
            'subject'          => 'Permit to expend Slaughter house in  Schrute Farms',
            'senders'          => [['type' => 'contact', 'id' => 1]],
            'destination'      => 15,
            'processLimitDate' => '2029-01-01'
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
        $response = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
        $this->assertSame(400, $response->getStatusCode());
        $responseBody = json_decode((string)$response->getBody(), true);
        $this->assertSame('Body customFields[1] is empty', $responseBody['errors']);

        $body = [
            'doctype'          => 102,
            'priority'         => 'poiuytre1357nbvc',
            'documentDate'     => 'wrong format',
            'arrivalDate'      => 'wrong format',
            'subject'          => 'Permit to expend Slaughter house in  Schrute Farms',
            'senders'          => [['type' => 'contact', 'id' => 1]],
            'destination'      => 15,
            'processLimitDate' => '2029-01-01',
            'customFields'     => [1 => 'wrong format']
        ];
        $fullRequest = \httpRequestCustom::addContentInBody($body, $request);