From 050387f5cf0b5fcc82e8fc8865d425a213b50f0e Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Thu, 2 May 2019 14:37:28 +0100
Subject: [PATCH] FEAT #10387 TIME 2 improve unit test

---
 phpunit.xml                                   |  1 +
 .../entity/controllers/EntityController.php   | 19 +++--
 .../unitTests/app/home/HomeControllerTest.php | 14 ++++
 .../unitTests/app/user/UserControllerTest.php | 75 +++++++++++++++++++
 4 files changed, 99 insertions(+), 10 deletions(-)

diff --git a/phpunit.xml b/phpunit.xml
index ee363fbb3eb..9aaca30ba5e 100755
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -28,6 +28,7 @@
             <file>test/unitTests/app/resource/ExportControllerTest.php</file>
             <file>test/unitTests/app/resource/SummarySheetControllerTest.php</file>
             <file>test/unitTests/app/status/StatusControllerTest.php</file>
+            <file>test/unitTests/app/shipping/ShippingControllerTest.php</file>
             <file>test/unitTests/app/user/UserControllerTest.php</file>
             <file>test/unitTests/app/versionUpdate/VersionUpdateControllerTest.php</file>
             <file>test/unitTests/app/template/TemplateControllerTest.php</file>
diff --git a/src/app/entity/controllers/EntityController.php b/src/app/entity/controllers/EntityController.php
index a464cc05cd4..5ba5b9291e9 100755
--- a/src/app/entity/controllers/EntityController.php
+++ b/src/app/entity/controllers/EntityController.php
@@ -14,7 +14,6 @@
 
 namespace Entity\controllers;
 
-use Basket\models\BasketModel;
 use Basket\models\GroupBasketRedirectModel;
 use Entity\models\EntityModel;
 use Entity\models\ListInstanceModel;
@@ -39,7 +38,7 @@ class EntityController
 
     public function getById(Request $request, Response $response, array $aArgs)
     {
-        $entity = entitymodel::getByEntityId(['entityId' => $aArgs['id']]);
+        $entity = EntityModel::getByEntityId(['entityId' => $aArgs['id']]);
         if (empty($entity)) {
             return $response->withStatus(400)->withJson(['errors' => 'Entity not found']);
         }
@@ -53,7 +52,7 @@ class EntityController
             return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
         }
 
-        $entity = entitymodel::getByEntityId(['entityId' => $aArgs['id']]);
+        $entity = EntityModel::getByEntityId(['entityId' => $aArgs['id']]);
         if (empty($entity)) {
             return $response->withStatus(400)->withJson(['errors' => 'Entity not found']);
         }
@@ -122,7 +121,7 @@ class EntityController
                         'sequence'              => $listTemplate['sequence'],
                         'title'                 => $listTemplate['title'],
                         'description'           => $listTemplate['description'],
-                        'labelToDisplay'        => entitymodel::getByEntityId(['entityId' => $listTemplate['item_id'], 'select' => ['entity_label']])['entity_label'],
+                        'labelToDisplay'        => EntityModel::getByEntityId(['entityId' => $listTemplate['item_id'], 'select' => ['entity_label']])['entity_label'],
                         'descriptionToDisplay'  => ''
                     ];
                 }
@@ -181,7 +180,7 @@ class EntityController
             return $response->withStatus(400)->withJson(['errors' => 'Bad Request']);
         }
 
-        $existingEntity = entitymodel::getByEntityId(['entityId' => $data['entity_id'], 'select' => [1]]);
+        $existingEntity = EntityModel::getByEntityId(['entityId' => $data['entity_id'], 'select' => [1]]);
         if (!empty($existingEntity)) {
             return $response->withStatus(400)->withJson(['errors' => _ENTITY_ID_ALREADY_EXISTS]);
         }
@@ -224,7 +223,7 @@ class EntityController
             return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
         }
 
-        $entity = entitymodel::getByEntityId(['entityId' => $aArgs['id'], 'select' => [1]]);
+        $entity = EntityModel::getByEntityId(['entityId' => $aArgs['id'], 'select' => [1]]);
         if (empty($entity)) {
             return $response->withStatus(400)->withJson(['errors' => 'Entity not found']);
         }
@@ -301,7 +300,7 @@ class EntityController
             return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
         }
 
-        $entity = entitymodel::getByEntityId(['entityId' => $aArgs['id'], 'select' => [1]]);
+        $entity = EntityModel::getByEntityId(['entityId' => $aArgs['id'], 'select' => [1]]);
         if (empty($entity)) {
             return $response->withStatus(400)->withJson(['errors' => 'Entity not found']);
         }
@@ -346,8 +345,8 @@ class EntityController
             return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
         }
 
-        $dyingEntity = entitymodel::getByEntityId(['entityId' => $aArgs['id'], 'select' => ['parent_entity_id']]);
-        $successorEntity = entitymodel::getByEntityId(['entityId' => $aArgs['newEntityId'], 'select' => [1]]);
+        $dyingEntity = EntityModel::getByEntityId(['entityId' => $aArgs['id'], 'select' => ['parent_entity_id']]);
+        $successorEntity = EntityModel::getByEntityId(['entityId' => $aArgs['newEntityId'], 'select' => [1]]);
         if (empty($dyingEntity) || empty($successorEntity)) {
             return $response->withStatus(400)->withJson(['errors' => 'Entity does not exist']);
         }
