]> git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/Links.php
87c0e8df892d3f337c78188cbf97ed89d1c3d492
[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 * @return void
65 */
66 public function setUp(): void
67 {
68 parent::setUp();
69 $this->initStatus();
70 $this->initContributionsTypes();
71
72 $this->links = new \Galette\Core\Links($this->zdb, false);
73 $this->contrib = new \Galette\Entity\Contribution($this->zdb, $this->login);
74
75 $this->adh = new \Galette\Entity\Adherent($this->zdb);
76 $this->adh->setDependencies(
77 $this->preferences,
78 $this->members_fields,
79 $this->history
80 );
81 }
82
83 /**
84 * Cleanup after testeach test method
85 *
86 * @return void
87 */
88 public function tearDown(): void
89 {
90 $delete = $this->zdb->delete(\Galette\Entity\Contribution::TABLE);
91 $delete->where(['info_cotis' => 'FAKER' . $this->seed]);
92 $this->zdb->execute($delete);
93
94 $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE);
95 $delete->where(['fingerprint' => 'FAKER' . $this->seed]);
96 $this->zdb->execute($delete);
97
98 $delete = $this->zdb->delete(\Galette\Core\Links::TABLE);
99 $this->zdb->execute($delete);
100
101 $this->cleanHistory();
102 parent::tearDown();
103 }
104
105 /**
106 * Test new Link generation
107 *
108 * @return void
109 */
110 public function testGenerateNewLink()
111 {
112 $links = $this->links;
113 $this->getMemberTwo();
114 $id = $this->adh->id;
115
116 $res = $links->generateNewLink(
117 \Galette\Core\Links::TARGET_MEMBERCARD,
118 $id
119 );
120
121 $this->assertNotEmpty($res);
122
123 $select = $this->zdb->select(\Galette\Core\Links::TABLE);
124 $results = $this->zdb->execute($select);
125 $this->assertSame(1, $results->count());
126
127 $this->assertSame(
128 $links->isHashValid($res, 'phoarau@tele2.fr'),
129 [
130 \Galette\Core\Links::TARGET_MEMBERCARD,
131 $id
132 ]
133 );
134
135 $this->assertFalse($links->isHashValid($res, 'any@mail.com'));
136 $this->assertFalse($links->isHashValid(base64_encode('sthingthatisnotahash'), 'phoarau@tele2.fr'));
137
138 $this->createContribution();
139 $cid = $this->contrib->id;
140 $res = $links->generateNewLink(
141 \Galette\Core\Links::TARGET_INVOICE,
142 $cid
143 );
144
145 $this->assertNotEmpty($res);
146 $this->assertSame(
147 $links->isHashValid($res, 'phoarau@tele2.fr'),
148 [
149 \Galette\Core\Links::TARGET_INVOICE,
150 $cid
151 ]
152 );
153 }
154
155 /**
156 * Test expired is invalid
157 *
158 * @return void
159 */
160 public function testExpiredValidate()
161 {
162 $links = $this->links;
163 $this->getMemberTwo();
164 $id = $this->adh->id;
165
166 $res = $links->generateNewLink(
167 \Galette\Core\Links::TARGET_MEMBERCARD,
168 $id
169 );
170
171 $this->assertNotEmpty($res);
172
173 $this->assertSame(
174 $links->isHashValid($res, 'phoarau@tele2.fr'),
175 [
176 \Galette\Core\Links::TARGET_MEMBERCARD,
177 $id
178 ]
179 );
180
181 $select = $this->zdb->select(\Galette\Core\Links::TABLE);
182 $results = $this->zdb->execute($select);
183 $this->assertSame(1, $results->count());
184
185 $update = $this->zdb->update(\Galette\Core\Links::TABLE);
186 $old_date = new \DateTime();
187 $old_date->sub(new \DateInterval('P2W'));
188 $update
189 ->set(['creation_date' => $old_date->format('Y-m-d')])
190 ->where(['hash' => base64_decode($res)]);
191 $this->zdb->execute($update);
192
193 $this->assertFalse($links->isHashValid($res, 'phoarau@tele2.fr'));
194 }
195
196 /**
197 * Test cleanExpired
198 *
199 * @return void
200 */
201 public function testCleanExpired()
202 {
203 $date = new \DateTime();
204 $date->sub(new \DateInterval('PT48H'));
205
206 $insert = $this->zdb->insert(\Galette\Core\Links::TABLE);
207 $insert->values(
208 [
209 'hash' => 'Not expired link',
210 'creation_date' => $date->format('Y-m-d'),
211 'target' => \Galette\Core\Links::TARGET_MEMBERCARD,
212 'id' => 1
213 ]
214 );
215 $this->zdb->execute($insert);
216
217 $date->sub(new \DateInterval('P1W'));
218 $insert = $this->zdb->insert(\Galette\Core\Links::TABLE);
219 $insert->values(
220 [
221 'hash' => 'Expired link',
222 'creation_date' => $date->format('Y-m-d'),
223 'target' => \Galette\Core\Links::TARGET_MEMBERCARD,
224 'id' => 2
225 ]
226 );
227 $this->zdb->execute($insert);
228
229 $select = $this->zdb->select(\Galette\Core\Links::TABLE);
230 $results = $this->zdb->execute($select);
231 $this->assertSame(2, $results->count());
232
233 $links = new \Galette\Core\Links($this->zdb, true);
234
235 $results = $this->zdb->execute($select);
236 $result = $results->current();
237 $this->assertSame(1, $results->count());
238 $this->assertSame('Not expired link', $result['hash']);
239 }
240
241 /**
242 * Test duplicate target
243 *
244 * @return void
245 */
246 public function testDuplicateLinkTarget()
247 {
248 $date = new \DateTime();
249 $date->sub(new \DateInterval('PT48H'));
250
251 $insert = $this->zdb->insert(\Galette\Core\Links::TABLE);
252 $insert->values(
253 [
254 'hash' => 'Unique link',
255 'creation_date' => $date->format('Y-m-d'),
256 'target' => \Galette\Core\Links::TARGET_MEMBERCARD,
257 'id' => 1
258 ]
259 );
260 $this->zdb->execute($insert);
261
262 $date->sub(new \DateInterval('PT1H'));
263
264 $insert = $this->zdb->insert(\Galette\Core\Links::TABLE);
265 $insert->values(
266 [
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,
270 'id' => 1
271 ]
272 );
273
274 $this->expectExceptionMessage('Duplicate entry');
275 $this->zdb->execute($insert);
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->assertInstanceOf('DateTime', $contrib->raw_date);
330 $this->assertInstanceOf('DateTime', $contrib->raw_begin_date);
331 $this->assertInstanceOf('DateTime', $contrib->raw_end_date);
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->assertSame($expecteds['date_fin_cotis'], $contrib->raw_end_date->format('Y-m-d'));
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->assertSame($value, (int)$ct->id);
352 } else {
353 $this->assertSame($value, $ct);
354 }
355 break;
356 default:
357 $this->assertEquals($contrib->$property, $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->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());
368 }
369 }