PreviewControllerTest.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Tests\Core\Controller;
  24. use OC\Core\Controller\PreviewController;
  25. use OCP\AppFramework\Http;
  26. use OCP\AppFramework\Http\DataResponse;
  27. use OCP\AppFramework\Utility\ITimeFactory;
  28. use OCP\Files\File;
  29. use OCP\Files\Folder;
  30. use OCP\Files\IRootFolder;
  31. use OCP\Files\NotFoundException;
  32. use OCP\Files\SimpleFS\ISimpleFile;
  33. use OCP\Files\Storage\IStorage;
  34. use OCP\IPreview;
  35. use OCP\IRequest;
  36. class PreviewControllerTest extends \Test\TestCase {
  37. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
  38. private $rootFolder;
  39. /** @var string */
  40. private $userId;
  41. /** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
  42. private $previewManager;
  43. /** @var PreviewController|\PHPUnit\Framework\MockObject\MockObject */
  44. private $controller;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->rootFolder = $this->createMock(IRootFolder::class);
  48. $this->userId = 'user';
  49. $this->previewManager = $this->createMock(IPreview::class);
  50. $this->controller = new PreviewController(
  51. 'core',
  52. $this->createMock(IRequest::class),
  53. $this->previewManager,
  54. $this->rootFolder,
  55. $this->userId,
  56. $this->createMock(ITimeFactory::class)
  57. );
  58. }
  59. public function testInvalidFile() {
  60. $res = $this->controller->getPreview('');
  61. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  62. $this->assertEquals($expected, $res);
  63. }
  64. public function testInvalidWidth() {
  65. $res = $this->controller->getPreview('file', 0);
  66. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  67. $this->assertEquals($expected, $res);
  68. }
  69. public function testInvalidHeight() {
  70. $res = $this->controller->getPreview('file', 10, 0);
  71. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  72. $this->assertEquals($expected, $res);
  73. }
  74. public function testFileNotFound() {
  75. $userFolder = $this->createMock(Folder::class);
  76. $this->rootFolder->method('getUserFolder')
  77. ->with($this->equalTo($this->userId))
  78. ->willReturn($userFolder);
  79. $userFolder->method('get')
  80. ->with($this->equalTo('file'))
  81. ->willThrowException(new NotFoundException());
  82. $res = $this->controller->getPreview('file');
  83. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  84. $this->assertEquals($expected, $res);
  85. }
  86. public function testNotAFile() {
  87. $userFolder = $this->createMock(Folder::class);
  88. $this->rootFolder->method('getUserFolder')
  89. ->with($this->equalTo($this->userId))
  90. ->willReturn($userFolder);
  91. $folder = $this->createMock(Folder::class);
  92. $userFolder->method('get')
  93. ->with($this->equalTo('file'))
  94. ->willReturn($folder);
  95. $res = $this->controller->getPreview('file');
  96. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  97. $this->assertEquals($expected, $res);
  98. }
  99. public function testNoPreviewAndNoIcon() {
  100. $userFolder = $this->createMock(Folder::class);
  101. $this->rootFolder->method('getUserFolder')
  102. ->with($this->equalTo($this->userId))
  103. ->willReturn($userFolder);
  104. $file = $this->createMock(File::class);
  105. $userFolder->method('get')
  106. ->with($this->equalTo('file'))
  107. ->willReturn($file);
  108. $this->previewManager->method('isAvailable')
  109. ->with($this->equalTo($file))
  110. ->willReturn(false);
  111. $res = $this->controller->getPreview('file', 10, 10, true, false);
  112. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  113. $this->assertEquals($expected, $res);
  114. }
  115. public function testForbiddenFile() {
  116. $userFolder = $this->createMock(Folder::class);
  117. $this->rootFolder->method('getUserFolder')
  118. ->with($this->equalTo($this->userId))
  119. ->willReturn($userFolder);
  120. $file = $this->createMock(File::class);
  121. $userFolder->method('get')
  122. ->with($this->equalTo('file'))
  123. ->willReturn($file);
  124. $this->previewManager->method('isAvailable')
  125. ->with($this->equalTo($file))
  126. ->willReturn(true);
  127. $file->method('isReadable')
  128. ->willReturn(false);
  129. $res = $this->controller->getPreview('file', 10, 10, true, true);
  130. $expected = new DataResponse([], Http::STATUS_FORBIDDEN);
  131. $this->assertEquals($expected, $res);
  132. }
  133. public function testNoPreview() {
  134. $userFolder = $this->createMock(Folder::class);
  135. $this->rootFolder->method('getUserFolder')
  136. ->with($this->equalTo($this->userId))
  137. ->willReturn($userFolder);
  138. $file = $this->createMock(File::class);
  139. $userFolder->method('get')
  140. ->with($this->equalTo('file'))
  141. ->willReturn($file);
  142. $storage = $this->createMock(IStorage::class);
  143. $file->method('getStorage')
  144. ->willReturn($storage);
  145. $this->previewManager->method('isAvailable')
  146. ->with($this->equalTo($file))
  147. ->willReturn(true);
  148. $file->method('isReadable')
  149. ->willReturn(true);
  150. $this->previewManager->method('getPreview')
  151. ->with($this->equalTo($file), 10, 10, false, $this->equalTo('myMode'))
  152. ->willThrowException(new NotFoundException());
  153. $res = $this->controller->getPreview('file', 10, 10, true, true, 'myMode');
  154. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  155. $this->assertEquals($expected, $res);
  156. }
  157. public function testValidPreview() {
  158. $userFolder = $this->createMock(Folder::class);
  159. $this->rootFolder->method('getUserFolder')
  160. ->with($this->equalTo($this->userId))
  161. ->willReturn($userFolder);
  162. $file = $this->createMock(File::class);
  163. $userFolder->method('get')
  164. ->with($this->equalTo('file'))
  165. ->willReturn($file);
  166. $this->previewManager->method('isAvailable')
  167. ->with($this->equalTo($file))
  168. ->willReturn(true);
  169. $file->method('isReadable')
  170. ->willReturn(true);
  171. $storage = $this->createMock(IStorage::class);
  172. $file->method('getStorage')
  173. ->willReturn($storage);
  174. $preview = $this->createMock(ISimpleFile::class);
  175. $preview->method('getName')->willReturn('my name');
  176. $preview->method('getMTime')->willReturn(42);
  177. $this->previewManager->method('getPreview')
  178. ->with($this->equalTo($file), 10, 10, false, $this->equalTo('myMode'))
  179. ->willReturn($preview);
  180. $preview->method('getMimeType')
  181. ->willReturn('myMime');
  182. $res = $this->controller->getPreview('file', 10, 10, true, true, 'myMode');
  183. $this->assertEquals('myMime', $res->getHeaders()['Content-Type']);
  184. $this->assertEquals(Http::STATUS_OK, $res->getStatus());
  185. $this->assertEquals($preview, $this->invokePrivate($res, 'file'));
  186. }
  187. }