]> git.mxchange.org Git - friendica.git/blob - tests/src/App/ModuleTest.php
Merge pull request #10697 from MrPetovan/bug/10692-api-expand-entities
[friendica.git] / tests / src / App / ModuleTest.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Test\src\App;
23
24 use Friendica\App;
25 use Friendica\Core\Cache\ICache;
26 use Friendica\Core\Config\IConfig;
27 use Friendica\Core\L10n;
28 use Friendica\Core\Lock\ILock;
29 use Friendica\LegacyModule;
30 use Friendica\Module\HTTPException\PageNotFound;
31 use Friendica\Module\WellKnown\HostMeta;
32 use Friendica\Test\DatabaseTest;
33 use Mockery;
34
35 class ModuleTest extends DatabaseTest
36 {
37         private function assertModule(array $assert, App\Module $module)
38         {
39                 self::assertEquals($assert['isBackend'], $module->isBackend());
40                 self::assertEquals($assert['name'], $module->getName());
41                 self::assertEquals($assert['class'], $module->getClassName());
42         }
43
44         /**
45          * Test the default module mode
46          */
47         public function testDefault()
48         {
49                 $module = new App\Module();
50
51                 self::assertModule([
52                         'isBackend' => false,
53                         'name'      => App\Module::DEFAULT,
54                         'class'     => App\Module::DEFAULT_CLASS,
55                 ], $module);
56         }
57
58         public function dataModuleName()
59         {
60                 return [
61                         'default'                   => [
62                                 'assert' => [
63                                         'isBackend' => false,
64                                         'name'      => 'network',
65                                         'class'     => App\Module::DEFAULT_CLASS,
66                                 ],
67                                 'args'   => new App\Arguments('network/data/in',
68                                         'network/data/in',
69                                         ['network', 'data', 'in'],
70                                         3),
71                         ],
72                         'withStrikeAndPoint'        => [
73                                 'assert' => [
74                                         'isBackend' => false,
75                                         'name'      => 'with_strike_and_point',
76                                         'class'     => App\Module::DEFAULT_CLASS,
77                                 ],
78                                 'args'   => new App\Arguments('with-strike.and-point/data/in',
79                                         'with-strike.and-point/data/in',
80                                         ['with-strike.and-point', 'data', 'in'],
81                                         3),
82                         ],
83                         'withNothing'               => [
84                                 'assert' => [
85                                         'isBackend' => false,
86                                         'name'      => App\Module::DEFAULT,
87                                         'class'     => App\Module::DEFAULT_CLASS,
88                                 ],
89                                 'args'   => new App\Arguments(),
90                         ],
91                         'withIndex'                 => [
92                                 'assert' => [
93                                         'isBackend' => false,
94                                         'name'      => App\Module::DEFAULT,
95                                         'class'     => App\Module::DEFAULT_CLASS,
96                                 ],
97                                 'args'   => new App\Arguments(),
98                         ],
99                         'withBackendMod'    => [
100                                 'assert' => [
101                                         'isBackend' => true,
102                                         'name'      => App\Module::BACKEND_MODULES[0],
103                                         'class'     => App\Module::DEFAULT_CLASS,
104                                 ],
105                                 'args'   => new App\Arguments(App\Module::BACKEND_MODULES[0] . '/data/in',
106                                         App\Module::BACKEND_MODULES[0] . '/data/in',
107                                         [App\Module::BACKEND_MODULES[0], 'data', 'in'],
108                                         3),
109                         ],
110                         'withFirefoxApp'            => [
111                                 'assert' => [
112                                         'isBackend' => false,
113                                         'name'      => 'login',
114                                         'class'     => App\Module::DEFAULT_CLASS,
115                                 ],
116                                 'args'   => new App\Arguments('users/sign_in',
117                                         'users/sign_in',
118                                         ['users', 'sign_in'],
119                                         3),
120                         ],
121                 ];
122         }
123
124         /**
125          * Test the module name and backend determination
126          *
127          * @dataProvider dataModuleName
128          */
129         public function testModuleName(array $assert, App\Arguments $args)
130         {
131                 $module = (new App\Module())->determineModule($args);
132
133                 self::assertModule($assert, $module);
134         }
135
136         public function dataModuleClass()
137         {
138                 return [
139                         'default' => [
140                                 'assert'  => App\Module::DEFAULT_CLASS,
141                                 'name'    => App\Module::DEFAULT,
142                                 'command' => App\Module::DEFAULT,
143                                 'privAdd' => false,
144                         ],
145                         'legacy'  => [
146                                 'assert'  => LegacyModule::class,
147                                 // API is one of the last modules to switch from legacy to new BaseModule
148                                 // so this should be a stable test case until we completely switch ;-)
149                                 'name'    => 'api',
150                                 'command' => 'api/test/it',
151                                 'privAdd' => false,
152                         ],
153                         'new'     => [
154                                 'assert'  => HostMeta::class,
155                                 'not_required',
156                                 'command' => '.well-known/host-meta',
157                                 'privAdd' => false,
158                         ],
159                         '404'     => [
160                                 'assert'  => PageNotFound::class,
161                                 'name'    => 'invalid',
162                                 'command' => 'invalid',
163                                 'privAdd' => false,
164                         ]
165                 ];
166         }
167
168         /**
169          * Test the determination of the module class
170          *
171          * @dataProvider dataModuleClass
172          */
173         public function testModuleClass($assert, string $name, string $command, bool $privAdd)
174         {
175                 $config = Mockery::mock(IConfig::class);
176                 $config->shouldReceive('get')->with('config', 'private_addons', false)->andReturn($privAdd)->atMost()->once();
177
178                 $l10n = Mockery::mock(L10n::class);
179                 $l10n->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
180
181                 $cache = Mockery::mock(ICache::class);
182                 $cache->shouldReceive('get')->with('routerDispatchData')->andReturn('')->atMost()->once();
183                 $cache->shouldReceive('get')->with('lastRoutesFileModifiedTime')->andReturn('')->atMost()->once();
184                 $cache->shouldReceive('set')->withAnyArgs()->andReturn(false)->atMost()->twice();
185
186                 $lock = Mockery::mock(ILock::class);
187                 $lock->shouldReceive('acquire')->andReturn(true);
188                 $lock->shouldReceive('isLocked')->andReturn(false);
189
190                 $router = (new App\Router([], __DIR__ . '/../../../static/routes.config.php', $l10n, $cache, $lock));
191
192                 $module = (new App\Module($name))->determineClass(new App\Arguments('', $command), $router, $config);
193
194                 self::assertEquals($assert, $module->getClassName());
195         }
196
197         /**
198          * Test that modules are immutable
199          */
200         public function testImmutable()
201         {
202                 $module = new App\Module();
203
204                 $moduleNew = $module->determineModule(new App\Arguments());
205
206                 self::assertNotSame($moduleNew, $module);
207         }
208 }