]> git.mxchange.org Git - friendica.git/blob - tests/src/Core/StorageManagerTest.php
Fix loading SystemResource files
[friendica.git] / tests / src / Core / StorageManagerTest.php
1 <?php
2
3 namespace Friendica\Test\src\Core;
4
5 use Dice\Dice;
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;
13 use Friendica\DI;
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;
26
27 class StorageManagerTest extends DatabaseTest
28 {
29         /** @var Database */
30         private $dba;
31         /** @var IConfiguration */
32         private $config;
33         /** @var LoggerInterface */
34         private $logger;
35         /** @var L10n */
36         private $l10n;
37
38         use VFSTrait;
39
40         public function setUp()
41         {
42                 parent::setUp();
43
44                 $this->setUpVfsDir();
45
46                 $this->logger = new NullLogger();
47
48                 $profiler = \Mockery::mock(Profiler::class);
49                 $profiler->shouldReceive('saveTimestamp')->withAnyArgs()->andReturn(true);
50
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);
55
56                 $this->dba = new StaticDatabase($configCache, $profiler, $this->logger);
57
58                 $configModel  = new Config($this->dba);
59                 $this->config = new PreloadConfiguration($configCache, $configModel);
60
61                 $this->l10n = \Mockery::mock(L10n::class);
62         }
63
64         /**
65          * Test plain instancing first
66          */
67         public function testInstance()
68         {
69                 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
70
71                 $this->assertInstanceOf(StorageManager::class, $storageManager);
72         }
73
74         public function dataStorages()
75         {
76                 return [
77                         'empty'          => [
78                                 'name'        => '',
79                                 'assert'      => null,
80                                 'assertName'  => '',
81                                 'userBackend' => false,
82                         ],
83                         'database'       => [
84                                 'name'        => Storage\Database::NAME,
85                                 'assert'      => Storage\Database::class,
86                                 'assertName'  => Storage\Database::NAME,
87                                 'userBackend' => true,
88                         ],
89                         'filesystem'     => [
90                                 'name'        => Storage\Filesystem::NAME,
91                                 'assert'      => Storage\Filesystem::class,
92                                 'assertName'  => Storage\Filesystem::NAME,
93                                 'userBackend' => true,
94                         ],
95                         'systemresource' => [
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,
102                         ],
103                         'invalid'        => [
104                                 'name'        => 'invalid',
105                                 'assert'      => null,
106                                 'assertName'  => '',
107                                 'userBackend' => false,
108                         ],
109                 ];
110         }
111
112         /**
113          * Test the getByName() method
114          *
115          * @dataProvider dataStorages
116          */
117         public function testGetByName($name, $assert, $assertName, $userBackend)
118         {
119                 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
120
121                 $storage = $storageManager->getByName($name, $userBackend);
122
123                 if (!empty($assert)) {
124                         $this->assertInstanceOf(Storage\IStorage::class, $storage);
125                         $this->assertInstanceOf($assert, $storage);
126                         $this->assertEquals($name, $storage::getName());
127                 } else {
128                         $this->assertNull($storage);
129                 }
130                 $this->assertEquals($assertName, $storage);
131         }
132
133         /**
134          * Test the isValidBackend() method
135          *
136          * @dataProvider dataStorages
137          */
138         public function testIsValidBackend($name, $assert, $assertName, $userBackend)
139         {
140                 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
141
142                 // true in every of the backends
143                 $this->assertEquals(!empty($assertName), $storageManager->isValidBackend($name));
144
145                 // if userBackend is set to true, filter out e.g. SystemRessource
146                 $this->assertEquals($userBackend, $storageManager->isValidBackend($name, true));
147         }
148
149         /**
150          * Test the method listBackends() with default setting
151          */
152         public function testListBackends()
153         {
154                 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
155
156                 $this->assertEquals(StorageManager::DEFAULT_BACKENDS, $storageManager->listBackends());
157         }
158
159         /**
160          * Test the method getBackend()
161          *
162          * @dataProvider dataStorages
163          */
164         public function testGetBackend($name, $assert, $assertName, $userBackend)
165         {
166                 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
167
168                 $this->assertNull($storageManager->getBackend());
169
170                 if ($userBackend) {
171                         $storageManager->setBackend($name);
172
173                         $this->assertInstanceOf($assert, $storageManager->getBackend());
174                 }
175         }
176
177         /**
178          * Test the method getBackend() with a pre-configured backend
179          *
180          * @dataProvider dataStorages
181          */
182         public function testPresetBackend($name, $assert, $assertName, $userBackend)
183         {
184                 $this->config->set('storage', 'name', $name);
185
186                 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
187
188                 if ($userBackend) {
189                         $this->assertInstanceOf($assert, $storageManager->getBackend());
190                 } else {
191                         $this->assertNull($storageManager->getBackend());
192                 }
193         }
194
195         /**
196          * Tests the register and unregister methods for a new backend storage class
197          *
198          * Uses a sample storage for testing
199          *
200          * @see SampleStorageBackend
201          */
202         public function testRegisterUnregisterBackends()
203         {
204                 /// @todo Remove dice once "Hook" is dynamic and mockable
205                 $dice   = (new Dice())
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]);
209                 DI::init($dice);
210
211                 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
212
213                 $this->assertTrue($storageManager->register(SampleStorageBackend::class));
214
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'));
221
222                 // inline call to register own class as hook (testing purpose only)
223                 SampleStorageBackend::registerHook();
224                 Hook::loadHooks();
225
226                 $this->assertTrue($storageManager->setBackend(SampleStorageBackend::NAME));
227                 $this->assertEquals(SampleStorageBackend::NAME, $this->config->get('storage', 'name'));
228
229                 $this->assertInstanceOf(SampleStorageBackend::class, $storageManager->getBackend());
230
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());
234
235                 $this->assertNull($storageManager->getBackend());
236                 $this->assertNull($this->config->get('storage', 'name'));
237         }
238
239         /**
240          * Test moving data to a new storage (currently testing db & filesystem)
241          *
242          * @dataProvider dataStorages
243          */
244         public function testMoveStorage($name, $assert, $assertName, $userBackend)
245         {
246                 if (!$userBackend) {
247                         return;
248                 }
249
250                 $this->loadFixture(__DIR__ . '/../../datasets/storage/database.fixture.php', $this->dba);
251
252                 $storageManager = new StorageManager($this->dba, $this->config, $this->logger, $this->l10n);
253                 $storage = $storageManager->getByName($name);
254                 $storageManager->move($storage);
255
256                 $photos = $this->dba->select('photo', ['backend-ref', 'backend-class', 'id', 'data']);
257
258                 while ($photo = $this->dba->fetch($photos)) {
259
260                         $this->assertEmpty($photo['data']);
261
262                         $storage = $storageManager->getByName($photo['backend-class']);
263                         $data = $storage->get($photo['backend-ref']);
264
265                         $this->assertNotEmpty($data);
266                 }
267         }
268 }