From bbf51ff1efbcfdc5e57d8e16e687e77e6bc28f31 Mon Sep 17 00:00:00 2001 From: Alessandro Lai Date: Thu, 25 May 2023 09:05:19 +0200 Subject: [PATCH] Fix PHP-CS-Fixer cache ignore --- .gitignore | 2 +- src/Controller/ContactController.php | 7 +---- src/Service/NullSpamCheckService.php | 6 ++-- src/Service/SpamCheckService.php | 13 +++++--- src/Service/SpamCheckServiceInterface.php | 6 ++-- tests/Controller/ContactControllerTest.php | 35 ++++++++++------------ 6 files changed, 32 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index 7e8695e0e..716fa956e 100644 --- a/.gitignore +++ b/.gitignore @@ -12,4 +12,4 @@ vendor/.git node_modules vendor diff.txt -.php_cs.cache +.php-cs-fixer.cache diff --git a/src/Controller/ContactController.php b/src/Controller/ContactController.php index 491770593..d57bb3ae7 100644 --- a/src/Controller/ContactController.php +++ b/src/Controller/ContactController.php @@ -82,14 +82,9 @@ public function contact(Request $request, PDO $db): void throw new Exception($message, Http::BAD_REQUEST); } - $clientIP = $request->getClientIP(); - $userAgent = $request->getClientUserAgent(); - if ( ! is_string($data['comment']) - || ! is_string($clientIP) - || ! is_string($userAgent) - || !$this->spamCheckService->isCommentAcceptable($data['comment'], $clientIP, $userAgent) + || !$this->spamCheckService->isCommentAcceptable($data['comment'], $request->getClientIP(), $request->getClientUserAgent()) ) { throw new Exception("Comment failed spam check", Http::BAD_REQUEST); } diff --git a/src/Service/NullSpamCheckService.php b/src/Service/NullSpamCheckService.php index 7c8ecea70..03ed6e62f 100644 --- a/src/Service/NullSpamCheckService.php +++ b/src/Service/NullSpamCheckService.php @@ -8,12 +8,12 @@ class NullSpamCheckService implements SpamCheckServiceInterface * Check your comment against the spam check service * * @param string $comment - * @param string $userIp - * @param string $userAgent + * @param string|null $userIp + * @param string|null $userAgent * * @return bool true if the comment is okay, false if it got rated as spam */ - public function isCommentAcceptable(string $comment, string $userIp, string $userAgent): bool + public function isCommentAcceptable(string $comment, ?string $userIp, ?string $userAgent): bool { return true; } diff --git a/src/Service/SpamCheckService.php b/src/Service/SpamCheckService.php index a51a32f1e..ffad31169 100644 --- a/src/Service/SpamCheckService.php +++ b/src/Service/SpamCheckService.php @@ -30,16 +30,21 @@ public function __construct(ClientInterface $httpClient, $apiKey, $blog) /** * Check your comment against the spam check service * - * @see https://akismet.crom/development/api/#comment-check + * @see https://akismet.com/development/api/#comment-check * * @param string $comment - * @param string $userIp - * @param string $userAgent + * @param string|null $userIp + * @param string|null $userAgent * * @return bool true if the comment is okay, false if it got rated as spam */ - public function isCommentAcceptable(string $comment, string $userIp, string $userAgent) + public function isCommentAcceptable(string $comment, ?string $userIp, ?string $userAgent): bool { + if (null === $userIp) { + // IP address is a required field: https://akismet.com/comment-check/ + return false; + } + if ('' === trim($comment)) { return false; } diff --git a/src/Service/SpamCheckServiceInterface.php b/src/Service/SpamCheckServiceInterface.php index 95aee110d..e6c5dcf02 100644 --- a/src/Service/SpamCheckServiceInterface.php +++ b/src/Service/SpamCheckServiceInterface.php @@ -8,10 +8,10 @@ interface SpamCheckServiceInterface * Check your comment against the spam check service * * @param string $comment - * @param string $userIp - * @param string $userAgent + * @param string|null $userIp + * @param string|null $userAgent * * @return bool true if the comment is okay, false if it got rated as spam */ - public function isCommentAcceptable(string $comment, string $userIp, string $userAgent); + public function isCommentAcceptable(string $comment, ?string $userIp, ?string $userAgent): bool; } diff --git a/tests/Controller/ContactControllerTest.php b/tests/Controller/ContactControllerTest.php index cf2d75d3f..750eb4458 100644 --- a/tests/Controller/ContactControllerTest.php +++ b/tests/Controller/ContactControllerTest.php @@ -20,22 +20,22 @@ final class ContactControllerTest extends TestCase * * @param bool $isClientPermittedPasswordGrant * @param array $returnValueMap - * @param bool $isCommentAcceptable - * @param null|class-string<\Throwable> $expectedException - * @param null|string $expectedExceptionMessage - * @param bool $spamShouldBeChecked - * @param bool $emailShouldBeSent + * @param bool $isCommentAcceptable + * @param class-string<\Throwable>|null $expectedException + * @param string|null $expectedExceptionMessage + * @param bool $spamShouldBeChecked + * @param bool $emailShouldBeSent * * @throws Exception */ public function testContactWorksAsExpected( bool $isClientPermittedPasswordGrant, array $returnValueMap = [], - $isCommentAcceptable = false, - $expectedException = null, - $expectedExceptionMessage = null, - $spamShouldBeChecked = false, - $emailShouldBeSent = false + bool $isCommentAcceptable = false, + string $expectedException = null, + string $expectedExceptionMessage = null, + bool $spamShouldBeChecked = false, + bool $emailShouldBeSent = false ): void { $request = $this->getMockBuilder(Request::class)->disableOriginalConstructor()->getMock(); @@ -105,8 +105,7 @@ public function testContactWorksAsExpected( public function dataProvider(): array { return [ - //Client cannot use the contactform - [ + 'Client cannot use the contactform' => [ 'isClientPermittedPasswordGrant' => false, 'returnValueMap' => [], 'isCommentAcceptable' => false, @@ -115,8 +114,7 @@ public function dataProvider(): array 'emailShouldBeSent' => false, 'spamShouldBeChecked' => false ], - //Not all required fields are set - [ + 'Not all required fields are set' => [ 'isClientPermittedPasswordGrant' => true, 'returnValueMap' => [ ['client_id', '', 'client_id'], @@ -130,8 +128,7 @@ public function dataProvider(): array 'exceptedException' => Exception::class, 'expectedExceptionMessage' => "The fields 'name', 'email', 'subject', 'comment' are required", ], - //Spamcheck fails - [ + 'Spamcheck fails' => [ 'isClientPermittedPasswordGrant' => true, 'returnValueMap' => [ ['client_id', '', 'client_id'], @@ -147,8 +144,7 @@ public function dataProvider(): array 'expectedExceptionMessage' => 'Comment failed spam check', 'spamShouldBeChecked' => true, ], - //Email is sent without spamcheck - [ + 'Email is sent without spamcheck' => [ 'isClientPermittedPasswordGrant' => true, 'returnValueMap' => [ ['client_id', '', 'client_id'], @@ -164,8 +160,7 @@ public function dataProvider(): array 'spamShouldBeChecked' => true, 'emailShouldBeSent' => true ], - //All is good email should be sent - [ + 'All is good email should be sent' => [ 'isClientPermittedPasswordGrant' => true, 'returnValueMap' => [ ['client_id', '', 'client_id'],