diff --git a/components/Person/tests/Controller/PersonSettingsTest.php b/components/Person/tests/Controller/PersonSettingsTest.php index 3ebbce4473..df21cf2060 100644 --- a/components/Person/tests/Controller/PersonSettingsTest.php +++ b/components/Person/tests/Controller/PersonSettingsTest.php @@ -33,10 +33,6 @@ class PersonSettingsTest extends GNUsocialTestCase { use AssertThrows; - /** - * @covers \App\Controller\PersonSettings::allSettings - * @covers \App\Controller\PersonSettings::personalInfo - */ public function testPersonalInfo() { $client = static::createClient(); @@ -64,10 +60,6 @@ class PersonSettingsTest extends GNUsocialTestCase // static::assertSame('908555842', $changed_user->getPhoneNumber()->getNationalNumber()); } - /** - * @covers \App\Controller\PersonSettings::account - * @covers \App\Controller\PersonSettings::allSettings - */ public function testEmail() { $client = static::createClient(); @@ -86,10 +78,6 @@ class PersonSettingsTest extends GNUsocialTestCase static::assertSame($changed_user->getIncomingEmail(), 'incoming@provider.any'); } - /** - * @covers \App\Controller\PersonSettings::account - * @covers \App\Controller\PersonSettings::allSettings - */ public function testCorrectPassword() { $client = static::createClient(); @@ -108,10 +96,6 @@ class PersonSettingsTest extends GNUsocialTestCase static::assertTrue($changed_user->checkPassword('this is some test password')); } - /** - * @covers \App\Controller\PersonSettings::account - * @covers \App\Controller\PersonSettings::allSettings - */ public function testAccountWrongPassword() { $client = static::createClient(); @@ -129,11 +113,7 @@ class PersonSettingsTest extends GNUsocialTestCase $this->assertSelectorTextContains('.stacktrace', 'AuthenticationException'); } - // TODO: First actually implement this functionality -// /** -// * @covers \App\Controller\PersonSettings::allSettings -// * @covers \App\Controller\PersonSettings::notifications -// */ +// TODO: First actually implement this functionality // public function testNotifications() // { // $client = static::createClient(); diff --git a/src/Core/Entity.php b/src/Core/Entity.php index 005e2aa200..c0a9adf716 100644 --- a/src/Core/Entity.php +++ b/src/Core/Entity.php @@ -33,6 +33,7 @@ use DateTime; use DateTimeInterface; use Exception; use Functional as F; +use InvalidArgumentException; /** * Base class to all entities, with some utilities @@ -68,6 +69,8 @@ abstract class Entity * properties of $obj with the associative array $args. Doesn't * persist the result * + * @throws InvalidArgumentException + * * @return static */ public static function create(array $args, bool $_delegated_call = false): self diff --git a/src/Core/Form.php b/src/Core/Form.php index a159419867..4142e2a49c 100644 --- a/src/Core/Form.php +++ b/src/Core/Form.php @@ -101,7 +101,7 @@ abstract class Form $name = $form[array_key_last($form)][0]; $r = Common::getRequest(); - $form[] = ['_next', HiddenType::class, ['data' => $r->get('next') ?? $r->get('_next') ?? $r->getRequestUri()]]; + $form[] = ['_next', HiddenType::class, ['data' => $r?->get('next') ?? $r?->get('_next') ?? $r?->getRequestUri()]]; $fb = self::$form_factory->createNamedBuilder($name, $type, data: null, options: array_merge($form_options, ['translation_domain' => false])); foreach ($form as [$key, $class, $options]) { diff --git a/src/Util/Common.php b/src/Util/Common.php index eeb8de3790..b44f9527e9 100644 --- a/src/Util/Common.php +++ b/src/Util/Common.php @@ -67,7 +67,7 @@ abstract class Common self::$request = $req; } - public static function getRequest(): Request + public static function getRequest(): ?Request { return self::$request; } @@ -185,12 +185,9 @@ abstract class Common /** * A recursive `array_diff`, while PHP itself doesn't provide one * - * @param mixed[] $array1 - * @param mixed[] $array2 - * * @return mixed[] */ - public static function arrayDiffRecursive(array $array1, array $array2): array + public static function arrayDiffRecursive(mixed $array1, mixed $array2): array { $diff = []; foreach ($array1 as $key => $value) { @@ -232,7 +229,7 @@ abstract class Common { // `memory_limit` can be -1 and `post_max_size` can be 0 // for unlimited. Consistency. - if (!isset($size) || $size === '-1' || $size === '0') { + if (!isset($size) || $size === '' || $size === '-1' || $size === '0') { $size = '3M'; } diff --git a/src/Util/Form/ActorForms.php b/src/Util/Form/ActorForms.php index ea44cc1134..10a0dd4b93 100644 --- a/src/Util/Form/ActorForms.php +++ b/src/Util/Form/ActorForms.php @@ -117,7 +117,7 @@ class ActorForms } // Delete related cache - $cache_keys = array_intersect(Actor::cacheKeys($target->getId()), array_flip(['id', 'nickname', 'fullname'])); + $cache_keys = array_intersect_key(Actor::cacheKeys($target->getId()), array_flip(['id', 'nickname', 'fullname'])); foreach ($cache_keys as $key) { Cache::delete($key); } diff --git a/src/Util/Formatting.php b/src/Util/Formatting.php index 6c6836243e..f1c5f7de5a 100644 --- a/src/Util/Formatting.php +++ b/src/Util/Formatting.php @@ -209,8 +209,12 @@ abstract class Formatting * * @param static::SPLIT_BY_BOTH|static::SPLIT_BY_COMMA|static::SPLIT_BY_SPACE $split_type */ - public static function toArray(string $input, array &$output, string $split_type = self::SPLIT_BY_COMMA): bool + public static function toArray(string $input, mixed &$output, string $split_type = self::SPLIT_BY_COMMA): bool { + if (!\in_array($split_type, [static::SPLIT_BY_BOTH, static::SPLIT_BY_COMMA, static::SPLIT_BY_SPACE])) { + throw new InvalidArgumentException; + } + if ($input == '') { $output = []; return true; diff --git a/tests/Controller/NoteTest.php b/tests/Controller/NoteTest.php new file mode 100644 index 0000000000..2cb86caf7a --- /dev/null +++ b/tests/Controller/NoteTest.php @@ -0,0 +1,46 @@ +. + +// }}} + +namespace App\Test\Controller; + +use App\Core\DB; +use App\Util\GNUsocialTestCase; + +class NoteTest extends GNUsocialTestCase +{ + public function testNoteView() + { + $client = static::createClient(); + $id = DB::findBy('note', ['neq' => ['content' => null]])[0]->getId(); + dd(DB::findBy('note', ['neq' => ['content' => null]])[0]); + $crawler = $client->request('GET', '/object/note/{$id}'); + $this->assertResponseIsSuccessful(); + // $form = $crawler->selectButton('Sign in')->form(); + $crawler = $client->submitForm('Sign in', [ + '_username' => $nickname, + '_password' => $password, + ]); + $this->assertResponseStatusCodeSame(302); + $crawler = $client->followRedirect(); + } +} diff --git a/tests/Controller/SecurityTest.php b/tests/Controller/SecurityTest.php index 3d064f0291..3b8bc4b4b5 100644 --- a/tests/Controller/SecurityTest.php +++ b/tests/Controller/SecurityTest.php @@ -97,7 +97,7 @@ class SecurityTest extends GNUsocialTestCase public function testRegisterSuccess() { - [$client,] = self::testRegister('new_nickname', 'new_email@provider.any', 'foobar'); + [$client] = self::testRegister('new_nickname', 'new_email@provider.any', 'foobar'); $this->assertResponseStatusCodeSame(302); $crawler = $client->followRedirect(); $this->assertRouteSame('security_login'); @@ -122,7 +122,7 @@ class SecurityTest extends GNUsocialTestCase private function testRegisterPasswordLength(string $password, string $error) { - [$client, ] = self::testRegister('new_nickname', 'email@provider.any', $password); + [$client] = self::testRegister('new_nickname', 'email@provider.any', $password); $this->assertResponseIsSuccessful(); $this->assertSelectorTextContains('.form-error', $error); $this->assertRouteSame('security_register'); @@ -145,7 +145,7 @@ class SecurityTest extends GNUsocialTestCase private function testRegisterNoEmail() { - [$client, ] = self::testRegister('new_nickname', '', 'foobar'); + [$client] = self::testRegister('new_nickname', '', 'foobar'); $this->assertResponseIsSuccessful(); $this->assertSelectorTextContains('.form-error', 'Please enter an email'); $this->assertRouteSame('security_register'); @@ -153,7 +153,7 @@ class SecurityTest extends GNUsocialTestCase private function testRegisterNicknameLength(string $nickname, string $error) { - [$client, ] = self::testRegister($nickname, 'email@provider.any', 'foobar'); + [$client] = self::testRegister($nickname, 'email@provider.any', 'foobar'); $this->assertResponseIsSuccessful(); $this->assertSelectorTextContains('.form-error', $error); $this->assertRouteSame('security_register'); @@ -171,13 +171,19 @@ class SecurityTest extends GNUsocialTestCase public function testRegisterExistingNickname() { - [$client, ] = self::testRegister('taken_user', 'new_new_email@provider.any', 'foobar'); + [$client] = self::testRegister('taken_user', 'new_new_email@provider.any', 'foobar'); $this->assertSelectorTextContains('.stacktrace', 'App\Util\Exception\NicknameTakenException'); } public function testRegisterExistingEmail() { - [$client, ] = self::testRegister('other_new_nickname', 'taken_user@provider.any', 'foobar'); + [$client] = self::testRegister('other_new_nickname', 'taken_user@provider.any', 'foobar'); $this->assertSelectorTextContains('.stacktrace', 'App\Util\Exception\EmailTakenException'); } + + public function testInvalidEmail() + { + [$client] = self::testRegister('nicknameec1038', 'userec1038', 'foobar'); + $this->assertSelectorTextContains('.stacktrace', 'App\Util\Exception\EmailException'); + } } diff --git a/tests/Core/CacheTest.php b/tests/Core/CacheTest.php index c37c5a4ec7..69d1304147 100644 --- a/tests/Core/CacheTest.php +++ b/tests/Core/CacheTest.php @@ -32,9 +32,10 @@ use Symfony\Component\DependencyInjection\ParameterBag\ContainerBagInterface; class CacheTest extends GNUsocialTestCase { // Needs to return something to conform to the interface - private function notCalled(mixed $i): array + public function notCalled(mixed $i): array { static::assertFalse('should not be called'); + static::markTestIncomplete('This should not be called.'); return []; } diff --git a/tests/Core/EntityTest.php b/tests/Core/EntityTest.php index 111876b948..c5f7847d97 100644 --- a/tests/Core/EntityTest.php +++ b/tests/Core/EntityTest.php @@ -25,9 +25,9 @@ namespace App\Test\Core; use App\Core\DB; use App\Entity\LocalUser; +use App\Util\Exception\BugFoundException; use App\Util\GNUsocialTestCase; use BadMethodCallException; -use InvalidArgumentException; use Jchook\AssertThrows\AssertThrows; class EntityTest extends GNUsocialTestCase @@ -46,7 +46,7 @@ class EntityTest extends GNUsocialTestCase { $user = LocalUser::create(['nickname' => 'foo']); static::assertSame('foo', $user->getNickname()); - static::assertThrows(InvalidArgumentException::class, fn () => LocalUser::create(['non_existant_property' => 'bar'])); + static::assertThrows(BugFoundException::class, fn () => LocalUser::create(['non_existant_property' => 'bar'])); } public function testCreateOrUpdate() diff --git a/tests/Core/FormTest.php b/tests/Core/FormTest.php index a3a36f9770..15f8c31249 100644 --- a/tests/Core/FormTest.php +++ b/tests/Core/FormTest.php @@ -61,20 +61,22 @@ class FormTest extends GNUsocialTestCase $form_class = $config->getType()->getInnerType(); - $found = false; - foreach ($form_array as [$array_name, $array_class, $options]) { - if ($name === $array_name) { - $found = true; - static::assertSame(\get_class($form_class), $array_class); - foreach (['label', 'attr', 'data'] as $field) { - if (isset($options[$field])) { - static::assertSame($form_options[$field], $options[$field]); + if ($name !== '_next') { + $found = false; + foreach ($form_array as [$array_name, $array_class, $options]) { + if ($name === $array_name) { + $found = true; + static::assertSame(\get_class($form_class), $array_class); + foreach (['label', 'attr', 'data'] as $field) { + if (isset($options[$field])) { + static::assertSame($form_options[$field], $options[$field]); + } } + break; } - break; } + static::assertTrue($found); } - static::assertTrue($found); static::assertSame(\get_class($f->getParent()), 'Symfony\\Component\\Form\\Form'); } diff --git a/tests/Util/FormattingTest.php b/tests/Util/FormattingTest.php index 2e15d13fb2..80020fc025 100644 --- a/tests/Util/FormattingTest.php +++ b/tests/Util/FormattingTest.php @@ -185,7 +185,7 @@ class FormattingTest extends WebTestCase public function testToArray() { - static::assertThrows(Exception::class, fn () => Formatting::toArray('foo', $a, '')); // @phpstan-ignore-line + static::assertThrows(InvalidArgumentException::class, fn () => Formatting::toArray('foo', $a, '')); // @phpstan-ignore-line static::assertTrue(Formatting::toArray('', $a)); static::assertSame([], $a);