3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2020 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
28 * @package GaletteTests
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2020 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
34 * @link http://galette.tuxfamily.org
38 namespace Galette\Entity\test\units
;
41 use Galette\Entity\Adherent
;
42 use Galette\DynamicFields\DynamicField
;
49 * @package GaletteTests
50 * @author Johan Cwiklinski <johan@x-tnd.be>
51 * @copyright 2020 The Galette Team
52 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
53 * @link http://galette.tuxfamily.org
56 class PdfModel
extends atoum
64 private $seed = 95842354;
69 private $members_fields;
74 * @param string $testMethod Calling method
78 public function beforeTestMethod($testMethod)
80 $this->zdb
= new \Galette\Core\
Db();
81 $this->preferences
= new \Galette\Core\
Preferences($this->zdb
);
82 $this->i18n
= new \Galette\Core\
I18n(
83 \Galette\Core\I18n
::DEFAULT_LANG
85 $this->login
= new \Galette\Core\
Login($this->zdb
, $this->i18n
);
87 $models = new \Galette\Repository\
PdfModels($this->zdb
, $this->preferences
, $this->login
);
88 $res = $models->installInit(false);
89 $this->boolean($res)->isTrue();
91 $container = new class {
95 * @param string $name Param name
99 public function get($name)
101 $router = new class {
105 * @param sttring $name Route name
109 public function pathFor($name)
117 $_SERVER['HTTP_HOST'] = '';
118 $this->container
= $container;
120 $this->history
= new \Galette\Core\
History($this->zdb
, $this->login
, $this->preferences
);
122 include_once GALETTE_ROOT
. 'includes/fields_defs/members_fields.php';
123 $this->members_fields
= $members_fields;
125 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
);
126 $this->adh
->setDependencies(
128 $this->members_fields
,
132 global $container, $zdb;
133 $zdb = $this->zdb
; //globals '(
134 $container = $this->container
; //globals '(
140 * @param string $testMethod Calling method
144 public function afterTestMethod($testMethod)
146 $delete = $this->zdb
->delete(\Galette\Entity\Contribution
::TABLE
);
147 $delete->where(['info_cotis' => 'FAKER' . $this->seed
]);
148 $this->zdb
->execute($delete);
149 $delete = $this->zdb
->delete(\Galette\Entity\Adherent
::TABLE
);
150 $this->zdb
->execute($delete);
151 $delete = $this->zdb
->delete(\Galette\Entity\DynamicFieldsHandle
::TABLE
);
152 $this->zdb
->execute($delete);
153 $delete = $this->zdb
->delete(DynamicField
::TABLE
);
154 $this->zdb
->execute($delete);
155 //cleanup dynamic translations
156 $delete = $this->zdb
->delete(\Galette\Core\L10n
::TABLE
);
159 'Dynamic choice field',
160 'Dynamic date field',
164 $this->zdb
->execute($delete);
166 /*if ($this->contents_table !== null) {
167 $this->zdb->drop($this->contents_table);
172 * Test expected patterns
176 public function testExpectedPatterns()
178 $model = new class ($this->zdb
, $this->preferences
, 1) extends \Galette\Entity\PdfModel
{
182 'asso_name' => '/{ASSO_NAME}/',
183 'asso_slogan' => '/{ASSO_SLOGAN}/',
184 'asso_address' => '/{ASSO_ADDRESS}/',
185 'asso_address_multi' => '/{ASSO_ADDRESS_MULTI}/',
186 'asso_website' => '/{ASSO_WEBSITE}/',
187 'asso_logo' => '/{ASSO_LOGO}/',
188 'date_now' => '/{DATE_NOW}/'
190 $this->array($model->patterns
)->isIdenticalTo($main_expected);
192 $model = new \Galette\Entity\
PdfMain($this->zdb
, $this->preferences
);
193 $this->array($model->patterns
)->isIdenticalTo($main_expected);
195 $expected = $main_expected +
[
196 'adh_title' => '/{TITLE_ADH}/',
197 'adh_id' => '/{ID_ADH}/',
198 'adh_name' => '/{NAME_ADH}/',
199 'adh_last_name' => '/{LAST_NAME_ADH}/',
200 'adh_first_name' => '/{FIRST_NAME_ADH}/',
201 'adh_nickname' => '/{NICKNAME_ADH}/',
202 'adh_gender' => '/{GENDER_ADH}/',
203 'adh_birth_date' => '/{ADH_BIRTH_DATE}/',
204 'adh_birth_place' => '/{ADH_BIRTH_PLACE}/',
205 'adh_profession' => '/{PROFESSION_ADH}/',
206 'adh_company' => '/{COMPANY_ADH}/',
207 'adh_address' => '/{ADDRESS_ADH}/',
208 'adh_zip' => '/{ZIP_ADH}/',
209 'adh_town' => '/{TOWN_ADH}/',
210 'adh_country' => '/{COUNTRY_ADH}/',
211 'adh_phone' => '/{PHONE_ADH}/',
212 'adh_mobile' => '/{MOBILE_ADH}/',
213 'adh_email' => '/{EMAIL_ADH}/',
214 'adh_login' => '/{LOGIN_ADH}/',
215 'adh_main_group' => '/{GROUP_ADH}/',
216 'adh_groups' => '/{GROUPS_ADH}/'
218 $model = new \Galette\Entity\
PdfAdhesionFormModel($this->zdb
, $this->preferences
);
219 $this->array($model->patterns
)->isIdenticalTo($expected);
221 $expected = $expected +
[
222 'contrib_label' => '/{CONTRIB_LABEL}/',
223 'contrib_amount' => '/{CONTRIB_AMOUNT}/',
224 'contrib_amount_letters' => '/{CONTRIB_AMOUNT_LETTERS}/',
225 'contrib_date' => '/{CONTRIB_DATE}/',
226 'contrib_year' => '/{CONTRIB_YEAR}/',
227 'contrib_comment' => '/{CONTRIB_COMMENT}/',
228 'contrib_bdate' => '/{CONTRIB_BEGIN_DATE}/',
229 'contrib_edate' => '/{CONTRIB_END_DATE}/',
230 'contrib_id' => '/{CONTRIB_ID}/',
231 'contrib_payment' => '/{CONTRIB_PAYMENT_TYPE}/',
232 '_contrib_label' => '/{CONTRIBUTION_LABEL}/',
233 '_contrib_amount' => '/{CONTRIBUTION_AMOUNT}/',
234 '_contrib_amount_letters' => '/{CONTRIBUTION_AMOUNT_LETTERS}/',
235 '_contrib_date' => '/{CONTRIBUTION_DATE}/',
236 '_contrib_year' => '/{CONTRIBUTION_YEAR}/',
237 '_contrib_comment' => '/{CONTRIBUTION_COMMENT}/',
238 '_contrib_bdate' => '/{CONTRIBUTION_BEGIN_DATE}/',
239 '_contrib_edate' => '/{CONTRIBUTION_END_DATE}/',
240 '_contrib_id' => '/{CONTRIBUTION_ID}/',
241 '_contrib_payment' => '/{CONTRIBUTION_PAYMENT_TYPE}/'
243 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
);
244 $this->array($model->patterns
)->isIdenticalTo($expected);
246 $model = new \Galette\Entity\
PdfReceipt($this->zdb
, $this->preferences
);
247 $this->array($model->patterns
)->isIdenticalTo($expected);
255 protected function typesProvider(): array
259 'type' => \Galette\Entity\PdfModel
::MAIN_MODEL
,
260 'expected' => 'Galette\Entity\PdfMain'
262 'type' => \Galette\Entity\PdfModel
::INVOICE_MODEL
,
263 'expected' => 'Galette\Entity\PdfInvoice'
265 'type' => \Galette\Entity\PdfModel
::RECEIPT_MODEL
,
266 'expected' => 'Galette\Entity\PdfReceipt'
268 'type' => \Galette\Entity\PdfModel
::ADHESION_FORM_MODEL
,
269 'expected' => 'Galette\Entity\PdfAdhesionFormModel'
272 'expected' => 'Galette\Entity\PdfMain'
279 * @dataProvider typesProvider
281 * @param integer $type Requested type
282 * @param string $expected Expected class name
286 public function testGetypeClass($type, $expected)
288 $this->string(\Galette\Entity\PdfModel
::getTypeClass($type))->isIdenticalTo($expected);
292 * Test model replacements
296 public function testReplacements()
298 //create dynamic fields
300 'form_name' => 'adh',
301 'field_name' => 'Dynamic text field',
302 'field_perm' => DynamicField
::PERM_USER_WRITE
,
303 'field_type' => DynamicField
::TEXT
,
304 'field_required' => 1,
308 $adf = DynamicField
::getFieldType($this->zdb
, $field_data['field_type']);
310 $stored = $adf->store($field_data);
311 $error_detected = $adf->getErrors();
312 $warning_detected = $adf->getWarnings();
313 $this->boolean($stored)->isTrue(
316 $adf->getErrors() +
$adf->getWarnings()
319 $this->array($error_detected)->isEmpty(implode(' ', $adf->getErrors()));
320 $this->array($warning_detected)->isEmpty(implode(' ', $adf->getWarnings()));
323 'form_name' => 'contrib',
324 'field_form' => 'contrib',
325 'field_name' => 'Dynamic date field',
326 'field_perm' => DynamicField
::PERM_USER_WRITE
,
327 'field_type' => DynamicField
::DATE
,
328 'field_required' => 1,
332 $cdf = DynamicField
::getFieldType($this->zdb
, $field_data['field_type']);
334 $stored = $cdf->store($field_data);
335 $error_detected = $cdf->getErrors();
336 $warning_detected = $cdf->getWarnings();
337 $this->boolean($stored)->isTrue(
340 $cdf->getErrors() +
$cdf->getWarnings()
343 $this->array($error_detected)->isEmpty(implode(' ', $cdf->getErrors()));
344 $this->array($warning_detected)->isEmpty(implode(' ', $cdf->getWarnings()));
347 $rs = new \
stdClass();
348 $pk = \Galette\Entity\PdfModel
::PK
;
350 $rs->model_name
= 'Test model';
351 $rs->model_title
= 'A simple tmodel for tests';
352 $rs->model_subtitle
= 'The subtitle';
353 $rs->model_header
= null;
354 $rs->model_footer
= null;
355 $rs->model_body
= 'name: {NAME_ADH} login: {LOGIN_ADH} birthdate: {ADH_BIRTH_DATE} dynlabel: {LABEL_DYNFIELD_' .
356 $adf->getId() . '_ADH} dynvalue: {INPUT_DYNFIELD_' . $adf->getId() . '_ADH} ' .
357 '- enddate: {CONTRIB_END_DATE} amount: {CONTRIB_AMOUNT} ({CONTRIB_AMOUNT_LETTERS}) dynlabel: ' .
358 '{LABEL_DYNFIELD_' . $cdf->getId() . '_CONTRIB} dynvalue: {INPUT_DYNFIELD_' . $cdf->getId() . '_CONTRIB}';
359 $rs->model_styles
= null;
360 $rs->model_parent
= \Galette\Entity\PdfModel
::MAIN_MODEL
;
362 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
, $rs);
364 $this->string($model->hheader
)->isIdenticalTo("<table>
366 <td id=\"pdf_assoname\"><strong id=\"asso_name\">Galette</strong><br/></td>
367 <td id=\"pdf_logo\"><img src=\"http://\" width=\"129\" height=\"60\"/></td>
371 $this->string($model->hfooter
)->isIdenticalTo('<div id="pdf_footer">
372 Association Galette - Galette
375 84000 Avignon - France<br/>
380 'nom_adh' => 'Durand',
381 'prenom_adh' => 'René',
382 'ville_adh' => 'Martel',
383 'cp_adh' => '39 069',
384 'adresse_adh' => '66, boulevard De Oliveira',
385 'email_adh' => 'meunier.josephine@ledoux.com',
386 'login_adh' => 'arthur.hamon',
387 'mdp_adh' => 'J^B-()f',
388 'mdp_adh2' => 'J^B-()f',
389 'bool_admin_adh' => false,
390 'bool_exempt_adh' => false,
391 'bool_display_info' => true,
393 'prof_adh' => 'Chef de fabrication',
395 'ddn_adh' => '1937-12-26',
396 'lieu_naissance' => 'Gonzalez-sur-Meunier',
397 'pseudo_adh' => 'ubertrand',
398 'pays_adh' => 'Antarctique',
399 'tel_adh' => '0439153432',
400 'url_adh' => 'http://bouchet.com/',
401 'activite_adh' => true,
403 'date_crea_adh' => '2020-06-10',
404 'pref_lang' => 'en_US',
405 'fingerprint' => 'FAKER' . $this->seed
,
406 'info_field_' . $adf->getId() . '_1' => 'My value (:'
408 $this->createMember($data);
409 $model->setMember($this->adh
);
411 $this->createContribution($cdf);
412 $model->setContribution($this->contrib
);
414 $this->string($model->hbody
)->isEqualTo(
415 'name: DURAND René login: arthur.hamon birthdate: 1937-12-26 dynlabel: Dynamic text field dynvalue: ' .
416 '<textarea id="Dynamic text field" name="Dynamic text field" value="My value (:"/> ' .
417 '- enddate: ' . $this->contrib
->end_date
. ' amount: 92 (ninety-two) dynlabel: Dynamic date field ' .
418 'dynvalue: <input type="text" name="Dynamic date field" value="2020-12-03" size="10" />'
421 $legend = $model->getLegend();
422 $this->array($legend)
424 ->hasKeys(['main', 'member', 'contribution']);
426 $this->array($legend['main']['patterns'])->hasSize(7);
427 $this->array($legend['member']['patterns'])
429 ->hasKeys(['LABEL_DYNFIELD_' . $adf->getId() . '_ADH', 'INPUT_DYNFIELD_' . $adf->getId() . '_ADH']);
430 $this->array($legend['contribution']['patterns'])
432 ->hasKeys(['LABEL_DYNFIELD_' . $cdf->getId() . '_CONTRIB', 'INPUT_DYNFIELD_' . $cdf->getId() . '_CONTRIB']);
436 * Create member from data
438 * @param array $data Data to use to create member
440 * @return \Galette\Entity\Adherent
442 public function createMember(array $data)
445 $check = $adh->check($data, [], []);
446 if (is_array($check)) {
449 $this->boolean($check)->isTrue();
451 $store = $adh->store();
452 $this->boolean($store)->isTrue();
456 * Create test contribution in database
458 * @param DynamicField $cdf Contribution dynamic field
462 private function createContribution($cdf)
464 $bdate = new \
DateTime(); // 2020-11-07
465 $bdate->sub(new \
DateInterval('P5M')); // 2020-06-07
466 $bdate->add(new \
DateInterval('P3D')); // 2020-06-10
468 $edate = clone $bdate;
469 $edate->add(new \
DateInterval('P1Y'));
471 $dyndate = new \
DateTime('2020-12-03 22:56:53');
474 'id_adh' => $this->adh
->id
,
475 'id_type_cotis' => 1,
476 'montant_cotis' => 92,
477 'type_paiement_cotis' => 3,
478 'info_cotis' => 'FAKER' . $this->seed
,
479 'date_enreg' => $bdate->format('Y-m-d'),
480 'date_debut_cotis' => $bdate->format('Y-m-d'),
481 'date_fin_cotis' => $edate->format('Y-m-d'),
482 'info_field_' . $cdf->getId() . '_1' => $dyndate->format('Y-m-d')
484 $this->createContrib($data);
488 * Create contribution from data
490 * @param array $data Data to use to create contribution
492 * @return \Galette\Entity\Contribution
494 public function createContrib(array $data)
496 $this->contrib
= new \Galette\Entity\
Contribution($this->zdb
, $this->login
);
497 $contrib = $this->contrib
;
498 $check = $contrib->check($data, [], []);
499 if (is_array($check)) {
502 $this->boolean($check)->isTrue();
504 $store = $contrib->store();
505 $this->boolean($store)->isTrue();