3 * @copyright Copyright (C) 2020, Friendica
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;
25 use Friendica\Core\Config\IConfig;
26 use Friendica\Core\L10n;
27 use Friendica\LegacyModule;
28 use Friendica\Module\HTTPException\PageNotFound;
29 use Friendica\Module\WellKnown\HostMeta;
30 use Friendica\Test\DatabaseTest;
32 class ModuleTest extends DatabaseTest
34 private function assertModule(array $assert, App\Module $module)
36 $this->assertEquals($assert['isBackend'], $module->isBackend());
37 $this->assertEquals($assert['name'], $module->getName());
38 $this->assertEquals($assert['class'], $module->getClassName());
42 * Test the default module mode
44 public function testDefault()
46 $module = new App\Module();
50 'name' => App\Module::DEFAULT,
51 'class' => App\Module::DEFAULT_CLASS,
55 public function dataModuleName()
62 'class' => App\Module::DEFAULT_CLASS,
64 'args' => new App\Arguments('network/data/in',
66 ['network', 'data', 'in'],
69 'withStrikeAndPoint' => [
72 'name' => 'with_strike_and_point',
73 'class' => App\Module::DEFAULT_CLASS,
75 'args' => new App\Arguments('with-strike.and-point/data/in',
76 'with-strike.and-point/data/in',
77 ['with-strike.and-point', 'data', 'in'],
83 'name' => App\Module::DEFAULT,
84 'class' => App\Module::DEFAULT_CLASS,
86 'args' => new App\Arguments(),
91 'name' => App\Module::DEFAULT,
92 'class' => App\Module::DEFAULT_CLASS,
94 'args' => new App\Arguments(),
99 'name' => App\Module::BACKEND_MODULES[0],
100 'class' => App\Module::DEFAULT_CLASS,
102 'args' => new App\Arguments(App\Module::BACKEND_MODULES[0] . '/data/in',
103 App\Module::BACKEND_MODULES[0] . '/data/in',
104 [App\Module::BACKEND_MODULES[0], 'data', 'in'],
107 'withFirefoxApp' => [
109 'isBackend' => false,
111 'class' => App\Module::DEFAULT_CLASS,
113 'args' => new App\Arguments('users/sign_in',
115 ['users', 'sign_in'],
122 * Test the module name and backend determination
124 * @dataProvider dataModuleName
126 public function testModuleName(array $assert, App\Arguments $args)
128 $module = (new App\Module())->determineModule($args);
130 $this->assertModule($assert, $module);
133 public function dataModuleClass()
137 'assert' => App\Module::DEFAULT_CLASS,
138 'name' => App\Module::DEFAULT,
139 'command' => App\Module::DEFAULT,
143 'assert' => LegacyModule::class,
144 // API is one of the last modules to switch from legacy to new BaseModule
145 // so this should be a stable test case until we completely switch ;-)
147 'command' => 'api/test/it',
151 'assert' => HostMeta::class,
153 'command' => '.well-known/host-meta',
157 'assert' => PageNotFound::class,
159 'command' => 'invalid',
166 * Test the determination of the module class
168 * @dataProvider dataModuleClass
170 public function testModuleClass($assert, string $name, string $command, bool $privAdd)
172 $config = \Mockery::mock(IConfig::class);
173 $config->shouldReceive('get')->with('config', 'private_addons', false)->andReturn($privAdd)->atMost()->once();
175 $l10n = \Mockery::mock(L10n::class);
176 $l10n->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
178 $router = (new App\Router([], $l10n))->loadRoutes(include __DIR__ . '/../../../static/routes.config.php');
180 $module = (new App\Module($name))->determineClass(new App\Arguments('', $command), $router, $config);
182 $this->assertEquals($assert, $module->getClassName());
186 * Test that modules are immutable
188 public function testImmutable()
190 $module = new App\Module();
192 $moduleNew = $module->determineModule(new App\Arguments());
194 $this->assertNotSame($moduleNew, $module);