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