From 4a89130965f21b5cc01ac8cc4c9d40134bec5bea Mon Sep 17 00:00:00 2001
From: "florian.azizian" <florian.azizian@maarch.org>
Date: Mon, 23 Sep 2019 16:28:45 +0100
Subject: [PATCH] FEAT #11271 TIME 1 TU indexing actions and entities

---
 phpunit.xml                                   |  1 +
 src/app/group/models/GroupModelAbstract.php   |  3 +-
 .../app/indexing/IndexingControllerTest.php   | 75 +++++++++++++++++++
 3 files changed, 78 insertions(+), 1 deletion(-)
 create mode 100644 test/unitTests/app/indexing/IndexingControllerTest.php

diff --git a/phpunit.xml b/phpunit.xml
index 79edad44b80..cb6eb99a317 100755
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -19,6 +19,7 @@
             <file>test/unitTests/app/group/GroupControllerTest.php</file>
             <file>test/unitTests/app/entity/ListTemplateControllerTest.php</file>
             <file>test/unitTests/app/indexingModel/IndexingModelControllerTest.php</file>
+            <file>test/unitTests/app/indexing/IndexingControllerTest.php</file>
             <file>test/unitTests/app/notification/NotificationControllerTest.php</file>
             <file>test/unitTests/app/notification/NotificationScheduleControllerTest.php</file>
             <file>test/unitTests/app/parameter/ParameterControllerTest.php</file>
diff --git a/src/app/group/models/GroupModelAbstract.php b/src/app/group/models/GroupModelAbstract.php
index baa7d04b69a..378d8f19a7e 100755
--- a/src/app/group/models/GroupModelAbstract.php
+++ b/src/app/group/models/GroupModelAbstract.php
@@ -219,7 +219,8 @@ abstract class GroupModelAbstract
     public static function getGroupByLogin(array $aArgs = [])
     {
         ValidatorModel::notEmpty($aArgs, ['login', 'groupId']);
-        ValidatorModel::stringType($aArgs, ['login', 'groupId']);
+        ValidatorModel::stringType($aArgs, ['login']);
+        ValidatorModel::intVal($aArgs, ['groupId']);
 
         $aGroups = DatabaseModel::select([
             'select'    => empty($aArgs['select']) ? ['*'] : $aArgs['select'],
diff --git a/test/unitTests/app/indexing/IndexingControllerTest.php b/test/unitTests/app/indexing/IndexingControllerTest.php
new file mode 100644
index 00000000000..c436d4169f9
--- /dev/null
+++ b/test/unitTests/app/indexing/IndexingControllerTest.php
@@ -0,0 +1,75 @@
+<?php
+
+/**
+* Copyright Maarch since 2008 under licence GPLv3.
+* See LICENCE.txt file at the root folder for more details.
+* This file is part of Maarch software.
+*
+*/
+
+use PHPUnit\Framework\TestCase;
+
+class IndexingControllerTest extends TestCase
+{
+    public function testGetIndexingActions()
+    {
+        $GLOBALS['userId'] = 'bbain';
+
+        $indexingController = new \Resource\controllers\IndexingController();
+
+        //  GET
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+
+        $response     = $indexingController->getIndexingActions($request, new \Slim\Http\Response(), ['groupId' => 2]);
+        $this->assertSame(200, $response->getStatusCode());
+
+        $responseBody = json_decode((string)$response->getBody());
+
+        $this->assertNotEmpty($responseBody->actions);
+        foreach ($responseBody->actions as $action) {
+            $this->assertNotEmpty($action->id);
+            $this->assertInternalType('int', $action->id);
+            $this->assertNotEmpty($action->label_action);
+            $this->assertNotEmpty($action->component);
+        }
+
+        //ERROR
+        $response = $indexingController->getIndexingActions($request, new \Slim\Http\Response(), ['groupId' => 99999]);
+        $responseBody = json_decode((string)$response->getBody());
+        $this->assertSame('This user is not in this group', $responseBody->errors);
+
+        $GLOBALS['userId'] = 'superadmin';
+    }
+
+    public function testGetIndexingEntities()
+    {
+        $GLOBALS['userId'] = 'bbain';
+
+        $indexingController = new \Resource\controllers\IndexingController();
+
+        //  GET
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+
+        $response     = $indexingController->getIndexingEntities($request, new \Slim\Http\Response(), ['groupId' => 2]);
+        $this->assertSame(200, $response->getStatusCode());
+
+        $responseBody = json_decode((string)$response->getBody());
+
+        $this->assertNotEmpty($responseBody->entities);
+        foreach ($responseBody->entities as $entity) {
+            $this->assertNotEmpty($entity->id);
+            $this->assertInternalType('int', $entity->id);
+            $this->assertNotEmpty($entity->entity_label);
+            $this->assertNotEmpty($entity->entity_id);
+        }
+
+        //ERROR
+        $response = $indexingController->getIndexingActions($request, new \Slim\Http\Response(), ['groupId' => 99999]);
+        $responseBody = json_decode((string)$response->getBody());
+        $this->assertSame('This user is not in this group', $responseBody->errors);
+
+        $GLOBALS['userId'] = 'superadmin';
+    }
+}
-- 
GitLab