2016-10-18 11:36:57 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016, Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
2019-12-03 21:57:53 +03:00
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-10-18 11:36:57 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2016-10-18 11:36:57 +03:00
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2016-10-18 11:36:57 +03:00
|
|
|
namespace OCA\Files_Trashbin\Tests\Controller;
|
|
|
|
|
|
|
|
use OCA\Files_Trashbin\Controller\PreviewController;
|
2018-10-08 17:42:37 +03:00
|
|
|
use OCA\Files_Trashbin\Trash\ITrashManager;
|
2016-10-18 11:36:57 +03:00
|
|
|
use OCP\AppFramework\Http;
|
|
|
|
use OCP\AppFramework\Http\DataResponse;
|
|
|
|
use OCP\AppFramework\Http\FileDisplayResponse;
|
2018-05-23 15:25:51 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2016-10-18 11:36:57 +03:00
|
|
|
use OCP\Files\File;
|
|
|
|
use OCP\Files\Folder;
|
|
|
|
use OCP\Files\IMimeTypeDetector;
|
|
|
|
use OCP\Files\IRootFolder;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFile;
|
|
|
|
use OCP\IPreview;
|
|
|
|
use OCP\IRequest;
|
2018-10-08 17:42:37 +03:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\IUserSession;
|
2016-10-18 11:36:57 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class PreviewControllerTest extends TestCase {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IRootFolder|\PHPUnit\Framework\MockObject\MockObject */
|
2016-10-18 11:36:57 +03:00
|
|
|
private $rootFolder;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $userId;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IMimeTypeDetector|\PHPUnit\Framework\MockObject\MockObject */
|
2016-10-18 11:36:57 +03:00
|
|
|
private $mimeTypeDetector;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IPreview|\PHPUnit\Framework\MockObject\MockObject */
|
2016-10-18 11:36:57 +03:00
|
|
|
private $previewManager;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
|
2018-05-23 15:25:51 +03:00
|
|
|
private $time;
|
|
|
|
|
2016-10-18 11:36:57 +03:00
|
|
|
/** @var PreviewController */
|
|
|
|
private $controller;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var ITrashManager|\PHPUnit\Framework\MockObject\MockObject */
|
2018-10-08 17:42:37 +03:00
|
|
|
private $trashManager;
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
|
2018-10-08 17:42:37 +03:00
|
|
|
private $userSession;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2016-10-18 11:36:57 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->rootFolder = $this->createMock(IRootFolder::class);
|
|
|
|
$this->userId = 'user';
|
|
|
|
$this->mimeTypeDetector = $this->createMock(IMimeTypeDetector::class);
|
|
|
|
$this->previewManager = $this->createMock(IPreview::class);
|
2018-05-23 15:25:51 +03:00
|
|
|
$this->time = $this->createMock(ITimeFactory::class);
|
2018-10-08 17:42:37 +03:00
|
|
|
$this->trashManager = $this->createMock(ITrashManager::class);
|
|
|
|
$this->userSession = $this->createMock(IUserSession::class);
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$user->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn($this->userId);
|
|
|
|
|
|
|
|
$this->userSession->expects($this->any())
|
|
|
|
->method('getUser')
|
|
|
|
->willReturn($user);
|
2016-10-18 11:36:57 +03:00
|
|
|
|
|
|
|
$this->controller = new PreviewController(
|
|
|
|
'files_versions',
|
|
|
|
$this->createMock(IRequest::class),
|
|
|
|
$this->rootFolder,
|
2018-10-08 17:42:37 +03:00
|
|
|
$this->trashManager,
|
|
|
|
$this->userSession,
|
2016-10-18 11:36:57 +03:00
|
|
|
$this->mimeTypeDetector,
|
2018-05-23 15:25:51 +03:00
|
|
|
$this->previewManager,
|
|
|
|
$this->time
|
2016-10-18 11:36:57 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidWidth() {
|
2018-05-23 15:25:51 +03:00
|
|
|
$res = $this->controller->getPreview(42, 0);
|
2016-10-18 11:36:57 +03:00
|
|
|
$expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $res);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testInvalidHeight() {
|
2018-05-23 15:25:51 +03:00
|
|
|
$res = $this->controller->getPreview(42, 10, 0);
|
2016-10-18 11:36:57 +03:00
|
|
|
$expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $res);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidPreview() {
|
|
|
|
$userFolder = $this->createMock(Folder::class);
|
|
|
|
$userRoot = $this->createMock(Folder::class);
|
|
|
|
$trash = $this->createMock(Folder::class);
|
|
|
|
|
|
|
|
$this->rootFolder->method('getUserFolder')
|
|
|
|
->with($this->userId)
|
|
|
|
->willReturn($userFolder);
|
|
|
|
$userFolder->method('getParent')
|
|
|
|
->willReturn($userRoot);
|
|
|
|
$userRoot->method('get')
|
|
|
|
->with('files_trashbin/files')
|
|
|
|
->willReturn($trash);
|
|
|
|
|
|
|
|
$this->mimeTypeDetector->method('detectPath')
|
|
|
|
->with($this->equalTo('file'))
|
|
|
|
->willReturn('myMime');
|
|
|
|
|
|
|
|
$file = $this->createMock(File::class);
|
2018-05-23 15:25:51 +03:00
|
|
|
$trash->method('getById')
|
|
|
|
->with($this->equalTo(42))
|
|
|
|
->willReturn([$file]);
|
2016-10-18 11:36:57 +03:00
|
|
|
$file->method('getName')
|
2018-10-08 17:42:37 +03:00
|
|
|
->willReturn('file.d1234');
|
2016-10-18 11:36:57 +03:00
|
|
|
|
2018-05-23 15:25:51 +03:00
|
|
|
$file->method('getParent')
|
|
|
|
->willReturn($trash);
|
|
|
|
|
2018-10-08 17:42:37 +03:00
|
|
|
$this->trashManager->expects($this->any())
|
|
|
|
->method('getTrashNodeById')
|
|
|
|
->willReturn($file);
|
|
|
|
|
2016-10-18 11:36:57 +03:00
|
|
|
$preview = $this->createMock(ISimpleFile::class);
|
|
|
|
$this->previewManager->method('getPreview')
|
|
|
|
->with($this->equalTo($file), 10, 10, true, IPreview::MODE_FILL, 'myMime')
|
|
|
|
->willReturn($preview);
|
|
|
|
$preview->method('getMimeType')
|
|
|
|
->willReturn('previewMime');
|
|
|
|
|
2018-05-23 15:25:51 +03:00
|
|
|
$this->time->method('getTime')
|
|
|
|
->willReturn(1337);
|
|
|
|
|
|
|
|
$this->overwriteService(ITimeFactory::class, $this->time);
|
|
|
|
|
|
|
|
$res = $this->controller->getPreview(42, 10, 10);
|
2016-10-18 11:36:57 +03:00
|
|
|
$expected = new FileDisplayResponse($preview, Http::STATUS_OK, ['Content-Type' => 'previewMime']);
|
2018-05-23 15:25:51 +03:00
|
|
|
$expected->cacheFor(3600 * 24);
|
2016-10-18 11:36:57 +03:00
|
|
|
|
|
|
|
$this->assertEquals($expected, $res);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTrashFileNotFound() {
|
|
|
|
$userFolder = $this->createMock(Folder::class);
|
|
|
|
$userRoot = $this->createMock(Folder::class);
|
|
|
|
$trash = $this->createMock(Folder::class);
|
|
|
|
|
|
|
|
$this->rootFolder->method('getUserFolder')
|
|
|
|
->with($this->userId)
|
|
|
|
->willReturn($userFolder);
|
|
|
|
$userFolder->method('getParent')
|
|
|
|
->willReturn($userRoot);
|
|
|
|
$userRoot->method('get')
|
|
|
|
->with('files_trashbin/files')
|
|
|
|
->willReturn($trash);
|
|
|
|
|
2018-05-23 15:25:51 +03:00
|
|
|
$trash->method('getById')
|
|
|
|
->with($this->equalTo(42))
|
|
|
|
->willReturn([]);
|
2016-10-18 11:36:57 +03:00
|
|
|
|
2018-05-23 15:25:51 +03:00
|
|
|
$res = $this->controller->getPreview(42, 10, 10);
|
2016-10-18 11:36:57 +03:00
|
|
|
$expected = new DataResponse([], Http::STATUS_NOT_FOUND);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $res);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testTrashFolder() {
|
|
|
|
$userFolder = $this->createMock(Folder::class);
|
|
|
|
$userRoot = $this->createMock(Folder::class);
|
|
|
|
$trash = $this->createMock(Folder::class);
|
|
|
|
|
|
|
|
$this->rootFolder->method('getUserFolder')
|
|
|
|
->with($this->userId)
|
|
|
|
->willReturn($userFolder);
|
|
|
|
$userFolder->method('getParent')
|
|
|
|
->willReturn($userRoot);
|
|
|
|
$userRoot->method('get')
|
|
|
|
->with('files_trashbin/files')
|
|
|
|
->willReturn($trash);
|
|
|
|
|
|
|
|
$folder = $this->createMock(Folder::class);
|
2018-10-08 17:42:37 +03:00
|
|
|
$this->trashManager->expects($this->any())
|
|
|
|
->method('getTrashNodeById')
|
|
|
|
->willReturn($folder);
|
2018-05-23 15:25:51 +03:00
|
|
|
$trash->method('getById')
|
|
|
|
->with($this->equalTo(43))
|
|
|
|
->willReturn([$folder]);
|
2016-10-18 11:36:57 +03:00
|
|
|
|
2018-05-23 15:25:51 +03:00
|
|
|
$res = $this->controller->getPreview(43, 10, 10);
|
2016-10-18 11:36:57 +03:00
|
|
|
$expected = new DataResponse([], Http::STATUS_BAD_REQUEST);
|
|
|
|
|
|
|
|
$this->assertEquals($expected, $res);
|
|
|
|
}
|
|
|
|
}
|