]> git.mxchange.org Git - friendica.git/blob - tests/src/App/ModuleTest.php
Update tests/src/App/ModuleTest.php
[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 Dice\Dice;
25 use Friendica\App;
26 use Friendica\Core\Cache\Capability\ICanCache;
27 use Friendica\Core\Config\Capability\IManageConfigValues;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Lock\Capability\ICanLock;
30 use Friendica\LegacyModule;
31 use Friendica\Module\HTTPException\PageNotFound;
32 use Friendica\Module\WellKnown\HostMeta;
33 use Friendica\Test\DatabaseTest;
34 use Mockery;
35
36 class ModuleControllerTest extends DatabaseTest
37 {
38         private function assertModule(array $assert, App\ModuleController $module)
39         {
40                 self::assertEquals($assert['isBackend'], $module->isBackend());
41                 self::assertEquals($assert['name'], $module->getName());
42                 self::assertEquals($assert['class'], $module->getModule());
43         }
44
45         /**
46          * Test the default module mode
47          */
48         public function testDefault()
49         {
50                 $module = new App\ModuleController();
51
52                 $defaultClass = App\ModuleController::DEFAULT_CLASS;
53
54                 self::assertModule([
55                         'isBackend' => false,
56                         'name'      => App\ModuleController::DEFAULT,
57                         'class'     => new $defaultClass(),
58                 ], $module);
59         }
60
61         public function dataModuleName()
62         {
63                 $defaultClass = App\ModuleController::DEFAULT_CLASS;
64
65                 return [
66                         'default'                   => [
67                                 'assert' => [
68                                         'isBackend' => false,
69                                         'name'      => 'network',
70                                         'class'     => new $defaultClass(),
71                                 ],
72                                 'args'   => new App\Arguments('network/data/in',
73                                         'network/data/in',
74                                         ['network', 'data', 'in'],
75                                         3),
76                         ],
77                         'withStrikeAndPoint'        => [
78                                 'assert' => [
79                                         'isBackend' => false,
80                                         'name'      => 'with_strike_and_point',
81                                         'class'     => new $defaultClass(),
82                                 ],
83                                 'args'   => new App\Arguments('with-strike.and-point/data/in',
84                                         'with-strike.and-point/data/in',
85                                         ['with-strike.and-point', 'data', 'in'],
86                                         3),
87                         ],
88                         'withNothing'               => [
89                                 'assert' => [
90                                         'isBackend' => false,
91                                         'name'      => App\ModuleController::DEFAULT,
92                                         'class'     => new $defaultClass(),
93                                 ],
94                                 'args'   => new App\Arguments(),
95                         ],
96                         'withIndex'                 => [
97                                 'assert' => [
98                                         'isBackend' => false,
99                                         'name'      => App\ModuleController::DEFAULT,
100                                         'class'     => new $defaultClass(),
101                                 ],
102                                 'args'   => new App\Arguments(),
103                         ],
104                         'withBackendMod'    => [
105                                 'assert' => [
106                                         'isBackend' => true,
107                                         'name'      => App\ModuleController::BACKEND_MODULES[0],
108                                         'class'     => new $defaultClass(),
109                                 ],
110                                 'args'   => new App\Arguments(App\ModuleController::BACKEND_MODULES[0] . '/data/in',
111                                         App\ModuleController::BACKEND_MODULES[0] . '/data/in',
112                                         [App\ModuleController::BACKEND_MODULES[0], 'data', 'in'],
113                                         3),
114                         ],
115                         'withFirefoxApp'            => [
116                                 'assert' => [
117                                         'isBackend' => false,
118                                         'name'      => 'login',
119                                         'class'     => new $defaultClass(),
120                                 ],
121                                 'args'   => new App\Arguments('users/sign_in',
122                                         'users/sign_in',
123                                         ['users', 'sign_in'],
124                                         3),
125                         ],
126                 ];
127         }
128
129         /**
130          * Test the module name and backend determination
131          *
132          * @dataProvider dataModuleName
133          */
134         public function testModuleName(array $assert, App\Arguments $args)
135         {
136                 $module = (new App\ModuleController())->determineName($args);
137
138                 self::assertModule($assert, $module);
139         }
140
141         public function dataModuleClass()
142         {
143                 return [
144                         'default' => [
145                                 'assert'  => App\ModuleController::DEFAULT_CLASS,
146                                 'name'    => App\ModuleController::DEFAULT,
147                                 'command' => App\ModuleController::DEFAULT,
148                                 'privAdd' => false,
149                                 'args'    => [],
150                         ],
151                         'legacy'  => [
152                                 'assert'  => LegacyModule::class,
153                                 'name'    => 'display',
154                                 'command' => 'display/test/it',
155                                 'privAdd' => false,
156                                 'args'    => [__DIR__ . '/../../datasets/legacy/legacy.php'],
157                         ],
158                         'new'     => [
159                                 'assert'  => HostMeta::class,
160                                 'not_required',
161                                 'command' => '.well-known/host-meta',
162                                 'privAdd' => false,
163                                 'args'    => [],
164                         ],
165                         '404'     => [
166                                 'assert'  => PageNotFound::class,
167                                 'name'    => 'invalid',
168                                 'command' => 'invalid',
169                                 'privAdd' => false,
170                                 'args'    => [],
171                         ]
172                 ];
173         }
174
175         /**
176          * Test the determination of the module class
177          *
178          * @dataProvider dataModuleClass
179          */
180         public function testModuleClass($assert, string $name, string $command, bool $privAdd, array $args)
181         {
182                 $config = Mockery::mock(IManageConfigValues::class);
183                 $config->shouldReceive('get')->with('config', 'private_addons', false)->andReturn($privAdd)->atMost()->once();
184
185                 $l10n = Mockery::mock(L10n::class);
186                 $l10n->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
187
188                 $cache = Mockery::mock(ICanCache::class);
189                 $cache->shouldReceive('get')->with('routerDispatchData')->andReturn('')->atMost()->once();
190                 $cache->shouldReceive('get')->with('lastRoutesFileModifiedTime')->andReturn('')->atMost()->once();
191                 $cache->shouldReceive('set')->withAnyArgs()->andReturn(false)->atMost()->twice();
192
193                 $lock = Mockery::mock(ICanLock::class);
194                 $lock->shouldReceive('acquire')->andReturn(true);
195                 $lock->shouldReceive('isLocked')->andReturn(false);
196
197                 $router = (new App\Router([], __DIR__ . '/../../../static/routes.config.php', $l10n, $cache, $lock));
198
199                 $dice = Mockery::mock(Dice::class);
200
201                 $dice->shouldReceive('create')->andReturn(new $assert(...$args));
202
203                 $module = (new App\ModuleController($name))->determineClass(new App\Arguments('', $command), $router, $config, $dice);
204
205                 self::assertEquals($assert, $module->getModule()->getClassName());
206         }
207
208         /**
209          * Test that modules are immutable
210          */
211         public function testImmutable()
212         {
213                 $module = new App\ModuleController();
214
215                 $moduleNew = $module->determineName(new App\Arguments());
216
217                 self::assertNotSame($moduleNew, $module);
218         }
219 }