Completed
Push — master ( 7a29a1...33d37f )
by Thomas
01:49
created

dataTestCheckNumericCharacters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Semih Serhat Karakaya <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
namespace OCA\Password_Policy\Tests;
22
use OC\HintException;
23
use OCA\Security\SecurityConfig;
24
use OCA\Security\PasswordValidator;
25
use OCP\IL10N;
26
use Test\TestCase;
27
class PasswordValidatorTest extends TestCase {
28
29
	/** @var  SecurityConfig|\PHPUnit_Framework_MockObject_MockObject */
30
	private $config;
31
32
	/** @var  IL10N|\PHPUnit_Framework_MockObject_MockObject */
33
	private $l10n;
34
35
	/** @var  PasswordValidator */
36
	private $passValidator;
37
38
	public function setUp() {
39
		parent::setUp();
40
		$this->l10n = $this->createMock(IL10N::class);
41
		$this->config = $this->createMock(SecurityConfig::class);
42
		$this->passValidator = new PasswordValidator($this->config, $this->l10n);
43
	}
44
45
	/**
46
	 * @param array $mockedMethods
47
	 * @return PasswordValidator | \PHPUnit_Framework_MockObject_MockObject
48
	 */
49
	private function getMockInstance($mockedMethods = []) {
50
		$passwordValidator = $this->getMockBuilder('OCA\Security\PasswordValidator')
51
			->setConstructorArgs([$this->config, $this->l10n])
52
			->setMethods($mockedMethods)->getMock();
53
		return $passwordValidator;
54
	}
55
56
	public function testCheckPasswordLength() {
57
		$this->config->expects($this->exactly(1))->method('getMinPasswordLength')->willReturn(4);
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OCA\Security\SecurityConfig>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
58
		$this->passValidator->checkPasswordLength('password');
59
	}
60
	/**
61
	 * @expectedException \OC\HintException
62
	 */
63
	public function testCheckPasswordLengthFail() {
64
		$this->config->expects($this->exactly(1))->method('getMinPasswordLength')->willReturn(4);
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OCA\Security\SecurityConfig>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
65
		$this->passValidator->checkPasswordLength('123');
66
	}
67
	/**
68
	 * @dataProvider dataTestCheckUpperLowerCase
69
	 *
70
	 * @param string $password
71
	 * @param bool $enforceUpperLowerCase
72
	 */
73
	public function testCheckUpperLowerCase($password, $enforceUpperLowerCase) {
74
		$this->config->expects($this->once())->method('getIsUpperLowerCaseEnforced')
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OCA\Security\SecurityConfig>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
75
			->willReturn($enforceUpperLowerCase);
76
		$this->passValidator->checkUpperLowerCase($password);
77
	}
78
	public function dataTestCheckUpperLowerCase() {
79
		return [
80
			['testPass', true],
81
			['Testpass', true],
82
			['testpass', false],
83
			['TESTPASS', false],
84
		];
85
	}
86
	/**
87
	 * @dataProvider dataTestCheckUpperLowerCaseFail
88
	 * @param string $password
89
	 * @expectedException \OC\HintException
90
	 */
91
	public function testCheckUpperLowerCaseFail($password) {
92
		$this->config->expects($this->once())->method('getIsUpperLowerCaseEnforced')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OCA\Security\SecurityConfig>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
93
		$this->passValidator->checkUpperLowerCase($password);
94
	}
95
	public function dataTestCheckUpperLowerCaseFail() {
96
		return [
97
			['TESTPASS'], ['testpass']
98
		];
99
	}
100
	/**
101
	 * @dataProvider dataTestCheckNumericCharacters
102
	 *
103
	 * @param string $password
104
	 * @param bool $enforceNumericCharacters
105
	 */
106
	public function testCheckNumericCharacters($password, $enforceNumericCharacters) {
107
		$this->config->expects($this->once())->method('getIsNumericCharactersEnforced')->willReturn($enforceNumericCharacters);
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OCA\Security\SecurityConfig>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
108
		$this->passValidator->checkNumericCharacters($password);
109
	}
110
	public function dataTestCheckNumericCharacters() {
111
		return [
112
			['testPass1', true],
113
			['testpass', false]
114
		];
115
	}
116
	/**
117
	 * @dataProvider dataTestCheckNumericCharactersFail
118
	 * @param string $password
119
	 * @expectedException \OC\HintException
120
	 */
121
	public function testCheckNumericCharactersFail($password) {
122
		$this->config->expects($this->once())->method('getIsNumericCharactersEnforced')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OCA\Security\SecurityConfig>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
123
		$this->passValidator->checkNumericCharacters($password);
124
	}
125
	public function dataTestCheckNumericCharactersFail() {
126
		return [
127
			['testpass'],
128
			['TestPass%'],
129
			['TEST*PASS']
130
		];
131
	}
132
	/**
133
	 * @dataProvider dataTestCheckSpecialCharacters
134
	 *
135
	 * @param string $password
136
	 * @param bool $enforceSpecialCharacters
137
	 */
138
	public function testCheckSpecialCharacters($password, $enforceSpecialCharacters) {
139
		$this->config->expects($this->once())->method('getIsSpecialCharactersEnforced')->willReturn($enforceSpecialCharacters);
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OCA\Security\SecurityConfig>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
140
		$this->passValidator->checkSpecialCharacters($password);
141
	}
142
	public function dataTestCheckSpecialCharacters() {
143
		return [
144
			['testPass%', true],
145
			['testpass', false]
146
		];
147
	}
148
	/**
149
	 * @dataProvider dataTestCheckSpecialCharactersFail
150
	 * @param string $password
151
	 * @expectedException \OC\HintException
152
	 */
153
	public function testCheckSpecialCharactersFail($password) {
154
		$this->config->expects($this->once())->method('getIsSpecialCharactersEnforced')->willReturn(true);
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OCA\Security\SecurityConfig>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
155
		$this->passValidator->checkSpecialCharacters($password);
156
	}
157
	public function dataTestCheckSpecialCharactersFail() {
158
		return [
159
			['testpass'],
160
			['TestPass1'],
161
			['TEST2PASS']
162
		];
163
	}
164
	public function testValidate() {
165
		$password = 'password';
166
		$instance = $this->getMockInstance(
167
			[
168
				'checkPasswordLength',
169
				'checkUpperLowerCase',
170
				'checkNumericCharacters',
171
				'checkSpecialCharacters',
172
			]
173
		);
174
		$instance->expects($this->once())->method('checkPasswordLength')->with($password);
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OCA\Security\PasswordValidator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
175
		$instance->expects($this->once())->method('checkUpperLowerCase')->with($password);
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OCA\Security\PasswordValidator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
176
		$instance->expects($this->once())->method('checkNumericCharacters')->with($password);
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OCA\Security\PasswordValidator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
177
		$instance->expects($this->once())->method('checkSpecialCharacters')->with($password);
0 ignored issues
show
Bug introduced by
The method expects() does not seem to exist on object<OCA\Security\PasswordValidator>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
178
		$instance->validate($password);
179
	}
180
}