3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Test\src\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;
36 class ModuleControllerTest extends DatabaseTest
38 private function assertModule(array $assert, App\ModuleController $module)
40 self::assertEquals($assert['isBackend'], $module->isBackend());
41 self::assertEquals($assert['name'], $module->getName());
42 self::assertEquals($assert['class'], $module->getModule());
46 * Test the default module mode
48 public function testDefault()
50 $module = new App\ModuleController();
52 $defaultClass = App\ModuleController::DEFAULT_CLASS;
56 'name' => App\ModuleController::DEFAULT,
57 'class' => new $defaultClass(),
61 public function dataModuleName()
63 $defaultClass = App\ModuleController::DEFAULT_CLASS;
70 'class' => new $defaultClass(),
72 'args' => new App\Arguments('network/data/in',
74 ['network', 'data', 'in'],
77 'withStrikeAndPoint' => [
80 'name' => 'with_strike_and_point',
81 'class' => new $defaultClass(),
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'],
91 'name' => App\ModuleController::DEFAULT,
92 'class' => new $defaultClass(),
94 'args' => new App\Arguments(),
99 'name' => App\ModuleController::DEFAULT,
100 'class' => new $defaultClass(),
102 'args' => new App\Arguments(),
104 'withBackendMod' => [
107 'name' => App\ModuleController::BACKEND_MODULES[0],
108 'class' => new $defaultClass(),
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'],
115 'withFirefoxApp' => [
117 'isBackend' => false,
119 'class' => new $defaultClass(),
121 'args' => new App\Arguments('users/sign_in',
123 ['users', 'sign_in'],
130 * Test the module name and backend determination
132 * @dataProvider dataModuleName
134 public function testModuleName(array $assert, App\Arguments $args)
136 $module = (new App\ModuleController())->determineName($args);
138 self::assertModule($assert, $module);
141 public function dataModuleClass()
145 'assert' => App\ModuleController::DEFAULT_CLASS,
146 'name' => App\ModuleController::DEFAULT,
147 'command' => App\ModuleController::DEFAULT,
152 'assert' => LegacyModule::class,
154 'command' => 'display/test/it',
156 'args' => [__DIR__ . '/../../datasets/legacy/legacy.php'],
159 'assert' => HostMeta::class,
161 'command' => '.well-known/host-meta',
166 'assert' => PageNotFound::class,
168 'command' => 'invalid',
176 * Test the determination of the module class
178 * @dataProvider dataModuleClass
180 public function testModuleClass($assert, string $name, string $command, bool $privAdd, array $args)
182 $config = Mockery::mock(IManageConfigValues::class);
183 $config->shouldReceive('get')->with('config', 'private_addons', false)->andReturn($privAdd)->atMost()->once();
185 $l10n = Mockery::mock(L10n::class);
186 $l10n->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
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();
193 $lock = Mockery::mock(ICanLock::class);
194 $lock->shouldReceive('acquire')->andReturn(true);
195 $lock->shouldReceive('isLocked')->andReturn(false);
197 $router = (new App\Router([], __DIR__ . '/../../../static/routes.config.php', $l10n, $cache, $lock));
199 $dice = Mockery::mock(Dice::class);
201 $dice->shouldReceive('create')->andReturn(new $assert(...$args));
203 $module = (new App\ModuleController($name))->determineClass(new App\Arguments('', $command), $router, $config, $dice);
205 self::assertEquals($assert, $module->getModule()->getClassName());
209 * Test that modules are immutable
211 public function testImmutable()
213 $module = new App\ModuleController();
215 $moduleNew = $module->determineName(new App\Arguments());
217 self::assertNotSame($moduleNew, $module);