Make Storage testable & add tests
[friendica.git/.git] / tests / src / Model / Storage / FilesystemStorageTest.php
1 <?php
2
3 namespace Friendica\Test\src\Model\Storage;
4
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;
15
16 class FilesystemStorageTest extends StorageTest
17 {
18         use VFSTrait;
19
20         /** @var MockInterface|IConfiguration */
21         protected $config;
22
23         protected function setUp()
24         {
25                 $this->setUpVfsDir();
26
27                 vfsStream::create(['storage' => []], $this->root);
28
29                 parent::setUp();
30         }
31
32         protected function getInstance()
33         {
34                 $logger = new NullLogger();
35                 $profiler = \Mockery::mock(Profiler::class);
36                 $profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
37
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());
44
45                 return new Filesystem($this->config, $logger, $l10n);
46         }
47
48         protected function assertOption(IStorage $storage)
49         {
50                 $this->assertEquals([
51                         'storagepath' => [
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'
55                         ]
56                 ], $storage->getOptions());
57         }
58
59         /**
60          * Test the exception in case of missing directorsy permissions
61          *
62          * @expectedException  \Friendica\Model\Storage\StorageException
63          * @expectedExceptionMessageRegExp /Filesystem storage failed to create \".*\". Check you write permissions./
64          */
65         public function testMissingDirPermissions()
66         {
67                 $this->root->getChild('storage')->chmod(000);
68
69                 $instance = $this->getInstance();
70                 $instance->put('test');
71         }
72
73         /**
74          * Test the exception in case of missing file permissions
75          *
76          * @expectedException \Friendica\Model\Storage\StorageException
77          * @expectedExceptionMessageRegExp /Filesystem storage failed to save data to \".*\". Check your write permissions/
78          */
79         public function testMissingFilePermissions()
80         {
81                 vfsStream::create(['storage' => ['f0' => ['c0' => ['k0i0' => '']]]], $this->root);
82
83                 $this->root->getChild('storage/f0/c0/k0i0')->chmod(000);
84
85                 $instance = $this->getInstance();
86                 $instance->put('test', 'f0c0k0i0');
87         }
88
89         /**
90          * Test the backend storage of the Filesystem Storage class
91          */
92         public function testDirectoryTree()
93         {
94                 $instance = $this->getInstance();
95
96                 $instance->put('test', 'f0c0d0i0');
97
98                 $dir = $this->root->getChild('storage/f0/c0')->url();
99                 $file = $this->root->getChild('storage/f0/c0/d0i0')->url();
100
101                 $this->assertDirectoryExists($dir);
102                 $this->assertFileExists($file);
103
104                 $this->assertDirectoryIsWritable($dir);
105                 $this->assertFileIsWritable($file);
106
107                 $this->assertEquals('test', file_get_contents($file));
108         }
109 }