]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/Storage/FilesystemStorageTest.php
Merge pull request #11129 from urbalazs/copyright-2022
[friendica.git] / tests / src / Core / Storage / FilesystemStorageTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
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\Core\Storage;
23
24 use Friendica\Core\Storage\Exception\StorageException;
25 use Friendica\Core\Storage\Type\Filesystem;
26 use Friendica\Core\Storage\Type\FilesystemConfig;
27 use Friendica\Test\Util\VFSTrait;
28 use org\bovigo\vfs\vfsStream;
29
30 class FilesystemStorageTest extends StorageTest
31 {
32         use VFSTrait;
33
34         protected function setUp(): void
35         {
36                 $this->setUpVfsDir();
37
38                 vfsStream::create(['storage' => []], $this->root);
39
40                 parent::setUp();
41         }
42
43         protected function getInstance()
44         {
45                 return new Filesystem($this->root->getChild(FilesystemConfig::DEFAULT_BASE_FOLDER)->url());
46         }
47
48         /**
49          * Test the exception in case of missing directory permissions during put new files
50          */
51         public function testMissingDirPermissionsDuringPut()
52         {
53                 $this->expectException(StorageException::class);
54                 $this->expectExceptionMessageMatches("/Filesystem storage failed to create \".*\". Check you write permissions./");
55                 $this->root->getChild(FilesystemConfig::DEFAULT_BASE_FOLDER)->chmod(0777);
56
57                 $instance = $this->getInstance();
58
59                 $this->root->getChild(FilesystemConfig::DEFAULT_BASE_FOLDER)->chmod(0000);
60                 $instance->put('test');
61         }
62
63         /**
64          * Test the exception in case the directory isn't writeable
65          */
66         public function testMissingDirPermissions()
67         {
68                 $this->expectException(StorageException::class);
69                 $this->expectExceptionMessageMatches("/Path \".*\" does not exist or is not writeable./");
70                 $this->root->getChild(FilesystemConfig::DEFAULT_BASE_FOLDER)->chmod(0000);
71
72                 $this->getInstance();
73         }
74
75         /**
76          * Test the exception in case of missing file permissions
77          *
78          */
79         public function testMissingFilePermissions()
80         {
81                 static::markTestIncomplete("Cannot catch file_put_content() error due vfsStream failure");
82
83                 $this->expectException(StorageException::class);
84                 $this->expectExceptionMessageMatches("/Filesystem storage failed to save data to \".*\". Check your write permissions/");
85
86                 vfsStream::create(['storage' => ['f0' => ['c0' => ['k0i0' => '']]]], $this->root);
87
88                 $this->root->getChild('storage/f0/c0/k0i0')->chmod(000);
89
90                 $instance = $this->getInstance();
91                 $instance->put('test', 'f0c0k0i0');
92         }
93
94         /**
95          * Test the backend storage of the Filesystem Storage class
96          */
97         public function testDirectoryTree()
98         {
99                 $instance = $this->getInstance();
100
101                 $instance->put('test', 'f0c0d0i0');
102
103                 $dir  = $this->root->getChild('storage/f0/c0')->url();
104                 $file = $this->root->getChild('storage/f0/c0/d0i0')->url();
105
106                 self::assertDirectoryExists($dir);
107                 self::assertFileExists($file);
108
109                 self::assertDirectoryIsWritable($dir);
110                 self::assertFileIsWritable($file);
111
112                 self::assertEquals('test', file_get_contents($file));
113         }
114 }