*
* PHP version 5
*
- * Copyright © 2020 The Galette Team
+ * Copyright © 2020-2021 The Galette Team
*
* This file is part of Galette (http://galette.tuxfamily.org).
*
* @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
namespace Galette\Controllers\test\units;
use atoum;
+use Galette\GaletteTestCase;
/**
* PDF controller tests
* @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
*/
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(
$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();
}
/**
$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"');
+ }
}