ManagerTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. <?php
  2. namespace Test\DirectEditing;
  3. use OC\DirectEditing\Manager;
  4. use OC\Files\Node\File;
  5. use OCP\AppFramework\Http\DataResponse;
  6. use OCP\AppFramework\Http\NotFoundResponse;
  7. use OCP\AppFramework\Http\Response;
  8. use OCP\DirectEditing\ACreateEmpty;
  9. use OCP\DirectEditing\IEditor;
  10. use OCP\DirectEditing\IToken;
  11. use OCP\Encryption\IManager;
  12. use OCP\Files\Folder;
  13. use OCP\Files\IRootFolder;
  14. use OCP\IDBConnection;
  15. use OCP\IL10N;
  16. use OCP\IUser;
  17. use OCP\IUserSession;
  18. use OCP\L10N\IFactory;
  19. use OCP\Security\ISecureRandom;
  20. use PHPUnit\Framework\MockObject\MockObject;
  21. use Test\TestCase;
  22. class CreateEmpty extends ACreateEmpty {
  23. public function getId(): string {
  24. return 'createEmpty';
  25. }
  26. public function getName(): string {
  27. return 'create empty file';
  28. }
  29. public function getExtension(): string {
  30. return '.txt';
  31. }
  32. public function getMimetype(): string {
  33. return 'text/plain';
  34. }
  35. }
  36. class Editor implements IEditor {
  37. public function getId(): string {
  38. return 'testeditor';
  39. }
  40. public function getName(): string {
  41. return 'Test editor';
  42. }
  43. public function getMimetypes(): array {
  44. return [ 'text/plain' ];
  45. }
  46. public function getMimetypesOptional(): array {
  47. return [];
  48. }
  49. public function getCreators(): array {
  50. return [
  51. new CreateEmpty()
  52. ];
  53. }
  54. public function isSecure(): bool {
  55. return false;
  56. }
  57. public function open(IToken $token): Response {
  58. return new DataResponse('edit page');
  59. }
  60. }
  61. /**
  62. * Class ManagerTest
  63. *
  64. * @package Test\DirectEditing
  65. * @group DB
  66. */
  67. class ManagerTest extends TestCase {
  68. private $manager;
  69. /**
  70. * @var Editor
  71. */
  72. private $editor;
  73. /**
  74. * @var MockObject|ISecureRandom
  75. */
  76. private $random;
  77. /**
  78. * @var IDBConnection
  79. */
  80. private $connection;
  81. /**
  82. * @var MockObject|IUserSession
  83. */
  84. private $userSession;
  85. /**
  86. * @var MockObject|IRootFolder
  87. */
  88. private $rootFolder;
  89. /**
  90. * @var MockObject|Folder
  91. */
  92. private $userFolder;
  93. /**
  94. * @var MockObject|IL10N
  95. */
  96. private $l10n;
  97. /**
  98. * @var MockObject|IManager
  99. */
  100. private $encryptionManager;
  101. protected function setUp(): void {
  102. parent::setUp();
  103. $this->editor = new Editor();
  104. $this->random = $this->createMock(ISecureRandom::class);
  105. $this->connection = \OC::$server->getDatabaseConnection();
  106. $this->userSession = $this->createMock(IUserSession::class);
  107. $this->rootFolder = $this->createMock(IRootFolder::class);
  108. $this->userFolder = $this->createMock(Folder::class);
  109. $this->l10n = $this->createMock(IL10N::class);
  110. $this->encryptionManager = $this->createMock(IManager::class);
  111. $l10nFactory = $this->createMock(IFactory::class);
  112. $l10nFactory->expects($this->once())
  113. ->method('get')
  114. ->willReturn($this->l10n);
  115. $this->rootFolder->expects($this->any())
  116. ->method('getUserFolder')
  117. ->willReturn($this->userFolder);
  118. $user = $this->createMock(IUser::class);
  119. $user->expects(self::any())
  120. ->method('getUID')
  121. ->willReturn('admin');
  122. $this->userSession->expects(self::any())
  123. ->method('getUser')
  124. ->willReturn($user);
  125. $this->manager = new Manager(
  126. $this->random, $this->connection, $this->userSession, $this->rootFolder, $l10nFactory, $this->encryptionManager
  127. );
  128. $this->manager->registerDirectEditor($this->editor);
  129. }
  130. public function testEditorRegistration() {
  131. $this->assertEquals($this->manager->getEditors(), ['testeditor' => $this->editor]);
  132. }
  133. public function testCreateToken() {
  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. $folder = $this->createMock(Folder::class);
  143. $this->userFolder
  144. ->method('nodeExists')
  145. ->withConsecutive(['/File.txt'], ['/'])
  146. ->willReturnOnConsecutiveCalls(false, true);
  147. $this->userFolder
  148. ->method('get')
  149. ->with('/')
  150. ->willReturn($folder);
  151. $folder->expects($this->once())
  152. ->method('newFile')
  153. ->willReturn($file);
  154. $token = $this->manager->create('/File.txt', 'testeditor', 'createEmpty');
  155. $this->assertEquals($token, $expectedToken);
  156. }
  157. public function testCreateTokenAccess() {
  158. $expectedToken = 'TOKEN' . time();
  159. $file = $this->createMock(File::class);
  160. $file->expects($this->any())
  161. ->method('getId')
  162. ->willReturn(123);
  163. $this->random->expects($this->once())
  164. ->method('generate')
  165. ->willReturn($expectedToken);
  166. $folder = $this->createMock(Folder::class);
  167. $this->userFolder
  168. ->method('nodeExists')
  169. ->withConsecutive(['/File.txt'], ['/'])
  170. ->willReturnOnConsecutiveCalls(false, true);
  171. $this->userFolder
  172. ->method('get')
  173. ->with('/')
  174. ->willReturn($folder);
  175. $folder->expects($this->once())
  176. ->method('newFile')
  177. ->willReturn($file);
  178. $this->manager->create('/File.txt', 'testeditor', 'createEmpty');
  179. $firstResult = $this->manager->edit($expectedToken);
  180. $secondResult = $this->manager->edit($expectedToken);
  181. $this->assertInstanceOf(DataResponse::class, $firstResult);
  182. $this->assertInstanceOf(NotFoundResponse::class, $secondResult);
  183. }
  184. public function testOpenByPath() {
  185. $expectedToken = 'TOKEN' . time();
  186. $file = $this->createMock(File::class);
  187. $file->expects($this->any())
  188. ->method('getId')
  189. ->willReturn(123);
  190. $file->expects($this->any())
  191. ->method('getPath')
  192. ->willReturn('/admin/files/File.txt');
  193. $this->random->expects($this->once())
  194. ->method('generate')
  195. ->willReturn($expectedToken);
  196. $folder = $this->createMock(Folder::class);
  197. $this->userFolder
  198. ->method('nodeExists')
  199. ->withConsecutive(['/File.txt'], ['/'])
  200. ->willReturnOnConsecutiveCalls(false, true);
  201. $this->userFolder
  202. ->method('get')
  203. ->with('/File.txt')
  204. ->willReturn($file);
  205. $this->userFolder
  206. ->method('getRelativePath')
  207. ->willReturn('/File.txt');
  208. $this->manager->open('/File.txt', 'testeditor');
  209. $firstResult = $this->manager->edit($expectedToken);
  210. $secondResult = $this->manager->edit($expectedToken);
  211. $this->assertInstanceOf(DataResponse::class, $firstResult);
  212. $this->assertInstanceOf(NotFoundResponse::class, $secondResult);
  213. }
  214. public function testOpenById() {
  215. $expectedToken = 'TOKEN' . time();
  216. $fileRead = $this->createMock(File::class);
  217. $fileRead->method('getPermissions')
  218. ->willReturn(1);
  219. $fileRead->expects($this->any())
  220. ->method('getId')
  221. ->willReturn(123);
  222. $fileRead->expects($this->any())
  223. ->method('getPath')
  224. ->willReturn('/admin/files/shared_file.txt');
  225. $file = $this->createMock(File::class);
  226. $file->method('getPermissions')
  227. ->willReturn(1);
  228. $file->expects($this->any())
  229. ->method('getId')
  230. ->willReturn(123);
  231. $file->expects($this->any())
  232. ->method('getPath')
  233. ->willReturn('/admin/files/File.txt');
  234. $this->random->expects($this->once())
  235. ->method('generate')
  236. ->willReturn($expectedToken);
  237. $folder = $this->createMock(Folder::class);
  238. $folder->expects($this->any())
  239. ->method('getById')
  240. ->willReturn([
  241. $fileRead,
  242. $file
  243. ]);
  244. $this->userFolder
  245. ->method('nodeExists')
  246. ->withConsecutive(['/File.txt'], ['/'])
  247. ->willReturnOnConsecutiveCalls(false, true);
  248. $this->userFolder
  249. ->method('get')
  250. ->with('/')
  251. ->willReturn($folder);
  252. $this->userFolder
  253. ->method('getRelativePath')
  254. ->willReturn('/File.txt');
  255. $this->manager->open('/', 'testeditor', 123);
  256. $firstResult = $this->manager->edit($expectedToken);
  257. $secondResult = $this->manager->edit($expectedToken);
  258. $this->assertInstanceOf(DataResponse::class, $firstResult);
  259. $this->assertInstanceOf(NotFoundResponse::class, $secondResult);
  260. }
  261. public function testCreateFileAlreadyExists() {
  262. $this->expectException(\RuntimeException::class);
  263. $this->userFolder
  264. ->method('nodeExists')
  265. ->with('/File.txt')
  266. ->willReturn(true);
  267. $this->manager->create('/File.txt', 'testeditor', 'createEmpty');
  268. }
  269. }