Skip to content

Commit 43f0a0e

Browse files
committed
Direct editing token tests
Signed-off-by: Julius Härtl <[email protected]>
1 parent eb305d2 commit 43f0a0e

1 file changed

Lines changed: 152 additions & 0 deletions

File tree

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
<?php
2+
3+
namespace Test\DirectEditing;
4+
5+
use OC\DirectEditing\Manager;
6+
use OC\Files\Node\File;
7+
use OCP\AppFramework\Http\DataResponse;
8+
use OCP\AppFramework\Http\NotFoundResponse;
9+
use OCP\AppFramework\Http\Response;
10+
use OCP\AppFramework\Http\TemplateResponse;
11+
use OCP\DirectEditing\ACreateEmpty;
12+
use OCP\DirectEditing\IEditor;
13+
use OCP\DirectEditing\IToken;
14+
use OCP\Files\Folder;
15+
use OCP\Files\IRootFolder;
16+
use OCP\IDBConnection;
17+
use OCP\IUserSession;
18+
use OCP\Security\ISecureRandom;
19+
use Test\TestCase;
20+
21+
class CreateEmpty extends ACreateEmpty {
22+
23+
public function getId(): string {
24+
return 'createEmpty';
25+
}
26+
27+
public function getName(): string {
28+
return 'create empty file';
29+
}
30+
31+
public function getExtension(): string {
32+
return '.txt';
33+
}
34+
}
35+
36+
class Editor implements IEditor {
37+
38+
public function getId(): string {
39+
return 'testeditor';
40+
}
41+
42+
public function getName(): string {
43+
return 'Test editor';
44+
}
45+
46+
public function getMimetypes(): array {
47+
return [ 'text/plain' ];
48+
}
49+
50+
51+
public function getMimetypesOptional(): array {
52+
return [];
53+
}
54+
55+
public function getCreators(): array {
56+
return [
57+
new CreateEmpty()
58+
];
59+
}
60+
61+
public function isSecure(): bool {
62+
return false;
63+
}
64+
65+
66+
public function open(IToken $token): Response {
67+
return new DataResponse('edit page');
68+
}
69+
}
70+
71+
/**
72+
* Class ManagerTest
73+
*
74+
* @package Test\DirectEditing
75+
* @group DB
76+
*/
77+
class ManagerTest extends TestCase {
78+
79+
private $manager;
80+
/**
81+
* @var Editor
82+
*/
83+
private $editor;
84+
/**
85+
* @var \PHPUnit\Framework\MockObject\MockObject
86+
*/
87+
private $random;
88+
89+
protected function setUp() {
90+
parent::setUp();
91+
92+
$this->editor = new Editor();
93+
94+
$this->random = $this->createMock(ISecureRandom::class);
95+
$this->connection = \OC::$server->getDatabaseConnection();
96+
$this->userSession = $this->createMock(IUserSession::class);
97+
$this->rootFolder = $this->createMock(IRootFolder::class);
98+
$this->userFolder = $this->createMock(Folder::class);
99+
100+
101+
$this->rootFolder->expects($this->any())
102+
->method('getUserFolder')
103+
->willReturn($this->userFolder);
104+
105+
$this->manager = new Manager(
106+
$this->random, $this->connection, $this->userSession, $this->rootFolder
107+
);
108+
109+
$this->manager->registerDirectEditor($this->editor);
110+
}
111+
112+
public function testEditorRegistration() {
113+
$this->assertEquals($this->manager->getEditors(), ['testeditor' => $this->editor]);
114+
}
115+
116+
117+
public function testCreateToken() {
118+
$expectedToken = 'TOKEN' . time();
119+
$file = $this->createMock(File::class);
120+
$file->expects($this->any())
121+
->method('getId')
122+
->willReturn(123);
123+
$this->random->expects($this->once())
124+
->method('generate')
125+
->willReturn($expectedToken);
126+
$this->userFolder->expects($this->once())
127+
->method('newFile')
128+
->willReturn($file);
129+
$token = $this->manager->create('/File.txt', 'testeditor', 'createEmpty');
130+
$this->assertEquals($token, $expectedToken);
131+
}
132+
133+
public function testCreateTokenAccess() {
134+
$expectedToken = 'TOKEN' . time();
135+
$file = $this->createMock(File::class);
136+
$file->expects($this->any())
137+
->method('getId')
138+
->willReturn(123);
139+
$this->random->expects($this->once())
140+
->method('generate')
141+
->willReturn($expectedToken);
142+
$this->userFolder->expects($this->once())
143+
->method('newFile')
144+
->willReturn($file);
145+
$this->manager->create('/File.txt', 'testeditor', 'createEmpty');
146+
$firstResult = $this->manager->edit($expectedToken);
147+
$secondResult = $this->manager->edit($expectedToken);
148+
$this->assertInstanceOf(DataResponse::class, $firstResult);
149+
$this->assertInstanceOf(NotFoundResponse::class, $secondResult);
150+
}
151+
152+
}

0 commit comments

Comments
 (0)