Files
FOSSBilling/tests-legacy/modules/Spamchecker/ServiceTest.php
T
Adam DaleyandCopilot a0b3158b30 PHPUnit test-legacy refactor and cleanup (#3144)
* PHPUnit test-legacy cleanup

Refactor and cleanup the legacy PHPUnit tests.

* Apply suggestions from code review

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-01-13 17:32:09 +00:00

128 lines
4.0 KiB
PHP

<?php
declare(strict_types=1);
namespace Box\Mod\Spamchecker;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
use Symfony\Component\HttpFoundation\Request;
#[Group('Core')]
final class ServiceTest extends \BBTestCase
{
protected ?Service $service;
public function setUp(): void
{
$this->service = new Service();
}
public function testGetDi(): void
{
$di = $this->getDi();
$this->service->setDi($di);
$getDi = $this->service->getDi();
$this->assertEquals($di, $getDi);
}
public function testOnBeforeClientSignUp(): void
{
$spamCheckerService = $this->createMock(Service::class);
$spamCheckerService->expects($this->atLeastOnce())
->method('isBlockedIp');
$spamCheckerService->expects($this->atLeastOnce())
->method('isSpam');
$di = $this->getDi();
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $spamCheckerService);
$boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()
->getMock();
$boxEventMock->expects($this->atLeastOnce())
->method('getDi')
->willReturn($di);
$this->service->onBeforeClientSignUp($boxEventMock);
}
public function testOnBeforeGuestPublicTicketOpen(): void
{
$spamCheckerService = $this->createMock(Service::class);
$spamCheckerService->expects($this->atLeastOnce())
->method('isBlockedIp');
$spamCheckerService->expects($this->atLeastOnce())
->method('isSpam');
$di = $this->getDi();
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $spamCheckerService);
$boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()
->getMock();
$boxEventMock->expects($this->atLeastOnce())
->method('getDi')
->willReturn($di);
$this->service->onBeforeGuestPublicTicketOpen($boxEventMock);
}
public function testIsBlockedIpIpNotBlocked(): void
{
$clientIp = '214.1.4.99';
$modConfig = [
'block_ips' => true,
'blocked_ips' => '1.1.1.1' . PHP_EOL . '2.2.2.2',
];
$di = $this->getDi();
$di['request'] = Request::createFromGlobals();
$di['mod_config'] = $di->protect(function ($modName) use ($modConfig) {
if ($modName == 'Spamchecker') {
return $modConfig;
}
});
$boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()
->getMock();
$boxEventMock->expects($this->atLeastOnce())
->method('getDi')
->willReturn($di);
$this->service->isBlockedIp($boxEventMock);
}
public function testIsBlockedIpBlockIpsNotEnabled(): void
{
$modConfig = [
'block_ips' => false,
];
$di = $this->getDi();
$di['mod_config'] = $di->protect(function ($modName) use ($modConfig) {
if ($modName == 'Spamchecker') {
return $modConfig;
}
});
$boxEventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()
->getMock();
$boxEventMock->expects($this->atLeastOnce())
->method('getDi')
->willReturn($di);
$this->service->isBlockedIp($boxEventMock);
}
public function dataProviderSpamResponses(): array
{
return [
[
'{"success" : "true", "username" : {"appears" : "true" }}', 'Your username is blacklisted in the Stop Forum Spam database',
],
[
'{"success" : "true", "email" : {"appears" : "true" }}', 'Your e-mail is blacklisted in the Stop Forum Spam database',
],
[
'{"success" : "true", "ip" : {"appears" : "true" }}', 'Your IP address is blacklisted in the Stop Forum Spam database',
],
];
}
}