3 * @copyright Copyright (C) 2010-2021, 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;
25 use Friendica\Core\Config\IConfig;
26 use Friendica\Core\Config\PreloadConfig;
27 use Friendica\Core\Hook;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Session\ISession;
30 use Friendica\Core\StorageManager;
31 use Friendica\Database\Database;
33 use Friendica\Factory\ConfigFactory;
34 use Friendica\Model\Config\Config;
35 use Friendica\Model\Storage;
36 use Friendica\Core\Session;
37 use Friendica\Model\Storage\StorageException;
38 use Friendica\Network\HTTPRequest;
39 use Friendica\Test\DatabaseTest;
40 use Friendica\Test\Util\Database\StaticDatabase;
41 use Friendica\Test\Util\VFSTrait;
42 use Friendica\Util\ConfigFileLoader;
43 use Friendica\Util\Profiler;
44 use Psr\Log\LoggerInterface;
45 use Psr\Log\NullLogger;
46 use Friendica\Test\Util\SampleStorageBackend;
48 class StorageManagerTest extends DatabaseTest
54 /** @var LoggerInterface */
58 /** @var HTTPRequest */
63 protected function setUp(): void
69 $this->logger = new NullLogger();
71 $profiler = \Mockery::mock(Profiler::class);
72 $profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
74 // load real config to avoid mocking every config-entry which is related to the Database class
75 $configFactory = new ConfigFactory();
76 $loader = new ConfigFileLoader($this->root->url());
77 $configCache = $configFactory->createCache($loader);
79 $this->dba = new StaticDatabase($configCache, $profiler, $this->logger);
81 $configModel = new Config($this->dba);
82 $this->config = new PreloadConfig($configCache, $configModel);
84 $this->l10n = \Mockery::mock(L10n::class);
86 $this->httpRequest = \Mockery::mock(HTTPRequest::class);
90 * Test plain instancing first
92 public function testInstance()
94 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
96 self::assertInstanceOf(StorageManager::class, $storageManager);
99 public function dataStorages()
106 'userBackend' => false,
109 'name' => Storage\Database::NAME,
110 'assert' => Storage\Database::class,
111 'assertName' => Storage\Database::NAME,
112 'userBackend' => true,
115 'name' => Storage\Filesystem::NAME,
116 'assert' => Storage\Filesystem::class,
117 'assertName' => Storage\Filesystem::NAME,
118 'userBackend' => true,
120 'systemresource' => [
121 'name' => Storage\SystemResource::NAME,
122 'assert' => Storage\SystemResource::class,
123 'assertName' => Storage\SystemResource::NAME,
124 // false here, because SystemResource isn't meant to be a user backend,
125 // it's for system resources only
126 'userBackend' => false,
132 'userBackend' => false,
138 * Data array for legacy backends
140 * @todo 2020.09 After 2 releases, remove the legacy functionality and these data array with it
144 public function dataLegacyBackends()
147 'legacyDatabase' => [
148 'name' => 'Friendica\Model\Storage\Database',
149 'assert' => Storage\Database::class,
150 'assertName' => Storage\Database::NAME,
151 'userBackend' => true,
153 'legacyFilesystem' => [
154 'name' => 'Friendica\Model\Storage\Filesystem',
155 'assert' => Storage\Filesystem::class,
156 'assertName' => Storage\Filesystem::NAME,
157 'userBackend' => true,
159 'legacySystemResource' => [
160 'name' => 'Friendica\Model\Storage\SystemResource',
161 'assert' => Storage\SystemResource::class,
162 'assertName' => Storage\SystemResource::NAME,
163 'userBackend' => false,
169 * Test the getByName() method
171 * @dataProvider dataStorages
172 * @dataProvider dataLegacyBackends
174 public function testGetByName($name, $assert, $assertName, $userBackend)
176 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
178 $storage = $storageManager->getByName($name, $userBackend);
180 if (!empty($assert)) {
181 self::assertInstanceOf(Storage\IStorage::class, $storage);
182 self::assertInstanceOf($assert, $storage);
184 self::assertNull($storage);
186 self::assertEquals($assertName, $storage);
190 * Test the isValidBackend() method
192 * @dataProvider dataStorages
194 public function testIsValidBackend($name, $assert, $assertName, $userBackend)
196 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
198 // true in every of the backends
199 self::assertEquals(!empty($assertName), $storageManager->isValidBackend($name));
201 // if userBackend is set to true, filter out e.g. SystemRessource
202 self::assertEquals($userBackend, $storageManager->isValidBackend($name, true));
206 * Test the method listBackends() with default setting
208 public function testListBackends()
210 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
212 self::assertEquals(StorageManager::DEFAULT_BACKENDS, $storageManager->listBackends());
216 * Test the method getBackend()
218 * @dataProvider dataStorages
220 public function testGetBackend($name, $assert, $assertName, $userBackend)
222 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
224 self::assertNull($storageManager->getBackend());
227 $storageManager->setBackend($name);
229 self::assertInstanceOf($assert, $storageManager->getBackend());
234 * Test the method getBackend() with a pre-configured backend
236 * @dataProvider dataStorages
237 * @dataProvider dataLegacyBackends
239 public function testPresetBackend($name, $assert, $assertName, $userBackend)
241 $this->config->set('storage', 'name', $name);
243 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
246 self::assertInstanceOf($assert, $storageManager->getBackend());
248 self::assertNull($storageManager->getBackend());
253 * Tests the register and unregister methods for a new backend storage class
255 * Uses a sample storage for testing
257 * @see SampleStorageBackend
259 public function testRegisterUnregisterBackends()
261 /// @todo Remove dice once "Hook" is dynamic and mockable
263 ->addRules(include __DIR__ . '/../../../static/dependencies.config.php')
264 ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
265 ->addRule(ISession::class, ['instanceOf' => Session\Memory::class, 'shared' => true, 'call' => null]);
268 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
270 self::assertTrue($storageManager->register(SampleStorageBackend::class));
272 self::assertEquals(array_merge(StorageManager::DEFAULT_BACKENDS, [
273 SampleStorageBackend::getName() => SampleStorageBackend::class,
274 ]), $storageManager->listBackends());
275 self::assertEquals(array_merge(StorageManager::DEFAULT_BACKENDS, [
276 SampleStorageBackend::getName() => SampleStorageBackend::class,
277 ]), $this->config->get('storage', 'backends'));
279 // inline call to register own class as hook (testing purpose only)
280 SampleStorageBackend::registerHook();
283 self::assertTrue($storageManager->setBackend(SampleStorageBackend::NAME));
284 self::assertEquals(SampleStorageBackend::NAME, $this->config->get('storage', 'name'));
286 self::assertInstanceOf(SampleStorageBackend::class, $storageManager->getBackend());
288 self::assertTrue($storageManager->unregister(SampleStorageBackend::class));
289 self::assertEquals(StorageManager::DEFAULT_BACKENDS, $this->config->get('storage', 'backends'));
290 self::assertEquals(StorageManager::DEFAULT_BACKENDS, $storageManager->listBackends());
292 self::assertNull($storageManager->getBackend());
293 self::assertNull($this->config->get('storage', 'name'));
297 * Test moving data to a new storage (currently testing db & filesystem)
299 * @dataProvider dataStorages
301 public function testMoveStorage($name, $assert, $assertName, $userBackend)
304 self::markTestSkipped("No user backend");
307 $this->loadFixture(__DIR__ . '/../../datasets/storage/database.fixture.php', $this->dba);
309 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
310 $storage = $storageManager->getByName($name);
311 $storageManager->move($storage);
313 $photos = $this->dba->select('photo', ['backend-ref', 'backend-class', 'id', 'data']);
315 while ($photo = $this->dba->fetch($photos)) {
317 self::assertEmpty($photo['data']);
319 $storage = $storageManager->getByName($photo['backend-class']);
320 $data = $storage->get($photo['backend-ref']);
322 self::assertNotEmpty($data);
327 * Test moving data to a WRONG storage
329 public function testMoveStorageWrong()
331 $this->expectExceptionMessage("Can't move to storage backend 'SystemResource'");
332 $this->expectException(StorageException::class);
334 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
335 $storage = $storageManager->getByName(Storage\SystemResource::getName());
336 $storageManager->move($storage);