3 namespace Friendica\Test\src\Model\Storage;
5 use Friendica\Core\Config\IConfiguration;
6 use Friendica\Core\L10n\L10n;
7 use Friendica\Model\Storage\Filesystem;
8 use Friendica\Model\Storage\IStorage;
9 use Friendica\Test\Util\VFSTrait;
10 use Friendica\Util\Profiler;
11 use Mockery\MockInterface;
12 use org\bovigo\vfs\vfsStream;
13 use Psr\Log\NullLogger;
14 use function GuzzleHttp\Psr7\uri_for;
16 class FilesystemStorageTest extends StorageTest
20 /** @var MockInterface|IConfiguration */
23 protected function setUp()
27 vfsStream::create(['storage' => []], $this->root);
32 protected function getInstance()
34 $logger = new NullLogger();
35 $profiler = \Mockery::mock(Profiler::class);
36 $profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
38 /** @var MockInterface|L10n $l10n */
39 $l10n = \Mockery::mock(L10n::class)->makePartial();
40 $this->config = \Mockery::mock(IConfiguration::class);
41 $this->config->shouldReceive('get')
42 ->with('storage', 'filesystem_path', Filesystem::DEFAULT_BASE_FOLDER)
43 ->andReturn($this->root->getChild('storage')->url());
45 return new Filesystem($this->config, $logger, $l10n);
48 protected function assertOption(IStorage $storage)
52 'input', 'Storage base path',
53 $this->root->getChild('storage')->url(),
54 'Folder where uploaded files are saved. For maximum security, This should be a path outside web server folder tree'
56 ], $storage->getOptions());
60 * Test the exception in case of missing directorsy permissions
62 * @expectedException \Friendica\Model\Storage\StorageException
63 * @expectedExceptionMessageRegExp /Filesystem storage failed to create \".*\". Check you write permissions./
65 public function testMissingDirPermissions()
67 $this->root->getChild('storage')->chmod(000);
69 $instance = $this->getInstance();
70 $instance->put('test');
74 * Test the exception in case of missing file permissions
76 * @expectedException \Friendica\Model\Storage\StorageException
77 * @expectedExceptionMessageRegExp /Filesystem storage failed to save data to \".*\". Check your write permissions/
79 public function testMissingFilePermissions()
81 vfsStream::create(['storage' => ['f0' => ['c0' => ['k0i0' => '']]]], $this->root);
83 $this->root->getChild('storage/f0/c0/k0i0')->chmod(000);
85 $instance = $this->getInstance();
86 $instance->put('test', 'f0c0k0i0');
90 * Test the backend storage of the Filesystem Storage class
92 public function testDirectoryTree()
94 $instance = $this->getInstance();
96 $instance->put('test', 'f0c0d0i0');
98 $dir = $this->root->getChild('storage/f0/c0')->url();
99 $file = $this->root->getChild('storage/f0/c0/d0i0')->url();
101 $this->assertDirectoryExists($dir);
102 $this->assertFileExists($file);
104 $this->assertDirectoryIsWritable($dir);
105 $this->assertFileIsWritable($file);
107 $this->assertEquals('test', file_get_contents($file));