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('startRecording');
73 $profiler->shouldReceive('stopRecording');
74 $profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
76 // load real config to avoid mocking every config-entry which is related to the Database class
77 $configFactory = new ConfigFactory();
78 $loader = new ConfigFileLoader($this->root->url());
79 $configCache = $configFactory->createCache($loader);
81 $this->dba = new StaticDatabase($configCache, $profiler, $this->logger);
83 $configModel = new Config($this->dba);
84 $this->config = new PreloadConfig($configCache, $configModel);
86 $this->l10n = \Mockery::mock(L10n::class);
88 $this->httpRequest = \Mockery::mock(HTTPRequest::class);
92 * Test plain instancing first
94 public function testInstance()
96 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
98 self::assertInstanceOf(StorageManager::class, $storageManager);
101 public function dataStorages()
108 'userBackend' => false,
111 'name' => Storage\Database::NAME,
112 'assert' => Storage\Database::class,
113 'assertName' => Storage\Database::NAME,
114 'userBackend' => true,
117 'name' => Storage\Filesystem::NAME,
118 'assert' => Storage\Filesystem::class,
119 'assertName' => Storage\Filesystem::NAME,
120 'userBackend' => true,
122 'systemresource' => [
123 'name' => Storage\SystemResource::NAME,
124 'assert' => Storage\SystemResource::class,
125 'assertName' => Storage\SystemResource::NAME,
126 // false here, because SystemResource isn't meant to be a user backend,
127 // it's for system resources only
128 'userBackend' => false,
134 'userBackend' => false,
140 * Data array for legacy backends
142 * @todo 2020.09 After 2 releases, remove the legacy functionality and these data array with it
146 public function dataLegacyBackends()
149 'legacyDatabase' => [
150 'name' => 'Friendica\Model\Storage\Database',
151 'assert' => Storage\Database::class,
152 'assertName' => Storage\Database::NAME,
153 'userBackend' => true,
155 'legacyFilesystem' => [
156 'name' => 'Friendica\Model\Storage\Filesystem',
157 'assert' => Storage\Filesystem::class,
158 'assertName' => Storage\Filesystem::NAME,
159 'userBackend' => true,
161 'legacySystemResource' => [
162 'name' => 'Friendica\Model\Storage\SystemResource',
163 'assert' => Storage\SystemResource::class,
164 'assertName' => Storage\SystemResource::NAME,
165 'userBackend' => false,
171 * Test the getByName() method
173 * @dataProvider dataStorages
174 * @dataProvider dataLegacyBackends
176 public function testGetByName($name, $assert, $assertName, $userBackend)
178 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
180 $storage = $storageManager->getByName($name, $userBackend);
182 if (!empty($assert)) {
183 self::assertInstanceOf(Storage\IStorage::class, $storage);
184 self::assertInstanceOf($assert, $storage);
186 self::assertNull($storage);
188 self::assertEquals($assertName, $storage);
192 * Test the isValidBackend() method
194 * @dataProvider dataStorages
196 public function testIsValidBackend($name, $assert, $assertName, $userBackend)
198 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
200 // true in every of the backends
201 self::assertEquals(!empty($assertName), $storageManager->isValidBackend($name));
203 // if userBackend is set to true, filter out e.g. SystemRessource
204 self::assertEquals($userBackend, $storageManager->isValidBackend($name, true));
208 * Test the method listBackends() with default setting
210 public function testListBackends()
212 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
214 self::assertEquals(StorageManager::DEFAULT_BACKENDS, $storageManager->listBackends());
218 * Test the method getBackend()
220 * @dataProvider dataStorages
222 public function testGetBackend($name, $assert, $assertName, $userBackend)
224 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
226 self::assertNull($storageManager->getBackend());
229 $storageManager->setBackend($name);
231 self::assertInstanceOf($assert, $storageManager->getBackend());
236 * Test the method getBackend() with a pre-configured backend
238 * @dataProvider dataStorages
239 * @dataProvider dataLegacyBackends
241 public function testPresetBackend($name, $assert, $assertName, $userBackend)
243 $this->config->set('storage', 'name', $name);
245 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
248 self::assertInstanceOf($assert, $storageManager->getBackend());
250 self::assertNull($storageManager->getBackend());
255 * Tests the register and unregister methods for a new backend storage class
257 * Uses a sample storage for testing
259 * @see SampleStorageBackend
261 public function testRegisterUnregisterBackends()
263 /// @todo Remove dice once "Hook" is dynamic and mockable
265 ->addRules(include __DIR__ . '/../../../static/dependencies.config.php')
266 ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
267 ->addRule(ISession::class, ['instanceOf' => Session\Memory::class, 'shared' => true, 'call' => null]);
270 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
272 self::assertTrue($storageManager->register(SampleStorageBackend::class));
274 self::assertEquals(array_merge(StorageManager::DEFAULT_BACKENDS, [
275 SampleStorageBackend::getName() => SampleStorageBackend::class,
276 ]), $storageManager->listBackends());
277 self::assertEquals(array_merge(StorageManager::DEFAULT_BACKENDS, [
278 SampleStorageBackend::getName() => SampleStorageBackend::class,
279 ]), $this->config->get('storage', 'backends'));
281 // inline call to register own class as hook (testing purpose only)
282 SampleStorageBackend::registerHook();
285 self::assertTrue($storageManager->setBackend(SampleStorageBackend::NAME));
286 self::assertEquals(SampleStorageBackend::NAME, $this->config->get('storage', 'name'));
288 self::assertInstanceOf(SampleStorageBackend::class, $storageManager->getBackend());
290 self::assertTrue($storageManager->unregister(SampleStorageBackend::class));
291 self::assertEquals(StorageManager::DEFAULT_BACKENDS, $this->config->get('storage', 'backends'));
292 self::assertEquals(StorageManager::DEFAULT_BACKENDS, $storageManager->listBackends());
294 self::assertNull($storageManager->getBackend());
295 self::assertNull($this->config->get('storage', 'name'));
299 * Test moving data to a new storage (currently testing db & filesystem)
301 * @dataProvider dataStorages
303 public function testMoveStorage($name, $assert, $assertName, $userBackend)
306 self::markTestSkipped("No user backend");
309 $this->loadFixture(__DIR__ . '/../../datasets/storage/database.fixture.php', $this->dba);
311 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
312 $storage = $storageManager->getByName($name);
313 $storageManager->move($storage);
315 $photos = $this->dba->select('photo', ['backend-ref', 'backend-class', 'id', 'data']);
317 while ($photo = $this->dba->fetch($photos)) {
319 self::assertEmpty($photo['data']);
321 $storage = $storageManager->getByName($photo['backend-class']);
322 $data = $storage->get($photo['backend-ref']);
324 self::assertNotEmpty($data);
329 * Test moving data to a WRONG storage
331 public function testMoveStorageWrong()
333 $this->expectExceptionMessage("Can't move to storage backend 'SystemResource'");
334 $this->expectException(StorageException::class);
336 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n, $this->httpRequest);
337 $storage = $storageManager->getByName(Storage\SystemResource::getName());
338 $storageManager->move($storage);