Restructure Storage to new paradigm

This commit is contained in:
Philipp 2021-10-23 12:11:38 +02:00
parent 24f8ee8e67
commit 2ab0d06996
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
29 changed files with 229 additions and 199 deletions

View file

@ -0,0 +1,70 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @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/>.
*
*/
namespace Friendica\Test\src\Core\Storage;
use Friendica\Core\Config\Factory\Config;
use Friendica\Core\Storage\Type\Database;
use Friendica\Test\DatabaseTestTrait;
use Friendica\Test\Util\Database\StaticDatabase;
use Friendica\Test\Util\VFSTrait;
use Friendica\Util\Profiler;
use Psr\Log\NullLogger;
class DatabaseStorageTest extends StorageTest
{
use DatabaseTestTrait;
use VFSTrait;
protected function setUp(): void
{
$this->setUpVfsDir();
$this->setUpDb();
parent::setUp();
}
protected function getInstance()
{
$logger = new NullLogger();
$profiler = \Mockery::mock(Profiler::class);
$profiler->shouldReceive('startRecording');
$profiler->shouldReceive('stopRecording');
$profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
// load real config to avoid mocking every config-entry which is related to the Database class
$configFactory = new Config();
$loader = (new Config())->createConfigFileLoader($this->root->url(), []);
$configCache = $configFactory->createCache($loader);
$dba = new StaticDatabase($configCache, $profiler, $logger);
return new Database($dba);
}
protected function tearDown(): void
{
$this->tearDownDb();
parent::tearDown();
}
}

View file

@ -0,0 +1,67 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @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/>.
*
*/
namespace Friendica\Test\src\Core\Storage;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\L10n;
use Friendica\Core\Storage\Capability\ICanConfigureStorage;
use Friendica\Core\Storage\Type\FilesystemConfig;
use Friendica\Test\Util\VFSTrait;
use Mockery\MockInterface;
use org\bovigo\vfs\vfsStream;
class FilesystemStorageConfigTest extends StorageConfigTest
{
use VFSTrait;
protected function setUp(): void
{
$this->setUpVfsDir();
vfsStream::create(['storage' => []], $this->root);
parent::setUp();
}
protected function getInstance()
{
/** @var MockInterface|L10n $l10n */
$l10n = \Mockery::mock(L10n::class)->makePartial();
$config = \Mockery::mock(IManageConfigValues::class);
$config->shouldReceive('get')
->with('storage', 'filesystem_path', FilesystemConfig::DEFAULT_BASE_FOLDER)
->andReturn($this->root->getChild('storage')->url());
return new FilesystemConfig($config, $l10n);
}
protected function assertOption(ICanConfigureStorage $storage)
{
self::assertEquals([
'storagepath' => [
'input', 'Storage base path',
$this->root->getChild('storage')->url(),
'Folder where uploaded files are saved. For maximum security, This should be a path outside web server folder tree'
]
], $storage->getOptions());
}
}

View file

