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}/'
132 $this->assertSame($main_expected, $model->getPatterns());
134 $model = new \Galette\Entity\
PdfMain($this->zdb
, $this->preferences
);
135 $this->assertSame($main_expected, $model->getPatterns());
137 $expected = $main_expected +
[
138 'adh_title' => '/{TITLE_ADH}/',
139 'adh_id' => '/{ID_ADH}/',
140 'adh_num' => '/{NUM_ADH}/',
141 'adh_name' => '/{NAME_ADH}/',
142 'adh_last_name' => '/{LAST_NAME_ADH}/',
143 'adh_first_name' => '/{FIRST_NAME_ADH}/',
144 'adh_nickname' => '/{NICKNAME_ADH}/',
145 'adh_gender' => '/{GENDER_ADH}/',
146 'adh_birth_date' => '/{ADH_BIRTH_DATE}/',
147 'adh_birth_place' => '/{ADH_BIRTH_PLACE}/',
148 'adh_profession' => '/{PROFESSION_ADH}/',
149 'adh_company' => '/{COMPANY_ADH}/',
150 'adh_address' => '/{ADDRESS_ADH}/',
151 'adh_address_multi' => '/{ADDRESS_ADH_MULTI}/',
152 'adh_zip' => '/{ZIP_ADH}/',
153 'adh_town' => '/{TOWN_ADH}/',
154 'adh_country' => '/{COUNTRY_ADH}/',
155 'adh_phone' => '/{PHONE_ADH}/',
156 'adh_mobile' => '/{MOBILE_ADH}/',
157 'adh_email' => '/{EMAIL_ADH}/',
158 'adh_login' => '/{LOGIN_ADH}/',
159 'adh_main_group' => '/{GROUP_ADH}/',
160 'adh_groups' => '/{GROUPS_ADH}/',
161 'adh_dues' => '/{ADH_DUES}/',
162 'days_remaining' => '/{DAYS_REMAINING}/',
163 'days_expired' => '/{DAYS_EXPIRED}/',
164 '_adh_company' => '/{COMPANY_NAME_ADH}/',
165 '_adh_last_name' => '/{LASTNAME_ADH}/',
166 '_adh_first_name' => '/{FIRSTNAME_ADH}/',
167 '_adh_login' => '/{LOGIN}/',
168 '_adh_email' => '/{MAIL_ADH}/',
170 $model = new \Galette\Entity\
PdfAdhesionFormModel($this->zdb
, $this->preferences
);
171 $this->assertSame($expected, $model->getPatterns());
174 'contrib_label' => '/{CONTRIB_LABEL}/',
175 'contrib_amount' => '/{CONTRIB_AMOUNT}/',
176 'contrib_amount_letters' => '/{CONTRIB_AMOUNT_LETTERS}/',
177 'contrib_date' => '/{CONTRIB_DATE}/',
178 'contrib_year' => '/{CONTRIB_YEAR}/',
179 'contrib_comment' => '/{CONTRIB_COMMENT}/',
180 'contrib_bdate' => '/{CONTRIB_BEGIN_DATE}/',
181 'contrib_edate' => '/{CONTRIB_END_DATE}/',
182 'contrib_id' => '/{CONTRIB_ID}/',
183 'contrib_payment' => '/{CONTRIB_PAYMENT_TYPE}/',
184 'contrib_info' => '/{CONTRIB_INFO}/',
185 '_contrib_label' => '/{CONTRIBUTION_LABEL}/',
186 '_contrib_amount' => '/{CONTRIBUTION_AMOUNT}/',
187 '_contrib_amount_letters' => '/{CONTRIBUTION_AMOUNT_LETTERS}/',
188 '_contrib_date' => '/{CONTRIBUTION_DATE}/',
189 '_contrib_year' => '/{CONTRIBUTION_YEAR}/',
190 '_contrib_comment' => '/{CONTRIBUTION_COMMENT}/',
191 '_contrib_bdate' => '/{CONTRIBUTION_BEGIN_DATE}/',
192 '_contrib_edate' => '/{CONTRIBUTION_END_DATE}/',
193 '_contrib_id' => '/{CONTRIBUTION_ID}/',
194 '_contrib_payment' => '/{CONTRIBUTION_PAYMENT_TYPE}/',
195 '_contrib_info' => '/{CONTRIBUTION_INFO}/',
196 '__contrib_label' => '/{CONTRIB_TYPE}/',
197 'deadline' => '/{DEADLINE}/'
199 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
);
200 $this->assertSame($expected, $model->getPatterns());
202 $model = new \Galette\Entity\
PdfReceipt($this->zdb
, $this->preferences
);
203 $this->assertSame($expected, $model->getPatterns());
211 public static function typesProvider(): array
215 'type' => \Galette\Entity\PdfModel
::MAIN_MODEL
,
216 'expected' => 'Galette\Entity\PdfMain'
218 'type' => \Galette\Entity\PdfModel
::INVOICE_MODEL
,
219 'expected' => 'Galette\Entity\PdfInvoice'
221 'type' => \Galette\Entity\PdfModel
::RECEIPT_MODEL
,
222 'expected' => 'Galette\Entity\PdfReceipt'
224 'type' => \Galette\Entity\PdfModel
::ADHESION_FORM_MODEL
,
225 'expected' => 'Galette\Entity\PdfAdhesionFormModel'
228 'expected' => 'Galette\Entity\PdfMain'
235 * @dataProvider typesProvider
237 * @param integer $type Requested type
238 * @param string $expected Expected class name
242 public function testGetypeClass($type, $expected)
244 $this->assertSame($expected, \Galette\Entity\PdfModel
::getTypeClass($type));
248 * Test model replacements
252 public function testReplacements()
254 //create dynamic fields
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,
264 $adf = DynamicField
::getFieldType($this->zdb
, $field_data['field_type']);
266 $stored = $adf->store($field_data);
267 $error_detected = $adf->getErrors();
268 $warning_detected = $adf->getWarnings();
273 $adf->getErrors() +
$adf->getWarnings()
276 $this->assertEmpty($error_detected, implode(' ', $adf->getErrors()));
277 $this->assertEmpty($warning_detected, implode(' ', $adf->getWarnings()));
280 'form_name' => 'contrib',
281 'field_form' => 'contrib',
282 'field_name' => 'Dynamic date field',
283 'field_perm' => DynamicField
::PERM_USER_WRITE
,
284 'field_type' => DynamicField
::DATE
,
285 'field_required' => 1,
289 $cdf = DynamicField
::getFieldType($this->zdb
, $field_data['field_type']);
291 $stored = $cdf->store($field_data);
292 $error_detected = $cdf->getErrors();
293 $warning_detected = $cdf->getWarnings();
298 $cdf->getErrors() +
$cdf->getWarnings()
301 $this->assertEmpty($error_detected, implode(' ', $cdf->getErrors()));
302 $this->assertEmpty($warning_detected, implode(' ', $cdf->getWarnings()));
305 $pk = \Galette\Entity\PdfModel
::PK
;
306 $rs = new \
ArrayObject([
308 'model_name' => 'Test model',
309 'model_title' => 'A simple tmodel for tests',
310 'model_subtitle' => 'The subtitle',
311 'model_header' => null,
312 'model_footer' => null,
313 'model_body' => 'name: {NAME_ADH} login: {LOGIN_ADH} birthdate: {ADH_BIRTH_DATE} dynlabel: {LABEL_DYNFIELD_' .
314 $adf->getId() . '_ADH} dynvalue: {INPUT_DYNFIELD_' . $adf->getId() . '_ADH} ' .
315 '- enddate: {CONTRIB_END_DATE} amount: {CONTRIB_AMOUNT} ({CONTRIB_AMOUNT_LETTERS}) dynlabel: ' .
316 '{LABEL_DYNFIELD_' . $cdf->getId() . '_CONTRIB} dynvalue: {INPUT_DYNFIELD_' . $cdf->getId() . '_CONTRIB}',
317 'model_styles' => null,
318 'model_parent' => \Galette\Entity\PdfModel
::MAIN_MODEL
319 ], \ArrayObject
::ARRAY_AS_PROPS
);
320 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
, $rs);
322 $data = $this->dataAdherentOne() +
[
323 'info_field_' . $adf->getId() . '_1' => 'My value (:'
325 $this->createMember($data);
326 $model->setMember($this->adh
);
328 $this->createPdfContribution($cdf);
329 $model->setContribution($this->contrib
);
331 $this->assertStringContainsString(
332 '<td id="pdf_assoname"><strong id="asso_name">Galette</strong><br/></td>',
336 $this->assertMatchesRegularExpression(
337 '/<td id="pdf_logo"><img src="@.+" width="129" height="60" alt="" \/><\/td>/',
342 '<div id="pdf_footer">
343 Association Galette - Galette
346 84000 Avignon - France<br/>
353 'name: DURAND René login: arthur.hamon' . $this->seed
. ' birthdate: ' . $data['ddn_adh'] . ' dynlabel: Dynamic text field dynvalue: ' .
355 '- enddate: ' . $this->contrib
->end_date
. ' amount: 92 (ninety-two) dynlabel: Dynamic date field ' .
356 'dynvalue: 2020-12-03',
360 $legend = $model->getLegend();
361 $this->assertCount(3, $legend);
362 $this->assertArrayHasKey('main', $legend);
363 $this->assertArrayHasKey('member', $legend);
364 $this->assertArrayHasKey('contribution', $legend);
366 $this->assertCount(8, $legend['main']['patterns']);
367 $this->assertCount(28, $legend['member']['patterns']);
368 $this->assertTrue(isset($legend['member']['patterns']['label_dynfield_' . $adf->getId() . '_adh']));
369 $this->assertCount(14, $legend['contribution']['patterns']);
370 $this->assertTrue(isset($legend['contribution']['patterns']['label_dynfield_' . $cdf->getId() . '_contrib']));
374 * Create test contribution in database
376 * @param DynamicField $cdf Contribution dynamic field
380 protected function createPdfContribution($cdf)
382 $bdate = new \
DateTime(); // 2020-11-07
383 $bdate->sub(new \
DateInterval('P5M')); // 2020-06-07
384 $bdate->add(new \
DateInterval('P3D')); // 2020-06-10
386 $edate = clone $bdate;
387 $edate->add(new \
DateInterval('P1Y'));
389 $dyndate = new \
DateTime('2020-12-03 22:56:53');
392 'id_adh' => $this->adh
->id
,
393 'id_type_cotis' => 1,
394 'montant_cotis' => 92,
395 'type_paiement_cotis' => 3,
396 'info_cotis' => 'FAKER' . $this->seed
,
397 'date_enreg' => $bdate->format('Y-m-d'),
398 'date_debut_cotis' => $bdate->format('Y-m-d'),
399 'date_fin_cotis' => $edate->format('Y-m-d'),
400 'info_field_' . $cdf->getId() . '_1' => $dyndate->format('Y-m-d')
402 $this->createContrib($data);
406 * Test model storage in db
410 public function testStorage()
412 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
);
414 $orig_title = $model->title
;
415 $this->assertSame('_T("Invoice") {CONTRIBUTION_YEAR}-{CONTRIBUTION_ID}', $orig_title);
417 $model->title
= 'Another test';
418 $this->assertTrue($model->store());
420 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
);
421 $this->assertSame('Another test', $model->title
);