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\Core\test\units
;
39 use Galette\GaletteTestCase
;
42 * Password tests class
46 * @package GaletteTests
47 * @author Johan Cwiklinski <johan@x-tnd.be>
48 * @copyright 2020-2023 The Galette Team
49 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
50 * @link http://galette.tuxfamily.org
53 class Links
extends GaletteTestCase
55 //private $pass = null;
56 protected int $seed = 95842355;
57 private \Galette\Core\Links
$links;
58 private array $ids = [];
59 protected array $excluded_after_methods = ['testDuplicateLinkTarget'];
66 public function setUp(): void
70 $this->initContributionsTypes();
72 $this->links
= new \Galette\Core\
Links($this->zdb
, false);
73 $this->contrib
= new \Galette\Entity\
Contribution($this->zdb
, $this->login
);
75 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
);
76 $this->adh
->setDependencies(
78 $this->members_fields
,
84 * Cleanup after testeach test method
88 public function tearDown(): void
90 $delete = $this->zdb
->delete(\Galette\Entity\Contribution
::TABLE
);
91 $delete->where(['info_cotis' => 'FAKER' . $this->seed
]);
92 $this->zdb
->execute($delete);
94 $delete = $this->zdb
->delete(\Galette\Entity\Adherent
::TABLE
);
95 $delete->where(['fingerprint' => 'FAKER' . $this->seed
]);
96 $this->zdb
->execute($delete);
98 $delete = $this->zdb
->delete(\Galette\Core\Links
::TABLE
);
99 $this->zdb
->execute($delete);
101 $this->cleanHistory();
106 * Test new Link generation
110 public function testGenerateNewLink()
112 $links = $this->links
;
113 $this->getMemberTwo();
114 $id = $this->adh
->id
;
116 $res = $links->generateNewLink(
117 \Galette\Core\Links
::TARGET_MEMBERCARD
,
121 $this->assertNotEmpty($res);
123 $select = $this->zdb
->select(\Galette\Core\Links
::TABLE
);
124 $results = $this->zdb
->execute($select);
125 $this->assertSame(1, $results->count());
128 $links->isHashValid($res, 'phoarau@tele2.fr'),
130 \Galette\Core\Links
::TARGET_MEMBERCARD
,
135 $this->assertFalse($links->isHashValid($res, 'any@mail.com'));
136 $this->assertFalse($links->isHashValid(base64_encode('sthingthatisnotahash'), 'phoarau@tele2.fr'));
138 $this->createContribution();
139 $cid = $this->contrib
->id
;
140 $res = $links->generateNewLink(
141 \Galette\Core\Links
::TARGET_INVOICE
,
145 $this->assertNotEmpty($res);
147 $links->isHashValid($res, 'phoarau@tele2.fr'),
149 \Galette\Core\Links
::TARGET_INVOICE
,
156 * Test expired is invalid
160 public function testExpiredValidate()
162 $links = $this->links
;
163 $this->getMemberTwo();
164 $id = $this->adh
->id
;
166 $res = $links->generateNewLink(
167 \Galette\Core\Links
::TARGET_MEMBERCARD
,
171 $this->assertNotEmpty($res);
174 $links->isHashValid($res, 'phoarau@tele2.fr'),
176 \Galette\Core\Links
::TARGET_MEMBERCARD
,
181 $select = $this->zdb
->select(\Galette\Core\Links
::TABLE
);
182 $results = $this->zdb
->execute($select);
183 $this->assertSame(1, $results->count());
185 $update = $this->zdb
->update(\Galette\Core\Links
::TABLE
);
186 $old_date = new \
DateTime();
187 $old_date->sub(new \
DateInterval('P2W'));
189 ->set(['creation_date' => $old_date->format('Y-m-d')])
190 ->where(['hash' => base64_decode($res)]);
191 $this->zdb
->execute($update);
193 $this->assertFalse($links->isHashValid($res, 'phoarau@tele2.fr'));
201 public function testCleanExpired()
203 $date = new \
DateTime();
204 $date->sub(new \
DateInterval('PT48H'));
206 $insert = $this->zdb
->insert(\Galette\Core\Links
::TABLE
);
209 'hash' => 'Not expired link',
210 'creation_date' => $date->format('Y-m-d'),
211 'target' => \Galette\Core\Links
::TARGET_MEMBERCARD
,
215 $this->zdb
->execute($insert);
217 $date->sub(new \
DateInterval('P1W'));
218 $insert = $this->zdb
->insert(\Galette\Core\Links
::TABLE
);
221 'hash' => 'Expired link',
222 'creation_date' => $date->format('Y-m-d'),
223 'target' => \Galette\Core\Links
::TARGET_MEMBERCARD
,
227 $this->zdb
->execute($insert);
229 $select = $this->zdb
->select(\Galette\Core\Links
::TABLE
);
230 $results = $this->zdb
->execute($select);
231 $this->assertSame(2, $results->count());
233 $links = new \Galette\Core\
Links($this->zdb
, true);
235 $results = $this->zdb
->execute($select);
236 $result = $results->current();
237 $this->assertSame(1, $results->count());
238 $this->assertSame('Not expired link', $result['hash']);
242 * Test duplicate target
246 public function testDuplicateLinkTarget()
248 $date = new \
DateTime();
249 $date->sub(new \
DateInterval('PT48H'));
251 $insert = $this->zdb
->insert(\Galette\Core\Links
::TABLE
);
254 'hash' => 'Unique link',
255 'creation_date' => $date->format('Y-m-d'),
256 'target' => \Galette\Core\Links
::TARGET_MEMBERCARD
,
260 $this->zdb
->execute($insert);
262 $date->sub(new \
DateInterval('PT1H'));
264 $insert = $this->zdb
->insert(\Galette\Core\Links
::TABLE
);
267 'hash' => 'Unique link (but we did not know before)',
268 'creation_date' => $date->format('Y-m-d'),
269 'target' => \Galette\Core\Links
::TARGET_MEMBERCARD
,
274 $this->expectExceptionMessage('Duplicate entry');
275 $this->zdb
->execute($insert);
279 * Create test contribution in database
283 protected function createContribution()
285 $now = new \
DateTime(); // 2020-11-07
286 $begin_date = clone $now;
287 $begin_date->sub(new \
DateInterval('P1Y')); // 2019-11-07
288 $begin_date->sub(new \
DateInterval('P6M')); // 2019-05-07
289 $begin_date->add(new \
DateInterval('P13D')); // 2019-05-20
291 $due_date = clone $begin_date;
292 $due_date->sub(new \
DateInterval('P1D'));
293 $due_date->add(new \
DateInterval('P1Y'));
296 'id_adh' => $this->adh
->id
,
297 'id_type_cotis' => 3,
298 'montant_cotis' => 111,
299 'type_paiement_cotis' => 6,
300 'info_cotis' => 'FAKER' . $this->seed
,
301 'date_enreg' => $begin_date->format('Y-m-d'),
302 'date_debut_cotis' => $begin_date->format('Y-m-d'),
303 'date_fin_cotis' => $due_date->format('Y-m-d'),
305 $this->createContrib($data);
306 $this->checkContribExpected();
310 * Check contributions expecteds
312 * @param Contribution $contrib Contribution instance, if any
313 * @param array $new_expecteds Changes on expected values
317 protected function checkContribExpected($contrib = null, $new_expecteds = [])
319 if ($contrib === null) {
320 $contrib = $this->contrib
;
323 $begin_date = $contrib->raw_begin_date
;
325 $due_date = clone $begin_date;
326 $due_date->sub(new \
DateInterval('P1D'));
327 $due_date->add(new \
DateInterval('P1Y'));
329 $this->assertInstanceOf('DateTime', $contrib->raw_date
);
330 $this->assertInstanceOf('DateTime', $contrib->raw_begin_date
);
331 $this->assertInstanceOf('DateTime', $contrib->raw_end_date
);
334 'id_adh' => "{$this->adh->id}",
335 'id_type_cotis' => 3,
336 'montant_cotis' => '111',
337 'type_paiement_cotis' => '6',
338 'info_cotis' => 'FAKER' . $this->seed
,
339 'date_fin_cotis' => $due_date->format('Y-m-d')
341 $expecteds = array_merge($expecteds, $new_expecteds);
343 $this->assertSame($expecteds['date_fin_cotis'], $contrib->raw_end_date
->format('Y-m-d'));
345 foreach ($expecteds as $key => $value) {
346 $property = $this->contrib
->fields
[$key]['propname'];
348 case \Galette\Entity\ContributionsTypes
::PK
:
349 $ct = $this->contrib
->type
;
350 if ($ct instanceof \Galette\Entity\ContributionsTypes
) {
351 $this->assertSame($value, (int)$ct->id
);
353 $this->assertSame($value, $ct);
357 $this->assertEquals($contrib->$property, $value, $property);
362 //load member from db
363 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
364 //member is now up-to-date
365 $this->assertSame('active-account cotis-late', $this->adh
->getRowClass());
366 $this->assertSame($this->contrib
->end_date
, $this->adh
->due_date
);
367 $this->assertFalse($this->adh
->isUp2Date());