@ -0,0 +1,114 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @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/>.
*
*/
namespace Friendica\Test\src\Core\Storage;
use Friendica\Core\Storage\Exception\StorageException;
use Friendica\Core\Storage\Type\Filesystem;
use Friendica\Core\Storage\Type\FilesystemConfig;
use Friendica\Test\Util\VFSTrait;
use org\bovigo\vfs\vfsStream;
class FilesystemStorageTest extends StorageTest
{
use VFSTrait;
protected function setUp(): void
{
$this->setUpVfsDir();
vfsStream::create(['storage' => []], $this->root);
parent::setUp();
}
protected function getInstance()
{
return new Filesystem($this->root->getChild(FilesystemConfig::DEFAULT_BASE_FOLDER)->url());
}
/**
* Test the exception in case of missing directory permissions during put new files
*/
public function testMissingDirPermissionsDuringPut()
{
$this->expectException(StorageException::class);
$this->expectExceptionMessageMatches("/Filesystem storage failed to create \".*\". Check you write permissions./");
$this->root->getChild(FilesystemConfig::DEFAULT_BASE_FOLDER)->chmod(0777);
$instance = $this->getInstance();
$this->root->getChild(FilesystemConfig::DEFAULT_BASE_FOLDER)->chmod(0000);
$instance->put('test');
}
/**
* Test the exception in case the directory isn't writeable
*/
public function testMissingDirPermissions()
{
$this->expectException(StorageException::class);
$this->expectExceptionMessageMatches("/Path \".*\" does not exist or is not writeable./");
$this->root->getChild(FilesystemConfig::DEFAULT_BASE_FOLDER)->chmod(0000);
$this->getInstance();
}
/**
* Test the exception in case of missing file permissions
*
*/
public function testMissingFilePermissions()
{
static::markTestIncomplete("Cannot catch file_put_content() error due vfsStream failure");
$this->expectException(StorageException::class);
$this->expectExceptionMessageMatches("/Filesystem storage failed to save data to \".*\". Check your write permissions/");
vfsStream::create(['storage' => ['f0' => ['c0' => ['k0i0' => '']]]], $this->root);
$this->root->getChild('storage/f0/c0/k0i0')->chmod(000);
$instance = $this->getInstance();
$instance->put('test', 'f0c0k0i0');
}
/**
* Test the backend storage of the Filesystem Storage class
*/
public function testDirectoryTree()
{
$instance = $this->getInstance();
$instance->put('test', 'f0c0d0i0');
$dir = $this->root->getChild('storage/f0/c0')->url();
$file = $this->root->getChild('storage/f0/c0/d0i0')->url();
self::assertDirectoryExists($dir);
self::assertFileExists($file);
self::assertDirectoryIsWritable($dir);
self::assertFileIsWritable($file);
self::assertEquals('test', file_get_contents($file));
}
}

View file

