Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make permalinks work for trashed files #24537

Merged
merged 1 commit into from
May 11, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/files/appinfo/application.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ public function __construct(array $urlParams=array()) {
$server->getConfig(),
$server->getEventDispatcher(),
$server->getUserSession(),
$server->getUserFolder()
$server->getAppManager(),
$server->getRootFolder()
);
});

Expand Down
52 changes: 35 additions & 17 deletions apps/files/controller/viewcontroller.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use OCP\AppFramework\Http\NotFoundResponse;
use OCP\Files\Folder;
use OCP\App\IAppManager;

/**
* Class ViewController
Expand All @@ -62,8 +63,10 @@ class ViewController extends Controller {
protected $eventDispatcher;
/** @var IUserSession */
protected $userSession;
/** @var IAppManager */
protected $appManager;
/** @var \OCP\Files\Folder */
protected $userFolder;
protected $rootFolder;

/**
* @param string $appName
Expand All @@ -74,7 +77,8 @@ class ViewController extends Controller {
* @param IConfig $config
* @param EventDispatcherInterface $eventDispatcherInterface
* @param IUserSession $userSession
* @param Folder $userFolder
* @param IAppManager $appManager
* @param Folder $rootFolder
*/
public function __construct($appName,
IRequest $request,
Expand All @@ -84,7 +88,8 @@ public function __construct($appName,
IConfig $config,
EventDispatcherInterface $eventDispatcherInterface,
IUserSession $userSession,
Folder $userFolder
IAppManager $appManager,
Folder $rootFolder
) {
parent::__construct($appName, $request);
$this->appName = $appName;
Expand All @@ -95,7 +100,8 @@ public function __construct($appName,
$this->config = $config;
$this->eventDispatcher = $eventDispatcherInterface;
$this->userSession = $userSession;
$this->userFolder = $userFolder;
$this->appManager = $appManager;
$this->rootFolder = $rootFolder;
}

/**
Expand Down Expand Up @@ -265,21 +271,33 @@ public function index($dir = '', $view = '', $fileid = null) {
* @NoAdminRequired
*/
public function showFile($fileId) {
$files = $this->userFolder->getById($fileId);
$params = [];
try {
$uid = $this->userSession->getUser()->getUID();
$baseFolder = $this->rootFolder->get($uid . '/files/');
$files = $baseFolder->getById($fileId);
$params = [];

if (empty($files) && $this->appManager->isEnabledForUser('files_trashbin')) {
$baseFolder = $this->rootFolder->get($uid . '/files_trashbin/files/');
$files = $baseFolder->getById($fileId);
$params['view'] = 'trashbin';
}

if (!empty($files)) {
$file = current($files);
if ($file instanceof Folder) {
// set the full path to enter the folder
$params['dir'] = $this->userFolder->getRelativePath($file->getPath());
} else {
// set parent path as dir
$params['dir'] = $this->userFolder->getRelativePath($file->getParent()->getPath());
// and scroll to the entry
$params['scrollto'] = $file->getName();
if (!empty($files)) {
$file = current($files);
if ($file instanceof Folder) {
// set the full path to enter the folder
$params['dir'] = $baseFolder->getRelativePath($file->getPath());
} else {
// set parent path as dir
$params['dir'] = $baseFolder->getRelativePath($file->getParent()->getPath());
// and scroll to the entry
$params['scrollto'] = $file->getName();
}
return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params));
}
return new RedirectResponse($this->urlGenerator->linkToRoute('files.view.index', $params));
} catch (\OCP\Files\NotFoundException $e) {
return new NotFoundResponse();
}
return new NotFoundResponse();
}
Expand Down
114 changes: 102 additions & 12 deletions apps/files/tests/controller/ViewControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use OCP\IUserSession;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use OCP\Files\Folder;
use OCP\App\IAppManager;

/**
* Class ViewControllerTest
Expand All @@ -62,8 +63,10 @@ class ViewControllerTest extends TestCase {
private $user;
/** @var IUserSession */
private $userSession;
/** @var IAppManager */
private $appManager;
/** @var Folder */
private $userFolder;
private $rootFolder;

public function setUp() {
parent::setUp();
Expand All @@ -74,11 +77,15 @@ public function setUp() {
$this->config = $this->getMock('\OCP\IConfig');
$this->eventDispatcher = $this->getMock('\Symfony\Component\EventDispatcher\EventDispatcherInterface');
$this->userSession = $this->getMock('\OCP\IUserSession');
$this->appManager = $this->getMock('\OCP\App\IAppManager');
$this->user = $this->getMock('\OCP\IUser');
$this->user->expects($this->any())
->method('getUID')
->will($this->returnValue('testuser1'));
$this->userSession->expects($this->any())
->method('getUser')
->will($this->returnValue($this->user));
$this->userFolder = $this->getMock('\OCP\Files\Folder');
$this->rootFolder = $this->getMock('\OCP\Files\Folder');
$this->viewController = $this->getMockBuilder('\OCA\Files\Controller\ViewController')
->setConstructorArgs([
'files',
Expand All @@ -89,7 +96,8 @@ public function setUp() {
$this->config,
$this->eventDispatcher,
$this->userSession,
$this->userFolder
$this->appManager,
$this->rootFolder
])
->setMethods([
'getStorageInfo',
Expand Down Expand Up @@ -307,15 +315,22 @@ public function testShowFileRouteWithFolder($useShowFile) {
$node = $this->getMock('\OCP\Files\Folder');
$node->expects($this->once())
->method('getPath')
->will($this->returnValue('/user/files/test/sub'));
->will($this->returnValue('/testuser1/files/test/sub'));

$this->userFolder->expects($this->at(0))
$baseFolder = $this->getMock('\OCP\Files\Folder');

$this->rootFolder->expects($this->once())
->method('get')
->with('testuser1/files/')
->will($this->returnValue($baseFolder));

$baseFolder->expects($this->at(0))
->method('getById')
->with(123)
->will($this->returnValue([$node]));
$this->userFolder->expects($this->at(1))
$baseFolder->expects($this->at(1))
->method('getRelativePath')
->with('/user/files/test/sub')
->with('/testuser1/files/test/sub')
->will($this->returnValue('/test/sub'));

$this->urlGenerator
Expand All @@ -339,7 +354,14 @@ public function testShowFileRouteWithFile($useShowFile) {
$parentNode = $this->getMock('\OCP\Files\Folder');
$parentNode->expects($this->once())
->method('getPath')
->will($this->returnValue('/user/files/test'));
->will($this->returnValue('testuser1/files/test'));

$baseFolder = $this->getMock('\OCP\Files\Folder');

$this->rootFolder->expects($this->once())
->method('get')
->with('testuser1/files/')
->will($this->returnValue($baseFolder));

$node = $this->getMock('\OCP\Files\File');
$node->expects($this->once())
Expand All @@ -349,13 +371,13 @@ public function testShowFileRouteWithFile($useShowFile) {
->method('getName')
->will($this->returnValue('somefile.txt'));

$this->userFolder->expects($this->at(0))
$baseFolder->expects($this->at(0))
->method('getById')
->with(123)
->will($this->returnValue([$node]));
$this->userFolder->expects($this->at(1))
$baseFolder->expects($this->at(1))
->method('getRelativePath')
->with('/user/files/test')
->with('testuser1/files/test')
->will($this->returnValue('/test'));

$this->urlGenerator
Expand All @@ -376,7 +398,13 @@ public function testShowFileRouteWithFile($useShowFile) {
* @dataProvider showFileMethodProvider
*/
public function testShowFileRouteWithInvalidFileId($useShowFile) {
$this->userFolder->expects($this->at(0))
$baseFolder = $this->getMock('\OCP\Files\Folder');
$this->rootFolder->expects($this->once())
->method('get')
->with('testuser1/files/')
->will($this->returnValue($baseFolder));

$baseFolder->expects($this->at(0))
->method('getById')
->with(123)
->will($this->returnValue([]));
Expand All @@ -388,4 +416,66 @@ public function testShowFileRouteWithInvalidFileId($useShowFile) {
$this->assertEquals($expected, $this->viewController->index('/whatever', '', '123'));
}
}

/**
* @dataProvider showFileMethodProvider
*/
public function testShowFileRouteWithTrashedFile($useShowFile) {
$this->appManager->expects($this->once())
->method('isEnabledForUser')
->with('files_trashbin')
->will($this->returnValue(true));

$parentNode = $this->getMock('\OCP\Files\Folder');
$parentNode->expects($this->once())
->method('getPath')
->will($this->returnValue('testuser1/files_trashbin/files/test.d1462861890/sub'));

$baseFolderFiles = $this->getMock('\OCP\Files\Folder');
$baseFolderTrash = $this->getMock('\OCP\Files\Folder');

$this->rootFolder->expects($this->at(0))
->method('get')
->with('testuser1/files/')
->will($this->returnValue($baseFolderFiles));
$this->rootFolder->expects($this->at(1))
->method('get')
->with('testuser1/files_trashbin/files/')
->will($this->returnValue($baseFolderTrash));

$baseFolderFiles->expects($this->once())
->method('getById')
->with(123)
->will($this->returnValue([]));

$node = $this->getMock('\OCP\Files\File');
$node->expects($this->once())
->method('getParent')
->will($this->returnValue($parentNode));
$node->expects($this->once())
->method('getName')
->will($this->returnValue('somefile.txt'));

$baseFolderTrash->expects($this->at(0))
->method('getById')
->with(123)
->will($this->returnValue([$node]));
$baseFolderTrash->expects($this->at(1))
->method('getRelativePath')
->with('testuser1/files_trashbin/files/test.d1462861890/sub')
->will($this->returnValue('/test.d1462861890/sub'));

$this->urlGenerator
->expects($this->once())
->method('linkToRoute')
->with('files.view.index', ['view' => 'trashbin', 'dir' => '/test.d1462861890/sub', 'scrollto' => 'somefile.txt'])
->will($this->returnValue('/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt'));

$expected = new Http\RedirectResponse('/apps/files/?view=trashbin&dir=/test.d1462861890/sub&scrollto=somefile.txt');
if ($useShowFile) {
$this->assertEquals($expected, $this->viewController->showFile(123));
} else {
$this->assertEquals($expected, $this->viewController->index('/whatever', '', '123'));
}
}
}