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
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 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;
63 * @param string $testMethod Method name
67 public function beforeTestMethod($testMethod)
69 parent
::beforeTestMethod($testMethod);
71 $this->initContributionsTypes();
73 $this->links
= new \Galette\Core\
Links($this->zdb
, false);
74 $this->contrib
= new \Galette\Entity\
Contribution($this->zdb
, $this->login
);
76 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
);
77 $this->adh
->setDependencies(
79 $this->members_fields
,
85 * Cleanup after testeach test method
87 * @param string $method Calling method
91 public function afterTestMethod($method)
93 $delete = $this->zdb
->delete(\Galette\Entity\Contribution
::TABLE
);
94 $delete->where(['info_cotis' => 'FAKER' . $this->seed
]);
95 $this->zdb
->execute($delete);
97 $delete = $this->zdb
->delete(\Galette\Entity\Adherent
::TABLE
);
98 $delete->where(['fingerprint' => 'FAKER' . $this->seed
]);
99 $this->zdb
->execute($delete);
101 $delete = $this->zdb
->delete(\Galette\Core\Links
::TABLE
);
102 $this->zdb
->execute($delete);
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->string($res)->isNotEmpty();
123 $select = $this->zdb
->select(\Galette\Core\Links
::TABLE
);
124 $results = $this->zdb
->execute($select);
125 $this->integer($results->count())->isIdenticalTo(1);
127 $this->array($links->isHashValid($res, 'phoarau@tele2.fr'))->isIdenticalTo([
128 \Galette\Core\Links
::TARGET_MEMBERCARD
,
132 $this->boolean($links->isHashValid($res, 'any@mail.com'))->isFalse();
133 $this->boolean($links->isHashValid(base64_encode('sthingthatisnotahash'), 'phoarau@tele2.fr'))->isFalse();
135 $this->createContribution();
136 $cid = $this->contrib
->id
;
137 $res = $links->generateNewLink(
138 \Galette\Core\Links
::TARGET_INVOICE
,
142 $this->string($res)->isNotEmpty();
143 $this->array($links->isHashValid($res, 'phoarau@tele2.fr'))->isIdenticalTo([
144 \Galette\Core\Links
::TARGET_INVOICE
,
150 * Test expired is invalid
154 public function testExpiredValidate()
156 $links = $this->links
;
157 $this->getMemberTwo();
158 $id = $this->adh
->id
;
160 $res = $links->generateNewLink(
161 \Galette\Core\Links
::TARGET_MEMBERCARD
,
165 $this->string($res)->isNotEmpty();
167 $this->array($links->isHashValid($res, 'phoarau@tele2.fr'))->isIdenticalTo([
168 \Galette\Core\Links
::TARGET_MEMBERCARD
,
172 $select = $this->zdb
->select(\Galette\Core\Links
::TABLE
);
173 $results = $this->zdb
->execute($select);
174 $this->integer($results->count())->isIdenticalTo(1);
176 $update = $this->zdb
->update(\Galette\Core\Links
::TABLE
);
177 $old_date = new \
DateTime();
178 $old_date->sub(new \
DateInterval('P2W'));
180 ->set(['creation_date' => $old_date->format('Y-m-d')])
181 ->where(['hash' => base64_decode($res)]);
182 $this->zdb
->execute($update);
184 $this->boolean($links->isHashValid($res, 'phoarau@tele2.fr'))->isFalse();
192 public function testCleanExpired()
194 $date = new \
DateTime();
195 $date->sub(new \
DateInterval('PT48H'));
197 $insert = $this->zdb
->insert(\Galette\Core\Links
::TABLE
);
200 'hash' => 'Not expired link',
201 'creation_date' => $date->format('Y-m-d'),
202 'target' => \Galette\Core\Links
::TARGET_MEMBERCARD
,
206 $this->zdb
->execute($insert);
208 $date->sub(new \
DateInterval('P1W'));
209 $insert = $this->zdb
->insert(\Galette\Core\Links
::TABLE
);
212 'hash' => 'Expired link',
213 'creation_date' => $date->format('Y-m-d'),
214 'target' => \Galette\Core\Links
::TARGET_MEMBERCARD
,
218 $this->zdb
->execute($insert);
220 $select = $this->zdb
->select(\Galette\Core\Links
::TABLE
);
221 $results = $this->zdb
->execute($select);
222 $this->integer($results->count())->isIdenticalTo(2);
224 $links = new \Galette\Core\
Links($this->zdb
, true);
226 $results = $this->zdb
->execute($select);
227 $result = $results->current();
228 $this->integer($results->count())->isIdenticalTo(1);
229 $this->string($result['hash'])->isIdenticalTo('Not expired link');
233 * Test duplicate target
237 public function testDuplicateLinkTarget()
239 $date = new \
DateTime();
240 $date->sub(new \
DateInterval('PT48H'));
242 $insert = $this->zdb
->insert(\Galette\Core\Links
::TABLE
);
245 'hash' => 'Unique link',
246 'creation_date' => $date->format('Y-m-d'),
247 'target' => \Galette\Core\Links
::TARGET_MEMBERCARD
,
251 $this->zdb
->execute($insert);
253 $date->sub(new \
DateInterval('PT1H'));
255 $insert = $this->zdb
->insert(\Galette\Core\Links
::TABLE
);
258 'hash' => 'Unique link (but we did not know before)',
259 'creation_date' => $date->format('Y-m-d'),
260 'target' => \Galette\Core\Links
::TARGET_MEMBERCARD
,
266 function () use ($insert) {
267 $this->zdb
->execute($insert);
270 ->hasMessage('Duplicate entry');
274 * Create test contribution in database
278 protected function createContribution()
280 $bdate = new \
DateTime(); // 2020-11-07
281 $bdate->sub(new \
DateInterval('P1Y')); // 2019-11-07
282 $bdate->sub(new \
DateInterval('P6M')); // 2019-05-07
283 $bdate->add(new \
DateInterval('P13D')); // 2019-05-20
285 $edate = clone $bdate;
286 $edate->add(new \
DateInterval('P1Y'));
289 'id_adh' => $this->adh
->id
,
290 'id_type_cotis' => 3,
291 'montant_cotis' => 111,
292 'type_paiement_cotis' => 6,
293 'info_cotis' => 'FAKER' . $this->seed
,
294 'date_enreg' => $bdate->format('Y-m-d'),
295 'date_debut_cotis' => $bdate->format('Y-m-d'),
296 'date_fin_cotis' => $edate->format('Y-m-d'),
298 $this->createContrib($data);
299 $this->checkContribExpected();
303 * Check contributions expecteds
305 * @param Contribution $contrib Contribution instance, if any
306 * @param array $new_expecteds Changes on expected values
310 protected function checkContribExpected($contrib = null, $new_expecteds = [])
312 if ($contrib === null) {
313 $contrib = $this->contrib
;
316 $date_begin = $contrib->raw_begin_date
;
317 $date_end = clone $date_begin;
318 $date_end->add(new \
DateInterval('P1Y'));
320 $this->object($contrib->raw_date
)->isInstanceOf('DateTime');
321 $this->object($contrib->raw_begin_date
)->isInstanceOf('DateTime');
322 $this->object($contrib->raw_end_date
)->isInstanceOf('DateTime');
325 'id_adh' => "{$this->adh->id}",
326 'id_type_cotis' => 3,
327 'montant_cotis' => '111',
328 'type_paiement_cotis' => '6',
329 'info_cotis' => 'FAKER' . $this->seed
,
330 'date_fin_cotis' => $date_end->format('Y-m-d')
332 $expecteds = array_merge($expecteds, $new_expecteds);
334 $this->string($contrib->raw_end_date
->format('Y-m-d'))->isIdenticalTo($expecteds['date_fin_cotis']);
336 foreach ($expecteds as $key => $value) {
337 $property = $this->contrib
->fields
[$key]['propname'];
339 case \Galette\Entity\ContributionsTypes
::PK
:
340 $ct = $this->contrib
->type
;
341 if ($ct instanceof \Galette\Entity\ContributionsTypes
) {
342 $this->integer((int)$ct->id
)->isIdenticalTo($value);
344 $this->integer($ct)->isIdenticalTo($value);
348 $this->variable($contrib->$property)->isEqualTo($value, $property);
353 //load member from db
354 $this->adh
= new \Galette\Entity\
Adherent($this->zdb
, $this->adh
->id
);
355 //member is now up-to-date
356 $this->string($this->adh
->getRowClass())->isIdenticalTo('active cotis-late');
357 $this->string($this->adh
->due_date
)->isIdenticalTo($this->contrib
->end_date
);
358 $this->boolean($this->adh
->isUp2Date())->isFalse();