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