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 $method Calling method
68 public function beforeTestMethod($method)
70 parent
::beforeTestMethod($method);
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 $method Calling method
92 public function afterTestMethod($method)
94 parent
::afterTestMethod($method);
96 $delete = $this->zdb
->delete(\Galette\Entity\Contribution
::TABLE
);
97 $delete->where(['info_cotis' => 'FAKER' . $this->seed
]);
98 $this->zdb
->execute($delete);
99 $delete = $this->zdb
->delete(\Galette\Entity\Adherent
::TABLE
);
100 $this->zdb
->execute($delete);
101 $delete = $this->zdb
->delete(\Galette\Entity\DynamicFieldsHandle
::TABLE
);
102 $this->zdb
->execute($delete);
103 $delete = $this->zdb
->delete(DynamicField
::TABLE
);
104 $this->zdb
->execute($delete);
105 //cleanup dynamic translations
106 $delete = $this->zdb
->delete(\Galette\Core\L10n
::TABLE
);
109 'Dynamic choice field',
110 'Dynamic date field',
114 $this->zdb
->execute($delete);
118 * Test expected patterns
122 public function testExpectedPatterns()
124 $model = new class ($this->zdb
, $this->preferences
, 1) extends \Galette\Entity\PdfModel
{
128 'asso_name' => '/{ASSO_NAME}/',
129 'asso_slogan' => '/{ASSO_SLOGAN}/',
130 'asso_address' => '/{ASSO_ADDRESS}/',
131 'asso_address_multi' => '/{ASSO_ADDRESS_MULTI}/',
132 'asso_website' => '/{ASSO_WEBSITE}/',
133 'asso_logo' => '/{ASSO_LOGO}/',
134 'date_now' => '/{DATE_NOW}/',
135 'login_uri' => '/{LOGIN_URI}/'
137 $this->array($model->getPatterns())->isIdenticalTo($main_expected);
139 $model = new \Galette\Entity\
PdfMain($this->zdb
, $this->preferences
);
140 $this->array($model->getPatterns())->isIdenticalTo($main_expected);
142 $expected = $main_expected +
[
143 'adh_title' => '/{TITLE_ADH}/',
144 'adh_id' => '/{ID_ADH}/',
145 'adh_name' => '/{NAME_ADH}/',
146 'adh_last_name' => '/{LAST_NAME_ADH}/',
147 'adh_first_name' => '/{FIRST_NAME_ADH}/',
148 'adh_nickname' => '/{NICKNAME_ADH}/',
149 'adh_gender' => '/{GENDER_ADH}/',
150 'adh_birth_date' => '/{ADH_BIRTH_DATE}/',
151 'adh_birth_place' => '/{ADH_BIRTH_PLACE}/',
152 'adh_profession' => '/{PROFESSION_ADH}/',
153 'adh_company' => '/{COMPANY_ADH}/',
154 'adh_address' => '/{ADDRESS_ADH}/',
155 'adh_zip' => '/{ZIP_ADH}/',
156 'adh_town' => '/{TOWN_ADH}/',
157 'adh_country' => '/{COUNTRY_ADH}/',
158 'adh_phone' => '/{PHONE_ADH}/',
159 'adh_mobile' => '/{MOBILE_ADH}/',
160 'adh_email' => '/{EMAIL_ADH}/',
161 'adh_login' => '/{LOGIN_ADH}/',
162 'adh_main_group' => '/{GROUP_ADH}/',
163 'adh_groups' => '/{GROUPS_ADH}/',
164 'adh_dues' => '/{ADH_DUES}/',
165 'days_remaining' => '/{DAYS_REMAINING}/',
166 'days_expired' => '/{DAYS_EXPIRED}/',
167 '_adh_company' => '/{COMPANY_NAME_ADH}/',
168 '_adh_last_name' => '/{LASTNAME_ADH}/',
169 '_adh_first_name' => '/{FIRSTNAME_ADH}/',
170 '_adh_login' => '/{LOGIN}/',
171 '_adh_email' => '/{MAIL_ADH}/',
173 $model = new \Galette\Entity\
PdfAdhesionFormModel($this->zdb
, $this->preferences
);
174 $this->array($model->getPatterns())->isIdenticalTo($expected);
177 'contrib_label' => '/{CONTRIB_LABEL}/',
178 'contrib_amount' => '/{CONTRIB_AMOUNT}/',
179 'contrib_amount_letters' => '/{CONTRIB_AMOUNT_LETTERS}/',
180 'contrib_date' => '/{CONTRIB_DATE}/',
181 'contrib_year' => '/{CONTRIB_YEAR}/',
182 'contrib_comment' => '/{CONTRIB_COMMENT}/',
183 'contrib_bdate' => '/{CONTRIB_BEGIN_DATE}/',
184 'contrib_edate' => '/{CONTRIB_END_DATE}/',
185 'contrib_id' => '/{CONTRIB_ID}/',
186 'contrib_payment' => '/{CONTRIB_PAYMENT_TYPE}/',
187 'contrib_info' => '/{CONTRIB_INFO}/',
188 '_contrib_label' => '/{CONTRIBUTION_LABEL}/',
189 '_contrib_amount' => '/{CONTRIBUTION_AMOUNT}/',
190 '_contrib_amount_letters' => '/{CONTRIBUTION_AMOUNT_LETTERS}/',
191 '_contrib_date' => '/{CONTRIBUTION_DATE}/',
192 '_contrib_year' => '/{CONTRIBUTION_YEAR}/',
193 '_contrib_comment' => '/{CONTRIBUTION_COMMENT}/',
194 '_contrib_bdate' => '/{CONTRIBUTION_BEGIN_DATE}/',
195 '_contrib_edate' => '/{CONTRIBUTION_END_DATE}/',
196 '_contrib_id' => '/{CONTRIBUTION_ID}/',
197 '_contrib_payment' => '/{CONTRIBUTION_PAYMENT_TYPE}/',
198 '_contrib_info' => '/{CONTRIBUTION_INFO}/',
199 '__contrib_label' => '/{CONTRIB_TYPE}/',
200 'deadline' => '/{DEADLINE}/'
202 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
);
203 $this->array($model->getPatterns())->isIdenticalTo($expected);
205 $model = new \Galette\Entity\
PdfReceipt($this->zdb
, $this->preferences
);
206 $this->array($model->getPatterns())->isIdenticalTo($expected);
214 protected function typesProvider(): array
218 'type' => \Galette\Entity\PdfModel
::MAIN_MODEL
,
219 'expected' => 'Galette\Entity\PdfMain'
221 'type' => \Galette\Entity\PdfModel
::INVOICE_MODEL
,
222 'expected' => 'Galette\Entity\PdfInvoice'
224 'type' => \Galette\Entity\PdfModel
::RECEIPT_MODEL
,
225 'expected' => 'Galette\Entity\PdfReceipt'
227 'type' => \Galette\Entity\PdfModel
::ADHESION_FORM_MODEL
,
228 'expected' => 'Galette\Entity\PdfAdhesionFormModel'
231 'expected' => 'Galette\Entity\PdfMain'
238 * @dataProvider typesProvider
240 * @param integer $type Requested type
241 * @param string $expected Expected class name
245 public function testGetypeClass($type, $expected)
247 $this->string(\Galette\Entity\PdfModel
::getTypeClass($type))->isIdenticalTo($expected);
251 * Test model replacements
255 public function testReplacements()
257 //create dynamic fields
259 'form_name' => 'adh',
260 'field_name' => 'Dynamic text field',
261 'field_perm' => DynamicField
::PERM_USER_WRITE
,
262 'field_type' => DynamicField
::TEXT
,
263 'field_required' => 1,
267 $adf = DynamicField
::getFieldType($this->zdb
, $field_data['field_type']);
269 $stored = $adf->store($field_data);
270 $error_detected = $adf->getErrors();
271 $warning_detected = $adf->getWarnings();
272 $this->boolean($stored)->isTrue(
275 $adf->getErrors() +
$adf->getWarnings()
278 $this->array($error_detected)->isEmpty(implode(' ', $adf->getErrors()));
279 $this->array($warning_detected)->isEmpty(implode(' ', $adf->getWarnings()));
282 'form_name' => 'contrib',
283 'field_form' => 'contrib',
284 'field_name' => 'Dynamic date field',
285 'field_perm' => DynamicField
::PERM_USER_WRITE
,
286 'field_type' => DynamicField
::DATE
,
287 'field_required' => 1,
291 $cdf = DynamicField
::getFieldType($this->zdb
, $field_data['field_type']);
293 $stored = $cdf->store($field_data);
294 $error_detected = $cdf->getErrors();
295 $warning_detected = $cdf->getWarnings();
296 $this->boolean($stored)->isTrue(
299 $cdf->getErrors() +
$cdf->getWarnings()
302 $this->array($error_detected)->isEmpty(implode(' ', $cdf->getErrors()));
303 $this->array($warning_detected)->isEmpty(implode(' ', $cdf->getWarnings()));
306 $rs = new \
stdClass();
307 $pk = \Galette\Entity\PdfModel
::PK
;
309 $rs->model_name
= 'Test model';
310 $rs->model_title
= 'A simple tmodel for tests';
311 $rs->model_subtitle
= 'The subtitle';
312 $rs->model_header
= null;
313 $rs->model_footer
= null;
314 $rs->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 $rs->model_styles
= null;
319 $rs->model_parent
= \Galette\Entity\PdfModel
::MAIN_MODEL
;
321 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
, $rs);
324 'nom_adh' => 'Durand',
325 'prenom_adh' => 'René',
326 'ville_adh' => 'Martel',
327 'cp_adh' => '39 069',
328 'adresse_adh' => '66, boulevard De Oliveira',
329 'email_adh' => 'meunier.josephine@ledoux.com',
330 'login_adh' => 'arthur.hamon',
331 'mdp_adh' => 'J^B-()f',
332 'mdp_adh2' => 'J^B-()f',
333 'bool_admin_adh' => false,
334 'bool_exempt_adh' => false,
335 'bool_display_info' => true,
337 'prof_adh' => 'Chef de fabrication',
339 'ddn_adh' => '1937-12-26',
340 'lieu_naissance' => 'Gonzalez-sur-Meunier',
341 'pseudo_adh' => 'ubertrand',
342 'pays_adh' => 'Antarctique',
343 'tel_adh' => '0439153432',
344 'url_adh' => 'http://bouchet.com/',
345 'activite_adh' => true,
347 'date_crea_adh' => '2020-06-10',
348 'pref_lang' => 'en_US',
349 'fingerprint' => 'FAKER' . $this->seed
,
350 'info_field_' . $adf->getId() . '_1' => 'My value (:'
352 $this->createMember($data);
353 $model->setMember($this->adh
);
355 $this->createPdfContribution($cdf);
356 $model->setContribution($this->contrib
);
358 $this->string($model->hheader
)->isIdenticalTo("<table>
360 <td id=\"pdf_assoname\"><strong id=\"asso_name\">Galette</strong><br/></td>
361 <td id=\"pdf_logo\"><img src=\"http://logo\" width=\"129\" height=\"60\"/></td>
365 $this->string($model->hfooter
)->isIdenticalTo('<div id="pdf_footer">
366 Association Galette - Galette
369 84000 Avignon - France<br/>
373 $this->string($model->hbody
)->isEqualTo(
374 'name: DURAND René login: arthur.hamon birthdate: 1937-12-26 dynlabel: Dynamic text field dynvalue: ' .
376 '- enddate: ' . $this->contrib
->end_date
. ' amount: 92 (ninety-two) dynlabel: Dynamic date field ' .
377 'dynvalue: 2020-12-03'
380 $legend = $model->getLegend();
381 $this->array($legend)
383 ->hasKeys(['main', 'member', 'contribution']);
385 $this->array($legend['main']['patterns'])->hasSize(8);
386 $this->array($legend['member']['patterns'])
388 ->hasKeys(['label_dynfield_' . $adf->getId() . '_adh', 'dynfield_' . $adf->getId() . '_adh']);
389 $this->array($legend['contribution']['patterns'])
391 ->hasKeys(['label_dynfield_' . $cdf->getId() . '_contrib', 'dynfield_' . $cdf->getId() . '_contrib']);
395 * Create member from data
397 * @param array $data Data to use to create member
401 public function createMember(array $data)
404 $check = $adh->check($data, [], []);
405 if (is_array($check)) {
408 $this->boolean($check)->isTrue();
410 $store = $adh->store();
411 $this->boolean($store)->isTrue();
416 * Create test contribution in database
418 * @param DynamicField $cdf Contribution dynamic field
422 protected function createPdfContribution($cdf)
424 $bdate = new \
DateTime(); // 2020-11-07
425 $bdate->sub(new \
DateInterval('P5M')); // 2020-06-07
426 $bdate->add(new \
DateInterval('P3D')); // 2020-06-10
428 $edate = clone $bdate;
429 $edate->add(new \
DateInterval('P1Y'));
431 $dyndate = new \
DateTime('2020-12-03 22:56:53');
434 'id_adh' => $this->adh
->id
,
435 'id_type_cotis' => 1,
436 'montant_cotis' => 92,
437 'type_paiement_cotis' => 3,
438 'info_cotis' => 'FAKER' . $this->seed
,
439 'date_enreg' => $bdate->format('Y-m-d'),
440 'date_debut_cotis' => $bdate->format('Y-m-d'),
441 'date_fin_cotis' => $edate->format('Y-m-d'),
442 'info_field_' . $cdf->getId() . '_1' => $dyndate->format('Y-m-d')
444 $this->createContrib($data);
448 * Test model storage in db
452 public function testStorage()
454 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
);
456 $orig_title = $model->title
;
457 $this->string($orig_title)->isIdenticalTo('_T("Invoice") {CONTRIBUTION_YEAR}-{CONTRIBUTION_ID}');
459 $model->title
= 'Another test';
460 $this->boolean($model->store())->isTrue();
462 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
);
463 $this->string($model->title
)->isIdenticalTo('Another test');