3 namespace Friendica\Test\src\Core;
6 use Friendica\Core\Config\IConfiguration;
7 use Friendica\Core\Config\PreloadConfiguration;
8 use Friendica\Core\Hook;
9 use Friendica\Core\L10n\L10n;
10 use Friendica\Core\Session\ISession;
11 use Friendica\Core\StorageManager;
12 use Friendica\Database\Database;
14 use Friendica\Factory\ConfigFactory;
15 use Friendica\Model\Config\Config;
16 use Friendica\Model\Storage;
17 use Friendica\Core\Session;
18 use Friendica\Test\DatabaseTest;
19 use Friendica\Test\Util\Database\StaticDatabase;
20 use Friendica\Test\Util\VFSTrait;
21 use Friendica\Util\ConfigFileLoader;
22 use Friendica\Util\Profiler;
23 use Psr\Log\LoggerInterface;
24 use Psr\Log\NullLogger;
25 use Friendica\Test\Util\SampleStorageBackend;
27 class StorageManagerTest extends DatabaseTest
31 /** @var IConfiguration */
33 /** @var LoggerInterface */
40 public function setUp()
46 $this->logger = new NullLogger();
48 $profiler = \Mockery::mock(Profiler::class);
49 $profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
51 // load real config to avoid mocking every config-entry which is related to the Database class
52 $configFactory = new ConfigFactory();
53 $loader = new ConfigFileLoader($this->root->url());
54 $configCache = $configFactory->createCache($loader);
56 $this->dba = new StaticDatabase($configCache, $profiler, $this->logger);
58 $configModel = new Config($this->dba);
59 $this->config = new PreloadConfiguration($configCache, $configModel);
61 $this->l10n = \Mockery::mock(L10n::class);
65 * Test plain instancing first
67 public function testInstance()
69 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
71 $this->assertInstanceOf(StorageManager::class, $storageManager);
74 public function dataStorages()
81 'userBackend' => false,
84 'name' => Storage\Database::NAME,
85 'assert' => Storage\Database::class,
86 'assertName' => Storage\Database::NAME,
87 'userBackend' => true,
90 'name' => Storage\Filesystem::NAME,
91 'assert' => Storage\Filesystem::class,
92 'assertName' => Storage\Filesystem::NAME,
93 'userBackend' => true,
96 'name' => Storage\SystemResource::NAME,
97 'assert' => Storage\SystemResource::class,
98 'assertName' => Storage\SystemResource::NAME,
99 // false here, because SystemResource isn't meant to be a user backend,
100 // it's for system resources only
101 'userBackend' => false,
107 'userBackend' => false,
113 * Test the getByName() method
115 * @dataProvider dataStorages
117 public function testGetByName($name, $assert, $assertName, $userBackend)
119 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
121 $storage = $storageManager->getByName($name, $userBackend);
123 if (!empty($assert)) {
124 $this->assertInstanceOf(Storage\IStorage::class, $storage);
125 $this->assertInstanceOf($assert, $storage);
126 $this->assertEquals($name, $storage::getName());
128 $this->assertNull($storage);
130 $this->assertEquals($assertName, $storage);
134 * Test the isValidBackend() method
136 * @dataProvider dataStorages
138 public function testIsValidBackend($name, $assert, $assertName, $userBackend)
140 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
142 // true in every of the backends
143 $this->assertEquals(!empty($assertName), $storageManager->isValidBackend($name));
145 // if userBackend is set to true, filter out e.g. SystemRessource
146 $this->assertEquals($userBackend, $storageManager->isValidBackend($name, true));
150 * Test the method listBackends() with default setting
152 public function testListBackends()
154 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
156 $this->assertEquals(StorageManager::DEFAULT_BACKENDS, $storageManager->listBackends());
160 * Test the method getBackend()
162 * @dataProvider dataStorages
164 public function testGetBackend($name, $assert, $assertName, $userBackend)
166 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
168 $this->assertNull($storageManager->getBackend());
171 $storageManager->setBackend($name);
173 $this->assertInstanceOf($assert, $storageManager->getBackend());
178 * Test the method getBackend() with a pre-configured backend
180 * @dataProvider dataStorages
182 public function testPresetBackend($name, $assert, $assertName, $userBackend)
184 $this->config->set('storage', 'name', $name);
186 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
189 $this->assertInstanceOf($assert, $storageManager->getBackend());
191 $this->assertNull($storageManager->getBackend());
196 * Tests the register and unregister methods for a new backend storage class
198 * Uses a sample storage for testing
200 * @see SampleStorageBackend
202 public function testRegisterUnregisterBackends()
204 /// @todo Remove dice once "Hook" is dynamic and mockable
206 ->addRules(include __DIR__ . '/../../../static/dependencies.config.php')
207 ->addRule(Database::class, ['instanceOf' => StaticDatabase::class, 'shared' => true])
208 ->addRule(ISession::class, ['instanceOf' => Session\Memory::class, 'shared' => true, 'call' => null]);
211 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
213 $this->assertTrue($storageManager->register(SampleStorageBackend::class));
215 $this->assertEquals(array_merge(StorageManager::DEFAULT_BACKENDS, [
216 SampleStorageBackend::getName() => SampleStorageBackend::class,
217 ]), $storageManager->listBackends());
218 $this->assertEquals(array_merge(StorageManager::DEFAULT_BACKENDS, [
219 SampleStorageBackend::getName() => SampleStorageBackend::class,
220 ]), $this->config->get('storage', 'backends'));
222 // inline call to register own class as hook (testing purpose only)
223 SampleStorageBackend::registerHook();
226 $this->assertTrue($storageManager->setBackend(SampleStorageBackend::NAME));
227 $this->assertEquals(SampleStorageBackend::NAME, $this->config->get('storage', 'name'));
229 $this->assertInstanceOf(SampleStorageBackend::class, $storageManager->getBackend());
231 $this->assertTrue($storageManager->unregister(SampleStorageBackend::class));
232 $this->assertEquals(StorageManager::DEFAULT_BACKENDS, $this->config->get('storage', 'backends'));
233 $this->assertEquals(StorageManager::DEFAULT_BACKENDS, $storageManager->listBackends());
235 $this->assertNull($storageManager->getBackend());
236 $this->assertNull($this->config->get('storage', 'name'));
240 * Test moving data to a new storage (currently testing db & filesystem)
242 * @dataProvider dataStorages
244 public function testMoveStorage($name, $assert, $assertName, $userBackend)
250 $this->loadFixture(__DIR__ . '/../../datasets/storage/database.fixture.php', $this->dba);
252 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
253 $storage = $storageManager->getByName($name);
254 $storageManager->move($storage);
256 $photos = $this->dba->select('photo', ['backend-ref', 'backend-class', 'id', 'data']);
258 while ($photo = $this->dba->fetch($photos)) {
260 $this->assertEmpty($photo['data']);
262 $storage = $storageManager->getByName($photo['backend-class']);
263 $data = $storage->get($photo['backend-ref']);
265 $this->assertNotEmpty($data);
270 * Test moving data to a WRONG storage
272 * @expectedException \Friendica\Model\Storage\StorageException
273 * @expectedExceptionMessageRegExp /Can't move to storage backend '.*'/
275 public function testMoveStorageWrong()
277 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
278 $storage = $storageManager->getByName(Storage\SystemResource::getName());
279 $storageManager->move($storage);