]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/PdfModel.php
Rework PDF models patterns and replacements
[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 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 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 atoum;
41 use Galette\Entity\Adherent;
42 use Galette\DynamicFields\DynamicField;
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 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 atoum
57 {
58 private $zdb;
59 private $preferences;
60 private $session;
61 private $login;
62 private $remove = [];
63 private $i18n;
64 private $container;
65 private $seed = 95842354;
66 private $history;
67
68 private $adh;
69 private $contrib;
70 private $members_fields;
71
72 /**
73 * Set up tests
74 *
75 * @param string $testMethod Calling method
76 *
77 * @return void
78 */
79 public function beforeTestMethod($testMethod)
80 {
81 $this->zdb = new \Galette\Core\Db();
82 $this->preferences = new \Galette\Core\Preferences($this->zdb);
83 $this->i18n = new \Galette\Core\I18n(
84 \Galette\Core\I18n::DEFAULT_LANG
85 );
86 $this->session = new \RKA\Session();
87 $this->login = new \Galette\Core\Login($this->zdb, $this->i18n, $this->session);
88
89 $models = new \Galette\Repository\PdfModels($this->zdb, $this->preferences, $this->login);
90 $res = $models->installInit(false);
91 $this->boolean($res)->isTrue();
92
93 $container = new class {
94 /**
95 * Get (only router)
96 *
97 * @param string $name Param name
98 *
99 * @return mixed
100 */
101 public function get($name)
102 {
103 $router = new class {
104 /**
105 * Get path ('')
106 *
107 * @param sttring $name Route name
108 *
109 * @return string
110 */
111 public function pathFor($name)
112 {
113 return '';
114 }
115 };
116 return $router;
117 }
118 };
119 $_SERVER['HTTP_HOST'] = '';
120 $this->container = $container;
121
122 $this->history = new \Galette\Core\History($this->zdb, $this->login, $this->preferences);
123
124 include_once GALETTE_ROOT . 'includes/fields_defs/members_fields.php';
125 $this->members_fields = $members_fields;
126
127 $this->adh = new \Galette\Entity\Adherent($this->zdb);
128 $this->adh->setDependencies(
129 $this->preferences,
130 $this->members_fields,
131 $this->history
132 );
133 }
134
135 /**
136 * Tear down tests
137 *
138 * @param string $testMethod Calling method
139 *
140 * @return void
141 */
142 public function afterTestMethod($testMethod)
143 {
144 $delete = $this->zdb->delete(\Galette\Entity\Contribution::TABLE);
145 $delete->where(['info_cotis' => 'FAKER' . $this->seed]);
146 $this->zdb->execute($delete);
147 $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE);
148 $this->zdb->execute($delete);
149 $delete = $this->zdb->delete(\Galette\Entity\DynamicFieldsHandle::TABLE);
150 $this->zdb->execute($delete);
151 $delete = $this->zdb->delete(DynamicField::TABLE);
152 $this->zdb->execute($delete);
153 //cleanup dynamic translations
154 $delete = $this->zdb->delete(\Galette\Core\L10n::TABLE);
155 $delete->where([
156 'text_orig' => [
157 'Dynamic choice field',
158 'Dynamic date field',
159 'Dynamic text field'
160 ]
161 ]);
162 $this->zdb->execute($delete);
163
164 /*if ($this->contents_table !== null) {
165 $this->zdb->drop($this->contents_table);
166 }*/
167 }
168
169 /**
170 * Test expected patterns
171 *
172 * @return void
173 */
174 public function testExpectedPatterns()
175 {
176 global $container, $zdb;
177 $zdb = $this->zdb; //globals '(
178 $container = $this->container; //globals '(
179 $model = new class ($this->zdb, $this->preferences, 1) extends \Galette\Entity\PdfModel {
180 };
181
182 $main_expected = [
183 'asso_name' => '/{ASSO_NAME}/',
184 'asso_slogan' => '/{ASSO_SLOGAN}/',
185 'asso_address' => '/{ASSO_ADDRESS}/',
186 'asso_address_multi' => '/{ASSO_ADDRESS_MULTI}/',
187 'asso_website' => '/{ASSO_WEBSITE}/',
188 'asso_logo' => '/{ASSO_LOGO}/',
189 'date_now' => '/{DATE_NOW}/'
190 ];
191 $this->array($model->patterns)->isIdenticalTo($main_expected);
192
193 $model = new \Galette\Entity\PdfMain($this->zdb, $this->preferences);
194 $this->array($model->patterns)->isIdenticalTo($main_expected);
195
196 $expected = $main_expected + [
197 'adh_title' => '/{TITLE_ADH}/',
198 'adh_id' => '/{ID_ADH}/',
199 'adh_name' => '/{NAME_ADH}/',
200 'adh_last_name' => '/{LAST_NAME_ADH}/',
201 'adh_first_name' => '/{FIRST_NAME_ADH}/',
202 'adh_nickname' => '/{NICKNAME_ADH}/',
203 'adh_gender' => '/{GENDER_ADH}/',
204 'adh_birth_date' => '/{ADH_BIRTH_DATE}/',
205 'adh_birth_place' => '/{ADH_BIRTH_PLACE}/',
206 'adh_profession' => '/{PROFESSION_ADH}/',
207 'adh_company' => '/{COMPANY_ADH}/',
208 'adh_address' => '/{ADDRESS_ADH}/',
209 'adh_zip' => '/{ZIP_ADH}/',
210 'adh_town' => '/{TOWN_ADH}/',
211 'adh_country' => '/{COUNTRY_ADH}/',
212 'adh_phone' => '/{PHONE_ADH}/',
213 'adh_mobile' => '/{MOBILE_ADH}/',
214 'adh_email' => '/{EMAIL_ADH}/',
215 'adh_login' => '/{LOGIN_ADH}/',
216 'adh_main_group' => '/{GROUP_ADH}/',
217 'adh_groups' => '/{GROUPS_ADH}/'
218 ];
219 $model = new \Galette\Entity\PdfAdhesionFormModel($this->zdb, $this->preferences);
220 $this->array($model->patterns)->isIdenticalTo($expected);
221
222 $expected = $expected + [
223 'contrib_label' => '/{CONTRIB_LABEL}/',
224 'contrib_amount' => '/{CONTRIB_AMOUNT}/',
225 'contrib_amount_letters' => '/{CONTRIB_AMOUNT_LETTERS}/',
226 'contrib_date' => '/{CONTRIB_DATE}/',
227 'contrib_year' => '/{CONTRIB_YEAR}/',
228 'contrib_comment' => '/{CONTRIB_COMMENT}/',
229 'contrib_bdate' => '/{CONTRIB_BEGIN_DATE}/',
230 'contrib_edate' => '/{CONTRIB_END_DATE}/',
231 'contrib_id' => '/{CONTRIB_ID}/',
232 'contrib_payment' => '/{CONTRIB_PAYMENT_TYPE}/',
233 '_contrib_label' => '/{CONTRIBUTION_LABEL}/',
234 '_contrib_amount' => '/{CONTRIBUTION_AMOUNT}/',
235 '_contrib_amount_letters' => '/{CONTRIBUTION_AMOUNT_LETTERS}/',
236 '_contrib_date' => '/{CONTRIBUTION_DATE}/',
237 '_contrib_year' => '/{CONTRIBUTION_YEAR}/',
238 '_contrib_comment' => '/{CONTRIBUTION_COMMENT}/',
239 '_contrib_bdate' => '/{CONTRIBUTION_BEGIN_DATE}/',
240 '_contrib_edate' => '/{CONTRIBUTION_END_DATE}/',
241 '_contrib_id' => '/{CONTRIBUTION_ID}/',
242 '_contrib_payment' => '/{CONTRIBUTION_PAYMENT_TYPE}/'
243 ];
244 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
245 $this->array($model->patterns)->isIdenticalTo($expected);
246
247 $model = new \Galette\Entity\PdfReceipt($this->zdb, $this->preferences);
248 $this->array($model->patterns)->isIdenticalTo($expected);
249 }
250
251 /**
252 * Types provider
253 *
254 * @return array
255 */
256 protected function typesProvider(): array
257 {
258 return [
259 [
260 'type' => \Galette\Entity\PdfModel::MAIN_MODEL,
261 'expected' => 'Galette\Entity\PdfMain'
262 ], [
263 'type' => \Galette\Entity\PdfModel::INVOICE_MODEL,
264 'expected' => 'Galette\Entity\PdfInvoice'
265 ], [
266 'type' => \Galette\Entity\PdfModel::RECEIPT_MODEL,
267 'expected' => 'Galette\Entity\PdfReceipt'
268 ], [
269 'type' => \Galette\Entity\PdfModel::ADHESION_FORM_MODEL,
270 'expected' => 'Galette\Entity\PdfAdhesionFormModel'
271 ], [
272 'type' => 0,
273 'expected' => 'Galette\Entity\PdfMain'
274 ]
275 ];
276 }
277
278 /**
279 * Tets getTypeClass
280 * @dataProvider typesProvider
281 *
282 * @param integer $type Requested type
283 * @param string $expected Expected class name
284 *
285 * @return void
286 */
287 public function testGetypeClass($type, $expected)
288 {
289 $this->string(\Galette\Entity\PdfModel::getTypeClass($type))->isIdenticalTo($expected);
290 }
291
292 /**
293 * Test model replacements
294 *
295 * @return void
296 */
297 public function testReplacements()
298 {
299 global $container, $zdb;
300 $zdb = $this->zdb; //globals '(
301 $container = $this->container; //globals '(
302
303 //create dynamic fields
304 $field_data = [
305 'form_name' => 'adh',
306 'field_name' => 'Dynamic text field',
307 'field_perm' => DynamicField::PERM_USER_WRITE,
308 'field_type' => DynamicField::TEXT,
309 'field_required' => 1,
310 'field_repeat' => 1
311 ];
312
313 $adf = DynamicField::getFieldType($this->zdb, $field_data['field_type']);
314
315 $stored = $adf->store($field_data);
316 $error_detected = $adf->getErrors();
317 $warning_detected = $adf->getWarnings();
318 $this->boolean($stored)->isTrue(
319 implode(
320 ' ',
321 $adf->getErrors() + $adf->getWarnings()
322 )
323 );
324 $this->array($error_detected)->isEmpty(implode(' ', $adf->getErrors()));
325 $this->array($warning_detected)->isEmpty(implode(' ', $adf->getWarnings()));
326
327 $field_data = [
328 'form_name' => 'contrib',
329 'field_form' => 'contrib',
330 'field_name' => 'Dynamic date field',
331 'field_perm' => DynamicField::PERM_USER_WRITE,
332 'field_type' => DynamicField::DATE,
333 'field_required' => 1,
334 'field_repeat' => 1
335 ];
336
337 $cdf = DynamicField::getFieldType($this->zdb, $field_data['field_type']);
338
339 $stored = $cdf->store($field_data);
340 $error_detected = $cdf->getErrors();
341 $warning_detected = $cdf->getWarnings();
342 $this->boolean($stored)->isTrue(
343 implode(
344 ' ',
345 $cdf->getErrors() + $cdf->getWarnings()
346 )
347 );
348 $this->array($error_detected)->isEmpty(implode(' ', $cdf->getErrors()));
349 $this->array($warning_detected)->isEmpty(implode(' ', $cdf->getWarnings()));
350
351 //prepare model
352 $rs = new \stdClass();
353 $pk = \Galette\Entity\PdfModel::PK;
354 $rs->$pk = 42;
355 $rs->model_name = 'Test model';
356 $rs->model_title = 'A simple tmodel for tests';
357 $rs->model_subtitle = 'The subtitle';
358 $rs->model_header = null;
359 $rs->model_footer = null;
360 $rs->model_body = 'name: {NAME_ADH} login: {LOGIN_ADH} birthdate: {ADH_BIRTH_DATE} dynlabel: {LABEL_DYNFIELD_' .
361 $adf->getId() . '_ADH} dynvalue: {INPUT_DYNFIELD_' . $adf->getId() . '_ADH} ' .
362 '- enddate: {CONTRIB_END_DATE} amount: {CONTRIB_AMOUNT} ({CONTRIB_AMOUNT_LETTERS}) dynlabel: ' .
363 '{LABEL_DYNFIELD_' . $cdf->getId() . '_CONTRIB} dynvalue: {INPUT_DYNFIELD_' . $cdf->getId() . '_CONTRIB}';
364 $rs->model_styles = null;
365 $rs->model_parent = \Galette\Entity\PdfModel::MAIN_MODEL;
366
367 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences, $rs);
368
369 $this->string($model->hheader)->isIdenticalTo("<table>
370 <tr>
371 <td id=\"pdf_assoname\"><strong id=\"asso_name\">Galette</strong><br/></td>
372 <td id=\"pdf_logo\"><img src=\"http://\" width=\"129\" height=\"60\"/></td>
373 </tr>
374 </table>");
375
376 $this->string($model->hfooter)->isIdenticalTo('<div id="pdf_footer">
377 Association Galette - Galette
378 Palais des Papes
379 Au milieu
380 84000 Avignon - France<br/>
381
382 </div>');
383
384 $data = [
385 'nom_adh' => 'Durand',
386 'prenom_adh' => 'René',
387 'ville_adh' => 'Martel',
388 'cp_adh' => '39 069',
389 'adresse_adh' => '66, boulevard De Oliveira',
390 'email_adh' => 'meunier.josephine@ledoux.com',
391 'login_adh' => 'arthur.hamon',
392 'mdp_adh' => 'J^B-()f',
393 'mdp_adh2' => 'J^B-()f',
394 'bool_admin_adh' => false,
395 'bool_exempt_adh' => false,
396 'bool_display_info' => true,
397 'sexe_adh' => 0,
398 'prof_adh' => 'Chef de fabrication',
399 'titre_adh' => null,
400 'ddn_adh' => '1937-12-26',
401 'lieu_naissance' => 'Gonzalez-sur-Meunier',
402 'pseudo_adh' => 'ubertrand',
403 'pays_adh' => 'Antarctique',
404 'tel_adh' => '0439153432',
405 'url_adh' => 'http://bouchet.com/',
406 'activite_adh' => true,
407 'id_statut' => 9,
408 'date_crea_adh' => '2020-06-10',
409 'pref_lang' => 'en_US',
410 'fingerprint' => 'FAKER' . $this->seed,
411 'info_field_' . $adf->getId() . '_1' => 'My value (:'
412 ];
413 $this->createMember($data);
414 $model->setMember($this->adh);
415
416 $this->createContribution($cdf);
417 $model->setContribution($this->contrib);
418
419 $this->string($model->hbody)->isEqualTo(
420 'name: DURAND René login: arthur.hamon birthdate: 1937-12-26 dynlabel: Dynamic text field dynvalue: ' .
421 '<textarea id="Dynamic text field" name="Dynamic text field" value="My value (:"/> ' .
422 '- enddate: ' . $this->contrib->end_date . ' amount: 92 (ninety-two) dynlabel: Dynamic date field ' .
423 'dynvalue: <input type="text" name="Dynamic date field" value="2020-12-03" size="10" />'
424 );
425 }
426
427 /**
428 * Create member from data
429 *
430 * @param array $data Data to use to create member
431 *
432 * @return \Galette\Entity\Adherent
433 */
434 public function createMember(array $data)
435 {
436 $adh = $this->adh;
437 $check = $adh->check($data, [], []);
438 if (is_array($check)) {
439 var_dump($check);
440 }
441 $this->boolean($check)->isTrue();
442
443 $store = $adh->store();
444 $this->boolean($store)->isTrue();
445 }
446
447 /**
448 * Create test contribution in database
449 *
450 * @param DynamicField $cdf Contribution dynamic field
451 *
452 * @return void
453 */
454 private function createContribution($cdf)
455 {
456 $bdate = new \DateTime(); // 2020-11-07
457 $bdate->sub(new \DateInterval('P5M')); // 2020-06-07
458 $bdate->add(new \DateInterval('P3D')); // 2020-06-10
459
460 $edate = clone $bdate;
461 $edate->add(new \DateInterval('P1Y'));
462
463 $dyndate = new \DateTime('2020-12-03 22:56:53');
464
465 $data = [
466 'id_adh' => $this->adh->id,
467 'id_type_cotis' => 1,
468 'montant_cotis' => 92,
469 'type_paiement_cotis' => 3,
470 'info_cotis' => 'FAKER' . $this->seed,
471 'date_enreg' => $bdate->format('Y-m-d'),
472 'date_debut_cotis' => $bdate->format('Y-m-d'),
473 'date_fin_cotis' => $edate->format('Y-m-d'),
474 'info_field_' . $cdf->getId() . '_1' => $dyndate->format('Y-m-d')
475 ];
476 $this->createContrib($data);
477 }
478
479 /**
480 * Create contribution from data
481 *
482 * @param array $data Data to use to create contribution
483 *
484 * @return \Galette\Entity\Contribution
485 */
486 public function createContrib(array $data)
487 {
488 $this->contrib = new \Galette\Entity\Contribution($this->zdb, $this->login);
489 $contrib = $this->contrib;
490 $check = $contrib->check($data, [], []);
491 if (is_array($check)) {
492 var_dump($check);
493 }
494 $this->boolean($check)->isTrue();
495
496 $store = $contrib->store();
497 $this->boolean($store)->isTrue();
498 }
499 }