From e63d6dc4d5cb1fa309b5051e8b9fd16f2a43323b Mon Sep 17 00:00:00 2001
From: Damien <damien.burel@maarch.org>
Date: Thu, 11 Jan 2018 12:24:58 +0100
Subject: [PATCH] FEAT #6949 Basket administration Test

---
 core/Test/BasketControllerTest.php | 338 +++++++++++++++++++++++++++++
 core/Test/define.php               |   1 +
 phpunit.xml                        |   3 +-
 3 files changed, 341 insertions(+), 1 deletion(-)
 create mode 100644 core/Test/BasketControllerTest.php

diff --git a/core/Test/BasketControllerTest.php b/core/Test/BasketControllerTest.php
new file mode 100644
index 00000000000..3d2ba449af7
--- /dev/null
+++ b/core/Test/BasketControllerTest.php
@@ -0,0 +1,338 @@
+<?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 BasketControllerTest extends TestCase
+{
+    public function testCreate()
+    {
+        $basketController = new \Basket\controllers\BasketController();
+
+        //  CREATE
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+
+        $aArgs = [
+            'id'                => 'TEST-BASKET123',
+            'name'              => 'TEST-BASKET123-NAME',
+            'description'       => 'TEST BASKET123 DESCRIPTION',
+            'clause'            => '1=2',
+            'isSearchBasket'    => true,
+            'isFolderBasket'    => true,
+            'color'             => '#123456'
+        ];
+        $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
+
+        $response     = $basketController->create($fullRequest, new \Slim\Http\Response());
+        $responseBody = json_decode((string)$response->getBody());
+
+        $this->assertSame('TEST-BASKET123', $responseBody->basket);
+
+        //  READ
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+        $response       = $basketController->getById($request, new \Slim\Http\Response(), ['id' => 'TEST-BASKET123']);
+        $responseBody   = json_decode((string)$response->getBody());
+
+        $this->assertSame('TEST-BASKET123', $responseBody->basket->basket_id);
+        $this->assertSame('TEST-BASKET123-NAME', $responseBody->basket->basket_name);
+        $this->assertSame('TEST BASKET123 DESCRIPTION', $responseBody->basket->basket_desc);
+        $this->assertSame('1=2', $responseBody->basket->basket_clause);
+        $this->assertSame('N', $responseBody->basket->is_visible);
+        $this->assertSame('Y', $responseBody->basket->is_folder_basket);
+        $this->assertSame('N', $responseBody->basket->flag_notif);
+        $this->assertSame('#123456', $responseBody->basket->color);
+    }
+
+    public function testUpdate()
+    {
+        $basketController = new \Basket\controllers\BasketController();
+
+        //  UPDATE
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+        $aArgs = [
+            'name'              => 'TEST-BASKET123-UPDATED',
+            'description'       => 'TEST BASKET123 DESCRIPTION UPDATED',
+            'clause'            => '1=3',
+            'isSearchBasket'    => false,
+            'isFolderBasket'    => false,
+            'flagNotif'         => true,
+            'color'             => '#111222'
+        ];
+        $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
+
+        $response     = $basketController->update($fullRequest, new \Slim\Http\Response(), ['id' => 'TEST-BASKET123']);
+        $responseBody = json_decode((string)$response->getBody());
+
+        $this->assertSame('success', $responseBody->success);
+
+        //  READ
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+        $response       = $basketController->getById($request, new \Slim\Http\Response(), ['id' => 'TEST-BASKET123']);
+        $responseBody   = json_decode((string)$response->getBody());
+
+        $this->assertSame('TEST-BASKET123', $responseBody->basket->basket_id);
+        $this->assertSame('TEST-BASKET123-UPDATED', $responseBody->basket->basket_name);
+        $this->assertSame('TEST BASKET123 DESCRIPTION UPDATED', $responseBody->basket->basket_desc);
+        $this->assertSame('1=3', $responseBody->basket->basket_clause);
+        $this->assertSame('Y', $responseBody->basket->is_visible);
+        $this->assertSame('N', $responseBody->basket->is_folder_basket);
+        $this->assertSame('Y', $responseBody->basket->flag_notif);
+        $this->assertSame('#111222', $responseBody->basket->color);
+    }
+
+    public function testCreateGroup()
+    {
+        $basketController = new \Basket\controllers\BasketController();
+
+        //  CREATE
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'POST']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+
+        $aArgs = [
+            'group_id'      => 'AGENT',
+            'result_page'   => 'redirect_to_action',
+            'groupActions'  => [
+                [
+                    'id_action'             => '112',
+                    'where_clause'          => '1=2',
+                    'used_in_basketlist'    => false,
+                    'used_in_action_page'   => false,
+                    'default_action_list'   => true,
+                    'statuses'              => [
+                        'NEW',
+                        'END'
+                    ],
+                    'redirects'             => [
+                        [
+                            'entity_id'     => '',
+                            'keyword'       => 'MY_ENTITIES',
+                            'redirect_mode' => 'ENTITY'
+                        ]
+                    ]
+                ]
+            ]
+        ];
+        $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
+
+        $response     = $basketController->createGroup($fullRequest, new \Slim\Http\Response(), ['id' => 'TEST-BASKET123']);
+        $responseBody = json_decode((string)$response->getBody());
+
+        $this->assertSame('success', $responseBody->success);
+
+        //  READ
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+        $response       = $basketController->getGroups($request, new \Slim\Http\Response(), ['id' => 'TEST-BASKET123']);
+        $responseBody   = json_decode((string)$response->getBody());
+
+        $this->assertSame('AGENT', $responseBody->groups[0]->group_id);
+        $this->assertSame('TEST-BASKET123', $responseBody->groups[0]->basket_id);
+        $this->assertSame('redirect_to_action', $responseBody->groups[0]->result_page);
+        $this->assertInternalType('array', $responseBody->groups[0]->groupActions);
+        $this->assertNotNull($responseBody->groups[0]->groupActions);
+        $this->assertSame(112, $responseBody->groups[0]->groupActions[0]->id_action);
+        $this->assertSame('1=2', $responseBody->groups[0]->groupActions[0]->where_clause);
+        $this->assertSame('N', $responseBody->groups[0]->groupActions[0]->used_in_basketlist);
+        $this->assertSame('N', $responseBody->groups[0]->groupActions[0]->used_in_action_page);
+        $this->assertSame('Y', $responseBody->groups[0]->groupActions[0]->default_action_list);
+        $this->assertInternalType('array', $responseBody->groups[0]->groupActions[0]->statuses);
+        $this->assertNotNull($responseBody->groups[0]->groupActions[0]->statuses);
+        $this->assertSame('NEW', $responseBody->groups[0]->groupActions[0]->statuses[0]);
+        $this->assertSame('END', $responseBody->groups[0]->groupActions[0]->statuses[1]);
+        $this->assertInternalType('array', $responseBody->groups[0]->groupActions[0]->redirects);
+        $this->assertNotNull($responseBody->groups[0]->groupActions[0]->redirects);
+        $this->assertSame('', $responseBody->groups[0]->groupActions[0]->redirects[0]->entity_id);
+        $this->assertSame('MY_ENTITIES', $responseBody->groups[0]->groupActions[0]->redirects[0]->keyword);
+        $this->assertSame('ENTITY', $responseBody->groups[0]->groupActions[0]->redirects[0]->redirect_mode);
+    }
+
+    public function testUpdateGroup()
+    {
+        $basketController = new \Basket\controllers\BasketController();
+
+        //  CREATE
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'PUT']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+
+        $aArgs = [
+            'result_page'   => 'list_with_attachments',
+            'groupActions'  => [
+                [
+                    'id_action'             => '1',
+                    'where_clause'          => '1=1',
+                    'used_in_basketlist'    => true,
+                    'used_in_action_page'   => true,
+                    'default_action_list'   => true,
+                    'statuses'              => [
+                        'END',
+                    ],
+                    'redirects'             => [
+                        [
+                            'entity_id'     => '',
+                            'keyword'       => 'ALL_ENTITIES',
+                            'redirect_mode' => 'ENTITY'
+                        ]
+                    ]
+                ],
+                [
+                    'id_action'             => '4',
+                    'where_clause'          => '1=4',
+                    'used_in_basketlist'    => false,
+                    'used_in_action_page'   => true,
+                    'default_action_list'   => false,
+                    'statuses'              => [
+                        'NEW',
+                        'COU'
+                    ],
+                    'redirects'             => [
+                        [
+                            'entity_id'     => 'PSO',
+                            'keyword'       => '',
+                            'redirect_mode' => 'ENTITY'
+                        ],
+                        [
+                            'entity_id'     => 'PSF',
+                            'keyword'       => '',
+                            'redirect_mode' => 'USERS'
+                        ]
+                    ]
+                ]
+            ]
+        ];
+        $fullRequest = \httpRequestCustom::addContentInBody($aArgs, $request);
+
+        $response     = $basketController->updateGroup($fullRequest, new \Slim\Http\Response(), ['id' => 'TEST-BASKET123', 'groupId' => 'AGENT']);
+        $responseBody = json_decode((string)$response->getBody());
+
+        $this->assertSame('success', $responseBody->success);
+
+        //  READ
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+        $response       = $basketController->getGroups($request, new \Slim\Http\Response(), ['id' => 'TEST-BASKET123']);
+        $responseBody   = json_decode((string)$response->getBody());
+
+        $this->assertSame('AGENT', $responseBody->groups[0]->group_id);
+        $this->assertSame('TEST-BASKET123', $responseBody->groups[0]->basket_id);
+        $this->assertSame('list_with_attachments', $responseBody->groups[0]->result_page);
+        $this->assertInternalType('array', $responseBody->groups[0]->groupActions);
+        $this->assertNotNull($responseBody->groups[0]->groupActions);
+
+        $this->assertSame(1, $responseBody->groups[0]->groupActions[0]->id_action);
+        $this->assertSame('1=1', $responseBody->groups[0]->groupActions[0]->where_clause);
+        $this->assertSame('Y', $responseBody->groups[0]->groupActions[0]->used_in_basketlist);
+        $this->assertSame('Y', $responseBody->groups[0]->groupActions[0]->used_in_action_page);
+        $this->assertSame('Y', $responseBody->groups[0]->groupActions[0]->default_action_list);
+        $this->assertInternalType('array', $responseBody->groups[0]->groupActions[0]->statuses);
+        $this->assertNotNull($responseBody->groups[0]->groupActions[0]->statuses);
+        $this->assertSame('END', $responseBody->groups[0]->groupActions[0]->statuses[0]);
+        $this->assertInternalType('array', $responseBody->groups[0]->groupActions[0]->redirects);
+        $this->assertNotNull($responseBody->groups[0]->groupActions[0]->redirects);
+        $this->assertSame('', $responseBody->groups[0]->groupActions[0]->redirects[0]->entity_id);
+        $this->assertSame('ALL_ENTITIES', $responseBody->groups[0]->groupActions[0]->redirects[0]->keyword);
+        $this->assertSame('ENTITY', $responseBody->groups[0]->groupActions[0]->redirects[0]->redirect_mode);
+
+        $this->assertSame(4, $responseBody->groups[0]->groupActions[1]->id_action);
+        $this->assertSame('1=4', $responseBody->groups[0]->groupActions[1]->where_clause);
+        $this->assertSame('N', $responseBody->groups[0]->groupActions[1]->used_in_basketlist);
+        $this->assertSame('Y', $responseBody->groups[0]->groupActions[1]->used_in_action_page);
+        $this->assertSame('N', $responseBody->groups[0]->groupActions[1]->default_action_list);
+        $this->assertInternalType('array', $responseBody->groups[0]->groupActions[1]->statuses);
+        $this->assertNotNull($responseBody->groups[0]->groupActions[1]->statuses);
+        $this->assertSame('NEW', $responseBody->groups[0]->groupActions[1]->statuses[0]);
+        $this->assertSame('COU', $responseBody->groups[0]->groupActions[1]->statuses[1]);
+        $this->assertInternalType('array', $responseBody->groups[0]->groupActions[1]->redirects);
+        $this->assertNotNull($responseBody->groups[0]->groupActions[1]->redirects);
+        $this->assertSame('PSO', $responseBody->groups[0]->groupActions[1]->redirects[0]->entity_id);
+        $this->assertSame('', $responseBody->groups[0]->groupActions[1]->redirects[0]->keyword);
+        $this->assertSame('ENTITY', $responseBody->groups[0]->groupActions[1]->redirects[0]->redirect_mode);
+        $this->assertSame('PSF', $responseBody->groups[0]->groupActions[1]->redirects[1]->entity_id);
+        $this->assertSame('', $responseBody->groups[0]->groupActions[1]->redirects[1]->keyword);
+        $this->assertSame('USERS', $responseBody->groups[0]->groupActions[1]->redirects[1]->redirect_mode);
+    }
+
+    public function testDeleteGroup()
+    {
+        $basketController = new \Basket\controllers\BasketController();
+
+        //  DELETE
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+        $response       = $basketController->deleteGroup($request, new \Slim\Http\Response(), ['id' => 'TEST-BASKET123', 'groupId' => 'AGENT']);
+        $responseBody   = json_decode((string)$response->getBody());
+
+        $this->assertSame('success', $responseBody->success);
+
+        //  READ
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+        $response       = $basketController->getGroups($request, new \Slim\Http\Response(), ['id' => 'TEST-BASKET123']);
+        $responseBody   = json_decode((string)$response->getBody());
+
+        $this->assertEmpty($responseBody->groups);
+    }
+
+    public function testGetDataForGroup()
+    {
+        $basketController = new \Basket\controllers\BasketController();
+
+        //  GET
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+        $response       = $basketController->getDataForGroupById($request, new \Slim\Http\Response(), ['id' => 'TEST-BASKET123']);
+        $responseBody   = json_decode((string)$response->getBody());
+
+        $this->assertInternalType('array', $responseBody->groups);
+        $this->assertNotNull($responseBody->groups);
+        $this->assertInternalType('array', $responseBody->pages);
+        $this->assertNotNull($responseBody->pages);
+        $this->assertInternalType('array', $responseBody->actions);
+        $this->assertNotNull($responseBody->actions);
+    }
+
+    public function testGet()
+    {
+        $basketController = new \Basket\controllers\BasketController();
+
+        //  GET
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+        $response       = $basketController->get($request, new \Slim\Http\Response());
+        $responseBody   = json_decode((string)$response->getBody());
+
+        $this->assertInternalType('array', $responseBody->baskets);
+        $this->assertNotNull($responseBody->baskets);
+    }
+
+    public function testDelete()
+    {
+        $basketController = new \Basket\controllers\BasketController();
+
+        //  DELETE
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'DELETE']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+        $response       = $basketController->delete($request, new \Slim\Http\Response(), ['id' => 'TEST-BASKET123']);
+        $responseBody   = json_decode((string)$response->getBody());
+
+        $this->assertInternalType('array', $responseBody->baskets);
+
+        //  READ
+        $environment    = \Slim\Http\Environment::mock(['REQUEST_METHOD' => 'GET']);
+        $request        = \Slim\Http\Request::createFromEnvironment($environment);
+        $response       = $basketController->getById($request, new \Slim\Http\Response(), ['id' => 'TEST-BASKET123']);
+        $responseBody   = json_decode((string)$response->getBody());
+
+        $this->assertSame('Basket not found', $responseBody->errors);
+    }
+
+}
diff --git a/core/Test/define.php b/core/Test/define.php
index 84266b0b287..f09989ef32a 100755
--- a/core/Test/define.php
+++ b/core/Test/define.php
@@ -20,6 +20,7 @@ require_once('core/services/CoreConfig.php');
 //for auth
 $_SERVER['PHP_AUTH_USER'] = 'superadmin';
 $_SERVER['PHP_AUTH_PW'] = 'superadmin';
