]> git.mxchange.org Git - friendica.git/blob - tests/src/App/ModuleTest.php
Merge pull request #8900 from tobiasd/20200718-serverblocklistcsv
[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
33 class ModuleTest extends DatabaseTest
34 {
35         private function assertModule(array $assert, App\Module $module)
36         {
37                 $this->assertEquals($assert['isBackend'], $module->isBackend());
38                 $this->assertEquals($assert['name'], $module->getName());
39                 $this->assertEquals($assert['class'], $module->getClassName());
40         }
41
42         /**
43          * Test the default module mode
44          */
45         public function testDefault()
46         {
47                 $module = new App\Module();
48
49                 $this->assertModule([
50                         'isBackend' => false,
51                         'name'      => App\Module::DEFAULT,
52                         'class'     => App\Module::DEFAULT_CLASS,
53                 ], $module);
54         }
55
56         public function dataModuleName()
57         {
58                 return [
59                         'default'                   => [
60                                 'assert' => [
61                                         'isBackend' => false,
62                                         'name'      => 'network',
63                                         'class'     => App\Module::DEFAULT_CLASS,
64                                 ],
65                                 'args'   => new App\Arguments('network/data/in',
66                                         'network/data/in',
67                                         ['network', 'data', 'in'],
68                                         3),
69                         ],
70                         'withStrikeAndPoint'        => [
71                                 'assert' => [
72                                         'isBackend' => false,
73                                         'name'      => 'with_strike_and_point',
74                                         'class'     => App\Module::DEFAULT_CLASS,
75                                 ],
76                                 'args'   => new App\Arguments('with-strike.and-point/data/in',
77                                         'with-strike.and-point/data/in',
78                                         ['with-strike.and-point', 'data', 'in'],
79                                         3),
80                         ],
81                         'withNothing'               => [
82                                 'assert' => [
83                                         'isBackend' => false,
84                                         'name'      => App\Module::DEFAULT,
85                                         'class'     => App\Module::DEFAULT_CLASS,
86                                 ],
87                                 'args'   => new App\Arguments(),
88                         ],
89                         'withIndex'                 => [
90                                 'assert' => [
91                                         'isBackend' => false,
92                                         'name'      => App\Module::DEFAULT,
93                                         'class'     => App\Module::DEFAULT_CLASS,
94                                 ],
95                                 'args'   => new App\Arguments(),
96                         ],
97                         'withBackendMod'    => [
98                                 'assert' => [
99                                         'isBackend' => true,
100                                         'name'      => App\Module::BACKEND_MODULES[0],
101                                         'class'     => App\Module::DEFAULT_CLASS,
102                                 ],
103                                 'args'   => new App\Arguments(App\Module::BACKEND_MODULES[0] . '/data/in',
104                                         App\Module::BACKEND_MODULES[0] . '/data/in',
105                                         [App\Module::BACKEND_MODULES[0], 'data', 'in'],
106                                         3),
107                         ],
108                         'withFirefoxApp'            => [
109                                 'assert' => [
110                                         'isBackend' => false,
111                                         'name'      => 'login',
112                                         'class'     => App\Module::DEFAULT_CLASS,
113                                 ],
114                                 'args'   => new App\Arguments('users/sign_in',
115                                         'users/sign_in',
116                                         ['users', 'sign_in'],
117                                         3),
118                         ],
119                 ];
120         }
121
122         /**
123          * Test the module name and backend determination
124          *
125          * @dataProvider dataModuleName
126          */
127         public function testModuleName(array $assert, App\Arguments $args)
128         {
129                 $module = (new App\Module())->determineModule($args);
130
131                 $this->assertModule($assert, $module);
132         }
133
134         public function dataModuleClass()
135         {
136                 return [
137                         'default' => [
138                                 'assert'  => App\Module::DEFAULT_CLASS,
139                                 'name'    => App\Module::DEFAULT,
140                                 'command' => App\Module::DEFAULT,
141                                 'privAdd' => false,
142                         ],
143                         'legacy'  => [
144                                 'assert'  => LegacyModule::class,
145                                 // API is one of the last modules to switch from legacy to new BaseModule
146                                 // so this should be a stable test case until we completely switch ;-)
147                                 'name'    => 'api',
148                                 'command' => 'api/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(IConfig::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(ICache::class);
180                 $cache->shouldReceive('get')->with('routerDispatchData')->andReturn('')->atMost()->once();
181                 $cache->shouldReceive('set')->withAnyArgs()->andReturn(false)->atMost()->once();
182
183                 $router = (new App\Router([], __DIR__ . '/../../../static/routes.config.php', $l10n, $cache));
184
185                 $module = (new App\Module($name))->determineClass(new App\Arguments('', $command), $router, $config);
186
187                 $this->assertEquals($assert, $module->getClassName());
188         }
189
190         /**
191          * Test that modules are immutable
192          */
193         public function testImmutable()
194         {
195                 $module = new App\Module();
196
197                 $moduleNew = $module->determineModule(new App\Arguments());
198
199                 $this->assertNotSame($moduleNew, $module);
200         }
201 }