You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ManagerTest.php 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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\IUserSession;
  17. use OCP\L10N\IFactory;
  18. use OCP\Security\ISecureRandom;
  19. use PHPUnit\Framework\MockObject\MockObject;
  20. use Test\TestCase;
  21. class CreateEmpty extends ACreateEmpty {
  22. public function getId(): string {
  23. return 'createEmpty';
  24. }
  25. public function getName(): string {
  26. return 'create empty file';
  27. }
  28. public function getExtension(): string {
  29. return '.txt';
  30. }
  31. public function getMimetype(): string {
  32. return 'text/plain';
  33. }
  34. }
  35. class Editor implements IEditor {
  36. public function getId(): string {
  37. return 'testeditor';
  38. }
  39. public function getName(): string {
  40. return 'Test editor';
  41. }
  42. public function getMimetypes(): array {
  43. return [ 'text/plain' ];
  44. }
  45. public function getMimetypesOptional(): array {
  46. return [];
  47. }
  48. public function getCreators(): array {
  49. return [
  50. new CreateEmpty()
  51. ];
  52. }
  53. public function isSecure(): bool {
  54. return false;
  55. }
  56. public function open(IToken $token): Response {
  57. return new DataResponse('edit page');
  58. }
  59. }
  60. /**
  61. * Class ManagerTest
  62. *
  63. * @package Test\DirectEditing
  64. * @group DB
  65. */
  66. class ManagerTest extends TestCase {
  67. private $manager;
  68. /**
  69. * @var Editor
  70. */
  71. private $editor;
  72. /**
  73. * @var MockObject|ISecureRandom
  74. */
  75. private $random;
  76. /**
  77. * @var IDBConnection
  78. */
  79. private $connection;
  80. /**
  81. * @var MockObject|IUserSession
  82. */
  83. private $userSession;
  84. /**
  85. * @var MockObject|IRootFolder
  86. */
  87. private $rootFolder;
  88. /**
  89. * @var MockObject|Folder
  90. */
  91. private $userFolder;
  92. /**
  93. * @var MockObject|IL10N
  94. */
  95. private $l10n;
  96. /**
  97. * @var MockObject|IManager
  98. */
  99. private $encryptionManager;
  100. protected function setUp(): void {
  101. parent::setUp();
  102. $this->editor = new Editor();
  103. $this->random = $this->createMock(ISecureRandom::class);
  104. $this->connection = \OC::$server->getDatabaseConnection();
  105. $this->userSession = $this->createMock(IUserSession::class);
  106. $this->rootFolder = $this->createMock(IRootFolder::class);
  107. $this->userFolder = $this->createMock(Folder::class);
  108. $this->l10n = $this->createMock(IL10N::class);
  109. $this->encryptionManager = $this->createMock(IManager::class);
  110. $l10nFactory = $this->createMock(IFactory::class);
  111. $l10nFactory->expects($this->once())
  112. ->method('get')
  113. ->willReturn($this->l10n);
  114. $this->rootFolder->expects($this->any())
  115. ->method('getUserFolder')
  116. ->willReturn($this->userFolder);
  117. $this->manager = new Manager(
  118. $this->random, $this->connection, $this->userSession, $this->rootFolder, $l10nFactory, $this->encryptionManager
  119. );
  120. $this->manager->registerDirectEditor($this->editor);
  121. }
  122. public function testEditorRegistration() {
  123. $this->assertEquals($this->manager->getEditors(), ['testeditor' => $this->editor]);
  124. }
  125. public function testCreateToken() {
  126. $expectedToken = 'TOKEN' . time();
  127. $file = $this->createMock(File::class);
  128. $file->expects($this->any())
  129. ->method('getId')
  130. ->willReturn(123);
  131. $this->random->expects($this->once())
  132. ->method('generate')
  133. ->willReturn($expectedToken);
  134. $folder = $this->createMock(Folder::class);
  135. $this->userFolder
  136. ->method('nodeExists')
  137. ->withConsecutive(['/File.txt'], ['/'])
  138. ->willReturnOnConsecutiveCalls(false, true);
  139. $this->userFolder
  140. ->method('get')
  141. ->with('/')
  142. ->willReturn($folder);
  143. $folder->expects($this->once())
  144. ->method('newFile')
  145. ->willReturn($file);
  146. $token = $this->manager->create('/File.txt', 'testeditor', 'createEmpty');
  147. $this->assertEquals($token, $expectedToken);
  148. }
  149. public function testCreateTokenAccess() {
  150. $expectedToken = 'TOKEN' . time();
  151. $file = $this->createMock(File::class);
  152. $file->expects($this->any())
  153. ->method('getId')
  154. ->willReturn(123);
  155. $this->random->expects($this->once())
  156. ->method('generate')
  157. ->willReturn($expectedToken);
  158. $folder = $this->createMock(Folder::class);
  159. $this->userFolder
  160. ->method('nodeExists')
  161. ->withConsecutive(['/File.txt'], ['/'])
  162. ->willReturnOnConsecutiveCalls(false, true);
  163. $this->userFolder
  164. ->method('get')
  165. ->with('/')
  166. ->willReturn($folder);
  167. $folder->expects($this->once())
  168. ->method('newFile')
  169. ->willReturn($file);
  170. $this->manager->create('/File.txt', 'testeditor', 'createEmpty');
  171. $firstResult = $this->manager->edit($expectedToken);
  172. $secondResult = $this->manager->edit($expectedToken);
  173. $this->assertInstanceOf(DataResponse::class, $firstResult);
  174. $this->assertInstanceOf(NotFoundResponse::class, $secondResult);
  175. }
  176. public function testCreateFileAlreadyExists() {
  177. $this->expectException(\RuntimeException::class);
  178. $this->userFolder
  179. ->method('nodeExists')
  180. ->with('/File.txt')
  181. ->willReturn(true);
  182. $this->manager->create('/File.txt', 'testeditor', 'createEmpty');
  183. }
  184. }