PreviewControllerTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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\IPreview;
  34. use OCP\IRequest;
  35. class PreviewControllerTest extends \Test\TestCase {
  36. /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject */
  37. private $rootFolder;
  38. /** @var string */
  39. private $userId;
  40. /** @var IPreview|\PHPUnit_Framework_MockObject_MockObject */
  41. private $previewManager;
  42. /** @var PreviewController|\PHPUnit_Framework_MockObject_MockObject */
  43. private $controller;
  44. public function setUp() {
  45. parent::setUp();
  46. $this->rootFolder = $this->createMock(IRootFolder::class);
  47. $this->userId = 'user';
  48. $this->previewManager = $this->createMock(IPreview::class);
  49. $this->controller = new PreviewController(
  50. 'core',
  51. $this->createMock(IRequest::class),
  52. $this->previewManager,
  53. $this->rootFolder,
  54. $this->userId,
  55. $this->createMock(ITimeFactory::class)
  56. );
  57. }
  58. public function testInvalidFile() {
  59. $res = $this->controller->getPreview('');
  60. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  61. $this->assertEquals($expected, $res);
  62. }
  63. public function testInvalidWidth() {
  64. $res = $this->controller->getPreview('file', 0);
  65. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  66. $this->assertEquals($expected, $res);
  67. }
  68. public function testInvalidHeight() {
  69. $res = $this->controller->getPreview('file', 10, 0);
  70. $expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
  71. $this->assertEquals($expected, $res);
  72. }
  73. public function testFileNotFound() {
  74. $userFolder = $this->createMock(Folder::class);
  75. $this->rootFolder->method('getUserFolder')
  76. ->with($this->equalTo($this->userId))
  77. ->willReturn($userFolder);
  78. $userFolder->method('get')
  79. ->with($this->equalTo('file'))
  80. ->willThrowException(new NotFoundException());
  81. $res = $this->controller->getPreview('file');
  82. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  83. $this->assertEquals($expected, $res);
  84. }
  85. public function testNotAFile() {
  86. $userFolder = $this->createMock(Folder::class);
  87. $this->rootFolder->method('getUserFolder')
  88. ->with($this->equalTo($this->userId))
  89. ->willReturn($userFolder);
  90. $folder = $this->createMock(Folder::class);
  91. $userFolder->method('get')
  92. ->with($this->equalTo('file'))
  93. ->willReturn($folder);
  94. $res = $this->controller->getPreview('file');
  95. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  96. $this->assertEquals($expected, $res);
  97. }
  98. public function testNoPreviewAndNoIcon() {
  99. $userFolder = $this->createMock(Folder::class);
  100. $this->rootFolder->method('getUserFolder')
  101. ->with($this->equalTo($this->userId))
  102. ->willReturn($userFolder);
  103. $file = $this->createMock(File::class);
  104. $userFolder->method('get')
  105. ->with($this->equalTo('file'))
  106. ->willReturn($file);
  107. $this->previewManager->method('isAvailable')
  108. ->with($this->equalTo($file))
  109. ->willReturn(false);
  110. $res = $this->controller->getPreview('file', 10, 10, true, false);
  111. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  112. $this->assertEquals($expected, $res);
  113. }
  114. public function testForbiddenFile() {
  115. $userFolder = $this->createMock(Folder::class);
  116. $this->rootFolder->method('getUserFolder')
  117. ->with($this->equalTo($this->userId))
  118. ->willReturn($userFolder);
  119. $file = $this->createMock(File::class);
  120. $userFolder->method('get')
  121. ->with($this->equalTo('file'))
  122. ->willReturn($file);
  123. $this->previewManager->method('isAvailable')
  124. ->with($this->equalTo($file))
  125. ->willReturn(true);
  126. $file->method('isReadable')
  127. ->willReturn(false);
  128. $res = $this->controller->getPreview('file', 10, 10, true, true);
  129. $expected = new DataResponse([], Http::STATUS_FORBIDDEN);
  130. $this->assertEquals($expected, $res);
  131. }
  132. public function testNoPreview() {
  133. $userFolder = $this->createMock(Folder::class);
  134. $this->rootFolder->method('getUserFolder')
  135. ->with($this->equalTo($this->userId))
  136. ->willReturn($userFolder);
  137. $file = $this->createMock(File::class);
  138. $userFolder->method('get')
  139. ->with($this->equalTo('file'))
  140. ->willReturn($file);
  141. $this->previewManager->method('isAvailable')
  142. ->with($this->equalTo($file))
  143. ->willReturn(true);
  144. $file->method('isReadable')
  145. ->willReturn(true);
  146. $this->previewManager->method('getPreview')
  147. ->with($this->equalTo($file), 10, 10, false, $this->equalTo('myMode'))
  148. ->willThrowException(new NotFoundException());
  149. $res = $this->controller->getPreview('file', 10, 10, true, true, 'myMode');
  150. $expected = new DataResponse([], Http::STATUS_NOT_FOUND);
  151. $this->assertEquals($expected, $res);
  152. }
  153. public function testValidPreview() {
  154. $userFolder = $this->createMock(Folder::class);
  155. $this->rootFolder->method('getUserFolder')
  156. ->with($this->equalTo($this->userId))
  157. ->willReturn($userFolder);
  158. $file = $this->createMock(File::class);
  159. $userFolder->method('get')
  160. ->with($this->equalTo('file'))
  161. ->willReturn($file);
  162. $this->previewManager->method('isAvailable')
  163. ->with($this->equalTo($file))
  164. ->willReturn(true);
  165. $file->method('isReadable')
  166. ->willReturn(true);
  167. $preview = $this->createMock(ISimpleFile::class);
  168. $this->previewManager->method('getPreview')
  169. ->with($this->equalTo($file), 10, 10, false, $this->equalTo('myMode'))
  170. ->willReturn($preview);
  171. $preview->method('getMimeType')
  172. ->willReturn('myMime');
  173. $res = $this->controller->getPreview('file', 10, 10, true, true, 'myMode');
  174. $this->assertEquals('myMime', $res->getHeaders()['Content-Type']);
  175. $this->assertEquals(Http::STATUS_OK, $res->getStatus());
  176. $this->assertEquals($preview, $this->invokePrivate($res, 'file'));
  177. }
  178. }