2021-04-10 01:27:19 +01:00
|
|
|
<?php
|
|
|
|
|
2021-10-10 09:26:18 +01:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2021-04-10 01:27:19 +01:00
|
|
|
// {{{ License
|
|
|
|
// This file is part of GNU social - https://www.gnu.org/software/social
|
|
|
|
//
|
|
|
|
// GNU social 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.
|
|
|
|
//
|
|
|
|
// GNU social 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 GNU social. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// }}}
|
|
|
|
|
2021-04-10 22:57:00 +01:00
|
|
|
namespace App\Tests\Util;
|
2021-04-10 01:27:19 +01:00
|
|
|
|
2021-05-02 21:42:25 +01:00
|
|
|
use App\Core\DB\DB;
|
|
|
|
use App\Core\Security;
|
2021-09-18 03:22:27 +01:00
|
|
|
use App\Entity\Actor;
|
2021-05-02 21:42:25 +01:00
|
|
|
use App\Entity\LocalUser;
|
2021-04-10 01:27:19 +01:00
|
|
|
use App\Util\Common;
|
2021-05-02 21:42:25 +01:00
|
|
|
use App\Util\Exception\NoLoggedInUser;
|
2021-05-06 22:56:28 +01:00
|
|
|
use App\Util\GNUsocialTestCase;
|
2021-05-02 21:42:25 +01:00
|
|
|
use Doctrine\ORM\EntityManager;
|
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataFactory;
|
|
|
|
use Doctrine\ORM\Mapping\ClassMetadataInfo;
|
|
|
|
use Jchook\AssertThrows\AssertThrows;
|
2021-04-10 01:27:19 +01:00
|
|
|
use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
|
2021-07-20 15:08:43 +01:00
|
|
|
use Symfony\Component\HttpFoundation\ParameterBag;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2021-05-02 21:42:25 +01:00
|
|
|
use Symfony\Component\Security\Core\Security as SSecurity;
|
2021-04-10 01:27:19 +01:00
|
|
|
|
2021-05-06 22:56:28 +01:00
|
|
|
class CommonTest extends GNUsocialTestCase
|
2021-04-10 01:27:19 +01:00
|
|
|
{
|
2021-05-02 21:42:25 +01:00
|
|
|
use AssertThrows;
|
|
|
|
|
2021-04-10 01:27:19 +01:00
|
|
|
public function testSetConfig()
|
|
|
|
{
|
|
|
|
$conf = ['test' => ['hydrogen' => 'helium']];
|
|
|
|
$cb = $this->createMock(ContainerBagInterface::class);
|
|
|
|
static::assertTrue($cb instanceof ContainerBagInterface);
|
|
|
|
$cb->method('get')
|
2021-10-10 09:26:18 +01:00
|
|
|
->willReturnMap([['gnusocial', $conf], ['gnusocial_defaults', $conf]]);
|
2021-04-10 01:27:19 +01:00
|
|
|
Common::setupConfig($cb);
|
|
|
|
|
|
|
|
if ($exists = file_exists(INSTALLDIR . '/social.local.yaml')) {
|
|
|
|
copy(INSTALLDIR . '/social.local.yaml', INSTALLDIR . '/social.local.yaml.back_test');
|
|
|
|
} else {
|
|
|
|
touch(INSTALLDIR . '/social.local.yaml');
|
|
|
|
}
|
|
|
|
|
|
|
|
static::assertSame('helium', Common::config('test', 'hydrogen'));
|
|
|
|
Common::setConfig('test', 'hydrogen', 'lithium');
|
|
|
|
static::assertSame('lithium', Common::config('test', 'hydrogen'));
|
2021-05-02 21:42:25 +01:00
|
|
|
static::assertSame($conf, Common::getConfigDefaults());
|
2021-04-10 01:27:19 +01:00
|
|
|
|
|
|
|
unlink(INSTALLDIR . '/social.local.yaml.back');
|
|
|
|
if ($exists) {
|
|
|
|
rename(INSTALLDIR . '/social.local.yaml.back_test', INSTALLDIR . '/social.local.yaml');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-20 15:08:43 +01:00
|
|
|
public function testSetRequestAndRoute()
|
|
|
|
{
|
|
|
|
$req = $this->createMock(Request::class);
|
|
|
|
$req->attributes = $this->createMock(ParameterBag::class);
|
|
|
|
$req->attributes->method('get')->willReturn('test_route');
|
|
|
|
Common::setRequest($req);
|
|
|
|
static::assertSame('test_route', Common::route());
|
|
|
|
static::assertTrue(Common::isRoute('test_route'));
|
|
|
|
}
|
|
|
|
|
2021-05-02 21:42:25 +01:00
|
|
|
/**
|
|
|
|
* Test Common::user, Common::actor and such. Requires a lot of setup
|
|
|
|
*/
|
|
|
|
public function testUserAndActorGetters()
|
|
|
|
{
|
|
|
|
$client = static::createClient();
|
|
|
|
static::assertNull(Common::user());
|
|
|
|
static::assertThrows(NoLoggedInUser::class, fn () => Common::ensureLoggedIn());
|
|
|
|
static::assertFalse(Common::isLoggedIn());
|
|
|
|
|
|
|
|
$metadata = $this->createMock(ClassMetadataInfo::class);
|
2021-09-18 03:22:27 +01:00
|
|
|
$metadata->method('getTableName')->willReturn('actor');
|
|
|
|
$metadata->method('getMetadataValue')->willReturn('App\Entity\Actor');
|
2021-05-02 21:42:25 +01:00
|
|
|
$factory = $this->createMock(ClassMetadataFactory::class);
|
|
|
|
$factory->method('getAllMetadata')->willReturn([$metadata]);
|
2021-09-18 03:22:27 +01:00
|
|
|
$actor = Actor::create(['nickname' => 'nick']);
|
2021-05-02 21:42:25 +01:00
|
|
|
$actor->setId(0);
|
|
|
|
$em = $this->createMock(EntityManager::class);
|
|
|
|
$em->method('find')->willReturn($actor);
|
|
|
|
$em->method('getMetadataFactory')->willReturn($factory);
|
|
|
|
DB::setManager($em);
|
|
|
|
DB::initTableMap();
|
|
|
|
$user = LocalUser::create(['nickname' => 'nick']);
|
|
|
|
$user->setId(0);
|
|
|
|
$sec = $this->getMockBuilder(SSecurity::class)->setConstructorArgs([self::$kernel->getContainer()])->getMock();
|
|
|
|
$sec->method('getUser')->willReturn($user);
|
|
|
|
Security::setHelper($sec, null);
|
|
|
|
|
|
|
|
// $cookies = $client->loginUser($user)->getCookieJar();
|
|
|
|
// $cookies->get('MOCKSESSID')->getValue();
|
|
|
|
|
|
|
|
static::assertSame($user, Common::user());
|
|
|
|
static::assertSame($actor, Common::actor());
|
|
|
|
static::assertSame('nick', Common::userNickname());
|
|
|
|
static::assertSame(0, Common::userId());
|
|
|
|
static::assertSame($user, Common::ensureLoggedIn());
|
|
|
|
static::assertTrue(Common::isLoggedIn());
|
|
|
|
}
|
|
|
|
|
2021-04-10 01:27:19 +01:00
|
|
|
public function testIsSystemPath()
|
|
|
|
{
|
|
|
|
static::bootKernel();
|
|
|
|
|
|
|
|
static::assertTrue(Common::isSystemPath('login'));
|
|
|
|
static::assertFalse(Common::isSystemPath('non-existent-path'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testArrayDiffRecursive()
|
|
|
|
{
|
2021-04-10 22:57:00 +01:00
|
|
|
static::assertSame(['foo'], Common::arrayDiffRecursive(['foo'], ['bar']));
|
|
|
|
static::assertSame([], Common::arrayDiffRecursive(['foo'], ['foo']));
|
2021-04-10 01:27:19 +01:00
|
|
|
// array_diff(['foo' => []], ['foo' => 'bar']) >>> Array to string conversion
|
2021-04-10 22:57:00 +01:00
|
|
|
static::assertSame([], Common::arrayDiffRecursive(['foo' => []], ['foo' => 'bar']));
|
|
|
|
static::assertSame([], Common::arrayDiffRecursive(['foo' => ['bar']], ['foo' => ['bar']]));
|
|
|
|
static::assertSame(['foo' => [1 => 'quux']], Common::arrayDiffRecursive(['foo' => ['bar', 'quux']], ['foo' => ['bar']]));
|
2021-10-10 09:26:18 +01:00
|
|
|
static::assertSame([], Common::arrayDiffRecursive(
|
|
|
|
['hydrogen' => ['helium' => ['lithium'], 'boron' => 'carbon']],
|
|
|
|
['hydrogen' => ['helium' => ['lithium'], 'boron' => 'carbon']],
|
|
|
|
));
|
|
|
|
static::assertSame(
|
|
|
|
['hydrogen' => ['helium' => ['lithium']]],
|
|
|
|
Common::arrayDiffRecursive(
|
|
|
|
['hydrogen' => ['helium' => ['lithium'], 'boron' => 'carbon']],
|
|
|
|
['hydrogen' => ['helium' => ['beryllium'], 'boron' => 'carbon']],
|
|
|
|
),
|
|
|
|
);
|
2021-04-10 01:27:19 +01:00
|
|
|
}
|
2021-04-10 22:57:00 +01:00
|
|
|
|
|
|
|
public function testArrayRemoveKeys()
|
|
|
|
{
|
|
|
|
static::assertSame([1 => 'helium'], Common::arrayRemoveKeys(['hydrogen', 'helium'], [0]));
|
|
|
|
static::assertSame(['helium' => 'bar'], Common::arrayRemoveKeys(['hydrogen' => 'foo', 'helium' => 'bar'], ['hydrogen']));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSizeStrToInt()
|
|
|
|
{
|
2021-10-10 09:26:18 +01:00
|
|
|
static::assertSame(1024 ** 0, Common::sizeStrToInt('1'));
|
|
|
|
static::assertSame(1024 ** 1, Common::sizeStrToInt('1K'));
|
|
|
|
static::assertSame(1024 ** 2, Common::sizeStrToInt('1M'));
|
|
|
|
static::assertSame(3 * 1024 ** 2, Common::sizeStrToInt(''));
|
|
|
|
static::assertSame(1024 ** 3, Common::sizeStrToInt('1G'));
|
|
|
|
static::assertSame(1024 ** 4, Common::sizeStrToInt('1T'));
|
|
|
|
static::assertSame(1024 ** 5, Common::sizeStrToInt('1P'));
|
|
|
|
static::assertSame(128, Common::sizeStrToInt('128'));
|
|
|
|
static::assertSame(128 * 1024, Common::sizeStrToInt('128K'));
|
|
|
|
static::assertSame(128 * 1024, Common::sizeStrToInt('128.5K'));
|
2021-05-02 21:42:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetPreferredPhpUploadLimit()
|
|
|
|
{
|
2021-08-19 13:46:04 +01:00
|
|
|
// These limits can only be set in the config files
|
|
|
|
// $post_max_size = ini_set('post_max_size', Common::sizeStrToInt('6M'));
|
|
|
|
// $upload_max_filesize = ini_set('upload_max_filesize', Common::sizeStrToInt('1M'));
|
|
|
|
$memory_limit = ini_set('memory_limit', Common::sizeStrToInt('128M'));
|
2021-05-02 21:42:25 +01:00
|
|
|
|
2021-08-19 13:46:04 +01:00
|
|
|
// 2M is the default for upload_max_filesize, the lowest considered
|
|
|
|
static::assertSame(Common::sizeStrToInt('2M'), Common::getPreferredPhpUploadLimit());
|
2021-05-02 21:42:25 +01:00
|
|
|
|
2021-08-19 13:46:04 +01:00
|
|
|
// ini_set('post_max_size', $post_max_size);
|
|
|
|
// ini_set('upload_max_filesize', $upload_max_filesize);
|
2021-05-02 21:42:25 +01:00
|
|
|
ini_set('memory_limit', $memory_limit);
|
2021-04-10 22:57:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testClamp()
|
|
|
|
{
|
|
|
|
static::assertSame(2, Common::clamp(value: 2, min: 0, max: 3));
|
|
|
|
static::assertSame(2, Common::clamp(value: 2, min: 2, max: 3));
|
|
|
|
static::assertSame(1, Common::clamp(value: 2, min: 0, max: 1));
|
|
|
|
static::assertSame(3, Common::clamp(value: 2, min: 3, max: 5));
|
2021-05-02 21:42:25 +01:00
|
|
|
static::assertSame(3.5, Common::clamp(value: 2.75, min: 3.5, max: 5.1));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsValidHttpUrl()
|
|
|
|
{
|
|
|
|
static::assertFalse(Common::isValidHttpUrl(''));
|
|
|
|
static::assertTrue(Common::isValidHttpUrl('http://gnu.org'));
|
|
|
|
static::assertFalse(Common::isValidHttpUrl('http://gnu.org', ensure_secure: true));
|
|
|
|
static::assertTrue(Common::isValidHttpUrl('https://gnu.org'));
|
|
|
|
static::assertTrue(Common::isValidHttpUrl('https://gnu.org', ensure_secure: true));
|
2021-04-10 22:57:00 +01:00
|
|
|
}
|
2021-04-10 01:27:19 +01:00
|
|
|
}
|