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;
25 use Friendica\Core\Cache\ICache;
26 use Friendica\Core\Config\IConfig;
27 use Friendica\Core\L10n;
28 use Friendica\LegacyModule;
29 use Friendica\Module\HTTPException\PageNotFound;
30 use Friendica\Module\WellKnown\HostMeta;
31 use Friendica\Test\DatabaseTest;
34 class ModuleTest extends DatabaseTest
36 private function assertModule(array $assert, App\Module $module)
38 self::assertEquals($assert['isBackend'], $module->isBackend());
39 self::assertEquals($assert['name'], $module->getName());
40 self::assertEquals($assert['class'], $module->getClassName());
44 * Test the default module mode
46 public function testDefault()
48 $module = new App\Module();
52 'name' => App\Module::DEFAULT,
53 'class' => App\Module::DEFAULT_CLASS,
57 public function dataModuleName()
64 'class' => App\Module::DEFAULT_CLASS,
66 'args' => new App\Arguments('network/data/in',
68 ['network', 'data', 'in'],
71 'withStrikeAndPoint' => [
74 'name' => 'with_strike_and_point',
75 'class' => App\Module::DEFAULT_CLASS,
77 'args' => new App\Arguments('with-strike.and-point/data/in',
78 'with-strike.and-point/data/in',
79 ['with-strike.and-point', 'data', 'in'],
85 'name' => App\Module::DEFAULT,
86 'class' => App\Module::DEFAULT_CLASS,
88 'args' => new App\Arguments(),
93 'name' => App\Module::DEFAULT,
94 'class' => App\Module::DEFAULT_CLASS,
96 'args' => new App\Arguments(),
101 'name' => App\Module::BACKEND_MODULES[0],
102 'class' => App\Module::DEFAULT_CLASS,
104 'args' => new App\Arguments(App\Module::BACKEND_MODULES[0] . '/data/in',
105 App\Module::BACKEND_MODULES[0] . '/data/in',
106 [App\Module::BACKEND_MODULES[0], 'data', 'in'],
109 'withFirefoxApp' => [
111 'isBackend' => false,
113 'class' => App\Module::DEFAULT_CLASS,
115 'args' => new App\Arguments('users/sign_in',
117 ['users', 'sign_in'],
124 * Test the module name and backend determination
126 * @dataProvider dataModuleName
128 public function testModuleName(array $assert, App\Arguments $args)
130 $module = (new App\Module())->determineModule($args);
132 self::assertModule($assert, $module);
135 public function dataModuleClass()
139 'assert' => App\Module::DEFAULT_CLASS,
140 'name' => App\Module::DEFAULT,
141 'command' => App\Module::DEFAULT,
145 'assert' => LegacyModule::class,
146 // API is one of the last modules to switch from legacy to new BaseModule
147 // so this should be a stable test case until we completely switch ;-)
149 'command' => 'api/test/it',
153 'assert' => HostMeta::class,
155 'command' => '.well-known/host-meta',
159 'assert' => PageNotFound::class,
161 'command' => 'invalid',
168 * Test the determination of the module class
170 * @dataProvider dataModuleClass
172 public function testModuleClass($assert, string $name, string $command, bool $privAdd)
174 $config = Mockery::mock(IConfig::class);
175 $config->shouldReceive('get')->with('config', 'private_addons', false)->andReturn($privAdd)->atMost()->once();
177 $l10n = Mockery::mock(L10n::class);
178 $l10n->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
180 $cache = Mockery::mock(ICache::class);
181 $cache->shouldReceive('get')->with('routerDispatchData')->andReturn('')->atMost()->once();
182 $cache->shouldReceive('get')->with('lastRoutesFileModifiedTime')->andReturn('')->atMost()->once();
183 $cache->shouldReceive('set')->withAnyArgs()->andReturn(false)->atMost()->twice();
185 $router = (new App\Router([], __DIR__ . '/../../../static/routes.config.php', $l10n, $cache));
187 $module = (new App\Module($name))->determineClass(new App\Arguments('', $command), $router, $config);
189 self::assertEquals($assert, $module->getClassName());
193 * Test that modules are immutable
195 public function testImmutable()
197 $module = new App\Module();
199 $moduleNew = $module->determineModule(new App\Arguments());
201 self::assertNotSame($moduleNew, $module);