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\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;
33 class ModuleTest extends DatabaseTest
35 private function assertModule(array $assert, App\Module $module)
37 $this->assertEquals($assert['isBackend'], $module->isBackend());
38 $this->assertEquals($assert['name'], $module->getName());
39 $this->assertEquals($assert['class'], $module->getClassName());
43 * Test the default module mode
45 public function testDefault()
47 $module = new App\Module();
51 'name' => App\Module::DEFAULT,
52 'class' => App\Module::DEFAULT_CLASS,
56 public function dataModuleName()
63 'class' => App\Module::DEFAULT_CLASS,
65 'args' => new App\Arguments('network/data/in',
67 ['network', 'data', 'in'],
70 'withStrikeAndPoint' => [
73 'name' => 'with_strike_and_point',
74 'class' => App\Module::DEFAULT_CLASS,
76 'args' => new App\Arguments('with-strike.and-point/data/in',
77 'with-strike.and-point/data/in',
78 ['with-strike.and-point', 'data', 'in'],
84 'name' => App\Module::DEFAULT,
85 'class' => App\Module::DEFAULT_CLASS,
87 'args' => new App\Arguments(),
92 'name' => App\Module::DEFAULT,
93 'class' => App\Module::DEFAULT_CLASS,
95 'args' => new App\Arguments(),
100 'name' => App\Module::BACKEND_MODULES[0],
101 'class' => App\Module::DEFAULT_CLASS,
103 'args' => new App\Arguments(App\Module::BACKEND_MODULES[0] . '/data/in',
104 App\Module::BACKEND_MODULES[0] . '/data/in',
105 [App\Module::BACKEND_MODULES[0], 'data', 'in'],
108 'withFirefoxApp' => [
110 'isBackend' => false,
112 'class' => App\Module::DEFAULT_CLASS,
114 'args' => new App\Arguments('users/sign_in',
116 ['users', 'sign_in'],
123 * Test the module name and backend determination
125 * @dataProvider dataModuleName
127 public function testModuleName(array $assert, App\Arguments $args)
129 $module = (new App\Module())->determineModule($args);
131 $this->assertModule($assert, $module);
134 public function dataModuleClass()
138 'assert' => App\Module::DEFAULT_CLASS,
139 'name' => App\Module::DEFAULT,
140 'command' => App\Module::DEFAULT,
144 'assert' => LegacyModule::class,
145 // API is one of the last modules to switch from legacy to new BaseModule
146 // so this should be a stable test case until we completely switch ;-)
148 'command' => 'api/test/it',
152 'assert' => HostMeta::class,
154 'command' => '.well-known/host-meta',
158 'assert' => PageNotFound::class,
160 'command' => 'invalid',
167 * Test the determination of the module class
169 * @dataProvider dataModuleClass
171 public function testModuleClass($assert, string $name, string $command, bool $privAdd)
173 $config = \Mockery::mock(IConfig::class);
174 $config->shouldReceive('get')->with('config', 'private_addons', false)->andReturn($privAdd)->atMost()->once();
176 $l10n = \Mockery::mock(L10n::class);
177 $l10n->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
179 $cache = \Mockery::mock(ICache::class);
180 $cache->shouldReceive('get')->with('routerDispatchData')->andReturn('')->atMost()->once();
181 $cache->shouldReceive('get')->with('lastRoutesFileModifiedTime')->andReturn('')->atMost()->once();
182 $cache->shouldReceive('set')->withAnyArgs()->andReturn(false)->atMost()->twice();
184 $router = (new App\Router([], __DIR__ . '/../../../static/routes.config.php', $l10n, $cache));
186 $module = (new App\Module($name))->determineClass(new App\Arguments('', $command), $router, $config);
188 $this->assertEquals($assert, $module->getClassName());
192 * Test that modules are immutable
194 public function testImmutable()
196 $module = new App\Module();
198 $moduleNew = $module->determineModule(new App\Arguments());
200 $this->assertNotSame($moduleNew, $module);