diff options
Diffstat (limited to 'tests/lib/DirectEditing')
-rw-r--r-- | tests/lib/DirectEditing/ManagerTest.php | 123 |
1 files changed, 114 insertions, 9 deletions
diff --git a/tests/lib/DirectEditing/ManagerTest.php b/tests/lib/DirectEditing/ManagerTest.php index b00de02bcf5..2ad7f98df49 100644 --- a/tests/lib/DirectEditing/ManagerTest.php +++ b/tests/lib/DirectEditing/ManagerTest.php @@ -1,5 +1,9 @@ <?php +/** + * SPDX-FileCopyrightText: 2019-2024 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ namespace Test\DirectEditing; use OC\DirectEditing\Manager; @@ -15,9 +19,11 @@ use OCP\Files\Folder; use OCP\Files\IRootFolder; use OCP\IDBConnection; use OCP\IL10N; +use OCP\IUser; use OCP\IUserSession; use OCP\L10N\IFactory; use OCP\Security\ISecureRandom; +use OCP\Server; use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; @@ -106,6 +112,10 @@ class ManagerTest extends TestCase { */ private $userFolder; /** + * @var MockObject|IL10N + */ + private $l10n; + /** * @var MockObject|IManager */ private $encryptionManager; @@ -116,7 +126,7 @@ class ManagerTest extends TestCase { $this->editor = new Editor(); $this->random = $this->createMock(ISecureRandom::class); - $this->connection = \OC::$server->getDatabaseConnection(); + $this->connection = Server::get(IDBConnection::class); $this->userSession = $this->createMock(IUserSession::class); $this->rootFolder = $this->createMock(IRootFolder::class); $this->userFolder = $this->createMock(Folder::class); @@ -133,6 +143,14 @@ class ManagerTest extends TestCase { ->method('getUserFolder') ->willReturn($this->userFolder); + $user = $this->createMock(IUser::class); + $user->expects(self::any()) + ->method('getUID') + ->willReturn('admin'); + $this->userSession->expects(self::any()) + ->method('getUser') + ->willReturn($user); + $this->manager = new Manager( $this->random, $this->connection, $this->userSession, $this->rootFolder, $l10nFactory, $this->encryptionManager ); @@ -140,12 +158,12 @@ class ManagerTest extends TestCase { $this->manager->registerDirectEditor($this->editor); } - public function testEditorRegistration() { + public function testEditorRegistration(): void { $this->assertEquals($this->manager->getEditors(), ['testeditor' => $this->editor]); } - public function testCreateToken() { + public function testCreateToken(): void { $expectedToken = 'TOKEN' . time(); $file = $this->createMock(File::class); $file->expects($this->any()) @@ -157,8 +175,10 @@ class ManagerTest extends TestCase { $folder = $this->createMock(Folder::class); $this->userFolder ->method('nodeExists') - ->withConsecutive(['/File.txt'], ['/']) - ->willReturnOnConsecutiveCalls(false, true); + ->willReturnMap([ + ['/File.txt', false], + ['/', true], + ]); $this->userFolder ->method('get') ->with('/') @@ -170,7 +190,7 @@ class ManagerTest extends TestCase { $this->assertEquals($token, $expectedToken); } - public function testCreateTokenAccess() { + public function testCreateTokenAccess(): void { $expectedToken = 'TOKEN' . time(); $file = $this->createMock(File::class); $file->expects($this->any()) @@ -182,8 +202,10 @@ class ManagerTest extends TestCase { $folder = $this->createMock(Folder::class); $this->userFolder ->method('nodeExists') - ->withConsecutive(['/File.txt'], ['/']) - ->willReturnOnConsecutiveCalls(false, true); + ->willReturnMap([ + ['/File.txt', false], + ['/', true], + ]); $this->userFolder ->method('get') ->with('/') @@ -198,7 +220,90 @@ class ManagerTest extends TestCase { $this->assertInstanceOf(NotFoundResponse::class, $secondResult); } - public function testCreateFileAlreadyExists() { + public function testOpenByPath(): void { + $expectedToken = 'TOKEN' . time(); + $file = $this->createMock(File::class); + $file->expects($this->any()) + ->method('getId') + ->willReturn(123); + $file->expects($this->any()) + ->method('getPath') + ->willReturn('/admin/files/File.txt'); + $this->random->expects($this->once()) + ->method('generate') + ->willReturn($expectedToken); + $this->userFolder + ->method('nodeExists') + ->willReturnMap([ + ['/File.txt', false], + ['/', true], + ]); + $this->userFolder + ->method('get') + ->with('/File.txt') + ->willReturn($file); + $this->userFolder + ->method('getRelativePath') + ->willReturn('/File.txt'); + $this->manager->open('/File.txt', 'testeditor'); + $firstResult = $this->manager->edit($expectedToken); + $secondResult = $this->manager->edit($expectedToken); + $this->assertInstanceOf(DataResponse::class, $firstResult); + $this->assertInstanceOf(NotFoundResponse::class, $secondResult); + } + + public function testOpenById(): void { + $expectedToken = 'TOKEN' . time(); + $fileRead = $this->createMock(File::class); + $fileRead->method('getPermissions') + ->willReturn(1); + $fileRead->expects($this->any()) + ->method('getId') + ->willReturn(123); + $fileRead->expects($this->any()) + ->method('getPath') + ->willReturn('/admin/files/shared_file.txt'); + $file = $this->createMock(File::class); + $file->method('getPermissions') + ->willReturn(1); + $file->expects($this->any()) + ->method('getId') + ->willReturn(123); + $file->expects($this->any()) + ->method('getPath') + ->willReturn('/admin/files/File.txt'); + $this->random->expects($this->once()) + ->method('generate') + ->willReturn($expectedToken); + $folder = $this->createMock(Folder::class); + $folder->expects($this->any()) + ->method('getById') + ->willReturn([ + $fileRead, + $file + ]); + $this->userFolder + ->method('nodeExists') + ->willReturnMap([ + ['/File.txt', false], + ['/', true], + ]); + $this->userFolder + ->method('get') + ->with('/') + ->willReturn($folder); + $this->userFolder + ->method('getRelativePath') + ->willReturn('/File.txt'); + + $this->manager->open('/', 'testeditor', 123); + $firstResult = $this->manager->edit($expectedToken); + $secondResult = $this->manager->edit($expectedToken); + $this->assertInstanceOf(DataResponse::class, $firstResult); + $this->assertInstanceOf(NotFoundResponse::class, $secondResult); + } + + public function testCreateFileAlreadyExists(): void { $this->expectException(\RuntimeException::class); $this->userFolder ->method('nodeExists') |