Skip to content

Commit

Permalink
Trying to fix unit tests for permalinks
Browse files Browse the repository at this point in the history
  • Loading branch information
PVince81 committed May 10, 2016
1 parent 4c03288 commit d7c19a0
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 16 deletions.
24 changes: 20 additions & 4 deletions lib/Controller/OCSEndPoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ protected function getPreview($owner, $fileId, $filePath) {
}

$preview = [
'link' => $this->getPreviewLink($fileId),
'link' => $this->getPreviewLink($info['path'], $info['is_dir'], $info['view'], $fileId),
'source' => '',
'isMimeTypeIcon' => true,
];
Expand Down Expand Up @@ -332,7 +332,7 @@ protected function getPreview($owner, $fileId, $filePath) {
protected function getPreviewFromPath($filePath, $info) {
$mimeType = $this->mimeTypeDetector->detectPath($filePath);
$preview = [
'link' => $this->getPreviewLink($info['fileid']),
'link' => $this->getPreviewLink($info['path'], $info['is_dir'], $info['view'], $info['fileid']),
'source' => $this->getPreviewPathFromMimeType($mimeType),
'isMimeTypeIcon' => true,
];
Expand All @@ -354,10 +354,26 @@ protected function getPreviewPathFromMimeType($mimeType) {
}

/**
* @param string $path
* @param bool $isDir
* @param string $view
* @param int $fileId
* @return string
*/
protected function getPreviewLink($fileId) {
return $this->urlGenerator->linkToRoute('files.viewcontroller.showFile', ['fileId' => $fileId]);
protected function getPreviewLink($path, $isDir, $view, $fileId) {
if ($fileId !== null && $fileId !== '') {
return $this->urlGenerator->linkToRoute('files.viewcontroller.showFile', ['fileId' => $fileId]);
}
$params = [
'dir' => $path,
];
if (!$isDir) {
$params['dir'] = (substr_count($path, '/') === 1) ? '/' : dirname($path);
$params['scrollto'] = basename($path);
}
if ($view !== '') {
$params['view'] = $view;
}
return $this->urlGenerator->linkToRoute('files.view.index', $params);
}
}
22 changes: 20 additions & 2 deletions lib/Formatter/FileFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,29 @@ public function format(IEvent $event, $parameter) {
$info = $this->infoCache->getInfoById($this->user, $fileId, $param);
} else {
$info = $this->infoCache->getInfoByPath($this->user, $param);
$fileId = $info['fileid'];
}

$param = trim($param, '/');
$fileLink = $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileId' => $fileId]);
if ($fileId !== '') {
$fileLink = $this->urlGenerator->linkToRouteAbsolute('files.viewcontroller.showFile', ['fileId' => $fileId]);
} else {
if ($info['is_dir']) {
$linkData = ['dir' => $info['path']];
} else {
$parentDir = (substr_count($info['path'], '/') === 1) ? '/' : dirname($info['path']);
$fileName = basename($info['path']);
$linkData = [
'dir' => $parentDir,
'scrollto' => $fileName,
];
}

if ($info['view'] !== '') {
$linkData['view'] = $info['view'];
}

$fileLink = $this->urlGenerator->linkToRouteAbsolute('files.view.index', $linkData);
}

return '<file link="' . $fileLink . '" id="' . Util::sanitizeHTML($fileId) . '">' . Util::sanitizeHTML($param) . '</file>';
}
Expand Down
23 changes: 22 additions & 1 deletion tests/Controller/OCSEndPointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -738,13 +738,34 @@ public function testGetPreviewPathFromMimeType($mimeType, $icon, $expected) {
);
}

public function dataGetPreviewLink() {
return [
['/folder', true, '', ['dir' => '/folder']],
['/folder/sub1', true, 'trashbin', ['dir' => '/folder/sub1', 'view' => 'trashbin']],
['/folder/sub1/sub2', true, '', ['dir' => '/folder/sub1/sub2']],
['/file.txt', false, '', ['dir' => '/', 'scrollto' => 'file.txt']],
['/folder/file.txt', false, 'trashbin', ['dir' => '/folder', 'scrollto' => 'file.txt', 'view' => 'trashbin']],
['/folder/sub1/file.txt', false, '', ['dir' => '/folder/sub1', 'scrollto' => 'file.txt']],
];
}

