]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/PdfModel.php
aa2443988beb75dc32b0c8254bf1457f7e2c3a16
[galette.git] / tests / Galette / Entity / tests / units / PdfModel.php
1 <?php
2
3 /**
4 * Copyright © 2003-2024 The Galette Team
5 *
6 * This file is part of Galette (https://galette.eu).
7 *
8 * Galette is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * Galette is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 namespace Galette\Entity\test\units;
23
24 use Galette\Entity\Adherent;
25 use Galette\DynamicFields\DynamicField;
26 use Galette\GaletteTestCase;
27
28 /**
29 * PDF model tests
30 *
31 * @author Johan Cwiklinski <johan@x-tnd.be>
32 */
33 class PdfModel extends GaletteTestCase
34 {
35 private array $remove = [];
36 protected int $seed = 95842354;
37
38 /**
39 * Set up tests
40 *
41 * @return void
42 */
43 public function setUp(): void
44 {
45 parent::setUp();
46
47 $models = new \Galette\Repository\PdfModels($this->zdb, $this->preferences, $this->login);
48 $res = $models->installInit(false);
49 $this->assertTrue($res);
50
51 $this->adh = new \Galette\Entity\Adherent($this->zdb);
52 $this->adh->setDependencies(
53 $this->preferences,
54 $this->members_fields,
55 $this->history
56 );
57 $this->contrib = new \Galette\Entity\Contribution($this->zdb, $this->login);
58 }
59
60 /**
61 * Tear down tests
62 *
63 * @return void
64 */
65 public function tearDown(): void
66 {
67 parent::tearDown();
68
69 $delete = $this->zdb->delete(\Galette\Entity\Contribution::TABLE);
70 $delete->where(['info_cotis' => 'FAKER' . $this->seed]);
71 $this->zdb->execute($delete);
72 $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE);
73 $this->zdb->execute($delete);
74 $delete = $this->zdb->delete(\Galette\Entity\DynamicFieldsHandle::TABLE);
75 $this->zdb->execute($delete);
76 $delete = $this->zdb->delete(DynamicField::TABLE);
77 $this->zdb->execute($delete);
78 //cleanup dynamic translations
79 $delete = $this->zdb->delete(\Galette\Core\L10n::TABLE);
80 $delete->where([
81 'text_orig' => [
82 'Dynamic choice field',
83 'Dynamic date field',
84 'Dynamic text field'
85 ]
86 ]);
87 $this->zdb->execute($delete);
88 }
89
90 /**
91 * Test expected patterns
92 *
93 * @return void
94 */
95 public function testExpectedPatterns()
96 {
97 $model = new class ($this->zdb, $this->preferences, 1) extends \Galette\Entity\PdfModel {
98 };
99
100 $main_expected = [
101 'asso_name' => '/{ASSO_NAME}/',
102 'asso_slogan' => '/{ASSO_SLOGAN}/',
103 'asso_address' => '/{ASSO_ADDRESS}/',
104 'asso_address_multi' => '/{ASSO_ADDRESS_MULTI}/',
105 'asso_website' => '/{ASSO_WEBSITE}/',
106 'asso_logo' => '/{ASSO_LOGO}/',
107 'asso_print_logo' => '/{ASSO_PRINT_LOGO}/',
108 'date_now' => '/{DATE_NOW}/',
109 'login_uri' => '/{LOGIN_URI}/'
110 ];
111 $this->assertSame($main_expected, $model->getPatterns());
112
113 $model = new \Galette\Entity\PdfMain($this->zdb, $this->preferences);
114 $this->assertSame($main_expected, $model->getPatterns());
115
116 $expected = $main_expected + [
117 'adh_title' => '/{TITLE_ADH}/',
118 'adh_id' => '/{ID_ADH}/',
119 'adh_num' => '/{NUM_ADH}/',
120 'adh_name' => '/{NAME_ADH}/',
121 'adh_last_name' => '/{LAST_NAME_ADH}/',
122 'adh_first_name' => '/{FIRST_NAME_ADH}/',
123 'adh_nickname' => '/{NICKNAME_ADH}/',
124 'adh_gender' => '/{GENDER_ADH}/',
125 'adh_birth_date' => '/{ADH_BIRTH_DATE}/',
126 'adh_birth_place' => '/{ADH_BIRTH_PLACE}/',
127 'adh_profession' => '/{PROFESSION_ADH}/',
128 'adh_company' => '/{COMPANY_ADH}/',
129 'adh_address' => '/{ADDRESS_ADH}/',
130 'adh_address_multi' => '/{ADDRESS_ADH_MULTI}/',
131 'adh_zip' => '/{ZIP_ADH}/',
132 'adh_town' => '/{TOWN_ADH}/',
133 'adh_country' => '/{COUNTRY_ADH}/',
134 'adh_phone' => '/{PHONE_ADH}/',
135 'adh_mobile' => '/{MOBILE_ADH}/',
136 'adh_email' => '/{EMAIL_ADH}/',
137 'adh_login' => '/{LOGIN_ADH}/',
138 'adh_main_group' => '/{GROUP_ADH}/',
139 'adh_groups' => '/{GROUPS_ADH}/',
140 'adh_dues' => '/{ADH_DUES}/',
141 'days_remaining' => '/{DAYS_REMAINING}/',
142 'days_expired' => '/{DAYS_EXPIRED}/',
143 '_adh_company' => '/{COMPANY_NAME_ADH}/',
144 '_adh_last_name' => '/{LASTNAME_ADH}/',
145 '_adh_first_name' => '/{FIRSTNAME_ADH}/',
146 '_adh_login' => '/{LOGIN}/',
147 '_adh_email' => '/{MAIL_ADH}/',
148 ];
149 $model = new \Galette\Entity\PdfAdhesionFormModel($this->zdb, $this->preferences);
150 $this->assertSame($expected, $model->getPatterns());
151
152 $expected += [
153 'contrib_label' => '/{CONTRIB_LABEL}/',
154 'contrib_amount' => '/{CONTRIB_AMOUNT}/',
155 'contrib_amount_letters' => '/{CONTRIB_AMOUNT_LETTERS}/',
156 'contrib_date' => '/{CONTRIB_DATE}/',
157 'contrib_year' => '/{CONTRIB_YEAR}/',
158 'contrib_comment' => '/{CONTRIB_COMMENT}/',
159 'contrib_bdate' => '/{CONTRIB_BEGIN_DATE}/',
160 'contrib_edate' => '/{CONTRIB_END_DATE}/',
161 'contrib_id' => '/{CONTRIB_ID}/',
162 'contrib_payment' => '/{CONTRIB_PAYMENT_TYPE}/',
163 'contrib_info' => '/{CONTRIB_INFO}/',
164 '_contrib_label' => '/{CONTRIBUTION_LABEL}/',
165 '_contrib_amount' => '/{CONTRIBUTION_AMOUNT}/',
166 '_contrib_amount_letters' => '/{CONTRIBUTION_AMOUNT_LETTERS}/',
167 '_contrib_date' => '/{CONTRIBUTION_DATE}/',
168 '_contrib_year' => '/{CONTRIBUTION_YEAR}/',
169 '_contrib_comment' => '/{CONTRIBUTION_COMMENT}/',
170 '_contrib_bdate' => '/{CONTRIBUTION_BEGIN_DATE}/',
171 '_contrib_edate' => '/{CONTRIBUTION_END_DATE}/',
172 '_contrib_id' => '/{CONTRIBUTION_ID}/',
173 '_contrib_payment' => '/{CONTRIBUTION_PAYMENT_TYPE}/',
174 '_contrib_info' => '/{CONTRIBUTION_INFO}/',
175 '__contrib_label' => '/{CONTRIB_TYPE}/',
176 'deadline' => '/{DEADLINE}/'
177 ];
178 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
179 $this->assertSame($expected, $model->getPatterns());
180
181 $model = new \Galette\Entity\PdfReceipt($this->zdb, $this->preferences);
182 $this->assertSame($expected, $model->getPatterns());
183 }
184
185 /**
186 * Types provider
187 *
188 * @return array
189 */
190 public static function typesProvider(): array
191 {
192 return [
193 [
194 'type' => \Galette\Entity\PdfModel::MAIN_MODEL,
195 'expected' => 'Galette\Entity\PdfMain'
196 ], [
197 'type' => \Galette\Entity\PdfModel::INVOICE_MODEL,
198 'expected' => 'Galette\Entity\PdfInvoice'
199 ], [
200 'type' => \Galette\Entity\PdfModel::RECEIPT_MODEL,
201 'expected' => 'Galette\Entity\PdfReceipt'
202 ], [
203 'type' => \Galette\Entity\PdfModel::ADHESION_FORM_MODEL,
204 'expected' => 'Galette\Entity\PdfAdhesionFormModel'
205 ], [
206 'type' => 0,
207 'expected' => 'Galette\Entity\PdfMain'
208 ]
209 ];
210 }
211
212 /**
213 * Tets getTypeClass
214 * @dataProvider typesProvider
215 *
216 * @param integer $type Requested type
217 * @param string $expected Expected class name
218 *
219 * @return void
220 */
221 public function testGetypeClass($type, $expected)
222 {
223 $this->assertSame($expected, \Galette\Entity\PdfModel::getTypeClass($type));
224 }
225
226 /**
227 * Test model replacements
228 *
229 * @return void
230 */
231 public function testReplacements()
232 {
233 //create dynamic fields
234 $field_data = [
235 'form_name' => 'adh',
236 'field_name' => 'Dynamic text field',
237 'field_perm' => \Galette\Entity\FieldsConfig::USER_WRITE,
238 'field_type' => DynamicField::TEXT,
239 'field_required' => 1,
240 'field_repeat' => 1
241 ];
242
243 $adf = DynamicField::getFieldType($this->zdb, $field_data['field_type']);
244
245 $stored = $adf->store($field_data);
246 $error_detected = $adf->getErrors();
247 $warning_detected = $adf->getWarnings();
248 $this->assertTrue(
249 $stored,
250 implode(
251 ' ',
252 $adf->getErrors() + $adf->getWarnings()
253 )
254 );
255 $this->assertEmpty($error_detected, implode(' ', $adf->getErrors()));
256 $this->assertEmpty($warning_detected, implode(' ', $adf->getWarnings()));
257
258 $field_data = [
259 'form_name' => 'contrib',
260 'field_form' => 'contrib',
261 'field_name' => 'Dynamic date field',
262 'field_perm' => \Galette\Entity\FieldsConfig::USER_WRITE,
263 'field_type' => DynamicField::DATE,
264 'field_required' => 1,
265 'field_repeat' => 1
266 ];
267
268 $cdf = DynamicField::getFieldType($this->zdb, $field_data['field_type']);
269
270 $stored = $cdf->store($field_data);
271 $error_detected = $cdf->getErrors();
272 $warning_detected = $cdf->getWarnings();
273 $this->assertTrue(
274 $stored,
275 implode(
276 ' ',
277 $cdf->getErrors() + $cdf->getWarnings()
278 )
279 );
280 $this->assertEmpty($error_detected, implode(' ', $cdf->getErrors()));
281 $this->assertEmpty($warning_detected, implode(' ', $cdf->getWarnings()));
282
283 //prepare model
284 $pk = \Galette\Entity\PdfModel::PK;
285 $rs = new \ArrayObject([
286 $pk => 42,
287 'model_name' => 'Test model',
288 'model_title' => 'A simple tmodel for tests',
289 'model_subtitle' => 'The subtitle',
290 'model_header' => null,
291 'model_footer' => null,
292 'model_body' => 'name: {NAME_ADH} login: {LOGIN_ADH} birthdate: {ADH_BIRTH_DATE} dynlabel: {LABEL_DYNFIELD_' .
293 $adf->getId() . '_ADH} dynvalue: {INPUT_DYNFIELD_' . $adf->getId() . '_ADH} ' .
294 '- enddate: {CONTRIB_END_DATE} amount: {CONTRIB_AMOUNT} ({CONTRIB_AMOUNT_LETTERS}) dynlabel: ' .
295 '{LABEL_DYNFIELD_' . $cdf->getId() . '_CONTRIB} dynvalue: {INPUT_DYNFIELD_' . $cdf->getId() . '_CONTRIB}',
296 'model_styles' => null,
297 'model_parent' => \Galette\Entity\PdfModel::MAIN_MODEL
298 ], \ArrayObject::ARRAY_AS_PROPS);
299 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences, $rs);
300
301 $data = $this->dataAdherentOne() + [
302 'info_field_' . $adf->getId() . '_1' => 'My value (:'
303 ];
304 $this->createMember($data);
305 $model->setMember($this->adh);
306
307 $this->createPdfContribution($cdf);
308 $model->setContribution($this->contrib);
309
310 $this->assertStringContainsString(
311 '<td id="pdf_assoname"><strong id="asso_name">Galette</strong><br/></td>',
312 $model->hheader
313 );
314
315 $this->assertMatchesRegularExpression(
316 '/<td id="pdf_logo"><img src="@.+" width="129" height="60" alt="" \/><\/td>/',
317 $model->hheader
318 );
319
320 $this->assertSame(
321 '<div id="pdf_footer">
322 Association Galette - Galette
323 Palais des Papes
324 Au milieu
325 84000 Avignon - France<br/>
326
327 </div>',
328 $model->hfooter
329 );
330
331 $this->assertSame(
332 'name: DURAND René login: arthur.hamon' . $this->seed . ' birthdate: ' . $data['ddn_adh'] . ' dynlabel: Dynamic text field dynvalue: ' .
333 'My value (: ' .
334 '- enddate: ' . $this->contrib->end_date . ' amount: 92 (ninety-two) dynlabel: Dynamic date field ' .
335 'dynvalue: 2020-12-03',
336 $model->hbody
337 );
338
339 $legend = $model->getLegend();
340 $this->assertCount(3, $legend);
341 $this->assertArrayHasKey('main', $legend);
342 $this->assertArrayHasKey('member', $legend);
343 $this->assertArrayHasKey('contribution', $legend);
344
345 $this->assertCount(9, $legend['main']['patterns']);
346 $this->assertCount(28, $legend['member']['patterns']);
347 $this->assertTrue(isset($legend['member']['patterns']['label_dynfield_' . $adf->getId() . '_adh']));
348 $this->assertCount(14, $legend['contribution']['patterns']);
349 $this->assertTrue(isset($legend['contribution']['patterns']['label_dynfield_' . $cdf->getId() . '_contrib']));
350 }
351
352 /**
353 * Create test contribution in database
354 *
355 * @param DynamicField $cdf Contribution dynamic field
356 *
357 * @return void
358 */
359 protected function createPdfContribution($cdf)
360 {
361 $bdate = new \DateTime(); // 2020-11-07
362 $bdate->sub(new \DateInterval('P5M')); // 2020-06-07
363 $bdate->add(new \DateInterval('P3D')); // 2020-06-10
364
365 $edate = clone $bdate;
366 $edate->add(new \DateInterval('P1Y'));
367
368 $dyndate = new \DateTime('2020-12-03 22:56:53');
369
370 $data = [
371 'id_adh' => $this->adh->id,
372 'id_type_cotis' => 1,
373 'montant_cotis' => 92,
374 'type_paiement_cotis' => 3,
375 'info_cotis' => 'FAKER' . $this->seed,
376 'date_enreg' => $bdate->format('Y-m-d'),
377 'date_debut_cotis' => $bdate->format('Y-m-d'),
378 'date_fin_cotis' => $edate->format('Y-m-d'),
379 'info_field_' . $cdf->getId() . '_1' => $dyndate->format('Y-m-d')
380 ];
381 $this->createContrib($data);
382 }
383
384 /**
385 * Test model storage in db
386 *
387 * @return void
388 */
389 public function testStorage()
390 {
391 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
392
393 $orig_title = $model->title;
394 $this->assertSame('_T("Invoice") {CONTRIBUTION_YEAR}-{CONTRIBUTION_ID}', $orig_title);
395
396 $model->title = 'Another test';
397 $this->assertTrue($model->store());
398
399 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
400 $this->assertSame('Another test', $model->title);
401 }
402 }