Completed
Push — master ( 3911d6...b5b320 )
by
unknown
31:08
created
tests/lib/Security/CSP/ContentSecurityPolicyNonceManagerTest.php 1 patch
Indentation   +44 added lines, -44 removed lines patch added patch discarded remove patch
@@ -17,55 +17,55 @@
 block discarded – undo
17 17
 use Test\TestCase;
18 18
 
19 19
 class ContentSecurityPolicyNonceManagerTest extends TestCase {
20
-	/** @var CsrfTokenManager&MockObject */
21
-	private $CSRFTokenManager;
22
-	/** @var Request&MockObject */
23
-	private $request;
24
-	/** @var ContentSecurityPolicyNonceManager */
25
-	private $nonceManager;
20
+    /** @var CsrfTokenManager&MockObject */
21
+    private $CSRFTokenManager;
22
+    /** @var Request&MockObject */
23
+    private $request;
24
+    /** @var ContentSecurityPolicyNonceManager */
25
+    private $nonceManager;
26 26
 
27
-	protected function setUp(): void {
28
-		$this->CSRFTokenManager = $this->createMock(CsrfTokenManager::class);
29
-		$this->request = $this->createMock(Request::class);
30
-		$this->nonceManager = new ContentSecurityPolicyNonceManager(
31
-			$this->CSRFTokenManager,
32
-			$this->request
33
-		);
34
-	}
27
+    protected function setUp(): void {
28
+        $this->CSRFTokenManager = $this->createMock(CsrfTokenManager::class);
29
+        $this->request = $this->createMock(Request::class);
30
+        $this->nonceManager = new ContentSecurityPolicyNonceManager(
31
+            $this->CSRFTokenManager,
32
+            $this->request
33
+        );
34
+    }
35 35
 
36
-	public function testGetNonce(): void {
37
-		$secret = base64_encode('secret');
38
-		$tokenValue = base64_encode('secret' ^ 'value_') . ':' . $secret;
39
-		$token = $this->createMock(CsrfToken::class);
40
-		$token
41
-			->expects($this->once())
42
-			->method('getEncryptedValue')
43
-			->willReturn($tokenValue);
36
+    public function testGetNonce(): void {
37
+        $secret = base64_encode('secret');
38
+        $tokenValue = base64_encode('secret' ^ 'value_') . ':' . $secret;
39
+        $token = $this->createMock(CsrfToken::class);
40
+        $token
41
+            ->expects($this->once())
42
+            ->method('getEncryptedValue')
43
+            ->willReturn($tokenValue);
44 44
 
45
-		$this->CSRFTokenManager
46
-			->expects($this->once())
47
-			->method('getToken')
48
-			->willReturn($token);
45
+        $this->CSRFTokenManager
46
+            ->expects($this->once())
47
+            ->method('getToken')
48
+            ->willReturn($token);
49 49
 
50
-		$this->assertSame($secret, $this->nonceManager->getNonce());
51
-		// call it twice but `getEncryptedValue` is expected to be called only once
52
-		$this->assertSame($secret, $this->nonceManager->getNonce());
53
-	}
50
+        $this->assertSame($secret, $this->nonceManager->getNonce());
51
+        // call it twice but `getEncryptedValue` is expected to be called only once
52
+        $this->assertSame($secret, $this->nonceManager->getNonce());
53
+    }
54 54
 
55
-	public function testGetNonceServerVar(): void {
56
-		$token = 'SERVERNONCE';
57
-		$this->request
58
-			->method('__isset')
59
-			->with('server')
60
-			->willReturn(true);
55
+    public function testGetNonceServerVar(): void {
56
+        $token = 'SERVERNONCE';
57
+        $this->request
58
+            ->method('__isset')
59
+            ->with('server')
60
+            ->willReturn(true);
61 61
 
62
-		$this->request
63
-			->method('__get')
64
-			->with('server')
65
-			->willReturn(['CSP_NONCE' => $token]);
62
+        $this->request
63
+            ->method('__get')
64
+            ->with('server')
65
+            ->willReturn(['CSP_NONCE' => $token]);
66 66
 
67
-		$this->assertSame($token, $this->nonceManager->getNonce());
68
-		// call it twice but `CSP_NONCE` variable is expected to be loaded only once
69
-		$this->assertSame($token, $this->nonceManager->getNonce());
70
-	}
67
+        $this->assertSame($token, $this->nonceManager->getNonce());
68
+        // call it twice but `CSP_NONCE` variable is expected to be loaded only once
69
+        $this->assertSame($token, $this->nonceManager->getNonce());
70
+    }
71 71
 }
Please login to merge, or discard this patch.
tests/lib/Security/CSP/AddContentSecurityPolicyEventTest.php 1 patch
Indentation   +9 added lines, -9 removed lines patch added patch discarded remove patch
@@ -15,15 +15,15 @@
 block discarded – undo
15 15
 use Test\TestCase;
16 16
 
17 17
 class AddContentSecurityPolicyEventTest extends TestCase {
18
-	public function testAddEvent(): void {
19
-		$cspManager = $this->createMock(ContentSecurityPolicyManager::class);
20
-		$policy = $this->createMock(ContentSecurityPolicy::class);
21
-		$event = new AddContentSecurityPolicyEvent($cspManager);
18
+    public function testAddEvent(): void {
19
+        $cspManager = $this->createMock(ContentSecurityPolicyManager::class);
20
+        $policy = $this->createMock(ContentSecurityPolicy::class);
21
+        $event = new AddContentSecurityPolicyEvent($cspManager);
22 22
 
23
-		$cspManager->expects($this->once())
24
-			->method('addDefaultPolicy')
25
-			->with($policy);
23
+        $cspManager->expects($this->once())
24
+            ->method('addDefaultPolicy')
25
+            ->with($policy);
26 26
 
27
-		$event->addPolicy($policy);
28
-	}
27
+        $event->addPolicy($policy);
28
+    }
29 29
 }
Please login to merge, or discard this patch.
tests/lib/Security/Events/GenerateSecurePasswordEventTest.php 1 patch
Indentation   +16 added lines, -16 removed lines patch added patch discarded remove patch
@@ -14,20 +14,20 @@
 block discarded – undo
14 14
 
