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\Database\DBStructure;
38 use Friendica\Test\DatabaseTest;
39 use Friendica\Test\Util\Database\StaticDatabase;
40 use Friendica\Test\Util\VFSTrait;
41 use Friendica\Util\ConfigFileLoader;
42 use Friendica\Util\Profiler;
43 use Psr\Log\LoggerInterface;
44 use Psr\Log\NullLogger;
45 use Friendica\Test\Util\SampleStorageBackend;
47 class StorageManagerTest extends DatabaseTest
53 /** @var LoggerInterface */
60 public function setUp()
66 $this->logger = new NullLogger();
68 $profiler = \Mockery::mock(Profiler::class);
69 $profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
71 // load real config to avoid mocking every config-entry which is related to the Database class
72 $configFactory = new ConfigFactory();
73 $loader = new ConfigFileLoader($this->root->url());
74 $configCache = $configFactory->createCache($loader);
76 $this->dba = new StaticDatabase($configCache, $profiler, $this->logger);
78 $this->dba->setTestmode(true);
80 $configModel = new Config($this->dba);
81 $this->config = new PreloadConfig($configCache, $configModel);
83 $this->l10n = \Mockery::mock(L10n::class);
87 * Test plain instancing first
89 public function testInstance()
91 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
93 $this->assertInstanceOf(StorageManager::class, $storageManager);
96 public function dataStorages()
103 'userBackend' => false,
106 'name' => Storage\Database::NAME,
107 'assert' => Storage\Database::class,
108 'assertName' => Storage\Database::NAME,
109 'userBackend' => true,
112 'name' => Storage\Filesystem::NAME,
113 'assert' => Storage\Filesystem::class,
114 'assertName' => Storage\Filesystem::NAME,
115 'userBackend' => true,
117 'systemresource' => [
118 'name' => Storage\SystemResource::NAME,
119 'assert' => Storage\SystemResource::class,
120 'assertName' => Storage\SystemResource::NAME,
121 // false here, because SystemResource isn't meant to be a user backend,
122 // it's for system resources only
123 'userBackend' => false,
129 'userBackend' => false,
135 * Data array for legacy backends
137 * @todo 2020.09 After 2 releases, remove the legacy functionality and these data array with it
141 public function dataLegacyBackends()
144 'legacyDatabase' => [
145 'name' => 'Friendica\Model\Storage\Database',
146 'assert' => Storage\Database::class,
147 'assertName' => Storage\Database::NAME,
148 'userBackend' => true,
150 'legacyFilesystem' => [
151 'name' => 'Friendica\Model\Storage\Filesystem',
152 'assert' => Storage\Filesystem::class,
153 'assertName' => Storage\Filesystem::NAME,
154 'userBackend' => true,
156 'legacySystemResource' => [
157 'name' => 'Friendica\Model\Storage\SystemResource',
158 'assert' => Storage\SystemResource::class,
159 'assertName' => Storage\SystemResource::NAME,
160 'userBackend' => false,
166 * Test the getByName() method
168 * @dataProvider dataStorages
169 * @dataProvider dataLegacyBackends
171 public function testGetByName($name, $assert, $assertName, $userBackend)
173 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
175 $storage = $storageManager->getByName($name, $userBackend);
177 if (!empty($assert)) {
178 $this->assertInstanceOf(Storage\IStorage::class, $storage);
179 $this->assertInstanceOf($assert, $storage);
181 $this->assertNull($storage);
183 $this->assertEquals($assertName, $storage);
187 * Test the isValidBackend() method
189 * @dataProvider dataStorages
191 public function testIsValidBackend($name, $assert, $assertName, $userBackend)
193 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
195 // true in every of the backends
196 $this->assertEquals(!empty($assertName), $storageManager->isValidBackend($name));
198 // if userBackend is set to true, filter out e.g. SystemRessource
199 $this->assertEquals($userBackend, $storageManager->isValidBackend($name, true));
203 * Test the method listBackends() with default setting
205 public function testListBackends()
207 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
209 $this->assertEquals(StorageManager::DEFAULT_BACKENDS, $storageManager->listBackends());
213 * Test the method getBackend()
215 * @dataProvider dataStorages
217 public function testGetBackend($name, $assert, $assertName, $userBackend)
219 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
221 $this->assertNull($storageManager->getBackend());
224 $storageManager->setBackend($name);
226 $this->assertInstanceOf($assert, $storageManager->getBackend());
231 * Test the method getBackend() with a pre-configured backend
233 * @dataProvider dataStorages
234 * @dataProvider dataLegacyBackends
236 public function testPresetBackend($name, $assert, $assertName, $userBackend)
238 $this->config->set('storage', 'name', $name);
240 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
243 $this->assertInstanceOf($assert, $storageManager->getBackend());
245 $this->assertNull($storageManager->getBackend());
250 * Tests the register and unregister methods for a new backend storage class
252 * Uses a sample storage for testing
254 * @see SampleStorageBackend
256 public function testRegisterUnregisterBackends()
258 /// @todo Remove dice once "Hook" is dynamic and mockable
260 ->addRules(include __DIR__ . '/../../../static/dependencies.config.php')
261 ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
262 ->addRule(ISession::class, ['instanceOf' => Session\Memory::class, 'shared' => true, 'call' => null]);
265 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
267 $this->assertTrue($storageManager->register(SampleStorageBackend::class));
269 $this->assertEquals(array_merge(StorageManager::DEFAULT_BACKENDS, [
270 SampleStorageBackend::getName() => SampleStorageBackend::class,
271 ]), $storageManager->listBackends());
272 $this->assertEquals(array_merge(StorageManager::DEFAULT_BACKENDS, [
273 SampleStorageBackend::getName() => SampleStorageBackend::class,
274 ]), $this->config->get('storage', 'backends'));
276 // inline call to register own class as hook (testing purpose only)
277 SampleStorageBackend::registerHook();
280 $this->assertTrue($storageManager->setBackend(SampleStorageBackend::NAME));
281 $this->assertEquals(SampleStorageBackend::NAME, $this->config->get('storage', 'name'));
283 $this->assertInstanceOf(SampleStorageBackend::class, $storageManager->getBackend());
285 $this->assertTrue($storageManager->unregister(SampleStorageBackend::class));
286 $this->assertEquals(StorageManager::DEFAULT_BACKENDS, $this->config->get('storage', 'backends'));
287 $this->assertEquals(StorageManager::DEFAULT_BACKENDS, $storageManager->listBackends());
289 $this->assertNull($storageManager->getBackend());
290 $this->assertNull($this->config->get('storage', 'name'));
294 * Test moving data to a new storage (currently testing db & filesystem)
296 * @dataProvider dataStorages
298 public function testMoveStorage($name, $assert, $assertName, $userBackend)
304 $this->loadFixture(__DIR__ . '/../../datasets/storage/database.fixture.php', $this->dba);
306 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
307 $storage = $storageManager->getByName($name);
308 $storageManager->move($storage);
310 $photos = $this->dba->select('photo', ['backend-ref', 'backend-class', 'id', 'data']);
312 while ($photo = $this->dba->fetch($photos)) {
314 $this->assertEmpty($photo['data']);
316 $storage = $storageManager->getByName($photo['backend-class']);
317 $data = $storage->get($photo['backend-ref']);
319 $this->assertNotEmpty($data);
324 * Test moving data to a WRONG storage
326 * @expectedException \Friendica\Model\Storage\StorageException
327 * @expectedExceptionMessage Can't move to storage backend 'SystemResource'
329 public function testMoveStorageWrong()
331 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
332 $storage = $storageManager->getByName(Storage\SystemResource::getName());
333 $storageManager->move($storage);