]> git.agnieray.net Git - galette.git/blob - tests/Galette/Entity/tests/units/PdfModel.php
Improve coding standards
[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(): void
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 'asso_footer' => '/{ASSO_FOOTER}/',
111 ];
112 $this->assertSame($main_expected, $model->getPatterns());
113
114 $model = new \Galette\Entity\PdfMain($this->zdb, $this->preferences);
115 $this->assertSame($main_expected, $model->getPatterns());
116
117 $expected = $main_expected + [
118 'adh_title' => '/{TITLE_ADH}/',
119 'adh_id' => '/{ID_ADH}/',
120 'adh_num' => '/{NUM_ADH}/',
121 'adh_name' => '/{NAME_ADH}/',
122 'adh_last_name' => '/{LAST_NAME_ADH}/',
123 'adh_first_name' => '/{FIRST_NAME_ADH}/',
124 'adh_nickname' => '/{NICKNAME_ADH}/',
125 'adh_gender' => '/{GENDER_ADH}/',
126 'adh_birth_date' => '/{ADH_BIRTH_DATE}/',
127 'adh_birth_place' => '/{ADH_BIRTH_PLACE}/',
128 'adh_profession' => '/{PROFESSION_ADH}/',
129 'adh_company' => '/{COMPANY_ADH}/',
130 'adh_address' => '/{ADDRESS_ADH}/',
131 'adh_address_multi' => '/{ADDRESS_ADH_MULTI}/',
132 'adh_zip' => '/{ZIP_ADH}/',
133 'adh_town' => '/{TOWN_ADH}/',
134 'adh_country' => '/{COUNTRY_ADH}/',
135 'adh_phone' => '/{PHONE_ADH}/',
136 'adh_mobile' => '/{MOBILE_ADH}/',
137 'adh_email' => '/{EMAIL_ADH}/',
138 'adh_login' => '/{LOGIN_ADH}/',
139 'adh_main_group' => '/{GROUP_ADH}/',
140 'adh_groups' => '/{GROUPS_ADH}/',
141 'adh_dues' => '/{ADH_DUES}/',
142 'days_remaining' => '/{DAYS_REMAINING}/',
143 'days_expired' => '/{DAYS_EXPIRED}/',
144 '_adh_company' => '/{COMPANY_NAME_ADH}/',
145 '_adh_last_name' => '/{LASTNAME_ADH}/',
146 '_adh_first_name' => '/{FIRSTNAME_ADH}/',
147 '_adh_login' => '/{LOGIN}/',
148 '_adh_email' => '/{MAIL_ADH}/',
149 ];
150 $model = new \Galette\Entity\PdfAdhesionFormModel($this->zdb, $this->preferences);
151 $this->assertSame($expected, $model->getPatterns());
152
153 $expected += [
154 'contrib_label' => '/{CONTRIB_LABEL}/',
155 'contrib_amount' => '/{CONTRIB_AMOUNT}/',
156 'contrib_amount_letters' => '/{CONTRIB_AMOUNT_LETTERS}/',
157 'contrib_date' => '/{CONTRIB_DATE}/',
158 'contrib_year' => '/{CONTRIB_YEAR}/',
159 'contrib_comment' => '/{CONTRIB_COMMENT}/',
160 'contrib_bdate' => '/{CONTRIB_BEGIN_DATE}/',
161 'contrib_edate' => '/{CONTRIB_END_DATE}/',
162 'contrib_id' => '/{CONTRIB_ID}/',
163 'contrib_payment' => '/{CONTRIB_PAYMENT_TYPE}/',
164 'contrib_info' => '/{CONTRIB_INFO}/',
165 '_contrib_label' => '/{CONTRIBUTION_LABEL}/',
166 '_contrib_amount' => '/{CONTRIBUTION_AMOUNT}/',
167 '_contrib_amount_letters' => '/{CONTRIBUTION_AMOUNT_LETTERS}/',
168 '_contrib_date' => '/{CONTRIBUTION_DATE}/',
169 '_contrib_year' => '/{CONTRIBUTION_YEAR}/',
170 '_contrib_comment' => '/{CONTRIBUTION_COMMENT}/',
171 '_contrib_bdate' => '/{CONTRIBUTION_BEGIN_DATE}/',
172 '_contrib_edate' => '/{CONTRIBUTION_END_DATE}/',
173 '_contrib_id' => '/{CONTRIBUTION_ID}/',
174 '_contrib_payment' => '/{CONTRIBUTION_PAYMENT_TYPE}/',
175 '_contrib_info' => '/{CONTRIBUTION_INFO}/',
176 '__contrib_label' => '/{CONTRIB_TYPE}/',
177 'deadline' => '/{DEADLINE}/'
178 ];
179 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
180 $this->assertSame($expected, $model->getPatterns());
181
182 $model = new \Galette\Entity\PdfReceipt($this->zdb, $this->preferences);
183 $this->assertSame($expected, $model->getPatterns());
184 }
185
186 /**
187 * Types provider
188 *
189 * @return array
190 */
191 public static function typesProvider(): array
192 {
193 return [
194 [
195 'type' => \Galette\Entity\PdfModel::MAIN_MODEL,
196 'expected' => 'Galette\Entity\PdfMain'
197 ], [
198 'type' => \Galette\Entity\PdfModel::INVOICE_MODEL,
199 'expected' => 'Galette\Entity\PdfInvoice'
200 ], [
201 'type' => \Galette\Entity\PdfModel::RECEIPT_MODEL,
202 'expected' => 'Galette\Entity\PdfReceipt'
203 ], [
204 'type' => \Galette\Entity\PdfModel::ADHESION_FORM_MODEL,
205 'expected' => 'Galette\Entity\PdfAdhesionFormModel'
206 ], [
207 'type' => 0,
208 'expected' => 'Galette\Entity\PdfMain'
209 ]
210 ];
211 }
212
213 /**
214 * Tets getTypeClass
215 * @dataProvider typesProvider
216 *
217 * @param integer $type Requested type
218 * @param string $expected Expected class name
219 *
220 * @return void
221 */
222 public function testGetypeClass(int $type, string $expected): void
223 {
224 $this->assertSame($expected, \Galette\Entity\PdfModel::getTypeClass($type));
225 }
226
227 /**
228 * Test model replacements
229 *
230 * @return void
231 */
232 public function testReplacements(): void
233 {
234 //create dynamic fields
235 $field_data = [
236 'form_name' => 'adh',
237 'field_name' => 'Dynamic text field',
238 'field_perm' => \Galette\Entity\FieldsConfig::USER_WRITE,
239 'field_type' => DynamicField::TEXT,
240 'field_required' => 1,
241 'field_repeat' => 1
242 ];
243
244 $adf = DynamicField::getFieldType($this->zdb, $field_data['field_type']);
245
246 $stored = $adf->store($field_data);
247 $error_detected = $adf->getErrors();
248 $warning_detected = $adf->getWarnings();
249 $this->assertTrue(
250 $stored,
251 implode(
252 ' ',
253 $adf->getErrors() + $adf->getWarnings()
254 )
255 );
256 $this->assertEmpty($error_detected, implode(' ', $adf->getErrors()));
257 $this->assertEmpty($warning_detected, implode(' ', $adf->getWarnings()));
258
259 $field_data = [
260 'form_name' => 'contrib',
261 'field_form' => 'contrib',
262 'field_name' => 'Dynamic date field',
263 'field_perm' => \Galette\Entity\FieldsConfig::USER_WRITE,
264 'field_type' => DynamicField::DATE,
265 'field_required' => 1,
266 'field_repeat' => 1
267 ];
268
269 $cdf = DynamicField::getFieldType($this->zdb, $field_data['field_type']);
270
271 $stored = $cdf->store($field_data);
272 $error_detected = $cdf->getErrors();
273 $warning_detected = $cdf->getWarnings();
274 $this->assertTrue(
275 $stored,
276 implode(
277 ' ',
278 $cdf->getErrors() + $cdf->getWarnings()
279 )
280 );
281 $this->assertEmpty($error_detected, implode(' ', $cdf->getErrors()));
282 $this->assertEmpty($warning_detected, implode(' ', $cdf->getWarnings()));
283
284 //prepare model
285 $pk = \Galette\Entity\PdfModel::PK;
286 $rs = new \ArrayObject([
287 $pk => 42,
288 'model_name' => 'Test model',
289 'model_title' => 'A simple tmodel for tests',
290 'model_subtitle' => 'The subtitle',
291 'model_header' => null,
292 'model_footer' => null,
293 'model_body' => 'name: {NAME_ADH} login: {LOGIN_ADH} birthdate: {ADH_BIRTH_DATE} dynlabel: {LABEL_DYNFIELD_' .
294 $adf->getId() . '_ADH} dynvalue: {INPUT_DYNFIELD_' . $adf->getId() . '_ADH} ' .
295 '- enddate: {CONTRIB_END_DATE} amount: {CONTRIB_AMOUNT} ({CONTRIB_AMOUNT_LETTERS}) dynlabel: ' .
296 '{LABEL_DYNFIELD_' . $cdf->getId() . '_CONTRIB} dynvalue: {INPUT_DYNFIELD_' . $cdf->getId() . '_CONTRIB}',
297 'model_styles' => null,
298 'model_parent' => \Galette\Entity\PdfModel::MAIN_MODEL
299 ], \ArrayObject::ARRAY_AS_PROPS);
300 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences, $rs);
301
302 $data = $this->dataAdherentOne() + [
303 'info_field_' . $adf->getId() . '_1' => 'My value (:'
304 ];
305 $this->createMember($data);
306 $model->setMember($this->adh);
307
308 $this->createPdfContribution($cdf);
309 $model->setContribution($this->contrib);
310
311 $this->assertStringContainsString(
312 '<td id="pdf_assoname"><strong id="asso_name">Galette</strong><br/></td>',
313 $model->hheader
314 );
315
316 $this->assertMatchesRegularExpression(
317 '/<td id="pdf_logo"><img src="@.+" width="129" height="60" alt="" \/><\/td>/',
318 $model->hheader
319 );
320
321 $this->assertSame(
322 '<div id="pdf_footer">
323 Association Galette - Galette
324 Palais des Papes
325 Au milieu
326 84000 Avignon - France<br/>
327
328 </div>',
329 $model->hfooter
330 );
331
332 $this->assertSame(
333 'name: DURAND René login: arthur.hamon' . $this->seed . ' birthdate: ' . $data['ddn_adh'] . ' dynlabel: Dynamic text field dynvalue: ' .
334 'My value (: ' .
335 '- enddate: ' . $this->contrib->end_date . ' amount: 92 (ninety-two) dynlabel: Dynamic date field ' .
336 'dynvalue: 2020-12-03',
337 $model->hbody
338 );
339
340 $legend = $model->getLegend();
341 $this->assertCount(3, $legend);
342 $this->assertArrayHasKey('main', $legend);
343 $this->assertArrayHasKey('member', $legend);
344 $this->assertArrayHasKey('contribution', $legend);
345
346 $this->assertCount(10, $legend['main']['patterns']);
347 $this->assertCount(28, $legend['member']['patterns']);
348 $this->assertTrue(isset($legend['member']['patterns']['label_dynfield_' . $adf->getId() . '_adh']));
349 $this->assertCount(14, $legend['contribution']['patterns']);
350 $this->assertTrue(isset($legend['contribution']['patterns']['label_dynfield_' . $cdf->getId() . '_contrib']));
351 }
352
353 /**
354 * Create test contribution in database
355 *
356 * @param DynamicField $cdf Contribution dynamic field
357 *
358 * @return void
359 */
360 protected function createPdfContribution(DynamicField $cdf): void
361 {
362 $bdate = new \DateTime(); // 2020-11-07
363 $bdate->sub(new \DateInterval('P5M')); // 2020-06-07
364 $bdate->add(new \DateInterval('P3D')); // 2020-06-10
365
366 $edate = clone $bdate;
367 $edate->add(new \DateInterval('P1Y'));
368
369 $dyndate = new \DateTime('2020-12-03 22:56:53');
370
371 $data = [
372 'id_adh' => $this->adh->id,
373 'id_type_cotis' => 1,
374 'montant_cotis' => 92,
375 'type_paiement_cotis' => 3,
376 'info_cotis' => 'FAKER' . $this->seed,
377 'date_enreg' => $bdate->format('Y-m-d'),
378 'date_debut_cotis' => $bdate->format('Y-m-d'),
379 'date_fin_cotis' => $edate->format('Y-m-d'),
380 'info_field_' . $cdf->getId() . '_1' => $dyndate->format('Y-m-d')
381 ];
382 $this->createContrib($data);
383 }
384
385 /**
386 * Test model storage in db
387 *
388 * @return void
389 */
390 public function testStorage(): void
391 {
392 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
393
394 $orig_title = $model->title;
395 $this->assertSame('_T("Invoice") {CONTRIBUTION_YEAR}-{CONTRIBUTION_ID}', $orig_title);
396
397 $model->title = 'Another test';
398 $this->assertTrue($model->store());
399
400 $model = new \Galette\Entity\PdfInvoice($this->zdb, $this->preferences);
401 $this->assertSame('Another test', $model->title);
402 }
403 }