Skip to content

Commit 921ee17

Browse files
committed
test: add test for trashbin when cross-storage move fails
Signed-off-by: Robin Appelman <robin@icewind.nl>
1 parent 7eae0e5 commit 921ee17

1 file changed

Lines changed: 60 additions & 0 deletions

File tree

apps/files_trashbin/tests/StorageTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,20 @@
66
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
77
* SPDX-License-Identifier: AGPL-3.0-only
88
*/
9+
910
namespace OCA\Files_Trashbin\Tests;
1011

12+
use OC\Files\Cache\Updater;
1113
use OC\Files\Filesystem;
1214
use OC\Files\Storage\Common;
15+
use OC\Files\Storage\Local;
1316
use OC\Files\Storage\Temporary;
1417
use OC\Files\View;
1518
use OCA\Files_Trashbin\AppInfo\Application;
1619
use OCA\Files_Trashbin\Events\MoveToTrashEvent;
1720
use OCA\Files_Trashbin\Storage;
1821
use OCA\Files_Trashbin\Trash\ITrashManager;
22+
use OCA\Files_Trashbin\Trashbin;
1923
use OCP\AppFramework\Bootstrap\IBootContext;
2024
use OCP\AppFramework\Utility\ITimeFactory;
2125
use OCP\Constants;
@@ -137,6 +141,62 @@ public function testTrashEntryCreatedWhenSourceNotInCache(): void {
137141
$this->assertTrue($trashStorage->getCache()->inCache($trashInternalPath));
138142
}
139143

144+
public function testTrashEntryNotCreatedWhenDeleteFailed(): void {
145+
$storage2 = $this->getMockBuilder(Temporary::class)
146+
->setConstructorArgs([])
147+
->onlyMethods(['unlink', 'instanceOfStorage'])
148+
->getMock();
149+
$storage2->method('unlink')
150+
->willReturn(false);
151+
152+
// disable same-storage move optimization
153+
$storage2->method('instanceOfStorage')
154+
->willReturnCallback(fn (string $class) => ($class !== Local::class) && (new Temporary([]))->instanceOfStorage($class));
155+
156+
157+
Filesystem::mount($storage2, [], $this->user . '/files/substorage');
158+
$this->userView->file_put_contents('substorage/test.txt', 'foo');
159+
160+
$this->assertFalse($this->userView->unlink('substorage/test.txt'));
161+
162+
$results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/');
163+
$this->assertEmpty($results);
164+
165+
$trashData = Trashbin::getExtraData($this->user);
166+
$this->assertEmpty($trashData);
167+
}
168+
169+
public function testTrashEntryNotCreatedWhenCacheRowFailed(): void {
170+
$trashStorage = $this->getMockBuilder(Temporary::class)
171+
->setConstructorArgs([])
172+
->onlyMethods(['getUpdater'])
173+
->getMock();
174+
$updater = $this->getMockBuilder(Updater::class)
175+
->setConstructorArgs([$trashStorage])
176+
->onlyMethods(['renameFromStorage'])
177+
->getMock();
178+
$trashStorage->method('getUpdater')
179+
->willReturn($updater);
180+
$updater->method('renameFromStorage')
181+
->willThrowException(new \Exception());
182+
183+
Filesystem::mount($trashStorage, [], $this->user . '/files_trashbin');
184+
$this->userView->file_put_contents('test.txt', 'foo');
185+
186+
try {
187+
$this->assertFalse($this->userView->unlink('test.txt'));
188+
$this->fail();
189+
} catch (\Exception) {
190+
// expected
191+
}
192+
193+
$results = $this->rootView->getDirectoryContent($this->user . '/files_trashbin/files/');
194+
$this->assertEmpty($results);
195+
196+
$trashData = Trashbin::getExtraData($this->user);
197+
$this->assertEmpty($trashData);
198+
}
199+
140200
/**
141201
* Test that deleting a folder puts it into the trashbin.
142202
*/

0 commit comments

Comments
 (0)