]> git.agnieray.net Git - galette.git/blob - tests/Galette/Core/tests/units/Password.php
Switch to PSR12, phpcbf fix
[galette.git] / tests / Galette / Core / tests / units / Password.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 © 2013-2014 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 2013-2014 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 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since 2013-10-22
36 */
37
38 namespace Galette\Core\test\units;
39
40 use atoum;
41
42 /**
43 * Password tests class
44 *
45 * @category Core
46 * @name Password
47 * @package GaletteTests
48 * @author Johan Cwiklinski <johan@x-tnd.be>
49 * @copyright 2013-2014 The Galette Team
50 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
51 * @link http://galette.tuxfamily.org
52 * @since 2013-01-13
53 */
54 class Password extends atoum
55 {
56 private $pass = null;
57 private $zdb;
58
59 /**
60 * Set up tests
61 *
62 * @param string $testMethod Method name
63 *
64 * @return void
65 */
66 public function beforeTestMethod($testMethod)
67 {
68 $this->zdb = new \Galette\Core\Db();
69 $this->pass = new \Galette\Core\Password($this->zdb, false);
70 }
71
72 /**
73 * Test unique password generator
74 *
75 * @return void
76 */
77 public function testRandom()
78 {
79 $results = array();
80
81 for ($i = 0; $i < 200; $i++) {
82 $random = $this->pass->makeRandomPassword(15);
83 $this->string($random)->hasLength(15);
84
85 $exists = in_array($random, $results);
86 $this->boolean($exists)->isFalse();
87
88 $results[] = $random;
89 $this->array($results)->hasSize($i + 1);
90 }
91
92 $random = $this->pass->makeRandomPassword();
93 $this->string($random)->hasLength(\Galette\Core\Password::DEFAULT_SIZE);
94 }
95
96 /**
97 * Create member and get its id
98 *
99 * @return int
100 */
101 private function createMember()
102 {
103 try {
104 $this->deleteMember();
105 } catch (\Exception $e) {
106 //empty catch
107 }
108
109 $status = new \Galette\Entity\Status($this->zdb);
110 if (count($status->getList()) === 0) {
111 $res = $status->installInit();
112 $this->boolean($res)->isTrue();
113 }
114 $insert = $this->zdb->insert(\Galette\Entity\Adherent::TABLE);
115 $insert->values(
116 [
117 'nom_adh' => 'Test password user',
118 'login_adh' => 'test_password_user'
119 ]
120 );
121 $this->zdb->execute($insert);
122
123 if ($this->zdb->isPostgres()) {
124 return $this->zdb->driver->getLastGeneratedValue(
125 PREFIX_DB . 'adherents_id_seq'
126 );
127 } else {
128 return $this->zdb->driver->getLastGeneratedValue();
129 }
130 }
131
132 /**
133 * Delete member
134 *
135 * @return void
136 */
137 private function deleteMember()
138 {
139 $delete = $this->zdb->delete(\Galette\Entity\Adherent::TABLE);
140 $delete->where(['login_adh' => 'test_password_user']);
141 $this->zdb->execute($delete);
142 }
143
144 /**
145 * Test new PasswordImage generation
146 *
147 * @return void
148 */
149 public function testGenerateNewPassword()
150 {
151 $id_adh = $this->createMember();
152 $pass = $this->pass;
153 $res = $pass->generateNewPassword($id_adh);
154 $this->boolean($res)->isTrue();
155 $new_pass = $pass->getNewPassword();
156 $this->string($new_pass)
157 ->hasLength($pass::DEFAULT_SIZE);
158 $hash = $pass->getHash();
159 $this->string($hash)->hasLength(60);
160
161 $is_valid = $pass->isHashValid($hash);
162 $this->variable($is_valid)->isNotNull();
163
164 $select = $this->zdb->select(\Galette\Core\Password::TABLE);
165 $results = $this->zdb->execute($select);
166 $this->integer($results->count())->isIdenticalTo(1);
167
168 $removed = $pass->removeHash($hash);
169 $this->boolean($removed)->isTrue();
170
171 $results = $this->zdb->execute($select);
172 $this->integer($results->count())->isIdenticalTo(0);
173
174 $this->deleteMember();
175 }
176
177 /**
178 * Test cleanExpired
179 *
180 * @return void
181 */
182 public function testCleanExpired()
183 {
184 $id_adh = $this->createMember();
185
186 $date = new \DateTime();
187 $date->sub(new \DateInterval('PT48H'));
188
189 $insert = $this->zdb->insert(\Galette\Core\Password::TABLE);
190 $insert->values(
191 [
192 \Galette\Core\Password::PK => $id_adh,
193 'date_crea_tmp_passwd' => $date->format('Y-m-d'),
194 'tmp_passwd' => 'azerty'
195 ]
196 );
197 $this->zdb->execute($insert);
198
199 $select = $this->zdb->select(\Galette\Core\Password::TABLE);
200 $results = $this->zdb->execute($select);
201 $this->integer($results->count())->isIdenticalTo(1);
202
203 $pass = new \Galette\Core\Password($this->zdb, true);
204
205 $results = $this->zdb->execute($select);
206 $this->integer($results->count())->isIdenticalTo(0);
207
208 $this->deleteMember();
209 }
210
211 /**
212 * Generate new password that throws an exception
213 *
214 * @return void
215 */
216 public function testGenerateNewPasswordWException()
217 {
218 $this->zdb = new \mock\Galette\Core\Db();
219 $this->calling($this->zdb)->execute = function ($o) {
220 throw new \LogicException('Error executing query!', 123);
221 };
222
223 $pass = new \Galette\Core\Password($this->zdb, false);
224 $res = $pass->generateNewPassword(12);
225 $this->boolean($res)->isFalse();
226 }
227
228 /**
229 * Generate new password when insert returns false
230 *
231 * @return void
232 */
233 public function testGenerateNewPasswordWFalseInsert()
234 {
235 $this->zdb = new \mock\Galette\Core\Db();
236 $this->calling($this->zdb)->execute = function ($o) {
237 return false;
238 };
239
240 $pass = new \Galette\Core\Password($this->zdb, false);
241 $res = $pass->generateNewPassword(12);
242 $this->boolean($res)->isFalse();
243 }
244
245 /**
246 * Test cleanExpired that throws an exception
247 *
248 * @return void
249 */
250 public function testCleanExpiredWException()
251 {
252 $this->zdb = new \mock\Galette\Core\Db();
253 $this->calling($this->zdb)->execute = function ($o) {
254 throw new \LogicException('Error executing query!', 123);
255 };
256
257 $pass = new \Galette\Core\Password($this->zdb);
258 }
259
260 /**
261 * Test hash validity that throws an exception
262 *
263 * @return void
264 */
265 public function testIsHashValidWException()
266 {
267 $this->zdb = new \mock\Galette\Core\Db();
268 $this->calling($this->zdb)->execute = function ($o) {
269 throw new \LogicException('Error executing query!', 123);
270 };
271
272 $pass = new \Galette\Core\Password($this->zdb, false);
273 $res = $pass->isHashValid('thehash');
274 $this->boolean($res)->isFalse();
275 }
276
277 /**
278 * Test hash removal that throws an exception
279 *
280 * @return void
281 */
282 public function testRemoveHashWException()
283 {
284 $this->zdb = new \mock\Galette\Core\Db();
285 $this->calling($this->zdb)->execute = function ($o) {
286 throw new \LogicException('Error executing query!', 123);
287 };
288
289 $pass = new \Galette\Core\Password($this->zdb, false);
290 $res = $pass->removeHash('thehash');
291 $this->boolean($res)->isFalse();
292 }
293 }