2020-01-06 17:42:28 +01:00
|
|
|
<?php
|
2020-02-09 15:45:36 +01:00
|
|
|
/**
|
2021-03-29 08:40:20 +02:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 15:45:36 +01:00
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
2020-01-06 17:42:28 +01:00
|
|
|
|
|
|
|
namespace Friendica\Test\src\Core;
|
|
|
|
|
|
|
|
use Dice\Dice;
|
2020-01-19 21:29:36 +01:00
|
|
|
use Friendica\Core\Config\IConfig;
|
|
|
|
use Friendica\Core\Config\PreloadConfig;
|
2020-01-08 22:51:37 +01:00
|
|
|
use Friendica\Core\Hook;
|
2020-01-18 20:59:39 +01:00
|
|
|
use Friendica\Core\L10n;
|
2020-01-06 17:42:28 +01:00
|
|
|
use Friendica\Core\Session\ISession;
|
|
|
|
use Friendica\Core\StorageManager;
|
|
|
|
use Friendica\Database\Database;
|
2020-01-08 22:51:37 +01:00
|
|
|
use Friendica\DI;
|
2020-01-06 17:42:28 +01:00
|
|
|
use Friendica\Factory\ConfigFactory;
|
|
|
|
use Friendica\Model\Config\Config;
|
|
|
|
use Friendica\Model\Storage;
|
|
|
|
use Friendica\Core\Session;
|
2021-06-28 04:39:02 +02:00
|
|
|
use Friendica\Network\HTTPRequest;
|
2020-01-06 17:42:28 +01:00
|
|
|
use Friendica\Test\DatabaseTest;
|
|
|
|
use Friendica\Test\Util\Database\StaticDatabase;
|
|
|
|
use Friendica\Test\Util\VFSTrait;
|
|
|
|
use Friendica\Util\ConfigFileLoader;
|
|
|
|
use Friendica\Util\Profiler;
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Psr\Log\NullLogger;
|
|
|
|
use Friendica\Test\Util\SampleStorageBackend;
|
|
|
|
|
|
|
|
class StorageManagerTest extends DatabaseTest
|
|
|
|
{
|
2021-08-10 23:56:30 +02:00
|
|
|
use VFSTrait;
|
2020-01-06 17:42:28 +01:00
|
|
|
/** @var Database */
|
|
|
|
private $dba;
|
2020-01-19 21:29:36 +01:00
|
|
|
/** @var IConfig */
|
2020-01-06 17:42:28 +01:00
|
|
|
private $config;
|
|
|
|
/** @var LoggerInterface */
|
|
|
|
private $logger;
|
2020-01-08 22:51:37 +01:00
|
|
|
/** @var L10n */
|
|
|
|
private $l10n;
|
2021-06-28 04:39:02 +02:00
|
|
|
/** @var HTTPRequest */
|
|
|
|
private $httpRequest;
|
2020-01-06 17:42:28 +01:00
|
|
|
|
2021-04-01 23:04:30 +02:00
|
|
|
protected function setUp(): void
|
2020-01-06 17:42:28 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->setUpVfsDir();
|
|
|
|
|
|
|
|
$this->logger = new NullLogger();
|
|
|
|
|
|
|
|
$profiler = \Mockery::mock(Profiler::class);
|
2021-07-27 08:22:37 +02:00
|
|
|
$profiler->shouldReceive('startRecording');
|
|
|
|
$profiler->shouldReceive('stopRecording');
|
2020-01-06 17:42:28 +01:00
|
|
|
$profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
|
|
|
|
|
|
|
|
// load real config to avoid mocking every config-entry which is related to the Database class
|
|
|
|
$configFactory = new ConfigFactory();
|
|
|
|
$loader = new ConfigFileLoader($this->root->url());
|
|
|
|
$configCache = $configFactory->createCache($loader);
|
|
|
|
|
|
|
|
$this->dba = new StaticDatabase($configCache, $profiler, $this->logger);
|
|
|
|
|
|
|
|
$configModel = new Config($this->dba);
|
2020-01-19 21:29:36 +01:00
|
|
|
$this->config = new PreloadConfig($configCache, $configModel);
|
2021-08-10 23:56:30 +02:00
|
|
|
$this->config->set('storage', 'name', 'Database');
|
2020-01-08 22:51:37 +01:00
|
|
|
|
|
|
|
$this->l10n = \Mockery::mock(L10n::class);
|
2021-06-28 04:39:02 +02:00
|
|
|
|
|
|
|
$this->httpRequest = \Mockery::mock(HTTPRequest::class);
|
2020-01-06 17:42:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test plain instancing first
|
|
|
|
*/
|
|
|
|
public function testInstance()
|
|
|
|
{
|
2021-06-28 04:39:02 +02:00
|
|
|
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
|
2020-01-06 17:42:28 +01:00
|
|
|
|
2020-10-17 14:19:57 +02:00
|
|
|
self::assertInstanceOf(StorageManager::class, $storageManager);
|
2020-01-06 17:42:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function dataStorages()
|
|
|
|
{
|
|
|
|
return [
|
2021-08-10 23:56:30 +02:00
|
|
|
'empty' => [
|
|
|
|
'name' => '',
|
|
|
|
'valid' => false,
|
|
|
|
'interface' => Storage\IStorage::class,
|
|
|
|
'assert' => null,
|
|
|
|
'assertName' => '',
|
2020-01-06 17:42:28 +01:00
|
|
|
],
|
2021-08-10 23:56:30 +02:00
|
|
|
'database' => [
|
|
|
|
'name' => Storage\Database::NAME,
|
|
|
|
'valid' => true,
|
|
|
|
'interface' => Storage\IWritableStorage::class,
|
|
|
|
'assert' => Storage\Database::class,
|
|
|
|
'assertName' => Storage\Database::NAME,
|
2020-01-06 17:42:28 +01:00
|
|
|
],
|
2021-08-10 23:56:30 +02:00
|
|
|
'filesystem' => [
|
|
|
|
'name' => Storage\Filesystem::NAME,
|
|
|
|
'valid' => true,
|
|
|
|
'interface' => Storage\IWritableStorage::class,
|
|
|
|
'assert' => Storage\Filesystem::class,
|
|
|
|
'assertName' => Storage\Filesystem::NAME,
|
2020-01-06 17:42:28 +01:00
|
|
|
],
|
|
|
|
'systemresource' => [
|
2021-08-10 23:56:30 +02:00
|
|
|
'name' => Storage\SystemResource::NAME,
|
|
|
|
'valid' => true,
|
|
|
|
'interface' => Storage\IStorage::class,
|
|
|
|
'assert' => Storage\SystemResource::class,
|
|
|
|
'assertName' => Storage\SystemResource::NAME,
|
2020-01-06 17:42:28 +01:00
|
|
|
],
|
2021-08-10 23:56:30 +02:00
|
|
|
'invalid' => [
|
2020-01-06 17:42:28 +01:00
|
|
|
'name' => 'invalid',
|
2021-08-10 23:56:30 +02:00
|
|
|
'valid' => false,
|
|
|
|
'interface' => null,
|
2020-01-06 17:42:28 +01:00
|
|
|
'assert' => null,
|
|
|
|
'assertName' => '',
|
|
|
|
'userBackend' => false,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the getByName() method
|
|
|
|
*
|
|
|
|
* @dataProvider dataStorages
|
|
|
|
*/
|
2021-08-10 23:56:30 +02:00
|
|
|
public function testGetByName($name, $valid, $interface, $assert, $assertName)
|
2020-01-06 17:42:28 +01:00
|
|
|
{
|
2021-08-10 23:56:30 +02:00
|
|
|
if (!$valid) {
|
|
|
|
$this->expectException(Storage\InvalidClassStorageException::class);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($interface === Storage\IWritableStorage::class) {
|
|
|
|
$this->config->set('storage', 'name', $name);
|
|
|
|
}
|
2020-01-06 17:42:28 +01:00
|
|
|
|
2021-08-10 23:56:30 +02:00
|
|
|
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
|
|
|
|
|
|
|
|
if ($interface === Storage\IWritableStorage::class) {
|
2021-08-10 22:07:52 +02:00
|
|
|
$storage = $storageManager->getWritableStorageByName($name);
|
2021-08-01 14:31:57 +02:00
|
|
|
} else {
|
|
|
|
$storage = $storageManager->getByName($name);
|
|
|
|
}
|
2020-01-06 17:42:28 +01:00
|
|
|
|
2021-08-10 23:56:30 +02:00
|
|
|
self::assertInstanceOf($interface, $storage);
|
|
|
|
self::assertInstanceOf($assert, $storage);
|
2020-10-17 14:19:57 +02:00
|
|
|
self::assertEquals($assertName, $storage);
|
2020-01-06 17:42:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the isValidBackend() method
|
|
|
|
*
|
|
|
|
* @dataProvider dataStorages
|
|
|
|
*/
|
2021-08-10 23:56:30 +02:00
|
|
|
public function testIsValidBackend($name, $valid, $interface, $assert, $assertName)
|
2020-01-06 17:42:28 +01:00
|
|
|
{
|
2021-08-01 14:31:57 +02:00
|
|
|
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
|
2020-01-06 17:42:28 +01:00
|
|
|
|
2020-01-17 19:31:34 +01:00
|
|
|
// true in every of the backends
|
2020-10-17 14:19:57 +02:00
|
|
|
self::assertEquals(!empty($assertName), $storageManager->isValidBackend($name));
|
2020-01-17 19:31:34 +01:00
|
|
|
|
2021-08-10 23:56:30 +02:00
|
|
|
// if it's a IWritableStorage, the valid backend should return true, otherwise false
|
|
|
|
self::assertEquals($interface === Storage\IWritableStorage::class, $storageManager->isValidBackend($name, StorageManager::DEFAULT_BACKENDS));
|
2020-01-06 17:42:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the method listBackends() with default setting
|
|
|
|
*/
|
|
|
|
public function testListBackends()
|
|
|
|
{
|
2021-08-01 14:31:57 +02:00
|
|
|
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
|
2020-01-06 17:42:28 +01:00
|
|
|
|
2020-10-17 14:19:57 +02:00
|
|
|
self::assertEquals(StorageManager::DEFAULT_BACKENDS, $storageManager->listBackends());
|
2020-01-06 17:42:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the method getBackend()
|
|
|
|
*
|
|
|
|
* @dataProvider dataStorages
|
|
|
|
*/
|
2021-08-10 23:56:30 +02:00
|
|
|
public function testGetBackend($name, $valid, $interface, $assert, $assertName)
|
2020-01-06 17:42:28 +01:00
|
|
|
{
|
2021-08-10 23:56:30 +02:00
|
|
|
if ($interface !== Storage\IWritableStorage::class) {
|
|
|
|
static::markTestSkipped('only works for IWritableStorage');
|
|
|
|
}
|
2020-01-06 17:42:28 +01:00
|
|
|
|
2021-08-10 23:56:30 +02:00
|
|
|
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
|
2020-01-06 17:42:28 +01:00
|
|
|
|
2021-08-10 23:56:30 +02:00
|
|
|
$selBackend = $storageManager->getWritableStorageByName($name);
|
|
|
|
$storageManager->setBackend($selBackend);
|
2020-01-06 17:42:28 +01:00
|
|
|
|
2021-08-10 23:56:30 +02:00
|
|
|
self::assertInstanceOf($assert, $storageManager->getBackend());
|
2020-01-06 17:42:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the method getBackend() with a pre-configured backend
|
|
|
|
*
|
|
|
|
* @dataProvider dataStorages
|
|
|
|
*/
|
2021-08-10 23:56:30 +02:00
|
|
|
public function testPresetBackend($name, $valid, $interface, $assert, $assertName)
|
2020-01-06 17:42:28 +01:00
|
|
|
{
|
|
|
|
$this->config->set('storage', 'name', $name);
|
2021-08-10 23:56:30 +02:00
|
|
|
if ($interface !== Storage\IWritableStorage::class) {
|
|
|
|
$this->expectException(Storage\InvalidClassStorageException::class);
|
|
|
|
}
|
2020-01-06 17:42:28 +01:00
|
|
|
|
2021-08-01 14:31:57 +02:00
|
|
|
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
|
2020-01-06 17:42:28 +01:00
|
|
|
|
2021-08-10 23:56:30 +02:00
|
|
|
self::assertInstanceOf($assert, $storageManager->getBackend());
|
2020-01-06 17:42:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Tests the register and unregister methods for a new backend storage class
|
|
|
|
*
|
|
|
|
* Uses a sample storage for testing
|
|
|
|
*
|
|
|
|
* @see SampleStorageBackend
|
|
|
|
*/
|
|
|
|
public function testRegisterUnregisterBackends()
|
|
|
|
{
|
2020-01-08 22:51:37 +01:00
|
|
|
/// @todo Remove dice once "Hook" is dynamic and mockable
|
2021-08-10 23:56:30 +02:00
|
|
|
$dice = (new Dice())
|
2020-01-08 22:51:37 +01:00
|
|
|
->addRules(include __DIR__ . '/../../../static/dependencies.config.php')
|
|
|
|
->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
|
|
|
|
->addRule(ISession::class, ['instanceOf' => Session\Memory::class, 'shared' => true, 'call' => null]);
|
|
|
|
DI::init($dice);
|
|
|
|
|
2021-08-01 14:31:57 +02:00
|
|
|
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
|
2020-01-06 17:42:28 +01:00
|
|
|
|
2020-10-17 14:19:57 +02:00
|
|
|
self::assertTrue($storageManager->register(SampleStorageBackend::class));
|
2020-01-06 17:42:28 +01:00
|
|
|
|
2020-10-17 14:19:57 +02:00
|
|
|
self::assertEquals(array_merge(StorageManager::DEFAULT_BACKENDS, [
|
2021-08-16 00:08:06 +02:00
|
|
|
SampleStorageBackend::getName(),
|
2020-01-06 17:42:28 +01:00
|
|
|
]), $storageManager->listBackends());
|
2020-10-17 14:19:57 +02:00
|
|
|
self::assertEquals(array_merge(StorageManager::DEFAULT_BACKENDS, [
|
2021-08-16 00:08:06 +02:00
|
|
|
SampleStorageBackend::getName()
|
|
|
|
]), $this->config->get('storage', 'backends'));
|
|
|
|
|
|
|
|
self::assertTrue($storageManager->unregister(SampleStorageBackend::class));
|
|
|
|
self::assertEquals(StorageManager::DEFAULT_BACKENDS, $this->config->get('storage', 'backends'));
|
|
|
|
self::assertEquals(StorageManager::DEFAULT_BACKENDS, $storageManager->listBackends());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* tests that an active backend cannot get unregistered
|
|
|
|
*/
|
|
|
|
public function testUnregisterActiveBackend()
|
|
|
|
{
|
|
|
|
/// @todo Remove dice once "Hook" is dynamic and mockable
|
|
|
|
$dice = (new Dice())
|
|
|
|
->addRules(include __DIR__ . '/../../../static/dependencies.config.php')
|
|
|
|
->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
|
|
|
|
->addRule(ISession::class, ['instanceOf' => Session\Memory::class, 'shared' => true, 'call' => null]);
|
|
|
|
DI::init($dice);
|
|
|
|
|
|
|
|
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
|
|
|
|
|
|
|
|
self::assertTrue($storageManager->register(SampleStorageBackend::class));
|
|
|
|
|
|
|
|
self::assertEquals(array_merge(StorageManager::DEFAULT_BACKENDS, [
|
|
|
|
SampleStorageBackend::getName(),
|
|
|
|
]), $storageManager->listBackends());
|
|
|
|
self::assertEquals(array_merge(StorageManager::DEFAULT_BACKENDS, [
|
|
|
|
SampleStorageBackend::getName()
|
2020-01-06 17:42:28 +01:00
|
|
|
]), $this->config->get('storage', 'backends'));
|
|
|
|
|
2020-01-08 22:51:37 +01:00
|
|
|
// inline call to register own class as hook (testing purpose only)
|
|
|
|
SampleStorageBackend::registerHook();
|
|
|
|
Hook::loadHooks();
|
|
|
|
|
2021-08-10 23:56:30 +02:00
|
|
|
self::assertTrue($storageManager->setBackend($storageManager->getWritableStorageByName(SampleStorageBackend::NAME)));
|
2020-10-17 14:19:57 +02:00
|
|
|
self::assertEquals(SampleStorageBackend::NAME, $this->config->get('storage', 'name'));
|
2020-01-06 17:42:28 +01:00
|
|
|
|
2020-10-17 14:19:57 +02:00
|
|
|
self::assertInstanceOf(SampleStorageBackend::class, $storageManager->getBackend());
|
2020-01-06 17:42:28 +01:00
|
|
|
|
2021-08-16 00:08:06 +02:00
|
|
|
self::expectException(Storage\StorageException::class);
|
|
|
|
self::expectExceptionMessage('Cannot unregister Sample Storage, because it\'s currently active.');
|
2020-01-06 17:42:28 +01:00
|
|
|
|
2021-08-16 00:08:06 +02:00
|
|
|
$storageManager->unregister(SampleStorageBackend::class);
|
2020-01-06 17:42:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test moving data to a new storage (currently testing db & filesystem)
|
|
|
|
*
|
|
|
|
* @dataProvider dataStorages
|
|
|
|
*/
|
2021-08-10 23:56:30 +02:00
|
|
|
public function testMoveStorage($name, $valid, $interface, $assert, $assertName)
|
2020-01-06 17:42:28 +01:00
|
|
|
{
|
2021-08-10 23:56:30 +02:00
|
|
|
if ($interface !== Storage\IWritableStorage::class) {
|
2021-04-01 23:04:30 +02:00
|
|
|
self::markTestSkipped("No user backend");
|
2020-01-06 17:42:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->loadFixture(__DIR__ . '/../../datasets/storage/database.fixture.php', $this->dba);
|
|
|
|
|
2021-08-01 14:31:57 +02:00
|
|
|
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
|
2021-08-10 23:56:30 +02:00
|
|
|
$storage = $storageManager->getWritableStorageByName($name);
|
2020-01-06 17:42:28 +01:00
|
|
|
$storageManager->move($storage);
|
|
|
|
|
|
|
|
$photos = $this->dba->select('photo', ['backend-ref', 'backend-class', 'id', 'data']);
|
|
|
|
|
|
|
|
while ($photo = $this->dba->fetch($photos)) {
|
2020-10-17 14:19:57 +02:00
|
|
|
self::assertEmpty($photo['data']);
|
2020-01-06 17:42:28 +01:00
|
|
|
|
|
|
|
$storage = $storageManager->getByName($photo['backend-class']);
|
2021-08-10 23:56:30 +02:00
|
|
|
$data = $storage->get($photo['backend-ref']);
|
2020-01-06 17:42:28 +01:00
|
|
|
|
2020-10-17 14:19:57 +02:00
|
|
|
self::assertNotEmpty($data);
|
2020-01-06 17:42:28 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-17 21:01:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test moving data to a WRONG storage
|
|
|
|
*/
|
2021-08-10 22:07:52 +02:00
|
|
|
public function testWrongWritableStorage()
|
2020-01-17 21:01:37 +01:00
|
|
|
{
|
2021-08-10 23:56:30 +02:00
|
|
|
$this->expectException(Storage\InvalidClassStorageException::class);
|
|
|
|
$this->expectExceptionMessage('Backend SystemResource is not valid');
|
2020-10-18 20:31:57 +02:00
|
|
|
|
2021-08-01 14:31:57 +02:00
|
|
|
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
|
2021-08-10 23:56:30 +02:00
|
|
|
$storage = $storageManager->getWritableStorageByName(Storage\SystemResource::getName());
|
2020-01-17 21:01:37 +01:00
|
|
|
$storageManager->move($storage);
|
|
|
|
}
|
2020-01-06 17:42:28 +01:00
|
|
|
}
|