3 * @copyright Copyright (C) 2020, Friendica
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\Test\DatabaseTest;
38 use Friendica\Test\Util\Database\StaticDatabase;
39 use Friendica\Test\Util\VFSTrait;
40 use Friendica\Util\ConfigFileLoader;
41 use Friendica\Util\Profiler;
42 use Psr\Log\LoggerInterface;
43 use Psr\Log\NullLogger;
44 use Friendica\Test\Util\SampleStorageBackend;
46 class StorageManagerTest extends DatabaseTest
52 /** @var LoggerInterface */
59 public function setUp()
65 $this->logger = new NullLogger();
67 $profiler = \Mockery::mock(Profiler::class);
68 $profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
70 // load real config to avoid mocking every config-entry which is related to the Database class
71 $configFactory = new ConfigFactory();
72 $loader = new ConfigFileLoader($this->root->url());
73 $configCache = $configFactory->createCache($loader);
75 $this->dba = new StaticDatabase($configCache, $profiler, $this->logger);
77 $configModel = new Config($this->dba);
78 $this->config = new PreloadConfig($configCache, $configModel);
80 $this->l10n = \Mockery::mock(L10n::class);
84 * Test plain instancing first
86 public function testInstance()
88 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
90 $this->assertInstanceOf(StorageManager::class, $storageManager);
93 public function dataStorages()
100 'userBackend' => false,
103 'name' => Storage\Database::NAME,
104 'assert' => Storage\Database::class,
105 'assertName' => Storage\Database::NAME,
106 'userBackend' => true,
109 'name' => Storage\Filesystem::NAME,
110 'assert' => Storage\Filesystem::class,
111 'assertName' => Storage\Filesystem::NAME,
112 'userBackend' => true,
114 'systemresource' => [
115 'name' => Storage\SystemResource::NAME,
116 'assert' => Storage\SystemResource::class,
117 'assertName' => Storage\SystemResource::NAME,
118 // false here, because SystemResource isn't meant to be a user backend,
119 // it's for system resources only
120 'userBackend' => false,
126 'userBackend' => false,
132 * Data array for legacy backends
134 * @todo 2020.09 After 2 releases, remove the legacy functionality and these data array with it
138 public function dataLegacyBackends()
141 'legacyDatabase' => [
142 'name' => 'Friendica\Model\Storage\Database',
143 'assert' => Storage\Database::class,
144 'assertName' => Storage\Database::NAME,
145 'userBackend' => true,
147 'legacyFilesystem' => [
148 'name' => 'Friendica\Model\Storage\Filesystem',
149 'assert' => Storage\Filesystem::class,
150 'assertName' => Storage\Filesystem::NAME,
151 'userBackend' => true,
153 'legacySystemResource' => [
154 'name' => 'Friendica\Model\Storage\SystemResource',
155 'assert' => Storage\SystemResource::class,
156 'assertName' => Storage\SystemResource::NAME,
157 'userBackend' => false,
163 * Test the getByName() method
165 * @dataProvider dataStorages
166 * @dataProvider dataLegacyBackends
168 public function testGetByName($name, $assert, $assertName, $userBackend)
170 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
172 $storage = $storageManager->getByName($name, $userBackend);
174 if (!empty($assert)) {
175 $this->assertInstanceOf(Storage\IStorage::class, $storage);
176 $this->assertInstanceOf($assert, $storage);
178 $this->assertNull($storage);
180 $this->assertEquals($assertName, $storage);
184 * Test the isValidBackend() method
186 * @dataProvider dataStorages
188 public function testIsValidBackend($name, $assert, $assertName, $userBackend)
190 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
192 // true in every of the backends
193 $this->assertEquals(!empty($assertName), $storageManager->isValidBackend($name));
195 // if userBackend is set to true, filter out e.g. SystemRessource
196 $this->assertEquals($userBackend, $storageManager->isValidBackend($name, true));
200 * Test the method listBackends() with default setting
202 public function testListBackends()
204 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
206 $this->assertEquals(StorageManager::DEFAULT_BACKENDS, $storageManager->listBackends());
210 * Test the method getBackend()
212 * @dataProvider dataStorages
214 public function testGetBackend($name, $assert, $assertName, $userBackend)
216 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
218 $this->assertNull($storageManager->getBackend());
221 $storageManager->setBackend($name);
223 $this->assertInstanceOf($assert, $storageManager->getBackend());
228 * Test the method getBackend() with a pre-configured backend
230 * @dataProvider dataStorages
231 * @dataProvider dataLegacyBackends
233 public function testPresetBackend($name, $assert, $assertName, $userBackend)
235 $this->config->set('storage', 'name', $name);
237 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
240 $this->assertInstanceOf($assert, $storageManager->getBackend());
242 $this->assertNull($storageManager->getBackend());
247 * Tests the register and unregister methods for a new backend storage class
249 * Uses a sample storage for testing
251 * @see SampleStorageBackend
253 public function testRegisterUnregisterBackends()
255 /// @todo Remove dice once "Hook" is dynamic and mockable
257 ->addRules(include __DIR__ . '/../../../static/dependencies.config.php')
258 ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
259 ->addRule(ISession::class, ['instanceOf' => Session\Memory::class, 'shared' => true, 'call' => null]);
262 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
264 $this->assertTrue($storageManager->register(SampleStorageBackend::class));
266 $this->assertEquals(array_merge(StorageManager::DEFAULT_BACKENDS, [
267 SampleStorageBackend::getName() => SampleStorageBackend::class,
268 ]), $storageManager->listBackends());
269 $this->assertEquals(array_merge(StorageManager::DEFAULT_BACKENDS, [
270 SampleStorageBackend::getName() => SampleStorageBackend::class,
271 ]), $this->config->get('storage', 'backends'));
273 // inline call to register own class as hook (testing purpose only)
274 SampleStorageBackend::registerHook();
277 $this->assertTrue($storageManager->setBackend(SampleStorageBackend::NAME));
278 $this->assertEquals(SampleStorageBackend::NAME, $this->config->get('storage', 'name'));
280 $this->assertInstanceOf(SampleStorageBackend::class, $storageManager->getBackend());
282 $this->assertTrue($storageManager->unregister(SampleStorageBackend::class));
283 $this->assertEquals(StorageManager::DEFAULT_BACKENDS, $this->config->get('storage', 'backends'));
284 $this->assertEquals(StorageManager::DEFAULT_BACKENDS, $storageManager->listBackends());
286 $this->assertNull($storageManager->getBackend());
287 $this->assertNull($this->config->get('storage', 'name'));
291 * Test moving data to a new storage (currently testing db & filesystem)
293 * @dataProvider dataStorages
295 public function testMoveStorage($name, $assert, $assertName, $userBackend)
301 $this->loadFixture(__DIR__ . '/../../datasets/storage/database.fixture.php', $this->dba);
303 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
304 $storage = $storageManager->getByName($name);
305 $storageManager->move($storage);
307 $photos = $this->dba->select('photo', ['backend-ref', 'backend-class', 'id', 'data']);
309 while ($photo = $this->dba->fetch($photos)) {
311 $this->assertEmpty($photo['data']);
313 $storage = $storageManager->getByName($photo['backend-class']);
314 $data = $storage->get($photo['backend-ref']);
316 $this->assertNotEmpty($data);
321 * Test moving data to a WRONG storage
323 * @expectedException \Friendica\Model\Storage\StorageException
324 * @expectedExceptionMessage Can't move to storage backend 'SystemResource'
326 public function testMoveStorageWrong()
328 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
329 $storage = $storageManager->getByName(Storage\SystemResource::getName());
330 $storageManager->move($storage);