15 15
 class GenerateSecurePasswordEventTest extends \Test\TestCase {
16 16
 
17
-	public function testDefaultProperties(): void {
18
-		$event = new GenerateSecurePasswordEvent();
19
-		$this->assertNull($event->getPassword());
20
-		$this->assertEquals(PasswordContext::ACCOUNT, $event->getContext());
21
-	}
22
-
23
-	public function testSettingPassword(): void {
24
-		$event = new GenerateSecurePasswordEvent();
25
-		$event->setPassword('example');
26
-		$this->assertEquals('example', $event->getPassword());
27
-	}
28
-
29
-	public function testSettingContext(): void {
30
-		$event = new GenerateSecurePasswordEvent(PasswordContext::SHARING);
31
-		$this->assertEquals(PasswordContext::SHARING, $event->getContext());
32
-	}
17
+    public function testDefaultProperties(): void {
18
+        $event = new GenerateSecurePasswordEvent();
19
+        $this->assertNull($event->getPassword());
20
+        $this->assertEquals(PasswordContext::ACCOUNT, $event->getContext());
21
+    }
22
+
23
+    public function testSettingPassword(): void {
24
+        $event = new GenerateSecurePasswordEvent();
25
+        $event->setPassword('example');
26
+        $this->assertEquals('example', $event->getPassword());
27
+    }
28
+
29
+    public function testSettingContext(): void {
30
+        $event = new GenerateSecurePasswordEvent(PasswordContext::SHARING);
31
+        $this->assertEquals(PasswordContext::SHARING, $event->getContext());
32
+    }
33 33
 }
Please login to merge, or discard this patch.
tests/lib/Security/Events/ValidatePasswordPolicyEventTest.php 1 patch
Indentation   +10 added lines, -10 removed lines patch added patch discarded remove patch
@@ -14,15 +14,15 @@
 block discarded – undo
14 14
 
15 15
 class ValidatePasswordPolicyEventTest extends \Test\TestCase {
16 16
 
17
-	public function testDefaultProperties(): void {
18
-		$password = 'example';
19
-		$event = new ValidatePasswordPolicyEvent($password);
20
-		$this->assertEquals($password, $event->getPassword());
21
-		$this->assertEquals(PasswordContext::ACCOUNT, $event->getContext());
22
-	}
17
+    public function testDefaultProperties(): void {
18
+        $password = 'example';
19
+        $event = new ValidatePasswordPolicyEvent($password);
20
+        $this->assertEquals($password, $event->getPassword());
21
+        $this->assertEquals(PasswordContext::ACCOUNT, $event->getContext());
22
+    }
23 23
 
24
-	public function testSettingContext(): void {
25
-		$event = new ValidatePasswordPolicyEvent('example', PasswordContext::SHARING);
26
-		$this->assertEquals(PasswordContext::SHARING, $event->getContext());
27
-	}
24
+    public function testSettingContext(): void {
25
+        $event = new ValidatePasswordPolicyEvent('example', PasswordContext::SHARING);
26
+        $this->assertEquals(PasswordContext::SHARING, $event->getContext());
27
+    }
28 28
 }
Please login to merge, or discard this patch.
tests/lib/Security/VerificationToken/VerificationTokenTest.php 1 patch
Indentation   +271 added lines, -271 removed lines patch added patch discarded remove patch
@@ -21,275 +21,275 @@
 block discarded – undo
21 21
 use Test\TestCase;
22 22
 
