]> git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/Links.php
b75e23dfdf1c11899c3786ad4e15b2a857168015
[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 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 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 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 $seed = 95842355;
57 private $links;
58 private $ids = [];
59
60 /**
61 * Set up tests
62 *
63 * @param string $testMethod Method name
64 *
65 * @return void
66 */
67 public function beforeTestMethod($testMethod)
68 {
69 parent::beforeTestMethod($testMethod);
70 $this->initStatus();
71 $this->initContributionsTypes();
72
73 $this->links = new \Galette\Core\Links($this->zdb, false);
74 $this->contrib = new \Galette\Entity\Contribution($this->zdb, $this->login);
75
76 $this->adh = new \Galette\Entity\Adherent($this->zdb);
77 $this->adh->setDependencies(
78 $this->preferences,
79 $this->members_fields,
80 $this->history
81 );
82 }
83
84 /**
85 * Cleanup after testeach test method
86 *
87 * @param string $method Calling method
88 *
89 * @return void
90 */
91 public function afterTestMethod($method)
92 {
93 $delete = $this->zdb->delete(\Galette\Entity\Contribution::TABLE);
94 $delete->where(['info_cotis' => 'FAKER' . $this->seed]);
95 $this->zdb->execute($delete);
96
97 $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE);
98 $delete->where(['fingerprint' => 'FAKER' . $this->seed]);
99 $this->zdb->execute($delete);
100
101 $delete = $this->zdb->delete(\Galette\Core\Links::TABLE);
102 $this->zdb->execute($delete);
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->string($res)->isNotEmpty();
122
123 $select = $this->zdb->select(\Galette\Core\Links::TABLE);
124 $results = $this->zdb->execute($select);
125 $this->integer($results->count())->isIdenticalTo(1);
126
127 $this->array($links->isHashValid($res, 'phoarau@tele2.fr'))->isIdenticalTo([
128 \Galette\Core\Links::TARGET_MEMBERCARD,
129 $id
130 ]);
131
132 $this->boolean($links->isHashValid($res, 'any@mail.com'))->isFalse();
133 $this->boolean($links->isHashValid(base64_encode('sthingthatisnotahash'), 'phoarau@tele2.fr'))->isFalse();
134
135 $this->createContribution();
136 $cid = $this->contrib->id;
137 $res = $links->generateNewLink(
138 \Galette\Core\Links::TARGET_INVOICE,
139 $cid
140 );
141
142 $this->string($res)->isNotEmpty();
143 $this->array($links->isHashValid($res, 'phoarau@tele2.fr'))->isIdenticalTo([
144 \Galette\Core\Links::TARGET_INVOICE,
145 $cid
146 ]);
147 }
148
149 /**
150 * Test expired is invalid
151 *
152 * @return void
153 */
154 public function testExpiredValidate()
155 {
156 $links = $this->links;
157 $this->getMemberTwo();
158 $id = $this->adh->id;
159
160 $res = $links->generateNewLink(
161 \Galette\Core\Links::TARGET_MEMBERCARD,
162 $id
163 );
164
165 $this->string($res)->isNotEmpty();
166
167 $this->array($links->isHashValid($res, 'phoarau@tele2.fr'))->isIdenticalTo([
168 \Galette\Core\Links::TARGET_MEMBERCARD,
169 $id
170 ]);
171
172 $select = $this->zdb->select(\Galette\Core\Links::TABLE);
173 $results = $this->zdb->execute($select);
174 $this->integer($results->count())->isIdenticalTo(1);
175
176 $update = $this->zdb->update(\Galette\Core\Links::TABLE);
177 $old_date = new \DateTime();
178 $old_date->sub(new \DateInterval('P2W'));
179 $update
180 ->set(['creation_date' => $old_date->format('Y-m-d')])
181 ->where(['hash' => base64_decode($res)]);
182 $this->zdb->execute($update);
183
184 $this->boolean($links->isHashValid($res, 'phoarau@tele2.fr'))->isFalse();
185 }
186
187 /**
188 * Test cleanExpired
189 *
190 * @return void
191 */
192 public function testCleanExpired()
193 {
194 $date = new \DateTime();
195 $date->sub(new \DateInterval('PT48H'));
196
197 $insert = $this->zdb->insert(\Galette\Core\Links::TABLE);
198 $insert->values(
199 [
200 'hash' => 'Not expired link',
201 'creation_date' => $date->format('Y-m-d'),
202 'target' => \Galette\Core\Links::TARGET_MEMBERCARD,
203 'id' => 1
204 ]
205 );
206 $this->zdb->execute($insert);
207
208 $date->sub(new \DateInterval('P1W'));
209 $insert = $this->zdb->insert(\Galette\Core\Links::TABLE);
210 $insert->values(
211 [
212 'hash' => 'Expired link',
213 'creation_date' => $date->format('Y-m-d'),
214 'target' => \Galette\Core\Links::TARGET_MEMBERCARD,
215 'id' => 2
216 ]
217 );
218 $this->zdb->execute($insert);
219
220 $select = $this->zdb->select(\Galette\Core\Links::TABLE);
221 $results = $this->zdb->execute($select);
222 $this->integer($results->count())->isIdenticalTo(2);
223
224 $links = new \Galette\Core\Links($this->zdb, true);
225
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');
230 }
231
232 /**
233 * Test duplicate target
234 *
235 * @return void
236 */
237 public function testDuplicateLinkTarget()
238 {
239 $date = new \DateTime();
240 $date->sub(new \DateInterval('PT48H'));
241
242 $insert = $this->zdb->insert(\Galette\Core\Links::TABLE);
243 $insert->values(
244 [
245 'hash' => 'Unique link',
246 'creation_date' => $date->format('Y-m-d'),
247 'target' => \Galette\Core\Links::TARGET_MEMBERCARD,
248 'id' => 1
249 ]
250 );
251 $this->zdb->execute($insert);
252
253 $date->sub(new \DateInterval('PT1H'));
254
255 $insert = $this->zdb->insert(\Galette\Core\Links::TABLE);
256 $insert->values(
257 [
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,
261 'id' => 1
262 ]
263 );
264
265 $this->exception(
266 function () use ($insert) {
267 $this->zdb->execute($insert);
268 }
269 )
270 ->hasMessage('Duplicate entry');
271 }
272
273 /**
274 * Create test contribution in database
275 *
276 * @return void
277 */
278 protected function createContribution()
279 {
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
284
285 $edate = clone $bdate;
286 $edate->add(new \DateInterval('P1Y'));
287
288 $data = [
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'),
297 ];
298 $this->createContrib($data);
299 $this->checkContribExpected();
300 }
301
302 /**
303 * Check contributions expecteds
304 *
305 * @param Contribution $contrib Contribution instance, if any
306 * @param array $new_expecteds Changes on expected values
307 *
308 * @return void
309 */
310 protected function checkContribExpected($contrib = null, $new_expecteds = [])
311 {
312 if ($contrib === null) {
313 $contrib = $this->contrib;
314 }
315
316 $date_begin = $contrib->raw_begin_date;
317 $date_end = clone $date_begin;
318 $date_end->add(new \DateInterval('P1Y'));
319
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');
323
324 $expecteds = [
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')
331 ];
332 $expecteds = array_merge($expecteds, $new_expecteds);
333
334 $this->string($contrib->raw_end_date->format('Y-m-d'))->isIdenticalTo($expecteds['date_fin_cotis']);
335
336 foreach ($expecteds as $key => $value) {
337 $property = $this->contrib->fields[$key]['propname'];
338 switch ($key) {
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);
343 } else {
344 $this->integer($ct)->isIdenticalTo($value);
345 }
346 break;
347 default:
348 $this->variable($contrib->$property)->isEqualTo($value, $property);
349 break;
350 }
351 }
352
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();
359 }
360 }