You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

185 lines
5.5 KiB

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