123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303 |
- <?php
- namespace Test\DirectEditing;
- use OC\DirectEditing\Manager;
- use OC\Files\Node\File;
- use OCP\AppFramework\Http\DataResponse;
- use OCP\AppFramework\Http\NotFoundResponse;
- use OCP\AppFramework\Http\Response;
- use OCP\DirectEditing\ACreateEmpty;
- use OCP\DirectEditing\IEditor;
- use OCP\DirectEditing\IToken;
- use OCP\Encryption\IManager;
- 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 PHPUnit\Framework\MockObject\MockObject;
- use Test\TestCase;
- class CreateEmpty extends ACreateEmpty {
- public function getId(): string {
- return 'createEmpty';
- }
- public function getName(): string {
- return 'create empty file';
- }
- public function getExtension(): string {
- return '.txt';
- }
- public function getMimetype(): string {
- return 'text/plain';
- }
- }
- class Editor implements IEditor {
- public function getId(): string {
- return 'testeditor';
- }
- public function getName(): string {
- return 'Test editor';
- }
- public function getMimetypes(): array {
- return [ 'text/plain' ];
- }
- public function getMimetypesOptional(): array {
- return [];
- }
- public function getCreators(): array {
- return [
- new CreateEmpty()
- ];
- }
- public function isSecure(): bool {
- return false;
- }
- public function open(IToken $token): Response {
- return new DataResponse('edit page');
- }
- }
- class ManagerTest extends TestCase {
- private $manager;
-
- private $editor;
-
- private $random;
-
- private $connection;
-
- private $userSession;
-
- private $rootFolder;
-
- private $userFolder;
-
- private $l10n;
-
- private $encryptionManager;
- protected function setUp(): void {
- parent::setUp();
- $this->editor = new Editor();
- $this->random = $this->createMock(ISecureRandom::class);
- $this->connection = \OC::$server->getDatabaseConnection();
- $this->userSession = $this->createMock(IUserSession::class);
- $this->rootFolder = $this->createMock(IRootFolder::class);
- $this->userFolder = $this->createMock(Folder::class);
- $this->l10n = $this->createMock(IL10N::class);
- $this->encryptionManager = $this->createMock(IManager::class);
- $l10nFactory = $this->createMock(IFactory::class);
- $l10nFactory->expects($this->once())
- ->method('get')
- ->willReturn($this->l10n);
- $this->rootFolder->expects($this->any())
- ->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
- );
- $this->manager->registerDirectEditor($this->editor);
- }
- public function testEditorRegistration() {
- $this->assertEquals($this->manager->getEditors(), ['testeditor' => $this->editor]);
- }
- public function testCreateToken() {
- $expectedToken = 'TOKEN' . time();
- $file = $this->createMock(File::class);
- $file->expects($this->any())
- ->method('getId')
- ->willReturn(123);
- $this->random->expects($this->once())
- ->method('generate')
- ->willReturn($expectedToken);
- $folder = $this->createMock(Folder::class);
- $this->userFolder
- ->method('nodeExists')
- ->withConsecutive(['/File.txt'], ['/'])
- ->willReturnOnConsecutiveCalls(false, true);
- $this->userFolder
- ->method('get')
- ->with('/')
- ->willReturn($folder);
- $folder->expects($this->once())
- ->method('newFile')
- ->willReturn($file);
- $token = $this->manager->create('/File.txt', 'testeditor', 'createEmpty');
- $this->assertEquals($token, $expectedToken);
- }
- public function testCreateTokenAccess() {
- $expectedToken = 'TOKEN' . time();
- $file = $this->createMock(File::class);
- $file->expects($this->any())
- ->method('getId')
- ->willReturn(123);
- $this->random->expects($this->once())
- ->method('generate')
- ->willReturn($expectedToken);
- $folder = $this->createMock(Folder::class);
- $this->userFolder
- ->method('nodeExists')
- ->withConsecutive(['/File.txt'], ['/'])
- ->willReturnOnConsecutiveCalls(false, true);
- $this->userFolder
- ->method('get')
- ->with('/')
- ->willReturn($folder);
- $folder->expects($this->once())
- ->method('newFile')
- ->willReturn($file);
- $this->manager->create('/File.txt', 'testeditor', 'createEmpty');
- $firstResult = $this->manager->edit($expectedToken);
- $secondResult = $this->manager->edit($expectedToken);
- $this->assertInstanceOf(DataResponse::class, $firstResult);
- $this->assertInstanceOf(NotFoundResponse::class, $secondResult);
- }
- public function testOpenByPath() {
- $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);
- $folder = $this->createMock(Folder::class);
- $this->userFolder
- ->method('nodeExists')
- ->withConsecutive(['/File.txt'], ['/'])
- ->willReturnOnConsecutiveCalls(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() {
- $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')
- ->withConsecutive(['/File.txt'], ['/'])
- ->willReturnOnConsecutiveCalls(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() {
- $this->expectException(\RuntimeException::class);
- $this->userFolder
- ->method('nodeExists')
- ->with('/File.txt')
- ->willReturn(true);
- $this->manager->create('/File.txt', 'testeditor', 'createEmpty');
- }
- }
|