3 namespace Friendica\Test\src\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;
13 class ModuleTest extends DatabaseTest
15 private function assertModule(array $assert, App\Module $module)
17 $this->assertEquals($assert['isBackend'], $module->isBackend());
18 $this->assertEquals($assert['name'], $module->getName());
19 $this->assertEquals($assert['class'], $module->getClassName());
23 * Test the default module mode
25 public function testDefault()
27 $module = new App\Module();
31 'name' => App\Module::DEFAULT,
32 'class' => App\Module::DEFAULT_CLASS,
36 public function dataModuleName()
43 'class' => App\Module::DEFAULT_CLASS,
45 'args' => new App\Arguments('network/data/in',
47 ['network', 'data', 'in'],
50 'withStrikeAndPoint' => [
53 'name' => 'with_strike_and_point',
54 'class' => App\Module::DEFAULT_CLASS,
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'],
64 'name' => App\Module::DEFAULT,
65 'class' => App\Module::DEFAULT_CLASS,
67 'args' => new App\Arguments(),
72 'name' => App\Module::DEFAULT,
73 'class' => App\Module::DEFAULT_CLASS,
75 'args' => new App\Arguments(),
80 'name' => App\Module::BACKEND_MODULES[0],
81 'class' => App\Module::DEFAULT_CLASS,
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'],
92 'class' => App\Module::DEFAULT_CLASS,
94 'args' => new App\Arguments('users/sign_in',
103 * Test the module name and backend determination
105 * @dataProvider dataModuleName
107 public function testModuleName(array $assert, App\Arguments $args)
109 $module = (new App\Module())->determineModule($args);
111 $this->assertModule($assert, $module);
114 public function dataModuleClass()
118 'assert' => App\Module::DEFAULT_CLASS,
119 'name' => App\Module::DEFAULT,
120 'command' => App\Module::DEFAULT,
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 ;-)
128 'command' => 'api/test/it',
132 'assert' => HostMeta::class,
134 'command' => '.well-known/host-meta',
138 'assert' => PageNotFound::class,
140 'command' => 'invalid',
147 * Test the determination of the module class
149 * @dataProvider dataModuleClass
151 public function testModuleClass($assert, string $name, string $command, bool $privAdd)
153 $config = \Mockery::mock(IConfig::class);
154 $config->shouldReceive('get')->with('config', 'private_addons', false)->andReturn($privAdd)->atMost()->once();
156 $l10n = \Mockery::mock(L10n::class);
157 $l10n->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
159 $router = (new App\Router([], $l10n))->loadRoutes(include __DIR__ . '/../../../static/routes.config.php');
161 $module = (new App\Module($name))->determineClass(new App\Arguments('', $command), $router, $config);
163 $this->assertEquals($assert, $module->getClassName());
167 * Test that modules are immutable
169 public function testImmutable()
171 $module = new App\Module();
173 $moduleNew = $module->determineModule(new App\Arguments());
175 $this->assertNotSame($moduleNew, $module);