2020-01-05 01:58:49 +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-05 01:58:49 +01:00
|
|
|
|
|
|
|
namespace Friendica\Test\src\Model\Storage;
|
|
|
|
|
2020-01-19 21:29:36 +01:00
|
|
|
use Friendica\Core\Config\IConfig;
|
2020-01-18 20:59:39 +01:00
|
|
|
use Friendica\Core\L10n;
|
2020-01-05 01:58:49 +01:00
|
|
|
use Friendica\Model\Storage\Filesystem;
|
2021-08-07 22:07:47 +02:00
|
|
|
use Friendica\Model\Storage\ISelectableStorage;
|
2020-10-18 20:31:57 +02:00
|
|
|
use Friendica\Model\Storage\StorageException;
|
2020-01-05 01:58:49 +01:00
|
|
|
use Friendica\Test\Util\VFSTrait;
|
|
|
|
use Friendica\Util\Profiler;
|
|
|
|
use Mockery\MockInterface;
|
|
|
|
use org\bovigo\vfs\vfsStream;
|
|
|
|
|
|
|
|
class FilesystemStorageTest extends StorageTest
|
|
|
|
{
|
|
|
|
use VFSTrait;
|
|
|
|
|
2020-01-19 21:29:36 +01:00
|
|
|
/** @var MockInterface|IConfig */
|
2020-01-05 01:58:49 +01:00
|
|
|
protected $config;
|
|
|
|
|
2021-04-01 23:04:30 +02:00
|
|
|
protected function setUp(): void
|
2020-01-05 01:58:49 +01:00
|
|
|
{
|
|
|
|
$this->setUpVfsDir();
|
|
|
|
|
|
|
|
vfsStream::create(['storage' => []], $this->root);
|
|
|
|
|
|
|
|
parent::setUp();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getInstance()
|
|
|
|
{
|
|
|
|
$profiler = \Mockery::mock(Profiler::class);
|
2021-07-27 08:22:37 +02:00
|
|
|
$profiler->shouldReceive('startRecording');
|
|
|
|
$profiler->shouldReceive('stopRecording');
|
2020-01-05 01:58:49 +01:00
|
|
|
$profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
|
|
|
|
|
|
|
|
/** @var MockInterface|L10n $l10n */
|
|
|
|
$l10n = \Mockery::mock(L10n::class)->makePartial();
|
2020-01-19 21:29:36 +01:00
|
|
|
$this->config = \Mockery::mock(IConfig::class);
|
2020-01-05 01:58:49 +01:00
|
|
|
$this->config->shouldReceive('get')
|
|
|
|
->with('storage', 'filesystem_path', Filesystem::DEFAULT_BASE_FOLDER)
|
|
|
|
->andReturn($this->root->getChild('storage')->url());
|
|
|
|
|
2021-08-01 14:31:57 +02:00
|
|
|
return new Filesystem($this->config, $l10n);
|
2020-01-05 01:58:49 +01:00
|
|
|
}
|
|
|
|
|
2021-08-07 22:07:47 +02:00
|
|
|
protected function assertOption(ISelectableStorage $storage)
|
2020-01-05 01:58:49 +01:00
|
|
|
{
|
2020-10-17 14:19:57 +02:00
|
|
|
self::assertEquals([
|
2020-01-05 01:58:49 +01:00
|
|
|
'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
|
|
|
|
*/
|
|
|
|
public function testMissingDirPermissions()
|
|
|
|
{
|
2020-10-18 20:31:57 +02:00
|
|
|
$this->expectException(StorageException::class);
|
2021-05-23 23:09:49 +02:00
|
|
|
$this->expectExceptionMessageMatches("/Filesystem storage failed to create \".*\". Check you write permissions./");
|
2020-01-05 01:58:49 +01:00
|
|
|
$this->root->getChild('storage')->chmod(000);
|
|
|
|
|
|
|
|
$instance = $this->getInstance();
|
|
|
|
$instance->put('test');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test the exception in case of missing file permissions
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function testMissingFilePermissions()
|
|
|
|
{
|
2020-10-18 20:31:57 +02:00
|
|
|
static::markTestIncomplete("Cannot catch file_put_content() error due vfsStream failure");
|
|
|
|
|
|
|
|
$this->expectException(StorageException::class);
|
2021-05-23 23:09:49 +02:00
|
|
|
$this->expectExceptionMessageMatches("/Filesystem storage failed to save data to \".*\". Check your write permissions/");
|
2020-01-08 21:31:16 +01:00
|
|
|
|
2020-01-05 01:58:49 +01:00
|
|
|
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();
|
|
|
|
|
2020-10-17 14:19:57 +02:00
|
|
|
self::assertDirectoryExists($dir);
|
|
|
|
self::assertFileExists($file);
|
2020-01-05 01:58:49 +01:00
|
|
|
|
2020-10-17 14:19:57 +02:00
|
|
|
self::assertDirectoryIsWritable($dir);
|
|
|
|
self::assertFileIsWritable($file);
|
2020-01-05 01:58:49 +01:00
|
|
|
|
2020-10-17 14:19:57 +02:00
|
|
|
self::assertEquals('test', file_get_contents($file));
|
2020-01-05 01:58:49 +01:00
|
|
|
}
|
|
|
|
}
|