1
0

PreviewControllerTest.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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 OCA\Files_Trashbin\Tests\Controller;
  24. use OCA\Files_Trashbin\Controller\PreviewController;
  25. use OCP\AppFramework\Http;
  26. use OCP\AppFramework\Http\DataResponse;
  27. use OCP\AppFramework\Http\FileDisplayResponse;
  28. use OCP\Files\File;
  29. use OCP\Files\Folder;
  30. use OCP\Files\IMimeTypeDetector;
  31. use OCP\Files\IRootFolder;
  32. use OCP\Files\NotFoundException;
  33. use OCP\Files\SimpleFS\ISimpleFile;
  34. use OCP\IPreview;
  35. use OCP\IRequest;
  36. use Test\TestCase;
  37. class PreviewControllerTest extends TestCase {
  38. /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
  39. private $rootFolder;
  40. /** @var string */
  41. private $userId;
  42. /** @var IMimeTypeDetector|\PHPUnit_Framework_MockObject_MockObject */
  43. private $mimeTypeDetector;
  44. /** @var IPreview|\PHPUnit_Framework_MockObject_MockObject */
  45. private $previewManager;
  46. /** @var PreviewController */
  47. private $controller;
  48. public function setUp() {
  49. parent::setUp();
  50. $this->rootFolder = $this->createMock(IRootFolder::class);
  51. $this->userId = 'user';
  52. $this->mimeTypeDetector = $this->createMock(IMimeTypeDetector::class);
  53. $this->previewManager = $this->createMock(IPreview::class);
  54. $this->controller = new PreviewController(
  55. 'files_versions',
  56. $this->createMock(IRequest::class),
  57. $this->rootFolder,
  58. $this->userId,
  59. $this->mimeTypeDetector,
  60. $this->previewManager
  61. );
  62. }
  63. public function testInvalidFile() {
  64. $res = $this->controller->getPreview('');
  65. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  66. $this->assertEquals($expected, $res);
  67. }
  68. public function testInvalidWidth() {
  69. $res = $this->controller->getPreview('file', 0);
  70. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  71. $this->assertEquals($expected, $res);
  72. }
  73. public function testInvalidHeight() {
  74. $res = $this->controller->getPreview('file', 10, 0);
  75. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  76. $this->assertEquals($expected, $res);
  77. }
  78. public function testValidPreview() {
  79. $userFolder = $this->createMock(Folder::class);
  80. $userRoot = $this->createMock(Folder::class);
  81. $trash = $this->createMock(Folder::class);
  82. $this->rootFolder->method('getUserFolder')
  83. ->with($this->userId)
  84. ->willReturn($userFolder);
  85. $userFolder->method('getParent')
  86. ->willReturn($userRoot);
  87. $userRoot->method('get')
  88. ->with('files_trashbin/files')
  89. ->willReturn($trash);
  90. $this->mimeTypeDetector->method('detectPath')
  91. ->with($this->equalTo('file'))
  92. ->willReturn('myMime');
  93. $file = $this->createMock(File::class);
  94. $trash->method('get')
  95. ->with($this->equalTo('file.1234'))
  96. ->willReturn($file);
  97. $file->method('getName')
  98. ->willReturn('file.1234');
  99. $preview = $this->createMock(ISimpleFile::class);
  100. $this->previewManager->method('getPreview')
  101. ->with($this->equalTo($file), 10, 10, true, IPreview::MODE_FILL, 'myMime')
  102. ->willReturn($preview);
  103. $preview->method('getMimeType')
  104. ->willReturn('previewMime');
  105. $res = $this->controller->getPreview('file.1234', 10, 10);
  106. $expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'previewMime']);
  107. $this->assertEquals($expected, $res);
  108. }
  109. public function testTrashFileNotFound() {
  110. $userFolder = $this->createMock(Folder::class);
  111. $userRoot = $this->createMock(Folder::class);
  112. $trash = $this->createMock(Folder::class);
  113. $this->rootFolder->method('getUserFolder')
  114. ->with($this->userId)
  115. ->willReturn($userFolder);
  116. $userFolder->method('getParent')
  117. ->willReturn($userRoot);
  118. $userRoot->method('get')
  119. ->with('files_trashbin/files')
  120. ->willReturn($trash);
  121. $trash->method('get')
  122. ->with($this->equalTo('file.1234'))
  123. ->willThrowException(new NotFoundException());
  124. $res = $this->controller->getPreview('file.1234', 10, 10);
  125. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  126. $this->assertEquals($expected, $res);
  127. }
  128. public function testTrashFolder() {
  129. $userFolder = $this->createMock(Folder::class);
  130. $userRoot = $this->createMock(Folder::class);
  131. $trash = $this->createMock(Folder::class);
  132. $this->rootFolder->method('getUserFolder')
  133. ->with($this->userId)
  134. ->willReturn($userFolder);
  135. $userFolder->method('getParent')
  136. ->willReturn($userRoot);
  137. $userRoot->method('get')
  138. ->with('files_trashbin/files')
  139. ->willReturn($trash);
  140. $folder = $this->createMock(Folder::class);
  141. $trash->method('get')
  142. ->with($this->equalTo('folder'))
  143. ->willReturn($folder);
  144. $res = $this->controller->getPreview('folder', 10, 10);
  145. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  146. $this->assertEquals($expected, $res);
  147. }
  148. }