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

@ -1,70 +0,0 @@
<?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\Model\Storage;
use Friendica\Core\Config\Factory\Config;
use Friendica\Model\Storage\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

@ -1,67 +0,0 @@
<?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\Model\Storage;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\L10n;
use Friendica\Model\Storage\FilesystemConfig;
use Friendica\Model\Storage\IStorageConfiguration;
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(IStorageConfiguration $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

@ -1,114 +0,0 @@
<?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\Model\Storage;
use Friendica\Model\Storage\Filesystem;
use Friendica\Model\Storage\FilesystemConfig;
use Friendica\Model\Storage\StorageException;
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

@ -1,43 +0,0 @@
<?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\Model\Storage;
use Friendica\Model\Storage\IStorageConfiguration;
use Friendica\Test\MockedTest;
abstract class StorageConfigTest extends MockedTest
{
/** @return IStorageConfiguration */
abstract protected function getInstance();
abstract protected function assertOption(IStorageConfiguration $storage);
/**
* Test if the "getOption" is asserted
*/
public function testGetOptions()
{
$instance = $this->getInstance();
$this->assertOption($instance);
}
}

View file

@ -1,107 +0,0 @@
<?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\Model\Storage;
use Friendica\Model\Storage\IWritableStorage;
use Friendica\Model\Storage\IStorage;
use Friendica\Model\Storage\ReferenceStorageException;
use Friendica\Test\MockedTest;
abstract class StorageTest extends MockedTest
{
/** @return IWritableStorage */
abstract protected function getInstance();
/**
* Test if the instance is "really" implementing the interface
*/
public function testInstance()
{
$instance = $this->getInstance();
self::assertInstanceOf(IStorage::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));
}
}