23 23
 class VerificationTokenTest extends TestCase {
24
-	/** @var VerificationToken */
25
-	protected $token;
26
-	/** @var IConfig|MockObject */
27
-	protected $config;
28
-	/** @var ISecureRandom|MockObject */
29
-	protected $secureRandom;
30
-	/** @var ICrypto|MockObject */
31
-	protected $crypto;
32
-	/** @var ITimeFactory|MockObject */
33
-	protected $timeFactory;
34
-	/** @var IJobList|MockObject */
35
-	protected $jobList;
36
-
37
-	protected function setUp(): void {
38
-		parent::setUp();
39
-
40
-		$this->config = $this->createMock(IConfig::class);
41
-		$this->crypto = $this->createMock(ICrypto::class);
42
-		$this->timeFactory = $this->createMock(ITimeFactory::class);
43
-		$this->secureRandom = $this->createMock(ISecureRandom::class);
44
-		$this->jobList = $this->createMock(IJobList::class);
45
-
46
-		$this->token = new VerificationToken(
47
-			$this->config,
48
-			$this->crypto,
49
-			$this->timeFactory,
50
-			$this->secureRandom,
51
-			$this->jobList
52
-		);
53
-	}
54
-
55
-	public function testTokenUserUnknown(): void {
56
-		$this->expectException(InvalidTokenException::class);
57
-		$this->expectExceptionCode(InvalidTokenException::USER_UNKNOWN);
58
-		$this->token->check('encryptedToken', null, 'fingerprintToken', 'foobar');
59
-	}
60
-
61
-	public function testTokenUserUnknown2(): void {
62
-		$user = $this->createMock(IUser::class);
63
-		$user->expects($this->atLeastOnce())
64
-			->method('isEnabled')
65
-			->willReturn(false);
66
-
67
-		$this->expectException(InvalidTokenException::class);
68
-		$this->expectExceptionCode(InvalidTokenException::USER_UNKNOWN);
69
-		$this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
70
-	}
71
-
72
-	public function testTokenNotFound(): void {
73
-		$user = $this->createMock(IUser::class);
74
-		$user->expects($this->atLeastOnce())
75
-			->method('isEnabled')
76
-			->willReturn(true);
77
-		$user->expects($this->atLeastOnce())
78
-			->method('getUID')
79
-			->willReturn('alice');
80
-
81
-		// implicit: IConfig::getUserValue returns null by default
82
-
83
-		$this->expectException(InvalidTokenException::class);
84
-		$this->expectExceptionCode(InvalidTokenException::TOKEN_NOT_FOUND);
85
-		$this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
86
-	}
87
-
88
-	public function testTokenDecryptionError(): void {
89
-		$user = $this->createMock(IUser::class);
90
-		$user->expects($this->atLeastOnce())
91
-			->method('isEnabled')
92
-			->willReturn(true);
93
-		$user->expects($this->atLeastOnce())
94
-			->method('getUID')
95
-			->willReturn('alice');
96
-
97
-		$this->config->expects($this->atLeastOnce())
98
-			->method('getUserValue')
99
-			->with('alice', 'core', 'fingerprintToken', null)
100
-			->willReturn('encryptedToken');
101
-		$this->config->expects($this->any())
102
-			->method('getSystemValueString')
103
-			->with('secret')
104
-			->willReturn('357111317');
105
-
106
-		$this->crypto->method('decrypt')
107
-			->with('encryptedToken', 'foobar' . '357111317')
108
-			->willThrowException(new \Exception('decryption failed'));
109
-
110
-		$this->expectException(InvalidTokenException::class);
111
-		$this->expectExceptionCode(InvalidTokenException::TOKEN_DECRYPTION_ERROR);
112
-		$this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
113
-	}
114
-
115
-	public function testTokenInvalidFormat(): void {
116
-		$user = $this->createMock(IUser::class);
117
-		$user->expects($this->atLeastOnce())
118
-			->method('isEnabled')
119
-			->willReturn(true);
120
-		$user->expects($this->atLeastOnce())
121
-			->method('getUID')
122
-			->willReturn('alice');
123
-
124
-		$this->config->expects($this->atLeastOnce())
125
-			->method('getUserValue')
126
-			->with('alice', 'core', 'fingerprintToken', null)
127
-			->willReturn('encryptedToken');
128
-		$this->config->expects($this->any())
129
-			->method('getSystemValueString')
130
-			->with('secret')
131
-			->willReturn('357111317');
132
-
133
-		$this->crypto->method('decrypt')
134
-			->with('encryptedToken', 'foobar' . '357111317')
135
-			->willReturn('decrypted^nonsense');
136
-
137
-		$this->expectException(InvalidTokenException::class);
138
-		$this->expectExceptionCode(InvalidTokenException::TOKEN_INVALID_FORMAT);
139
-		$this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
140
-	}
141
-
142
-	public function testTokenExpired(): void {
143
-		$user = $this->createMock(IUser::class);
144
-		$user->expects($this->atLeastOnce())
145
-			->method('isEnabled')
146
-			->willReturn(true);
147
-		$user->expects($this->atLeastOnce())
148
-			->method('getUID')
149
-			->willReturn('alice');
150
-		$user->expects($this->any())
151
-			->method('getLastLogin')
152
-			->willReturn(604803);
153
-
154
-		$this->config->expects($this->atLeastOnce())
155
-			->method('getUserValue')
156
-			->with('alice', 'core', 'fingerprintToken', null)
157
-			->willReturn('encryptedToken');
158
-		$this->config->expects($this->any())
159
-			->method('getSystemValueString')
160
-			->with('secret')
161
-			->willReturn('357111317');
162
-
163
-		$this->crypto->method('decrypt')
164
-			->with('encryptedToken', 'foobar' . '357111317')
165
-			->willReturn('604800:mY70K3n');
166
-
167
-		$this->timeFactory->expects($this->any())
168
-			->method('getTime')
169
-			->willReturn(604800 * 3);
170
-
171
-		$this->expectException(InvalidTokenException::class);
172
-		$this->expectExceptionCode(InvalidTokenException::TOKEN_EXPIRED);
173
-		$this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
174
-	}
175
-
176
-	public function testTokenExpiredByLogin(): void {
177
-		$user = $this->createMock(IUser::class);
178
-		$user->expects($this->atLeastOnce())
179
-			->method('isEnabled')
180
-			->willReturn(true);
181
-		$user->expects($this->atLeastOnce())
182
-			->method('getUID')
183
-			->willReturn('alice');
184
-		$user->expects($this->any())
185
-			->method('getLastLogin')
186
-			->willReturn(604803);
187
-
188
-		$this->config->expects($this->atLeastOnce())
189
-			->method('getUserValue')
190
-			->with('alice', 'core', 'fingerprintToken', null)
191
-			->willReturn('encryptedToken');
192
-		$this->config->expects($this->any())
193
-			->method('getSystemValueString')
194
-			->with('secret')
195
-			->willReturn('357111317');
196
-
197
-		$this->crypto->method('decrypt')
198
-			->with('encryptedToken', 'foobar' . '357111317')
199
-			->willReturn('604800:mY70K3n');
200
-
201
-		$this->timeFactory->expects($this->any())
202
-			->method('getTime')
203
-			->willReturn(604801);
204
-
205
-		$this->expectException(InvalidTokenException::class);
206
-		$this->expectExceptionCode(InvalidTokenException::TOKEN_EXPIRED);
207
-		$this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar', true);
208
-	}
209
-
210
-	public function testTokenMismatch(): void {
211
-		$user = $this->createMock(IUser::class);
212
-		$user->expects($this->atLeastOnce())
213
-			->method('isEnabled')
214
-			->willReturn(true);
215
-		$user->expects($this->atLeastOnce())
216
-			->method('getUID')
217
-			->willReturn('alice');
218
-		$user->expects($this->any())
219
-			->method('getLastLogin')
220
-			->willReturn(604703);
221
-
222
-		$this->config->expects($this->atLeastOnce())
223
-			->method('getUserValue')
224
-			->with('alice', 'core', 'fingerprintToken', null)
225
-			->willReturn('encryptedToken');
226
-		$this->config->expects($this->any())
227
-			->method('getSystemValueString')
228
-			->with('secret')
229
-			->willReturn('357111317');
230
-
231
-		$this->crypto->method('decrypt')
232
-			->with('encryptedToken', 'foobar' . '357111317')
233
-			->willReturn('604802:mY70K3n');
234
-
235
-		$this->timeFactory->expects($this->any())
236
-			->method('getTime')
237
-			->willReturn(604801);
238
-
239
-		$this->expectException(InvalidTokenException::class);
240
-		$this->expectExceptionCode(InvalidTokenException::TOKEN_MISMATCH);
241
-		$this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
242
-	}
243
-
244
-	public function testTokenSuccess(): void {
245
-		$user = $this->createMock(IUser::class);
246
-		$user->expects($this->atLeastOnce())
247
-			->method('isEnabled')
248
-			->willReturn(true);
249
-		$user->expects($this->atLeastOnce())
250
-			->method('getUID')
251
-			->willReturn('alice');
252
-		$user->expects($this->any())
253
-			->method('getLastLogin')
254
-			->willReturn(604703);
255
-
256
-		$this->config->expects($this->atLeastOnce())
257
-			->method('getUserValue')
258
-			->with('alice', 'core', 'fingerprintToken', null)
259
-			->willReturn('encryptedToken');
260
-		$this->config->expects($this->any())
261
-			->method('getSystemValueString')
262
-			->with('secret')
263
-			->willReturn('357111317');
264
-
265
-		$this->crypto->method('decrypt')
266
-			->with('encryptedToken', 'foobar' . '357111317')
267
-			->willReturn('604802:barfoo');
268
-
269
-		$this->timeFactory->expects($this->any())
270
-			->method('getTime')
271
-			->willReturn(604801);
272
-
273
-		$this->token->check('barfoo', $user, 'fingerprintToken', 'foobar');
274
-	}
275
-
276
-	public function testCreate(): void {
277
-		$user = $this->createMock(IUser::class);
278
-		$user->expects($this->any())
279
-			->method('getUID')
280
-			->willReturn('alice');
281
-
282
-		$this->secureRandom->expects($this->atLeastOnce())
283
-			->method('generate')
284
-			->willReturn('barfoo');
285
-		$this->crypto->expects($this->atLeastOnce())
286
-			->method('encrypt')
287
-			->willReturn('encryptedToken');
288
-		$this->config->expects($this->atLeastOnce())
289
-			->method('setUserValue')
290
-			->with('alice', 'core', 'fingerprintToken', 'encryptedToken');
291
-
292
-		$vToken = $this->token->create($user, 'fingerprintToken', 'foobar');
293
-		$this->assertSame('barfoo', $vToken);
294
-	}
24
+    /** @var VerificationToken */
25
+    protected $token;
26
+    /** @var IConfig|MockObject */
27
+    protected $config;
28
+    /** @var ISecureRandom|MockObject */
29
+    protected $secureRandom;
30
+    /** @var ICrypto|MockObject */
31
+    protected $crypto;
32
+    /** @var ITimeFactory|MockObject */
33
+    protected $timeFactory;
34
+    /** @var IJobList|MockObject */
35
+    protected $jobList;
36
+
37
+    protected function setUp(): void {
38
+        parent::setUp();
39
+
40
+        $this->config = $this->createMock(IConfig::class);
41
+        $this->crypto = $this->createMock(ICrypto::class);
42
+        $this->timeFactory = $this->createMock(ITimeFactory::class);
43
+        $this->secureRandom = $this->createMock(ISecureRandom::class);
44
+        $this->jobList = $this->createMock(IJobList::class);
45
+
46
+        $this->token = new VerificationToken(
47
+            $this->config,
48
+            $this->crypto,
49
+            $this->timeFactory,
50
+            $this->secureRandom,
51
+            $this->jobList
52
+        );
53
+    }
54
+
55
+    public function testTokenUserUnknown(): void {
56
+        $this->expectException(InvalidTokenException::class);
57
+        $this->expectExceptionCode(InvalidTokenException::USER_UNKNOWN);
58
+        $this->token->check('encryptedToken', null, 'fingerprintToken', 'foobar');
59
+    }
60
+
61
+    public function testTokenUserUnknown2(): void {
62
+        $user = $this->createMock(IUser::class);
63
+        $user->expects($this->atLeastOnce())
64
+            ->method('isEnabled')
65
+            ->willReturn(false);
66
+
67
+        $this->expectException(InvalidTokenException::class);
68
+        $this->expectExceptionCode(InvalidTokenException::USER_UNKNOWN);
69
+        $this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
70
+    }
71
+
72
+    public function testTokenNotFound(): void {
73
+        $user = $this->createMock(IUser::class);
74
+        $user->expects($this->atLeastOnce())
75
+            ->method('isEnabled')
76
+            ->willReturn(true);
77
+        $user->expects($this->atLeastOnce())
78
+            ->method('getUID')
79
+            ->willReturn('alice');
80
+
81
+        // implicit: IConfig::getUserValue returns null by default
82
+
83
+        $this->expectException(InvalidTokenException::class);
84
+        $this->expectExceptionCode(InvalidTokenException::TOKEN_NOT_FOUND);
85
+        $this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
86
+    }
87
+
88
+    public function testTokenDecryptionError(): void {
89
+        $user = $this->createMock(IUser::class);
90
+        $user->expects($this->atLeastOnce())
91
+            ->method('isEnabled')
92
+            ->willReturn(true);
93
+        $user->expects($this->atLeastOnce())
94
+            ->method('getUID')
95
+            ->willReturn('alice');
96
+
97
+        $this->config->expects($this->atLeastOnce())
98
+            ->method('getUserValue')
99
+            ->with('alice', 'core', 'fingerprintToken', null)
100
+            ->willReturn('encryptedToken');
101
+        $this->config->expects($this->any())
102
+            ->method('getSystemValueString')
103
+            ->with('secret')
104
+            ->willReturn('357111317');
105
+
106
+        $this->crypto->method('decrypt')
107
+            ->with('encryptedToken', 'foobar' . '357111317')
108
+            ->willThrowException(new \Exception('decryption failed'));
109
+
110
+        $this->expectException(InvalidTokenException::class);
111
+        $this->expectExceptionCode(InvalidTokenException::TOKEN_DECRYPTION_ERROR);
112
+        $this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
113
+    }
114
+
115
+    public function testTokenInvalidFormat(): void {
116
+        $user = $this->createMock(IUser::class);
117
+        $user->expects($this->atLeastOnce())
118
+            ->method('isEnabled')
119
+            ->willReturn(true);
120
+        $user->expects($this->atLeastOnce())
121
+            ->method('getUID')
122
+            ->willReturn('alice');
123
+
124
+        $this->config->expects($this->atLeastOnce())
125
+            ->method('getUserValue')
126
+            ->with('alice', 'core', 'fingerprintToken', null)
127
+            ->willReturn('encryptedToken');
128
+        $this->config->expects($this->any())
129
+            ->method('getSystemValueString')
130
+            ->with('secret')
131
+            ->willReturn('357111317');
132
+
133
+        $this->crypto->method('decrypt')
134
+            ->with('encryptedToken', 'foobar' . '357111317')
135
+            ->willReturn('decrypted^nonsense');
136
+
137
+        $this->expectException(InvalidTokenException::class);
138
+        $this->expectExceptionCode(InvalidTokenException::TOKEN_INVALID_FORMAT);
139
+        $this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
140
+    }
141
+
142
+    public function testTokenExpired(): void {
143
+        $user = $this->createMock(IUser::class);
144
+        $user->expects($this->atLeastOnce())
145
+            ->method('isEnabled')
146
+            ->willReturn(true);
147
+        $user->expects($this->atLeastOnce())
148
+            ->method('getUID')
149
+            ->willReturn('alice');
150
+        $user->expects($this->any())
151
+            ->method('getLastLogin')
152
+            ->willReturn(604803);
153
+
154
+        $this->config->expects($this->atLeastOnce())
155
+            ->method('getUserValue')
156
+            ->with('alice', 'core', 'fingerprintToken', null)
157
+            ->willReturn('encryptedToken');
158
+        $this->config->expects($this->any())
159
+            ->method('getSystemValueString')
160
+            ->with('secret')
161
+            ->willReturn('357111317');
162
+
163
+        $this->crypto->method('decrypt')
164
+            ->with('encryptedToken', 'foobar' . '357111317')
165
+            ->willReturn('604800:mY70K3n');
166
+
167
+        $this->timeFactory->expects($this->any())
168
+            ->method('getTime')
169
+            ->willReturn(604800 * 3);
170
+
171
+        $this->expectException(InvalidTokenException::class);
172
+        $this->expectExceptionCode(InvalidTokenException::TOKEN_EXPIRED);
173
+        $this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
174
+    }
175
+
176
+    public function testTokenExpiredByLogin(): void {
177
+        $user = $this->createMock(IUser::class);
178
+        $user->expects($this->atLeastOnce())
179
+            ->method('isEnabled')
180
+            ->willReturn(true);
181
+        $user->expects($this->atLeastOnce())
182
+            ->method('getUID')
183
+            ->willReturn('alice');
184
+        $user->expects($this->any())
185
+            ->method('getLastLogin')
186
+            ->willReturn(604803);
187
+
188
+        $this->config->expects($this->atLeastOnce())
189
+            ->method('getUserValue')
190
+            ->with('alice', 'core', 'fingerprintToken', null)
191
+            ->willReturn('encryptedToken');
192
+        $this->config->expects($this->any())
193
+            ->method('getSystemValueString')
194
+            ->with('secret')
195
+            ->willReturn('357111317');
196
+
197
+        $this->crypto->method('decrypt')
198
+            ->with('encryptedToken', 'foobar' . '357111317')
199
+            ->willReturn('604800:mY70K3n');
200
+
201
+        $this->timeFactory->expects($this->any())
202
+            ->method('getTime')
203
+            ->willReturn(604801);
204
+
205
+        $this->expectException(InvalidTokenException::class);
206
+        $this->expectExceptionCode(InvalidTokenException::TOKEN_EXPIRED);
207
+        $this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar', true);
208
+    }
209
+
210
+    public function testTokenMismatch(): void {
211
+        $user = $this->createMock(IUser::class);
212
+        $user->expects($this->atLeastOnce())
213
+            ->method('isEnabled')
214
+            ->willReturn(true);
215
+        $user->expects($this->atLeastOnce())
216
+            ->method('getUID')
217
+            ->willReturn('alice');
218
+        $user->expects($this->any())
219
+            ->method('getLastLogin')
220
+            ->willReturn(604703);
221
+
222
+        $this->config->expects($this->atLeastOnce())
223
+            ->method('getUserValue')
224
+            ->with('alice', 'core', 'fingerprintToken', null)
225
+            ->willReturn('encryptedToken');
226
+        $this->config->expects($this->any())
227
+            ->method('getSystemValueString')
228
+            ->with('secret')
229
+            ->willReturn('357111317');
230
+
231
+        $this->crypto->method('decrypt')
232
+            ->with('encryptedToken', 'foobar' . '357111317')
233
+            ->willReturn('604802:mY70K3n');
234
+
235
+        $this->timeFactory->expects($this->any())
236
+            ->method('getTime')
237
+            ->willReturn(604801);
238
+
239
+        $this->expectException(InvalidTokenException::class);
240
+        $this->expectExceptionCode(InvalidTokenException::TOKEN_MISMATCH);
241
+        $this->token->check('encryptedToken', $user, 'fingerprintToken', 'foobar');
242
+    }
243
+
244
+    public function testTokenSuccess(): void {
245
+        $user = $this->createMock(IUser::class);
246
+        $user->expects($this->atLeastOnce())
247
+            ->method('isEnabled')
248
+            ->willReturn(true);
249
+        $user->expects($this->atLeastOnce())
250
+            ->method('getUID')
251
+            ->willReturn('alice');
252
+        $user->expects($this->any())
253
+            ->method('getLastLogin')
254
+            ->willReturn(604703);
255
+
256
+        $this->config->expects($this->atLeastOnce())
257
+            ->method('getUserValue')
258
+            ->with('alice', 'core', 'fingerprintToken', null)
259
+            ->willReturn('encryptedToken');
260
+        $this->config->expects($this->any())
261
+            ->method('getSystemValueString')
262
+            ->with('secret')
263
+            ->willReturn('357111317');
264
+
265
+        $this->crypto->method('decrypt')
266
+            ->with('encryptedToken', 'foobar' . '357111317')
267
+            ->willReturn('604802:barfoo');
268
+
269
+        $this->timeFactory->expects($this->any())
270
+            ->method('getTime')
271
+            ->willReturn(604801);
272
+
273
+        $this->token->check('barfoo', $user, 'fingerprintToken', 'foobar');
274
+    }
275
+
276
+    public function testCreate(): void {
277
+        $user = $this->createMock(IUser::class);
278
+        $user->expects($this->any())
279
+            ->method('getUID')
280
+            ->willReturn('alice');
281
+
282
+        $this->secureRandom->expects($this->atLeastOnce())
283
+            ->method('generate')
284
+            ->willReturn('barfoo');
285
+        $this->crypto->expects($this->atLeastOnce())
286
+            ->method('encrypt')
287
+            ->willReturn('encryptedToken');
288
+        $this->config->expects($this->atLeastOnce())
289
+            ->method('setUserValue')
290
+            ->with('alice', 'core', 'fingerprintToken', 'encryptedToken');
291
+
292
+        $vToken = $this->token->create($user, 'fingerprintToken', 'foobar');
293
+        $this->assertSame('barfoo', $vToken);
294
+    }
295 295
 }
