mirror of
https://github.com/friendica/friendica
synced 2025-04-26 06:30:11 +00:00
Make Storage testable & add tests
- Making StorageManager dynamic (DI::facStorage()) - Making concrete Storage dynamic (DI::storage()) - Add tests for Storage backend and failure handling - Bumping Level-2/Dice to "dev-master" until new release - Using Storage-Names instead of Storage-Classes in config (includes migration)
This commit is contained in:
parent
a5895f8623
commit
08edeae2f9
18 changed files with 744 additions and 242 deletions
52
tests/src/Model/Storage/DatabaseStorageTest.php
Normal file
52
tests/src/Model/Storage/DatabaseStorageTest.php
Normal file
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Test\src\Model\Storage;
|
||||
|
||||
use Friendica\Core\L10n\L10n;
|
||||
use Friendica\Factory\ConfigFactory;
|
||||
use Friendica\Model\Storage\Database;
|
||||
use Friendica\Model\Storage\IStorage;
|
||||
use Friendica\Test\DatabaseTestTrait;
|
||||
use Friendica\Test\Util\Database\StaticDatabase;
|
||||
use Friendica\Test\Util\VFSTrait;
|
||||
use Friendica\Util\ConfigFileLoader;
|
||||
use Friendica\Util\Profiler;
|
||||
use Mockery\MockInterface;
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
class DatabaseStorageTest extends StorageTest
|
||||
{
|
||||
use DatabaseTestTrait;
|
||||
use VFSTrait;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->setUpVfsDir();
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
protected function getInstance()
|
||||
{
|
||||
$logger = new NullLogger();
|
||||
$profiler = \Mockery::mock(Profiler::class);
|
||||
$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);
|
||||
|
||||
$dba = new StaticDatabase($configCache, $profiler, $logger);
|
||||
|
||||
/** @var MockInterface|L10n $l10n */
|
||||
$l10n = \Mockery::mock(L10n::class)->makePartial();
|
||||
|
||||
return new Database($dba, $logger, $l10n);
|
||||
}
|
||||
|
||||
protected function assertOption(IStorage $storage)
|
||||
{
|
||||
$this->assertEmpty($storage->getOptions());
|
||||
}
|
||||
}
|
109
tests/src/Model/Storage/FilesystemStorageTest.php
Normal file
109
tests/src/Model/Storage/FilesystemStorageTest.php
Normal file
|
@ -0,0 +1,109 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Test\src\Model\Storage;
|
||||
|
||||
use Friendica\Core\Config\IConfiguration;
|
||||
use Friendica\Core\L10n\L10n;
|
||||
use Friendica\Model\Storage\Filesystem;
|
||||
use Friendica\Model\Storage\IStorage;
|
||||
use Friendica\Test\Util\VFSTrait;
|
||||
use Friendica\Util\Profiler;
|
||||
use Mockery\MockInterface;
|
||||
use org\bovigo\vfs\vfsStream;
|
||||
use Psr\Log\NullLogger;
|
||||
use function GuzzleHttp\Psr7\uri_for;
|
||||
|
||||
class FilesystemStorageTest extends StorageTest
|
||||
{
|
||||
use VFSTrait;
|
||||
|
||||
/** @var MockInterface|IConfiguration */
|
||||
protected $config;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->setUpVfsDir();
|
||||
|
||||
vfsStream::create(['storage' => []], $this->root);
|
||||
|
||||
parent::setUp();
|
||||
}
|
||||
|
||||
protected function getInstance()
|
||||
{
|
||||
$logger = new NullLogger();
|
||||
$profiler = \Mockery::mock(Profiler::class);
|
||||
$profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
|
||||
|
||||
/** @var MockInterface|L10n $l10n */
|
||||
$l10n = \Mockery::mock(L10n::class)->makePartial();
|
||||
$this->config = \Mockery::mock(IConfiguration::class);
|
||||
$this->config->shouldReceive('get')
|
||||
->with('storage', 'filesystem_path', Filesystem::DEFAULT_BASE_FOLDER)
|
||||
->andReturn($this->root->getChild('storage')->url());
|
||||
|
||||
return new Filesystem($this->config, $logger, $l10n);
|
||||
}
|
||||
|
||||
protected function assertOption(IStorage $storage)
|
||||
{
|
||||
$this->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());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the exception in case of missing directorsy permissions
|
||||
*
|
||||
* @expectedException \Friendica\Model\Storage\StorageException
|
||||
* @expectedExceptionMessageRegExp /Filesystem storage failed to create \".*\". Check you write permissions./
|
||||
*/
|
||||
public function testMissingDirPermissions()
|
||||
{
|
||||
$this->root->getChild('storage')->chmod(000);
|
||||
|
||||
$instance = $this->getInstance();
|
||||
$instance->put('test');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test the exception in case of missing file permissions
|
||||
*
|
||||
* @expectedException \Friendica\Model\Storage\StorageException
|
||||
* @expectedExceptionMessageRegExp /Filesystem storage failed to save data to \".*\". Check your write permissions/
|
||||
*/
|
||||
public function testMissingFilePermissions()
|
||||
{
|
||||
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();
|
||||
|
||||
$this->assertDirectoryExists($dir);
|
||||
$this->assertFileExists($file);
|
||||
|
||||
$this->assertDirectoryIsWritable($dir);
|
||||
$this->assertFileIsWritable($file);
|
||||
|
||||
$this->assertEquals('test', file_get_contents($file));
|
||||
}
|
||||
}
|
96
tests/src/Model/Storage/StorageTest.php
Normal file
96
tests/src/Model/Storage/StorageTest.php
Normal file
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Test\src\Model\Storage;
|
||||
|
||||
use Friendica\Model\Storage\IStorage;
|
||||
use Friendica\Test\MockedTest;
|
||||
|
||||
abstract class StorageTest extends MockedTest
|
||||
{
|
||||
/** @return IStorage */
|
||||
abstract protected function getInstance();
|
||||
|
||||
abstract protected function assertOption(IStorage $storage);
|
||||
|
||||
/**
|
||||
* Test if the instance is "really" implementing the interface
|
||||
*/
|
||||
public function testInstance()
|
||||
{
|
||||
$instance = $this->getInstance();
|
||||
$this->assertInstanceOf(IStorage::class, $instance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if the "getOption" is asserted
|
||||
*/
|
||||
public function testGetOptions()
|
||||
{
|
||||
$instance = $this->getInstance();
|
||||
|
||||
$this->assertOption($instance);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test basic put, get and delete operations
|
||||
*/
|
||||
public function testPutGetDelete()
|
||||
{
|
||||
$instance = $this->getInstance();
|
||||
|
||||
$ref = $instance->put('data12345');
|
||||
$this->assertNotEmpty($ref);
|
||||
|
||||
$this->assertEquals('data12345', $instance->get($ref));
|
||||
|
||||
$this->assertTrue($instance->delete($ref));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a delete with an invalid reference
|
||||
*/
|
||||
public function testInvalidDelete()
|
||||
{
|
||||
$instance = $this->getInstance();
|
||||
|
||||
// Even deleting not existing references should return "true"
|
||||
$this->assertTrue($instance->delete(-1234456));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test a get with an invalid reference
|
||||
*/
|
||||
public function testInvalidGet()
|
||||
{
|
||||
$instance = $this->getInstance();
|
||||
|
||||
// Invalid references return an empty string
|
||||
$this->assertEmpty($instance->get(-123456));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test an update with a given reference
|
||||
*/
|
||||
public function testUpdateReference()
|
||||
{
|
||||
$instance = $this->getInstance();
|
||||
|
||||
$ref = $instance->put('data12345');
|
||||
$this->assertNotEmpty($ref);
|
||||
|
||||
$this->assertEquals('data12345', $instance->get($ref));
|
||||
|
||||
$this->assertEquals($ref, $instance->put('data5432', $ref));
|
||||
$this->assertEquals('data5432', $instance->get($ref));
|
||||
}
|
||||
|
||||
/**
|
||||
* Test that an invalid update results in an insert
|
||||
*/
|
||||
public function testInvalidUpdate()
|
||||
{
|
||||
$instance = $this->getInstance();
|
||||
|
||||
$this->assertEquals(-123, $instance->put('data12345', -123));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue