Files
FOSSBilling/tests-legacy/modules/Activity/ServiceTest.php
T
65a028ef1e Normalize Massmailer filters and migrate to Doctrine entities (#3360)
* Normalize and validate Massmailer filters

* Migrate Massmailer to Doctrine entities

* Add strict typing to Massmailer API and Service

* Use DBAL/Doctrine for modules and add `ExtensionMeta`

* Fix PHPDoc `@return` type mismatch in Massmailer Admin API (#3361)

* Initial plan

* Fix PHPDoc @return type mismatch in Massmailer Admin API

Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>
Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/bc7567ec-98df-4f54-83b9-06906daa43f1

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>

* Update src/modules/Redirect/Service.php

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

* Update tests-legacy/modules/Massmailer/ServiceTest.php

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

* Update src/modules/Notification/Service.php

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

* Fix TypeError in Massmailer `create()` when entity ID is null post-flush (#3362)

* Initial plan

* Throw exception when create() returns null ID instead of causing TypeError

Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>
Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/a842fa3a-e1ff-40b8-b038-30b789ff3499

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>

* Fix test assertion for DBAL integer return type in Massmailer ServiceTest (#3364)

* Initial plan

* fix: update test assertion to expect int id from DBAL query

Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>
Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/c18cfc23-4f38-4d1e-8468-03b332a6760d

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>
Co-authored-by: Adam Daley <admdly@users.noreply.github.com>

* Fix `TypeError` in Massmailer `copy()` and `create()` due to nullable ID return (#3363)

* Initial plan

* Fix nullable ID return type mismatch in Massmailer Admin API copy() and create()

Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>
Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/b52c3722-adfe-4772-8bb4-628b1b56e989

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>
Co-authored-by: Adam Daley <admdly@users.noreply.github.com>

---------

Co-authored-by: Copilot <198982749+Copilot@users.noreply.github.com>
Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-03-23 22:27:55 +00:00

179 lines
6.3 KiB
PHP

<?php
declare(strict_types=1);
namespace Box\Tests\Mod\Activity;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DriverManager;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
#[Group('Core')]
final class ServiceTest extends \BBTestCase
{
public function testDi(): void
{
$service = new \Box\Mod\Activity\Service();
$di = $this->createDi($this->createDbalConnection());
$service->setDi($di);
$this->assertEquals($di, $service->getDi());
}
public static function searchFilters(): array
{
return [
[[], 'FROM activity_system ', true],
[['user_filter' => 'only_clients'], 'm.client_id IS NOT NULL', true],
[['user_filter' => 'only_staff'], 'm.admin_id IS NOT NULL', true],
[['priority' => '2'], 'm.priority =', true],
[['search' => 'keyword'], 'm.message LIKE ', true],
[['min_priority' => 6], 'm.priority <= :min_priority', true],
[['priority' => 6], 'm.priority = :priority', true],
[['priority' => 5, 'min_priority' => 3], 'm.priority = :priority', true],
[['priority' => 5, 'min_priority' => 3], 'm.priority <= :min_priority', false],
];
}
#[DataProvider('searchFilters')]
public function testGetSearchQuery(array $filterKey, string $search, bool $expected): void
{
$service = new \Box\Mod\Activity\Service();
$service->setDi($this->createDi($this->createDbalConnection()));
$result = $service->getSearchQuery($filterKey);
$this->assertIsString($result[0]);
$this->assertIsArray($result[1]);
$this->assertSame($expected, str_contains($result[0], $search));
}
public function testLogEventInsertsRow(): void
{
$dbal = $this->createDbalConnection();
$service = new \Box\Mod\Activity\Service();
$service->setDi($this->createDi($dbal));
$service->logEvent([
'client_id' => 3,
'priority' => 4,
'message' => 'Created order',
]);
$row = $dbal->executeQuery('SELECT client_id, priority, message, ip FROM activity_system')->fetchAssociative();
$this->assertSame('3', (string) $row['client_id']);
$this->assertSame('4', (string) $row['priority']);
$this->assertSame('Created order', $row['message']);
$this->assertSame('127.0.0.1', $row['ip']);
}
public function testLogEmail(): void
{
$dbal = $this->createDbalConnection();
$service = new \Box\Mod\Activity\Service();
$service->setDi($this->createDi($dbal));
$result = $service->logEmail('subject', 1, 'sender', 'recipients', 'html', 'text');
$row = $dbal->executeQuery('SELECT subject, client_id FROM activity_client_email')->fetchAssociative();
$this->assertTrue($result);
$this->assertSame('subject', $row['subject']);
$this->assertSame('1', (string) $row['client_id']);
}
public function testToApiArray(): void
{
$dbal = $this->createDbalConnection();
$dbal->insert('client', [
'id' => 1,
'first_name' => 'Ada',
'last_name' => 'Lovelace',
'email' => 'ada@example.com',
]);
$history = new \Model_ActivityClientHistory();
$history->loadBean(new \DummyBean());
$history->id = 5;
$history->client_id = 1;
$history->ip = '127.0.0.1';
$history->created_at = '2026-03-23 10:00:00';
$service = new \Box\Mod\Activity\Service();
$service->setDi($this->createDi($dbal));
$result = $service->toApiArray($history);
$this->assertSame(5, $result['id']);
$this->assertSame('127.0.0.1', $result['ip']);
$this->assertSame('Ada', $result['client']['first_name']);
$this->assertSame('ada@example.com', $result['client']['email']);
}
public function testRmByClientDeletesRows(): void
{
$dbal = $this->createDbalConnection();
$dbal->insert('activity_system', [
'client_id' => 1,
'priority' => 1,
'message' => 'One',
'ip' => '127.0.0.1',
'created_at' => '2026-03-23 10:00:00',
]);
$dbal->insert('activity_system', [
'client_id' => 2,
'priority' => 1,
'message' => 'Two',
'ip' => '127.0.0.1',
'created_at' => '2026-03-23 10:00:00',
]);
$client = new \Model_Client();
$client->loadBean(new \DummyBean());
$client->id = 1;
$service = new \Box\Mod\Activity\Service();
$service->setDi($this->createDi($dbal));
$service->rmByClient($client);
$remaining = (int) $dbal->executeQuery('SELECT COUNT(*) FROM activity_system')->fetchOne();
$this->assertSame(1, $remaining);
}
private function createDi(Connection $dbal): \Pimple\Container
{
$di = $this->getDi();
$di['dbal'] = $dbal;
$extensionService = $this->createMock(\Box\Mod\Extension\Service::class);
$extensionService->method('isExtensionActive')
->willReturn(false);
$request = $this->createMock(\Symfony\Component\HttpFoundation\Request::class);
$request->method('getClientIp')
->willReturn('127.0.0.1');
$di['request'] = $request;
$di['mod_service'] = $di->protect(fn (string $service): object => match ($service) {
'extension' => $extensionService,
default => throw new \RuntimeException("Unexpected service $service"),
});
return $di;
}
private function createDbalConnection(): Connection
{
$connection = DriverManager::getConnection([
'driver' => 'pdo_sqlite',
'memory' => true,
]);
$connection->executeStatement('CREATE TABLE activity_system (id INTEGER PRIMARY KEY AUTOINCREMENT, client_id INTEGER, admin_id INTEGER, priority INTEGER, message TEXT, ip TEXT, created_at TEXT)');
$connection->executeStatement('CREATE TABLE activity_client_email (id INTEGER PRIMARY KEY AUTOINCREMENT, client_id INTEGER, sender TEXT, recipients TEXT, subject TEXT, content_html TEXT, content_text TEXT, created_at TEXT)');
$connection->executeStatement('CREATE TABLE client (id INTEGER PRIMARY KEY, first_name TEXT, last_name TEXT, email TEXT)');
return $connection;
}
}