Completed
Push — add/password-package ( 0700bd...8fa5f9 )
by
unknown
62:36 queued 52:24
created

Password_Checker_Test::set_up()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 16
rs 9.7333
c 0
b 0
f 0
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 $rule            Rule name.
66
	 * @param string $password        The password.
67
	 * @param bool   $expected_result The expected result.
68
	 * @param string $output_message  The output message.
69
	 */
70
	public function test_password( $rule, $password, $expected_result, $output_message ) {
71
		$tests = apply_filters( 'password_checker_tests', array() );
72
		echo count( $tests ) . "\n";
73
		echo esc_attr( $output_message ) . "\n";
74
75
		$results = $this->password_checker->test( $password, true );
76
		$this->assertSame( $results['passed'], $expected_result );
77
	}
78
79
	/**
80
	 * Data provider for password tests.
81
	 *
82
	 * @return array
83
	 */
84
	public function rule_provider() {
85
		/**
86
		 * Data format.
87
		 *
88
		 * Param 1 -> rule
89
		 * Param 2 -> password
90
		 * Param 3 -> expected_result
91
		 * Param 4 -> output_message
92
		 */
93
		return array(
94
			array( 'no_backslashes', 'abc\123', false, 'Passwords may not contain the character "\".' ),
95
			array( 'minimum_length', 'abc12', false, 'Password must be at least 6 characters.' ),
96
			array( 'has_mixed_case', 'abc123', false, 'Password must have mixed case characters.' ),
97
			array( 'has_digit', 'abcdef', false, 'Password must have digits.' ),
98
			array( 'has_special_char', 'abcdef', false, 'Password must have special characters.' ),
99
		);
100
	}
101
}
102