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