1
0

PreviewControllerTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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->previewManager = $this->createMock(IPreview::class);
  66. $this->userSession = $this->createMock(IUserSession::class);
  67. $this->userSession->expects($this->any())
  68. ->method('getUser')
  69. ->willReturn($user);
  70. $this->versionManager = $this->createMock(IVersionManager::class);
  71. $this->controller = new PreviewController(
  72. 'files_versions',
  73. $this->createMock(IRequest::class),
  74. $this->rootFolder,
  75. $this->userSession,
  76. $this->versionManager,
  77. $this->previewManager
  78. );
  79. }
  80. public function testInvalidFile() {
  81. $res = $this->controller->getPreview('');
  82. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  83. $this->assertEquals($expected, $res);
  84. }
  85. public function testInvalidWidth() {
  86. $res = $this->controller->getPreview('file', 0);
  87. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  88. $this->assertEquals($expected, $res);
  89. }
  90. public function testInvalidHeight() {
  91. $res = $this->controller->getPreview('file', 10, 0);
  92. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  93. $this->assertEquals($expected, $res);
  94. }
  95. public function testInvalidVersion() {
  96. $res = $this->controller->getPreview('file', 10, 0);
  97. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  98. $this->assertEquals($expected, $res);
  99. }
  100. public function testValidPreview() {
  101. $userFolder = $this->createMock(Folder::class);
  102. $userRoot = $this->createMock(Folder::class);
  103. $this->rootFolder->method('getUserFolder')
  104. ->with($this->userId)
  105. ->willReturn($userFolder);
  106. $userFolder->method('getParent')
  107. ->willReturn($userRoot);
  108. $sourceFile = $this->createMock(File::class);
  109. $userFolder->method('get')
  110. ->with('file')
  111. ->willReturn($sourceFile);
  112. $file = $this->createMock(File::class);
  113. $file->method('getMimetype')
  114. ->willReturn('myMime');
  115. $this->versionManager->method('getVersionFile')
  116. ->willReturn($file);
  117. $preview = $this->createMock(ISimpleFile::class);
  118. $preview->method('getName')->willReturn('name');
  119. $preview->method('getMTime')->willReturn(42);
  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->versionManager->method('getVersionFile')
  142. ->willThrowException(new NotFoundException());
  143. $res = $this->controller->getPreview('file', 10, 10, '42');
  144. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  145. $this->assertEquals($expected, $res);
  146. }
  147. }