]> git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/Links.php
8b970f80993b3e002e7fc78538ff90271745c975
[galette.git] / tests / Galette / Core / tests / units / Links.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Password tests
7 *
8 * PHP version 5
9 *
10 * Copyright © 2020-2023 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
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.
18 *
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.
23 *
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/>.
26 *
27 * @category Core
28 * @package GaletteTests
29 *
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
34 * @since 2020-03-15
35 */
36
37 namespace Galette\Core\test\units;
38
39 use Galette\GaletteTestCase;
40
41 /**
42 * Password tests class
43 *
44 * @category Core
45 * @name Password
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
51 * @since 2020-03-15
52 */
53 class Links extends GaletteTestCase
54 {
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'];
60
61 /**
62 * Set up tests
63 *
64 * @param string $method Method name
65 *
66 * @return void
67 */
68 public function beforeTestMethod($method)
69 {
70 parent::beforeTestMethod($method);
71 $this->initStatus();
72 $this->initContributionsTypes();
73
74 $this->links = new \Galette\Core\Links($this->zdb, false);
75 $this->contrib = new \Galette\Entity\Contribution($this->zdb, $this->login);
76
77 $this->adh = new \Galette\Entity\Adherent($this->zdb);
78 $this->adh->setDependencies(
79 $this->preferences,
80 $this->members_fields,
81 $this->history
82 );
83 }
84
85 /**
86 * Cleanup after testeach test method
87 *
88 * @param string $method Calling method
89 *
90 * @return void
91 */
92 public function afterTestMethod($method)
93 {
94 parent::afterTestMethod($method);
95
96 $delete = $this->zdb->delete(\Galette\Entity\Contribution::TABLE);
97 $delete->where(['info_cotis' => 'FAKER' . $this->seed]);
98 $this->zdb->execute($delete);
99
100 $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE);
101 $delete->where(['fingerprint' => 'FAKER' . $this->seed]);
102 $this->zdb->execute($delete);
103
104 $delete = $this->zdb->delete(\Galette\Core\Links::TABLE);
105 $this->zdb->execute($delete);
106
107 $this->cleanHistory();
108 }
109
110 /**
111 * Test new Link generation
112 *
113 * @return void
114 */
115 public function testGenerateNewLink()
116 {
117 $links = $this->links;
118 $this->getMemberTwo();
119 $id = $this->adh->id;
120
121 $res = $links->generateNewLink(
122 \Galette\Core\Links::TARGET_MEMBERCARD,
123 $id
124 );
125
126 $this->string($res)->isNotEmpty();
127
128 $select = $this->zdb->select(\Galette\Core\Links::TABLE);
129 $results = $this->zdb->execute($select);
130 $this->integer($results->count())->isIdenticalTo(1);
131
132 $this->array($links->isHashValid($res, 'phoarau@tele2.fr'))->isIdenticalTo([
133 \Galette\Core\Links::TARGET_MEMBERCARD,
134 $id
135 ]);
136
137 $this->boolean($links->isHashValid($res, 'any@mail.com'))->isFalse();
138 $this->boolean($links->isHashValid(base64_encode('sthingthatisnotahash'), 'phoarau@tele2.fr'))->isFalse();
139
140 $this->createContribution();
141 $cid = $this->contrib->id;
142 $res = $links->generateNewLink(
143 \Galette\Core\Links::TARGET_INVOICE,
144 $cid
145 );
146
147 $this->string($res)->isNotEmpty();
148 $this->array($links->isHashValid($res, 'phoarau@tele2.fr'))->isIdenticalTo([
149 \Galette\Core\Links::TARGET_INVOICE,
150 $cid
151 ]);
152 }
153
154 /**
155 * Test expired is invalid
156 *
157 * @return void
158 */
159 public function testExpiredValidate()
160 {
161 $links = $this->links;
162 $this->getMemberTwo();
163 $id = $this->adh->id;
164
165 $res = $links->generateNewLink(
166 \Galette\Core\Links::TARGET_MEMBERCARD,
167 $id
168 );
169
170 $this->string($res)->isNotEmpty();
171
172 $this->array($links->isHashValid($res, 'phoarau@tele2.fr'))->isIdenticalTo([
173 \Galette\Core\Links::TARGET_MEMBERCARD,
174 $id
175 ]);
176
177 $select = $this->zdb->select(\Galette\Core\Links::TABLE);
178 $results = $this->zdb->execute($select);
179 $this->integer($results->count())->isIdenticalTo(1);
180
181 $update = $this->zdb->update(\Galette\Core\Links::TABLE);
182 $old_date = new \DateTime();
183 $old_date->sub(new \DateInterval('P2W'));
184 $update
185 ->set(['creation_date' => $old_date->format('Y-m-d')])
186 ->where(['hash' => base64_decode($res)]);
187 $this->zdb->execute($update);
188
189 $this->boolean($links->isHashValid($res, 'phoarau@tele2.fr'))->isFalse();
190 }
191
192 /**
193 * Test cleanExpired
194 *
195 * @return void
196 */
197 public function testCleanExpired()
198 {
199 $date = new \DateTime();
200 $date->sub(new \DateInterval('PT48H'));
201
202 $insert = $this->zdb->insert(\Galette\Core\Links::TABLE);
203 $insert->values(
204 [
205 'hash' => 'Not expired link',
206 'creation_date' => $date->format('Y-m-d'),
207 'target' => \Galette\Core\Links::TARGET_MEMBERCARD,
208 'id' => 1
209 ]
210 );
211 $this->zdb->execute($insert);
212
213 $date->sub(new \DateInterval('P1W'));
214 $insert = $this->zdb->insert(\Galette\Core\Links::TABLE);
215 $insert->values(
216 [
217 'hash' => 'Expired link',
218 'creation_date' => $date->format('Y-m-d'),
219 'target' => \Galette\Core\Links::TARGET_MEMBERCARD,
220 'id' => 2
221 ]
222 );
223 $this->zdb->execute($insert);
224
225 $select = $this->zdb->select(\Galette\Core\Links::TABLE);
226 $results = $this->zdb->execute($select);
227 $this->integer($results->count())->isIdenticalTo(2);
228
229 $links = new \Galette\Core\Links($this->zdb, true);
230
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');
235 }
236
237 /**
238 * Test duplicate target
239 *
240 * @return void
241 */
242 public function testDuplicateLinkTarget()
243 {
244 $date = new \DateTime();
245 $date->sub(new \DateInterval('PT48H'));
246
247 $insert = $this->zdb->insert(\Galette\Core\Links::TABLE);
248 $insert->values(
249 [
250 'hash' => 'Unique link',
251 'creation_date' => $date->format('Y-m-d'),
252 'target' => \Galette\Core\Links::TARGET_MEMBERCARD,
253 'id' => 1
254 ]
255 );
256 $this->zdb->execute($insert);
257
258 $date->sub(new \DateInterval('PT1H'));
259
260 $insert = $this->zdb->insert(\Galette\Core\Links::TABLE);
261 $insert->values(
262 [
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,
266 'id' => 1
267 ]
268 );
269
270 $this->exception(
271 function () use ($insert) {
272 $this->zdb->execute($insert);
273 }
274 )
275 ->hasMessage('Duplicate entry');
276 }
277
278 /**
279 * Create test contribution in database
280 *
281 * @return void
282 */
283 protected function createContribution()
284 {
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
290
291 $due_date = clone $begin_date;
292 $due_date->sub(new \DateInterval('P1D'));
293 $due_date->add(new \DateInterval('P1Y'));
294
295 $data = [
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'),
304 ];
305 $this->createContrib($data);
306 $this->checkContribExpected();
307 }
308
309 /**
310 * Check contributions expecteds
311 *
312 * @param Contribution $contrib Contribution instance, if any
313 * @param array $new_expecteds Changes on expected values
314 *
315 * @return void
316 */
317 protected function checkContribExpected($contrib = null, $new_expecteds = [])
318 {
319 if ($contrib === null) {
320 $contrib = $this->contrib;
321 }
322
323 $begin_date = $contrib->raw_begin_date;
324
325 $due_date = clone $begin_date;
326 $due_date->sub(new \DateInterval('P1D'));
327 $due_date->add(new \DateInterval('P1Y'));
328
329 $this->object($contrib->raw_date)->isInstanceOf('DateTime');
330 $this->object($contrib->raw_begin_date)->isInstanceOf('DateTime');
331 $this->object($contrib->raw_end_date)->isInstanceOf('DateTime');
332
333 $expecteds = [
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')
340 ];
341 $expecteds = array_merge($expecteds, $new_expecteds);
342
343 $this->string($contrib->raw_end_date->format('Y-m-d'))->isIdenticalTo($expecteds['date_fin_cotis']);
344
345 foreach ($expecteds as $key => $value) {
346 $property = $this->contrib->fields[$key]['propname'];
347 switch ($key) {
348 case \Galette\Entity\ContributionsTypes::PK:
349 $ct = $this->contrib->type;
350 if ($ct instanceof \Galette\Entity\ContributionsTypes) {
351 $this->integer((int)$ct->id)->isIdenticalTo($value);
352 } else {
353 $this->integer($ct)->isIdenticalTo($value);
354 }
355 break;
356 default:
357 $this->variable($contrib->$property)->isEqualTo($value, $property);
358 break;
359 }
360 }
361
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->string($this->adh->getRowClass())->isIdenticalTo('active cotis-late');
366 $this->string($this->adh->due_date)->isIdenticalTo($this->contrib->end_date);
367 $this->boolean($this->adh->isUp2Date())->isFalse();
368 }
369 }