/**
* @dataProvider dataGetPreviewLink
*
* @param string $path
* @param bool $isDir
* @param string $view
* @param array $expected
*/
public function testGetPreviewLink($fileId, $expected) {
public function testGetPreviewLink($path, $isDir, $view, $expected) {
$this->urlGenerator->expects($this->once())
->method('linkToRoute')
->with('files.view.index', $expected);

$this->invokePrivate($this->controller, 'getPreviewLink', [$path, $isDir, $view]);
}

public function testGetPreviewLinkFileId() {
$this->urlGenerator->expects($this->once())
->method('linkToRoute')
->with('files.viewcontroller.showFile', ['fileId' => 123]);
Expand Down
2 changes: 1 addition & 1 deletion tests/FilesHooksTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -871,7 +871,7 @@ public function testAddNotificationsForUser($user, $subject, $parameter, $fileId

$this->urlGenerator->expects($this->once())
->method('linkToRouteAbsolute')
->with('files.view.index', ['dir' => $urlPath])
->with('files.viewcontroller.showFile', ['fileId' => $fileId])
->willReturn('routeToFilesIndex');

$event = $this->getMockBuilder('OCP\Activity\IEvent')
Expand Down
27 changes: 19 additions & 8 deletions tests/Formatter/FileFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,28 @@ public function dataFormat() {
'exists' => true,
'view' => 'trashbin',
];
$trash2 = [
'path' => '/test2',
'is_dir' => true,
'exists' => true,
'view' => 'trashbin',
'fileid' => 42
];

return [
['user1', '/test1', false, [], '<file link="apps/files/?dir=%2F&scrollto=test1" id="">test1</file>'],
['user1', '/test1', false, [], '<file link="apps/files/?dir=%2F&scrollto=test1" id="">test1</file>', 'files.view.index'],
['user1', '/test1', true, [], '<file link="apps/files/?dir=%2Ftest1" id="">test1</file>'],
['user1', '/test1/test2', false, [], '<file link="apps/files/?dir=%2Ftest1&scrollto=test2" id="">test1/test2</file>'],
['user1', '/test1/test2', true, [], '<file link="apps/files/?dir=%2Ftest1%2Ftest2" id="">test1/test2</file>'],
['user1', '/test1/test2', false, [], '<file link="apps/files/?dir=%2Ftest1&scrollto=test2" id="">test1/test2</file>', 'files.view.index'],
['user1', '/test1/test2', true, [], '<file link="apps/files/?dir=%2Ftest1%2Ftest2" id="">test1/test2</file>', 'files.view.index'],

['user1', [42 => '/test1/test2'], false, [], '<file link="f/42" id="42">test1/test2</file>', 'files.viewcontroller.showFile'],
['user1', [42 => '/test1/test2'], true, [], '<file link="f/42" id="42">test1/test2</file>', 'files.viewcontroller.showFile'],

['user1', '/test1/test2', false, $trash0, '<file link="apps/files/?dir=%2F&scrollto=test2&view=trashbin" id="42">test1/test2</file>'],
['user1', '/test1/test2', true, $trash1, '<file link="apps/files/?dir=%2Ftest2&view=trashbin" id="42">test1/test2</file>'],
['user1', '/test1/test2', false, $trash0, '<file link="apps/files/?dir=%2F&scrollto=test2&view=trashbin" id="42">test1/test2</file>', 'files.view.index'],
['user1', '/test1/test2', true, $trash1, '<file link="apps/files/?dir=%2Ftest2&view=trashbin" id="42">test1/test2</file>', 'files.view.index'],
['user1', '/test1/test2', true, $trash2, '<file link="f/42" id="42">test1/test2</file>', 'files.viewcontroller.showFile'],

['user2', '/test1', false, [], '<file link="apps/files/?dir=%2F&scrollto=test1" id="">test1</file>'],
['user2', '/test1', false, [], '<file link="apps/files/?dir=%2F&scrollto=test1" id="">test1</file>', 'files.view.index'],
];
}

Expand All @@ -118,7 +129,7 @@ public function dataFormat() {
* @param array $info
* @param string $expected
*/
public function testFormat($user, $parameter, $isDir, array $info, $expected) {
public function testFormat($user, $parameter, $isDir, array $info, $expected, $routeName) {
/** @var \OCP\Activity\IEvent|\PHPUnit_Framework_MockObject_MockObject $event */
$event = $this->getMockBuilder('OCP\Activity\IEvent')
->disableOriginalConstructor()
Expand All @@ -137,7 +148,7 @@ public function testFormat($user, $parameter, $isDir, array $info, $expected) {

$this->urlGenerator->expects($this->once())
->method('linkToRouteAbsolute')
->with('files.view.index', $this->anything())
->with($routeName, $this->anything())
->willReturnCallback(function($route, $parameters) {
$paramList = [];
foreach ($parameters as $key => $value) {
Expand Down

0 comments on commit d7c19a0

Please sign in to comment.