Please login to merge, or discard this patch.
tests/lib/Security/RateLimiting/Backend/MemoryCacheBackendTest.php 1 patch
Indentation   +123 added lines, -123 removed lines patch added patch discarded remove patch
@@ -17,127 +17,127 @@
 block discarded – undo
17 17
 use Test\TestCase;
18 18
 
19 19
 class MemoryCacheBackendTest extends TestCase {
20
-	/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
21
-	private $config;
22
-	/** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
23
-	private $cacheFactory;
24
-	/** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
25
-	private $timeFactory;
26
-	/** @var ICache|\PHPUnit\Framework\MockObject\MockObject */
27
-	private $cache;
28
-	/** @var MemoryCacheBackend */
29
-	private $memoryCache;
30
-
31
-	protected function setUp(): void {
32
-		parent::setUp();
33
-
34
-		$this->config = $this->createMock(IConfig::class);
35
-		$this->cacheFactory = $this->createMock(ICacheFactory::class);
36
-		$this->timeFactory = $this->createMock(ITimeFactory::class);
37
-		$this->cache = $this->createMock(ICache::class);
38
-
39
-		$this->cacheFactory
40
-			->expects($this->once())
41
-			->method('createDistributed')
42
-			->with('OC\Security\RateLimiting\Backend\MemoryCacheBackend')
43
-			->willReturn($this->cache);
44
-
45
-		$this->config->method('getSystemValueBool')
46
-			->with('ratelimit.protection.enabled')
47
-			->willReturn(true);
48
-
49
-		$this->memoryCache = new MemoryCacheBackend(
50
-			$this->config,
51
-			$this->cacheFactory,
52
-			$this->timeFactory
53
-		);
54
-	}
55
-
56
-	public function testGetAttemptsWithNoAttemptsBefore(): void {
57
-		$this->cache
58
-			->expects($this->once())
59
-			->method('get')
60
-			->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
61
-			->willReturn(null);
62
-
63
-		$this->assertSame(0, $this->memoryCache->getAttempts('Method', 'User'));
64
-	}
65
-
66
-	public function testGetAttempts(): void {
67
-		$this->timeFactory
68
-			->expects($this->once())
69
-			->method('getTime')
70
-			->willReturn(210);
71
-		$this->cache
72
-			->expects($this->once())
73
-			->method('get')
74
-			->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
75
-			->willReturn(json_encode([
76
-				'1',
77
-				'2',
78
-				'87',
79
-				'223',
80
-				'223',
81
-				'224',
82
-			]));
83
-
84
-		$this->assertSame(3, $this->memoryCache->getAttempts('Method', 'User'));
85
-	}
86
-
87
-	public function testRegisterAttemptWithNoAttemptsBefore(): void {
88
-		$this->timeFactory
89
-			->expects($this->once())
90
-			->method('getTime')
91
-			->willReturn(123);
92
-
93
-		$this->cache
94
-			->expects($this->once())
95
-			->method('get')
96
-			->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
97
-			->willReturn(null);
98
-		$this->cache
99
-			->expects($this->once())
100
-			->method('set')
101
-			->with(
102
-				'eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b',
103
-				json_encode(['223'])
104
-			);
105
-
106
-		$this->memoryCache->registerAttempt('Method', 'User', 100);
107
-	}
108
-
109
-	public function testRegisterAttempt(): void {
110
-		$this->timeFactory
111
-			->expects($this->once())
112
-			->method('getTime')
113
-			->willReturn(86);
114
-
115
-		$this->cache
116
-			->expects($this->once())
117
-			->method('get')
118
-			->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
119
-			->willReturn(json_encode([
120
-				'1',
121
-				'2',
122
-				'87',
123
-				'123',
124
-				'123',
125
-				'124',
126
-			]));
127
-		$this->cache
128
-			->expects($this->once())
129
-			->method('set')
130
-			->with(
131
-				'eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b',
132
-				json_encode([
133
-					'87',
134
-					'123',
135
-					'123',
136
-					'124',
137
-					'186',
138
-				])
139
-			);
140
-
141
-		$this->memoryCache->registerAttempt('Method', 'User', 100);
142
-	}
20
+    /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
21
+    private $config;
22
+    /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
23
+    private $cacheFactory;
24
+    /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
25
+    private $timeFactory;
26
+    /** @var ICache|\PHPUnit\Framework\MockObject\MockObject */
27
+    private $cache;
28
+    /** @var MemoryCacheBackend */
29
+    private $memoryCache;
30
+
31
+    protected function setUp(): void {
32
+        parent::setUp();
33
+
34
+        $this->config = $this->createMock(IConfig::class);
35
+        $this->cacheFactory = $this->createMock(ICacheFactory::class);
36
+        $this->timeFactory = $this->createMock(ITimeFactory::class);
37
+        $this->cache = $this->createMock(ICache::class);
38
+
39
+        $this->cacheFactory
40
+            ->expects($this->once())
41
+            ->method('createDistributed')
42
+            ->with('OC\Security\RateLimiting\Backend\MemoryCacheBackend')
43
+            ->willReturn($this->cache);
44
+
45
+        $this->config->method('getSystemValueBool')
46
+            ->with('ratelimit.protection.enabled')
47
+            ->willReturn(true);
48
+
49
+        $this->memoryCache = new MemoryCacheBackend(
50
+            $this->config,
51
+            $this->cacheFactory,
52
+            $this->timeFactory
53
+        );
54
+    }
55
+
56
+    public function testGetAttemptsWithNoAttemptsBefore(): void {
57
+        $this->cache
58
+            ->expects($this->once())
59
+            ->method('get')
60
+            ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
61
+            ->willReturn(null);
62
+
63
+        $this->assertSame(0, $this->memoryCache->getAttempts('Method', 'User'));
64
+    }
65
+
66
+    public function testGetAttempts(): void {
67
+        $this->timeFactory
68
+            ->expects($this->once())
69
+            ->method('getTime')
70
+            ->willReturn(210);
71
+        $this->cache
72
+            ->expects($this->once())
73
+            ->method('get')
74
+            ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
75
+            ->willReturn(json_encode([
76
+                '1',
77
+                '2',
78
+                '87',
79
+                '223',
80
+                '223',
81
+                '224',
82
+            ]));
83
+
84
+        $this->assertSame(3, $this->memoryCache->getAttempts('Method', 'User'));
85
+    }
86
+
87
+    public function testRegisterAttemptWithNoAttemptsBefore(): void {
88
+        $this->timeFactory
89
+            ->expects($this->once())
90
+            ->method('getTime')
91
+            ->willReturn(123);
92
+
93
+        $this->cache
94
+            ->expects($this->once())
95
+            ->method('get')
96
+            ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
97
+            ->willReturn(null);
98
+        $this->cache
99
+            ->expects($this->once())
100
+            ->method('set')
101
+            ->with(
102
+                'eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b',
103
+                json_encode(['223'])
104
+            );
105
+
106
+        $this->memoryCache->registerAttempt('Method', 'User', 100);
107
+    }
108
+
109
+    public function testRegisterAttempt(): void {
110
+        $this->timeFactory
111
+            ->expects($this->once())
112
+            ->method('getTime')
113
+            ->willReturn(86);
114
+
115
+        $this->cache
116
+            ->expects($this->once())
117
+            ->method('get')
118
+            ->with('eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b')
119
+            ->willReturn(json_encode([
120
+                '1',
121
+                '2',
122
+                '87',
123
+                '123',
124
+                '123',
125
+                '124',
126
+            ]));
127
+        $this->cache
128
+            ->expects($this->once())
129
+            ->method('set')
130
+            ->with(
131
+                'eea460b8d756885099c7f0a4c083bf6a745069ee4a301984e726df58fd4510bffa2dac4b7fd5d835726a6753ffa8343ba31c7e902bbef78fc68c2e743667cb4b',
132
+                json_encode([
133
+                    '87',
134
+                    '123',
135
+                    '123',
136
+                    '124',
137
+                    '186',
138
+                ])
139
+            );
140
+
141
+        $this->memoryCache->registerAttempt('Method', 'User', 100);
142
+    }
143 143
 }
