1
0

ManagerTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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|IManager
  94. */
  95. private $encryptionManager;
  96. protected function setUp(): void {
  97. parent::setUp();
  98. $this->editor = new Editor();
  99. $this->random = $this->createMock(ISecureRandom::class);
  100. $this->connection = \OC::$server->getDatabaseConnection();
  101. $this->userSession = $this->createMock(IUserSession::class);
  102. $this->rootFolder = $this->createMock(IRootFolder::class);
  103. $this->userFolder = $this->createMock(Folder::class);
  104. $this->l10n = $this->createMock(IL10N::class);
  105. $this->encryptionManager = $this->createMock(IManager::class);
  106. $l10nFactory = $this->createMock(IFactory::class);
  107. $l10nFactory->expects($this->once())
  108. ->method('get')
  109. ->willReturn($this->l10n);
  110. $this->rootFolder->expects($this->any())
  111. ->method('getUserFolder')
  112. ->willReturn($this->userFolder);
  113. $this->manager = new Manager(
  114. $this->random, $this->connection, $this->userSession, $this->rootFolder, $l10nFactory, $this->encryptionManager
  115. );
  116. $this->manager->registerDirectEditor($this->editor);
  117. }
  118. public function testEditorRegistration() {
  119. $this->assertEquals($this->manager->getEditors(), ['testeditor' => $this->editor]);
  120. }
  121. public function testCreateToken() {
  122. $expectedToken = 'TOKEN' . time();
  123. $file = $this->createMock(File::class);
  124. $file->expects($this->any())
  125. ->method('getId')
  126. ->willReturn(123);
  127. $this->random->expects($this->once())
  128. ->method('generate')
  129. ->willReturn($expectedToken);
  130. $folder = $this->createMock(Folder::class);
  131. $this->userFolder
  132. ->method('nodeExists')
  133. ->withConsecutive(['/File.txt'], ['/'])
  134. ->willReturnOnConsecutiveCalls(false, true);
  135. $this->userFolder
  136. ->method('get')
  137. ->with('/')
  138. ->willReturn($folder);
  139. $folder->expects($this->once())
  140. ->method('newFile')
  141. ->willReturn($file);
  142. $token = $this->manager->create('/File.txt', 'testeditor', 'createEmpty');
  143. $this->assertEquals($token, $expectedToken);
  144. }
  145. public function testCreateTokenAccess() {
  146. $expectedToken = 'TOKEN' . time();
  147. $file = $this->createMock(File::class);
  148. $file->expects($this->any())
  149. ->method('getId')
  150. ->willReturn(123);
  151. $this->random->expects($this->once())
  152. ->method('generate')
  153. ->willReturn($expectedToken);
  154. $folder = $this->createMock(Folder::class);
  155. $this->userFolder
  156. ->method('nodeExists')
  157. ->withConsecutive(['/File.txt'], ['/'])
  158. ->willReturnOnConsecutiveCalls(false, true);
  159. $this->userFolder
  160. ->method('get')
  161. ->with('/')
  162. ->willReturn($folder);
  163. $folder->expects($this->once())
  164. ->method('newFile')
  165. ->willReturn($file);
  166. $this->manager->create('/File.txt', 'testeditor', 'createEmpty');
  167. $firstResult = $this->manager->edit($expectedToken);
  168. $secondResult = $this->manager->edit($expectedToken);
  169. $this->assertInstanceOf(DataResponse::class, $firstResult);
  170. $this->assertInstanceOf(NotFoundResponse::class, $secondResult);
  171. }
  172. public function testCreateFileAlreadyExists() {
  173. $this->expectException(\RuntimeException::class);
  174. $this->userFolder
  175. ->method('nodeExists')
  176. ->with('/File.txt')
  177. ->willReturn(true);
  178. $this->manager->create('/File.txt', 'testeditor', 'createEmpty');
  179. }
  180. }