2019-11-14 18:19:58 +03:00
|
|
|
<?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;
|
2020-08-25 18:12:27 +03:00
|
|
|
use OCP\Encryption\IManager;
|
2019-11-14 18:19:58 +03:00
|
|
|
use OCP\Files\Folder;
|
|
|
|
use OCP\Files\IRootFolder;
|
|
|
|
use OCP\IDBConnection;
|
2019-12-16 16:58:56 +03:00
|
|
|
use OCP\IL10N;
|
2019-11-14 18:19:58 +03:00
|
|
|
use OCP\IUserSession;
|
2019-12-16 16:58:56 +03:00
|
|
|
use OCP\L10N\IFactory;
|
2019-11-14 18:19:58 +03:00
|
|
|
use OCP\Security\ISecureRandom;
|
2019-11-25 16:09:38 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2019-11-14 18:19:58 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class CreateEmpty extends ACreateEmpty {
|
2020-04-10 15:19:56 +03:00
|
|
|
public function getId(): string {
|
2020-04-09 10:22:29 +03:00
|
|
|
return 'createEmpty';
|
2019-11-14 18:19:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getName(): string {
|
|
|
|
return 'create empty file';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getExtension(): string {
|
|
|
|
return '.txt';
|
|
|
|
}
|
2019-11-26 15:07:15 +03:00
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
public function getMimetype(): string {
|
2019-11-26 15:07:15 +03:00
|
|
|
return 'text/plain';
|
|
|
|
}
|
2019-11-14 18:19:58 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class Editor implements IEditor {
|
2020-04-10 15:19:56 +03:00
|
|
|
public function getId(): string {
|
2020-04-09 10:22:29 +03:00
|
|
|
return 'testeditor';
|
2020-04-10 15:19:56 +03:00
|
|
|
}
|
2019-11-14 18:19:58 +03:00
|
|
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @package Test\DirectEditing
|
|
|
|
* @group DB
|
|
|
|
*/
|
|
|
|
class ManagerTest extends TestCase {
|
|
|
|
private $manager;
|
|
|
|
/**
|
|
|
|
* @var Editor
|
|
|
|
*/
|
|
|
|
private $editor;
|
|
|
|
/**
|
2019-11-25 16:09:38 +03:00
|
|
|
* @var MockObject|ISecureRandom
|
2019-11-14 18:19:58 +03:00
|
|
|
*/
|
|
|
|
private $random;
|
2019-11-25 16:09:38 +03:00
|
|
|
/**
|
|
|
|
* @var IDBConnection
|
|
|
|
*/
|
|
|
|
private $connection;
|
|
|
|
/**
|
|
|
|
* @var MockObject|IUserSession
|
|
|
|
*/
|
|
|
|
private $userSession;
|
|
|
|
/**
|
|
|
|
* @var MockObject|IRootFolder
|
|
|
|
*/
|
|
|
|
private $rootFolder;
|
|
|
|
/**
|
|
|
|
* @var MockObject|Folder
|
|
|
|
*/
|
|
|
|
private $userFolder;
|
2020-08-25 18:12:27 +03:00
|
|
|
/**
|
|
|
|
* @var MockObject|IManager
|
|
|
|
*/
|
|
|
|
private $encryptionManager;
|
2019-11-14 18:19:58 +03:00
|
|
|
|
2019-11-28 12:59:22 +03:00
|
|
|
protected function setUp(): void {
|
2019-11-14 18:19:58 +03:00
|
|
|
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);
|
2019-12-16 16:58:56 +03:00
|
|
|
$this->l10n = $this->createMock(IL10N::class);
|
2020-08-25 18:12:27 +03:00
|
|
|
$this->encryptionManager = $this->createMock(IManager::class);
|
2019-12-16 16:58:56 +03:00
|
|
|
|
|
|
|
$l10nFactory = $this->createMock(IFactory::class);
|
|
|
|
$l10nFactory->expects($this->once())
|
|
|
|
->method('get')
|
|
|
|
->willReturn($this->l10n);
|
2019-11-14 18:19:58 +03:00
|
|
|
|
|
|
|
|
|
|
|
$this->rootFolder->expects($this->any())
|
|
|
|
->method('getUserFolder')
|
|
|
|
->willReturn($this->userFolder);
|
|
|
|
|
|
|
|
$this->manager = new Manager(
|
2020-08-25 18:12:27 +03:00
|
|
|
$this->random, $this->connection, $this->userSession, $this->rootFolder, $l10nFactory, $this->encryptionManager
|
2019-11-14 18:19:58 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
$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);
|
2021-03-31 16:44:47 +03:00
|
|
|
$folder = $this->createMock(Folder::class);
|
2020-01-10 15:06:27 +03:00
|
|
|
$this->userFolder
|
2020-01-14 12:57:54 +03:00
|
|
|
->method('nodeExists')
|
2021-03-31 16:44:47 +03:00
|
|
|
->withConsecutive(['/File.txt'], ['/'])
|
|
|
|
->willReturnOnConsecutiveCalls(false, true);
|
|
|
|
$this->userFolder
|
|
|
|
->method('get')
|
|
|
|
->with('/')
|
|
|
|
->willReturn($folder);
|
|
|
|
$folder->expects($this->once())
|
2019-11-14 18:19:58 +03:00
|
|
|
->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);
|
2021-03-31 16:44:47 +03:00
|
|
|
$folder = $this->createMock(Folder::class);
|
2020-01-10 15:06:27 +03:00
|
|
|
$this->userFolder
|
2020-01-14 12:57:54 +03:00
|
|
|
->method('nodeExists')
|
2021-03-31 16:44:47 +03:00
|
|
|
->withConsecutive(['/File.txt'], ['/'])
|
|
|
|
->willReturnOnConsecutiveCalls(false, true);
|
|
|
|
$this->userFolder
|
|
|
|
->method('get')
|
|
|
|
->with('/')
|
|
|
|
->willReturn($folder);
|
|
|
|
$folder->expects($this->once())
|
2019-11-14 18:19:58 +03:00
|
|
|
->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);
|
|
|
|
}
|
|
|
|
|
2020-01-10 15:06:27 +03:00
|
|
|
public function testCreateFileAlreadyExists() {
|
|
|
|
$this->expectException(\RuntimeException::class);
|
2020-01-14 12:57:54 +03:00
|
|
|
$this->userFolder
|
|
|
|
->method('nodeExists')
|
|
|
|
->with('/File.txt')
|
|
|
|
->willReturn(true);
|
2020-01-10 15:06:27 +03:00
|
|
|
|
|
|
|
$this->manager->create('/File.txt', 'testeditor', 'createEmpty');
|
|
|
|
}
|
2019-11-14 18:19:58 +03:00
|
|
|
}
|