3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2020-2023 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
28 * @package GaletteTests
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
37 namespace Galette\Entity\test\units
;
39 use Galette\Entity\Adherent
;
40 use Galette\DynamicFields\DynamicField
;
41 use Galette\GaletteTestCase
;
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
55 class PdfModel
extends GaletteTestCase
57 private array $remove = [];
58 protected int $seed = 95842354;
65 public function setUp(): void
69 $models = new \Galette\Repository\
PdfModels($this->zdb
, $this->preferences
, $this->login
);
70 $res = $models->installInit(false);
71 $this->assertTrue($res);
73 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
);
74 $this->adh
->setDependencies(
76 $this->members_fields
,
79 $this->contrib
= new \Galette\Entity\
Contribution($this->zdb
, $this->login
);
87 public function tearDown(): void
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
);
104 'Dynamic choice field',
105 'Dynamic date field',
109 $this->zdb
->execute($delete);
113 * Test expected patterns
117 public function testExpectedPatterns()
119 $model = new class ($this->zdb
, $this->preferences
, 1) extends \Galette\Entity\PdfModel
{
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}/',
133 $this->assertSame($main_expected, $model->getPatterns());
135 $model = new \Galette\Entity\
PdfMain($this->zdb
, $this->preferences
);
136 $this->assertSame($main_expected, $model->getPatterns());
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}/',
171 $model = new \Galette\Entity\
PdfAdhesionFormModel($this->zdb
, $this->preferences
);
172 $this->assertSame($expected, $model->getPatterns());
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}/'
200 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
);
201 $this->assertSame($expected, $model->getPatterns());
203 $model = new \Galette\Entity\
PdfReceipt($this->zdb
, $this->preferences
);
204 $this->assertSame($expected, $model->getPatterns());
212 public static function typesProvider(): array
216 'type' => \Galette\Entity\PdfModel
::MAIN_MODEL
,
217 'expected' => 'Galette\Entity\PdfMain'
219 'type' => \Galette\Entity\PdfModel
::INVOICE_MODEL
,
220 'expected' => 'Galette\Entity\PdfInvoice'
222 'type' => \Galette\Entity\PdfModel
::RECEIPT_MODEL
,
223 'expected' => 'Galette\Entity\PdfReceipt'
225 'type' => \Galette\Entity\PdfModel
::ADHESION_FORM_MODEL
,
226 'expected' => 'Galette\Entity\PdfAdhesionFormModel'
229 'expected' => 'Galette\Entity\PdfMain'
236 * @dataProvider typesProvider
238 * @param integer $type Requested type
239 * @param string $expected Expected class name
243 public function testGetypeClass($type, $expected)
245 $this->assertSame($expected, \Galette\Entity\PdfModel
::getTypeClass($type));
249 * Test model replacements
253 public function testReplacements()
255 //create dynamic fields
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,
265 $adf = DynamicField
::getFieldType($this->zdb
, $field_data['field_type']);
267 $stored = $adf->store($field_data);
268 $error_detected = $adf->getErrors();
269 $warning_detected = $adf->getWarnings();
274 $adf->getErrors() +
$adf->getWarnings()
277 $this->assertEmpty($error_detected, implode(' ', $adf->getErrors()));
278 $this->assertEmpty($warning_detected, implode(' ', $adf->getWarnings()));
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,
290 $cdf = DynamicField
::getFieldType($this->zdb
, $field_data['field_type']);
292 $stored = $cdf->store($field_data);
293 $error_detected = $cdf->getErrors();
294 $warning_detected = $cdf->getWarnings();
299 $cdf->getErrors() +
$cdf->getWarnings()
302 $this->assertEmpty($error_detected, implode(' ', $cdf->getErrors()));
303 $this->assertEmpty($warning_detected, implode(' ', $cdf->getWarnings()));
306 $pk = \Galette\Entity\PdfModel
::PK
;
307 $rs = new \
ArrayObject([
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);
323 $data = $this->dataAdherentOne() +
[
324 'info_field_' . $adf->getId() . '_1' => 'My value (:'
326 $this->createMember($data);
327 $model->setMember($this->adh
);
329 $this->createPdfContribution($cdf);
330 $model->setContribution($this->contrib
);
332 $this->assertStringContainsString(
333 '<td id="pdf_assoname"><strong id="asso_name">Galette</strong><br/></td>',
337 $this->assertMatchesRegularExpression(
338 '/<td id="pdf_logo"><img src="@.+" width="129" height="60" alt="" \/><\/td>/',
343 '<div id="pdf_footer">
344 Association Galette - Galette
347 84000 Avignon - France<br/>
354 'name: DURAND René login: arthur.hamon' . $this->seed
. ' birthdate: ' . $data['ddn_adh'] . ' dynlabel: Dynamic text field dynvalue: ' .
356 '- enddate: ' . $this->contrib
->end_date
. ' amount: 92 (ninety-two) dynlabel: Dynamic date field ' .
357 'dynvalue: 2020-12-03',
361 $legend = $model->getLegend();
362 $this->assertCount(3, $legend);
363 $this->assertArrayHasKey('main', $legend);
364 $this->assertArrayHasKey('member', $legend);
365 $this->assertArrayHasKey('contribution', $legend);
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']));
375 * Create test contribution in database
377 * @param DynamicField $cdf Contribution dynamic field
381 protected function createPdfContribution($cdf)
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
387 $edate = clone $bdate;
388 $edate->add(new \
DateInterval('P1Y'));
390 $dyndate = new \
DateTime('2020-12-03 22:56:53');
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')
403 $this->createContrib($data);
407 * Test model storage in db
411 public function testStorage()
413 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
);
415 $orig_title = $model->title
;
416 $this->assertSame('_T("Invoice") {CONTRIBUTION_YEAR}-{CONTRIBUTION_ID}', $orig_title);
418 $model->title
= 'Another test';
419 $this->assertTrue($model->store());
421 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
);
422 $this->assertSame('Another test', $model->title
);