249f195257
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
145 lines
5.5 KiB
PHP
145 lines
5.5 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace Cf7stg\Tests;
|
|
|
|
use Cf7stg\Classifier;
|
|
|
|
final class ClassifierDropTest extends Cf7stgTestCase
|
|
{
|
|
public function test_has_valid_email_anywhere_finds_gmail(): void
|
|
{
|
|
$fields = ['some-key' => 'pasha@gmail.com'];
|
|
self::assertTrue(Classifier::has_valid_email_anywhere($fields));
|
|
}
|
|
|
|
public function test_has_valid_email_anywhere_finds_in_array_value(): void
|
|
{
|
|
$fields = ['x' => ['noise', 'info@some-domain.com']];
|
|
self::assertTrue(Classifier::has_valid_email_anywhere($fields));
|
|
}
|
|
|
|
public function test_has_valid_email_anywhere_returns_false_for_invalid(): void
|
|
{
|
|
$fields = ['email' => 'not-an-email', 'x' => 'AB'];
|
|
self::assertFalse(Classifier::has_valid_email_anywhere($fields));
|
|
}
|
|
|
|
public function test_has_valid_email_anywhere_returns_false_when_empty(): void
|
|
{
|
|
self::assertFalse(Classifier::has_valid_email_anywhere([]));
|
|
}
|
|
|
|
public function test_has_valid_email_anywhere_finds_email_embedded_in_text(): void
|
|
{
|
|
$fields = ['your-message' => 'reach me at user@domain.com thanks'];
|
|
self::assertTrue(Classifier::has_valid_email_anywhere($fields));
|
|
}
|
|
|
|
public function test_has_valid_email_anywhere_finds_second_email_when_first_invalid(): void
|
|
{
|
|
// First candidate is "alice@in" which is not a valid email; second is valid.
|
|
// Without preg_match_all, the method would return false here.
|
|
$fields = ['msg' => 'try alice@in or bob@example.com'];
|
|
self::assertTrue(Classifier::has_valid_email_anywhere($fields));
|
|
}
|
|
|
|
public function test_has_short_latin_name_true_for_two_letters(): void
|
|
{
|
|
$fields = ['your-name' => 'OB'];
|
|
self::assertTrue(Classifier::has_short_latin_name($fields));
|
|
}
|
|
|
|
public function test_has_short_latin_name_true_for_three_alnum(): void
|
|
{
|
|
$fields = ['name' => 'A1B'];
|
|
self::assertTrue(Classifier::has_short_latin_name($fields));
|
|
}
|
|
|
|
public function test_has_short_latin_name_false_for_long_name(): void
|
|
{
|
|
$fields = ['your-name' => 'Melissa'];
|
|
self::assertFalse(Classifier::has_short_latin_name($fields));
|
|
}
|
|
|
|
public function test_has_short_latin_name_false_for_cyrillic(): void
|
|
{
|
|
$fields = ['your-name' => 'Иван'];
|
|
self::assertFalse(Classifier::has_short_latin_name($fields));
|
|
}
|
|
|
|
public function test_has_short_latin_name_false_when_no_name_field(): void
|
|
{
|
|
// Только телефон, нет name-полей. FieldDetector::find_name пропустит +79991234567 (digits)
|
|
$fields = ['tel-1' => '+79991234567'];
|
|
self::assertFalse(Classifier::has_short_latin_name($fields));
|
|
}
|
|
|
|
public function test_has_short_latin_name_picks_via_field_detector(): void
|
|
{
|
|
// text-211 не в списке известных name-ключей, FieldDetector найдёт 'XY' (no digits, len=2)
|
|
$fields = ['text-211' => 'XY', 'text-212' => '+79991234567'];
|
|
self::assertTrue(Classifier::has_short_latin_name($fields));
|
|
}
|
|
|
|
public function test_has_short_latin_name_true_for_single_letter_boundary(): void
|
|
{
|
|
// Lower boundary of {1,3}: 1 char accepted
|
|
$fields = ['your-name' => 'A'];
|
|
self::assertTrue(Classifier::has_short_latin_name($fields));
|
|
}
|
|
|
|
public function test_has_short_latin_name_false_for_four_letters_boundary(): void
|
|
{
|
|
// Upper boundary of {1,3}: 4 chars rejected
|
|
$fields = ['your-name' => 'ABCD'];
|
|
self::assertFalse(Classifier::has_short_latin_name($fields));
|
|
}
|
|
|
|
public function test_has_short_latin_name_false_for_array_value_with_space_join(): void
|
|
{
|
|
// pick_field joins array values with a space → "OB MU" is 5 chars → no false positive.
|
|
$fields = ['your-name' => ['OB', 'MU']];
|
|
self::assertFalse(Classifier::has_short_latin_name($fields));
|
|
}
|
|
|
|
public function test_count_placeholder_fields_screenshot_1(): void
|
|
{
|
|
$fields = require __DIR__ . '/fixtures/spam-screenshot-1.php';
|
|
// text-785: OB(2), text-787: 70(2), text-786: BP(2), text-788: ZZ(2), text-789: QV(2)
|
|
// text-790: 10 цифр — не placeholder. acceptance-data: служебное → исключено.
|
|
self::assertGreaterThanOrEqual(3, Classifier::count_placeholder_fields($fields));
|
|
}
|
|
|
|
public function test_count_placeholder_fields_ignores_empty(): void
|
|
{
|
|
$fields = ['a' => 'AB', 'b' => '', 'c' => 'XY', 'd' => null, 'e' => 'CD'];
|
|
self::assertSame(3, Classifier::count_placeholder_fields($fields));
|
|
}
|
|
|
|
public function test_count_placeholder_fields_ignores_long_values(): void
|
|
{
|
|
$fields = ['a' => 'AB', 'b' => 'Hello world', 'c' => 'CD'];
|
|
self::assertSame(2, Classifier::count_placeholder_fields($fields));
|
|
}
|
|
|
|
public function test_count_placeholder_fields_ignores_service_keys(): void
|
|
{
|
|
$fields = [
|
|
'acceptance-data' => '1',
|
|
'_wpcf7' => 'AB',
|
|
'_wpcf7_unit_tag' => 'XY',
|
|
'g-recaptcha-response' => 'CD',
|
|
'real' => 'EF',
|
|
];
|
|
self::assertSame(1, Classifier::count_placeholder_fields($fields));
|
|
}
|
|
|
|
public function test_count_placeholder_fields_ignores_non_alnum(): void
|
|
{
|
|
// Кириллица или с пробелами не считается placeholder
|
|
$fields = ['a' => 'Ия', 'b' => 'a b', 'c' => 'XY'];
|
|
self::assertSame(1, Classifier::count_placeholder_fields($fields));
|
|
}
|
|
}
|