Only pass parent if paths match

As the user folder might be initialized by the root from two levels
down the hierarchy, passing this as a parent only works if the path matches

Signed-off-by: Julius Härtl <jus@bitgrid.net>
pull/33602/head
Julius Härtl 2022-08-24 18:01:10 +07:00
parent 4baf960c21
commit 83b1415906
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
2 changed files with 38 additions and 6 deletions

@ -119,10 +119,11 @@ class Folder extends Node implements \OCP\Files\Folder {
} else {
$isDir = $info->getType() === FileInfo::TYPE_FOLDER;
}
$parent = dirname($path) === $this->getPath() ? $this : null;
if ($isDir) {
return new Folder($this->root, $this->view, $path, $info);
return new Folder($this->root, $this->view, $path, $info, $parent);
} else {
return new File($this->root, $this->view, $path, $info);
return new File($this->root, $this->view, $path, $info, $parent);
}
}
@ -163,7 +164,8 @@ class Folder extends Node implements \OCP\Files\Folder {
if (!$this->view->mkdir($fullPath)) {
throw new NotPermittedException('Could not create folder');
}
$node = new Folder($this->root, $this->view, $fullPath, null, $this);
$parent = dirname($fullPath) === $this->getPath() ? $this : null;
$node = new Folder($this->root, $this->view, $fullPath, null, $parent);
$this->sendHooks(['postWrite', 'postCreate'], [$node]);
return $node;
} else {

@ -14,6 +14,7 @@ use OC\Files\Config\CachedMountInfo;
use OC\Files\FileInfo;
use OC\Files\Mount\Manager;
use OC\Files\Mount\MountPoint;
use OC\Files\Node\File;
use OC\Files\Node\Folder;
use OC\Files\Node\Node;
use OC\Files\Node\Root;
@ -105,11 +106,13 @@ class FolderTest extends NodeTest {
->method('getUser')
->willReturn($this->user);
$node = new File($root, $view, '/bar/foo/asd');
$root->method('get')
->with('/bar/foo/asd');
->with('/bar/foo/asd')
->willReturn($node);
$node = new Folder($root, $view, '/bar/foo');
$node->get('asd');
$parentNode = new Folder($root, $view, '/bar/foo');
self::assertEquals($node, $parentNode->get('asd'));
}
public function testNodeExists() {
@ -183,6 +186,33 @@ class FolderTest extends NodeTest {
$this->assertEquals($child, $result);
}
public function testNewFolderDeepParent() {
$manager = $this->createMock(Manager::class);
/**
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
*/
$view = $this->createMock(View::class);
$root = $this->getMockBuilder(Root::class)
->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher])
->getMock();
$root->expects($this->any())
->method('getUser')
->willReturn($this->user);
$view->method('getFileInfo')
->with('/foobar')
->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL]));
$view->method('mkdir')
->with('/foobar/asd/sdf')
->willReturn(true);
$node = new Folder($root, $view, '/foobar');
$child = new Folder($root, $view, '/foobar/asd/sdf', null, null);
$result = $node->newFolder('asd/sdf');
$this->assertEquals($child, $result);
}
public function testNewFolderNotPermitted() {
$this->expectException(\OCP\Files\NotPermittedException::class);