]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/PdfModel.php
Add pre_footer in replacements; closes #1808
[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-2023 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-2023 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 * @link http://galette.tuxfamily.org
34 * @since 2020-11-21
35 */
36
37 namespace Galette\Entity\test\units;
38
39 use Galette\Entity\Adherent;
40 use Galette\DynamicFields\DynamicField;
41 use Galette\GaletteTestCase;
42
43 /**
44 * PDF model tests
45 *
46 * @category Entity
47 * @name PdfModel
48 * @package GaletteTests
49 * @author Johan Cwiklinski <johan@x-tnd.be>
50 * @copyright 2020-2023 The Galette Team
51 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
52 * @link http://galette.tuxfamily.org
53 * @since 2020-11-21
54 */
55 class PdfModel extends GaletteTestCase
56 {
57 private array $remove = [];
58 protected int $seed = 95842354;
59
60 /**
61 * Set up tests
62 *
63 * @return void
64 */
65 public function setUp(): void
66 {
67 parent::setUp();
68
69 $models = new \Galette\Repository\PdfModels($this->zdb, $this->preferences, $this->login);
70 $res = $models->installInit(false);
71 $this->assertTrue($res);
72
73 $this->adh = new \Galette\Entity\Adherent($this->zdb);
74 $this->adh->setDependencies(
75 $this->preferences,
76 $this->members_fields,
77 $this->history
78 );
79 $this->contrib = new \Galette\Entity\Contribution($this->zdb, $this->login);
80 }
81
82 /**
83 * Tear down tests
84 *
85 * @return void
86 */
87 public function tearDown(): void
88 {
89 parent::tearDown();
90
91 $delete = $this->zdb->delete(\Galette\Entity\Contribution::TABLE);
92 $delete->where(['info_cotis' => 'FAKER' . $this->seed]);
93 $this->zdb->execute($delete);
94 $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE);
95 $this->zdb->execute($delete);
96 $delete = $this->zdb->delete(\Galette\Entity\DynamicFieldsHandle::TABLE);
97 $this->zdb->execute($delete);
98 $delete = $this->zdb->delete(DynamicField::TABLE);
99 $this->zdb->execute($delete);
100 //cleanup dynamic translations
101 $delete = $this->zdb->delete(\Galette\Core\L10n::TABLE);
102 $delete->where([
103 'text_orig' => [
104 'Dynamic choice field',
105 'Dynamic date field',
106 'Dynamic text field'
107 ]
108 ]);
109 $this->zdb->execute($delete);
110 }
111
112 /**
113 * Test expected patterns
114 *
115 * @return void
116 */
117 public function testExpectedPatterns()
118 {
119 $model = new class ($this->zdb, $this->preferences, 1) extends \Galette\Entity\PdfModel {
120 };
121
122 $main_expected = [
123 'asso_name' => '/{ASSO_NAME}/',
124 'asso_slogan' => '/{ASSO_SLOGAN}/',
125 'asso_address' => '/{ASSO_ADDRESS}/',
126 'asso_address_multi' => '/{ASSO_ADDRESS_MULTI}/',
127 'asso_website' => '/{ASSO_WEBSITE}/',
128 'asso_logo' => '/{ASSO_LOGO}/',
129 'date_now' => '/{DATE_NOW}/',
130 'login_uri' => '/{LOGIN_URI}/',
131 'asso_footer' => '/{ASSO_FOOTER}/',
132 ];
133 $this->assertSame($main_expected, $model->getPatterns());
134
135 $model = new \Galette\Entity\PdfMain($this->zdb, $this->preferences);
136 $this->assertSame($main_expected, $model->getPatterns());
137
138 $expected = $main_expected + [
139 'adh_title' => '/{TITLE_ADH}/',
140 'adh_id' => '/{ID_ADH}/',
141 'adh_num' => '/{NUM_ADH}/',
142 'adh_name' => '/{NAME_ADH}/',
143 'adh_last_name' => '/{LAST_NAME_ADH}/',
144 'adh_first_name' => '/{FIRST_NAME_ADH}/',
145 'adh_nickname' => '/{NICKNAME_ADH}/',
146 'adh_gender' => '/{GENDER_ADH}/',
147 'adh_birth_date' => '/{ADH_BIRTH_DATE}/',
148 'adh_birth_place' => '/{ADH_BIRTH_PLACE}/',
149 'adh_profession' => '/{PROFESSION_ADH}/',
150 'adh_company' => '/{COMPANY_ADH}/',
151 'adh_address' => '/{ADDRESS_ADH}/',
152 'adh_address_multi' => '/{ADDRESS_ADH_MULTI}/',
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->assertSame($expected, $model->getPatterns());
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->assertSame($expected, $model->getPatterns());
202
203 $model = new \Galette\Entity\PdfReceipt($this->zdb, $this->preferences);
204 $this->assertSame($expected, $model->getPatterns());
205 }
206
207 /**
208 * Types provider
209 *
210 * @return array
211 */
212 public static 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->assertSame($expected, \Galette\Entity\PdfModel::getTypeClass($type));
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->assertTrue(
271 $stored,
272 implode(
273 ' ',
274 $adf->getErrors() + $adf->getWarnings()
275 )
276 );
277 $this->assertEmpty($error_detected, implode(' ', $adf->getErrors()));
278 $this->assertEmpty($warning_detected, implode(' ', $adf->getWarnings()));
279
280 $field_data = [
281 'form_name' => 'contrib',
282 'field_form' => 'contrib',
283 'field_name' => 'Dynamic date field',
284 'field_perm' => DynamicField::PERM_USER_WRITE,
285 'field_type' => DynamicField::DATE,
286 'field_required' => 1,
287 'field_repeat' => 1
288 ];
289
290 $cdf = DynamicField::getFieldType($this->zdb, $field_data['field_type']);
291
292 $stored = $cdf->store($field_data);
293 $error_detected = $cdf->getErrors();
294 $warning_detected = $cdf->getWarnings();
295 $this->assertTrue(
296 $stored,
297 implode(
298 ' ',
299 $cdf->getErrors() + $cdf->getWarnings()
300 )
301 );
302 $this->assertEmpty($error_detected, implode(' ', $cdf->getErrors()));
303 $this->assertEmpty($warning_detected, implode(' ', $cdf->getWarnings()));
304
305 //prepare model
306 $pk = \Galette\Entity\PdfModel::PK;
307 $rs = new \ArrayObject([
308 $pk => 42,
309 'model_name' => 'Test model',
310 'model_title' => 'A simple tmodel for tests',
311 'model_subtitle' => 'The subtitle',
312 'model_header' => null,
313 'model_footer' => null,
314 '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 'model_styles' => null,
319 'model_parent' => \Galette\Entity\PdfModel::MAIN_MODEL
320 ], \ArrayObject::ARRAY_AS_PROPS);
321 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences, $rs);
322
323 $data = $this->dataAdherentOne() + [
324 'info_field_' . $adf->getId() . '_1' => 'My value (:'
325 ];
326 $this->createMember($data);
327 $model->setMember($this->adh);
328
329 $this->createPdfContribution($cdf);
330 $model->setContribution($this->contrib);
331
332 $this->assertStringContainsString(
333 '<td id="pdf_assoname"><strong id="asso_name">Galette</strong><br/></td>',
334 $model->hheader
335 );
336
337 $this->assertMatchesRegularExpression(
338 '/<td id="pdf_logo"><img src="@.+" width="129" height="60" alt="" \/><\/td>/',
339 $model->hheader
340 );
341
342 $this->assertSame(
343 '<div id="pdf_footer">
344 Association Galette - Galette
345 Palais des Papes
346 Au milieu
347 84000 Avignon - France<br/>
348
349 </div>',
350 $model->hfooter
351 );
352
353 $this->assertSame(
354 'name: DURAND René login: arthur.hamon' . $this->seed . ' birthdate: ' . $data['ddn_adh'] . ' dynlabel: Dynamic text field dynvalue: ' .
355 'My value (: ' .
356 '- enddate: ' . $this->contrib->end_date . ' amount: 92 (ninety-two) dynlabel: Dynamic date field ' .
357 'dynvalue: 2020-12-03',
358 $model->hbody
359 );
360
361 $legend = $model->getLegend();
362 $this->assertCount(3, $legend);
363 $this->assertArrayHasKey('main', $legend);
364 $this->assertArrayHasKey('member', $legend);
365 $this->assertArrayHasKey('contribution', $legend);
366
367 $this->assertCount(9, $legend['main']['patterns']);
368 $this->assertCount(28, $legend['member']['patterns']);
369 $this->assertTrue(isset($legend['member']['patterns']['label_dynfield_' . $adf->getId() . '_adh']));
370 $this->assertCount(14, $legend['contribution']['patterns']);
371 $this->assertTrue(isset($legend['contribution']['patterns']['label_dynfield_' . $cdf->getId() . '_contrib']));
372 }
373
374 /**
375 * Create test contribution in database
376 *
377 * @param DynamicField $cdf Contribution dynamic field
378 *
379 * @return void
380 */
381 protected function createPdfContribution($cdf)
382 {
383 $bdate = new \DateTime(); // 2020-11-07
384 $bdate->sub(new \DateInterval('P5M')); // 2020-06-07
385 $bdate->add(new \DateInterval('P3D')); // 2020-06-10
386
387 $edate = clone $bdate;
388 $edate->add(new \DateInterval('P1Y'));
389
390 $dyndate = new \DateTime('2020-12-03 22:56:53');
391
392 $data = [
393 'id_adh' => $this->adh->id,
394 'id_type_cotis' => 1,
395 'montant_cotis' => 92,
396 'type_paiement_cotis' => 3,
397 'info_cotis' => 'FAKER' . $this->seed,
398 'date_enreg' => $bdate->format('Y-m-d'),
399 'date_debut_cotis' => $bdate->format('Y-m-d'),
400 'date_fin_cotis' => $edate->format('Y-m-d'),
401 'info_field_' . $cdf->getId() . '_1' => $dyndate->format('Y-m-d')
402 ];
403 $this->createContrib($data);
404 }
405
406 /**
407 * Test model storage in db
408 *
409 * @return void
410 */
411 public function testStorage()
412 {
413 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
414
415 $orig_title = $model->title;
416 $this->assertSame('_T("Invoice") {CONTRIBUTION_YEAR}-{CONTRIBUTION_ID}', $orig_title);
417
418 $model->title = 'Another test';
419 $this->assertTrue($model->store());
420
421 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
422 $this->assertSame('Another test', $model->title);
423 }
424 }