]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/PdfModel.php
phpstan, level 2
[galette.git] / tests / Galette / Entity / tests / units / PdfModel.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * PDF model tests
7 *
8 * PHP version 5
9 *
10 * Copyright © 2020-2023 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
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.
18 *
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.
23 *
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/>.
26 *
27 * @category Entity
28 * @package GaletteTests
29 *
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
33 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since 2020-11-21
36 */
37
38 namespace Galette\Entity\test\units;
39
40 use Galette\Entity\Adherent;
41 use Galette\DynamicFields\DynamicField;
42 use Galette\GaletteTestCase;
43
44 /**
45 * PDF model tests
46 *
47 * @category Entity
48 * @name PdfModel
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
54 * @since 2020-11-21
55 */
56 class PdfModel extends GaletteTestCase
57 {
58 private array $remove = [];
59 protected int $seed = 95842354;
60
61 /**
62 * Set up tests
63 *
64 * @param string $method Calling method
65 *
66 * @return void
67 */
68 public function beforeTestMethod($method)
69 {
70 parent::beforeTestMethod($method);
71
72 $models = new \Galette\Repository\PdfModels($this->zdb, $this->preferences, $this->login);
73 $res = $models->installInit(false);
74 $this->boolean($res)->isTrue();
75
76 $this->adh = new \Galette\Entity\Adherent($this->zdb);
77 $this->adh->setDependencies(
78 $this->preferences,
79 $this->members_fields,
80 $this->history
81 );
82 $this->contrib = new \Galette\Entity\Contribution($this->zdb, $this->login);
83 }
84
85 /**
86 * Tear down tests
87 *
88 * @param string $method Calling method
89 *
90 * @return void
91 */
92 public function afterTestMethod($method)
93 {
94 parent::afterTestMethod($method);
95
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);
107 $delete->where([
108 'text_orig' => [
109 'Dynamic choice field',
110 'Dynamic date field',
111 'Dynamic text field'
112 ]
113 ]);
114 $this->zdb->execute($delete);
115 }
116
117 /**
118 * Test expected patterns
119 *
120 * @return void
121 */
122 public function testExpectedPatterns()
123 {
124 $model = new class ($this->zdb, $this->preferences, 1) extends \Galette\Entity\PdfModel {
125 };
126
127 $main_expected = [
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}/'
136 ];
137 $this->array($model->getPatterns())->isIdenticalTo($main_expected);
138
139 $model = new \Galette\Entity\PdfMain($this->zdb, $this->preferences);
140 $this->array($model->getPatterns())->isIdenticalTo($main_expected);
141
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}/',
174 ];
175 $model = new \Galette\Entity\PdfAdhesionFormModel($this->zdb, $this->preferences);
176 $this->array($model->getPatterns())->isIdenticalTo($expected);
177
178 $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}/'
203 ];
204 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
205 $this->array($model->getPatterns())->isIdenticalTo($expected);
206
207 $model = new \Galette\Entity\PdfReceipt($this->zdb, $this->preferences);
208 $this->array($model->getPatterns())->isIdenticalTo($expected);
209 }
210
211 /**
212 * Types provider
213 *
214 * @return array
215 */
216 protected function typesProvider(): array
217 {
218 return [
219 [
220 'type' => \Galette\Entity\PdfModel::MAIN_MODEL,
221 'expected' => 'Galette\Entity\PdfMain'
222 ], [
223 'type' => \Galette\Entity\PdfModel::INVOICE_MODEL,
224 'expected' => 'Galette\Entity\PdfInvoice'
225 ], [
226 'type' => \Galette\Entity\PdfModel::RECEIPT_MODEL,
227 'expected' => 'Galette\Entity\PdfReceipt'
228 ], [
229 'type' => \Galette\Entity\PdfModel::ADHESION_FORM_MODEL,
230 'expected' => 'Galette\Entity\PdfAdhesionFormModel'
231 ], [
232 'type' => 0,
233 'expected' => 'Galette\Entity\PdfMain'
234 ]
235 ];
236 }
237
238 /**
239 * Tets getTypeClass
240 * @dataProvider typesProvider
241 *
242 * @param integer $type Requested type
243 * @param string $expected Expected class name
244 *
245 * @return void
246 */
247 public function testGetypeClass($type, $expected)
248 {
249 $this->string(\Galette\Entity\PdfModel::getTypeClass($type))->isIdenticalTo($expected);
250 }
251
252 /**
253 * Test model replacements
254 *
255 * @return void
256 */
257 public function testReplacements()
258 {
259 //create dynamic fields
260 $field_data = [
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,
266 'field_repeat' => 1
267 ];
268
269 $adf = DynamicField::getFieldType($this->zdb, $field_data['field_type']);
270
271 $stored = $adf->store($field_data);
272 $error_detected = $adf->getErrors();
273 $warning_detected = $adf->getWarnings();
274 $this->boolean($stored)->isTrue(
275 implode(
276 ' ',
277 $adf->getErrors() + $adf->getWarnings()
278 )
279 );
280 $this->array($error_detected)->isEmpty(implode(' ', $adf->getErrors()));
281 $this->array($warning_detected)->isEmpty(implode(' ', $adf->getWarnings()));
282
283 $field_data = [
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,
290 'field_repeat' => 1
291 ];
292
293 $cdf = DynamicField::getFieldType($this->zdb, $field_data['field_type']);
294
295 $stored = $cdf->store($field_data);
296 $error_detected = $cdf->getErrors();
297 $warning_detected = $cdf->getWarnings();
298 $this->boolean($stored)->isTrue(
299 implode(
300 ' ',
301 $cdf->getErrors() + $cdf->getWarnings()
302 )
303 );
304 $this->array($error_detected)->isEmpty(implode(' ', $cdf->getErrors()));
305 $this->array($warning_detected)->isEmpty(implode(' ', $cdf->getWarnings()));
306
307 //prepare model
308 $pk = \Galette\Entity\PdfModel::PK;
309 $rs = new \ArrayObject([
310 $pk => 42,
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);
324
325 $data = [
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,
338 'sexe_adh' => 0,
339 'prof_adh' => 'Chef de fabrication',
340 'titre_adh' => null,
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,
347 'id_statut' => 9,
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 (:'
352 ];
353 $this->createMember($data);
354 $model->setMember($this->adh);
355
356 $this->createPdfContribution($cdf);
357 $model->setContribution($this->contrib);
358
359 $this->string($model->hheader)->isIdenticalTo("<table>
360 <tr>
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>
363 </tr>
364 </table>");
365
366 $this->string($model->hfooter)->isIdenticalTo('<div id="pdf_footer">
367 Association Galette - Galette
368 Palais des Papes
369 Au milieu
370 84000 Avignon - France<br/>
371
372 </div>');
373
374 $this->string($model->hbody)->isEqualTo(
375 'name: DURAND René login: arthur.hamon birthdate: 1937-12-26 dynlabel: Dynamic text field dynvalue: ' .
376 'My value (: ' .
377 '- enddate: ' . $this->contrib->end_date . ' amount: 92 (ninety-two) dynlabel: Dynamic date field ' .
378 'dynvalue: 2020-12-03'
379 );
380
381 $legend = $model->getLegend();
382 $this->array($legend)
383 ->hasSize(3)
384 ->hasKeys(['main', 'member', 'contribution']);
385
386 $this->array($legend['main']['patterns'])->hasSize(8);
387 $this->array($legend['member']['patterns'])
388 ->hasSize(28)
389 ->hasKeys(['label_dynfield_' . $adf->getId() . '_adh', 'dynfield_' . $adf->getId() . '_adh']);
390 $this->array($legend['contribution']['patterns'])
391 ->hasSize(14)
392 ->hasKeys(['label_dynfield_' . $cdf->getId() . '_contrib', 'dynfield_' . $cdf->getId() . '_contrib']);
393 }
394
395 /**
396 * Create member from data
397 *
398 * @param array $data Data to use to create member
399 *
400 * @return Adherent
401 */
402 public function createMember(array $data)
403 {
404 $adh = $this->adh;
405 $check = $adh->check($data, [], []);
406 if (is_array($check)) {
407 var_dump($check);
408 }
409 $this->boolean($check)->isTrue();
410
411 $store = $adh->store();
412 $this->boolean($store)->isTrue();
413 return $adh;
414 }
415
416 /**
417 * Create test contribution in database
418 *
419 * @param DynamicField $cdf Contribution dynamic field
420 *
421 * @return void
422 */
423 protected function createPdfContribution($cdf)
424 {
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
428
429 $edate = clone $bdate;
430 $edate->add(new \DateInterval('P1Y'));
431
432 $dyndate = new \DateTime('2020-12-03 22:56:53');
433
434 $data = [
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')
444 ];
445 $this->createContrib($data);
446 }
447
448 /**
449 * Test model storage in db
450 *
451 * @return void
452 */
453 public function testStorage()
454 {
455 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
456
457 $orig_title = $model->title;
458 $this->string($orig_title)->isIdenticalTo('_T("Invoice") {CONTRIBUTION_YEAR}-{CONTRIBUTION_ID}');
459
460 $model->title = 'Another test';
461 $this->boolean($model->store())->isTrue();
462
463 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
464 $this->string($model->title)->isIdenticalTo('Another test');
465 }
466 }