]> git.agnieray.net Git - galette.git/commitdiff
Work on PdfController tests
authorJohan Cwiklinski <johan@x-tnd.be>
Tue, 23 Nov 2021 07:37:17 +0000 (08:37 +0100)
committerJohan Cwiklinski <johan@x-tnd.be>
Wed, 24 Nov 2021 06:49:33 +0000 (07:49 +0100)
galette/lib/Galette/Controllers/PdfController.php
galette/lib/Galette/Entity/Contribution.php
tests/Galette/Controllers/tests/units/PdfController.php
tests/GaletteTestCase.php

index 7868216e689f8c9e03f437bb2626d3798e633c96..3142e47f3f6f160a22be493dafe69518e0fa8d64 100644 (file)
@@ -125,7 +125,7 @@ class PdfController extends AbstractController
                     );
             }
 
-            //check if member is up to date
+            //check if member is up-to-date
             if ($this->login->id == $id_adh) {
                 if (!$adh->isUp2Date()) {
                     Analog::log(
index 65a7576f30a2119d67c6336c73849ea6db0cb263..47cd94d568895f19165a0ebcab5f56d11db6e609 100644 (file)
@@ -121,7 +121,7 @@ class Contribution
      * @param Db                 $zdb   Database
      * @param Login              $login Login instance
      * @param null|int|ResultSet $args  Either a ResultSet row to load
-     *                                  a specific contribution, or an type id
+     *                                  a specific contribution, or a type id
      *                                  to just instantiate object
      */
     public function __construct(Db $zdb, Login $login, $args = null)
@@ -291,6 +291,10 @@ class Contribution
      */
     public function load($id)
     {
+        if (!$this->login->isLogged()) {
+            return false;
+        }
+
         try {
             $select = $this->zdb->select(self::TABLE, 'c');
             $select->join(
index dc6171993ca2807e1bd035e89e80c42d444b231a..dcdadd9909a6649f4d637db234bc4524831d3b88 100644 (file)
@@ -7,7 +7,7 @@
  *
  * PHP version 5
  *
- * Copyright © 2020 The Galette Team
+ * Copyright © 2020-2021 The Galette Team
  *
  * This file is part of Galette (http://galette.tuxfamily.org).
  *
@@ -28,7 +28,7 @@
  * @package   GaletteTests
  *
  * @author    Johan Cwiklinski <johan@x-tnd.be>
- * @copyright 2020 The Galette Team
+ * @copyright 2020-2021 The Galette Team
  * @license   http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
  * @version   SVN: $Id$
  * @link      http://galette.tuxfamily.org
@@ -38,6 +38,7 @@
 namespace Galette\Controllers\test\units;
 
 use atoum;
+use Galette\GaletteTestCase;
 
 /**
  * PDF controller tests
@@ -51,24 +52,9 @@ use atoum;
  * @link      http://galette.tuxfamily.org
  * @since     2020-12-06
  */
-class PdfController extends atoum
+class PdfController extends GaletteTestCase
 {
-    private $zdb;
-    private $preferences;
-    private $login;
-    private $remove = [];
-    private $i18n;
-    private $container;
-    private $history;
-
-    private $adh;
-    private $contrib;
-    private $members_fields;
-
-    private $mocked_router;
-    private $session;
-    private $flash_data = [];
-    private $flash;
+    protected $seed = 58144569971203;
 
     /**
      * Set up tests
@@ -79,32 +65,11 @@ class PdfController extends atoum
      */
     public function beforeTestMethod($method)
     {
-        $this->zdb = new \Galette\Core\Db();
-        $this->preferences = new \Galette\Core\Preferences($this->zdb);
-        $this->i18n = new \Galette\Core\I18n(
-            \Galette\Core\I18n::DEFAULT_LANG
-        );
-        $this->login = new \Galette\Core\Login($this->zdb, $this->i18n);
-
-        $models = new \Galette\Repository\PdfModels($this->zdb, $this->preferences, $this->login);
-        $res = $models->installInit(false);
-        $this->boolean($res)->isTrue();
-
-        $this->mocked_router = new \mock\Slim\Router();
-        $this->calling($this->mocked_router)->pathFor = function ($name, $params) {
-            return $name;
-        };
-
-        $this->session = new \RKA\Session();
-        $this->history = new \Galette\Core\History($this->zdb, $this->login, $this->preferences);
-        $flash_data = [];
-        $this->flash_data = &$flash_data;
-        $this->flash = new \Slim\Flash\Messages($flash_data);
+        parent::beforeTestMethod($method);
 
-        $_SERVER['HTTP_HOST'] = '';
-
-        include_once GALETTE_ROOT . 'includes/fields_defs/members_fields.php';
-        $this->members_fields = $members_fields;
+        $this->initModels();
+        $this->initStatus();
+        $this->initContributionsTypes();
 
         $this->adh = new \Galette\Entity\Adherent($this->zdb);
         $this->adh->setDependencies(
@@ -112,56 +77,26 @@ class PdfController extends atoum
             $this->members_fields,
             $this->history
         );
-
-        $app = new \Slim\App(['router' => $this->mocked_router, 'flash' => $this->flash]);
-        $this->container = $app->getContainer();
-
-        $this->container['view'] = null;
-        $this->container['zdb'] = $this->zdb;
-        $this->container['login'] = $this->login;
-        $this->container['session'] = $this->session;
-        $this->container['preferences'] = $this->preferences;
-        $this->container['logo'] = null;
-        $this->container['print_logo'] = null;
-        $this->container['plugins'] = null;
-        $this->container['history'] = $this->history;
-        $this->container['i18n'] = null;
-        $this->container['fields_config'] = null;
-        $this->container['lists_config'] = null;
-        $this->container['l10n'] = null;
-        $this->container['members_fields'] = $this->members_fields;
-        $members_form_fields = $this->members_fields;
-        foreach ($members_form_fields as $k => $field) {
-            if ($field['position'] == -1) {
-                unset($members_form_fields[$k]);
-            }
-        }
-        //$this->members_form_fields = $members_form_fields;
-        $this->container['members_form_fields'] = $members_form_fields;
-
-        include_once GALETTE_ROOT . 'includes/fields_defs/members_fields_cats.php';
-        $this->container['members_fields_cats'] = $members_fields_cats;
-
-        global $container, $zdb, $i18n, $login, $hist;
-        $zdb = $this->zdb; //globals '(
-        $container = $this->container; //globals '(
-        $i18n = $this->i18n;
-        $login = $this->login;
-        $hist = $this->history;
     }
 
     /**
-     * Tear down tests
-     *
-     * @param string $method Calling method
+     * Cleanup after tests
      *
      * @return void
      */
-    public function afterTestMethod($method)
+    public function tearDown()
     {
-        if (TYPE_DB === 'mysql') {
-            $this->array($this->zdb->getWarnings())->isIdenticalTo([]);
-        }
+        $this->zdb = new \Galette\Core\Db();
+
+        $delete = $this->zdb->delete(\Galette\Entity\Contribution::TABLE);
+        $delete->where(['info_cotis' => 'FAKER' . $this->seed]);
+        $this->zdb->execute($delete);
+
+        $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE);
+        $delete->where(['fingerprint' => 'FAKER' . $this->seed]);
+        $this->zdb->execute($delete);
+
+        $this->cleanHistory();
     }
 
     /**
@@ -210,4 +145,351 @@ class PdfController extends atoum
         $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
         $this->string($model->title)->isIdenticalTo('DaTitle');
     }
+
+    /**
+     * Test membersCards
+     *
+     * @return void
+     */
+    public function testMembersCards()
+    {
+        $this->getMemberOne();
+        $environment = \Slim\Http\Environment::mock(
+            [
+                'REQUEST_METHOD' => 'POST',
+                'REQUEST_URI' => '/members/card/' . $this->adh->id
+            ]
+        );
+        $uri = \Slim\Http\Uri::createFromEnvironment($environment);
+        $headers = \Slim\Http\Headers::createFromEnvironment($environment);
+        $cookies = [];
+        $serverParams = $environment->all();
+
+        $body = new \Slim\Http\RequestBody();
+        $request = new \Slim\Http\Request('POST', $uri, $headers, $cookies, $serverParams, $body);
+        $request = $request->withHeader('Content-Type', 'text/html');
+        $response = new \Slim\Http\Response();
+        $controller = new \Galette\Controllers\PdfController($this->container);
+
+        //test with non-logged-in user
+        $test_response = $controller->membersCards($request, $response, $this->adh->id);
+        $this->array($test_response->getHeaders())->isIdenticalTo(['Location' => ['me']]);
+        $this->integer($test_response->getStatusCode())->isIdenticalTo(302);
+        $this->boolean($test_response->isServerError())->isFalse();
+        $this->array($this->flash_data['slimFlash'])->isIdenticalTo([
+            'error_detected' => [
+                'You do not have permission for requested URL.'
+            ]
+        ]);
+        $this->flash_data = [];
+
+        //test logged-in as superadmin
+        $this->logSuperAdmin();
+        $test_response = null;
+        $this
+            ->output(
+                function () use ($controller, $request, $response, &$test_response) {
+                    $test_response = $controller->membersCards($request, $response, $this->adh->id);
+                }
+            )->isNotEmpty();
+
+        $this->integer($test_response->getStatusCode())->isIdenticalTo(200);
+        $this->boolean($test_response->isServerError())->isFalse();
+        $this->string($test_response->getHeader('Content-type')[0])->isIdenticalTo('application/pdf');
+        $this->string($test_response->getHeader('Content-Disposition')[0])->isIdenticalTo('attachment;filename="cards.pdf"');
+
+        //test no selection
+        $test_response = null;
+        $test_response = $controller->membersCards($request, $response);
+        $this->array($test_response->getHeaders())->isIdenticalTo(['Location' => ['members']]);
+        $this->integer($test_response->getStatusCode())->isIdenticalTo(301);
+        $this->boolean($test_response->isServerError())->isFalse();
+        $this->array($this->flash_data['slimFlash'])->isIdenticalTo([
+            'error_detected' => [
+                'No member was selected, please check at least one name.'
+            ]
+        ]);
+        $this->flash_data = [];
+
+        //test again from filters
+        $test_response = null;
+        $filters = new \Galette\Filters\MembersList();
+        $filters->selected = [$this->adh->id];
+        $this->session->filter_members = $filters;
+        $this
+            ->output(
+                function () use ($controller, $request, $response, &$test_response) {
+                    $test_response = $controller->membersCards($request, $response);
+                }
+            )->isNotEmpty();
+
+        unset($this->session->filter_members);
+        $this->integer($test_response->getStatusCode())->isIdenticalTo(200);
+        $this->boolean($test_response->isServerError())->isFalse();
+        $this->string($test_response->getHeader('Content-type')[0])->isIdenticalTo('application/pdf');
+        $this->string($test_response->getHeader('Content-Disposition')[0])->isIdenticalTo('attachment;filename="cards.pdf"');
+    }
+
+    /**
+     * Test membersLabels
+     *
+     * @return void
+     */
+    public function testMembersLabels()
+    {
+        unset($this->session->filter_members);
+        $this->getMemberOne();
+        $environment = \Slim\Http\Environment::mock(
+            [
+                'REQUEST_METHOD' => 'POST',
+                'REQUEST_URI' => '/members/labels'
+            ]
+        );
+        $uri = \Slim\Http\Uri::createFromEnvironment($environment);
+        $headers = \Slim\Http\Headers::createFromEnvironment($environment);
+        $cookies = [];
+        $serverParams = $environment->all();
+
+        $body = new \Slim\Http\RequestBody();
+        $request = new \Slim\Http\Request('POST', $uri, $headers, $cookies, $serverParams, $body);
+        $request = $request->withHeader('Content-Type', 'text/html');
+        $response = new \Slim\Http\Response();
+        $controller = new \Galette\Controllers\PdfController($this->container);
+
+        //test with non-logged-in user
+        $test_response = $controller->membersLabels($request, $response, $this->adh->id);
+        $this->array($test_response->getHeaders())->isIdenticalTo(['Location' => ['members']]);
+        $this->integer($test_response->getStatusCode())->isIdenticalTo(301);
+        $this->boolean($test_response->isServerError())->isFalse();
+        $this->array($this->flash_data['slimFlash'])->isIdenticalTo([
+            'error_detected' => [
+                'No member was selected, please check at least one name.'
+            ]
+        ]);
+        $this->flash_data = [];
+
+        //test again from filters
+        $test_response = null;
+        $filters = new \Galette\Filters\MembersList();
+        $filters->selected = [$this->adh->id];
+        $this->session->filter_members = $filters;
+        $this
+            ->output(
+                function () use ($controller, $request, $response, &$test_response) {
+                    $test_response = $controller->membersLabels($request, $response);
+                }
+            )->isNotEmpty();
+
+        $this->integer($test_response->getStatusCode())->isIdenticalTo(200);
+        $this->boolean($test_response->isServerError())->isFalse();
+        $this->string($test_response->getHeader('Content-type')[0])->isIdenticalTo('application/pdf');
+        $this->string($test_response->getHeader('Content-Disposition')[0])->isIdenticalTo('attachment;filename="labels_print_filename.pdf"');
+        unset($this->session->filter_members);
+
+        //test logged-in as superadmin
+        $this->logSuperAdmin();
+        //test no selection
+        $test_response = null;
+        $test_response = $controller->membersCards($request, $response);
+        $this->array($test_response->getHeaders())->isIdenticalTo(['Location' => ['members']]);
+        $this->integer($test_response->getStatusCode())->isIdenticalTo(301);
+        $this->boolean($test_response->isServerError())->isFalse();
+        $this->array($this->flash_data['slimFlash'])->isIdenticalTo([
+            'error_detected' => [
+                'No member was selected, please check at least one name.'
+            ]
+        ]);
+        $this->flash_data = [];
+
+        //test again from filters
+        $test_response = null;
+        $filters = new \Galette\Filters\MembersList();
+        $filters->selected = [$this->adh->id];
+        $this->session->filter_members = $filters;
+        $this
+            ->output(
+                function () use ($controller, $request, $response, &$test_response) {
+                    $test_response = $controller->membersCards($request, $response);
+                }
+            )->isNotEmpty();
+
+        $this->integer($test_response->getStatusCode())->isIdenticalTo(200);
+        $this->boolean($test_response->isServerError())->isFalse();
+        $this->string($test_response->getHeader('Content-type')[0])->isIdenticalTo('application/pdf');
+        $this->string($test_response->getHeader('Content-Disposition')[0])->isIdenticalTo('attachment;filename="cards.pdf"');
+    }
+
+    /**
+     * Test adhesionForm
+     *
+     * @return void
+     */
+    public function testadhesionForm()
+    {
+        unset($this->session->filter_members);
+        $this->getMemberOne();
+        $environment = \Slim\Http\Environment::mock(
+            [
+                'REQUEST_METHOD' => 'POST',
+                'REQUEST_URI' => '/members/labels'
+            ]
+        );
+        $uri = \Slim\Http\Uri::createFromEnvironment($environment);
+        $headers = \Slim\Http\Headers::createFromEnvironment($environment);
+        $cookies = [];
+        $serverParams = $environment->all();
+
+        $body = new \Slim\Http\RequestBody();
+        $request = new \Slim\Http\Request('POST', $uri, $headers, $cookies, $serverParams, $body);
+        $request = $request->withHeader('Content-Type', 'text/html');
+        $response = new \Slim\Http\Response();
+        $controller = new \Galette\Controllers\PdfController($this->container);
+
+        //test with non-logged-in user
+        $test_response = $controller->adhesionForm($request, $response, $this->adh->id);
+        $this->array($test_response->getHeaders())->isIdenticalTo(['Location' => ['me']]);
+        $this->integer($test_response->getStatusCode())->isIdenticalTo(302);
+        $this->boolean($test_response->isServerError())->isFalse();
+        $this->array($this->flash_data['slimFlash'])->isIdenticalTo([
+            'error_detected' => [
+                'You do not have permission for requested URL.'
+            ]
+        ]);
+        $this->flash_data = [];
+
+        //test logged-in as superadmin
+        $this->logSuperAdmin();
+        $test_response = null;
+        $this
+            ->output(
+                function () use ($controller, $request, $response, &$test_response) {
+                    $test_response = $controller->adhesionForm($request, $response, $this->adh->id);
+                }
+            )->isNotEmpty();
+
+        $this->integer($test_response->getStatusCode())->isIdenticalTo(200);
+        $this->boolean($test_response->isServerError())->isFalse();
+        $this->string($test_response->getHeader('Content-type')[0])->isIdenticalTo('application/pdf');
+        $this->string($test_response->getHeader('Content-Disposition')[0])->isIdenticalTo('attachment;filename="adherent_form.' . $this->adh->id . '.pdf"');
+    }
+
+    /**
+     * Test attendanceSheet
+     *
+     * @return void
+     */
+    public function testAttendanceSheet()
+    {
+        $this->getMemberOne();
+        $environment = \Slim\Http\Environment::mock(
+            [
+                'REQUEST_METHOD' => 'POST',
+                'REQUEST_URI' => '/attendance-sheet'
+            ]
+        );
+        $uri = \Slim\Http\Uri::createFromEnvironment($environment);
+        $headers = \Slim\Http\Headers::createFromEnvironment($environment);
+        $cookies = [];
+        $serverParams = $environment->all();
+
+        $body = new \Slim\Http\RequestBody();
+
+        $request = new \Slim\Http\Request('POST', $uri, $headers, $cookies, $serverParams, $body);
+        $request = $request->withHeader('Content-Type', 'application/json');
+        $response = new \Slim\Http\Response();
+        $controller = new \Galette\Controllers\PdfController($this->container);
+
+        //test no selection
+        $test_response = null;
+        $test_response = $controller->membersCards($request, $response);
+        $this->array($test_response->getHeaders())->isIdenticalTo(['Location' => ['members']]);
+        $this->integer($test_response->getStatusCode())->isIdenticalTo(301);
+        $this->boolean($test_response->isServerError())->isFalse();
+        $this->array($this->flash_data['slimFlash'])->isIdenticalTo([
+            'error_detected' => [
+                'No member was selected, please check at least one name.'
+            ]
+        ]);
+        $this->flash_data = [];
+
+        //test with selection
+        $body = new \Slim\Http\RequestBody();
+        $body->write(
+            json_encode([
+                'selection' => [$this->adh->id]
+            ])
+        );
+
+        $request = new \Slim\Http\Request('POST', $uri, $headers, $cookies, $serverParams, $body);
+        $request = $request->withHeader('Content-Type', 'application/json');
+        $test_response = null;
+        $this
+            ->output(
+                function () use ($controller, $request, $response, &$test_response) {
+                    $test_response = $controller->attendanceSheet($request, $response);
+                }
+            )->isNotEmpty();
+
+        $this->integer($test_response->getStatusCode())->isIdenticalTo(200);
+        $this->boolean($test_response->isServerError())->isFalse();
+        $this->string($test_response->getHeader('Content-type')[0])->isIdenticalTo('application/pdf');
+        $this->string($test_response->getHeader('Content-Disposition')[0])->isIdenticalTo('attachment;filename="attendance_sheet.pdf"');
+    }
+
+    /**
+     * Test contribution
+     *
+     * @return void
+     */
+    public function testContribution()
+    {
+        $this->getMemberOne();
+        $this->createContribution();
+        $environment = \Slim\Http\Environment::mock(
+            [
+                'REQUEST_METHOD' => 'POST',
+                'REQUEST_URI' => '/contribution/print/' . $this->contrib->id
+            ]
+        );
+        $uri = \Slim\Http\Uri::createFromEnvironment($environment);
+        $headers = \Slim\Http\Headers::createFromEnvironment($environment);
+        $cookies = [];
+        $serverParams = $environment->all();
+
+        $body = new \Slim\Http\RequestBody();
+        $request = new \Slim\Http\Request('POST', $uri, $headers, $cookies, $serverParams, $body);
+        $request = $request->withHeader('Content-Type', 'application/json');
+        $response = new \Slim\Http\Response();
+        $controller = new \Galette\Controllers\PdfController($this->container);
+
+        //test not logged
+        $test_response = $controller->contribution($request, $response, $this->contrib->id);
+        $this->array($test_response->getHeaders())->isIdenticalTo(['Location' => ['contributions']]);
+        $this->integer($test_response->getStatusCode())->isIdenticalTo(301);
+        $this->boolean($test_response->isServerError())->isFalse();
+        $this->array($this->flash_data['slimFlash'])->isIdenticalTo([
+            'error_detected' => [
+                'Unable to load contribution #' . $this->contrib->id . '!'
+            ]
+        ]);
+        $this->flash_data = [];
+
+        //test superadmin
+        $this->logSuperAdmin();
+        $body = new \Slim\Http\RequestBody();
+        $request = new \Slim\Http\Request('POST', $uri, $headers, $cookies, $serverParams, $body);
+        $request = $request->withHeader('Content-Type', 'application/json');
+        $test_response = null;
+        $this
+            ->output(
+                function () use ($controller, $request, $response, &$test_response) {
+                    $test_response = $controller->contribution($request, $response, $this->contrib->id);
+                }
+            )->isNotEmpty();
+
+        $this->integer($test_response->getStatusCode())->isIdenticalTo(200);
+        $this->boolean($test_response->isServerError())->isFalse();
+        $this->string($test_response->getHeader('Content-type')[0])->isIdenticalTo('application/pdf');
+        $this->string($test_response->getHeader('Content-Disposition')[0])->isIdenticalTo('attachment;filename="contribution_' . $this->contrib->id . '_invoice.pdf"');
+    }
 }
index 92d3aef57e81af5d826ff110421e3a994ab2fc9e..052e752d45ca96466807e494eb9f17b86d27996f 100644 (file)
@@ -747,6 +747,18 @@ abstract class GaletteTestCase extends atoum
         }
     }
 
+    /**
+     * Initialize default PDF models in database
+     *
+     * @return void
+     */
+    protected function initModels(): void
+    {
+        $models = new \Galette\Repository\PdfModels($this->zdb, $this->preferences, $this->login);
+        $res = $models->installInit(false);
+        $this->boolean($res)->isTrue();
+    }
+
     /**
      * Clean history
      *