3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2020-2023 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-2023 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
;
40 use Galette\Entity\Adherent
;
41 use Galette\DynamicFields\DynamicField
;
42 use Galette\GaletteTestCase
;
49 * @package GaletteTests
50 * @author Johan Cwiklinski <johan@x-tnd.be>
51 * @copyright 2020-2023 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 GaletteTestCase
58 private array $remove = [];
59 protected int $seed = 95842354;
64 * @param string $method Calling method
68 public function beforeTestMethod($method)
70 parent
::beforeTestMethod($method);
72 $models = new \Galette\Repository\
PdfModels($this->zdb
, $this->preferences
, $this->login
);
73 $res = $models->installInit(false);
74 $this->boolean($res)->isTrue();
76 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
);
77 $this->adh
->setDependencies(
79 $this->members_fields
,
82 $this->contrib
= new \Galette\Entity\
Contribution($this->zdb
, $this->login
);
88 * @param string $method Calling method
92 public function afterTestMethod($method)
94 parent
::afterTestMethod($method);
96 $delete = $this->zdb
->delete(\Galette\Entity\Contribution
::TABLE
);
97 $delete->where(['info_cotis' => 'FAKER' . $this->seed
]);
98 $this->zdb
->execute($delete);
99 $delete = $this->zdb
->delete(\Galette\Entity\Adherent
::TABLE
);
100 $this->zdb
->execute($delete);
101 $delete = $this->zdb
->delete(\Galette\Entity\DynamicFieldsHandle
::TABLE
);
102 $this->zdb
->execute($delete);
103 $delete = $this->zdb
->delete(DynamicField
::TABLE
);
104 $this->zdb
->execute($delete);
105 //cleanup dynamic translations
106 $delete = $this->zdb
->delete(\Galette\Core\L10n
::TABLE
);
109 'Dynamic choice field',
110 'Dynamic date field',
114 $this->zdb
->execute($delete);
118 * Test expected patterns
122 public function testExpectedPatterns()
124 $model = new class ($this->zdb
, $this->preferences
, 1) extends \Galette\Entity\PdfModel
{
128 'asso_name' => '/{ASSO_NAME}/',
129 'asso_slogan' => '/{ASSO_SLOGAN}/',
130 'asso_address' => '/{ASSO_ADDRESS}/',
131 'asso_address_multi' => '/{ASSO_ADDRESS_MULTI}/',
132 'asso_website' => '/{ASSO_WEBSITE}/',
133 'asso_logo' => '/{ASSO_LOGO}/',
134 'date_now' => '/{DATE_NOW}/',
135 'login_uri' => '/{LOGIN_URI}/'
137 $this->array($model->getPatterns())->isIdenticalTo($main_expected);
139 $model = new \Galette\Entity\
PdfMain($this->zdb
, $this->preferences
);
140 $this->array($model->getPatterns())->isIdenticalTo($main_expected);
142 $expected = $main_expected +
[
143 'adh_title' => '/{TITLE_ADH}/',
144 'adh_id' => '/{ID_ADH}/',
145 'adh_num' => '/{NUM_ADH}/',
146 'adh_name' => '/{NAME_ADH}/',
147 'adh_last_name' => '/{LAST_NAME_ADH}/',
148 'adh_first_name' => '/{FIRST_NAME_ADH}/',
149 'adh_nickname' => '/{NICKNAME_ADH}/',
150 'adh_gender' => '/{GENDER_ADH}/',
151 'adh_birth_date' => '/{ADH_BIRTH_DATE}/',
152 'adh_birth_place' => '/{ADH_BIRTH_PLACE}/',
153 'adh_profession' => '/{PROFESSION_ADH}/',
154 'adh_company' => '/{COMPANY_ADH}/',
155 'adh_address' => '/{ADDRESS_ADH}/',
156 'adh_address_multi' => '/{ADDRESS_ADH_MULTI}/',
157 'adh_zip' => '/{ZIP_ADH}/',
158 'adh_town' => '/{TOWN_ADH}/',
159 'adh_country' => '/{COUNTRY_ADH}/',
160 'adh_phone' => '/{PHONE_ADH}/',
161 'adh_mobile' => '/{MOBILE_ADH}/',
162 'adh_email' => '/{EMAIL_ADH}/',
163 'adh_login' => '/{LOGIN_ADH}/',
164 'adh_main_group' => '/{GROUP_ADH}/',
165 'adh_groups' => '/{GROUPS_ADH}/',
166 'adh_dues' => '/{ADH_DUES}/',
167 'days_remaining' => '/{DAYS_REMAINING}/',
168 'days_expired' => '/{DAYS_EXPIRED}/',
169 '_adh_company' => '/{COMPANY_NAME_ADH}/',
170 '_adh_last_name' => '/{LASTNAME_ADH}/',
171 '_adh_first_name' => '/{FIRSTNAME_ADH}/',
172 '_adh_login' => '/{LOGIN}/',
173 '_adh_email' => '/{MAIL_ADH}/',
175 $model = new \Galette\Entity\
PdfAdhesionFormModel($this->zdb
, $this->preferences
);
176 $this->array($model->getPatterns())->isIdenticalTo($expected);
179 'contrib_label' => '/{CONTRIB_LABEL}/',
180 'contrib_amount' => '/{CONTRIB_AMOUNT}/',
181 'contrib_amount_letters' => '/{CONTRIB_AMOUNT_LETTERS}/',
182 'contrib_date' => '/{CONTRIB_DATE}/',
183 'contrib_year' => '/{CONTRIB_YEAR}/',
184 'contrib_comment' => '/{CONTRIB_COMMENT}/',
185 'contrib_bdate' => '/{CONTRIB_BEGIN_DATE}/',
186 'contrib_edate' => '/{CONTRIB_END_DATE}/',
187 'contrib_id' => '/{CONTRIB_ID}/',
188 'contrib_payment' => '/{CONTRIB_PAYMENT_TYPE}/',
189 'contrib_info' => '/{CONTRIB_INFO}/',
190 '_contrib_label' => '/{CONTRIBUTION_LABEL}/',
191 '_contrib_amount' => '/{CONTRIBUTION_AMOUNT}/',
192 '_contrib_amount_letters' => '/{CONTRIBUTION_AMOUNT_LETTERS}/',
193 '_contrib_date' => '/{CONTRIBUTION_DATE}/',
194 '_contrib_year' => '/{CONTRIBUTION_YEAR}/',
195 '_contrib_comment' => '/{CONTRIBUTION_COMMENT}/',
196 '_contrib_bdate' => '/{CONTRIBUTION_BEGIN_DATE}/',
197 '_contrib_edate' => '/{CONTRIBUTION_END_DATE}/',
198 '_contrib_id' => '/{CONTRIBUTION_ID}/',
199 '_contrib_payment' => '/{CONTRIBUTION_PAYMENT_TYPE}/',
200 '_contrib_info' => '/{CONTRIBUTION_INFO}/',
201 '__contrib_label' => '/{CONTRIB_TYPE}/',
202 'deadline' => '/{DEADLINE}/'
204 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
);
205 $this->array($model->getPatterns())->isIdenticalTo($expected);
207 $model = new \Galette\Entity\
PdfReceipt($this->zdb
, $this->preferences
);
208 $this->array($model->getPatterns())->isIdenticalTo($expected);
216 protected function typesProvider(): array
220 'type' => \Galette\Entity\PdfModel
::MAIN_MODEL
,
221 'expected' => 'Galette\Entity\PdfMain'
223 'type' => \Galette\Entity\PdfModel
::INVOICE_MODEL
,
224 'expected' => 'Galette\Entity\PdfInvoice'
226 'type' => \Galette\Entity\PdfModel
::RECEIPT_MODEL
,
227 'expected' => 'Galette\Entity\PdfReceipt'
229 'type' => \Galette\Entity\PdfModel
::ADHESION_FORM_MODEL
,
230 'expected' => 'Galette\Entity\PdfAdhesionFormModel'
233 'expected' => 'Galette\Entity\PdfMain'
240 * @dataProvider typesProvider
242 * @param integer $type Requested type
243 * @param string $expected Expected class name
247 public function testGetypeClass($type, $expected)
249 $this->string(\Galette\Entity\PdfModel
::getTypeClass($type))->isIdenticalTo($expected);
253 * Test model replacements
257 public function testReplacements()
259 //create dynamic fields
261 'form_name' => 'adh',
262 'field_name' => 'Dynamic text field',
263 'field_perm' => DynamicField
::PERM_USER_WRITE
,
264 'field_type' => DynamicField
::TEXT
,
265 'field_required' => 1,
269 $adf = DynamicField
::getFieldType($this->zdb
, $field_data['field_type']);
271 $stored = $adf->store($field_data);
272 $error_detected = $adf->getErrors();
273 $warning_detected = $adf->getWarnings();
274 $this->boolean($stored)->isTrue(
277 $adf->getErrors() +
$adf->getWarnings()
280 $this->array($error_detected)->isEmpty(implode(' ', $adf->getErrors()));
281 $this->array($warning_detected)->isEmpty(implode(' ', $adf->getWarnings()));
284 'form_name' => 'contrib',
285 'field_form' => 'contrib',
286 'field_name' => 'Dynamic date field',
287 'field_perm' => DynamicField
::PERM_USER_WRITE
,
288 'field_type' => DynamicField
::DATE
,
289 'field_required' => 1,
293 $cdf = DynamicField
::getFieldType($this->zdb
, $field_data['field_type']);
295 $stored = $cdf->store($field_data);
296 $error_detected = $cdf->getErrors();
297 $warning_detected = $cdf->getWarnings();
298 $this->boolean($stored)->isTrue(
301 $cdf->getErrors() +
$cdf->getWarnings()
304 $this->array($error_detected)->isEmpty(implode(' ', $cdf->getErrors()));
305 $this->array($warning_detected)->isEmpty(implode(' ', $cdf->getWarnings()));
308 $pk = \Galette\Entity\PdfModel
::PK
;
309 $rs = new \
ArrayObject([
311 'model_name' => 'Test model',
312 'model_title' => 'A simple tmodel for tests',
313 'model_subtitle' => 'The subtitle',
314 'model_header' => null,
315 'model_footer' => null,
316 'model_body' => 'name: {NAME_ADH} login: {LOGIN_ADH} birthdate: {ADH_BIRTH_DATE} dynlabel: {LABEL_DYNFIELD_' .
317 $adf->getId() . '_ADH} dynvalue: {INPUT_DYNFIELD_' . $adf->getId() . '_ADH} ' .
318 '- enddate: {CONTRIB_END_DATE} amount: {CONTRIB_AMOUNT} ({CONTRIB_AMOUNT_LETTERS}) dynlabel: ' .
319 '{LABEL_DYNFIELD_' . $cdf->getId() . '_CONTRIB} dynvalue: {INPUT_DYNFIELD_' . $cdf->getId() . '_CONTRIB}',
320 'model_styles' => null,
321 'model_parent' => \Galette\Entity\PdfModel
::MAIN_MODEL
322 ], \ArrayObject
::ARRAY_AS_PROPS
);
323 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
, $rs);
326 'nom_adh' => 'Durand',
327 'prenom_adh' => 'René',
328 'ville_adh' => 'Martel',
329 'cp_adh' => '39 069',
330 'adresse_adh' => '66, boulevard De Oliveira',
331 'email_adh' => 'meunier.josephine@ledoux.com',
332 'login_adh' => 'arthur.hamon',
333 'mdp_adh' => 'J^B-()f',
334 'mdp_adh2' => 'J^B-()f',
335 'bool_admin_adh' => false,
336 'bool_exempt_adh' => false,
337 'bool_display_info' => true,
339 'prof_adh' => 'Chef de fabrication',
341 'ddn_adh' => '1937-12-26',
342 'lieu_naissance' => 'Gonzalez-sur-Meunier',
343 'pseudo_adh' => 'ubertrand',
344 'pays_adh' => 'Antarctique',
345 'tel_adh' => '0439153432',
346 'activite_adh' => true,
348 'date_crea_adh' => '2020-06-10',
349 'pref_lang' => 'en_US',
350 'fingerprint' => 'FAKER' . $this->seed
,
351 'info_field_' . $adf->getId() . '_1' => 'My value (:'
353 $this->createMember($data);
354 $model->setMember($this->adh
);
356 $this->createPdfContribution($cdf);
357 $model->setContribution($this->contrib
);
359 $this->string($model->hheader
)->isIdenticalTo("<table>
361 <td id=\"pdf_assoname\"><strong id=\"asso_name\">Galette</strong><br/></td>
362 <td id=\"pdf_logo\"><img src=\"http:///logo\" width=\"129\" height=\"60\"/></td>
366 $this->string($model->hfooter
)->isIdenticalTo('<div id="pdf_footer">
367 Association Galette - Galette
370 84000 Avignon - France<br/>
374 $this->string($model->hbody
)->isEqualTo(
375 'name: DURAND René login: arthur.hamon birthdate: 1937-12-26 dynlabel: Dynamic text field dynvalue: ' .
377 '- enddate: ' . $this->contrib
->end_date
. ' amount: 92 (ninety-two) dynlabel: Dynamic date field ' .
378 'dynvalue: 2020-12-03'
381 $legend = $model->getLegend();
382 $this->array($legend)
384 ->hasKeys(['main', 'member', 'contribution']);
386 $this->array($legend['main']['patterns'])->hasSize(8);
387 $this->array($legend['member']['patterns'])
389 ->hasKeys(['label_dynfield_' . $adf->getId() . '_adh', 'dynfield_' . $adf->getId() . '_adh']);
390 $this->array($legend['contribution']['patterns'])
392 ->hasKeys(['label_dynfield_' . $cdf->getId() . '_contrib', 'dynfield_' . $cdf->getId() . '_contrib']);
396 * Create member from data
398 * @param array $data Data to use to create member
402 public function createMember(array $data)
405 $check = $adh->check($data, [], []);
406 if (is_array($check)) {
409 $this->boolean($check)->isTrue();
411 $store = $adh->store();
412 $this->boolean($store)->isTrue();
417 * Create test contribution in database
419 * @param DynamicField $cdf Contribution dynamic field
423 protected function createPdfContribution($cdf)
425 $bdate = new \
DateTime(); // 2020-11-07
426 $bdate->sub(new \
DateInterval('P5M')); // 2020-06-07
427 $bdate->add(new \
DateInterval('P3D')); // 2020-06-10
429 $edate = clone $bdate;
430 $edate->add(new \
DateInterval('P1Y'));
432 $dyndate = new \
DateTime('2020-12-03 22:56:53');
435 'id_adh' => $this->adh
->id
,
436 'id_type_cotis' => 1,
437 'montant_cotis' => 92,
438 'type_paiement_cotis' => 3,
439 'info_cotis' => 'FAKER' . $this->seed
,
440 'date_enreg' => $bdate->format('Y-m-d'),
441 'date_debut_cotis' => $bdate->format('Y-m-d'),
442 'date_fin_cotis' => $edate->format('Y-m-d'),
443 'info_field_' . $cdf->getId() . '_1' => $dyndate->format('Y-m-d')
445 $this->createContrib($data);
449 * Test model storage in db
453 public function testStorage()
455 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
);
457 $orig_title = $model->title
;
458 $this->string($orig_title)->isIdenticalTo('_T("Invoice") {CONTRIBUTION_YEAR}-{CONTRIBUTION_ID}');
460 $model->title
= 'Another test';
461 $this->boolean($model->store())->isTrue();
463 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
);
464 $this->string($model->title
)->isIdenticalTo('Another test');