+$userId = 'superadmin';
 
 //load Maarch session vars
 $portal = new portal();
diff --git a/phpunit.xml b/phpunit.xml
index a3e41ca67c4..503790f2f46 100755
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -9,6 +9,7 @@
             <file>core/Test/ResExtControllerTest.php</file-->
             <file>core/Test/StatusControllerTest.php</file>
             <file>core/Test/UserControllerTest.php</file>
+            <file>core/Test/BasketControllerTest.php</file>
             <!--file>modules/convert/Test/ProcessConvertTest.php</file-->
             <!--file>modules/convert/Test/ProcessFulltextTest.php</file-->
         </testsuite>
@@ -16,6 +17,7 @@
     <filter>
       <whitelist>
           <directory suffix="Test.php">core/Test</directory>
+          <directory suffix=".php">src</directory>
           <directory suffix=".php">core/Controllers</directory>
           <directory suffix=".php">core/Models</directory>
           <directory suffix=".php">apps/maarch_entreprise/Models</directory>
@@ -27,7 +29,6 @@
           <directory suffix=".php">modules/attachments/Models</directory>          
           <directory suffix=".php">modules/entities/Controllers</directory>
           <directory suffix=".php">modules/entities/Models</directory>
-          <directory suffix=".php">modules/basket/Models</directory>
           <directory suffix=".php">modules/notes/Models</directory>
           <directory suffix=".php">modules/convert/Controllers</directory>
           <directory suffix=".php">modules/convert/Models</directory>
-- 
GitLab