1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
|
<?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\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
*
* @package Test\DirectEditing
* @group DB
*/
class ManagerTest extends TestCase {
private $manager;
/**
* @var Editor
*/
private $editor;
/**
* @var MockObject|ISecureRandom
*/
private $random;
/**
* @var IDBConnection
*/
private $connection;
/**
* @var MockObject|IUserSession
*/
private $userSession;
/**
* @var MockObject|IRootFolder
*/
private $rootFolder;
/**
* @var MockObject|Folder
*/
private $userFolder;
/**
* @var MockObject|IManager
*/
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);
$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 testCreateFileAlreadyExists() {
$this->expectException(\RuntimeException::class);
$this->userFolder
->method('nodeExists')
->with('/File.txt')
->willReturn(true);
$this->manager->create('/File.txt', 'testeditor', 'createEmpty');
}
}
|