Please login to merge, or discard this patch.
tests/lib/EventSourceFactoryTest.php 1 patch
Indentation   +6 added lines, -6 removed lines patch added patch discarded remove patch
@@ -11,11 +11,11 @@
 block discarded – undo
11 11
 use OCP\IRequest;
12 12
 
13 13
 class EventSourceFactoryTest extends TestCase {
14
-	public function testCreate(): void {
15
-		$request = $this->createMock(IRequest::class);
16
-		$factory = new EventSourceFactory($request);
14
+    public function testCreate(): void {
15
+        $request = $this->createMock(IRequest::class);
16
+        $factory = new EventSourceFactory($request);
17 17
 
18
-		$instance = $factory->create();
19
-		$this->assertInstanceOf(IEventSource::class, $instance);
20
-	}
18
+        $instance = $factory->create();
19
+        $this->assertInstanceOf(IEventSource::class, $instance);
20
+    }
21 21
 }
Please login to merge, or discard this patch.
tests/lib/Template/JSResourceLocatorTest.php 1 patch
Indentation   +162 added lines, -162 removed lines patch added patch discarded remove patch
@@ -17,166 +17,166 @@
 block discarded – undo
17 17
 use Psr\Log\LoggerInterface;
18 18
 
19 19
 class JSResourceLocatorTest extends \Test\TestCase {
20
-	/** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
21
-	protected $appData;
22
-	/** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
23
-	protected $urlGenerator;
24
-	/** @var SystemConfig|\PHPUnit\Framework\MockObject\MockObject */
25
-	protected $config;
26
-	/** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
27
-	protected $cacheFactory;
28
-	/** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
29
-	protected $logger;
30
-	/** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
31
-	protected $appManager;
32
-
33
-	protected function setUp(): void {
34
-		parent::setUp();
35
-
36
-		$this->appData = $this->createMock(IAppData::class);
37
-		$this->urlGenerator = $this->createMock(IURLGenerator::class);
38
-		$this->config = $this->createMock(SystemConfig::class);
39
-		$this->cacheFactory = $this->createMock(ICacheFactory::class);
40
-		$this->logger = $this->createMock(LoggerInterface::class);
41
-		$this->appManager = $this->createMock(IAppManager::class);
42
-	}
43
-
44
-	private function jsResourceLocator() {
45
-		$jsCombiner = new JSCombiner(
46
-			$this->appData,
47
-			$this->urlGenerator,
48
-			$this->cacheFactory,
49
-			$this->config,
50
-			$this->logger
51
-		);
52
-		return new JSResourceLocator(
53
-			$this->logger,
54
-			$jsCombiner,
55
-			$this->appManager,
56
-		);
57
-	}
58
-
59
-	private function rrmdir($directory) {
60
-		$files = array_diff(scandir($directory), ['.','..']);
61
-		foreach ($files as $file) {
62
-			if (is_dir($directory . '/' . $file)) {
63
-				$this->rrmdir($directory . '/' . $file);
64
-			} else {
65
-				unlink($directory . '/' . $file);
66
-			}
67
-		}
68
-		return rmdir($directory);
69
-	}
70
-
71
-	private function randomString() {
72
-		return sha1(uniqid(mt_rand(), true));
73
-	}
74
-
75
-	public function testFindWithAppPathSymlink(): void {
76
-		$appName = 'test-js-app';
77
-
78
-		// First create new apps path, and a symlink to it
79
-		$apps_dirname = $this->randomString();
80
-		$new_apps_path = sys_get_temp_dir() . '/' . $apps_dirname;
81
-		$new_apps_path_symlink = $new_apps_path . '_link';
82
-		$this->assertTrue((
83
-			mkdir($new_apps_path) && symlink($apps_dirname, $new_apps_path_symlink)
84
-		), 'Setup of apps path failed');
85
-
86
-		// Create an app within that path
87
-		$this->assertTrue((
88
-			mkdir($new_apps_path . '/' . $appName) && touch($new_apps_path . '/' . $appName . '/' . 'test-file.js')
89
-		), 'Setup of app within the new apps path failed');
90
-
91
-		// Use the symlink as the app path
92
-		$this->appManager->expects($this->once())
93
-			->method('getAppPath')
94
-			->with($appName)
95
-			->willReturn("$new_apps_path_symlink/$appName");
96
-		$this->appManager->expects($this->once())
97
-			->method('getAppWebPath')
98
-			->with($appName)
99
-			->willReturn("/js-apps-test/$appName");
100
-
101
-		// Run the tests
102
-		$locator = $this->jsResourceLocator();
103
-		$locator->find(["$appName/test-file"]);
104
-
105
-		$resources = $locator->getResources();
106
-		$this->assertCount(1, $resources);
107
-		$resource = $resources[0];
108
-		$this->assertCount(3, $resource);
109
-		$root = $resource[0];
110
-		$webRoot = $resource[1];
111
-		$file = $resource[2];
112
-
113
-		$expectedRoot = $new_apps_path;
114
-		$expectedWebRoot = \OC::$WEBROOT . '/js-apps-test';
115
-		$expectedFile = $appName . '/test-file.js';
116
-
117
-		$this->assertEquals($expectedRoot, $root,
118
-			'Ensure the app path symlink is resolved into the real path');
119
-		$this->assertEquals($expectedWebRoot, $webRoot);
120
-		$this->assertEquals($expectedFile, $file);
121
-
122
-		unlink($new_apps_path_symlink);
123
-		$this->rrmdir($new_apps_path);
124
-	}
125
-
126
-	public function testNotExistingTranslationHandledSilent(): void {
127
-		$this->appManager->expects($this->once())
128
-			->method('getAppPath')
129
-			->with('core')
130
-			->willThrowException(new AppPathNotFoundException());
131
-		$this->appManager->expects($this->atMost(1))
132
-			->method('getAppWebPath')
133
-			->with('core')
134
-			->willThrowException(new AppPathNotFoundException());
135
-		// Assert logger is not called
136
-		$this->logger->expects($this->never())
137
-			->method('error');
138
-
139
-		// Run the tests
140
-		$locator = $this->jsResourceLocator();
141
-		$locator->find(['core/l10n/en.js']);
142
-
143
-		$resources = $locator->getResources();
144
-		$this->assertCount(0, $resources);
145
-	}
146
-
147
-	public function testFindModuleJSWithFallback(): void {
148
-		// First create new apps path, and a symlink to it
149
-		$apps_dirname = $this->randomString();
150
-		$new_apps_path = sys_get_temp_dir() . '/' . $apps_dirname;
151
-		mkdir($new_apps_path);
152
-
153
-		// Create an app within that path
154
-		mkdir("$new_apps_path/test-js-app");
155
-		touch("$new_apps_path/test-js-app/module.mjs");
156
-		touch("$new_apps_path/test-js-app/both.mjs");
157
-		touch("$new_apps_path/test-js-app/both.js");
158
-		touch("$new_apps_path/test-js-app/plain.js");
159
-
160
-		// Use the app path
161
-		$this->appManager->expects($this->any())
162
-			->method('getAppPath')
163
-			->with('test-js-app')
164
-			->willReturn("$new_apps_path/test-js-app");
165
-
166
-		$locator = $this->jsResourceLocator();
167
-		$locator->find(['test-js-app/module', 'test-js-app/both', 'test-js-app/plain']);
168
-
169
-		$resources = $locator->getResources();
170
-		$this->assertCount(3, $resources);
171
-
172
-		$expectedWebRoot = \OC::$WEBROOT . '/js-apps-test/test-js-app';
173
-		$expectedFiles = ['module.mjs', 'both.mjs', 'plain.js'];
174
-
175
-		for ($idx = 0; $idx++; $idx < 3) {
176
-			$this->assertEquals($expectedWebRoot, $resources[$idx][1]);
177
-			$this->assertEquals($expectedFiles[$idx], $resources[$idx][2]);
178
-		}
179
-
180
-		$this->rrmdir($new_apps_path);
181
-	}
20
+    /** @var IAppData|\PHPUnit\Framework\MockObject\MockObject */
21
+    protected $appData;
22
+    /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
23
+    protected $urlGenerator;
24
+    /** @var SystemConfig|\PHPUnit\Framework\MockObject\MockObject */
25
+    protected $config;
26
+    /** @var ICacheFactory|\PHPUnit\Framework\MockObject\MockObject */
27
+    protected $cacheFactory;
28
+    /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
29
+    protected $logger;
30
+    /** @var IAppManager|\PHPUnit\Framework\MockObject\MockObject */
31
+    protected $appManager;
32
+
33
+    protected function setUp(): void {
34
+        parent::setUp();
35
+
36
+        $this->appData = $this->createMock(IAppData::class);
37
+        $this->urlGenerator = $this->createMock(IURLGenerator::class);
38
+        $this->config = $this->createMock(SystemConfig::class);
39
+        $this->cacheFactory = $this->createMock(ICacheFactory::class);
40
+        $this->logger = $this->createMock(LoggerInterface::class);
41
+        $this->appManager = $this->createMock(IAppManager::class);
42
+    }
43
+
44
+    private function jsResourceLocator() {
45
+        $jsCombiner = new JSCombiner(
46
+            $this->appData,
47
+            $this->urlGenerator,
48
+            $this->cacheFactory,
49
+            $this->config,
50
+            $this->logger
51
+        );
52
+        return new JSResourceLocator(
53
+            $this->logger,
54
+            $jsCombiner,
55
+            $this->appManager,
56
+        );
57
+    }
58
+
59
+    private function rrmdir($directory) {
60
+        $files = array_diff(scandir($directory), ['.','..']);
61
+        foreach ($files as $file) {
62
+            if (is_dir($directory . '/' . $file)) {
63
+                $this->rrmdir($directory . '/' . $file);
64
+            } else {
65
+                unlink($directory . '/' . $file);
66
+            }
67
+        }
68
+        return rmdir($directory);
69
+    }
70
+
71
+    private function randomString() {
72
+        return sha1(uniqid(mt_rand(), true));
73
+    }
74
+
75
+    public function testFindWithAppPathSymlink(): void {
76
+        $appName = 'test-js-app';
77
+
78
+        // First create new apps path, and a symlink to it
79
+        $apps_dirname = $this->randomString();
80
+        $new_apps_path = sys_get_temp_dir() . '/' . $apps_dirname;
81
+        $new_apps_path_symlink = $new_apps_path . '_link';
82
+        $this->assertTrue((
83
+            mkdir($new_apps_path) && symlink($apps_dirname, $new_apps_path_symlink)
84
+        ), 'Setup of apps path failed');
85
+
86
+        // Create an app within that path
87
+        $this->assertTrue((
88
+            mkdir($new_apps_path . '/' . $appName) && touch($new_apps_path . '/' . $appName . '/' . 'test-file.js')
89
+        ), 'Setup of app within the new apps path failed');
90
+
91
+        // Use the symlink as the app path
92
+        $this->appManager->expects($this->once())
93
+            ->method('getAppPath')
94
+            ->with($appName)
95
+            ->willReturn("$new_apps_path_symlink/$appName");
96
+        $this->appManager->expects($this->once())
97
+            ->method('getAppWebPath')
98
+            ->with($appName)
99
+            ->willReturn("/js-apps-test/$appName");
100
+
101
+        // Run the tests
102
+        $locator = $this->jsResourceLocator();
103
+        $locator->find(["$appName/test-file"]);
104
+
105
+        $resources = $locator->getResources();
106
+        $this->assertCount(1, $resources);
107
+        $resource = $resources[0];
108
+        $this->assertCount(3, $resource);
109
+        $root = $resource[0];
110
+        $webRoot = $resource[1];
111
+        $file = $resource[2];
112
+
113
+        $expectedRoot = $new_apps_path;
114
+        $expectedWebRoot = \OC::$WEBROOT . '/js-apps-test';
115
+        $expectedFile = $appName . '/test-file.js';
116
+
117
+        $this->assertEquals($expectedRoot, $root,
118
+            'Ensure the app path symlink is resolved into the real path');
119
+        $this->assertEquals($expectedWebRoot, $webRoot);
120
+        $this->assertEquals($expectedFile, $file);
121
+
122
+        unlink($new_apps_path_symlink);
123
+        $this->rrmdir($new_apps_path);
124
+    }
125
+
126
+    public function testNotExistingTranslationHandledSilent(): void {
127
+        $this->appManager->expects($this->once())
128
+            ->method('getAppPath')
129
+            ->with('core')
130
+            ->willThrowException(new AppPathNotFoundException());
131
+        $this->appManager->expects($this->atMost(1))
132
+            ->method('getAppWebPath')
133
+            ->with('core')
134
+            ->willThrowException(new AppPathNotFoundException());
135
+        // Assert logger is not called
136
+        $this->logger->expects($this->never())
137
+            ->method('error');
138
+
139
+        // Run the tests
140
+        $locator = $this->jsResourceLocator();
141
+        $locator->find(['core/l10n/en.js']);
142
+
143
+        $resources = $locator->getResources();
144
+        $this->assertCount(0, $resources);
145
+    }
146
+
147
+    public function testFindModuleJSWithFallback(): void {
148
+        // First create new apps path, and a symlink to it
149
+        $apps_dirname = $this->randomString();
150
+        $new_apps_path = sys_get_temp_dir() . '/' . $apps_dirname;
151
+        mkdir($new_apps_path);
152
+
153
+        // Create an app within that path
154
+        mkdir("$new_apps_path/test-js-app");
155
+        touch("$new_apps_path/test-js-app/module.mjs");
156
+        touch("$new_apps_path/test-js-app/both.mjs");
157
+        touch("$new_apps_path/test-js-app/both.js");
158
+        touch("$new_apps_path/test-js-app/plain.js");
159
+
160
+        // Use the app path
161
+        $this->appManager->expects($this->any())
162
+            ->method('getAppPath')
163
+            ->with('test-js-app')
164
+            ->willReturn("$new_apps_path/test-js-app");
165
+
166
+        $locator = $this->jsResourceLocator();
167
+        $locator->find(['test-js-app/module', 'test-js-app/both', 'test-js-app/plain']);
168
+
169
+        $resources = $locator->getResources();
170
+        $this->assertCount(3, $resources);
171
+
172
+        $expectedWebRoot = \OC::$WEBROOT . '/js-apps-test/test-js-app';
173
+        $expectedFiles = ['module.mjs', 'both.mjs', 'plain.js'];
174
+
175
+        for ($idx = 0; $idx++; $idx < 3) {
176
+            $this->assertEquals($expectedWebRoot, $resources[$idx][1]);
177
+            $this->assertEquals($expectedFiles[$idx], $resources[$idx][2]);
178
+        }
179
+
180
+        $this->rrmdir($new_apps_path);
181
+    }
182 182
 }
Please login to merge, or discard this patch.
tests/lib/Talk/ConversationOptionsTest.php 1 patch
Indentation   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -13,9 +13,9 @@
 block discarded – undo
13 13
 use Test\TestCase;
14 14
 
15 15
 class ConversationOptionsTest extends TestCase {
16
-	public function testDefaults(): void {
17
-		ConversationOptions::default();
16
+    public function testDefaults(): void {
17
+        ConversationOptions::default();
18 18
 
19
-		$this->addToAssertionCount(1);
20
-	}
19
+        $this->addToAssertionCount(1);
20
+    }
21 21
 }
Please login to merge, or discard this patch.