4 * Copyright © 2003-2024 The Galette Team
6 * This file is part of Galette (https://galette.eu).
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.
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.
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/>.
22 namespace Galette\Entity\test\units
;
24 use Galette\Entity\Adherent
;
25 use Galette\DynamicFields\DynamicField
;
26 use Galette\GaletteTestCase
;
31 * @author Johan Cwiklinski <johan@x-tnd.be>
33 class PdfModel
extends GaletteTestCase
35 private array $remove = [];
36 protected int $seed = 95842354;
43 public function setUp(): void
47 $models = new \Galette\Repository\
PdfModels($this->zdb
, $this->preferences
, $this->login
);
48 $res = $models->installInit(false);
49 $this->assertTrue($res);
51 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
);
52 $this->adh
->setDependencies(
54 $this->members_fields
,
57 $this->contrib
= new \Galette\Entity\
Contribution($this->zdb
, $this->login
);
65 public function tearDown(): void
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
);
82 'Dynamic choice field',
87 $this->zdb
->execute($delete);
91 * Test expected patterns
95 public function testExpectedPatterns()
97 $model = new class ($this->zdb
, $this->preferences
, 1) extends \Galette\Entity\PdfModel
{
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}/'
111 $this->assertSame($main_expected, $model->getPatterns());
113 $model = new \Galette\Entity\
PdfMain($this->zdb
, $this->preferences
);
114 $this->assertSame($main_expected, $model->getPatterns());
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}/',
149 $model = new \Galette\Entity\
PdfAdhesionFormModel($this->zdb
, $this->preferences
);
150 $this->assertSame($expected, $model->getPatterns());
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}/'
178 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
);
179 $this->assertSame($expected, $model->getPatterns());
181 $model = new \Galette\Entity\
PdfReceipt($this->zdb
, $this->preferences
);
182 $this->assertSame($expected, $model->getPatterns());
190 public static function typesProvider(): array
194 'type' => \Galette\Entity\PdfModel
::MAIN_MODEL
,
195 'expected' => 'Galette\Entity\PdfMain'
197 'type' => \Galette\Entity\PdfModel
::INVOICE_MODEL
,
198 'expected' => 'Galette\Entity\PdfInvoice'
200 'type' => \Galette\Entity\PdfModel
::RECEIPT_MODEL
,
201 'expected' => 'Galette\Entity\PdfReceipt'
203 'type' => \Galette\Entity\PdfModel
::ADHESION_FORM_MODEL
,
204 'expected' => 'Galette\Entity\PdfAdhesionFormModel'
207 'expected' => 'Galette\Entity\PdfMain'
214 * @dataProvider typesProvider
216 * @param integer $type Requested type
217 * @param string $expected Expected class name
221 public function testGetypeClass($type, $expected)
223 $this->assertSame($expected, \Galette\Entity\PdfModel
::getTypeClass($type));
227 * Test model replacements
231 public function testReplacements()
233 //create dynamic fields
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,
243 $adf = DynamicField
::getFieldType($this->zdb
, $field_data['field_type']);
245 $stored = $adf->store($field_data);
246 $error_detected = $adf->getErrors();
247 $warning_detected = $adf->getWarnings();
252 $adf->getErrors() +
$adf->getWarnings()
255 $this->assertEmpty($error_detected, implode(' ', $adf->getErrors()));
256 $this->assertEmpty($warning_detected, implode(' ', $adf->getWarnings()));
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,
268 $cdf = DynamicField
::getFieldType($this->zdb
, $field_data['field_type']);
270 $stored = $cdf->store($field_data);
271 $error_detected = $cdf->getErrors();
272 $warning_detected = $cdf->getWarnings();
277 $cdf->getErrors() +
$cdf->getWarnings()
280 $this->assertEmpty($error_detected, implode(' ', $cdf->getErrors()));
281 $this->assertEmpty($warning_detected, implode(' ', $cdf->getWarnings()));
284 $pk = \Galette\Entity\PdfModel
::PK
;
285 $rs = new \
ArrayObject([
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);
301 $data = $this->dataAdherentOne() +
[
302 'info_field_' . $adf->getId() . '_1' => 'My value (:'
304 $this->createMember($data);
305 $model->setMember($this->adh
);
307 $this->createPdfContribution($cdf);
308 $model->setContribution($this->contrib
);
310 $this->assertStringContainsString(
311 '<td id="pdf_assoname"><strong id="asso_name">Galette</strong><br/></td>',
315 $this->assertMatchesRegularExpression(
316 '/<td id="pdf_logo"><img src="@.+" width="129" height="60" alt="" \/><\/td>/',
321 '<div id="pdf_footer">
322 Association Galette - Galette
325 84000 Avignon - France<br/>
332 'name: DURAND René login: arthur.hamon' . $this->seed
. ' birthdate: ' . $data['ddn_adh'] . ' dynlabel: Dynamic text field dynvalue: ' .
334 '- enddate: ' . $this->contrib
->end_date
. ' amount: 92 (ninety-two) dynlabel: Dynamic date field ' .
335 'dynvalue: 2020-12-03',
339 $legend = $model->getLegend();
340 $this->assertCount(3, $legend);
341 $this->assertArrayHasKey('main', $legend);
342 $this->assertArrayHasKey('member', $legend);
343 $this->assertArrayHasKey('contribution', $legend);
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']));
353 * Create test contribution in database
355 * @param DynamicField $cdf Contribution dynamic field
359 protected function createPdfContribution($cdf)
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
365 $edate = clone $bdate;
366 $edate->add(new \
DateInterval('P1Y'));
368 $dyndate = new \
DateTime('2020-12-03 22:56:53');
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')
381 $this->createContrib($data);
385 * Test model storage in db
389 public function testStorage()
391 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
);
393 $orig_title = $model->title
;
394 $this->assertSame('_T("Invoice") {CONTRIBUTION_YEAR}-{CONTRIBUTION_ID}', $orig_title);
396 $model->title
= 'Another test';
397 $this->assertTrue($model->store());
399 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
);
400 $this->assertSame('Another test', $model->title
);