2014-03-25 15:52:32 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2014 Vincent Petry <pvince81@owncloud.com>
|
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
2015-04-13 17:34:10 +03:00
|
|
|
|
2015-03-27 01:19:27 +03:00
|
|
|
namespace Test\Repair;
|
2014-03-25 15:52:32 +04:00
|
|
|
|
2015-04-13 17:34:10 +03:00
|
|
|
use OC\Files\Cache\Cache;
|
|
|
|
use OC\Files\Cache\Storage;
|
2016-04-22 16:35:39 +03:00
|
|
|
use OCP\Migration\IOutput;
|
|
|
|
use PHPUnit_Framework_MockObject_MockObject;
|
2015-04-13 17:34:10 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
2014-03-25 15:52:32 +04:00
|
|
|
/**
|
|
|
|
* Tests for the converting of legacy storages to home storages.
|
|
|
|
*
|
2015-11-03 03:52:41 +03:00
|
|
|
* @group DB
|
|
|
|
*
|
2014-03-25 15:52:32 +04:00
|
|
|
* @see \OC\Repair\RepairLegacyStorages
|
|
|
|
*/
|
2015-04-13 17:34:10 +03:00
|
|
|
class RepairLegacyStorages extends TestCase {
|
|
|
|
/** @var \OCP\IDBConnection */
|
2014-12-02 15:51:36 +03:00
|
|
|
private $connection;
|
2015-04-13 17:34:10 +03:00
|
|
|
/** @var \OCP\IConfig */
|
2014-12-02 15:51:36 +03:00
|
|
|
private $config;
|
2014-03-25 15:52:32 +04:00
|
|
|
private $user;
|
2015-04-13 17:34:10 +03:00
|
|
|
/** @var \OC\Repair\RepairLegacyStorages */
|
2014-03-25 15:52:32 +04:00
|
|
|
private $repair;
|
|
|
|
|
|
|
|
private $dataDir;
|
|
|
|
private $oldDataDir;
|
|
|
|
|
|
|
|
private $legacyStorageId;
|
|
|
|
private $newStorageId;
|
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
/** @var IOutput | PHPUnit_Framework_MockObject_MockObject */
|
|
|
|
private $outputMock;
|
2015-01-20 16:25:07 +03:00
|
|
|
|
2014-11-11 01:30:38 +03:00
|
|
|
protected function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
2014-03-25 15:52:32 +04:00
|
|
|
$this->config = \OC::$server->getConfig();
|
2015-04-13 17:34:10 +03:00
|
|
|
$this->connection = \OC::$server->getDatabaseConnection();
|
2014-03-25 15:52:32 +04:00
|
|
|
$this->oldDataDir = $this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data/');
|
|
|
|
|
|
|
|
$this->repair = new \OC\Repair\RepairLegacyStorages($this->config, $this->connection);
|
2015-01-20 16:25:07 +03:00
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
$this->outputMock = $this->getMockBuilder('\OCP\Migration\IOutput')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
2014-03-25 15:52:32 +04:00
|
|
|
}
|
|
|
|
|
2014-11-11 01:30:38 +03:00
|
|
|
protected function tearDown() {
|
2015-04-13 17:34:10 +03:00
|
|
|
$user = \OC::$server->getUserManager()->get($this->user);
|
|
|
|
if ($user) {
|
|
|
|
$user->delete();
|
|
|
|
}
|
2014-03-25 15:52:32 +04:00
|
|
|
|
|
|
|
$sql = 'DELETE FROM `*PREFIX*storages`';
|
|
|
|
$this->connection->executeQuery($sql);
|
|
|
|
$sql = 'DELETE FROM `*PREFIX*filecache`';
|
|
|
|
$this->connection->executeQuery($sql);
|
2015-04-13 17:34:10 +03:00
|
|
|
$this->config->setSystemValue('datadirectory', $this->oldDataDir);
|
2014-03-25 15:52:32 +04:00
|
|
|
$this->config->setAppValue('core', 'repairlegacystoragesdone', 'no');
|
2014-11-11 01:30:38 +03:00
|
|
|
|
|
|
|
parent::tearDown();
|
2014-03-25 15:52:32 +04:00
|
|
|
}
|
|
|
|
|
2015-04-13 17:34:10 +03:00
|
|
|
/**
|
|
|
|
* @param string $dataDir
|
|
|
|
* @param string $userId
|
|
|
|
* @throws \Exception
|
|
|
|
*/
|
2014-03-25 15:52:32 +04:00
|
|
|
function prepareSettings($dataDir, $userId) {
|
|
|
|
// hard-coded string as we want a predictable fixed length
|
|
|
|
// no data will be written there
|
|
|
|
$this->dataDir = $dataDir;
|
2015-04-13 17:34:10 +03:00
|
|
|
$this->config->setSystemValue('datadirectory', $this->dataDir);
|
2014-03-25 15:52:32 +04:00
|
|
|
|
|
|
|
$this->user = $userId;
|
|
|
|
$this->legacyStorageId = 'local::' . $this->dataDir . $this->user . '/';
|
|
|
|
$this->newStorageId = 'home::' . $this->user;
|
2015-04-13 17:34:10 +03:00
|
|
|
\OC::$server->getUserManager()->createUser($this->user, $this->user);
|
2014-03-25 15:52:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a storage entry
|
|
|
|
*
|
|
|
|
* @param string $storageId
|
2015-04-13 17:34:10 +03:00
|
|
|
* @return int
|
2014-03-25 15:52:32 +04:00
|
|
|
*/
|
|
|
|
private function createStorage($storageId) {
|
|
|
|
$sql = 'INSERT INTO `*PREFIX*storages` (`id`)'
|
|
|
|
. ' VALUES (?)';
|
|
|
|
|
2015-04-13 17:34:10 +03:00
|
|
|
$storageId = Storage::adjustStorageId($storageId);
|
2014-03-25 15:52:32 +04:00
|
|
|
$numRows = $this->connection->executeUpdate($sql, array($storageId));
|
|
|
|
$this->assertEquals(1, $numRows);
|
|
|
|
|
2016-01-07 12:22:30 +03:00
|
|
|
return \OC::$server->getDatabaseConnection()->lastInsertId('*PREFIX*storages');
|
2014-03-25 15:52:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the storage id based on the numeric id
|
|
|
|
*
|
2015-04-13 17:34:10 +03:00
|
|
|
* @param int $storageId numeric id of the storage
|
2014-03-25 15:52:32 +04:00
|
|
|
* @return string storage id or null if not found
|
|
|
|
*/
|
|
|
|
private function getStorageId($storageId) {
|
2015-04-13 17:34:10 +03:00
|
|
|
$numericId = Storage::getNumericStorageId($storageId);
|
2014-03-25 15:52:32 +04:00
|
|
|
if (!is_null($numericId)) {
|
|
|
|
return (int)$numericId;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create dummy data in the filecache for the given storage numeric id
|
|
|
|
*
|
|
|
|
* @param string $storageId storage id
|
|
|
|
*/
|
|
|
|
private function createData($storageId) {
|
2015-04-13 17:34:10 +03:00
|
|
|
$cache = new Cache($storageId);
|
2014-03-25 15:52:32 +04:00
|
|
|
$cache->put(
|
|
|
|
'dummyfile.txt',
|
|
|
|
array('size' => 5, 'mtime' => 12, 'mimetype' => 'text/plain')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that existing home storages are left alone when valid.
|
2015-04-13 17:34:10 +03:00
|
|
|
*
|
2014-03-25 15:52:32 +04:00
|
|
|
* @dataProvider settingsProvider
|
2015-04-13 17:34:10 +03:00
|
|
|
*
|
|
|
|
* @param string $dataDir
|
|
|
|
* @param string $userId
|
2014-03-25 15:52:32 +04:00
|
|
|
*/
|
|
|
|
public function testNoopWithExistingHomeStorage($dataDir, $userId) {
|
|
|
|
$this->prepareSettings($dataDir, $userId);
|
|
|
|
$newStorageNumId = $this->createStorage($this->newStorageId);
|
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
$this->repair->run($this->outputMock);
|
2014-03-25 15:52:32 +04:00
|
|
|
|
|
|
|
$this->assertNull($this->getStorageId($this->legacyStorageId));
|
|
|
|
$this->assertEquals($newStorageNumId, $this->getStorageId($this->newStorageId));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that legacy storages are converted to home storages when
|
|
|
|
* the latter does not exist.
|
2015-04-13 17:34:10 +03:00
|
|
|
*
|
2014-03-25 15:52:32 +04:00
|
|
|
* @dataProvider settingsProvider
|
2015-04-13 17:34:10 +03:00
|
|
|
*
|
|
|
|
* @param string $dataDir
|
|
|
|
* @param string $userId
|
2014-03-25 15:52:32 +04:00
|
|
|
*/
|
|
|
|
public function testConvertLegacyToHomeStorage($dataDir, $userId) {
|
|
|
|
$this->prepareSettings($dataDir, $userId);
|
|
|
|
$legacyStorageNumId = $this->createStorage($this->legacyStorageId);
|
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
$this->repair->run($this->outputMock);
|
2014-03-25 15:52:32 +04:00
|
|
|
|
|
|
|
$this->assertNull($this->getStorageId($this->legacyStorageId));
|
|
|
|
$this->assertEquals($legacyStorageNumId, $this->getStorageId($this->newStorageId));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that legacy storages are converted to home storages
|
|
|
|
* when home storage already exists but has no data.
|
2015-04-13 17:34:10 +03:00
|
|
|
*
|
2014-03-25 15:52:32 +04:00
|
|
|
* @dataProvider settingsProvider
|
2015-04-13 17:34:10 +03:00
|
|
|
*
|
|
|
|
* @param string $dataDir
|
|
|
|
* @param string $userId
|
2014-03-25 15:52:32 +04:00
|
|
|
*/
|
|
|
|
public function testConvertLegacyToExistingEmptyHomeStorage($dataDir, $userId) {
|
|
|
|
$this->prepareSettings($dataDir, $userId);
|
|
|
|
$legacyStorageNumId = $this->createStorage($this->legacyStorageId);
|
2015-04-13 17:34:10 +03:00
|
|
|
$this->createStorage($this->newStorageId);
|
2014-03-25 15:52:32 +04:00
|
|
|
|
|
|
|
$this->createData($this->legacyStorageId);
|
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
$this->repair->run($this->outputMock);
|
2014-03-25 15:52:32 +04:00
|
|
|
|
|
|
|
$this->assertNull($this->getStorageId($this->legacyStorageId));
|
|
|
|
$this->assertEquals($legacyStorageNumId, $this->getStorageId($this->newStorageId));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that legacy storages are converted to home storages
|
|
|
|
* when home storage already exists and the legacy storage
|
|
|
|
* has no data.
|
2015-04-13 17:34:10 +03:00
|
|
|
*
|
2014-03-25 15:52:32 +04:00
|
|
|
* @dataProvider settingsProvider
|
2015-04-13 17:34:10 +03:00
|
|
|
*
|
|
|
|
* @param string $dataDir
|
|
|
|
* @param string $userId
|
2014-03-25 15:52:32 +04:00
|
|
|
*/
|
|
|
|
public function testConvertEmptyLegacyToHomeStorage($dataDir, $userId) {
|
|
|
|
$this->prepareSettings($dataDir, $userId);
|
2015-04-13 17:34:10 +03:00
|
|
|
$this->createStorage($this->legacyStorageId);
|
2014-03-25 15:52:32 +04:00
|
|
|
$newStorageNumId = $this->createStorage($this->newStorageId);
|
|
|
|
|
|
|
|
$this->createData($this->newStorageId);
|
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
$this->repair->run($this->outputMock);
|
2014-03-25 15:52:32 +04:00
|
|
|
|
|
|
|
$this->assertNull($this->getStorageId($this->legacyStorageId));
|
|
|
|
$this->assertEquals($newStorageNumId, $this->getStorageId($this->newStorageId));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that nothing is done when both conflicting legacy
|
|
|
|
* and home storage have data.
|
2015-04-13 17:34:10 +03:00
|
|
|
*
|
2014-03-25 15:52:32 +04:00
|
|
|
* @dataProvider settingsProvider
|
2015-04-13 17:34:10 +03:00
|
|
|
*
|
|
|
|
* @param string $dataDir
|
|
|
|
* @param string $userId
|
2014-03-25 15:52:32 +04:00
|
|
|
*/
|
|
|
|
public function testConflictNoop($dataDir, $userId) {
|
|
|
|
$this->prepareSettings($dataDir, $userId);
|
|
|
|
$legacyStorageNumId = $this->createStorage($this->legacyStorageId);
|
|
|
|
$newStorageNumId = $this->createStorage($this->newStorageId);
|
|
|
|
|
|
|
|
$this->createData($this->legacyStorageId);
|
|
|
|
$this->createData($this->newStorageId);
|
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
$this->outputMock->expects($this->exactly(2))->method('warning');
|
|
|
|
$this->repair->run($this->outputMock);
|
2014-03-25 15:52:32 +04:00
|
|
|
|
|
|
|
// storages left alone
|
|
|
|
$this->assertEquals($legacyStorageNumId, $this->getStorageId($this->legacyStorageId));
|
|
|
|
$this->assertEquals($newStorageNumId, $this->getStorageId($this->newStorageId));
|
|
|
|
|
2015-02-27 15:16:38 +03:00
|
|
|
// do not set the done flag
|
|
|
|
$this->assertNotEquals('yes', $this->config->getAppValue('core', 'repairlegacystoragesdone'));
|
2014-03-25 15:52:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the data dir local entry is left alone
|
2015-04-13 17:34:10 +03:00
|
|
|
*
|
2014-03-25 15:52:32 +04:00
|
|
|
* @dataProvider settingsProvider
|
2015-04-13 17:34:10 +03:00
|
|
|
*
|
|
|
|
* @param string $dataDir
|
|
|
|
* @param string $userId
|
2014-03-25 15:52:32 +04:00
|
|
|
*/
|
|
|
|
public function testDataDirEntryNoop($dataDir, $userId) {
|
|
|
|
$this->prepareSettings($dataDir, $userId);
|
|
|
|
$storageId = 'local::' . $this->dataDir;
|
|
|
|
$numId = $this->createStorage($storageId);
|
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
$this->repair->run($this->outputMock);
|
2014-03-25 15:52:32 +04:00
|
|
|
|
|
|
|
$this->assertEquals($numId, $this->getStorageId($storageId));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that external local storages are left alone
|
2015-04-13 17:34:10 +03:00
|
|
|
*
|
2014-03-25 15:52:32 +04:00
|
|
|
* @dataProvider settingsProvider
|
2015-04-13 17:34:10 +03:00
|
|
|
*
|
|
|
|
* @param string $dataDir
|
|
|
|
* @param string $userId
|
2014-03-25 15:52:32 +04:00
|
|
|
*/
|
|
|
|
public function testLocalExtStorageNoop($dataDir, $userId) {
|
|
|
|
$this->prepareSettings($dataDir, $userId);
|
|
|
|
$storageId = 'local::/tmp/somedir/' . $this->user;
|
|
|
|
$numId = $this->createStorage($storageId);
|
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
$this->repair->run($this->outputMock);
|
2014-03-25 15:52:32 +04:00
|
|
|
|
|
|
|
$this->assertEquals($numId, $this->getStorageId($storageId));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that other external storages are left alone
|
2015-04-13 17:34:10 +03:00
|
|
|
*
|
2014-03-25 15:52:32 +04:00
|
|
|
* @dataProvider settingsProvider
|
2015-04-13 17:34:10 +03:00
|
|
|
*
|
|
|
|
* @param string $dataDir
|
|
|
|
* @param string $userId
|
2014-03-25 15:52:32 +04:00
|
|
|
*/
|
|
|
|
public function testExtStorageNoop($dataDir, $userId) {
|
|
|
|
$this->prepareSettings($dataDir, $userId);
|
|
|
|
$storageId = 'smb::user@password/tmp/somedir/' . $this->user;
|
|
|
|
$numId = $this->createStorage($storageId);
|
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
$this->repair->run($this->outputMock);
|
2014-03-25 15:52:32 +04:00
|
|
|
|
|
|
|
$this->assertEquals($numId, $this->getStorageId($storageId));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Provides data dir and user name
|
|
|
|
*/
|
|
|
|
function settingsProvider() {
|
|
|
|
return array(
|
|
|
|
// regular data dir
|
|
|
|
array(
|
|
|
|
'/tmp/oc-autotest/datadir/',
|
2014-12-02 15:51:36 +03:00
|
|
|
$this->getUniqueID('user_'),
|
2014-03-25 15:52:32 +04:00
|
|
|
),
|
|
|
|
// long datadir / short user
|
|
|
|
array(
|
|
|
|
'/tmp/oc-autotest/datadir01234567890123456789012345678901234567890123456789END/',
|
2014-12-02 15:51:36 +03:00
|
|
|
$this->getUniqueID('user_'),
|
2014-03-25 15:52:32 +04:00
|
|
|
),
|
|
|
|
// short datadir / long user
|
|
|
|
array(
|
|
|
|
'/tmp/oc-autotest/datadir/',
|
|
|
|
'u123456789012345678901234567890123456789012345678901234567890END', // 64 chars
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Only run the repair once
|
|
|
|
*/
|
|
|
|
public function testOnlyRunOnce() {
|
2016-04-22 16:35:39 +03:00
|
|
|
$this->outputMock->expects($this->exactly(1))->method('info');
|
2014-03-25 15:52:32 +04:00
|
|
|
|
2014-12-02 15:51:36 +03:00
|
|
|
$this->prepareSettings('/tmp/oc-autotest/datadir', $this->getUniqueID('user_'));
|
2014-03-25 15:52:32 +04:00
|
|
|
$this->assertNotEquals('yes', $this->config->getAppValue('core', 'repairlegacystoragesdone'));
|
2016-04-22 16:35:39 +03:00
|
|
|
$this->repair->run($this->outputMock);
|
2014-03-25 15:52:32 +04:00
|
|
|
$this->assertEquals('yes', $this->config->getAppValue('core', 'repairlegacystoragesdone'));
|
|
|
|
|
2016-04-22 16:35:39 +03:00
|
|
|
$this->outputMock->expects($this->never())->method('info');
|
|
|
|
$this->repair->run($this->outputMock);
|
2014-03-25 15:52:32 +04:00
|
|
|
$this->assertEquals('yes', $this->config->getAppValue('core', 'repairlegacystoragesdone'));
|
|
|
|
}
|
|
|
|
}
|