Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mrow4a committed Feb 3, 2023
1 parent e787194 commit c730269
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 8 deletions.
10 changes: 8 additions & 2 deletions controller/filehandlingcontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,10 @@ public function load($dir, $filename) {
$fileContents = $node->getContent();

if ($fileContents !== false) {
$activePersistentLock = $this->getPersistentLock($node);
$permissions = $this->getPermissions($node);
$writable = ($permissions & Constants::PERMISSION_UPDATE) === Constants::PERMISSION_UPDATE;
$writable = (($permissions & Constants::PERMISSION_UPDATE) === Constants::PERMISSION_UPDATE &&
$activePersistentLock === null);
$mime = $node->getMimeType();
$mTime = $node->getMTime();
$encoding = \mb_detect_encoding($fileContents . "a", "UTF-8, GB2312, GBK ,BIG5, WINDOWS-1252, SJIS-win, EUC-JP, ISO-8859-15, ISO-8859-1, ASCII", true);
Expand All @@ -140,6 +142,7 @@ public function load($dir, $filename) {
[
'filecontents' => $fileContents,
'writeable' => $writable,
'locked' => $activePersistentLock ? $activePersistentLock->getOwner() : null,
'mime' => $mime,
'mtime' => $mTime
],
Expand Down Expand Up @@ -304,7 +307,10 @@ private function getPermissions(File $node): int {
private function getPersistentLock(File $node): ?ILock {
$storage = $node->getStorage();
if ($storage->instanceOfStorage(IPersistentLockingStorage::class)) {
/** @var IPersistentLockingStorage $storage */
/**
* @var IPersistentLockingStorage $storage
* @phpstan-ignore-next-line
*/
'@phan-var IPersistentLockingStorage $storage';
$locks = $storage->getLocks($node->getInternalPath(), false);
if (\count($locks) > 0) {
Expand Down
15 changes: 14 additions & 1 deletion js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,16 @@ var Files_Texteditor = {
$('#editor_wrap').before(controlBar);
this.setFilenameMaxLength();
this.bindControlBar();

if (file.locked) {
$('#editor_controls small.saving-message')
.text(t('files_texteditor', 'file is read-only, locked by {locked}', {locked: file.locked}))
.show();
} else if (!file.writeable) {
$('#editor_controls small.saving-message')
.text(t('files_texteditor', 'file is read-only, edit permission is missing'))
.show();
}

},

Expand Down Expand Up @@ -389,7 +399,9 @@ var Files_Texteditor = {
window.aceEditor = ace.edit(this.editor);
aceEditor.setShowPrintMargin(false);
aceEditor.getSession().setUseWrapMode(true);
if (!file.writeable) { aceEditor.setReadOnly(true); }
if (!file.writeable) {
aceEditor.setReadOnly(true);
}
if (file.mime && file.mime === 'text/html') {
this.setEditorSyntaxMode('html');
} else {
Expand Down Expand Up @@ -504,6 +516,7 @@ var Files_Texteditor = {
).done(function(data) {
// Call success callback
OCA.Files_Texteditor.file.writeable = data.writeable;
OCA.Files_Texteditor.file.locked = data.locked;
OCA.Files_Texteditor.file.mime = data.mime;
OCA.Files_Texteditor.file.mtime = data.mtime;
success(OCA.Files_Texteditor.file, data.filecontents);
Expand Down
209 changes: 204 additions & 5 deletions tests/unit/controller/filehandlingcontrollerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@
use OCA\Files_Texteditor\Controller\FileHandlingController;
use OCP\Constants;
use OCP\Files\ForbiddenException;
use OCP\Files\Storage\IStorage;
use OCP\Files\Storage\IPersistentLockingStorage;
use OCP\Lock\Persistent\ILock;
use OCP\Lock\LockedException;
use Test\TestCase;
use PHPUnit\Framework\MockObject\MockObject;

interface IPersistentLockingStorageTest extends IPersistentLockingStorage, IStorage {
}

class FileHandlingControllerTest extends TestCase {

Expand Down Expand Up @@ -57,12 +64,18 @@ class FileHandlingControllerTest extends TestCase {
/** @var \OCP\Files\File|\PHPUnit\Framework\MockObject\MockObject */
private $fileMock;

/** @var IPersistentLockingStorageTest|\PHPUnit\Framework\MockObject\MockObject */
private $fileStorageMock;

/** @var \OCP\IUser|\PHPUnit\Framework\MockObject\MockObject */
private $userMock;

/** @var \OCP\Share\IShare|\PHPUnit\Framework\MockObject\MockObject */
private $shareMock;

/** @var ILock|MockObject */
private $persistentLockMock;

public function setUp(): void {
parent::setUp();
$this->appName = 'files_texteditor';
Expand All @@ -85,21 +98,32 @@ public function setUp(): void {
->disableOriginalConstructor()
->getMock();

$this->fileStorageMock = $this->getMockBuilder(IPersistentLockingStorageTest::class)
->disableOriginalConstructor()
->getMock();
$this->fileStorageMock->expects($this->any())
->method('instanceOfStorage')
->willReturn(true);

$this->fileMock = $this->getMockBuilder('OCP\Files\File')
->disableOriginalConstructor()
->getMock();
$this->fileMock->expects($this->any())
->method('getStorage')
->willReturn($this->fileStorageMock);
$this->userMock = $this->getMockBuilder('OCP\IUser')
->disableOriginalConstructor()
->getMock();
$this->shareMock = $this->getMockBuilder('OCP\Share\IShare')
->disableOriginalConstructor()
->getMock();
$this->persistentLockMock = $this->getMockBuilder(ILock::class)
->disableOriginalConstructor()
->getMock();

$this->l10nMock->expects($this->any())->method('t')->willReturnCallback(
function ($message) {
return $message;
}
);
$this->l10nMock->expects($this->any())->method('t')->willReturnCallback(function ($text, $parameters = []) {
return \vsprintf($text, $parameters);
});

$this->controller = new FileHandlingController(
$this->appName,
Expand Down Expand Up @@ -129,6 +153,10 @@ public function testLoad($filename, $fileContent, $expectedStatus, $expectedMess
->method('getPermissions')
->willReturn(Constants::PERMISSION_ALL);

$this->fileStorageMock->expects($this->any())
->method('getLocks')
->willReturn([]);

$this->rootMock->expects($this->any())
->method('get')
->willReturn($this->fileMock);
Expand All @@ -148,9 +176,12 @@ public function testLoad($filename, $fileContent, $expectedStatus, $expectedMess
if ($status === 200) {
$this->assertArrayHasKey('filecontents', $data);
$this->assertArrayHasKey('writeable', $data);
$this->assertArrayHasKey('locked', $data);
$this->assertArrayHasKey('mime', $data);
$this->assertArrayHasKey('mtime', $data);
$this->assertSame($data['filecontents'], $fileContent);
$this->assertSame($data['writeable'], true);
$this->assertSame($data['locked'], null);
} else {
$this->assertArrayHasKey('message', $data);
$this->assertSame($expectedMessage, $data['message']);
Expand Down Expand Up @@ -191,6 +222,10 @@ public function testLoadExceptionWithException(\Exception $exception, $expectedM
$this->fileMock->expects($this->any())
->method('getPermissions')
->willReturn(Constants::PERMISSION_ALL);

$this->fileStorageMock->expects($this->any())
->method('getLocks')
->willReturn([]);

$this->rootMock->expects($this->any())
->method('get')
Expand Down Expand Up @@ -228,6 +263,10 @@ public function testSaveExceptionWithException(\Exception $exception, $expectedM
->method('getPermissions')
->willReturn(Constants::PERMISSION_ALL);

$this->fileStorageMock->expects($this->any())
->method('getLocks')
->willReturn([]);

$this->rootMock->expects($this->any())
->method('get')
->willReturn($this->fileMock);
Expand Down Expand Up @@ -273,6 +312,10 @@ public function testSave($path, $fileContents, $mTime, $fileMTime, $isUpdatable,
->method('getPermissions')
->willReturn($permissions);

$this->fileStorageMock->expects($this->any())
->method('getLocks')
->willReturn([]);

$this->rootMock->expects($this->any())
->method('get')
->willReturn($this->fileMock);
Expand Down Expand Up @@ -314,6 +357,10 @@ public function testFileTooBig() {
->method('getPermissions')
->willReturn(Constants::PERMISSION_ALL);

$this->fileStorageMock->expects($this->any())
->method('getLocks')
->willReturn([]);

$this->rootMock->expects($this->any())
->method('get')
->willReturn($this->fileMock);
Expand Down Expand Up @@ -369,6 +416,10 @@ public function testLoadWithShare() {
->method('getContent')
->willReturn($fileContent);

$this->fileStorageMock->expects($this->any())
->method('getLocks')
->willReturn([]);

$this->shareMock->expects($this->any())
->method('getPermissions')
->willReturn(Constants::PERMISSION_ALL);
Expand All @@ -384,6 +435,7 @@ public function testLoadWithShare() {

$this->assertArrayHasKey('filecontents', $data);
$this->assertArrayHasKey('writeable', $data);
$this->assertArrayHasKey('locked', $data);
$this->assertArrayHasKey('mime', $data);
$this->assertArrayHasKey('mtime', $data);
$this->assertSame($data['filecontents'], $fileContent);
Expand Down Expand Up @@ -414,6 +466,10 @@ public function testSaveWithShare() {
$this->fileMock->expects($this->any())
->method('getMTime')
->willReturn($fileMTime);

$this->fileStorageMock->expects($this->any())
->method('getLocks')
->willReturn([]);

$this->rootMock->expects($this->any())
->method('get')
Expand All @@ -429,4 +485,147 @@ public function testSaveWithShare() {
$this->assertArrayHasKey('mtime', $data);
$this->assertArrayHasKey('size', $data);
}

public function testLoadReadOnly() {
$filename = 'test.txt';
$fileContent = 'test';
$fileMTime = 65638643;

$this->userMock->expects($this->any())
->method('getUID')
->willReturn('admin');

$this->userSessionMock->expects($this->any())
->method('getUser')
->willReturn($this->userMock);

$this->fileMock->expects($this->any())
->method('getPermissions')
->willReturn(Constants::PERMISSION_READ);

$this->fileMock->expects($this->any())
->method('getContent')
->willReturn($fileContent);

$this->fileMock->expects($this->any())
->method('getMTime')
->willReturn($fileMTime);

$this->fileStorageMock->expects($this->any())
->method('getLocks')
->willReturn([]);

$this->rootMock->expects($this->any())
->method('get')
->willReturn($this->fileMock);

$result = $this->controller->load('/', $filename);
$data = $result->getData();
$status = $result->getStatus();
$this->assertSame($status, 200);

$this->assertArrayHasKey('filecontents', $data);
$this->assertArrayHasKey('writeable', $data);
$this->assertArrayHasKey('locked', $data);
$this->assertArrayHasKey('mime', $data);
$this->assertArrayHasKey('mtime', $data);
$this->assertSame($data['filecontents'], $fileContent);
$this->assertSame($data['writeable'], false);
$this->assertSame($data['locked'], null);
}

public function testLoadWithPersistentLock() {
$filename = 'test.txt';
$fileContent = 'test';
$fileMTime = 65638643;

$this->userMock->expects($this->any())
->method('getUID')
->willReturn('admin');

$this->userSessionMock->expects($this->any())
->method('getUser')
->willReturn($this->userMock);

$this->fileMock->expects($this->any())
->method('getPermissions')
->willReturn(Constants::PERMISSION_ALL);

$this->fileMock->expects($this->any())
->method('getContent')
->willReturn($fileContent);

$this->fileMock->expects($this->any())
->method('getMTime')
->willReturn($fileMTime);

$this->persistentLockMock->expects($this->any())
->method('getOwner')
->willReturn('[email protected]');

$this->fileStorageMock->expects($this->any())
->method('getLocks')
->willReturn([$this->persistentLockMock]);

$this->rootMock->expects($this->any())
->method('get')
->willReturn($this->fileMock);

$result = $this->controller->load('/', $filename);
$data = $result->getData();
$status = $result->getStatus();
$this->assertSame($status, 200);

$this->assertArrayHasKey('filecontents', $data);
$this->assertArrayHasKey('writeable', $data);
$this->assertArrayHasKey('locked', $data);
$this->assertArrayHasKey('mime', $data);
$this->assertArrayHasKey('mtime', $data);
$this->assertSame($data['filecontents'], $fileContent);
$this->assertSame($data['writeable'], false);
$this->assertSame($data['locked'], '[email protected]');
}

public function testSaveWithPersistentLock() {
$path = '/test.txt';
$fileContent = 'test';
$mTime = 65638643;
$fileMTime = 65638643;

$this->userMock->expects($this->any())
->method('getUID')
->willReturn('admin');

$this->userSessionMock->expects($this->any())
->method('getUser')
->willReturn($this->userMock);

$this->fileMock->expects($this->any())
->method('getPermissions')
->willReturn(Constants::PERMISSION_ALL);

$this->fileMock->expects($this->any())
->method('getMTime')
->willReturn($fileMTime);

$this->persistentLockMock->expects($this->any())
->method('getOwner')
->willReturn('[email protected]');

$this->fileStorageMock->expects($this->any())
->method('getLocks')
->willReturn([$this->persistentLockMock]);

$this->rootMock->expects($this->any())
->method('get')
->willReturn($this->fileMock);

$result = $this->controller->save($path, $fileContent, $mTime);
$status = $result->getStatus();
$data = $result->getData();

$this->assertSame(400, $status);
$this->assertArrayHasKey('message', $data);
$this->assertSame('Cannot save file as it is locked by [email protected].', $data['message']);
}
}

0 comments on commit c730269

Please sign in to comment.