@@ -421,7 +420,7 @@ class EntityController
             return $response->withStatus(403)->withJson(['errors' => 'Service forbidden']);
         }
 
-        $entity = entitymodel::getByEntityId(['entityId' => $aArgs['id'], 'select' => [1]]);
+        $entity = EntityModel::getByEntityId(['entityId' => $aArgs['id'], 'select' => [1]]);
         if (empty($entity)) {
             return $response->withStatus(400)->withJson(['errors' => 'Entity not found']);
         }
diff --git a/test/unitTests/app/home/HomeControllerTest.php b/test/unitTests/app/home/HomeControllerTest.php
index d4ee53e6d18..a0019921eae 100755
--- a/test/unitTests/app/home/HomeControllerTest.php
+++ b/test/unitTests/app/home/HomeControllerTest.php
@@ -10,6 +10,7 @@
 * @author  dev <dev@maarch.org>
 * @ingroup core
 */
+
 use PHPUnit\Framework\TestCase;
 
 class HomeControllerTest extends TestCase
@@ -30,4 +31,17 @@ class HomeControllerTest extends TestCase
         $this->assertNotNull($responseBody->assignedBaskets);
         $this->assertNotEmpty($responseBody->homeMessage);
     }
+
+    public function testGetLastRessources()
+    {
+        $homeController = new \Home\controllers\HomeController();
+
+        $environment = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
+        $request = \Slim\Http\Request::createFromEnvironment($environment);
+
+        $response = $homeController->getLastRessources($request, new \Slim\Http\Response());
+        $responseBody = json_decode((string) $response->getBody());
+        
+        $this->assertInternalType('array', $responseBody->lastResources);
+    }
 }
diff --git a/test/unitTests/app/user/UserControllerTest.php b/test/unitTests/app/user/UserControllerTest.php
index a859d85a397..179040edd86 100755
--- a/test/unitTests/app/user/UserControllerTest.php
+++ b/test/unitTests/app/user/UserControllerTest.php
@@ -272,6 +272,81 @@ class UserControllerTest extends TestCase
         $this->assertSame('Y', $responseBody->entities[0]->primary_entity);
     }
 
+    public function testGetUsersById()
+    {
+        $entityController = new \Entity\controllers\EntityController();
+
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+        $response       = $entityController->getById($request, new \Slim\Http\Response(), ['id' => 'DGS']);
+        $responseBody   = json_decode((string)$response->getBody());
+        $entitySerialId = $responseBody->entity->id;
+
+        $response     = $entityController->getUsersById($request, new \Slim\Http\Response(), ['id' => $entitySerialId]);
+        $responseBody = json_decode((string)$response->getBody());
+
+        $this->assertNotNull($responseBody->users);
+
+        $found = false;
+        foreach ($responseBody->users as $value) {
+            $this->assertNotNull($value->id);
+            $this->assertInternalType('integer', $value->id);
+            $this->assertNotNull($value->user_id);
+            $this->assertNotNull($value->firstname);
+            $this->assertNotNull($value->lastname);
+            $this->assertNotNull($value->labelToDisplay);
+            $this->assertNotNull($value->descriptionToDisplay);
+
+            if ($value->id == self::$id) {
+                $this->assertSame('test-ckent', $value->user_id);
+                $this->assertSame('TEST-CLARK2', $value->firstname);
+                $this->assertSame('TEST-KENT2', $value->lastname);
+                $this->assertSame($value->firstname . ' ' . $value->lastname, $value->labelToDisplay);
+                $found = true;
+            }
+        }
+
+        $this->assertSame(true, $found);
+
+        //ERROR
+        $response     = $entityController->getUsersById($request, new \Slim\Http\Response(), ['id' => 99989]);
+        $responseBody = json_decode((string)$response->getBody());
+        $this->assertSame('Entity not found', $responseBody->errors);
+    }
+
+    public function testIsDeletable()
+    {
+        $userController = new \User\controllers\UserController();
+
+        //  GET
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+
+        $response     = $userController->isDeletable($request, new \Slim\Http\Response(), ['id' => self::$id]);
+        $responseBody = json_decode((string)$response->getBody());
+
+        $this->assertSame(true, $responseBody->isDeletable);
+        $this->assertInternalType('array', $responseBody->listTemplates);
+        $this->assertEmpty($responseBody->listTemplates);
+        $this->assertInternalType('array', $responseBody->listInstances);
+        $this->assertEmpty($responseBody->listInstances);
+    }
+
+    public function testIsEntityDeletable()
+    {
+        $userController = new \User\controllers\UserController();
+
+        //  GET
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+
+        $response     = $userController->isEntityDeletable($request, new \Slim\Http\Response(), ['id' => self::$id, 'entityId' => 'DGS']);
+        $responseBody = json_decode((string)$response->getBody());
+
+        $this->assertSame(false, $responseBody->hasConfidentialityInstances);
+        $this->assertSame(false, $responseBody->hasListTemplates);
+    }
+
     public function testUpdatePrimaryEntity()
     {
         $userController = new \User\controllers\UserController();
-- 
GitLab