Skip to content
Snippets Groups Projects
ResControllerTest.php 57.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • <?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;
    
        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);
    
            $aArgs = [
    
                'modelId'       => 1,
    
    Damien's avatar
    Damien committed
                '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',
    
                'priority'      => 'poiuytre1357nbvc',
    
                'senders'       => [['type' => 'contact', 'id' => 1], ['type' => 'user', 'id' => 21], ['type' => 'entity', 'id' => 1]],
    
    Damien's avatar
    Damien committed
            ];
    
            $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $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);
    
            $aArgs = [
                '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($aArgs, $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'];
    
    
    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);
    
            $aArgs = [
                '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($aArgs, $request);
    
            $response     = $resController->create($fullRequest, new \Slim\Http\Response());
            $responseBody = json_decode((string)$response->getBody());
    
            $this->assertSame('Body modelId is empty or not an integer', $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 = [
    
                'modelId'          => 1,
                '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 = [
                'modelId'          => 1,
                '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 = [
                'modelId'          => 1,
                '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']);
    
            $GLOBALS['login'] = 'ddur';
            $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]);
            $GLOBALS['id'] = $userInfo['id'];
    
            $response     = $resController->update($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
            $this->assertSame(403, $response->getStatusCode());
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertSame('Service forbidden',  $responseBody['errors']);
    
            $GLOBALS['login'] = 'superadmin';
            $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]);
            $GLOBALS['id'] = $userInfo['id'];
    
            \Folder\models\FolderModel::delete([
                'where' => ['id = ?'],
                'data'  => [$folder]
            ]);
    
    Florian Azizian's avatar
    Florian Azizian committed
        public function testGetOriginalContent()
    
        {
            $resController = new \Resource\controllers\ResController();
    
            $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
            $request        = \Slim\Http\Request::createFromEnvironment($environment);
    
    
    Florian Azizian's avatar
    Florian Azizian committed
            $response     = $resController->getOriginalFileContent($request, new \Slim\Http\Response(), ['resId' => self::$id]);
    
            $responseBody = json_decode((string)$response->getBody());
    
            $this->assertSame(null, $responseBody);
    
    
            // GET FILE CONTENT
            $response     = $resController->getFileContent($request, new \Slim\Http\Response(), ['resId' => self::$id]);
            $responseBody = json_decode((string)$response->getBody());
    
            $this->assertSame(null, $responseBody);
    
            // GET FILE CONTENT
            $aArgs = [
                'mode'  => 'base64'
            ];
            $fullRequest = $request->withQueryParams($aArgs);
            $response     = $resController->getFileContent($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
            $responseBody = json_decode((string)$response->getBody());
    
            $this->assertNotEmpty($responseBody->encodedDocument);
            $this->assertSame('txt', $responseBody->originalFormat);
            $this->assertNotEmpty($responseBody->originalCreatorId);
    
    
            $aArgs = [
                'mode'  => 'base64'
            ];
            $fullRequest = $request->withQueryParams($aArgs);
            $response     = $resController->getOriginalFileContent($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
            $responseBody = json_decode((string)$response->getBody(), true);
    
            $this->assertNotEmpty($responseBody['encodedDocument']);
            $this->assertIsString($responseBody['encodedDocument']);
            $this->assertSame('txt', $responseBody['extension']);
            $this->assertNotEmpty($responseBody['mimeType']);
            $this->assertIsString($responseBody['mimeType']);
    
    
            $response     = $resController->getFileContent($request, new \Slim\Http\Response(), ['resId' => -2]);
    
            $this->assertSame(400, $response->getStatusCode());
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertSame('Document does not exist', $responseBody['errors']);
    
            $response     = $resController->getFileContent($request, new \Slim\Http\Response(), ['resId' => self::$id2]);
            $this->assertSame(400, $response->getStatusCode());
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertSame('Document has no file', $responseBody['errors']);
    
            $GLOBALS['login'] = 'ddur';
            $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]);
            $GLOBALS['id'] = $userInfo['id'];
    
            $response     = $resController->getFileContent($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
            $this->assertSame(403, $response->getStatusCode());
            $responseBody = json_decode((string)$response->getBody(), true);
            $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'];
    
            $response     = $resController->getOriginalFileContent($request, new \Slim\Http\Response(), ['resId' => -2]);
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertSame('Document does not exist', $responseBody['errors']);
    
            $response     = $resController->getOriginalFileContent($request, new \Slim\Http\Response(), ['resId' => self::$id2]);
            $this->assertSame(400, $response->getStatusCode());
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertSame('Document has no file', $responseBody['errors']);
    
            $GLOBALS['login'] = 'ddur';
            $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]);
            $GLOBALS['id'] = $userInfo['id'];
    
            $response     = $resController->getOriginalFileContent($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id]);
            $this->assertSame(403, $response->getStatusCode());
            $responseBody = json_decode((string)$response->getBody(), true);
            $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'];
    
        }
    
        public function testGetThumbnailContent()
        {
            $resController = new \Resource\controllers\ResController();
    
            $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
            $request        = \Slim\Http\Request::createFromEnvironment($environment);
    
            $response     = $resController->getThumbnailContent($request, new \Slim\Http\Response(), ['resId' => self::$id]);
    
            $this->assertSame(200, $response->getStatusCode());
            $responseBody = json_decode((string)$response->getBody(), true);
    
            $this->assertEmpty($responseBody);
    
    
            $response     = $resController->getThumbnailContent($request, new \Slim\Http\Response(), ['resId' => -2]);
    
            $this->assertSame(400, $response->getStatusCode());
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertSame('Document does not exist', $responseBody['errors']);
    
            $response     = $resController->getThumbnailContent($request, new \Slim\Http\Response(), ['resId' => 'wrong format']);
            $this->assertSame(403, $response->getStatusCode());
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertSame('resId param is not an integer', $responseBody['errors']);
        }
    
        public function testGetItems()
        {
            $resController = new \Resource\controllers\ResController();
    
            $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
            $request        = \Slim\Http\Request::createFromEnvironment($environment);
    
            // Errors
            $response     = $resController->getItems($request, new \Slim\Http\Response(), ['resId' => 'wrong format']);
            $this->assertSame(403, $response->getStatusCode());
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertSame('Document out of perimeter', $responseBody['errors']);
    
            $response     = $resController->getItems($request, new \Slim\Http\Response(), ['resId' => -2]);
            $this->assertSame(400, $response->getStatusCode());
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertSame('Document does not exist', $responseBody['errors']);
    
            // Success
            $response     = $resController->getItems($request, new \Slim\Http\Response(), ['resId' => self::$id]);
            $this->assertSame(200, $response->getStatusCode());
            $responseBody = json_decode((string)$response->getBody(), true);
    
            $this->assertSame(0, $responseBody['linkedResources']);
            $this->assertSame(0, $responseBody['attachments']);
            $this->assertSame(0, $responseBody['diffusionList']);
            $this->assertSame(0, $responseBody['visaCircuit']);
            $this->assertSame(0, $responseBody['opinionCircuit']);
            $this->assertSame(0, $responseBody['notes']);
            $this->assertSame(0, $responseBody['emails']);
        }
    
        public function testGetField()
        {
            $resController = new \Resource\controllers\ResController();
    
            $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
            $request        = \Slim\Http\Request::createFromEnvironment($environment);
    
            // Errors
            $GLOBALS['login'] = 'ddur';
            $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]);
            $GLOBALS['id'] = $userInfo['id'];
    
            $response     = $resController->getField($request, new \Slim\Http\Response(), ['resId' => self::$id]);
            $this->assertSame(403, $response->getStatusCode());
            $responseBody = json_decode((string)$response->getBody(), true);
            $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'];
    
            $response     = $resController->getField($request, new \Slim\Http\Response(), ['resId' => self::$id, 'fieldId' => 'initiator']);
            $this->assertSame(403, $response->getStatusCode());
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertSame('Field out of perimeter', $responseBody['errors']);
    
            $response     = $resController->getField($request, new \Slim\Http\Response(), ['resId' => self::$id * 1000, 'fieldId' => 'destination']);
            $this->assertSame(400, $response->getStatusCode());
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertSame('Document does not exist', $responseBody['errors']);
    
            // Success
            $response     = $resController->getField($request, new \Slim\Http\Response(), ['resId' => self::$id2, 'fieldId' => 'externalId']);
            $this->assertSame(200, $response->getStatusCode());
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertIsArray($responseBody['field']);
            $this->assertEmpty($responseBody['field']);
    
            $fullRequest = $request->withQueryParams(['alt' => true]);
            $response     = $resController->getField($fullRequest, new \Slim\Http\Response(), ['resId' => self::$id2, 'fieldId' => 'destination']);
            $this->assertSame(200, $response->getStatusCode());
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertSame(15, $responseBody['field']);
        }
    
        public function testGetEncodedDocument()
        {
            $resController = new \Resource\controllers\ResController();
    
            $response = $resController::getEncodedDocument(['resId' => self::$id, 'original' => false]);
    
            $this->assertIsString($response['encodedDocument']);
            $this->assertNotEmpty($response['encodedDocument']);
    
            $this->assertSame('Breaking News _ Superman is al.pdf', $response['fileName']);
    
        public function testGetCategories()
        {
            $resController = new \Resource\controllers\ResController();
    
            $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
            $request        = \Slim\Http\Request::createFromEnvironment($environment);
    
            $response     = $resController->getCategories($request, new \Slim\Http\Response());
            $responseBody = json_decode((string)$response->getBody());
    
            $this->assertNotEmpty($responseBody->categories);
    
            foreach ($responseBody->categories as $value) {
                $this->assertNotEmpty($value->id);
                $this->assertNotEmpty($value->label);
            }
        }
    
        public function testIsAllowedForCurrentUser()
        {
            $resController = new \Resource\controllers\ResController();
    
            $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
            $request        = \Slim\Http\Request::createFromEnvironment($environment);
    
            $response     = $resController->isAllowedForCurrentUser($request, new \Slim\Http\Response(), ['resId' => self::$id]);
            $responseBody = json_decode((string)$response->getBody());
    
            $this->assertSame(true, $responseBody->isAllowed);
    
            // NOT ALLOWED
    
            $GLOBALS['login'] = 'bbain';
            $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]);
    
            $GLOBALS['id'] = $userInfo['id'];
    
            $response     = $resController->isAllowedForCurrentUser($request, new \Slim\Http\Response(), ['resId' => 123456]);
            $responseBody = json_decode((string)$response->getBody());
    
            $this->assertSame(false, $responseBody->isAllowed);
    
    
            $GLOBALS['login'] = 'superadmin';
            $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]);
    
            $GLOBALS['id'] = $userInfo['id'];
        }
    
        public function testSetInIntegrations()
        {
            $resController = new \Resource\controllers\ResController();
    
            $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']);
            $request        = \Slim\Http\Request::createFromEnvironment($environment);
    
            $aArgs = [
                'resources'   => [self::$id],
                'integrations' => ['inSignatureBook' => true, 'inShipping' => true]
            ];
            $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
            $response     = $resController->setInIntegrations($fullRequest, new \Slim\Http\Response());
    
            $this->assertSame(204, $response->getStatusCode());
    
            // ERROR
    
            $GLOBALS['login'] = 'bbain';
            $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]);
    
            $GLOBALS['id'] = $userInfo['id'];
    
            $aArgs = [
                'resources'   => [12345],
                'integrations' => ['inSignatureBook' => true, 'inShipping' => true]
            ];
            $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
            $response     = $resController->setInIntegrations($fullRequest, new \Slim\Http\Response());
            $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'];
        }
    
    
    Damien's avatar
    Damien committed
        public function testUpdateStatus()
        {
            $resController = new \Resource\controllers\ResController();
    
            //  UPDATE STATUS
            $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']);
            $request        = \Slim\Http\Request::createFromEnvironment($environment);
    
    
    Damien's avatar
    Damien committed
                'status'        => 'EVIS'
            ];
    
            $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
    
    Damien's avatar
    Damien committed
    
            $response     = $resController->updateStatus($fullRequest, new \Slim\Http\Response());
            $responseBody = json_decode((string)$response->getBody());
            $this->assertSame('success', $responseBody->success);
    
            //  READ
    
            $res = \Resource\models\ResModel::getById(['resId' => self::$id, 'select' => ['*']]);
    
            $this->assertIsArray($res);
    
    Damien's avatar
    Damien committed
            $this->assertSame('EVIS', $res['status']);
    
            //  UPDATE WITHOUT STATUS
            $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']);
            $request        = \Slim\Http\Request::createFromEnvironment($environment);
    
    
            $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
    
    Damien's avatar
    Damien committed
    
            $response     = $resController->updateStatus($fullRequest, new \Slim\Http\Response());
            $responseBody = json_decode((string)$response->getBody());
    
            $this->assertSame('success', $responseBody->success);
    
            //  READ
    
            $res = \Resource\models\ResModel::getById(['resId' => self::$id, 'select' => ['*']]);
    
            $this->assertIsArray($res);
    
    Damien's avatar
    Damien committed
            $this->assertSame('COU', $res['status']);
    
    
            $body = [
                'status' => 'STATUS_THAT_DOES_NOT_EXIST'
            ];
    
            $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
    
            $response     = $resController->updateStatus($fullRequest, new \Slim\Http\Response());
            $this->assertSame(400, $response->getStatusCode());
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertSame(_STATUS_NOT_FOUND, $responseBody['errors']);
    
            $body = [
                'status' => 'EVIS',
            ];
    
            $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
    
            $response     = $resController->updateStatus($fullRequest, new \Slim\Http\Response());
            $this->assertSame(400, $response->getStatusCode());
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertSame('Bad Request', $responseBody['errors']);
    
            $body = [
                'status' => 'EVIS',
                'resId'  => [self::$id * 1000]
            ];
    
            $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
    
            $response     = $resController->updateStatus($fullRequest, new \Slim\Http\Response());
            $this->assertSame(400, $response->getStatusCode());
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertSame(_DOCUMENT_NOT_FOUND, $responseBody['errors']);
    
            $GLOBALS['login'] = 'ddur';
            $userInfo = \User\models\UserModel::getByLogin(['login' => $GLOBALS['login'], 'select' => ['id']]);
            $GLOBALS['id'] = $userInfo['id'];
    
            $body = [
                'status' => 'EVIS',
                'resId'  => [self::$id]
            ];
    
            $fullRequest = \httpRequestCustom::addContentInBody($body, $request);
    
            $response     = $resController->updateStatus($fullRequest, new \Slim\Http\Response());
            $this->assertSame(403, $response->getStatusCode());
            $responseBody = json_decode((string)$response->getBody(), true);
            $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'];
    
        public function testUpdateExternalInfos()
        {
    
            $resController = new \Resource\controllers\ResController();
    
            //  UPDATE STATUS
            $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']);
            $request        = \Slim\Http\Request::createFromEnvironment($environment);
    
            //ALL OK
            $aArgs = [
    
                    'externalInfos' => [
    
                        [
                            'res_id'        => self::$id,
                            'external_id'   => "BB981212IIYZ",
                            'external_link' => "https://publik.nancy.fr/res/BB981212BB65"
                        ]
                    ],
                    'status'        => "GRCSENT"
            ];
    
            $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
    
            $response = $resController->updateExternalInfos($fullRequest, new \Slim\Http\Response());
    
            $responseBody = json_decode((string) $response->getBody());
    
            $this->assertSame('success', $responseBody->success);
    
            // EXTERNAL INFOS EMPTY AND RES ID IS NOT INTEGER
            $aArgs = [
    
                'externalInfos' => [
    
                        [
                            'res_id'        => "res_id",
                            'external_id'   => "",
                            'external_link' => ""
                        ]
                    ],
                'status'        => "GRCSENT"
    
            ];
    
            $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
    
            $response = $resController->updateExternalInfos($fullRequest, new \Slim\Http\Response());
    
            $responseBody = json_decode((string) $response->getBody());
    
    
            $this->assertSame('Bad Request: invalid res_id', $responseBody->errors);
    
    
            // DOCUMENT DOES NOT EXIST
            $aArgs = [
                'externalInfos' => [
                            [
                                'res_id'        => 123456789,
                                'external_id'   => "BB981212IIYZ",
                                'external_link' => "https://publik.nancy.fr/res/BB981212BB65"
                            ]
                        ],
                'status'        => 'GRCSENT'
            ];
    
            $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
    
            $response = $resController->updateExternalInfos($fullRequest, new \Slim\Http\Response());
    
            $responseBody = json_decode((string) $response->getBody());
    
            $this->assertSame(_DOCUMENT_NOT_FOUND, $responseBody->errors);
    
    
            $aArgs = [
                    'externalInfos' => [
                        [
    
                            'external_id'   => "BB981212IIYZ",
                            'external_link' => "https://publik.nancy.fr/res/BB981212BB65"
                        ]
                    ],
    
                    'status'        => null
    
            ];
    
            $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
    
            $response = $resController->updateExternalInfos($fullRequest, new \Slim\Http\Response());
    
            $responseBody = json_decode((string) $response->getBody());
    
    
    Florian Azizian's avatar
    Florian Azizian committed
            $this->assertSame('Bad Request : status is empty', $responseBody->errors);
    
                'externalInfos' => null,
    
                'status'        => "GRCSENT"
            ];
    
            $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
    
            $response = $resController->updateExternalInfos($fullRequest, new \Slim\Http\Response());
    
            $responseBody = json_decode((string) $response->getBody());
    
    
    Florian Azizian's avatar
    Florian Azizian committed
            $this->assertSame('Bad Request : externalInfos is empty', $responseBody->errors);
    
        public function testGetList()
        {
    
            $resController = new \Resource\controllers\ResController();
    
            $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']);
            $request        = \Slim\Http\Request::createFromEnvironment($environment);
    
            $aArgs = [
    
                'select'        => 'sve_start_date',
    
                'clause'        => '1=1',
                'withFile'      => true,
                'orderBy'       => ['res_id'],
                'limit'         => 1
            ];
            $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
    
            $response     = $resController->getList($fullRequest, new \Slim\Http\Response());
            $responseBody = json_decode((string)$response->getBody());
            $arr_res = $responseBody->resources;
            $this->assertNotNull($arr_res[0]->fileBase64Content);
    
            $this->assertIsInt($arr_res[0]->res_id);
    
    
            $aArgs = [
                'select'        => 'res_id',
                'clause'        => '1=1',
                'withFile'      => false,
                'orderBy'       => ['res_id'],
                'limit'         => 1
            ];
            $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
    
            $response     = $resController->getList($fullRequest, new \Slim\Http\Response());
            $responseBody = json_decode((string)$response->getBody());
            $arr_res = $responseBody->resources;
    
            $this->assertSame(null, $arr_res[0]->fileBase64Content);
    
            $this->assertIsInt($arr_res[0]->res_id);
    
            $aArgs = [
                'select'        => '',
                'clause'        => '1=1',
                'withFile'      => false,
                'orderBy'       => ['res_id'],
                'limit'         => 1
            ];
            $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
    
            $response     = $resController->getList($fullRequest, new \Slim\Http\Response());
    
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertSame('Bad Request: select is not valid', $responseBody['errors']);
    
    
            $aArgs = [
                'select'        => 'res_id',
                'clause'        => '',
                'withFile'      => false,
                'orderBy'       => ['res_id'],
                'limit'         => 1
            ];
            $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
    
            $response     = $resController->getList($fullRequest, new \Slim\Http\Response());
    
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertSame('Bad Request: clause is not valid', $responseBody['errors']);
    
            $aArgs = [
                'select'        => 'res_id',
                'clause'        => '1=1',
                'withFile'      => 'wrong format',
                'orderBy'       => ['res_id'],
                'limit'         => 1
            ];
            $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
    
            $response     = $resController->getList($fullRequest, new \Slim\Http\Response());
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertSame('Bad Request: withFile parameter is not a boolean', $responseBody['errors']);
    
            $aArgs = [
                'select'        => 'res_id',
                'clause'        => '1=1',
                'withFile'      => false,
                'orderBy'       => 'wrong format',
                'limit'         => 1
            ];
            $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
    
            $response     = $resController->getList($fullRequest, new \Slim\Http\Response());
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertSame('Bad Request: orderBy parameter not valid', $responseBody['errors']);
    
            $aArgs = [
                'select'        => 'res_id',
                'clause'        => '1=1',
                'withFile'      => false,
                'orderBy'       => ['res_id'],
                'limit'         => 'wrong format'
            ];
            $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
    
            $response     = $resController->getList($fullRequest, new \Slim\Http\Response());
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertSame('Bad Request: limit parameter not valid', $responseBody['errors']);
    
            $aArgs = [
                'select'        => 'res_id',
                'clause'        => 'dundermifflin_clients.branch',
                'withFile'      => false,
                'orderBy'       => ['res_id'],
                'limit'         => 1
            ];
            $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
    
            $response     = $resController->getList($fullRequest, new \Slim\Http\Response());
            $responseBody = json_decode((string)$response->getBody(), true);
            $this->assertSame(_INVALID_REQUEST, $responseBody['errors']);
        }
    
        public function testGetProcessingData()
        {
            $resController = new \Resource\controllers\ResController();
    
            $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
            $request        = \Slim\Http\Request::createFromEnvironment($environment);