2015-04-07 12:44:34 +03:00
|
|
|
|
<?php
|
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
|
*
|
2015-04-07 12:44:34 +03:00
|
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
2016-07-21 19:13:36 +03:00
|
|
|
|
* @author Robin Appelman <robin@icewind.nl>
|
2016-01-12 17:02:16 +03:00
|
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-04-07 12:44:34 +03:00
|
|
|
|
*
|
|
|
|
|
* @license AGPL-3.0
|
|
|
|
|
*
|
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
namespace OCA\Files\Tests\Command;
|
|
|
|
|
|
2016-09-23 18:21:07 +03:00
|
|
|
|
use OC\Files\View;
|
2015-04-07 12:44:34 +03:00
|
|
|
|
use OCA\Files\Command\DeleteOrphanedFiles;
|
2015-10-14 14:27:05 +03:00
|
|
|
|
use OCP\Files\StorageNotAvailableException;
|
2017-10-26 14:46:16 +03:00
|
|
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
|
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
2016-09-23 18:21:07 +03:00
|
|
|
|
use Test\TestCase;
|
2015-04-07 12:44:34 +03:00
|
|
|
|
|
2015-11-19 16:39:05 +03:00
|
|
|
|
/**
|
|
|
|
|
* Class DeleteOrphanedFilesTest
|
|
|
|
|
*
|
|
|
|
|
* @group DB
|
|
|
|
|
*
|
|
|
|
|
* @package OCA\Files\Tests\Command
|
|
|
|
|
*/
|
2016-09-23 18:21:07 +03:00
|
|
|
|
class DeleteOrphanedFilesTest extends TestCase {
|
2015-04-07 12:44:34 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var DeleteOrphanedFiles
|
|
|
|
|
*/
|
|
|
|
|
private $command;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var \OCP\IDBConnection
|
|
|
|
|
*/
|
|
|
|
|
private $connection;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var string
|
|
|
|
|
*/
|
|
|
|
|
private $user1;
|
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
|
protected function setUp(): void {
|
2015-04-07 12:44:34 +03:00
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$this->connection = \OC::$server->getDatabaseConnection();
|
|
|
|
|
|
|
|
|
|
$this->user1 = $this->getUniqueID('user1_');
|
|
|
|
|
|
|
|
|
|
$userManager = \OC::$server->getUserManager();
|
|
|
|
|
$userManager->createUser($this->user1, 'pass');
|
|
|
|
|
|
|
|
|
|
$this->command = new DeleteOrphanedFiles($this->connection);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
|
protected function tearDown(): void {
|
2015-04-07 12:44:34 +03:00
|
|
|
|
$userManager = \OC::$server->getUserManager();
|
|
|
|
|
$user1 = $userManager->get($this->user1);
|
|
|
|
|
if($user1) {
|
|
|
|
|
$user1->delete();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->logout();
|
|
|
|
|
|
|
|
|
|
parent::tearDown();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getFile($fileId) {
|
|
|
|
|
$stmt = $this->connection->executeQuery('SELECT * FROM `*PREFIX*filecache` WHERE `fileid` = ?', [$fileId]);
|
|
|
|
|
return $stmt->fetchAll();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Test clearing orphaned files
|
|
|
|
|
*/
|
|
|
|
|
public function testClearFiles() {
|
2017-10-26 14:46:16 +03:00
|
|
|
|
$input = $this->getMockBuilder(InputInterface::class)
|
2015-04-07 12:44:34 +03:00
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
2017-10-26 14:46:16 +03:00
|
|
|
|
$output = $this->getMockBuilder(OutputInterface::class)
|
2015-04-07 12:44:34 +03:00
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
$this->loginAsUser($this->user1);
|
|
|
|
|
|
2016-09-23 18:21:07 +03:00
|
|
|
|
$view = new View('/' . $this->user1 . '/');
|
2015-04-07 12:44:34 +03:00
|
|
|
|
$view->mkdir('files/test');
|
|
|
|
|
|
|
|
|
|
$fileInfo = $view->getFileInfo('files/test');
|
|
|
|
|
|
|
|
|
|
$storageId = $fileInfo->getStorage()->getId();
|
|
|
|
|
|
|
|
|
|
$this->assertCount(1, $this->getFile($fileInfo->getId()), 'Asserts that file is available');
|
|
|
|
|
|
|
|
|
|
$this->command->execute($input, $output);
|
|
|
|
|
|
|
|
|
|
$this->assertCount(1, $this->getFile($fileInfo->getId()), 'Asserts that file is still available');
|
|
|
|
|
|
|
|
|
|
$deletedRows = $this->connection->executeUpdate('DELETE FROM `*PREFIX*storages` WHERE `id` = ?', [$storageId]);
|
|
|
|
|
$this->assertNotNull($deletedRows, 'Asserts that storage got deleted');
|
|
|
|
|
$this->assertSame(1, $deletedRows, 'Asserts that storage got deleted');
|
|
|
|
|
|
|
|
|
|
// parent folder, `files`, ´test` and `welcome.txt` => 4 elements
|
|
|
|
|
$output
|
|
|
|
|
->expects($this->once())
|
|
|
|
|
->method('writeln')
|
2016-09-23 18:21:07 +03:00
|
|
|
|
->with('3 orphaned file cache entries deleted');
|
2015-04-07 12:44:34 +03:00
|
|
|
|
|
|
|
|
|
$this->command->execute($input, $output);
|
|
|
|
|
|
|
|
|
|
$this->assertCount(0, $this->getFile($fileInfo->getId()), 'Asserts that file gets cleaned up');
|
|
|
|
|
|
2015-10-14 14:27:05 +03:00
|
|
|
|
// since we deleted the storage it might throw a (valid) StorageNotAvailableException
|
|
|
|
|
try {
|
|
|
|
|
$view->unlink('files/test');
|
|
|
|
|
} catch (StorageNotAvailableException $e) {
|
|
|
|
|
}
|
2015-04-07 12:44:34 +03:00
|
|
|
|
}
|
|
|
|
|
}
|