3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2020-2021 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-2021 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-2021 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 $seed = 95842355;
59 protected $excluded_after_methods = ['testDuplicateLinkTarget'];
64 * @param string $method Method name
68 public function beforeTestMethod($method)
70 parent
::beforeTestMethod($method);
72 $this->initContributionsTypes();
74 $this->links
= new \Galette\Core\
Links($this->zdb
, false);
75 $this->contrib
= new \Galette\Entity\
Contribution($this->zdb
, $this->login
);
77 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
);
78 $this->adh
->setDependencies(
80 $this->members_fields
,
86 * Cleanup after testeach test method
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);
100 $delete = $this->zdb
->delete(\Galette\Entity\Adherent
::TABLE
);
101 $delete->where(['fingerprint' => 'FAKER' . $this->seed
]);
102 $this->zdb
->execute($delete);
104 $delete = $this->zdb
->delete(\Galette\Core\Links
::TABLE
);
105 $this->zdb
->execute($delete);
107 $this->cleanHistory();
111 * Test new Link generation
115 public function testGenerateNewLink()
117 $links = $this->links
;
118 $this->getMemberTwo();
119 $id = $this->adh
->id
;
121 $res = $links->generateNewLink(
122 \Galette\Core\Links
::TARGET_MEMBERCARD
,
126 $this->string($res)->isNotEmpty();
128 $select = $this->zdb
->select(\Galette\Core\Links
::TABLE
);
129 $results = $this->zdb
->execute($select);
130 $this->integer($results->count())->isIdenticalTo(1);
132 $this->array($links->isHashValid($res, 'phoarau@tele2.fr'))->isIdenticalTo([
133 \Galette\Core\Links
::TARGET_MEMBERCARD
,
137 $this->boolean($links->isHashValid($res, 'any@mail.com'))->isFalse();
138 $this->boolean($links->isHashValid(base64_encode('sthingthatisnotahash'), 'phoarau@tele2.fr'))->isFalse();
140 $this->createContribution();
141 $cid = $this->contrib
->id
;
142 $res = $links->generateNewLink(
143 \Galette\Core\Links
::TARGET_INVOICE
,
147 $this->string($res)->isNotEmpty();
148 $this->array($links->isHashValid($res, 'phoarau@tele2.fr'))->isIdenticalTo([
149 \Galette\Core\Links
::TARGET_INVOICE
,
155 * Test expired is invalid
159 public function testExpiredValidate()
161 $links = $this->links
;
162 $this->getMemberTwo();
163 $id = $this->adh
->id
;
165 $res = $links->generateNewLink(
166 \Galette\Core\Links
::TARGET_MEMBERCARD
,
170 $this->string($res)->isNotEmpty();
172 $this->array($links->isHashValid($res, 'phoarau@tele2.fr'))->isIdenticalTo([
173 \Galette\Core\Links
::TARGET_MEMBERCARD
,
177 $select = $this->zdb
->select(\Galette\Core\Links
::TABLE
);
178 $results = $this->zdb
->execute($select);
179 $this->integer($results->count())->isIdenticalTo(1);
181 $update = $this->zdb
->update(\Galette\Core\Links
::TABLE
);
182 $old_date = new \
DateTime();
183 $old_date->sub(new \
DateInterval('P2W'));
185 ->set(['creation_date' => $old_date->format('Y-m-d')])
186 ->where(['hash' => base64_decode($res)]);
187 $this->zdb
->execute($update);
189 $this->boolean($links->isHashValid($res, 'phoarau@tele2.fr'))->isFalse();
197 public function testCleanExpired()
199 $date = new \
DateTime();
200 $date->sub(new \
DateInterval('PT48H'));
202 $insert = $this->zdb
->insert(\Galette\Core\Links
::TABLE
);
205 'hash' => 'Not expired link',
206 'creation_date' => $date->format('Y-m-d'),
207 'target' => \Galette\Core\Links
::TARGET_MEMBERCARD
,
211 $this->zdb
->execute($insert);
213 $date->sub(new \
DateInterval('P1W'));
214 $insert = $this->zdb
->insert(\Galette\Core\Links
::TABLE
);
217 'hash' => 'Expired link',
218 'creation_date' => $date->format('Y-m-d'),
219 'target' => \Galette\Core\Links
::TARGET_MEMBERCARD
,
223 $this->zdb
->execute($insert);
225 $select = $this->zdb
->select(\Galette\Core\Links
::TABLE
);
226 $results = $this->zdb
->execute($select);
227 $this->integer($results->count())->isIdenticalTo(2);
229 $links = new \Galette\Core\
Links($this->zdb
, true);
231 $results = $this->zdb
->execute($select);
232 $result = $results->current();
233 $this->integer($results->count())->isIdenticalTo(1);
234 $this->string($result['hash'])->isIdenticalTo('Not expired link');
238 * Test duplicate target
242 public function testDuplicateLinkTarget()
244 $date = new \
DateTime();
245 $date->sub(new \
DateInterval('PT48H'));
247 $insert = $this->zdb
->insert(\Galette\Core\Links
::TABLE
);
250 'hash' => 'Unique link',
251 'creation_date' => $date->format('Y-m-d'),
252 'target' => \Galette\Core\Links
::TARGET_MEMBERCARD
,
256 $this->zdb
->execute($insert);
258 $date->sub(new \
DateInterval('PT1H'));
260 $insert = $this->zdb
->insert(\Galette\Core\Links
::TABLE
);
263 'hash' => 'Unique link (but we did not know before)',
264 'creation_date' => $date->format('Y-m-d'),
265 'target' => \Galette\Core\Links
::TARGET_MEMBERCARD
,
271 function () use ($insert) {
272 $this->zdb
->execute($insert);
275 ->hasMessage('Duplicate entry');
279 * Create test contribution in database
283 protected function createContribution()
285 $bdate = new \
DateTime(); // 2020-11-07
286 $bdate->sub(new \
DateInterval('P1Y')); // 2019-11-07
287 $bdate->sub(new \
DateInterval('P6M')); // 2019-05-07
288 $bdate->add(new \
DateInterval('P13D')); // 2019-05-20
290 $edate = clone $bdate;
291 $edate->add(new \
DateInterval('P1Y'));
294 'id_adh' => $this->adh
->id
,
295 'id_type_cotis' => 3,
296 'montant_cotis' => 111,
297 'type_paiement_cotis' => 6,
298 'info_cotis' => 'FAKER' . $this->seed
,
299 'date_enreg' => $bdate->format('Y-m-d'),
300 'date_debut_cotis' => $bdate->format('Y-m-d'),
301 'date_fin_cotis' => $edate->format('Y-m-d'),
303 $this->createContrib($data);
304 $this->checkContribExpected();
308 * Check contributions expecteds
310 * @param Contribution $contrib Contribution instance, if any
311 * @param array $new_expecteds Changes on expected values
315 protected function checkContribExpected($contrib = null, $new_expecteds = [])
317 if ($contrib === null) {
318 $contrib = $this->contrib
;
321 $date_begin = $contrib->raw_begin_date
;
322 $date_end = clone $date_begin;
323 $date_end->add(new \
DateInterval('P1Y'));
325 $this->object($contrib->raw_date
)->isInstanceOf('DateTime');
326 $this->object($contrib->raw_begin_date
)->isInstanceOf('DateTime');
327 $this->object($contrib->raw_end_date
)->isInstanceOf('DateTime');
330 'id_adh' => "{$this->adh->id}",
331 'id_type_cotis' => 3,
332 'montant_cotis' => '111',
333 'type_paiement_cotis' => '6',
334 'info_cotis' => 'FAKER' . $this->seed
,
335 'date_fin_cotis' => $date_end->format('Y-m-d')
337 $expecteds = array_merge($expecteds, $new_expecteds);
339 $this->string($contrib->raw_end_date
->format('Y-m-d'))->isIdenticalTo($expecteds['date_fin_cotis']);
341 foreach ($expecteds as $key => $value) {
342 $property = $this->contrib
->fields
[$key]['propname'];
344 case \Galette\Entity\ContributionsTypes
::PK
:
345 $ct = $this->contrib
->type
;
346 if ($ct instanceof \Galette\Entity\ContributionsTypes
) {
347 $this->integer((int)$ct->id
)->isIdenticalTo($value);
349 $this->integer($ct)->isIdenticalTo($value);
353 $this->variable($contrib->$property)->isEqualTo($value, $property);
358 //load member from db
359 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
360 //member is now up-to-date
361 $this->string($this->adh
->getRowClass())->isIdenticalTo('active cotis-late');
362 $this->string($this->adh
->due_date
)->isIdenticalTo($this->contrib
->end_date
);
363 $this->boolean($this->adh
->isUp2Date())->isFalse();