Skip to content

Commit 83b1415

Browse files
committed
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>
1 parent 4baf960 commit 83b1415

2 files changed

Lines changed: 38 additions & 6 deletions

File tree

lib/private/Files/Node/Folder.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,11 @@ protected function createNode($path, FileInfo $info = null) {
119119
} else {
120120
$isDir = $info->getType() === FileInfo::TYPE_FOLDER;
121121
}
122+
$parent = dirname($path) === $this->getPath() ? $this : null;
122123
if ($isDir) {
123-
return new Folder($this->root, $this->view, $path, $info);
124+
return new Folder($this->root, $this->view, $path, $info, $parent);
124125
} else {
125-
return new File($this->root, $this->view, $path, $info);
126+
return new File($this->root, $this->view, $path, $info, $parent);
126127
}
127128
}
128129

@@ -163,7 +164,8 @@ public function newFolder($path) {
163164
if (!$this->view->mkdir($fullPath)) {
164165
throw new NotPermittedException('Could not create folder');
165166
}
166-
$node = new Folder($this->root, $this->view, $fullPath, null, $this);
167+
$parent = dirname($fullPath) === $this->getPath() ? $this : null;
168+
$node = new Folder($this->root, $this->view, $fullPath, null, $parent);
167169
$this->sendHooks(['postWrite', 'postCreate'], [$node]);
168170
return $node;
169171
} else {

tests/lib/Files/Node/FolderTest.php

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OC\Files\FileInfo;
1515
use OC\Files\Mount\Manager;
1616
use OC\Files\Mount\MountPoint;
17+
use OC\Files\Node\File;
1718
use OC\Files\Node\Folder;
1819
use OC\Files\Node\Node;
1920
use OC\Files\Node\Root;
@@ -105,11 +106,13 @@ public function testGet() {
105106
->method('getUser')
106107
->willReturn($this->user);
107108

109+
$node = new File($root, $view, '/bar/foo/asd');
108110
$root->method('get')
109-
->with('/bar/foo/asd');
111+
->with('/bar/foo/asd')
112+
->willReturn($node);
110113

111-
$node = new Folder($root, $view, '/bar/foo');
112-
$node->get('asd');
114+
$parentNode = new Folder($root, $view, '/bar/foo');
115+
self::assertEquals($node, $parentNode->get('asd'));
113116
}
114117

115118
public function testNodeExists() {
@@ -183,6 +186,33 @@ public function testNewFolder() {
183186
$this->assertEquals($child, $result);
184187
}
185188

189+
public function testNewFolderDeepParent() {
190+
$manager = $this->createMock(Manager::class);
191+
/**
192+
* @var \OC\Files\View | \PHPUnit\Framework\MockObject\MockObject $view
193+
*/
194+
$view = $this->createMock(View::class);
195+
$root = $this->getMockBuilder(Root::class)
196+
->setConstructorArgs([$manager, $view, $this->user, $this->userMountCache, $this->logger, $this->userManager, $this->eventDispatcher])
197+
->getMock();
198+
$root->expects($this->any())
199+
->method('getUser')
200+
->willReturn($this->user);
201+
202+
$view->method('getFileInfo')
203+
->with('/foobar')
204+
->willReturn($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL]));
205+
206+
$view->method('mkdir')
207+
->with('/foobar/asd/sdf')
208+
->willReturn(true);
209+
210+
$node = new Folder($root, $view, '/foobar');
211+
$child = new Folder($root, $view, '/foobar/asd/sdf', null, null);
212+
$result = $node->newFolder('asd/sdf');
213+
$this->assertEquals($child, $result);
214+
}
215+
186216

187217
public function testNewFolderNotPermitted() {
188218
$this->expectException(\OCP\Files\NotPermittedException::class);

0 commit comments

Comments
 (0)