3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Test\src\Core\Storage;
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;
30 class FilesystemStorageTest extends StorageTest
34 protected function setUp(): void
38 vfsStream::create(['storage' => []], $this->root);
43 protected function getInstance()
45 return new Filesystem($this->root->getChild(FilesystemConfig::DEFAULT_BASE_FOLDER)->url());
49 * Test the exception in case of missing directory permissions during put new files
51 public function testMissingDirPermissionsDuringPut()
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);
57 $instance = $this->getInstance();
59 $this->root->getChild(FilesystemConfig::DEFAULT_BASE_FOLDER)->chmod(0000);
60 $instance->put('test');
64 * Test the exception in case the directory isn't writeable
66 public function testMissingDirPermissions()
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);
76 * Test the exception in case of missing file permissions
79 public function testMissingFilePermissions()
81 static::markTestIncomplete("Cannot catch file_put_content() error due vfsStream failure");
83 $this->expectException(StorageException::class);
84 $this->expectExceptionMessageMatches("/Filesystem storage failed to save data to \".*\". Check your write permissions/");
86 vfsStream::create(['storage' => ['f0' => ['c0' => ['k0i0' => '']]]], $this->root);
88 $this->root->getChild('storage/f0/c0/k0i0')->chmod(000);
90 $instance = $this->getInstance();
91 $instance->put('test', 'f0c0k0i0');
95 * Test the backend storage of the Filesystem Storage class
97 public function testDirectoryTree()
99 $instance = $this->getInstance();
101 $instance->put('test', 'f0c0d0i0');
103 $dir = $this->root->getChild('storage/f0/c0')->url();
104 $file = $this->root->getChild('storage/f0/c0/d0i0')->url();
106 self::assertDirectoryExists($dir);
107 self::assertFileExists($file);
109 self::assertDirectoryIsWritable($dir);
110 self::assertFileIsWritable($file);
112 self::assertEquals('test', file_get_contents($file));