3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2020-2023 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-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
37 namespace Galette\Util\test\units
;
39 use PHPUnit\Framework\TestCase
;
40 use Galette\Core\Preferences
;
41 use PHPUnit\Framework\Attributes\DataProvider
;
44 * Password tests class
48 * @package GaletteTests
49 * @author Johan Cwiklinski <johan@x-tnd.be>
50 * @copyright 2020-2023 The Galette Team
51 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
52 * @link http://galette.tuxfamily.org
55 class Password
extends TestCase
57 private \Galette\Core\Db
$zdb;
58 private \Galette\Core\Preferences
$preferences;
65 public function tearDow(): void
67 if (TYPE_DB
=== 'mysql') {
68 $this->assertSame([], $this->zdb
->getWarnings());
70 $this->preferences
->pref_password_strength
= Preferences
::PWD_NONE
;
71 $this->preferences
->pref_password_length
= 6;
72 $this->preferences
->pref_password_blacklist
= false;
73 $this->preferences
->store();
81 public function setUp(): void
83 $this->zdb
= new \Galette\Core\
Db();
84 $this->preferences
= new \Galette\Core\
Preferences($this->zdb
);
88 * Passwords data provider
92 public static function passProvider()
95 // [strength, password, errors]
96 [Preferences
::PWD_WEAK
, 'weaker', ['A', '1', '@']],
97 [Preferences
::PWD_WEAK
, '123456', ['nl', '@']],
98 [Preferences
::PWD_WEAK
, '²²²²²²', ['nl', '@']],
99 [Preferences
::PWD_WEAK
, 'foobar', ['A', '1', '@']],
100 [Preferences
::PWD_WEAK
, 'ömgwat', ['A', '1', '@']],
101 [Preferences
::PWD_WEAK
, '!.!.!.', ['nl', '1']],
102 [Preferences
::PWD_WEAK
, '!.!.!﴾', ['nl', '1']],
103 [Preferences
::PWD_WEAK
, '7857375923752947', ['nl', '@']],
104 [Preferences
::PWD_WEAK
, 'FSDFJSLKFFSDFDSF', ['a', '1', '@']],
105 [Preferences
::PWD_WEAK
, 'FÜKFJSLKFFSDFDSF', ['a', '1', '@']],
106 [Preferences
::PWD_WEAK
, 'fjsfjdljfsjsjjlsj', ['A', '1', '@']],
108 [Preferences
::PWD_MEDIUM
, 'wee6eak',['A', '@']],
109 [Preferences
::PWD_MEDIUM
, 'foobar!', ['A', '1']],
110 [Preferences
::PWD_MEDIUM
, 'Foobar', ['1', '@']],
111 [Preferences
::PWD_MEDIUM
, '123456!', ['nl']],
112 [Preferences
::PWD_MEDIUM
, 'fjsfjdljfsjsjjls1', ['A', '@']],
113 [Preferences
::PWD_MEDIUM
, '785737592375294b', ['A', '@']],
115 [Preferences
::PWD_STRONG
, 'Foobar﴾', ['1']],
116 [Preferences
::PWD_STRONG
, 'foo-b0r!', ['A']],
118 [Preferences
::PWD_VERY_STRONG
, 'Foobar!55!', []],
119 [Preferences
::PWD_VERY_STRONG
, 'Foobar$55', []],
120 [Preferences
::PWD_VERY_STRONG
, 'Foobar€55', []],
121 [Preferences
::PWD_VERY_STRONG
, 'Foobar€55', []],
122 [Preferences
::PWD_VERY_STRONG
, 'Foobar$55_4&F', []],
123 [Preferences
::PWD_VERY_STRONG
, 'L33RoyJ3Jenkins!', []],
128 * Test password validation
130 * @param integer $level Password level
131 * @param string $pass Password
132 * @param array $errors Errors
136 #[DataProvider('passProvider')]
137 public function testValidatePassword($level, $pass, $errors)
139 //errror messages mapping
140 foreach ($errors as &$err) {
143 $err = 'Does not contains letters';
146 $err = 'Does not contains lowercase letters';
149 $err = 'Does not contains uppercase letters';
152 $err = 'Does not contains numbers';
155 $err = 'Does not contains special characters';
160 if ($level < Preferences
::PWD_VERY_STRONG
) {
161 $this->preferences
->pref_password_strength
= $level +
1;
162 $password = new \Galette\Util\
Password($this->preferences
);
163 $this->assertFalse($password->isValid($pass));
164 $this->assertEquals($password->getErrors(), $errors);
167 $this->preferences
->pref_password_strength
= $level;
168 $password = new \Galette\Util\
Password($this->preferences
);
169 $this->assertTrue($password->isValid($pass), implode(', ', $password->getErrors()));
170 $this->assertSame($password->getErrors(), []);
171 $this->assertEquals($password->getStrenghtErrors(), $errors);
175 * Blacklist password provider
179 public static function blacklistProvider()
184 ['mypassisgreat', false],
192 * Test password blacklist
194 * @param string $pass Password to test
195 * @param boolean $expected Excpected return
199 #[DataProvider('blacklistProvider')]
200 public function testBlacklist($pass, $expected)
202 $this->preferences
->pref_password_blacklist
= true;
203 $password = new \Galette\Util\
Password($this->preferences
);
204 $this->assertSame($password->isBlacklisted($pass), $expected, $pass);
206 $this->preferences
->pref_password_blacklist
= false;
207 $password = new \Galette\Util\
Password($this->preferences
);
208 $this->assertFalse($password->isBlacklisted($pass));
212 * Test with personal information
216 public function testPersonalInformation()
219 'login' => 'mylogin',
221 'surname' => 'mysurname',
222 'nickname' => 'mynickname'
225 $this->preferences
->pref_password_strength
= Preferences
::PWD_NONE
;
226 $password = new \Galette\Util\
Password($this->preferences
);
227 $password->addPersonalInformation($infos);
228 foreach ($infos as $info) {
229 $this->assertTrue($password->isValid($info), implode(', ', $password->getErrors()));
230 $this->assertSame($password->getErrors(), []);
233 $this->preferences
->pref_password_strength
= Preferences
::PWD_WEAK
;
234 $password = new \Galette\Util\
Password($this->preferences
);
235 $password->addPersonalInformation($infos);
236 foreach ($infos as $info) {
237 $this->assertFalse($password->isValid($info));
239 $password->getErrors(),
240 ['Do not use any of your personal information as password!']
244 $this->assertFalse($password->isValid('MyLoGiN'));
245 $this->assertTrue($password->isValid('iMyLoGiN'));
248 global $zdb, $login, $i18n; // globals :(
250 $i18n = new \Galette\Core\
I18n(
251 \Galette\Core\I18n
::DEFAULT_LANG
253 $login = new \Galette\Core\
Login($this->zdb
, $i18n);
254 $history = new \Galette\Core\
History($this->zdb
, $login, $this->preferences
);
255 include_once GALETTE_ROOT
. 'includes/fields_defs/members_fields.php';
256 $members_fields = $members_fields;
258 $adh = new \Galette\Entity\
Adherent($this->zdb
);
259 $adh->setDependencies(
266 'nom_adh' => 'Pignon',
267 'prenom_adh' => 'Jean-Cloud Juste',
268 'ddn_adh' => '1980-05-01',
269 'ville_adh' => 'Paris',
270 'pseudo_adh' => 'petit-cheval-de-manège',
271 'login_adh' => 'log_In',
272 'email_adh' => 'mail@galette.eu',
273 //required for check to work
274 'date_crea_adh' => date('Y-m-d'),
275 'sexe_adh' => \Galette\Entity\Adherent
::NC
277 $check = $adh->check($adh_data, [], []);
278 if (is_array($check)) {
281 $this->assertTrue($check);
283 $password = new \Galette\Util\
Password($this->preferences
);
284 $password->setAdherent($adh);
286 unset($adh_data['date_crea_adh']);
287 unset($adh_data['sexe_adh']);
289 $adh_data['c00'] = 'jean-cloud justepignon';
290 $adh_data['c000'] = 'pignonjean-cloud juste';
291 $adh_data['c01'] = 'jcjpignon';
292 $adh_data['c02'] = 'pignonjcj';
293 $adh_data['c03'] = 'pignonj';
294 $adh_data['c04'] = 'jpignon';
296 $adh_data['c10'] = 'log_inpignon';
297 $adh_data['c100'] = 'pignonlog_in';
298 $adh_data['c11'] = 'pignonli';
299 $adh_data['c12'] = 'lipignon';
300 $adh_data['c13'] = 'lpignon';
301 $adh_data['c14'] = 'pignonl';
303 $adh_data['c20'] = 'petit-cheval-de-manègepignon';
304 $adh_data['c200'] = 'pignonpetit-cheval-de-manège';
305 $adh_data['c21'] = 'pignonpcdm';
306 $adh_data['c22'] = 'pcdmpignon';
307 $adh_data['c23'] = 'ppignon';
308 $adh_data['c24'] = 'pignonp';
310 foreach ($adh_data as $key => $data) {
311 $this->assertFalse($password->isValid($data), $key);
314 $this->assertFalse($password->isValid('19800501'));
315 $this->assertFalse($password->isValid('01051980'));