Completed
Push — add/subscription-block-tests ( 854511...58cdf8 )
by
unknown
18:03 queued 07:43
created

Password_Checker_Test::rule_provider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 70

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 70
rs 8.6545
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 );
0 ignored issues
show
Documentation introduced by
$section is of type string, but the function expects a false|array.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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