|
1
|
|
|
<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Tests the Password_Checker package. |
|
5
|
|
|
* |
|
6
|
|
|
* @package automattic/jetpack-password-checker |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Automattic\Jetpack; |
|
10
|
|
|
|
|
11
|
|
|
use WorDBless\BaseTestCase; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Test Password_Checker class |
|
15
|
|
|
*/ |
|
16
|
|
|
class Password_Checker_Test extends BaseTestCase { |
|
17
|
|
|
/** |
|
18
|
|
|
* User ID. |
|
19
|
|
|
* |
|
20
|
|
|
* @var int|\WP_Error |
|
21
|
|
|
*/ |
|
22
|
|
|
private $user_id; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* User object. |
|
26
|
|
|
* |
|
27
|
|
|
* @var \WP_User |
|
28
|
|
|
*/ |
|
29
|
|
|
private $user; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Password_Checker object. |
|
33
|
|
|
* |
|
34
|
|
|
* @var Password_Checker |
|
35
|
|
|
*/ |
|
36
|
|
|
private $password_checker; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Initialize tests. |
|
40
|
|
|
* |
|
41
|
|
|
* @before |
|
42
|
|
|
*/ |
|
43
|
|
|
public function set_up() { |
|
44
|
|
|
$this->user_id = wp_insert_user( |
|
45
|
|
|
array( |
|
46
|
|
|
'user_login' => 'test-user', |
|
47
|
|
|
'user_pass' => '123', |
|
48
|
|
|
'first_name' => 'Test', |
|
49
|
|
|
'last_name' => 'User', |
|
50
|
|
|
'nickname' => 'test', |
|
51
|
|
|
'role' => 'subscriber', |
|
52
|
|
|
) |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
$this->user = new \WP_User( $this->user_id ); |
|
56
|
|
|
|
|
57
|
|
|
$this->password_checker = new Password_Checker( $this->user ); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Test the password checker. |
|
62
|
|
|
* |
|
63
|
|
|
* @dataProvider rule_provider |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $section Section name. |
|
66
|
|
|
* @param string $rule Rule name. |
|
67
|
|
|
* @param string $password The password. |
|
68
|
|
|
* @param bool $expected_result The expected result. |
|
69
|
|
|
* @param string $output_message The output message. |
|
70
|
|
|
*/ |
|
71
|
|
|
public function test_password( $section, $rule, $password, $expected_result, $output_message ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
|
72
|
|
|
$this->password_checker->common_passwords = array( 'password' ); |
|
73
|
|
|
|
|
74
|
|
|
$tests = $this->password_checker->get_tests( $section ); |
|
|
|
|
|
|
75
|
|
|
|
|
76
|
|
|
$results = $this->password_checker->run_tests( $password, array( $section => array( $rule => $tests[ $section ][ $rule ] ) ) ); |
|
77
|
|
|
|
|
78
|
|
|
$this->assertSame( $expected_result, ! empty( $results['passed'] ), $output_message ); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Data provider for password tests. |
|
83
|
|
|
* |
|
84
|
|
|
* @return array |
|
85
|
|
|
*/ |
|
86
|
|
|
public function rule_provider() { |
|
87
|
|
|
/** |
|
88
|
|
|
* Data format. |
|
89
|
|
|
* |
|
90
|
|
|
* Param 1 -> section |
|
91
|
|
|
* Param 2 -> rule |
|
92
|
|
|
* Param 3 -> password |
|
93
|
|
|
* Param 4 -> expected_result |
|
94
|
|
|
* Param 5 -> output_message |
|
95
|
|
|
*/ |
|
96
|
|
|
|
|
97
|
|
|
return array( |
|
98
|
|
|
'no_backslashes' => array( |
|
99
|
|
|
'preg_match', |
|
100
|
|
|
'no_backslashes', |
|
101
|
|
|
'abc123', |
|
102
|
|
|
true, |
|
103
|
|
|
'Passwords may not contain the character "\".', |
|
104
|
|
|
), |
|
105
|
|
|
'minimum_length' => array( |
|
106
|
|
|
'preg_match', |
|
107
|
|
|
'minimum_length', |
|
108
|
|
|
'abc123', |
|
109
|
|
|
true, |
|
110
|
|
|
'Password must be at least 6 characters.', |
|
111
|
|
|
), |
|
112
|
|
|
'has_mixed_case' => array( |
|
113
|
|
|
'preg_match', |
|
114
|
|
|
'has_mixed_case', |
|
115
|
|
|
'Abc123', |
|
116
|
|
|
true, |
|
117
|
|
|
'Password must have mixed case characters.', |
|
118
|
|
|
), |
|
119
|
|
|
'has_digit' => array( |
|
120
|
|
|
'preg_match', |
|
121
|
|
|
'has_digit', |
|
122
|
|
|
'abc123', |
|
123
|
|
|
true, |
|
124
|
|
|
'Password must have digits.', |
|
125
|
|
|
), |
|
126
|
|
|
'has_special_char' => array( |
|
127
|
|
|
'preg_match', |
|
128
|
|
|
'has_special_char', |
|
129
|
|
|
'abc!def', |
|
130
|
|
|
true, |
|
131
|
|
|
'Password must have special characters.', |
|
132
|
|
|
), |
|
133
|
|
|
'compare_to_list_1' => array( |
|
134
|
|
|
'compare_to_list', |
|
135
|
|
|
'not_a_common_password', |
|
136
|
|
|
'password', |
|
137
|
|
|
false, |
|
138
|
|
|
'Common passwords that should not be used.', |
|
139
|
|
|
), |
|
140
|
|
|
'compare_to_list_2' => array( |
|
141
|
|
|
'compare_to_list', |
|
142
|
|
|
'not_a_common_password', |
|
143
|
|
|
'hunter2', |
|
144
|
|
|
true, |
|
145
|
|
|
'Common passwords that should not be used.', |
|
146
|
|
|
), |
|
147
|
|
|
'compare_to_list_3' => array( |
|
148
|
|
|
'compare_to_list', |
|
149
|
|
|
'not_same_as_other_user_data', |
|
150
|
|
|
'test-user', |
|
151
|
|
|
false, |
|
152
|
|
|
'Password contains user data.', |
|
153
|
|
|
), |
|
154
|
|
|
); |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: