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