PreviewControllerTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Robin Appelman <robin@icewind.nl>
  7. * @author Roeland Jago Douma <roeland@famdouma.nl>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OCA\Files_Versions\Tests\Controller;
  26. use OCA\Files_Versions\Controller\PreviewController;
  27. use OCA\Files_Versions\Versions\IVersionManager;
  28. use OCP\AppFramework\Http;
  29. use OCP\AppFramework\Http\DataResponse;
  30. use OCP\AppFramework\Http\FileDisplayResponse;
  31. use OCP\Files\File;
  32. use OCP\Files\Folder;
  33. use OCP\Files\IMimeTypeDetector;
  34. use OCP\Files\IRootFolder;
  35. use OCP\Files\NotFoundException;
  36. use OCP\Files\SimpleFS\ISimpleFile;
  37. use OCP\IPreview;
  38. use OCP\IRequest;
  39. use OCP\IUser;
  40. use OCP\IUserSession;
  41. use Test\TestCase;
  42. class PreviewControllerTest extends TestCase {
  43. /** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
  44. private $rootFolder;
  45. /** @var string */
  46. private $userId;
  47. /** @var IMimeTypeDetector|\PHPUnit\Framework\MockObject\MockObject */
  48. private $mimeTypeDetector;
  49. /** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
  50. private $previewManager;
  51. /** @var PreviewController|\PHPUnit\Framework\MockObject\MockObject */
  52. private $controller;
  53. /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
  54. private $userSession;
  55. /** @var IVersionManager|\PHPUnit\Framework\MockObject\MockObject */
  56. private $versionManager;
  57. protected function setUp(): void {
  58. parent::setUp();
  59. $this->rootFolder = $this->createMock(IRootFolder::class);
  60. $this->userId = 'user';
  61. $user = $this->createMock(IUser::class);
  62. $user->expects($this->any())
  63. ->method('getUID')
  64. ->willReturn($this->userId);
  65. $this->mimeTypeDetector = $this->createMock(IMimeTypeDetector::class);
  66. $this->previewManager = $this->createMock(IPreview::class);
  67. $this->userSession = $this->createMock(IUserSession::class);
  68. $this->userSession->expects($this->any())
  69. ->method('getUser')
  70. ->willReturn($user);
  71. $this->versionManager = $this->createMock(IVersionManager::class);
  72. $this->controller = new PreviewController(
  73. 'files_versions',
  74. $this->createMock(IRequest::class),
  75. $this->rootFolder,
  76. $this->userSession,
  77. $this->mimeTypeDetector,
  78. $this->versionManager,
  79. $this->previewManager
  80. );
  81. }
  82. public function testInvalidFile() {
  83. $res = $this->controller->getPreview('');
  84. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  85. $this->assertEquals($expected, $res);
  86. }
  87. public function testInvalidWidth() {
  88. $res = $this->controller->getPreview('file', 0);
  89. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  90. $this->assertEquals($expected, $res);
  91. }
  92. public function testInvalidHeight() {
  93. $res = $this->controller->getPreview('file', 10, 0);
  94. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  95. $this->assertEquals($expected, $res);
  96. }
  97. public function testInvalidVersion() {
  98. $res = $this->controller->getPreview('file', 10, 0);
  99. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  100. $this->assertEquals($expected, $res);
  101. }
  102. public function testValidPreview() {
  103. $userFolder = $this->createMock(Folder::class);
  104. $userRoot = $this->createMock(Folder::class);
  105. $this->rootFolder->method('getUserFolder')
  106. ->with($this->userId)
  107. ->willReturn($userFolder);
  108. $userFolder->method('getParent')
  109. ->willReturn($userRoot);
  110. $sourceFile = $this->createMock(File::class);
  111. $userFolder->method('get')
  112. ->with('file')
  113. ->willReturn($sourceFile);
  114. $file = $this->createMock(File::class);
  115. $file->method('getMimetype')
  116. ->willReturn('myMime');
  117. $this->versionManager->method('getVersionFile')
  118. ->willReturn($file);
  119. $preview = $this->createMock(ISimpleFile::class);
  120. $this->previewManager->method('getPreview')
  121. ->with($this->equalTo($file), 10, 10, true, IPreview::MODE_FILL, 'myMime')
  122. ->willReturn($preview);
  123. $preview->method('getMimeType')
  124. ->willReturn('previewMime');
  125. $res = $this->controller->getPreview('file', 10, 10, '42');
  126. $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'previewMime']);
  127. $this->assertEquals($expected, $res);
  128. }
  129. public function testVersionNotFound() {
  130. $userFolder = $this->createMock(Folder::class);
  131. $userRoot = $this->createMock(Folder::class);
  132. $this->rootFolder->method('getUserFolder')
  133. ->with($this->userId)
  134. ->willReturn($userFolder);
  135. $userFolder->method('getParent')
  136. ->willReturn($userRoot);
  137. $sourceFile = $this->createMock(File::class);
  138. $userFolder->method('get')
  139. ->with('file')
  140. ->willReturn($sourceFile);
  141. $this->mimeTypeDetector->method('detectPath')
  142. ->with($this->equalTo('file'))
  143. ->willReturn('myMime');
  144. $this->versionManager->method('getVersionFile')
  145. ->willThrowException(new NotFoundException());
  146. $res = $this->controller->getPreview('file', 10, 10, '42');
  147. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  148. $this->assertEquals($expected, $res);
  149. }
  150. }