]> git.mxchange.org Git - friendica.git/blob - tests/src/Model/Storage/FilesystemStorageTest.php
Merge pull request #8265 from nupplaphil/task/add_license
[friendica.git] / tests / src / Model / Storage / FilesystemStorageTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Test\src\Model\Storage;
23
24 use Friendica\Core\Config\IConfig;
25 use Friendica\Core\L10n;
26 use Friendica\Model\Storage\Filesystem;
27 use Friendica\Model\Storage\IStorage;
28 use Friendica\Test\Util\VFSTrait;
29 use Friendica\Util\Profiler;
30 use Mockery\MockInterface;
31 use org\bovigo\vfs\vfsStream;
32 use Psr\Log\NullLogger;
33 use function GuzzleHttp\Psr7\uri_for;
34
35 class FilesystemStorageTest extends StorageTest
36 {
37         use VFSTrait;
38
39         /** @var MockInterface|IConfig */
40         protected $config;
41
42         protected function setUp()
43         {
44                 $this->setUpVfsDir();
45
46                 vfsStream::create(['storage' => []], $this->root);
47
48                 parent::setUp();
49         }
50
51         protected function getInstance()
52         {
53                 $logger = new NullLogger();
54                 $profiler = \Mockery::mock(Profiler::class);
55                 $profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
56
57                 /** @var MockInterface|L10n $l10n */
58                 $l10n = \Mockery::mock(L10n::class)->makePartial();
59                 $this->config = \Mockery::mock(IConfig::class);
60                 $this->config->shouldReceive('get')
61                              ->with('storage', 'filesystem_path', Filesystem::DEFAULT_BASE_FOLDER)
62                              ->andReturn($this->root->getChild('storage')->url());
63
64                 return new Filesystem($this->config, $logger, $l10n);
65         }
66
67         protected function assertOption(IStorage $storage)
68         {
69                 $this->assertEquals([
70                         'storagepath' => [
71                                 'input', 'Storage base path',
72                                 $this->root->getChild('storage')->url(),
73                                 'Folder where uploaded files are saved. For maximum security, This should be a path outside web server folder tree'
74                         ]
75                 ], $storage->getOptions());
76         }
77
78         /**
79          * Test the exception in case of missing directorsy permissions
80          *
81          * @expectedException  \Friendica\Model\Storage\StorageException
82          * @expectedExceptionMessageRegExp /Filesystem storage failed to create \".*\". Check you write permissions./
83          */
84         public function testMissingDirPermissions()
85         {
86                 $this->root->getChild('storage')->chmod(000);
87
88                 $instance = $this->getInstance();
89                 $instance->put('test');
90         }
91
92         /**
93          * Test the exception in case of missing file permissions
94          *
95          * @expectedException \Friendica\Model\Storage\StorageException
96          * @expectedExceptionMessageRegExp /Filesystem storage failed to save data to \".*\". Check your write permissions/
97          */
98         public function testMissingFilePermissions()
99         {
100                 $this->markTestIncomplete("Cannot catch file_put_content() error due vfsStream failure");
101
102                 vfsStream::create(['storage' => ['f0' => ['c0' => ['k0i0' => '']]]], $this->root);
103
104                 $this->root->getChild('storage/f0/c0/k0i0')->chmod(000);
105
106                 $instance = $this->getInstance();
107                 $instance->put('test', 'f0c0k0i0');
108         }
109
110         /**
111          * Test the backend storage of the Filesystem Storage class
112          */
113         public function testDirectoryTree()
114         {
115                 $instance = $this->getInstance();
116
117                 $instance->put('test', 'f0c0d0i0');
118
119                 $dir = $this->root->getChild('storage/f0/c0')->url();
120                 $file = $this->root->getChild('storage/f0/c0/d0i0')->url();
121
122                 $this->assertDirectoryExists($dir);
123                 $this->assertFileExists($file);
124
125                 $this->assertDirectoryIsWritable($dir);
126                 $this->assertFileIsWritable($file);
127
128                 $this->assertEquals('test', file_get_contents($file));
129         }
130 }