CommonTest.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. <?php
  2. declare(strict_types = 1);
  3. // {{{ License
  4. // This file is part of GNU social - https://www.gnu.org/software/social
  5. //
  6. // GNU social is free software: you can redistribute it and/or modify
  7. // it under the terms of the GNU Affero General Public License as published by
  8. // the Free Software Foundation, either version 3 of the License, or
  9. // (at your option) any later version.
  10. //
  11. // GNU social is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. // GNU Affero General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU Affero General Public License
  17. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  18. // }}}
  19. namespace App\Tests\Util;
  20. use App\Core\DB\DB;
  21. use App\Core\Security;
  22. use App\Entity\Actor;
  23. use App\Entity\LocalUser;
  24. use App\Util\Common;
  25. use App\Util\Exception\NoLoggedInUser;
  26. use App\Util\GNUsocialTestCase;
  27. use Doctrine\ORM\EntityManager;
  28. use Doctrine\ORM\Mapping\ClassMetadataFactory;
  29. use Doctrine\ORM\Mapping\ClassMetadataInfo;
  30. use Jchook\AssertThrows\AssertThrows;
  31. use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface;
  32. use Symfony\Component\HttpFoundation\ParameterBag;
  33. use Symfony\Component\HttpFoundation\Request;
  34. use Symfony\Component\Security\Core\Security as SSecurity;
  35. class CommonTest extends GNUsocialTestCase
  36. {
  37. use AssertThrows;
  38. public function testSetConfig()
  39. {
  40. $conf = ['test' => ['hydrogen' => 'helium']];
  41. $cb = $this->createMock(ContainerBagInterface::class);
  42. static::assertTrue($cb instanceof ContainerBagInterface);
  43. $cb->method('get')
  44. ->willReturnMap([['gnusocial', $conf], ['gnusocial_defaults', $conf]]);
  45. Common::setupConfig($cb);
  46. if ($exists = file_exists(INSTALLDIR . '/social.local.yaml')) {
  47. copy(INSTALLDIR . '/social.local.yaml', INSTALLDIR . '/social.local.yaml.back_test');
  48. } else {
  49. touch(INSTALLDIR . '/social.local.yaml');
  50. }
  51. static::assertSame('helium', Common::config('test', 'hydrogen'));
  52. Common::setConfig('test', 'hydrogen', 'lithium');
  53. static::assertSame('lithium', Common::config('test', 'hydrogen'));
  54. static::assertSame($conf, Common::getConfigDefaults());
  55. unlink(INSTALLDIR . '/social.local.yaml.back');
  56. if ($exists) {
  57. rename(INSTALLDIR . '/social.local.yaml.back_test', INSTALLDIR . '/social.local.yaml');
  58. }
  59. }
  60. public function testSetRequestAndRoute()
  61. {
  62. $req = $this->createMock(Request::class);
  63. $req->attributes = $this->createMock(ParameterBag::class);
  64. $req->attributes->method('get')->willReturn('test_route');
  65. Common::setRequest($req);
  66. static::assertSame('test_route', Common::route());
  67. static::assertTrue(Common::isRoute('test_route'));
  68. }
  69. /**
  70. * Test Common::user, Common::actor and such. Requires a lot of setup
  71. */
  72. public function testUserAndActorGetters()
  73. {
  74. $client = static::createClient();
  75. static::assertNull(Common::user());
  76. static::assertThrows(NoLoggedInUser::class, fn () => Common::ensureLoggedIn());
  77. static::assertFalse(Common::isLoggedIn());
  78. $metadata = $this->createMock(ClassMetadataInfo::class);
  79. $metadata->method('getTableName')->willReturn('actor');
  80. $metadata->method('getMetadataValue')->willReturn('App\Entity\Actor');
  81. $factory = $this->createMock(ClassMetadataFactory::class);
  82. $factory->method('getAllMetadata')->willReturn([$metadata]);
  83. $actor = Actor::create(['nickname' => 'nick']);
  84. $actor->setId(0);
  85. $em = $this->createMock(EntityManager::class);
  86. $em->method('find')->willReturn($actor);
  87. $em->method('getMetadataFactory')->willReturn($factory);
  88. DB::setManager($em);
  89. DB::initTableMap();
  90. $user = LocalUser::create(['nickname' => 'nick']);
  91. $user->setId(0);
  92. $sec = $this->getMockBuilder(SSecurity::class)->setConstructorArgs([self::$kernel->getContainer()])->getMock();
  93. $sec->method('getUser')->willReturn($user);
  94. Security::setHelper($sec);
  95. // $cookies = $client->loginUser($user)->getCookieJar();
  96. // $cookies->get('MOCKSESSID')->getValue();
  97. static::assertSame($user, Common::user());
  98. static::assertSame($actor, Common::actor());
  99. static::assertSame('nick', Common::userNickname());
  100. static::assertSame(0, Common::userId());
  101. static::assertSame($user, Common::ensureLoggedIn());
  102. static::assertTrue(Common::isLoggedIn());
  103. }
  104. public function testIsSystemPath()
  105. {
  106. static::bootKernel();
  107. static::assertTrue(Common::isSystemPath('main/login'));
  108. static::assertTrue(Common::isSystemPath('main/all'));
  109. static::assertFalse(Common::isSystemPath('non-existent-path'));
  110. }
  111. public function testArrayDiffRecursive()
  112. {
  113. static::assertSame(['foo'], Common::arrayDiffRecursive(['foo'], ['bar']));
  114. static::assertSame([], Common::arrayDiffRecursive(['foo'], ['foo']));
  115. // array_diff(['foo' => []], ['foo' => 'bar']) >>> Array to string conversion
  116. static::assertSame([], Common::arrayDiffRecursive(['foo' => []], ['foo' => 'bar']));
  117. static::assertSame([], Common::arrayDiffRecursive(['foo' => ['bar']], ['foo' => ['bar']]));
  118. static::assertSame(['foo' => [1 => 'quux']], Common::arrayDiffRecursive(['foo' => ['bar', 'quux']], ['foo' => ['bar']]));
  119. static::assertSame([], Common::arrayDiffRecursive(
  120. ['hydrogen' => ['helium' => ['lithium'], 'boron' => 'carbon']],
  121. ['hydrogen' => ['helium' => ['lithium'], 'boron' => 'carbon']],
  122. ));
  123. static::assertSame(
  124. ['hydrogen' => ['helium' => ['lithium']]],
  125. Common::arrayDiffRecursive(
  126. ['hydrogen' => ['helium' => ['lithium'], 'boron' => 'carbon']],
  127. ['hydrogen' => ['helium' => ['beryllium'], 'boron' => 'carbon']],
  128. ),
  129. );
  130. }
  131. public function testArrayRemoveKeys()
  132. {
  133. static::assertSame([1 => 'helium'], Common::arrayRemoveKeys(['hydrogen', 'helium'], [0]));
  134. static::assertSame(['helium' => 'bar'], Common::arrayRemoveKeys(['hydrogen' => 'foo', 'helium' => 'bar'], ['hydrogen']));
  135. }
  136. public function testSizeStrToInt()
  137. {
  138. static::assertSame(1024 ** 0, Common::sizeStrToInt('1'));
  139. static::assertSame(1024 ** 1, Common::sizeStrToInt('1K'));
  140. static::assertSame(1024 ** 2, Common::sizeStrToInt('1M'));
  141. static::assertSame(3 * 1024 ** 2, Common::sizeStrToInt(''));
  142. static::assertSame(1024 ** 3, Common::sizeStrToInt('1G'));
  143. static::assertSame(1024 ** 4, Common::sizeStrToInt('1T'));
  144. static::assertSame(1024 ** 5, Common::sizeStrToInt('1P'));
  145. static::assertSame(128, Common::sizeStrToInt('128'));
  146. static::assertSame(128 * 1024, Common::sizeStrToInt('128K'));
  147. static::assertSame(128 * 1024, Common::sizeStrToInt('128.5K'));
  148. }
  149. public function testGetPreferredPhpUploadLimit()
  150. {
  151. // These limits can only be set in the config files
  152. // $post_max_size = ini_set('post_max_size', Common::sizeStrToInt('6M'));
  153. // $upload_max_filesize = ini_set('upload_max_filesize', Common::sizeStrToInt('1M'));
  154. $memory_limit = ini_set('memory_limit', (string) Common::sizeStrToInt('128M'));
  155. // 2M is the default for upload_max_filesize, the lowest considered
  156. static::assertSame(Common::sizeStrToInt('2M'), Common::getPreferredPhpUploadLimit());
  157. // ini_set('post_max_size', $post_max_size);
  158. // ini_set('upload_max_filesize', $upload_max_filesize);
  159. ini_set('memory_limit', $memory_limit);
  160. }
  161. public function testClamp()
  162. {
  163. static::assertSame(2, Common::clamp(value: 2, min: 0, max: 3));
  164. static::assertSame(2, Common::clamp(value: 2, min: 2, max: 3));
  165. static::assertSame(1, Common::clamp(value: 2, min: 0, max: 1));
  166. static::assertSame(3, Common::clamp(value: 2, min: 3, max: 5));
  167. static::assertSame(3.5, Common::clamp(value: 2.75, min: 3.5, max: 5.1));
  168. }
  169. public function testIsValidHttpUrl()
  170. {
  171. static::assertFalse(Common::isValidHttpUrl(''));
  172. static::assertTrue(Common::isValidHttpUrl('http://gnu.org'));
  173. static::assertFalse(Common::isValidHttpUrl('http://gnu.org', ensure_secure: true));
  174. static::assertTrue(Common::isValidHttpUrl('https://gnu.org'));
  175. static::assertTrue(Common::isValidHttpUrl('https://gnu.org', ensure_secure: true));
  176. }
  177. }