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