From 2ec13f5d50fc7885c6c1b0007fd998294d40927e Mon Sep 17 00:00:00 2001 From: Alexandr Chernyaev Date: Sun, 24 Nov 2024 01:00:54 +0300 Subject: [PATCH] Fix spam detector handling of unreadable special characters --- app/Services/SpamDetector.php | 4 ++++ tests/Unit/SpamDetectorTest.php | 6 ++++++ 2 files changed, 10 insertions(+) diff --git a/app/Services/SpamDetector.php b/app/Services/SpamDetector.php index 96a6a056..70c37171 100644 --- a/app/Services/SpamDetector.php +++ b/app/Services/SpamDetector.php @@ -105,6 +105,10 @@ public function hasTooManySpecialCharacters() ->squish() ->wordCount(); + if($countWords === 0) { + return true; + } + $diff = ($withSpecialCharacters - $withOutSpecialCharacters) / 2; // Proportion of special characters in the message diff --git a/tests/Unit/SpamDetectorTest.php b/tests/Unit/SpamDetectorTest.php index 22f9d8b7..4de6756d 100644 --- a/tests/Unit/SpamDetectorTest.php +++ b/tests/Unit/SpamDetectorTest.php @@ -144,4 +144,10 @@ public function testMessageOneSpecialCharacters(): void $spamDetector = new SpamDetector('πŸ”₯'); $this->assertTrue($spamDetector->hasTooManySpecialCharacters()); } + + public function testMessageSpecialCharacters(): void + { + $spamDetector = new SpamDetector('βœ… πŸ”€πŸ”€πŸ”€πŸ”€ πŸ”€πŸ”€πŸ”€πŸ”€πŸ”€πŸ”€ βœ… πŸ” πŸ” πŸ” πŸ” πŸ”  πŸ”  πŸ” πŸ” πŸ” πŸ” πŸ” πŸ” πŸ”  πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯ πŸ”₯πŸ”₯πŸ”₯πŸ”₯πŸ”₯ β™¦οΈπŸ”ΊπŸ”Ίβ™¦οΈβ™¦οΈ β™¦οΈπŸ”ΊπŸ”ΊπŸ”Ί 4️⃣2️⃣5οΈβƒ£πŸ’² πŸ”‘ πŸ”‘πŸ”‘πŸ”‘πŸ”‘ βšͺ️βšͺοΈπŸ”΄βšͺ️βšͺοΈπŸ”΄ πŸ”΄βšͺ️βšͺ️ πŸ”€πŸ”€πŸ”€πŸ”€πŸ”€πŸ”€ πŸ”€ πŸ”€πŸ”€'); + $this->assertTrue($spamDetector->hasTooManySpecialCharacters()); + } }