PreviewControllerTest.php 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Tests\Core\Controller;
  7. use OC\Core\Controller\PreviewController;
  8. use OCP\AppFramework\Http;
  9. use OCP\AppFramework\Http\DataResponse;
  10. use OCP\Files\File;
  11. use OCP\Files\Folder;
  12. use OCP\Files\IRootFolder;
  13. use OCP\Files\NotFoundException;
  14. use OCP\Files\SimpleFS\ISimpleFile;
  15. use OCP\Files\Storage\IStorage;
  16. use OCP\IPreview;
  17. use OCP\IRequest;
  18. use OCP\Preview\IMimeIconProvider;
  19. class PreviewControllerTest extends \Test\TestCase {
  20. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
  21. private $rootFolder;
  22. /** @var string */
  23. private $userId;
  24. /** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
  25. private $previewManager;
  26. /** @var PreviewController|\PHPUnit\Framework\MockObject\MockObject */
  27. private $controller;
  28. protected function setUp(): void {
  29. parent::setUp();
  30. $this->rootFolder = $this->createMock(IRootFolder::class);
  31. $this->userId = 'user';
  32. $this->previewManager = $this->createMock(IPreview::class);
  33. $this->controller = new PreviewController(
  34. 'core',
  35. $this->createMock(IRequest::class),
  36. $this->previewManager,
  37. $this->rootFolder,
  38. $this->userId,
  39. $this->createMock(IMimeIconProvider::class)
  40. );
  41. }
  42. public function testInvalidFile() {
  43. $res = $this->controller->getPreview('');
  44. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  45. $this->assertEquals($expected, $res);
  46. }
  47. public function testInvalidWidth() {
  48. $res = $this->controller->getPreview('file', 0);
  49. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  50. $this->assertEquals($expected, $res);
  51. }
  52. public function testInvalidHeight() {
  53. $res = $this->controller->getPreview('file', 10, 0);
  54. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  55. $this->assertEquals($expected, $res);
  56. }
  57. public function testFileNotFound() {
  58. $userFolder = $this->createMock(Folder::class);
  59. $this->rootFolder->method('getUserFolder')
  60. ->with($this->equalTo($this->userId))
  61. ->willReturn($userFolder);
  62. $userFolder->method('get')
  63. ->with($this->equalTo('file'))
  64. ->willThrowException(new NotFoundException());
  65. $res = $this->controller->getPreview('file');
  66. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  67. $this->assertEquals($expected, $res);
  68. }
  69. public function testNotAFile() {
  70. $userFolder = $this->createMock(Folder::class);
  71. $this->rootFolder->method('getUserFolder')
  72. ->with($this->equalTo($this->userId))
  73. ->willReturn($userFolder);
  74. $folder = $this->createMock(Folder::class);
  75. $userFolder->method('get')
  76. ->with($this->equalTo('file'))
  77. ->willReturn($folder);
  78. $res = $this->controller->getPreview('file');
  79. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  80. $this->assertEquals($expected, $res);
  81. }
  82. public function testNoPreviewAndNoIcon() {
  83. $userFolder = $this->createMock(Folder::class);
  84. $this->rootFolder->method('getUserFolder')
  85. ->with($this->equalTo($this->userId))
  86. ->willReturn($userFolder);
  87. $file = $this->createMock(File::class);
  88. $userFolder->method('get')
  89. ->with($this->equalTo('file'))
  90. ->willReturn($file);
  91. $this->previewManager->method('isAvailable')
  92. ->with($this->equalTo($file))
  93. ->willReturn(false);
  94. $res = $this->controller->getPreview('file', 10, 10, true, false);
  95. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  96. $this->assertEquals($expected, $res);
  97. }
  98. public function testForbiddenFile() {
  99. $userFolder = $this->createMock(Folder::class);
  100. $this->rootFolder->method('getUserFolder')
  101. ->with($this->equalTo($this->userId))
  102. ->willReturn($userFolder);
  103. $file = $this->createMock(File::class);
  104. $userFolder->method('get')
  105. ->with($this->equalTo('file'))
  106. ->willReturn($file);
  107. $this->previewManager->method('isAvailable')
  108. ->with($this->equalTo($file))
  109. ->willReturn(true);
  110. $file->method('isReadable')
  111. ->willReturn(false);
  112. $res = $this->controller->getPreview('file', 10, 10, true, true);
  113. $expected = new DataResponse([], Http::STATUS_FORBIDDEN);
  114. $this->assertEquals($expected, $res);
  115. }
  116. public function testNoPreview() {
  117. $userFolder = $this->createMock(Folder::class);
  118. $this->rootFolder->method('getUserFolder')
  119. ->with($this->equalTo($this->userId))
  120. ->willReturn($userFolder);
  121. $file = $this->createMock(File::class);
  122. $userFolder->method('get')
  123. ->with($this->equalTo('file'))
  124. ->willReturn($file);
  125. $storage = $this->createMock(IStorage::class);
  126. $file->method('getStorage')
  127. ->willReturn($storage);
  128. $this->previewManager->method('isAvailable')
  129. ->with($this->equalTo($file))
  130. ->willReturn(true);
  131. $file->method('isReadable')
  132. ->willReturn(true);
  133. $this->previewManager->method('getPreview')
  134. ->with($this->equalTo($file), 10, 10, false, $this->equalTo('myMode'))
  135. ->willThrowException(new NotFoundException());
  136. $res = $this->controller->getPreview('file', 10, 10, true, true, 'myMode');
  137. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  138. $this->assertEquals($expected, $res);
  139. }
  140. public function testValidPreview() {
  141. $userFolder = $this->createMock(Folder::class);
  142. $this->rootFolder->method('getUserFolder')
  143. ->with($this->equalTo($this->userId))
  144. ->willReturn($userFolder);
  145. $file = $this->createMock(File::class);
  146. $userFolder->method('get')
  147. ->with($this->equalTo('file'))
  148. ->willReturn($file);
  149. $this->previewManager->method('isAvailable')
  150. ->with($this->equalTo($file))
  151. ->willReturn(true);
  152. $file->method('isReadable')
  153. ->willReturn(true);
  154. $storage = $this->createMock(IStorage::class);
  155. $file->method('getStorage')
  156. ->willReturn($storage);
  157. $preview = $this->createMock(ISimpleFile::class);
  158. $preview->method('getName')->willReturn('my name');
  159. $preview->method('getMTime')->willReturn(42);
  160. $this->previewManager->method('getPreview')
  161. ->with($this->equalTo($file), 10, 10, false, $this->equalTo('myMode'))
  162. ->willReturn($preview);
  163. $preview->method('getMimeType')
  164. ->willReturn('myMime');
  165. $res = $this->controller->getPreview('file', 10, 10, true, true, 'myMode');
  166. $this->assertEquals('myMime', $res->getHeaders()['Content-Type']);
  167. $this->assertEquals(Http::STATUS_OK, $res->getStatus());
  168. $this->assertEquals($preview, $this->invokePrivate($res, 'file'));
  169. }
  170. }