3 namespace Friendica\Test\src\Model\Storage;
5 use Friendica\Core\Config\IConfig;
6 use Friendica\Core\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|IConfig */
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(IConfig::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 $this->markTestIncomplete("Cannot catch file_put_content() error due vfsStream failure");
83 vfsStream::create(['storage' => ['f0' => ['c0' => ['k0i0' => '']]]], $this->root);
85 $this->root->getChild('storage/f0/c0/k0i0')->chmod(000);
87 $instance = $this->getInstance();
88 $instance->put('test', 'f0c0k0i0');
92 * Test the backend storage of the Filesystem Storage class
94 public function testDirectoryTree()
96 $instance = $this->getInstance();
98 $instance->put('test', 'f0c0d0i0');
100 $dir = $this->root->getChild('storage/f0/c0')->url();
101 $file = $this->root->getChild('storage/f0/c0/d0i0')->url();
103 $this->assertDirectoryExists($dir);
104 $this->assertFileExists($file);
106 $this->assertDirectoryIsWritable($dir);
107 $this->assertFileIsWritable($file);
109 $this->assertEquals('test', file_get_contents($file));