From abda73b8e3335d9b94d84adb8e9af121d0ebe581 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Sun, 2 May 2021 20:42:25 +0000 Subject: [PATCH] [TESTS] Raise App\Util\Common test coverage to 100% --- src/Util/Common.php | 2 +- tests/Util/CommonTest.php | 96 +++++++++++++++++++++++++++++++++++---- 2 files changed, 88 insertions(+), 10 deletions(-) diff --git a/src/Util/Common.php b/src/Util/Common.php index 76ffba55e0..74b7eeeeef 100644 --- a/src/Util/Common.php +++ b/src/Util/Common.php @@ -94,7 +94,7 @@ abstract class Common return self::ensureLoggedIn()->getNickname(); } - public static function userId(): ?string + public static function userId(): ?int { return self::ensureLoggedIn()->getId(); } diff --git a/tests/Util/CommonTest.php b/tests/Util/CommonTest.php index 731638dbff..61661d230b 100644 --- a/tests/Util/CommonTest.php +++ b/tests/Util/CommonTest.php @@ -19,16 +19,28 @@ namespace App\Tests\Util; +use App\Core\DB\DB; use App\Core\Event; use App\Core\Router\Router; +use App\Core\Security; +use App\Entity\GSActor; +use App\Entity\LocalUser; use App\Util\Common; +use App\Util\Exception\NoLoggedInUser; +use Doctrine\ORM\EntityManager; +use Doctrine\ORM\Mapping\ClassMetadataFactory; +use Doctrine\ORM\Mapping\ClassMetadataInfo; +use Jchook\AssertThrows\AssertThrows; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; +use Symfony\Component\Security\Core\Security as SSecurity; class CommonTest extends WebTestCase { + use AssertThrows; + public function testSetConfig() { $conf = ['test' => ['hydrogen' => 'helium']]; @@ -47,6 +59,7 @@ class CommonTest extends WebTestCase static::assertSame('helium', Common::config('test', 'hydrogen')); Common::setConfig('test', 'hydrogen', 'lithium'); static::assertSame('lithium', Common::config('test', 'hydrogen')); + static::assertSame($conf, Common::getConfigDefaults()); unlink(INSTALLDIR . '/social.local.yaml.back'); if ($exists) { @@ -54,6 +67,47 @@ class CommonTest extends WebTestCase } } + /** + * Test Common::user, Common::actor and such. Requires a lot of setup + */ + public function testUserAndActorGetters() + { + $client = static::createClient(); + $sec = $this->getMockBuilder(SSecurity::class)->setConstructorArgs([self::$kernel->getContainer()])->getMock(); + Security::setHelper($sec, null); + static::assertNull(Common::user()); + static::assertThrows(NoLoggedInUser::class, fn () => Common::ensureLoggedIn()); + static::assertFalse(Common::isLoggedIn()); + + $metadata = $this->createMock(ClassMetadataInfo::class); + $metadata->method('getTableName')->willReturn('gsactor'); + $metadata->method('getMetadataValue')->willReturn('App\Entity\GSActor'); + $factory = $this->createMock(ClassMetadataFactory::class); + $factory->method('getAllMetadata')->willReturn([$metadata]); + $actor = GSActor::create(['nickname' => 'nick']); + $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()); + } + public function testIsSystemPath() { static::bootKernel(); @@ -91,15 +145,29 @@ class CommonTest extends WebTestCase public function testSizeStrToInt() { - static::assertSame(pow(1024, 0), Common::sizeStrToInt('1')); - static::assertSame(pow(1024, 1), Common::sizeStrToInt('1K')); - static::assertSame(pow(1024, 2), Common::sizeStrToInt('1M')); - static::assertSame(pow(1024, 3), Common::sizeStrToInt('1G')); - static::assertSame(pow(1024, 4), Common::sizeStrToInt('1T')); - static::assertSame(pow(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')); + static::assertSame(pow(1024, 0), Common::sizeStrToInt('1')); + static::assertSame(pow(1024, 1), Common::sizeStrToInt('1K')); + static::assertSame(pow(1024, 2), Common::sizeStrToInt('1M')); + static::assertSame(3 * pow(1024, 2), Common::sizeStrToInt('')); + static::assertSame(pow(1024, 3), Common::sizeStrToInt('1G')); + static::assertSame(pow(1024, 4), Common::sizeStrToInt('1T')); + static::assertSame(pow(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')); + } + + public function testGetPreferredPhpUploadLimit() + { + $post_max_size = ini_set('post_max_size', 1); + $upload_max_filesize = ini_set('upload_max_filesize', 1); + $memory_limit = ini_set('memory_limit', 1); + + static::assertSame(1, Common::getPreferredPhpUploadLimit()); + + ini_set('post_max_size', $post_max_size); + ini_set('upload_max_filesize', $upload_max_filesize); + ini_set('memory_limit', $memory_limit); } public function testClamp() @@ -108,5 +176,15 @@ class CommonTest extends WebTestCase 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)); + 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)); } }