@ -19,7 +19,7 @@
*
*/
namespace Friendica\Test\src\Core;
namespace Friendica\Test\src\Core\Storage\Repository;
use Dice\Dice;
use Friendica\Core\Config\Capability\IManageConfigValues;
@ -28,12 +28,17 @@ use Friendica\Core\Hook;
use Friendica\Core\L10n;
use Friendica\Core\Session\Capability\IHandleSessions;
use Friendica\Core\Session\Type\Memory;
use Friendica\Core\StorageManager;
use Friendica\Core\Storage\Exception\InvalidClassStorageException;
use Friendica\Core\Storage\Capability\ICanReadFromStorage;
use Friendica\Core\Storage\Capability\ICanWriteToStorage;
use Friendica\Core\Storage\Repository\StorageManager;
use Friendica\Core\Storage\Type\Filesystem;
use Friendica\Core\Storage\Type\SystemResource;
use Friendica\Database\Database;
use Friendica\DI;
use Friendica\Core\Config\Factory\Config;
use Friendica\Core\Config\Repository;
use Friendica\Model\Storage;
use Friendica\Core\Storage\Type;
use Friendica\Network\HTTPClient;
use Friendica\Test\DatabaseTest;
use Friendica\Test\Util\Database\StaticDatabase;
@ -64,7 +69,7 @@ class StorageManagerTest extends DatabaseTest
$this->setUpVfsDir();
vfsStream::newDirectory(Storage\FilesystemConfig::DEFAULT_BASE_FOLDER, 0777)->at($this->root);
vfsStream::newDirectory(Type\FilesystemConfig::DEFAULT_BASE_FOLDER, 0777)->at($this->root);
$this->logger = new NullLogger();
@ -83,7 +88,7 @@ class StorageManagerTest extends DatabaseTest
$configModel = new Repository\Config($this->dba);
$this->config = new PreloadConfig($configCache, $configModel);
$this->config->set('storage', 'name', 'Database');
$this->config->set('storage', 'filesystem_path', $this->root->getChild(Storage\FilesystemConfig::DEFAULT_BASE_FOLDER)->url());
$this->config->set('storage', 'filesystem_path', $this->root->getChild(Type\FilesystemConfig::DEFAULT_BASE_FOLDER)->url());
$this->l10n = \Mockery::mock(L10n::class);
@ -92,7 +97,7 @@ class StorageManagerTest extends DatabaseTest
protected function tearDown(): void
{
$this->root->removeChild(Storage\FilesystemConfig::DEFAULT_BASE_FOLDER);
$this->root->removeChild(Type\FilesystemConfig::DEFAULT_BASE_FOLDER);
parent::tearDown();
}
@ -113,30 +118,30 @@ class StorageManagerTest extends DatabaseTest
'empty' => [
'name' => '',
'valid' => false,
'interface' => Storage\IStorage::class,
'interface' => ICanReadFromStorage::class,
'assert' => null,
'assertName' => '',
],
'database' => [
'name' => Storage\Database::NAME,
'name' => Type\Database::NAME,
'valid' => true,
'interface' => Storage\IWritableStorage::class,
'assert' => Storage\Database::class,
'assertName' => Storage\Database::NAME,
'interface' => ICanWriteToStorage::class,
'assert' => Type\Database::class,
'assertName' => Type\Database::NAME,
],
'filesystem' => [
'name' => Storage\Filesystem::NAME,
'name' => Filesystem::NAME,
'valid' => true,
'interface' => Storage\IWritableStorage::class,
'assert' => Storage\Filesystem::class,
'assertName' => Storage\Filesystem::NAME,
'interface' => ICanWriteToStorage::class,
'assert' => Filesystem::class,
'assertName' => Filesystem::NAME,
],
'systemresource' => [
'name' => Storage\SystemResource::NAME,
'name' => SystemResource::NAME,
'valid' => true,
'interface' => Storage\IStorage::class,
'assert' => Storage\SystemResource::class,
'assertName' => Storage\SystemResource::NAME,
'interface' => ICanReadFromStorage::class,
'assert' => SystemResource::class,
'assertName' => SystemResource::NAME,
],
'invalid' => [
'name' => 'invalid',
@ -157,16 +162,16 @@ class StorageManagerTest extends DatabaseTest
public function testGetByName($name, $valid, $interface, $assert, $assertName)
{
if (!$valid) {
$this->expectException(Storage\InvalidClassStorageException::class);
$this->expectException(InvalidClassStorageException::class);
}
if ($interface === Storage\IWritableStorage::class) {
if ($interface === ICanWriteToStorage::class) {
$this->config->set('storage', 'name', $name);
}
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
if ($interface === Storage\IWritableStorage::class) {
if ($interface === ICanWriteToStorage::class) {
$storage = $storageManager->getWritableStorageByName($name);
} else {
$storage = $storageManager->getByName($name);
@ -189,8 +194,8 @@ class StorageManagerTest extends DatabaseTest
// true in every of the backends
self::assertEquals(!empty($assertName), $storageManager->isValidBackend($name));
// 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));
// if it's a ICanWriteToStorage, the valid backend should return true, otherwise false
self::assertEquals($interface === ICanWriteToStorage::class, $storageManager->isValidBackend($name, StorageManager::DEFAULT_BACKENDS));
}
/**
@ -210,8 +215,8 @@ class StorageManagerTest extends DatabaseTest
*/
public function testGetBackend($name, $valid, $interface, $assert, $assertName)
{
if ($interface !== Storage\IWritableStorage::class) {
static::markTestSkipped('only works for IWritableStorage');
if ($interface !== ICanWriteToStorage::class) {
static::markTestSkipped('only works for ICanWriteToStorage');
}
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
@ -230,8 +235,8 @@ class StorageManagerTest extends DatabaseTest
public function testPresetBackend($name, $valid, $interface, $assert, $assertName)
{
$this->config->set('storage', 'name', $name);
if ($interface !== Storage\IWritableStorage::class) {
$this->expectException(Storage\InvalidClassStorageException::class);
if ($interface !== ICanWriteToStorage::class) {
$this->expectException(InvalidClassStorageException::class);
}
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
@ -250,7 +255,7 @@ class StorageManagerTest extends DatabaseTest
{
/// @todo Remove dice once "Hook" is dynamic and mockable
$dice = (new Dice())
->addRules(include __DIR__ . '/../../../static/dependencies.config.php')
->addRules(include __DIR__ . '/../../../../../static/dependencies.config.php')
->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
->addRule(IHandleSessions::class, ['instanceOf' => Session\Type\Memory::class, 'shared' => true, 'call' => null]);
DI::init($dice);
@ -278,7 +283,7 @@ class StorageManagerTest extends DatabaseTest
{
/// @todo Remove dice once "Hook" is dynamic and mockable
$dice = (new Dice())
->addRules(include __DIR__ . '/../../../static/dependencies.config.php')
->addRules(include __DIR__ . '/../../../../../static/dependencies.config.php')
->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
->addRule(IHandleSessions::class, ['instanceOf' => Memory::class, 'shared' => true, 'call' => null]);
DI::init($dice);
@ -303,7 +308,7 @@ class StorageManagerTest extends DatabaseTest
self::assertInstanceOf(SampleStorageBackend::class, $storageManager->getBackend());
self::expectException(Storage\StorageException::class);
self::expectException(\Friendica\Core\Storage\Exception\StorageException::class);
self::expectExceptionMessage('Cannot unregister Sample Storage, because it\'s currently active.');
$storageManager->unregister(SampleStorageBackend::class);
@ -316,11 +321,11 @@ class StorageManagerTest extends DatabaseTest
*/
public function testMoveStorage($name, $valid, $interface, $assert, $assertName)
{
if ($interface !== Storage\IWritableStorage::class) {
if ($interface !== ICanWriteToStorage::class) {
self::markTestSkipped("No user backend");
}
$this->loadFixture(__DIR__ . '/../../datasets/storage/database.fixture.php', $this->dba);
$this->loadFixture(__DIR__ . '/../../../../datasets/storage/database.fixture.php', $this->dba);
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
$storage = $storageManager->getWritableStorageByName($name);
@ -343,11 +348,11 @@ class StorageManagerTest extends DatabaseTest
*/
public function testWrongWritableStorage()
{
$this->expectException(Storage\InvalidClassStorageException::class);
$this->expectException(InvalidClassStorageException::class);
$this->expectExceptionMessage('Backend SystemResource is not valid');
$storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
$storage = $storageManager->getWritableStorageByName(Storage\SystemResource::getName());
$storage = $storageManager->getWritableStorageByName(SystemResource::getName());
$storageManager->move($storage);
}
}

View file

@ -0,0 +1,43 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @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/>.
*
*/
namespace Friendica\Test\src\Core\Storage;
use Friendica\Core\Storage\Capability\ICanConfigureStorage;
use Friendica\Test\MockedTest;
abstract class StorageConfigTest extends MockedTest
{
/** @return \Friendica\Core\Storage\Capability\ICanConfigureStorage */
abstract protected function getInstance();
abstract protected function assertOption(\Friendica\Core\Storage\Capability\ICanConfigureStorage $storage);
/**
* Test if the "getOption" is asserted
*/
public function testGetOptions()
{
$instance = $this->getInstance();
$this->assertOption($instance);
}
}

View file

@ -0,0 +1,107 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @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/>.
*
*/
namespace Friendica\Test\src\Core\Storage;
use Friendica\Core\Storage\Capability\ICanReadFromStorage;
use Friendica\Core\Storage\Capability\ICanWriteToStorage;
use Friendica\Core\Storage\Exception\ReferenceStorageException;
use Friendica\Test\MockedTest;
abstract class StorageTest extends MockedTest
{
/** @return ICanWriteToStorage */
abstract protected function getInstance();
/**
* Test if the instance is "really" implementing the interface
*/
public function testInstance()
{
$instance = $this->getInstance();
self::assertInstanceOf(ICanReadFromStorage::class, $instance);
}
/**
* Test basic put, get and delete operations
*/
public function testPutGetDelete()
{
$instance = $this->getInstance();
$ref = $instance->put('data12345');
self::assertNotEmpty($ref);
self::assertEquals('data12345', $instance->get($ref));
$instance->delete($ref);
}
/**
* Test a delete with an invalid reference
*/
public function testInvalidDelete()
{
self::expectException(ReferenceStorageException::class);
$instance = $this->getInstance();
$instance->delete(-1234456);
}
/**
* Test a get with an invalid reference
*/
public function testInvalidGet()
{
self::expectException(ReferenceStorageException::class);
$instance = $this->getInstance();
$instance->get(-123456);
}
/**
* Test an update with a given reference
*/
public function testUpdateReference()
{
$instance = $this->getInstance();
$ref = $instance->put('data12345');
self::assertNotEmpty($ref);
self::assertEquals('data12345', $instance->get($ref));
self::assertEquals($ref, $instance->put('data5432', $ref));
self::assertEquals('data5432', $instance->get($ref));
}
/**
* Test that an invalid update results in an insert
*/
public function testInvalidUpdate()
{
$instance = $this->getInstance();
self::assertEquals(-123, $instance->put('data12345', -123));
}
}