]> 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 ];
199 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
200 $this->array($model->getPatterns())->isIdenticalTo($expected);
201
202 $model = new \Galette\Entity\PdfReceipt($this->zdb, $this->preferences);
203 $this->array($model->getPatterns())->isIdenticalTo($expected);
204 }
205
206 /**
207 * Types provider
208 *
209 * @return array
210 */
211 protected function typesProvider(): array
212 {
213 return [
214 [
215 'type' => \Galette\Entity\PdfModel::MAIN_MODEL,
216 'expected' => 'Galette\Entity\PdfMain'
217 ], [
218 'type' => \Galette\Entity\PdfModel::INVOICE_MODEL,
219 'expected' => 'Galette\Entity\PdfInvoice'
220 ], [
221 'type' => \Galette\Entity\PdfModel::RECEIPT_MODEL,
222 'expected' => 'Galette\Entity\PdfReceipt'
223 ], [
224 'type' => \Galette\Entity\PdfModel::ADHESION_FORM_MODEL,
225 'expected' => 'Galette\Entity\PdfAdhesionFormModel'
226 ], [
227 'type' => 0,
228 'expected' => 'Galette\Entity\PdfMain'
229 ]
230 ];
231 }
232
233 /**
234 * Tets getTypeClass
235 * @dataProvider typesProvider
236 *
237 * @param integer $type Requested type
238 * @param string $expected Expected class name
239 *
240 * @return void
241 */
242 public function testGetypeClass($type, $expected)
243 {
244 $this->string(\Galette\Entity\PdfModel::getTypeClass($type))->isIdenticalTo($expected);
245 }
246
247 /**
248 * Test model replacements
249 *
250 * @return void
251 */
252 public function testReplacements()
253 {
254 //create dynamic fields
255 $field_data = [
256 'form_name' => 'adh',
257 'field_name' => 'Dynamic text field',
258 'field_perm' => DynamicField::PERM_USER_WRITE,
259 'field_type' => DynamicField::TEXT,
260 'field_required' => 1,
261 'field_repeat' => 1
262 ];
263
264 $adf = DynamicField::getFieldType($this->zdb, $field_data['field_type']);
265
266 $stored = $adf->store($field_data);
267 $error_detected = $adf->getErrors();
268 $warning_detected = $adf->getWarnings();
269 $this->boolean($stored)->isTrue(
270 implode(
271 ' ',
272 $adf->getErrors() + $adf->getWarnings()
273 )
274 );
275 $this->array($error_detected)->isEmpty(implode(' ', $adf->getErrors()));
276 $this->array($warning_detected)->isEmpty(implode(' ', $adf->getWarnings()));
277
278 $field_data = [
279 'form_name' => 'contrib',
280 'field_form' => 'contrib',
281 'field_name' => 'Dynamic date field',
282 'field_perm' => DynamicField::PERM_USER_WRITE,
283 'field_type' => DynamicField::DATE,
284 'field_required' => 1,
285 'field_repeat' => 1
286 ];
287
288 $cdf = DynamicField::getFieldType($this->zdb, $field_data['field_type']);
289
290 $stored = $cdf->store($field_data);
291 $error_detected = $cdf->getErrors();
292 $warning_detected = $cdf->getWarnings();
293 $this->boolean($stored)->isTrue(
294 implode(
295 ' ',
296 $cdf->getErrors() + $cdf->getWarnings()
297 )
298 );
299 $this->array($error_detected)->isEmpty(implode(' ', $cdf->getErrors()));
300 $this->array($warning_detected)->isEmpty(implode(' ', $cdf->getWarnings()));
301
302 //prepare model
303 $rs = new \stdClass();
304 $pk = \Galette\Entity\PdfModel::PK;
305 $rs->$pk = 42;
306 $rs->model_name = 'Test model';
307 $rs->model_title = 'A simple tmodel for tests';
308 $rs->model_subtitle = 'The subtitle';
309 $rs->model_header = null;
310 $rs->model_footer = null;
311 $rs->model_body = 'name: {NAME_ADH} login: {LOGIN_ADH} birthdate: {ADH_BIRTH_DATE} dynlabel: {LABEL_DYNFIELD_' .
312 $adf->getId() . '_ADH} dynvalue: {INPUT_DYNFIELD_' . $adf->getId() . '_ADH} ' .
313 '- enddate: {CONTRIB_END_DATE} amount: {CONTRIB_AMOUNT} ({CONTRIB_AMOUNT_LETTERS}) dynlabel: ' .
314 '{LABEL_DYNFIELD_' . $cdf->getId() . '_CONTRIB} dynvalue: {INPUT_DYNFIELD_' . $cdf->getId() . '_CONTRIB}';
315 $rs->model_styles = null;
316 $rs->model_parent = \Galette\Entity\PdfModel::MAIN_MODEL;
317
318 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences, $rs);
319
320 $data = [
321 'nom_adh' => 'Durand',
322 'prenom_adh' => 'René',
323 'ville_adh' => 'Martel',
324 'cp_adh' => '39 069',
325 'adresse_adh' => '66, boulevard De Oliveira',
326 'email_adh' => 'meunier.josephine@ledoux.com',
327 'login_adh' => 'arthur.hamon',
328 'mdp_adh' => 'J^B-()f',
329 'mdp_adh2' => 'J^B-()f',
330 'bool_admin_adh' => false,
331 'bool_exempt_adh' => false,
332 'bool_display_info' => true,
333 'sexe_adh' => 0,
334 'prof_adh' => 'Chef de fabrication',
335 'titre_adh' => null,
336 'ddn_adh' => '1937-12-26',
337 'lieu_naissance' => 'Gonzalez-sur-Meunier',
338 'pseudo_adh' => 'ubertrand',
339 'pays_adh' => 'Antarctique',
340 'tel_adh' => '0439153432',
341 'url_adh' => 'http://bouchet.com/',
342 'activite_adh' => true,
343 'id_statut' => 9,
344 'date_crea_adh' => '2020-06-10',
345 'pref_lang' => 'en_US',
346 'fingerprint' => 'FAKER' . $this->seed,
347 'info_field_' . $adf->getId() . '_1' => 'My value (:'
348 ];
349 $this->createMember($data);
350 $model->setMember($this->adh);
351
352 $this->createContribution($cdf);
353 $model->setContribution($this->contrib);
354
355 $this->string($model->hheader)->isIdenticalTo("<table>
356 <tr>
357 <td id=\"pdf_assoname\"><strong id=\"asso_name\">Galette</strong><br/></td>
358 <td id=\"pdf_logo\"><img src=\"http://logo\" width=\"129\" height=\"60\"/></td>
359 </tr>
360 </table>");
361
362 $this->string($model->hfooter)->isIdenticalTo('<div id="pdf_footer">
363 Association Galette - Galette
364 Palais des Papes
365 Au milieu
366 84000 Avignon - France<br/>
367
368 </div>');
369
370 $this->string($model->hbody)->isEqualTo(
371 'name: DURAND René login: arthur.hamon birthdate: 1937-12-26 dynlabel: Dynamic text field dynvalue: ' .
372 '<textarea id="Dynamic text field" name="Dynamic text field" value="My value (:"/> ' .
373 '- enddate: ' . $this->contrib->end_date . ' amount: 92 (ninety-two) dynlabel: Dynamic date field ' .
374 'dynvalue: <input type="text" name="Dynamic date field" value="2020-12-03" size="10" />'
375 );
376
377 $legend = $model->getLegend();
378 $this->array($legend)
379 ->hasSize(3)
380 ->hasKeys(['main', 'member', 'contribution']);
381
382 $this->array($legend['main']['patterns'])->hasSize(8);
383 $this->array($legend['member']['patterns'])
384 ->hasSize(26)
385 ->hasKeys(['label_dynfield_' . $adf->getId() . '_adh', 'input_dynfield_' . $adf->getId() . '_adh']);
386 $this->array($legend['contribution']['patterns'])
387 ->hasSize(13)
388 ->hasKeys(['label_dynfield_' . $cdf->getId() . '_contrib', 'input_dynfield_' . $cdf->getId() . '_contrib']);
389 }
390
391 /**
392 * Create member from data
393 *
394 * @param array $data Data to use to create member
395 *
396 * @return Adherent
397 */
398 public function createMember(array $data)
399 {
400 $adh = $this->adh;
401 $check = $adh->check($data, [], []);
402 if (is_array($check)) {
403 var_dump($check);
404 }
405 $this->boolean($check)->isTrue();
406
407 $store = $adh->store();
408 $this->boolean($store)->isTrue();
409 return $adh;
410 }
411
412 /**
413 * Create test contribution in database
414 *
415 * @param DynamicField $cdf Contribution dynamic field
416 *
417 * @return void
418 */
419 private function createContribution($cdf)
420 {
421 $bdate = new \DateTime(); // 2020-11-07
422 $bdate->sub(new \DateInterval('P5M')); // 2020-06-07
423 $bdate->add(new \DateInterval('P3D')); // 2020-06-10
424
425 $edate = clone $bdate;
426 $edate->add(new \DateInterval('P1Y'));
427
428 $dyndate = new \DateTime('2020-12-03 22:56:53');
429
430 $data = [
431 'id_adh' => $this->adh->id,
432 'id_type_cotis' => 1,
433 'montant_cotis' => 92,
434 'type_paiement_cotis' => 3,
435 'info_cotis' => 'FAKER' . $this->seed,
436 'date_enreg' => $bdate->format('Y-m-d'),
437 'date_debut_cotis' => $bdate->format('Y-m-d'),
438 'date_fin_cotis' => $edate->format('Y-m-d'),
439 'info_field_' . $cdf->getId() . '_1' => $dyndate->format('Y-m-d')
440 ];
441 $this->createContrib($data);
442 }
443
444 /**
445 * Test model storage in db
446 *
447 * @return void
448 */
449 public function testStorage()
450 {
451 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
452
453 $orig_title = $model->title;
454 $this->string($orig_title)->isIdenticalTo('_T("Invoice") {CONTRIBUTION_YEAR}-{CONTRIBUTION_ID}');
455
456 $model->title = 'Another test';
457 $this->boolean($model->store())->isTrue();
458
459 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
460 $this->string($model->title)->isIdenticalTo('Another test');
461 }
462 }