diff options
author | Carl Schwan <carl@carlschwan.eu> | 2022-07-15 17:11:54 +0200 |
---|---|---|
committer | Carl Schwan <carl@carlschwan.eu> | 2022-07-31 19:37:59 +0200 |
commit | 7b723813cef60e744ab14ab418c82e5ec67a9f2e (patch) | |
tree | eda4a044c9defc883c35d98e1ba5a9e261a3ecdf /apps/files_sharing/tests/ApplicationTest.php | |
parent | ab1a20522b1b2730398869a764f672175a1abcb8 (diff) | |
download | nextcloud-server-7b723813cef60e744ab14ab418c82e5ec67a9f2e.tar.gz nextcloud-server-7b723813cef60e744ab14ab418c82e5ec67a9f2e.zip |
Multiple fixes
- Fix tests
- Use non deprecated event stuff
- Add a bit of type hinting to the new stuff
- More safe handling of instanceOfStorage (share might not be the first
wrapper)
- Fix resharing
Signed-off-by: Carl Schwan <carl@carlschwan.eu>
Diffstat (limited to 'apps/files_sharing/tests/ApplicationTest.php')
-rw-r--r-- | apps/files_sharing/tests/ApplicationTest.php | 47 |
1 files changed, 23 insertions, 24 deletions
diff --git a/apps/files_sharing/tests/ApplicationTest.php b/apps/files_sharing/tests/ApplicationTest.php index ee04996ac15..11c8137c398 100644 --- a/apps/files_sharing/tests/ApplicationTest.php +++ b/apps/files_sharing/tests/ApplicationTest.php @@ -22,6 +22,8 @@ */ namespace OCA\Files_Sharing\Tests; +use OCP\Files\Events\BeforeDirectFileDownloadEvent; +use OCP\Files\Events\BeforeZipCreatedEvent; use Psr\Log\LoggerInterface; use OC\Share20\LegacyHooks; use OC\Share20\Manager; @@ -32,6 +34,7 @@ use OCP\Constants; use OCP\EventDispatcher\GenericEvent; use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\Cache\ICacheEntry; +use OCP\Files\Event\BeforeDirectGetEvent; use OCP\Files\File; use OCP\Files\Folder; use OCP\Files\IRootFolder; @@ -45,12 +48,8 @@ use Symfony\Component\EventDispatcher\EventDispatcher as SymfonyDispatcher; use Test\TestCase; class ApplicationTest extends TestCase { - - /** @var Application */ - private $application; - - /** @var IEventDispatcher */ - private $eventDispatcher; + private Application $application; + private IEventDispatcher $eventDispatcher; /** @var IUserSession */ private $userSession; @@ -81,7 +80,7 @@ class ApplicationTest extends TestCase { ); } - public function providesDataForCanGet() { + public function providesDataForCanGet(): array { // normal file (sender) - can download directly $senderFileStorage = $this->createMock(IStorage::class); $senderFileStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(false); @@ -130,7 +129,7 @@ class ApplicationTest extends TestCase { /** * @dataProvider providesDataForCanGet */ - public function testCheckDirectCanBeDownloaded($path, $userFolder, $run) { + public function testCheckDirectCanBeDownloaded(string $path, Folder $userFolder, bool $run): void { $user = $this->createMock(IUser::class); $user->method('getUID')->willReturn('test'); $this->userSession->method('getUser')->willReturn($user); @@ -138,13 +137,13 @@ class ApplicationTest extends TestCase { $this->rootFolder->method('getUserFolder')->willReturn($userFolder); // Simulate direct download of file - $event = new GenericEvent(null, [ 'path' => $path ]); - $this->eventDispatcher->dispatch('file.beforeGetDirect', $event); + $event = new BeforeDirectFileDownloadEvent($path); + $this->eventDispatcher->dispatchTyped($event); - $this->assertEquals($run, !$event->hasArgument('errorMessage')); + $this->assertEquals($run, $event->isSuccessful()); } - public function providesDataForCanZip() { + public function providesDataForCanZip(): array { // Mock: Normal file/folder storage $nonSharedStorage = $this->createMock(IStorage::class); $nonSharedStorage->method('instanceOfStorage')->with(SharedStorage::class)->willReturn(false); @@ -172,7 +171,7 @@ class ApplicationTest extends TestCase { $sender1UserFolder->method('get')->willReturn($sender1RootFolder); $return[] = [ '/folder', ['bar1.txt', 'bar2.txt'], $sender1UserFolder, true ]; - $return[] = [ '/', 'folder', $sender1UserFolder, true ]; + $return[] = [ '/', ['folder'], $sender1UserFolder, true ]; // 3. cannot download zipped 1 non-shared file and 1 secure-shared inside non-shared folder $receiver1File = $this->createMock(File::class); @@ -199,7 +198,7 @@ class ApplicationTest extends TestCase { $receiver2UserFolder = $this->createMock(Folder::class); $receiver2UserFolder->method('get')->willReturn($receiver2RootFolder); - $return[] = [ '/', 'secured-folder', $receiver2UserFolder, false ]; + $return[] = [ '/', ['secured-folder'], $receiver2UserFolder, false ]; return $return; } @@ -207,7 +206,7 @@ class ApplicationTest extends TestCase { /** * @dataProvider providesDataForCanZip */ - public function testCheckZipCanBeDownloaded($dir, $files, $userFolder, $run) { + public function testCheckZipCanBeDownloaded(string $dir, array $files, Folder $userFolder, bool $run): void { $user = $this->createMock(IUser::class); $user->method('getUID')->willReturn('test'); $this->userSession->method('getUser')->willReturn($user); @@ -216,22 +215,22 @@ class ApplicationTest extends TestCase { $this->rootFolder->method('getUserFolder')->with('test')->willReturn($userFolder); // Simulate zip download of folder folder - $event = new GenericEvent(null, ['dir' => $dir, 'files' => $files, 'run' => true]); - $this->eventDispatcher->dispatch('file.beforeCreateZip', $event); + $event = new BeforeZipCreatedEvent($dir, $files); + $this->eventDispatcher->dispatchTyped($event); - $this->assertEquals($run, $event->getArgument('run')); - $this->assertEquals($run, !$event->hasArgument('errorMessage')); + $this->assertEquals($run, $event->isSuccessful()); + $this->assertEquals($run, $event->getErrorMessage() === null); } - public function testCheckFileUserNotFound() { + public function testCheckFileUserNotFound(): void { $this->userSession->method('isLoggedIn')->willReturn(false); // Simulate zip download of folder folder - $event = new GenericEvent(null, ['dir' => '/test', 'files' => ['test.txt'], 'run' => true]); - $this->eventDispatcher->dispatch('file.beforeCreateZip', $event); + $event = new BeforeZipCreatedEvent('/test', ['test.txt']); + $this->eventDispatcher->dispatchTyped($event); // It should run as this would restrict e.g. share links otherwise - $this->assertTrue($event->getArgument('run')); - $this->assertFalse($event->hasArgument('errorMessage')); + $this->assertTrue($event->isSuccessful()); + $this->assertEquals(null, $event->getErrorMessage()); } } |