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
;
41 use Galette\Entity\Adherent
;
42 use Galette\DynamicFields\DynamicField
;
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 atoum
65 private $seed = 95842354;
70 private $members_fields;
75 * @param string $testMethod Calling method
79 public function beforeTestMethod($testMethod)
81 $this->zdb
= new \Galette\Core\
Db();
82 $this->preferences
= new \Galette\Core\
Preferences($this->zdb
);
83 $this->i18n
= new \Galette\Core\
I18n(
84 \Galette\Core\I18n
::DEFAULT_LANG
86 $this->session
= new \RKA\
Session();
87 $this->login
= new \Galette\Core\
Login($this->zdb
, $this->i18n
, $this->session
);
89 $models = new \Galette\Repository\
PdfModels($this->zdb
, $this->preferences
, $this->login
);
90 $res = $models->installInit(false);
91 $this->boolean($res)->isTrue();
93 $container = new class {
97 * @param string $name Param name
101 public function get($name)
103 $router = new class {
107 * @param sttring $name Route name
111 public function pathFor($name)
119 $_SERVER['HTTP_HOST'] = '';
120 $this->container
= $container;
122 $this->history
= new \Galette\Core\
History($this->zdb
, $this->login
, $this->preferences
);
124 include_once GALETTE_ROOT
. 'includes/fields_defs/members_fields.php';
125 $this->members_fields
= $members_fields;
127 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
);
128 $this->adh
->setDependencies(
130 $this->members_fields
,
138 * @param string $testMethod Calling method
142 public function afterTestMethod($testMethod)
144 $delete = $this->zdb
->delete(\Galette\Entity\Contribution
::TABLE
);
145 $delete->where(['info_cotis' => 'FAKER' . $this->seed
]);
146 $this->zdb
->execute($delete);
147 $delete = $this->zdb
->delete(\Galette\Entity\Adherent
::TABLE
);
148 $this->zdb
->execute($delete);
149 $delete = $this->zdb
->delete(\Galette\Entity\DynamicFieldsHandle
::TABLE
);
150 $this->zdb
->execute($delete);
151 $delete = $this->zdb
->delete(DynamicField
::TABLE
);
152 $this->zdb
->execute($delete);
153 //cleanup dynamic translations
154 $delete = $this->zdb
->delete(\Galette\Core\L10n
::TABLE
);
157 'Dynamic choice field',
158 'Dynamic date field',
162 $this->zdb
->execute($delete);
164 /*if ($this->contents_table !== null) {
165 $this->zdb->drop($this->contents_table);
170 * Test expected patterns
174 public function testExpectedPatterns()
176 global $container, $zdb;
177 $zdb = $this->zdb
; //globals '(
178 $container = $this->container
; //globals '(
179 $model = new class ($this->zdb
, $this->preferences
, 1) extends \Galette\Entity\PdfModel
{
183 'asso_name' => '/{ASSO_NAME}/',
184 'asso_slogan' => '/{ASSO_SLOGAN}/',
185 'asso_address' => '/{ASSO_ADDRESS}/',
186 'asso_address_multi' => '/{ASSO_ADDRESS_MULTI}/',
187 'asso_website' => '/{ASSO_WEBSITE}/',
188 'asso_logo' => '/{ASSO_LOGO}/',
189 'date_now' => '/{DATE_NOW}/'
191 $this->array($model->patterns
)->isIdenticalTo($main_expected);
193 $model = new \Galette\Entity\
PdfMain($this->zdb
, $this->preferences
);
194 $this->array($model->patterns
)->isIdenticalTo($main_expected);
196 $expected = $main_expected +
[
197 'adh_title' => '/{TITLE_ADH}/',
198 'adh_id' => '/{ID_ADH}/',
199 'adh_name' => '/{NAME_ADH}/',
200 'adh_last_name' => '/{LAST_NAME_ADH}/',
201 'adh_first_name' => '/{FIRST_NAME_ADH}/',
202 'adh_nickname' => '/{NICKNAME_ADH}/',
203 'adh_gender' => '/{GENDER_ADH}/',
204 'adh_birth_date' => '/{ADH_BIRTH_DATE}/',
205 'adh_birth_place' => '/{ADH_BIRTH_PLACE}/',
206 'adh_profession' => '/{PROFESSION_ADH}/',
207 'adh_company' => '/{COMPANY_ADH}/',
208 'adh_address' => '/{ADDRESS_ADH}/',
209 'adh_zip' => '/{ZIP_ADH}/',
210 'adh_town' => '/{TOWN_ADH}/',
211 'adh_country' => '/{COUNTRY_ADH}/',
212 'adh_phone' => '/{PHONE_ADH}/',
213 'adh_mobile' => '/{MOBILE_ADH}/',
214 'adh_email' => '/{EMAIL_ADH}/',
215 'adh_login' => '/{LOGIN_ADH}/',
216 'adh_main_group' => '/{GROUP_ADH}/',
217 'adh_groups' => '/{GROUPS_ADH}/'
219 $model = new \Galette\Entity\
PdfAdhesionFormModel($this->zdb
, $this->preferences
);
220 $this->array($model->patterns
)->isIdenticalTo($expected);
222 $expected = $expected +
[
223 'contrib_label' => '/{CONTRIB_LABEL}/',
224 'contrib_amount' => '/{CONTRIB_AMOUNT}/',
225 'contrib_amount_letters' => '/{CONTRIB_AMOUNT_LETTERS}/',
226 'contrib_date' => '/{CONTRIB_DATE}/',
227 'contrib_year' => '/{CONTRIB_YEAR}/',
228 'contrib_comment' => '/{CONTRIB_COMMENT}/',
229 'contrib_bdate' => '/{CONTRIB_BEGIN_DATE}/',
230 'contrib_edate' => '/{CONTRIB_END_DATE}/',
231 'contrib_id' => '/{CONTRIB_ID}/',
232 'contrib_payment' => '/{CONTRIB_PAYMENT_TYPE}/',
233 '_contrib_label' => '/{CONTRIBUTION_LABEL}/',
234 '_contrib_amount' => '/{CONTRIBUTION_AMOUNT}/',
235 '_contrib_amount_letters' => '/{CONTRIBUTION_AMOUNT_LETTERS}/',
236 '_contrib_date' => '/{CONTRIBUTION_DATE}/',
237 '_contrib_year' => '/{CONTRIBUTION_YEAR}/',
238 '_contrib_comment' => '/{CONTRIBUTION_COMMENT}/',
239 '_contrib_bdate' => '/{CONTRIBUTION_BEGIN_DATE}/',
240 '_contrib_edate' => '/{CONTRIBUTION_END_DATE}/',
241 '_contrib_id' => '/{CONTRIBUTION_ID}/',
242 '_contrib_payment' => '/{CONTRIBUTION_PAYMENT_TYPE}/'
244 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
);
245 $this->array($model->patterns
)->isIdenticalTo($expected);
247 $model = new \Galette\Entity\
PdfReceipt($this->zdb
, $this->preferences
);
248 $this->array($model->patterns
)->isIdenticalTo($expected);
256 protected function typesProvider(): array
260 'type' => \Galette\Entity\PdfModel
::MAIN_MODEL
,
261 'expected' => 'Galette\Entity\PdfMain'
263 'type' => \Galette\Entity\PdfModel
::INVOICE_MODEL
,
264 'expected' => 'Galette\Entity\PdfInvoice'
266 'type' => \Galette\Entity\PdfModel
::RECEIPT_MODEL
,
267 'expected' => 'Galette\Entity\PdfReceipt'
269 'type' => \Galette\Entity\PdfModel
::ADHESION_FORM_MODEL
,
270 'expected' => 'Galette\Entity\PdfAdhesionFormModel'
273 'expected' => 'Galette\Entity\PdfMain'
280 * @dataProvider typesProvider
282 * @param integer $type Requested type
283 * @param string $expected Expected class name
287 public function testGetypeClass($type, $expected)
289 $this->string(\Galette\Entity\PdfModel
::getTypeClass($type))->isIdenticalTo($expected);
293 * Test model replacements
297 public function testReplacements()
299 global $container, $zdb;
300 $zdb = $this->zdb
; //globals '(
301 $container = $this->container
; //globals '(
303 //create dynamic fields
305 'form_name' => 'adh',
306 'field_name' => 'Dynamic text field',
307 'field_perm' => DynamicField
::PERM_USER_WRITE
,
308 'field_type' => DynamicField
::TEXT
,
309 'field_required' => 1,
313 $adf = DynamicField
::getFieldType($this->zdb
, $field_data['field_type']);
315 $stored = $adf->store($field_data);
316 $error_detected = $adf->getErrors();
317 $warning_detected = $adf->getWarnings();
318 $this->boolean($stored)->isTrue(
321 $adf->getErrors() +
$adf->getWarnings()
324 $this->array($error_detected)->isEmpty(implode(' ', $adf->getErrors()));
325 $this->array($warning_detected)->isEmpty(implode(' ', $adf->getWarnings()));
328 'form_name' => 'contrib',
329 'field_form' => 'contrib',
330 'field_name' => 'Dynamic date field',
331 'field_perm' => DynamicField
::PERM_USER_WRITE
,
332 'field_type' => DynamicField
::DATE
,
333 'field_required' => 1,
337 $cdf = DynamicField
::getFieldType($this->zdb
, $field_data['field_type']);
339 $stored = $cdf->store($field_data);
340 $error_detected = $cdf->getErrors();
341 $warning_detected = $cdf->getWarnings();
342 $this->boolean($stored)->isTrue(
345 $cdf->getErrors() +
$cdf->getWarnings()
348 $this->array($error_detected)->isEmpty(implode(' ', $cdf->getErrors()));
349 $this->array($warning_detected)->isEmpty(implode(' ', $cdf->getWarnings()));
352 $rs = new \
stdClass();
353 $pk = \Galette\Entity\PdfModel
::PK
;
355 $rs->model_name
= 'Test model';
356 $rs->model_title
= 'A simple tmodel for tests';
357 $rs->model_subtitle
= 'The subtitle';
358 $rs->model_header
= null;
359 $rs->model_footer
= null;
360 $rs->model_body
= 'name: {NAME_ADH} login: {LOGIN_ADH} birthdate: {ADH_BIRTH_DATE} dynlabel: {LABEL_DYNFIELD_' .
361 $adf->getId() . '_ADH} dynvalue: {INPUT_DYNFIELD_' . $adf->getId() . '_ADH} ' .
362 '- enddate: {CONTRIB_END_DATE} amount: {CONTRIB_AMOUNT} ({CONTRIB_AMOUNT_LETTERS}) dynlabel: ' .
363 '{LABEL_DYNFIELD_' . $cdf->getId() . '_CONTRIB} dynvalue: {INPUT_DYNFIELD_' . $cdf->getId() . '_CONTRIB}';
364 $rs->model_styles
= null;
365 $rs->model_parent
= \Galette\Entity\PdfModel
::MAIN_MODEL
;
367 $model = new \Galette\Entity\
PdfInvoice($this->zdb
, $this->preferences
, $rs);
369 $this->string($model->hheader
)->isIdenticalTo("<table>
371 <td id=\"pdf_assoname\"><strong id=\"asso_name\">Galette</strong><br/></td>
372 <td id=\"pdf_logo\"><img src=\"http://\" width=\"129\" height=\"60\"/></td>
376 $this->string($model->hfooter
)->isIdenticalTo('<div id="pdf_footer">
377 Association Galette - Galette
380 84000 Avignon - France<br/>
385 'nom_adh' => 'Durand',
386 'prenom_adh' => 'René',
387 'ville_adh' => 'Martel',
388 'cp_adh' => '39 069',
389 'adresse_adh' => '66, boulevard De Oliveira',
390 'email_adh' => 'meunier.josephine@ledoux.com',
391 'login_adh' => 'arthur.hamon',
392 'mdp_adh' => 'J^B-()f',
393 'mdp_adh2' => 'J^B-()f',
394 'bool_admin_adh' => false,
395 'bool_exempt_adh' => false,
396 'bool_display_info' => true,
398 'prof_adh' => 'Chef de fabrication',
400 'ddn_adh' => '1937-12-26',
401 'lieu_naissance' => 'Gonzalez-sur-Meunier',
402 'pseudo_adh' => 'ubertrand',
403 'pays_adh' => 'Antarctique',
404 'tel_adh' => '0439153432',
405 'url_adh' => 'http://bouchet.com/',
406 'activite_adh' => true,
408 'date_crea_adh' => '2020-06-10',
409 'pref_lang' => 'en_US',
410 'fingerprint' => 'FAKER' . $this->seed
,
411 'info_field_' . $adf->getId() . '_1' => 'My value (:'
413 $this->createMember($data);
414 $model->setMember($this->adh
);
416 $this->createContribution($cdf);
417 $model->setContribution($this->contrib
);
419 $this->string($model->hbody
)->isEqualTo(
420 'name: DURAND René login: arthur.hamon birthdate: 1937-12-26 dynlabel: Dynamic text field dynvalue: ' .
421 '<textarea id="Dynamic text field" name="Dynamic text field" value="My value (:"/> ' .
422 '- enddate: ' . $this->contrib
->end_date
. ' amount: 92 (ninety-two) dynlabel: Dynamic date field ' .
423 'dynvalue: <input type="text" name="Dynamic date field" value="2020-12-03" size="10" />'
426 $legend = $model->getLegend();
427 $this->array($legend)
429 ->hasKeys(['main', 'member', 'contribution']);
431 $this->array($legend['main']['patterns'])->hasSize(7);
432 $this->array($legend['member']['patterns'])
434 ->hasKeys(['LABEL_DYNFIELD_' . $adf->getId() . '_ADH', 'INPUT_DYNFIELD_' . $adf->getId() . '_ADH']);
435 $this->array($legend['contribution']['patterns'])
437 ->hasKeys(['LABEL_DYNFIELD_' . $cdf->getId() . '_CONTRIB', 'INPUT_DYNFIELD_' . $cdf->getId() . '_CONTRIB']);
441 * Create member from data
443 * @param array $data Data to use to create member
445 * @return \Galette\Entity\Adherent
447 public function createMember(array $data)
450 $check = $adh->check($data, [], []);
451 if (is_array($check)) {
454 $this->boolean($check)->isTrue();
456 $store = $adh->store();
457 $this->boolean($store)->isTrue();
461 * Create test contribution in database
463 * @param DynamicField $cdf Contribution dynamic field
467 private function createContribution($cdf)
469 $bdate = new \
DateTime(); // 2020-11-07
470 $bdate->sub(new \
DateInterval('P5M')); // 2020-06-07
471 $bdate->add(new \
DateInterval('P3D')); // 2020-06-10
473 $edate = clone $bdate;
474 $edate->add(new \
DateInterval('P1Y'));
476 $dyndate = new \
DateTime('2020-12-03 22:56:53');
479 'id_adh' => $this->adh
->id
,
480 'id_type_cotis' => 1,
481 'montant_cotis' => 92,
482 'type_paiement_cotis' => 3,
483 'info_cotis' => 'FAKER' . $this->seed
,
484 'date_enreg' => $bdate->format('Y-m-d'),
485 'date_debut_cotis' => $bdate->format('Y-m-d'),
486 'date_fin_cotis' => $edate->format('Y-m-d'),
487 'info_field_' . $cdf->getId() . '_1' => $dyndate->format('Y-m-d')
489 $this->createContrib($data);
493 * Create contribution from data
495 * @param array $data Data to use to create contribution
497 * @return \Galette\Entity\Contribution
499 public function createContrib(array $data)
501 $this->contrib
= new \Galette\Entity\
Contribution($this->zdb
, $this->login
);
502 $contrib = $this->contrib
;
503 $check = $contrib->check($data, [], []);
504 if (is_array($check)) {
507 $this->boolean($check)->isTrue();
509 $store = $contrib->store();
510 $this->boolean($store)->isTrue();