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\Core\Lock\ILock;
29 use Friendica\LegacyModule;
30 use Friendica\Module\HTTPException\PageNotFound;
31 use Friendica\Module\WellKnown\HostMeta;
32 use Friendica\Test\DatabaseTest;
35 class ModuleTest extends DatabaseTest
37 private function assertModule(array $assert, App\Module $module)
39 self::assertEquals($assert['isBackend'], $module->isBackend());
40 self::assertEquals($assert['name'], $module->getName());
41 self::assertEquals($assert['class'], $module->getClassName());
45 * Test the default module mode
47 public function testDefault()
49 $module = new App\Module();
53 'name' => App\Module::DEFAULT,
54 'class' => App\Module::DEFAULT_CLASS,
58 public function dataModuleName()
65 'class' => App\Module::DEFAULT_CLASS,
67 'args' => new App\Arguments('network/data/in',
69 ['network', 'data', 'in'],
72 'withStrikeAndPoint' => [
75 'name' => 'with_strike_and_point',
76 'class' => App\Module::DEFAULT_CLASS,
78 'args' => new App\Arguments('with-strike.and-point/data/in',
79 'with-strike.and-point/data/in',
80 ['with-strike.and-point', 'data', 'in'],
86 'name' => App\Module::DEFAULT,
87 'class' => App\Module::DEFAULT_CLASS,
89 'args' => new App\Arguments(),
94 'name' => App\Module::DEFAULT,
95 'class' => App\Module::DEFAULT_CLASS,
97 'args' => new App\Arguments(),
102 'name' => App\Module::BACKEND_MODULES[0],
103 'class' => App\Module::DEFAULT_CLASS,
105 'args' => new App\Arguments(App\Module::BACKEND_MODULES[0] . '/data/in',
106 App\Module::BACKEND_MODULES[0] . '/data/in',
107 [App\Module::BACKEND_MODULES[0], 'data', 'in'],
110 'withFirefoxApp' => [
112 'isBackend' => false,
114 'class' => App\Module::DEFAULT_CLASS,
116 'args' => new App\Arguments('users/sign_in',
118 ['users', 'sign_in'],
125 * Test the module name and backend determination
127 * @dataProvider dataModuleName
129 public function testModuleName(array $assert, App\Arguments $args)
131 $module = (new App\Module())->determineModule($args);
133 self::assertModule($assert, $module);
136 public function dataModuleClass()
140 'assert' => App\Module::DEFAULT_CLASS,
141 'name' => App\Module::DEFAULT,
142 'command' => App\Module::DEFAULT,
146 'assert' => LegacyModule::class,
147 // API is one of the last modules to switch from legacy to new BaseModule
148 // so this should be a stable test case until we completely switch ;-)
150 'command' => 'api/test/it',
154 'assert' => HostMeta::class,
156 'command' => '.well-known/host-meta',
160 'assert' => PageNotFound::class,
162 'command' => 'invalid',
169 * Test the determination of the module class
171 * @dataProvider dataModuleClass
173 public function testModuleClass($assert, string $name, string $command, bool $privAdd)
175 $config = Mockery::mock(IConfig::class);
176 $config->shouldReceive('get')->with('config', 'private_addons', false)->andReturn($privAdd)->atMost()->once();
178 $l10n = Mockery::mock(L10n::class);
179 $l10n->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
181 $cache = Mockery::mock(ICache::class);
182 $cache->shouldReceive('get')->with('routerDispatchData')->andReturn('')->atMost()->once();
183 $cache->shouldReceive('get')->with('lastRoutesFileModifiedTime')->andReturn('')->atMost()->once();
184 $cache->shouldReceive('set')->withAnyArgs()->andReturn(false)->atMost()->twice();
186 $lock = Mockery::mock(ILock::class);
187 $lock->shouldReceive('acquire')->andReturn(true);
188 $lock->shouldReceive('isLocked')->andReturn(false);
190 $router = (new App\Router([], __DIR__ . '/../../../static/routes.config.php', $l10n, $cache, $lock));
192 $module = (new App\Module($name))->determineClass(new App\Arguments('', $command), $router, $config);
194 self::assertEquals($assert, $module->getClassName());
198 * Test that modules are immutable
200 public function testImmutable()
202 $module = new App\Module();
204 $moduleNew = $module->determineModule(new App\Arguments());
206 self::assertNotSame($moduleNew, $module);