]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/PdfModel.php
Fix storage issue; refs #1393
[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 $login;
61 private $remove = [];
62 private $i18n;
63 private $container;
64 private $seed = 95842354;
65 private $history;
66
67 private $adh;
68 private $contrib;
69 private $members_fields;
70
71 /**
72 * Set up tests
73 *
74 * @param string $testMethod Calling method
75 *
76 * @return void
77 */
78 public function beforeTestMethod($testMethod)
79 {
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
84 );
85 $this->login = new \Galette\Core\Login($this->zdb, $this->i18n);
86
87 $models = new \Galette\Repository\PdfModels($this->zdb, $this->preferences, $this->login);
88 $res = $models->installInit(false);
89 $this->boolean($res)->isTrue();
90
91 $container = new class {
92 /**
93 * Get (only router)
94 *
95 * @param string $name Param name
96 *
97 * @return mixed
98 */
99 public function get($name)
100 {
101 $router = new class {
102 /**
103 * Get path ('')
104 *
105 * @param sttring $name Route name
106 *
107 * @return string
108 */
109 public function pathFor($name)
110 {
111 return '';
112 }
113 };
114 return $router;
115 }
116 };
117 $_SERVER['HTTP_HOST'] = '';
118 $this->container = $container;
119
120 $this->history = new \Galette\Core\History($this->zdb, $this->login, $this->preferences);
121
122 include_once GALETTE_ROOT . 'includes/fields_defs/members_fields.php';
123 $this->members_fields = $members_fields;
124
125 $this->adh = new \Galette\Entity\Adherent($this->zdb);
126 $this->adh->setDependencies(
127 $this->preferences,
128 $this->members_fields,
129 $this->history
130 );
131
132 global $container, $zdb;
133 $zdb = $this->zdb; //globals '(
134 $container = $this->container; //globals '(
135 }
136
137 /**
138 * Tear down tests
139 *
140 * @param string $testMethod Calling method
141 *
142 * @return void
143 */
144 public function afterTestMethod($testMethod)
145 {
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);
157 $delete->where([
158 'text_orig' => [
159 'Dynamic choice field',
160 'Dynamic date field',
161 'Dynamic text field'
162 ]
163 ]);
164 $this->zdb->execute($delete);
165
166 /*if ($this->contents_table !== null) {
167 $this->zdb->drop($this->contents_table);
168 }*/
169 }
170
171 /**
172 * Test expected patterns
173 *
174 * @return void
175 */
176 public function testExpectedPatterns()
177 {
178 $model = new class ($this->zdb, $this->preferences, 1) extends \Galette\Entity\PdfModel {
179 };
180
181 $main_expected = [
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}/'
189 ];
190 $this->array($model->patterns)->isIdenticalTo($main_expected);
191
192 $model = new \Galette\Entity\PdfMain($this->zdb, $this->preferences);
193 $this->array($model->patterns)->isIdenticalTo($main_expected);
194
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}/'
217 ];
218 $model = new \Galette\Entity\PdfAdhesionFormModel($this->zdb, $this->preferences);
219 $this->array($model->patterns)->isIdenticalTo($expected);
220
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}/'
242 ];
243 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
244 $this->array($model->patterns)->isIdenticalTo($expected);
245
246 $model = new \Galette\Entity\PdfReceipt($this->zdb, $this->preferences);
247 $this->array($model->patterns)->isIdenticalTo($expected);
248 }
249
250 /**
251 * Types provider
252 *
253 * @return array
254 */
255 protected function typesProvider(): array
256 {
257 return [
258 [
259 'type' => \Galette\Entity\PdfModel::MAIN_MODEL,
260 'expected' => 'Galette\Entity\PdfMain'
261 ], [
262 'type' => \Galette\Entity\PdfModel::INVOICE_MODEL,
263 'expected' => 'Galette\Entity\PdfInvoice'
264 ], [
265 'type' => \Galette\Entity\PdfModel::RECEIPT_MODEL,
266 'expected' => 'Galette\Entity\PdfReceipt'
267 ], [
268 'type' => \Galette\Entity\PdfModel::ADHESION_FORM_MODEL,
269 'expected' => 'Galette\Entity\PdfAdhesionFormModel'
270 ], [
271 'type' => 0,
272 'expected' => 'Galette\Entity\PdfMain'
273 ]
274 ];
275 }
276
277 /**
278 * Tets getTypeClass
279 * @dataProvider typesProvider
280 *
281 * @param integer $type Requested type
282 * @param string $expected Expected class name
283 *
284 * @return void
285 */
286 public function testGetypeClass($type, $expected)
287 {
288 $this->string(\Galette\Entity\PdfModel::getTypeClass($type))->isIdenticalTo($expected);
289 }
290
291 /**
292 * Test model replacements
293 *
294 * @return void
295 */
296 public function testReplacements()
297 {
298 //create dynamic fields
299 $field_data = [
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,
305 'field_repeat' => 1
306 ];
307
308 $adf = DynamicField::getFieldType($this->zdb, $field_data['field_type']);
309
310 $stored = $adf->store($field_data);
311 $error_detected = $adf->getErrors();
312 $warning_detected = $adf->getWarnings();
313 $this->boolean($stored)->isTrue(
314 implode(
315 ' ',
316 $adf->getErrors() + $adf->getWarnings()
317 )
318 );
319 $this->array($error_detected)->isEmpty(implode(' ', $adf->getErrors()));
320 $this->array($warning_detected)->isEmpty(implode(' ', $adf->getWarnings()));
321
322 $field_data = [
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,
329 'field_repeat' => 1
330 ];
331
332 $cdf = DynamicField::getFieldType($this->zdb, $field_data['field_type']);
333
334 $stored = $cdf->store($field_data);
335 $error_detected = $cdf->getErrors();
336 $warning_detected = $cdf->getWarnings();
337 $this->boolean($stored)->isTrue(
338 implode(
339 ' ',
340 $cdf->getErrors() + $cdf->getWarnings()
341 )
342 );
343 $this->array($error_detected)->isEmpty(implode(' ', $cdf->getErrors()));
344 $this->array($warning_detected)->isEmpty(implode(' ', $cdf->getWarnings()));
345
346 //prepare model
347 $rs = new \stdClass();
348 $pk = \Galette\Entity\PdfModel::PK;
349 $rs->$pk = 42;
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;
361
362 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences, $rs);
363
364 $this->string($model->hheader)->isIdenticalTo("<table>
365 <tr>
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>
368 </tr>
369 </table>");
370
371 $this->string($model->hfooter)->isIdenticalTo('<div id="pdf_footer">
372 Association Galette - Galette
373 Palais des Papes
374 Au milieu
375 84000 Avignon - France<br/>
376
377 </div>');
378
379 $data = [
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,
392 'sexe_adh' => 0,
393 'prof_adh' => 'Chef de fabrication',
394 'titre_adh' => null,
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,
402 'id_statut' => 9,
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 (:'
407 ];
408 $this->createMember($data);
409 $model->setMember($this->adh);
410
411 $this->createContribution($cdf);
412 $model->setContribution($this->contrib);
413
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" />'
419 );
420
421 $legend = $model->getLegend();
422 $this->array($legend)
423 ->hasSize(3)
424 ->hasKeys(['main', 'member', 'contribution']);
425
426 $this->array($legend['main']['patterns'])->hasSize(7);
427 $this->array($legend['member']['patterns'])
428 ->hasSize(23)
429 ->hasKeys(['LABEL_DYNFIELD_' . $adf->getId() . '_ADH', 'INPUT_DYNFIELD_' . $adf->getId() . '_ADH']);
430 $this->array($legend['contribution']['patterns'])
431 ->hasSize(12)
432 ->hasKeys(['LABEL_DYNFIELD_' . $cdf->getId() . '_CONTRIB', 'INPUT_DYNFIELD_' . $cdf->getId() . '_CONTRIB']);
433 }
434
435 /**
436 * Create member from data
437 *
438 * @param array $data Data to use to create member
439 *
440 * @return \Galette\Entity\Adherent
441 */
442 public function createMember(array $data)
443 {
444 $adh = $this->adh;
445 $check = $adh->check($data, [], []);
446 if (is_array($check)) {
447 var_dump($check);
448 }
449 $this->boolean($check)->isTrue();
450
451 $store = $adh->store();
452 $this->boolean($store)->isTrue();
453 }
454
455 /**
456 * Create test contribution in database
457 *
458 * @param DynamicField $cdf Contribution dynamic field
459 *
460 * @return void
461 */
462 private function createContribution($cdf)
463 {
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
467
468 $edate = clone $bdate;
469 $edate->add(new \DateInterval('P1Y'));
470
471 $dyndate = new \DateTime('2020-12-03 22:56:53');
472
473 $data = [
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')
483 ];
484 $this->createContrib($data);
485 }
486
487 /**
488 * Create contribution from data
489 *
490 * @param array $data Data to use to create contribution
491 *
492 * @return \Galette\Entity\Contribution
493 */
494 public function createContrib(array $data)
495 {
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)) {
500 var_dump($check);
501 }
502 $this->boolean($check)->isTrue();
503
504 $store = $contrib->store();
505 $this->boolean($store)->isTrue();
506 }
507 }