]> git.mxchange.org Git - friendica.git/blob - tests/src/App/ModuleTest.php
Replaced most "api_get_user" calls with newer BaseApi calls
[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\Capability\ICanCache;
26 use Friendica\Core\Config\Capability\IManageConfigValues;
27 use Friendica\Core\L10n;
28 use Friendica\Core\Lock\Capability\ICanLock;
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                                 'name'    => 'display',
148                                 'command' => 'display/test/it',
149                                 'privAdd' => false,
150                         ],
151                         'new'     => [
152                                 'assert'  => HostMeta::class,
153                                 'not_required',
154                                 'command' => '.well-known/host-meta',
155                                 'privAdd' => false,
156                         ],
157                         '404'     => [
158                                 'assert'  => PageNotFound::class,
159                                 'name'    => 'invalid',
160                                 'command' => 'invalid',
161                                 'privAdd' => false,
162                         ]
163                 ];
164         }
165
166         /**
167          * Test the determination of the module class
168          *
169          * @dataProvider dataModuleClass
170          */
171         public function testModuleClass($assert, string $name, string $command, bool $privAdd)
172         {
173                 $config = Mockery::mock(IManageConfigValues::class);
174                 $config->shouldReceive('get')->with('config', 'private_addons', false)->andReturn($privAdd)->atMost()->once();
175
176                 $l10n = Mockery::mock(L10n::class);
177                 $l10n->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
178
179                 $cache = Mockery::mock(ICanCache::class);
180                 $cache->shouldReceive('get')->with('routerDispatchData')->andReturn('')->atMost()->once();
181                 $cache->shouldReceive('get')->with('lastRoutesFileModifiedTime')->andReturn('')->atMost()->once();
182                 $cache->shouldReceive('set')->withAnyArgs()->andReturn(false)->atMost()->twice();
183
184                 $lock = Mockery::mock(ICanLock::class);
185                 $lock->shouldReceive('acquire')->andReturn(true);
186                 $lock->shouldReceive('isLocked')->andReturn(false);
187
188                 $router = (new App\Router([], __DIR__ . '/../../../static/routes.config.php', $l10n, $cache, $lock));
189
190                 $module = (new App\Module($name))->determineClass(new App\Arguments('', $command), $router, $config);
191
192                 self::assertEquals($assert, $module->getClassName());
193         }
194
195         /**
196          * Test that modules are immutable
197          */
198         public function testImmutable()
199         {
200                 $module = new App\Module();
201
202                 $moduleNew = $module->determineModule(new App\Arguments());
203
204                 self::assertNotSame($moduleNew, $module);
205         }
206 }