11<?php
22
3+ declare (strict_types=1 );
4+
35/**
46 * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
57 * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
3032use Test \TestCase ;
3133
3234class EncryptAllTest extends TestCase {
35+
3336 protected KeyManager &MockObject $ keyManager ;
3437 protected Util &MockObject $ util ;
3538 protected IUserManager &MockObject $ userManager ;
@@ -46,8 +49,7 @@ class EncryptAllTest extends TestCase {
4649 protected ISecureRandom &MockObject $ secureRandom ;
4750 protected LoggerInterface &MockObject $ logger ;
4851
49- /** @var EncryptAll */
50- protected $ encryptAll ;
52+ protected EncryptAll $ encryptAll ;
5153
5254 protected function setUp (): void {
5355 parent ::setUp ();
@@ -80,7 +82,7 @@ protected function setUp(): void {
8082
8183 /**
8284 * We need format method to return a string
83- * @var OutputFormatterInterface|\PHPUnit\Framework\MockObject\ MockObject
85+ * @var OutputFormatterInterface& MockObject
8486 */
8587 $ outputFormatter = $ this ->createMock (OutputFormatterInterface::class);
8688 $ outputFormatter ->method ('isDecorated ' )->willReturn (false );
@@ -112,6 +114,13 @@ protected function setUp(): void {
112114 );
113115 }
114116
117+ protected function createFileInfoMock ($ type , string $ name ): FileInfo &MockObject {
118+ $ fileInfo = $ this ->createMock (FileInfo::class);
119+ $ fileInfo ->method ('getType ' )->willReturn ($ type );
120+ $ fileInfo ->method ('getName ' )->willReturn ($ name );
121+ return $ fileInfo ;
122+ }
123+
115124 public function testEncryptAll (): void {
116125 /** @var EncryptAll&MockObject $encryptAll */
117126 $ encryptAll = $ this ->getMockBuilder (EncryptAll::class)
@@ -131,7 +140,7 @@ public function testEncryptAll(): void {
131140 $ this ->logger ,
132141 ]
133142 )
134- ->setMethods (['createKeyPairs ' , 'encryptAllUsersFiles ' , 'outputPasswords ' ])
143+ ->onlyMethods (['createKeyPairs ' , 'encryptAllUsersFiles ' , 'outputPasswords ' ])
135144 ->getMock ();
136145
137146 $ this ->util ->expects ($ this ->any ())->method ('isMasterKeyEnabled ' )->willReturn (false );
@@ -161,7 +170,7 @@ public function testEncryptAllWithMasterKey(): void {
161170 $ this ->logger ,
162171 ]
163172 )
164- ->setMethods (['createKeyPairs ' , 'encryptAllUsersFiles ' , 'outputPasswords ' ])
173+ ->onlyMethods (['createKeyPairs ' , 'encryptAllUsersFiles ' , 'outputPasswords ' ])
165174 ->getMock ();
166175
167176 $ this ->util ->expects ($ this ->any ())->method ('isMasterKeyEnabled ' )->willReturn (true );
@@ -192,7 +201,7 @@ public function testCreateKeyPairs(): void {
192201 $ this ->logger ,
193202 ]
194203 )
195- ->setMethods (['setupUserFS ' , 'generateOneTimePassword ' ])
204+ ->onlyMethods (['setupUserFS ' , 'generateOneTimePassword ' ])
196205 ->getMock ();
197206
198207
@@ -225,7 +234,7 @@ function ($user) {
225234 }
226235
227236 public function testEncryptAllUsersFiles (): void {
228- /** @var EncryptAll | \PHPUnit\Framework\ MockObject\MockObject $encryptAll */
237+ /** @var EncryptAll& MockObject $encryptAll */
229238 $ encryptAll = $ this ->getMockBuilder (EncryptAll::class)
230239 ->setConstructorArgs (
231240 [
@@ -243,7 +252,7 @@ public function testEncryptAllUsersFiles(): void {
243252 $ this ->logger ,
244253 ]
245254 )
246- ->setMethods (['encryptUsersFiles ' ])
255+ ->onlyMethods (['encryptUsersFiles ' ])
247256 ->getMock ();
248257
249258 $ this ->util ->expects ($ this ->any ())->method ('isMasterKeyEnabled ' )->willReturn (false );
@@ -252,17 +261,22 @@ public function testEncryptAllUsersFiles(): void {
252261 $ this ->invokePrivate ($ encryptAll , 'output ' , [$ this ->outputInterface ]);
253262 $ this ->invokePrivate ($ encryptAll , 'userPasswords ' , [['user1 ' => 'pwd1 ' , 'user2 ' => 'pwd2 ' ]]);
254263
255- $ encryptAll ->expects ($ this ->exactly (2 ))->method ('encryptUsersFiles ' )
256- ->withConsecutive (
257- ['user1 ' ],
258- ['user2 ' ],
259- );
264+ $ encryptAllCalls = [];
265+ $ encryptAll ->expects ($ this ->exactly (2 ))
266+ ->method ('encryptUsersFiles ' )
267+ ->willReturnCallback (function ($ uid ) use (&$ encryptAllCalls ): void {
268+ $ encryptAllCalls [] = $ uid ;
269+ });
260270
261271 $ this ->invokePrivate ($ encryptAll , 'encryptAllUsersFiles ' );
272+ self ::assertEquals ([
273+ 'user1 ' ,
274+ 'user2 ' ,
275+ ], $ encryptAllCalls );
262276 }
263277
264278 public function testEncryptUsersFiles (): void {
265- /** @var EncryptAll | \PHPUnit\Framework\ MockObject\MockObject $encryptAll */
279+ /** @var EncryptAll& MockObject $encryptAll */
266280 $ encryptAll = $ this ->getMockBuilder (EncryptAll::class)
267281 ->setConstructorArgs (
268282 [
@@ -280,40 +294,39 @@ public function testEncryptUsersFiles(): void {
280294 $ this ->logger ,
281295 ]
282296 )
283- ->setMethods (['encryptFile ' , 'setupUserFS ' ])
297+ ->onlyMethods (['encryptFile ' , 'setupUserFS ' ])
284298 ->getMock ();
285299
286300 $ this ->util ->expects ($ this ->any ())->method ('isMasterKeyEnabled ' )->willReturn (false );
287301
288302 $ this ->view ->expects ($ this ->exactly (2 ))->method ('getDirectoryContent ' )
289- ->withConsecutive (
290- ['/user1/files ' ],
291- ['/user1/files/foo ' ],
292- )->willReturnOnConsecutiveCalls (
303+ ->willReturnMap ([
293304 [
294- ['name ' => 'foo ' , 'type ' => 'dir ' ],
295- ['name ' => 'bar ' , 'type ' => 'file ' ],
305+ '/user1/files ' ,
306+ '' ,
307+ null ,
308+ [
309+ $ this ->createFileInfoMock (FileInfo::TYPE_FOLDER , 'foo ' ),
310+ $ this ->createFileInfoMock (FileInfo::TYPE_FILE , 'bar ' ),
311+ ],
296312 ],
297313 [
298- ['name ' => 'subfile ' , 'type ' => 'file ' ]
299- ]
300- );
301-
302- $ this ->view ->expects ($ this ->any ())->method ('is_dir ' )
303- ->willReturnCallback (
304- function ($ path ) {
305- if ($ path === '/user1/files/foo ' ) {
306- return true ;
307- }
308- return false ;
309- }
310- );
314+ '/user1/files/foo ' ,
315+ '' ,
316+ null ,
317+ [
318+ $ this ->createFileInfoMock (FileInfo::TYPE_FILE , 'subfile ' ),
319+ ],
320+ ],
321+ ]);
311322
312- $ encryptAll ->expects ($ this ->exactly (2 ))->method ('encryptFile ' )
313- ->withConsecutive (
314- ['/user1/files/bar ' ],
315- ['/user1/files/foo/subfile ' ],
316- );
323+ $ encryptAllCalls = [];
324+ $ encryptAll ->expects ($ this ->exactly (2 ))
325+ ->method ('encryptFile ' )
326+ ->willReturnCallback (function (FileInfo $ file , string $ path ) use (&$ encryptAllCalls ): bool {
327+ $ encryptAllCalls [] = $ path ;
328+ return true ;
329+ });
317330
318331 $ outputFormatter = $ this ->createMock (OutputFormatterInterface::class);
319332 $ outputFormatter ->method ('isDecorated ' )->willReturn (false );
@@ -323,6 +336,10 @@ function ($path) {
323336 $ progressBar = new ProgressBar ($ this ->outputInterface );
324337
325338 $ this ->invokePrivate ($ encryptAll , 'encryptUsersFiles ' , ['user1 ' , $ progressBar , '' ]);
339+ self ::assertEquals ([
340+ '/user1/files/bar ' ,
341+ '/user1/files/foo/subfile ' ,
342+ ], $ encryptAllCalls );
326343 }
327344
328345 public function testGenerateOneTimePassword (): void {
@@ -343,8 +360,7 @@ public function testEncryptFile($isEncrypted): void {
343360 $ fileInfo = $ this ->createMock (FileInfo::class);
344361 $ fileInfo ->expects ($ this ->any ())->method ('isEncrypted ' )
345362 ->willReturn ($ isEncrypted );
346- $ this ->view ->expects ($ this ->any ())->method ('getFileInfo ' )
347- ->willReturn ($ fileInfo );
363+ $ this ->view ->expects ($ this ->never ())->method ('getFileInfo ' );
348364
349365
350366 if ($ isEncrypted ) {
@@ -356,11 +372,11 @@ public function testEncryptFile($isEncrypted): void {
356372 }
357373
358374 $ this ->assertTrue (
359- $ this ->invokePrivate ($ this ->encryptAll , 'encryptFile ' , ['foo.txt ' ])
375+ $ this ->invokePrivate ($ this ->encryptAll , 'encryptFile ' , [$ fileInfo , 'foo.txt ' ])
360376 );
361377 }
362378
363- public function dataTestEncryptFile () {
379+ public static function dataTestEncryptFile (): array {
364380 return [
365381 [true ],
366382 [false ],
0 commit comments