friendica/tests/src/App/ModuleControllerTest.php

220 lines
6.4 KiB
PHP
Raw Normal View History

2019-08-12 18:13:58 +02:00
<?php
2020-02-09 15:45:36 +01:00
/**
2021-03-29 08:40:20 +02:00
* @copyright Copyright (C) 2010-2021, the Friendica project
2020-02-09 15:45:36 +01:00
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
2019-08-12 18:13:58 +02:00
namespace Friendica\Test\src\App;
use Dice\Dice;
2019-08-12 18:13:58 +02:00
use Friendica\App;
2021-10-26 21:44:29 +02:00
use Friendica\Core\Cache\Capability\ICanCache;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\Core\L10n;
2021-10-26 21:44:29 +02:00
use Friendica\Core\Lock\Capability\ICanLock;
2019-08-12 18:13:58 +02:00
use Friendica\LegacyModule;
use Friendica\Module\HTTPException\PageNotFound;
2019-08-12 18:13:58 +02:00
use Friendica\Module\WellKnown\HostMeta;
use Friendica\Test\DatabaseTest;
use Mockery;
2019-08-12 18:13:58 +02:00
class ModuleControllerTest extends DatabaseTest
2019-08-12 18:13:58 +02:00
{
private function assertModule(array $assert, App\ModuleController $module)
2019-08-12 18:13:58 +02:00
{
self::assertEquals($assert['isBackend'], $module->isBackend());
self::assertEquals($assert['name'], $module->getName());
self::assertEquals($assert['class'], $module->getModule());
2019-08-12 18:13:58 +02:00
}
/**
* Test the default module mode
*/
public function testDefault()
{
$module = new App\ModuleController();
2019-08-12 18:13:58 +02:00
$defaultClass = App\ModuleController::DEFAULT_CLASS;
self::assertModule([
2019-08-12 18:13:58 +02:00
'isBackend' => false,
'name' => App\ModuleController::DEFAULT,
'class' => null,
2019-08-12 18:13:58 +02:00
], $module);
}
public function dataModuleName()
{
$defaultClass = App\ModuleController::DEFAULT_CLASS;
2019-08-12 18:13:58 +02:00
return [
'default' => [
'assert' => [
'isBackend' => false,
'name' => 'network',
'class' => new $defaultClass(),
2019-08-12 18:13:58 +02:00
],
'args' => new App\Arguments('network/data/in',
'network/data/in',
['network', 'data', 'in'],
3),
],
'withStrikeAndPoint' => [
'assert' => [
'isBackend' => false,
'name' => 'with_strike_and_point',
'class' => new $defaultClass(),
2019-08-12 18:13:58 +02:00
],
'args' => new App\Arguments('with-strike.and-point/data/in',
'with-strike.and-point/data/in',
['with-strike.and-point', 'data', 'in'],
3),
],
'withNothing' => [
'assert' => [
'isBackend' => false,
'name' => App\ModuleController::DEFAULT,
'class' => new $defaultClass(),
2019-08-12 18:13:58 +02:00
],
'args' => new App\Arguments(),
],
'withIndex' => [
'assert' => [
'isBackend' => false,
'name' => App\ModuleController::DEFAULT,
'class' => new $defaultClass(),
2019-08-12 18:13:58 +02:00
],
'args' => new App\Arguments(),
],
'withBackendMod' => [
2019-08-12 18:13:58 +02:00
'assert' => [
'isBackend' => true,
'name' => App\ModuleController::BACKEND_MODULES[0],
'class' => new $defaultClass(),
2019-08-12 18:13:58 +02:00
],
'args' => new App\Arguments(App\ModuleController::BACKEND_MODULES[0] . '/data/in',
App\ModuleController::BACKEND_MODULES[0] . '/data/in',
[App\ModuleController::BACKEND_MODULES[0], 'data', 'in'],
2019-08-12 18:13:58 +02:00
3),
],
'withFirefoxApp' => [
'assert' => [
'isBackend' => false,
'name' => 'login',
'class' => new $defaultClass(),
2019-08-12 18:13:58 +02:00
],
'args' => new App\Arguments('users/sign_in',
'users/sign_in',
['users', 'sign_in'],
3),
],
];
}
/**
* Test the module name and backend determination
*
* @dataProvider dataModuleName
*/
public function testModuleName(array $assert, App\Arguments $args)
2019-08-12 18:13:58 +02:00
{
$module = (new App\ModuleController())->determineName($args);
2019-08-12 18:13:58 +02:00
self::assertModule($assert, $module);
2019-08-12 18:13:58 +02:00
}
public function dataModuleClass()
{
return [
'default' => [
'assert' => App\ModuleController::DEFAULT_CLASS,
'name' => App\ModuleController::DEFAULT,
'command' => App\ModuleController::DEFAULT,
2019-08-12 18:13:58 +02:00
'privAdd' => false,
'args' => [Mockery::mock(L10n::class)],
2019-08-12 18:13:58 +02:00
],
'legacy' => [
'assert' => LegacyModule::class,
2021-11-08 08:32:46 +01:00
'name' => 'display',
'command' => 'display/test/it',
2019-08-12 18:13:58 +02:00
'privAdd' => false,
'args' => [Mockery::mock(L10n::class), __DIR__ . '/../../datasets/legacy/legacy.php'],
2019-08-12 18:13:58 +02:00
],
'new' => [
'assert' => HostMeta::class,
'not_required',
'command' => '.well-known/host-meta',
'privAdd' => false,
'args' => [Mockery::mock(L10n::class)],
2019-08-12 18:13:58 +02:00
],
'404' => [
'assert' => PageNotFound::class,
'name' => 'invalid',
'command' => 'invalid',
'privAdd' => false,
'args' => [Mockery::mock(L10n::class)],
2019-08-12 18:13:58 +02:00
]
];
}
/**
* Test the determination of the module class
*
* @dataProvider dataModuleClass
*/
2021-11-15 00:01:16 +01:00
public function testModuleClass($assert, string $name, string $command, bool $privAdd, array $args)
2019-08-12 18:13:58 +02:00
{
2021-10-26 21:44:29 +02:00
$config = Mockery::mock(IManageConfigValues::class);
2019-08-12 18:13:58 +02:00
$config->shouldReceive('get')->with('config', 'private_addons', false)->andReturn($privAdd)->atMost()->once();
$l10n = Mockery::mock(L10n::class);
$l10n->shouldReceive('t')->andReturnUsing(function ($args) { return $args; });
2021-10-26 21:44:29 +02:00
$cache = Mockery::mock(ICanCache::class);
$cache->shouldReceive('get')->with('routerDispatchData')->andReturn('')->atMost()->once();
$cache->shouldReceive('get')->with('lastRoutesFileModifiedTime')->andReturn('')->atMost()->once();
$cache->shouldReceive('set')->withAnyArgs()->andReturn(false)->atMost()->twice();
2021-10-26 21:44:29 +02:00
$lock = Mockery::mock(ICanLock::class);
2021-07-25 06:56:40 +02:00
$lock->shouldReceive('acquire')->andReturn(true);
$lock->shouldReceive('isLocked')->andReturn(false);
2021-07-25 06:42:34 +02:00
$router = (new App\Router([], __DIR__ . '/../../../static/routes.config.php', $l10n, $cache, $lock));
$dice = Mockery::mock(Dice::class);
2021-11-15 00:01:16 +01:00
$dice->shouldReceive('create')->andReturn(new $assert(...$args));
$module = (new App\ModuleController($name))->determineClass(new App\Arguments('', $command), $router, $config, $dice);
2019-08-12 18:13:58 +02:00
self::assertEquals($assert, $module->getModule()->getClassName());
2019-08-12 18:13:58 +02:00
}
/**
* Test that modules are immutable
*/
public function testImmutable()
{
$module = new App\ModuleController();
2019-08-12 18:13:58 +02:00
$moduleNew = $module->determineName(new App\Arguments());
2019-08-12 18:13:58 +02:00
self::assertNotSame($moduleNew, $module);
2019-08-12 18:13:58 +02:00
}
}