3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2020 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 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
34 * @link http://galette.tuxfamily.org
38 namespace Galette\Entity\test\units
;
40 use Galette\Entity\Adherent
;
41 use Galette\DynamicFields\DynamicField
;
42 use Galette\GaletteTestCase
;
49 * @package GaletteTests
50 * @author Johan Cwiklinski <johan@x-tnd.be>
51 * @copyright 2020 The Galette Team
52 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
53 * @link http://galette.tuxfamily.org
56 class PdfModel
extends GaletteTestCase
59 protected $seed = 95842354;
64 * @param string $testMethod Calling method
68 public function beforeTestMethod($testMethod)
70 parent
::beforeTestMethod($testMethod);
72 $models = new \Galette\Repository\
PdfModels($this->zdb
, $this->preferences
, $this->login
);
73 $res = $models->installInit(false);
74 $this->boolean($res)->isTrue();
76 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
);
77 $this->adh
->setDependencies(
79 $this->members_fields
,
82 $this->contrib
= new \Galette\Entity\
Contribution($this->zdb
, $this->login
);
88 * @param string $testMethod Calling method
92 public function afterTestMethod($testMethod)
94 $delete = $this->zdb
->delete(\Galette\Entity\Contribution
::TABLE
);
95 $delete->where(['info_cotis' => 'FAKER' . $this->seed
]);
96 $this->zdb
->execute($delete);
97 $delete = $this->zdb
->delete(\Galette\Entity\Adherent
::TABLE
);
98 $this->zdb
->execute($delete);
99 $delete = $this->zdb
->delete(\Galette\Entity\DynamicFieldsHandle
::TABLE
);
100 $this->zdb
->execute($delete);
101 $delete = $this->zdb
->delete(DynamicField
::TABLE
);
102 $this->zdb
->execute($delete);
103 //cleanup dynamic translations
104 $delete = $this->zdb
->delete(\Galette\Core\L10n
::TABLE
);
107 'Dynamic choice field',
108 'Dynamic date field',
112 $this->zdb
->execute($delete);
116 * Test expected patterns
120 public function testExpectedPatterns()
122 $model = new class ($this->zdb
, $this->preferences
, 1) extends \Galette\Entity\PdfModel
{
126 'asso_name' => '/{ASSO_NAME}/',
127 'asso_slogan' => '/{ASSO_SLOGAN}/',
128 'asso_address' => '/{ASSO_ADDRESS}/',
129 'asso_address_multi' => '/{ASSO_ADDRESS_MULTI}/',
130 'asso_website' => '/{ASSO_WEBSITE}/',
131 'asso_logo' => '/{ASSO_LOGO}/',
132 'date_now' => '/{DATE_NOW}/',
133 'login_uri' => '/{LOGIN_URI}/'
135 $this->array($model->getPatterns())->isIdenticalTo($main_expected);
137 $model = new \Galette\Entity\
PdfMain($this->zdb
, $this->preferences
);
138 $this->array($model->getPatterns())->isIdenticalTo($main_expected);
140 $expected = $main_expected +
[
141 'adh_title' => '/{TITLE_ADH}/',
142 'adh_id' => '/{ID_ADH}/',
143 'adh_name' => '/{NAME_ADH}/',
144 'adh_last_name' => '/{LAST_NAME_ADH}/',
145 'adh_first_name' => '/{FIRST_NAME_ADH}/',
146 'adh_nickname' => '/{NICKNAME_ADH}/',
147 'adh_gender' => '/{GENDER_ADH}/',
148 'adh_birth_date' => '/{ADH_BIRTH_DATE}/',
149 'adh_birth_place' => '/{ADH_BIRTH_PLACE}/',
150 'adh_profession' => '/{PROFESSION_ADH}/',
151 'adh_company' => '/{COMPANY_ADH}/',
152 'adh_address' => '/{ADDRESS_ADH}/',
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->array($model->getPatterns())->isIdenticalTo($expected);
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->array($model->getPatterns())->isIdenticalTo($expected);
203 $model = new \Galette\Entity\
PdfReceipt($this->zdb
, $this->preferences
);
204 $this->array($model->getPatterns())->isIdenticalTo($expected);
212 protected 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->string(\Galette\Entity\PdfModel
::getTypeClass($type))->isIdenticalTo($expected);
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();
270 $this->boolean($stored)->isTrue(
273 $adf->getErrors() +
$adf->getWarnings()
276 $this->array($error_detected)->isEmpty(implode(' ', $adf->getErrors()));
277 $this->array($warning_detected)->isEmpty(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();
294 $this->boolean($stored)->isTrue(
297 $cdf->getErrors() +
$cdf->getWarnings()
300 $this->array($error_detected)->isEmpty(implode(' ', $cdf->getErrors()));
301 $this->array($warning_detected)->isEmpty(implode(' ', $cdf->getWarnings()));
304 $rs = new \
stdClass();
305 $pk = \Galette\Entity\PdfModel
::PK
;
307 $rs->model_name
= 'Test model';
308 $rs->model_title
= 'A simple tmodel for tests';
309 $rs->model_subtitle
= 'The subtitle';
310 $rs->model_header
= null;
311 $rs->model_footer
= null;
312 $rs->model_body
= 'name: {NAME_ADH} login: {LOGIN_ADH} birthdate: {ADH_BIRTH_DATE} dynlabel: {LABEL_DYNFIELD_' .
313 $adf->getId() . '_ADH} dynvalue: {INPUT_DYNFIELD_' . $adf->getId() . '_ADH} ' .
314 '- enddate: {CONTRIB_END_DATE} amount: {CONTRIB_AMOUNT} ({CONTRIB_AMOUNT_LETTERS}) dynlabel: ' .
315 '{LABEL_DYNFIELD_' . $cdf->getId() . '_CONTRIB} dynvalue: {INPUT_DYNFIELD_' . $cdf->getId() . '_CONTRIB}';
316 $rs->model_styles
= null;
317 $rs->model_parent
= \Galette\Entity\PdfModel
::MAIN_MODEL
;
319 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
, $rs);
322 'nom_adh' => 'Durand',
323 'prenom_adh' => 'René',
324 'ville_adh' => 'Martel',
325 'cp_adh' => '39 069',
326 'adresse_adh' => '66, boulevard De Oliveira',
327 'email_adh' => 'meunier.josephine@ledoux.com',
328 'login_adh' => 'arthur.hamon',
329 'mdp_adh' => 'J^B-()f',
330 'mdp_adh2' => 'J^B-()f',
331 'bool_admin_adh' => false,
332 'bool_exempt_adh' => false,
333 'bool_display_info' => true,
335 'prof_adh' => 'Chef de fabrication',
337 'ddn_adh' => '1937-12-26',
338 'lieu_naissance' => 'Gonzalez-sur-Meunier',
339 'pseudo_adh' => 'ubertrand',
340 'pays_adh' => 'Antarctique',
341 'tel_adh' => '0439153432',
342 'url_adh' => 'http://bouchet.com/',
343 'activite_adh' => true,
345 'date_crea_adh' => '2020-06-10',
346 'pref_lang' => 'en_US',
347 'fingerprint' => 'FAKER' . $this->seed
,
348 'info_field_' . $adf->getId() . '_1' => 'My value (:'
350 $this->createMember($data);
351 $model->setMember($this->adh
);
353 $this->createPdfContribution($cdf);
354 $model->setContribution($this->contrib
);
356 $this->string($model->hheader
)->isIdenticalTo("<table>
358 <td id=\"pdf_assoname\"><strong id=\"asso_name\">Galette</strong><br/></td>
359 <td id=\"pdf_logo\"><img src=\"http://logo\" width=\"129\" height=\"60\"/></td>
363 $this->string($model->hfooter
)->isIdenticalTo('<div id="pdf_footer">
364 Association Galette - Galette
367 84000 Avignon - France<br/>
371 $this->string($model->hbody
)->isEqualTo(
372 'name: DURAND René login: arthur.hamon birthdate: 1937-12-26 dynlabel: Dynamic text field dynvalue: ' .
374 '- enddate: ' . $this->contrib
->end_date
. ' amount: 92 (ninety-two) dynlabel: Dynamic date field ' .
375 'dynvalue: 2020-12-03'
378 $legend = $model->getLegend();
379 $this->array($legend)
381 ->hasKeys(['main', 'member', 'contribution']);
383 $this->array($legend['main']['patterns'])->hasSize(8);
384 $this->array($legend['member']['patterns'])
386 ->hasKeys(['label_dynfield_' . $adf->getId() . '_adh', 'dynfield_' . $adf->getId() . '_adh']);
387 $this->array($legend['contribution']['patterns'])
389 ->hasKeys(['label_dynfield_' . $cdf->getId() . '_contrib', 'dynfield_' . $cdf->getId() . '_contrib']);
393 * Create member from data
395 * @param array $data Data to use to create member
399 public function createMember(array $data)
402 $check = $adh->check($data, [], []);
403 if (is_array($check)) {
406 $this->boolean($check)->isTrue();
408 $store = $adh->store();
409 $this->boolean($store)->isTrue();
414 * Create test contribution in database
416 * @param DynamicField $cdf Contribution dynamic field
420 protected function createPdfContribution($cdf)
422 $bdate = new \
DateTime(); // 2020-11-07
423 $bdate->sub(new \
DateInterval('P5M')); // 2020-06-07
424 $bdate->add(new \
DateInterval('P3D')); // 2020-06-10
426 $edate = clone $bdate;
427 $edate->add(new \
DateInterval('P1Y'));
429 $dyndate = new \
DateTime('2020-12-03 22:56:53');
432 'id_adh' => $this->adh
->id
,
433 'id_type_cotis' => 1,
434 'montant_cotis' => 92,
435 'type_paiement_cotis' => 3,
436 'info_cotis' => 'FAKER' . $this->seed
,
437 'date_enreg' => $bdate->format('Y-m-d'),
438 'date_debut_cotis' => $bdate->format('Y-m-d'),
439 'date_fin_cotis' => $edate->format('Y-m-d'),
440 'info_field_' . $cdf->getId() . '_1' => $dyndate->format('Y-m-d')
442 $this->createContrib($data);
446 * Test model storage in db
450 public function testStorage()
452 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
);
454 $orig_title = $model->title
;
455 $this->string($orig_title)->isIdenticalTo('_T("Invoice") {CONTRIBUTION_YEAR}-{CONTRIBUTION_ID}');
457 $model->title
= 'Another test';
458 $this->boolean($model->store())->isTrue();
460 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
);
461 $this->string($model->title
)->isIdenticalTo('Another test');