]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/PdfModel.php
42d03d1ff0516620f5fb89e88a4047328ac299a9
[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 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 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 $remove = [];
59 protected $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_name' => '/{NAME_ADH}/',
146 'adh_last_name' => '/{LAST_NAME_ADH}/',
147 'adh_first_name' => '/{FIRST_NAME_ADH}/',
148 'adh_nickname' => '/{NICKNAME_ADH}/',
149 'adh_gender' => '/{GENDER_ADH}/',
150 'adh_birth_date' => '/{ADH_BIRTH_DATE}/',
151 'adh_birth_place' => '/{ADH_BIRTH_PLACE}/',
152 'adh_profession' => '/{PROFESSION_ADH}/',
153 'adh_company' => '/{COMPANY_ADH}/',
154 'adh_address' => '/{ADDRESS_ADH}/',
155 'adh_zip' => '/{ZIP_ADH}/',
156 'adh_town' => '/{TOWN_ADH}/',
157 'adh_country' => '/{COUNTRY_ADH}/',
158 'adh_phone' => '/{PHONE_ADH}/',
159 'adh_mobile' => '/{MOBILE_ADH}/',
160 'adh_email' => '/{EMAIL_ADH}/',
161 'adh_login' => '/{LOGIN_ADH}/',
162 'adh_main_group' => '/{GROUP_ADH}/',
163 'adh_groups' => '/{GROUPS_ADH}/',
164 'adh_dues' => '/{ADH_DUES}/',
165 'days_remaining' => '/{DAYS_REMAINING}/',
166 'days_expired' => '/{DAYS_EXPIRED}/',
167 '_adh_company' => '/{COMPANY_NAME_ADH}/',
168 '_adh_last_name' => '/{LASTNAME_ADH}/',
169 '_adh_first_name' => '/{FIRSTNAME_ADH}/',
170 '_adh_login' => '/{LOGIN}/',
171 '_adh_email' => '/{MAIL_ADH}/',
172 ];
173 $model = new \Galette\Entity\PdfAdhesionFormModel($this->zdb, $this->preferences);
174 $this->array($model->getPatterns())->isIdenticalTo($expected);
175
176 $expected += [
177 'contrib_label' => '/{CONTRIB_LABEL}/',
178 'contrib_amount' => '/{CONTRIB_AMOUNT}/',
179 'contrib_amount_letters' => '/{CONTRIB_AMOUNT_LETTERS}/',
180 'contrib_date' => '/{CONTRIB_DATE}/',
181 'contrib_year' => '/{CONTRIB_YEAR}/',
182 'contrib_comment' => '/{CONTRIB_COMMENT}/',
183 'contrib_bdate' => '/{CONTRIB_BEGIN_DATE}/',
184 'contrib_edate' => '/{CONTRIB_END_DATE}/',
185 'contrib_id' => '/{CONTRIB_ID}/',
186 'contrib_payment' => '/{CONTRIB_PAYMENT_TYPE}/',
187 'contrib_info' => '/{CONTRIB_INFO}/',
188 '_contrib_label' => '/{CONTRIBUTION_LABEL}/',
189 '_contrib_amount' => '/{CONTRIBUTION_AMOUNT}/',
190 '_contrib_amount_letters' => '/{CONTRIBUTION_AMOUNT_LETTERS}/',
191 '_contrib_date' => '/{CONTRIBUTION_DATE}/',
192 '_contrib_year' => '/{CONTRIBUTION_YEAR}/',
193 '_contrib_comment' => '/{CONTRIBUTION_COMMENT}/',
194 '_contrib_bdate' => '/{CONTRIBUTION_BEGIN_DATE}/',
195 '_contrib_edate' => '/{CONTRIBUTION_END_DATE}/',
196 '_contrib_id' => '/{CONTRIBUTION_ID}/',
197 '_contrib_payment' => '/{CONTRIBUTION_PAYMENT_TYPE}/',
198 '_contrib_info' => '/{CONTRIBUTION_INFO}/',
199 '__contrib_label' => '/{CONTRIB_TYPE}/',
200 'deadline' => '/{DEADLINE}/'
201 ];
202 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
203 $this->array($model->getPatterns())->isIdenticalTo($expected);
204
205 $model = new \Galette\Entity\PdfReceipt($this->zdb, $this->preferences);
206 $this->array($model->getPatterns())->isIdenticalTo($expected);
207 }
208
209 /**
210 * Types provider
211 *
212 * @return array
213 */
214 protected function typesProvider(): array
215 {
216 return [
217 [
218 'type' => \Galette\Entity\PdfModel::MAIN_MODEL,
219 'expected' => 'Galette\Entity\PdfMain'
220 ], [
221 'type' => \Galette\Entity\PdfModel::INVOICE_MODEL,
222 'expected' => 'Galette\Entity\PdfInvoice'
223 ], [
224 'type' => \Galette\Entity\PdfModel::RECEIPT_MODEL,
225 'expected' => 'Galette\Entity\PdfReceipt'
226 ], [
227 'type' => \Galette\Entity\PdfModel::ADHESION_FORM_MODEL,
228 'expected' => 'Galette\Entity\PdfAdhesionFormModel'
229 ], [
230 'type' => 0,
231 'expected' => 'Galette\Entity\PdfMain'
232 ]
233 ];
234 }
235
236 /**
237 * Tets getTypeClass
238 * @dataProvider typesProvider
239 *
240 * @param integer $type Requested type
241 * @param string $expected Expected class name
242 *
243 * @return void
244 */
245 public function testGetypeClass($type, $expected)
246 {
247 $this->string(\Galette\Entity\PdfModel::getTypeClass($type))->isIdenticalTo($expected);
248 }
249
250 /**
251 * Test model replacements
252 *
253 * @return void
254 */
255 public function testReplacements()
256 {
257 //create dynamic fields
258 $field_data = [
259 'form_name' => 'adh',
260 'field_name' => 'Dynamic text field',
261 'field_perm' => DynamicField::PERM_USER_WRITE,
262 'field_type' => DynamicField::TEXT,
263 'field_required' => 1,
264 'field_repeat' => 1
265 ];
266
267 $adf = DynamicField::getFieldType($this->zdb, $field_data['field_type']);
268
269 $stored = $adf->store($field_data);
270 $error_detected = $adf->getErrors();
271 $warning_detected = $adf->getWarnings();
272 $this->boolean($stored)->isTrue(
273 implode(
274 ' ',
275 $adf->getErrors() + $adf->getWarnings()
276 )
277 );
278 $this->array($error_detected)->isEmpty(implode(' ', $adf->getErrors()));
279 $this->array($warning_detected)->isEmpty(implode(' ', $adf->getWarnings()));
280
281 $field_data = [
282 'form_name' => 'contrib',
283 'field_form' => 'contrib',
284 'field_name' => 'Dynamic date field',
285 'field_perm' => DynamicField::PERM_USER_WRITE,
286 'field_type' => DynamicField::DATE,
287 'field_required' => 1,
288 'field_repeat' => 1
289 ];
290
291 $cdf = DynamicField::getFieldType($this->zdb, $field_data['field_type']);
292
293 $stored = $cdf->store($field_data);
294 $error_detected = $cdf->getErrors();
295 $warning_detected = $cdf->getWarnings();
296 $this->boolean($stored)->isTrue(
297 implode(
298 ' ',
299 $cdf->getErrors() + $cdf->getWarnings()
300 )
301 );
302 $this->array($error_detected)->isEmpty(implode(' ', $cdf->getErrors()));
303 $this->array($warning_detected)->isEmpty(implode(' ', $cdf->getWarnings()));
304
305 //prepare model
306 $rs = new \stdClass();
307 $pk = \Galette\Entity\PdfModel::PK;
308 $rs->$pk = 42;
309 $rs->model_name = 'Test model';
310 $rs->model_title = 'A simple tmodel for tests';
311 $rs->model_subtitle = 'The subtitle';
312 $rs->model_header = null;
313 $rs->model_footer = null;
314 $rs->model_body = 'name: {NAME_ADH} login: {LOGIN_ADH} birthdate: {ADH_BIRTH_DATE} dynlabel: {LABEL_DYNFIELD_' .
315 $adf->getId() . '_ADH} dynvalue: {INPUT_DYNFIELD_' . $adf->getId() . '_ADH} ' .
316 '- enddate: {CONTRIB_END_DATE} amount: {CONTRIB_AMOUNT} ({CONTRIB_AMOUNT_LETTERS}) dynlabel: ' .
317 '{LABEL_DYNFIELD_' . $cdf->getId() . '_CONTRIB} dynvalue: {INPUT_DYNFIELD_' . $cdf->getId() . '_CONTRIB}';
318 $rs->model_styles = null;
319 $rs->model_parent = \Galette\Entity\PdfModel::MAIN_MODEL;
320
321 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences, $rs);
322
323 $data = [
324 'nom_adh' => 'Durand',
325 'prenom_adh' => 'René',
326 'ville_adh' => 'Martel',
327 'cp_adh' => '39 069',
328 'adresse_adh' => '66, boulevard De Oliveira',
329 'email_adh' => 'meunier.josephine@ledoux.com',
330 'login_adh' => 'arthur.hamon',
331 'mdp_adh' => 'J^B-()f',
332 'mdp_adh2' => 'J^B-()f',
333 'bool_admin_adh' => false,
334 'bool_exempt_adh' => false,
335 'bool_display_info' => true,
336 'sexe_adh' => 0,
337 'prof_adh' => 'Chef de fabrication',
338 'titre_adh' => null,
339 'ddn_adh' => '1937-12-26',
340 'lieu_naissance' => 'Gonzalez-sur-Meunier',
341 'pseudo_adh' => 'ubertrand',
342 'pays_adh' => 'Antarctique',
343 'tel_adh' => '0439153432',
344 'url_adh' => 'http://bouchet.com/',
345 'activite_adh' => true,
346 'id_statut' => 9,
347 'date_crea_adh' => '2020-06-10',
348 'pref_lang' => 'en_US',
349 'fingerprint' => 'FAKER' . $this->seed,
350 'info_field_' . $adf->getId() . '_1' => 'My value (:'
351 ];
352 $this->createMember($data);
353 $model->setMember($this->adh);
354
355 $this->createPdfContribution($cdf);
356 $model->setContribution($this->contrib);
357
358 $this->string($model->hheader)->isIdenticalTo("<table>
359 <tr>
360 <td id=\"pdf_assoname\"><strong id=\"asso_name\">Galette</strong><br/></td>
361 <td id=\"pdf_logo\"><img src=\"http://logo\" width=\"129\" height=\"60\"/></td>
362 </tr>
363 </table>");
364
365 $this->string($model->hfooter)->isIdenticalTo('<div id="pdf_footer">
366 Association Galette - Galette
367 Palais des Papes
368 Au milieu
369 84000 Avignon - France<br/>
370
371 </div>');
372
373 $this->string($model->hbody)->isEqualTo(
374 'name: DURAND René login: arthur.hamon birthdate: 1937-12-26 dynlabel: Dynamic text field dynvalue: ' .
375 'My value (: ' .
376 '- enddate: ' . $this->contrib->end_date . ' amount: 92 (ninety-two) dynlabel: Dynamic date field ' .
377 'dynvalue: 2020-12-03'
378 );
379
380 $legend = $model->getLegend();
381 $this->array($legend)
382 ->hasSize(3)
383 ->hasKeys(['main', 'member', 'contribution']);
384
385 $this->array($legend['main']['patterns'])->hasSize(8);
386 $this->array($legend['member']['patterns'])
387 ->hasSize(26)
388 ->hasKeys(['label_dynfield_' . $adf->getId() . '_adh', 'dynfield_' . $adf->getId() . '_adh']);
389 $this->array($legend['contribution']['patterns'])
390 ->hasSize(14)
391 ->hasKeys(['label_dynfield_' . $cdf->getId() . '_contrib', 'dynfield_' . $cdf->getId() . '_contrib']);
392 }
393
394 /**
395 * Create member from data
396 *
397 * @param array $data Data to use to create member
398 *
399 * @return Adherent
400 */
401 public function createMember(array $data)
402 {
403 $adh = $this->adh;
404 $check = $adh->check($data, [], []);
405 if (is_array($check)) {
406 var_dump($check);
407 }
408 $this->boolean($check)->isTrue();
409
410 $store = $adh->store();
411 $this->boolean($store)->isTrue();
412 return $adh;
413 }
414
415 /**
416 * Create test contribution in database
417 *
418 * @param DynamicField $cdf Contribution dynamic field
419 *
420 * @return void
421 */
422 protected function createPdfContribution($cdf)
423 {
424 $bdate = new \DateTime(); // 2020-11-07
425 $bdate->sub(new \DateInterval('P5M')); // 2020-06-07
426 $bdate->add(new \DateInterval('P3D')); // 2020-06-10
427
428 $edate = clone $bdate;
429 $edate->add(new \DateInterval('P1Y'));
430
431 $dyndate = new \DateTime('2020-12-03 22:56:53');
432
433 $data = [
434 'id_adh' => $this->adh->id,
435 'id_type_cotis' => 1,
436 'montant_cotis' => 92,
437 'type_paiement_cotis' => 3,
438 'info_cotis' => 'FAKER' . $this->seed,
439 'date_enreg' => $bdate->format('Y-m-d'),
440 'date_debut_cotis' => $bdate->format('Y-m-d'),
441 'date_fin_cotis' => $edate->format('Y-m-d'),
442 'info_field_' . $cdf->getId() . '_1' => $dyndate->format('Y-m-d')
443 ];
444 $this->createContrib($data);
445 }
446
447 /**
448 * Test model storage in db
449 *
450 * @return void
451 */
452 public function testStorage()
453 {
454 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
455
456 $orig_title = $model->title;
457 $this->string($orig_title)->isIdenticalTo('_T("Invoice") {CONTRIBUTION_YEAR}-{CONTRIBUTION_ID}');
458
459 $model->title = 'Another test';
460 $this->boolean($model->store())->isTrue();
461
462 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
463 $this->string($model->title)->isIdenticalTo('Another test');
464 }
465 }