Files
FOSSBilling/tests-legacy/modules/Client/ServiceTest.php
T
a7ef873368 Revamp admin UI and improve dashboard layout and functionality (#3596)
* Adjust admin alerts and installer warning

* Make dashboard cards full-height and align badges

* Split dashboard cards into partials and add AJAX

* Replace status badges with ribbon elements

* Revamp activity panel layout and responsiveness

* Adjust dashboard card spacing and styles

* Style dashboard links and adjust chart heights

* Back-to-top: update classes and styles

* Fix client admin UI labels and flags

* Improve API response parsing and errors

* Mark action columns no-sort, add table-hover

* Update table classes for hover and wrapping

* Refactor admin templates to card-header layout

* Update admin templates layout and semantics

* Tidy admin templates; remove tabs, update notice

* Standardize admin alert styles and messages

* Handle null/empty status in status_name macro

* Validate resolved renewal price before invoicing

* Keep IP actions inline in activity table

* Right-align activity pagination controls

* Align pagination and export buttons in admin lists

* Improve admin pagination & per-page defaults

* Extract record selector into partial

* Add tab deep-linking and URL hash persistence

* Refactor search controls & capitalize 'One Time'

* Refactor client search filters and toggle state

* Refactor activity filters UI and tomselect styling

* Redesign admin filters; capitalize 'On Hold'

* Revamp admin filter panels (Invoice & Support)

* Group search clause in Massmailer query

* Add filters and expanded search for pages/gateways

* Add filter panels, refine search logic, tests

* Add advanced search filters for activity modules

* Add structured filters, date/ip search & tests

* Add Email queue filters and admin tab

* Add collapsible filter panel to Staff settings

* Refactor badges to use shared macros

* Add massmailer statuses & boolean badges

* Add search icon and refine search input layout

* Update admin templates layout and markup

* Use modals for creating clients/groups

* Centralize tab header action buttons

* Centralize toolbar actions; add Order modal

* Bump Composer dependencies (lockfile)

* Capitalize 'One Time' payment label

* Return raw API body; use button for search

* Wrap search button in input-group-text

* Make search max width configurable

* Fix malformed aria-label Twig expression in layout_default.html.twig

Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/c8db6ba9-bda4-413a-989f-e4777e06bd12

Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>

* Fix XSS risk in updateExternalIP: append IP via createTextNode instead of innerHTML concat

Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/0c435e52-1784-4466-a8dc-c41e12443a6f

Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>

* Refactor New Client modal layout and styles

* Limit and style phone country code field

* Use `country_names` helper and `country_name` filter

* Use Symfony Intl for currencies & locales

* Use Symfony Intl for currencies; drop built-in country lists

* Refactor Currency module API, entity and service

* Remove system checkLimits usages from modules and tests

* Remove `currency.title` column and update APIs

* Add inputmode and maxlength to cspell dictionary

* Fix dropdown toggle accessibility: use button instead of anchor

Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/b7f9474b-1988-4c08-aec0-f8991edfea71

Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>

* Improve multi-currency handling and installer

* Log currency code when changing cart currency

* Use removeCurrency in Admin API and update tests

* Use Intl currency symbols and fraction digits

* Use currency filters in templates

* Rename currency title to name and update schema

* Remove deprecated Service::getLanguages

* Capitalize system permission labels and fix messages

* Use Symfony Countries and remove phone codes list

* Validate country codes; use country select

* Set fb_locale cookie and fix admin locale display

* Validate locales and add default locale selector

* Use Locales::getNames; tweak language selector CSS

* Tweak header spacing and make nav links icon-only

* Localize client login texts

* Add phone country-code attrs and styles

* Center .modal-dialog in two themes

* Add phone_codes API to list calling codes

* Make form the modal-content wrapper

* Move create forms to Bootstrap modals

* Convert new-item tabs to modals in admin UI

* Revamp admin UI, embed snippets & modal

* Revamp admin settings UI and templates

* Revamp Formbuilder & Redirect admin UIs

* Add 'Segoe' and 'minmax' to cspell dictionary

* Rename warning() to warn() in test logger

* Refactor admin templates to card-based layout

* Expose initAvatars globally and scoped init

* Add 'view' permissions and enforce checks

* Refactor Servicehosting admin UI and scripts

* Add staff permission checks and module permissions

* Refactor admin templates layout and forms

* Add staff permission checks and module perms

* Add staff permission checks and module perms

* Refactor admin Twig templates and UI

* Refactor client admin UI to use summary/form cards

* Refactor admin invoice/subscription/transaction templates

* Refactor Order admin UI; fix country labels

* Refactor admin templates and add IP country filter

* Add 'view' permissions and checks to admin APIs

* Use detail/related partials in admin templates

* Refactor admin product templates to use partials

* Add endembed to cspell ignoreWords to fix spellcheck CI failure

Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/e8973bef-7364-4b30-8dd8-13701ca15c5d

Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>

* Refactor admin templates to use common partials

* Validate order status, add permission checks

* Center form buttons and add horizontal padding

* Introduce createAdminApi and BBTestCaseContainer

* Refactor admin UI templates and forms

* Run intl compression after composer install

* Disable provenance in buildx outputs

* Revamp admin templates for UI and previews

* Refactor admin templates, pricing UI & styles

* Refactor admin invoice sidebar and SEO settings UI

* Use minor currency units, country list & API fixes

* Rename language selector class to locale-selector

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>
2026-05-24 23:10:28 +01:00

1392 lines
46 KiB
PHP

<?php
declare(strict_types=1);
namespace Box\Tests\Mod\Client;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
#[Group('Core')]
final class ServiceTest extends \BBTestCase
{
public function testGetModulePermissions(): void
{
$service = new \Box\Mod\Client\Service();
$permissions = $service->getModulePermissions();
$this->assertArrayHasKey('create', $permissions);
$this->assertArrayHasKey('edit_profile', $permissions);
$this->assertArrayHasKey('impersonate_login', $permissions);
$this->assertArrayHasKey('manage_api_keys', $permissions);
$this->assertArrayHasKey('change_password', $permissions);
$this->assertArrayHasKey('manage_balance', $permissions);
$this->assertArrayHasKey('view_login_history', $permissions);
$this->assertArrayHasKey('manage_groups', $permissions);
$this->assertArrayHasKey('delete', $permissions);
$this->assertArrayHasKey('bulk_delete', $permissions);
$this->assertArrayHasKey('export', $permissions);
$this->assertArrayHasKey('manage_settings', $permissions);
}
public function testApproveClientEmailByHash(): void
{
$database = $this->createMock('\Box_Database');
$database->expects($this->atLeastOnce())
->method('getRow')->willReturn(['client_id' => 2, 'id' => 1]);
$database->expects($this->atLeastOnce())->method('exec');
$di = $this->getDi();
$di['db'] = $database;
$clientService = new \Box\Mod\Client\Service();
$clientService->setDi($di);
$result = $clientService->approveClientEmailByHash('');
$this->assertTrue($result);
}
public function testApproveClientEmailByHashException(): void
{
$database = $this->createMock('\Box_Database');
$database->expects($this->atLeastOnce())
->method('getRow')->willReturn([]);
$di = $this->getDi();
$di['db'] = $database;
$clientService = new \Box\Mod\Client\Service();
$clientService->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Invalid email confirmation link');
$clientService->approveClientEmailByHash('');
}
public function testGenerateEmailConfirmationLink(): void
{
$model = new \Model_ExtensionMeta();
$model->loadBean(new \DummyBean());
$database = $this->createMock('\Box_Database');
$database->expects($this->atLeastOnce())
->method('dispense')->willReturn($model);
$database->expects($this->atLeastOnce())->method('store')
->willReturn(1);
$toolsMock = $this->createMock(\FOSSBilling\Tools::class);
$toolsMock->expects($this->atLeastOnce())->method('url')
->willReturn('fossbilling.org/index.php/client/confirm-email/');
$di = $this->getDi();
$di['db'] = $database;
$di['tools'] = $toolsMock;
$clientService = new \Box\Mod\Client\Service();
$clientService->setDi($di);
$client_id = 1;
$result = $clientService->generateEmailConfirmationLink($client_id);
$this->assertIsString($result);
$this->assertTrue(str_contains($result, '/client/confirm-email/'));
}
public function testOnAfterClientSignUp(): void
{
$eventParams = ['id' => 1];
$eventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()->getMock();
$eventMock->expects($this->atLeastOnce())->
method('getParameters')->willReturn($eventParams);
$service = $this->createMock(\Box\Mod\Email\Service::class);
$service->expects($this->atLeastOnce())
->method('sendTemplate')
->with($this->callback(function (array $email): bool {
$this->assertArrayNotHasKey('password', $email);
return $email['to_client'] === 1 && $email['code'] === 'mod_client_signup';
}))
->willReturn(true);
$di = $this->getDi();
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $service);
$di['mod_config'] = $di->protect(fn ($name): array => ['require_email_confirmation' => false]);
$eventMock->expects($this->atLeastOnce())
->method('getDi')
->willReturn($di);
$clientService = new \Box\Mod\Client\Service();
$clientService->setDi($di);
$result = $clientService->onAfterClientSignUp($eventMock);
$this->assertTrue($result);
}
public function testRequireEmailConfirmonAfterClientSignUp(): void
{
$eventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()->getMock();
$eventParams = [
'id' => 1,
'require_email_confirmation' => true,
];
$eventMock->expects($this->atLeastOnce())->
method('getParameters')->willReturn($eventParams);
$service = $this->createMock(\Box\Mod\Email\Service::class);
$service->expects($this->atLeastOnce())
->method('sendTemplate')
->with($this->callback(function (array $email): bool {
$this->assertArrayNotHasKey('password', $email);
return $email['to_client'] === 1
&& $email['code'] === 'mod_client_signup'
&& $email['require_email_confirmation'] === true
&& $email['email_confirmation_link'] === 'Link_string';
}))
->willReturn(true);
$clientServiceMock = $this->getMockBuilder(\Box\Mod\Client\Service::class)->onlyMethods(['generateEmailConfirmationLink'])->getMock();
$clientServiceMock->expects($this->atLeastOnce())->
method('generateEmailConfirmationLink')->willReturn('Link_string');
$di = $this->getDi();
$di['mod_service'] = $di->protect(function ($serviceName) use ($service, $clientServiceMock) {
if ($serviceName == 'email') {
return $service;
}
if ($serviceName == 'client') {
return $clientServiceMock;
}
});
$di['mod_config'] = $di->protect(fn ($name): array => ['require_email_confirmation' => true]);
$eventMock->expects($this->atLeastOnce())
->method('getDi')
->willReturn($di);
$clientService = new \Box\Mod\Client\Service();
$result = $clientService->onAfterClientSignUp($eventMock);
$this->assertTrue($result);
}
public function testExceptiononAfterClientSignUp(): void
{
$eventParams = ['id' => 1];
$eventMock = $this->getMockBuilder('\Box_Event')->disableOriginalConstructor()->getMock();
$eventMock->expects($this->atLeastOnce())->
method('getParameters')->willReturn($eventParams);
$service = $this->createMock(\Box\Mod\Email\Service::class);
$service->expects($this->atLeastOnce())->
method('sendTemplate')->will($this->throwException(new \Exception('exception created in unit test')));
$di = $this->getDi();
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $service);
$di['mod_config'] = $di->protect(function ($name): void {
['require_email_confirmation' => false];
});
$eventMock->expects($this->atLeastOnce())
->method('getDi')
->willReturn($di);
$clientService = new \Box\Mod\Client\Service();
$clientService->setDi($di);
$result = $clientService->onAfterClientSignUp($eventMock);
$this->assertTrue($result);
}
public static function searchQueryData(): array
{
return [
[[], 'SELECT c.*', []],
[
['id' => 1],
'(c.id = :client_id OR c.aid = :alt_client_id)',
[':client_id' => '', ':alt_client_id' => ''],
],
[
['name' => 'test'],
'(c.first_name LIKE :first_name or c.last_name LIKE :last_name )',
[':first_name' => '', ':last_name' => ''],
],
[
['email' => 'test@example.com'],
'c.email LIKE :email',
[':email' => 'test@example.com'],
],
[
['company' => 'LTD company'],
'c.company LIKE :company',
[':company' => 'LTD company'],
],
[
['status' => 'TEST status'],
'c.status = :status',
[':status' => 'TEST status'],
],
[
['group_id' => '1'],
'c.client_group_id = :group_id',
[':group_id' => '1'],
],
[
['created_at' => '2012-12-12'],
"DATE_FORMAT(c.created_at, '%Y-%m-%d') = :created_at",
[':created_at' => '2012-12-12'],
],
[
['date_from' => '2012-12-10'],
'UNIX_TIMESTAMP(c.created_at) >= :date_from',
[':date_from' => '2012-12-10'],
],
[
['date_to' => '2012-12-11'],
'UNIX_TIMESTAMP(c.created_at) <= :date_to',
[':date_to' => '2012-12-11'],
],
[
['search' => '2'],
'(c.id = :cid OR c.aid = :caid)',
[':cid' => '2', ':caid' => '2'],
],
[
['search' => 'Keyword'],
"c.company LIKE :s_company OR c.first_name LIKE :s_first_name OR c.last_name LIKE :s_last_name OR c.email LIKE :s_email OR CONCAT(c.first_name, ' ', c.last_name ) LIKE :full_name",
[':s_company' => 'Keyword',
':s_first_name' => 'Keyword',
':s_last_name' => 'Keyword',
':s_email' => 'Keyword',
':full_name' => 'Keyword',
],
],
];
}
#[DataProvider('searchQueryData')]
public function testGetSearchQuery(array $data, string $expectedStr, array $expectedParams): void
{
$clientService = new \Box\Mod\Client\Service();
$result = $clientService->getSearchQuery($data);
$this->assertIsString($result[0]);
$this->assertIsArray($result[1]);
$this->assertTrue(str_contains($result[0], $expectedStr), $result[0]);
$this->assertEquals([], array_diff_key($result[1], $expectedParams));
}
public function testGetSearchQueryChangeSelect(): void
{
$data = [];
$selectStmt = 'c.id, CONCAT(c.first_name, c.last_name) as full_name';
$clientService = new \Box\Mod\Client\Service();
$result = $clientService->getSearchQuery($data, $selectStmt);
$this->assertIsString($result[0]);
$this->assertIsArray($result[1]);
$this->assertTrue(str_contains($result[0], $selectStmt), $result[0]);
}
public function testGetPairs(): void
{
$data = [];
$database = $this->createMock('\Box_Database');
$database->expects($this->atLeastOnce())->method('getAssoc')
->willReturn([]);
$di = $this->getDi();
$di['db'] = $database;
$clientService = new \Box\Mod\Client\Service();
$clientService->setDi($di);
$result = $clientService->getPairs($data);
$this->assertIsArray($result);
}
public function testToSessionArray(): void
{
$expectedArrayKeys = [
'id' => 1,
'email' => 'email@example.com',
'name' => 'John Smith',
'role' => 'admin',
];
$model = new \Model_Client();
$model->loadBean(new \DummyBean());
$clientService = new \Box\Mod\Client\Service();
$result = $clientService->toSessionArray($model);
$this->assertIsArray($result);
$this->assertEquals([], array_diff_key($result, $expectedArrayKeys));
}
public function testEmailAlreadyRegistered(): void
{
$email = 'test@example.com';
$model = new \Model_Client();
$model->loadBean(new \DummyBean());
$database = $this->createMock('\Box_Database');
$database->expects($this->atLeastOnce())->method('findOne')
->willReturn($model);
$di = $this->getDi();
$di['db'] = $database;
$clientService = new \Box\Mod\Client\Service();
$clientService->setDi($di);
$result = $clientService->emailAlreadyRegistered($email);
$this->assertIsBool($result);
}
public function testEmailAlreadyRegWithModel(): void
{
$email = 'test@example.com';
$model = new \Model_Client();
$model->loadBean(new \DummyBean());
$model->email = $email;
$clientService = new \Box\Mod\Client\Service();
$result = $clientService->emailAlreadyRegistered($email, $model);
$this->assertIsBool($result);
$this->assertFalse($result);
}
public function testCanChangeCurrency(): void
{
$currency = 'EUR';
$model = new \Model_Client();
$model->loadBean(new \DummyBean());
$model->currency = 'USD';
$database = $this->createMock('\Box_Database');
$database->expects($this->atLeastOnce())->method('findOne')
->willReturn(null);
$di = $this->getDi();
$di['db'] = $database;
$clientService = new \Box\Mod\Client\Service();
$clientService->setDi($di);
$result = $clientService->canChangeCurrency($model, $currency);
$this->assertIsBool($result);
$this->assertTrue($result);
}
public function testCanChangeCurrencyModelCurrencyNotSet(): void
{
$currency = 'EUR';
$model = new \Model_Client();
$model->loadBean(new \DummyBean());
$database = $this->createMock('\Box_Database');
$database->expects($this->never())->method('findOne');
$clientService = new \Box\Mod\Client\Service();
$result = $clientService->canChangeCurrency($model, $currency);
$this->assertIsBool($result);
$this->assertTrue($result);
}
public function testCanChangeCurrencyIdenticalCurrencies(): void
{
$currency = 'EUR';
$model = new \Model_Client();
$model->loadBean(new \DummyBean());
$model->currency = $currency;
$database = $this->createMock('\Box_Database');
$database->expects($this->never())->method('findOne');
$clientService = new \Box\Mod\Client\Service();
$result = $clientService->canChangeCurrency($model, $currency);
$this->assertIsBool($result);
$this->assertFalse($result);
}
public function testCanChangeCurrencyHasInvoice(): void
{
$currency = 'EUR';
$model = new \Model_Client();
$model->loadBean(new \DummyBean());
$model->id = 1;
$model->currency = 'USD';
$invoiceModel = new \Model_Invoice();
$invoiceModel->loadBean(new \DummyBean());
$database = $this->createMock('\Box_Database');
$database->expects($this->once())
->method('findOne')
->willReturn($invoiceModel);
$di = $this->getDi();
$di['db'] = $database;
$clientService = new \Box\Mod\Client\Service();
$clientService->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Currency cannot be changed. Client already has invoices issued.');
$clientService->canChangeCurrency($model, $currency);
}
/* Disabled due to the random PHPUnit failures
public function testCanChangeCurrencyHasOrder(): void
{
$currency = 'EUR';
$model = new \Model_Client();
$model->loadBean(new \DummyBean());
$model->id = 1;
$model->currency = 'USD';
$clientOrderModel = new \Model_ClientOrder();
$clientOrderModel->loadBean(new \DummyBean());
$database = $this->createMock('\Box_Database');
$database->expects($this->exactly(2))->method('findOne')
->willReturnOnConsecutiveCalls(null, null);
$di = $this->getDi();
$di['db'] = $database;
$clientService = new \Box\Mod\Client\Service();
$clientService->setDi($di);
$clientService->canChangeCurrency($model, $currency);
}
*/
public static function searchBalanceQueryData(): array
{
return [
[[], 'FROM client_balance as m', []],
[
['id' => 1],
'm.id = :id',
[':id' => '1'],
],
[
['client_id' => 1],
'm.client_id = :client_id',
[':client_id' => '1'],
],
[
['date_from' => '2012-12-10'],
'm.created_at >= :date_from',
[':date_from' => '2012-12-10'],
],
[
['date_to' => '2012-12-11'],
'm.created_at <= :date_to',
[':date_to' => '2012-12-11'],
],
];
}
#[DataProvider('searchBalanceQueryData')]
public function testGetBalanceSearchQuery(array $data, string $expectedStr, array $expectedParams): void
{
$di = $this->getDi();
$clientBalanceService = new \Box\Mod\Client\ServiceBalance();
$clientBalanceService->setDi($di);
[$sql, $params] = $clientBalanceService->getSearchQuery($data);
$this->assertNotEmpty($sql);
$this->assertIsString($sql);
$this->assertIsArray($params);
$this->assertTrue(str_contains($sql, $expectedStr), $sql);
$this->assertEquals([], array_diff_key($params, $expectedParams));
}
public function testAddFunds(): void
{
$modelClient = new \Model_Client();
$modelClient->loadBean(new \DummyBean());
$modelClient->currency = 'USD';
$model = new \Model_ClientBalance();
$model->loadBean(new \DummyBean());
$amount = '2.22';
$description = 'test description';
$database = $this->createMock('\Box_Database');
$database->expects($this->atLeastOnce())->method('dispense')
->willReturn($model);
$di = $this->getDi();
$di['db'] = $database;
$clientService = new \Box\Mod\Client\Service();
$clientService->setDi($di);
$result = $clientService->addFunds($modelClient, $amount, $description);
$this->assertTrue($result);
}
public function testAddFundsCurrencyNotDefined(): void
{
$modelClient = new \Model_Client();
$modelClient->loadBean(new \DummyBean());
$amount = '2.22';
$description = 'test description';
$clientService = new \Box\Mod\Client\Service();
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('You must define the client\'s currency before adding funds.');
$clientService->addFunds($modelClient, $amount, $description);
}
public function testAddFundsAmountMissing(): void
{
$modelClient = new \Model_Client();
$modelClient->loadBean(new \DummyBean());
$modelClient->currency = 'USD';
$amount = null;
$description = '';
$clientService = new \Box\Mod\Client\Service();
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Funds amount is invalid');
$clientService->addFunds($modelClient, $amount, $description);
}
public function testAddFundsInvalidDescription(): void
{
$modelClient = new \Model_Client();
$modelClient->loadBean(new \DummyBean());
$modelClient->currency = 'USD';
$amount = '2.22';
$description = null;
$clientService = new \Box\Mod\Client\Service();
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Funds description is invalid');
$result = $clientService->addFunds($modelClient, $amount, $description);
$this->assertTrue($result);
}
public function testGetExpiredPasswordReminders(): void
{
$database = $this->createMock('\Box_Database');
$database->expects($this->atLeastOnce())->method('find')
->willReturn([]);
$di = $this->getDi();
$di['db'] = $database;
$clientService = new \Box\Mod\Client\Service();
$clientService->setDi($di);
$result = $clientService->getExpiredPasswordReminders();
$this->assertIsArray($result);
}
public static function searchHistoryQueryData(): array
{
return [
[[], 'SELECT ach.*, c.first_name, c.last_name, c.email', []],
[
['search' => 'sameValue'],
'c.first_name LIKE :first_name OR c.last_name LIKE :last_name OR c.email LIKE :email OR c.id LIKE :id',
[
':first_name' => '%sameValue%',
':last_name' => '%sameValue%',
':email' => '%sameValue%',
':id' => 'sameValue'],
],
[
['client_id' => '1'],
'ach.client_id = :client_id',
[':client_id' => '1'],
],
[
['id' => '7'],
'ach.id = :event_id',
[':event_id' => 7],
],
[
['ip' => '192.0.2'],
'ach.ip LIKE :ip',
[':ip' => '%192.0.2%'],
],
];
}
#[DataProvider('searchHistoryQueryData')]
public function testGetHistorySearchQuery(array $data, string $expectedStr, array $expectedParams): void
{
$clientService = new \Box\Mod\Client\Service();
$di = $this->getDi();
$clientService->setDi($di);
[$sql, $params] = $clientService->getHistorySearchQuery($data);
$this->assertNotEmpty($sql);
$this->assertIsString($sql);
$this->assertIsArray($params);
$this->assertTrue(str_contains($sql, $expectedStr), $sql);
$this->assertEquals([], array_diff_key($params, $expectedParams));
}
public function testGetHistorySearchQueryWithDateRangeFilters(): void
{
$clientService = new \Box\Mod\Client\Service();
$di = $this->getDi();
$clientService->setDi($di);
[$sql, $params] = $clientService->getHistorySearchQuery([
'date_from' => '2026-05-10',
'date_to' => '2026-05-12',
]);
$this->assertStringContainsString('ach.created_at >= :date_from', $sql);
$this->assertStringContainsString('ach.created_at <= :date_to', $sql);
$this->assertSame('2026-05-10 00:00:00', $params[':date_from']);
$this->assertSame('2026-05-12 23:59:59', $params[':date_to']);
}
public function testCounter(): void
{
$database = $this->createMock('\Box_Database');
$database->expects($this->atLeastOnce())->method('getAssoc')
->willReturn([]);
$di = $this->getDi();
$di['db'] = $database;
$clientService = new \Box\Mod\Client\Service();
$clientService->setDi($di);
$result = $clientService->counter();
$this->assertIsArray($result);
$expected = [
'total' => 0,
\Model_Client::ACTIVE => 0,
\Model_Client::SUSPENDED => 0,
\Model_Client::CANCELED => 0,
];
}
public function testGetGroupPairs(): void
{
$database = $this->createMock('\Box_Database');
$database->expects($this->atLeastOnce())->method('getAssoc')
->willReturn([]);
$di = $this->getDi();
$di['db'] = $database;
$clientService = new \Box\Mod\Client\Service();
$clientService->setDi($di);
$result = $clientService->getGroupPairs();
$this->assertIsArray($result);
}
public function testClientAlreadyExists(): void
{
$model = new \Model_Client();
$model->loadBean(new \DummyBean());
$database = $this->createMock('\Box_Database');
$database->expects($this->atLeastOnce())->method('findOne')
->willReturn($model);
$di = $this->getDi();
$di['db'] = $database;
$clientService = new \Box\Mod\Client\Service();
$clientService->setDi($di);
$result = $clientService->clientAlreadyExists('email@example.com');
$this->assertTrue($result);
}
public function testGetByLoginDetails(): void
{
$model = new \Model_Client();
$model->loadBean(new \DummyBean());
$database = $this->createMock('\Box_Database');
$database->expects($this->atLeastOnce())->method('findOne')
->willReturn($model);
$di = $this->getDi();
$di['db'] = $database;
$clientService = new \Box\Mod\Client\Service();
$clientService->setDi($di);
$result = $clientService->getByLoginDetails('email@example.com', 'password');
$this->assertInstanceOf('\Model_Client', $result);
}
public static function getProvider(): array
{
return [
['id', 1],
['email', 'test@email.com'],
];
}
#[DataProvider('getProvider')]
public function testGet(string $fieldName, int|string $fieldValue): void
{
$model = new \Model_Client();
$model->loadBean(new \DummyBean());
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('findOne')->willReturn($model);
$di = $this->getDi();
$di['db'] = $dbMock;
$service = new \Box\Mod\Client\Service();
$service->setDi($di);
$data = [$fieldName => $fieldValue];
$result = $service->get($data);
$this->assertInstanceOf('\Model_Client', $result);
}
public function testGetClientNotFound(): void
{
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('findOne')->willReturn([]);
$di = $this->getDi();
$di['db'] = $dbMock;
$service = new \Box\Mod\Client\Service();
$service->setDi($di);
$data = ['id' => 0];
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Client not found');
$service->get($data);
}
public function testGetClientBalance(): void
{
$model = new \Model_Client();
$model->loadBean(new \DummyBean());
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('getCell')->willReturn(1.0);
$di = $this->getDi();
$di['db'] = $dbMock;
$service = new \Box\Mod\Client\Service();
$service->setDi($di);
$result = $service->getClientBalance($model);
$this->assertIsNumeric($result);
}
public function testToApiArray(): void
{
$model = new \Model_Client();
$model->loadBean(new \DummyBean());
$model->api_token = 'client-token';
$model->custom_1 = 'custom field';
$clientGroup = new \Model_ClientGroup();
$clientGroup->loadBean(new \DummyBean());
$clientGroup->title = 'Group Title';
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('toArray')->willReturn([]);
$dbMock->expects($this->atLeastOnce())
->method('load')->willReturn($clientGroup);
$di = $this->getDi();
$di['db'] = $dbMock;
$serviceMock = $this->getMockBuilder(\Box\Mod\Client\Service::class)
->onlyMethods(['getClientBalance'])
->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getClientBalance');
$serviceMock->setDi($di);
$result = $serviceMock->toApiArray($model, true, new \Model_Admin());
$this->assertIsArray($result);
$this->assertArrayNotHasKey('api_token', $result);
$result = $serviceMock->toApiArray($model, true, new \Model_Admin(), true);
$this->assertArrayHasKey('api_token', $result);
$this->assertSame('client-token', $result['api_token']);
}
public static function isClientTaxableProvider(): array
{
return [
[
false,
false,
false,
],
[
true,
true,
false,
],
[
true,
false,
true,
],
];
}
#[DataProvider('isClientTaxableProvider')]
public function testIsClientTaxable(bool $getParamValueReturn, bool $tax_exempt, bool $expected): void
{
$service = $this->createMock(\Box\Mod\System\Service::class);
$service->expects($this->atLeastOnce())
->method('getParamValue')
->willReturn($getParamValueReturn);
$di = $this->getDi();
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $service);
$service = new \Box\Mod\Client\Service();
$service->setDi($di);
$client = new \Model_Client();
$client->loadBean(new \DummyBean());
$client->tax_exempt = $tax_exempt;
$result = $service->isClientTaxable($client);
$this->assertEquals($expected, $result);
}
public function testAdminCreateClient(): void
{
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$clientModel->id = 1;
$data = [
'password' => uniqid(),
'email' => 'test@unit.vm',
'first_name' => 'test',
'send_welcome_email' => false,
];
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('dispense')
->willReturn($clientModel);
$dbMock->expects($this->atLeastOnce())
->method('store');
$eventManagerMock = $this->createMock('\Box_EventManager');
$eventManagerMock->expects($this->exactly(2))
->method('fire');
$passwordMock = $this->createMock(\FOSSBilling\PasswordManager::class);
$passwordMock->expects($this->atLeastOnce())
->method('hashIt')
->with($data['password']);
$modMock = $this->getMockBuilder(\FOSSBilling\Module::class)->disableOriginalConstructor()->getMock();
$modMock->expects($this->atLeastOnce())
->method('getConfig')
->willReturn([]);
$di = $this->getDi();
$di['db'] = $dbMock;
$di['events_manager'] = $eventManagerMock;
$di['logger'] = new \Box_Log();
$di['mod'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $modMock);
$di['password'] = $passwordMock;
$service = new \Box\Mod\Client\Service();
$service->setDi($di);
$result = $service->adminCreateClient($data);
$this->assertIsInt($result);
}
public function testAdminCreateClientSendsWelcomeEmail(): void
{
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$clientModel->id = 1;
$clientModel->status = \Model_Client::ACTIVE;
$passwordReset = new \Model_ClientPasswordReset();
$passwordReset->loadBean(new \DummyBean());
$data = [
'password' => 'StrongPass123',
'email' => 'test@unit.vm',
'first_name' => 'test',
'status' => \Model_Client::ACTIVE,
'send_welcome_email' => true,
];
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->exactly(2))
->method('dispense')
->willReturnOnConsecutiveCalls($clientModel, $passwordReset);
$dbMock->expects($this->exactly(2))
->method('store');
$dbMock->expects($this->once())
->method('findOne')
->with('ClientPasswordReset', 'client_id = ?', [$clientModel->id])
->willReturn(null);
$eventManagerMock = $this->createMock('\Box_EventManager');
$eventManagerMock->expects($this->exactly(2))
->method('fire');
$passwordMock = $this->createMock(\FOSSBilling\PasswordManager::class);
$passwordMock->expects($this->once())
->method('hashIt')
->with($data['password']);
$modMock = $this->getMockBuilder(\FOSSBilling\Module::class)->disableOriginalConstructor()->getMock();
$modMock->expects($this->once())
->method('getConfig')
->willReturn([]);
$emailService = $this->createMock(\Box\Mod\Email\Service::class);
$emailService->expects($this->once())
->method('sendTemplate')
->with($this->callback(fn (array $email): bool => $email['to_client'] === 1
&& $email['code'] === 'mod_client_signup_admin'
&& is_string($email['hash'])
&& strlen($email['hash']) === 64
&& $email['send_now'] === true
&& $email['require_email_confirmation'] === false));
$di = $this->getDi();
$di['db'] = $dbMock;
$di['events_manager'] = $eventManagerMock;
$di['logger'] = new \Box_Log();
$di['mod'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $modMock);
$di['mod_config'] = $di->protect(fn (): array => []);
$di['mod_service'] = $di->protect(fn (string $name): \PHPUnit\Framework\MockObject\MockObject => match (strtolower($name)) {
'email' => $emailService,
});
$di['password'] = $passwordMock;
$di['tools'] = new \FOSSBilling\Tools();
$service = new \Box\Mod\Client\Service();
$service->setDi($di);
$result = $service->adminCreateClient($data);
$this->assertSame(1, $result);
}
public function testCreatePasswordResetRequestForClientStoresRequestIp(): void
{
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$clientModel->id = 123;
$clientModel->ip = '198.51.100.10';
$passwordReset = new \Model_ClientPasswordReset();
$passwordReset->loadBean(new \DummyBean());
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->once())
->method('findOne')
->with('ClientPasswordReset', 'client_id = ?', [$clientModel->id])
->willReturn(null);
$dbMock->expects($this->once())
->method('dispense')
->with('ClientPasswordReset')
->willReturn($passwordReset);
$dbMock->expects($this->once())
->method('store')
->with($this->callback(static fn (\Model_ClientPasswordReset $reset): bool => $reset->ip === '203.0.113.42'));
$request = new class {
public function getClientIp(): string
{
return '203.0.113.42';
}
};
$di = $this->getDi();
$di['db'] = $dbMock;
$di['request'] = $request;
$service = new \Box\Mod\Client\Service();
$service->setDi($di);
$hash = $service->createPasswordResetRequestForClient($clientModel);
$this->assertSame(64, strlen($hash));
}
public function testDeleteGroup(): void
{
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('findOne');
$dbMock->expects($this->once())
->method('trash');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['logger'] = new \Box_Log();
$service = new \Box\Mod\Client\Service();
$service->setDi($di);
$model = new \Model_ClientGroup();
$model->loadBean(new \DummyBean());
$result = $service->deleteGroup($model);
$this->assertTrue($result);
}
public function testDeleteGroupGroupHasClients(): void
{
$clientModel = new \Model_Client();
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->willReturn($clientModel);
$di = $this->getDi();
$di['db'] = $dbMock;
$service = new \Box\Mod\Client\Service();
$service->setDi($di);
$model = new \Model_ClientGroup();
$model->loadBean(new \DummyBean());
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Cannot remove groups with clients');
$service->deleteGroup($model);
}
public function testAuthorizeClientDidntFoundEmail(): void
{
$email = 'example@fossbilling.vm';
$password = '123456';
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->with('Client')
->willReturn(null);
$authMock = $this->getMockBuilder('\Box_Authorization')->disableOriginalConstructor()->getMock();
$authMock->expects($this->atLeastOnce())
->method('authorizeUser')
->with(null, $password)
->willReturn(null);
$di = $this->getDi();
$di['db'] = $dbMock;
$di['auth'] = $authMock;
$service = new \Box\Mod\Client\Service();
$service->setDi($di);
$result = $service->authorizeClient($email, $password);
$this->assertNull($result);
}
public function testAuthorizeClient(): void
{
$email = 'example@fossbilling.vm';
$password = '123456';
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->with('Client')
->willReturn($clientModel);
$authMock = $this->getMockBuilder('\Box_Authorization')->disableOriginalConstructor()->getMock();
$authMock->expects($this->atLeastOnce())
->method('authorizeUser')
->with($clientModel, $password)
->willReturn($clientModel);
$di = $this->getDi();
$di['db'] = $dbMock;
$di['auth'] = $authMock;
$di['mod_config'] = $di->protect(fn ($name): array => ['require_email_confirmation' => false]);
$service = new \Box\Mod\Client\Service();
$service->setDi($di);
$result = $service->authorizeClient($email, $password);
$this->assertInstanceOf('\Model_Client', $result);
}
public function testAuthorizeClientEmailRequiredConfirmed(): void
{
$email = 'example@fossbilling.vm';
$password = '123456';
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$clientModel->email_approved = 1;
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('findOne')
->with('Client')
->willReturn($clientModel);
$authMock = $this->getMockBuilder('\Box_Authorization')->disableOriginalConstructor()->getMock();
$authMock->expects($this->any())
->method('authorizeUser')
->with($clientModel, $password)
->willReturn($clientModel);
$di = $this->getDi();
$di['db'] = $dbMock;
$di['auth'] = $authMock;
$di['mod_config'] = $di->protect(fn ($name): array => ['require_email_confirmation' => true]);
$service = new \Box\Mod\Client\Service();
$service->setDi($di);
$result = $service->authorizeClient($email, $password);
$this->assertInstanceOf('\Model_Client', $result);
}
public function testCanChangeEmail(): void
{
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$email = 'client@fossbilling.org';
$config = [
'disable_change_email' => false,
];
$di = $this->getDi();
$di['mod_config'] = $di->protect(fn ($modName): array => $config);
$service = new \Box\Mod\Client\Service();
$service->setDi($di);
$result = $service->canChangeEmail($clientModel, $email);
$this->assertTrue($result);
}
public function testCanChangeEmailEmailAreTheSame(): void
{
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$email = 'client@fossbilling.org';
$clientModel->email = $email;
$config = [
'disable_change_email' => false,
];
$di = $this->getDi();
$di['mod_config'] = $di->protect(fn ($modName): array => $config);
$service = new \Box\Mod\Client\Service();
$service->setDi($di);
$result = $service->canChangeEmail($clientModel, $email);
$this->assertTrue($result);
}
public function testCanChangeEmailEmptyConfig(): void
{
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$email = 'client@fossbilling.org';
$config = [];
$di = $this->getDi();
$di['mod_config'] = $di->protect(fn ($modName): array => $config);
$service = new \Box\Mod\Client\Service();
$service->setDi($di);
$result = $service->canChangeEmail($clientModel, $email);
$this->assertTrue($result);
}
public function testCanChangeEmailCanntChangeEmail(): void
{
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$email = 'client@fossbilling.org';
$config = [
'disable_change_email' => true,
];
$di = $this->getDi();
$di['mod_config'] = $di->protect(fn ($modName): array => $config);
$service = new \Box\Mod\Client\Service();
$service->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Email address cannot be changed');
$service->canChangeEmail($clientModel, $email);
}
public function testCheckExtraRequiredFields(): void
{
$required = ['id'];
$data = [];
$config['required'] = $required;
$di = $this->getDi();
$di['mod_config'] = $di->protect(fn ($modName): array => $config);
$service = new \Box\Mod\Client\Service();
$service->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Field Id cannot be empty');
$service->checkExtraRequiredFields($data);
}
public function testCheckCustomFields(): void
{
$custom_field = [
'custom_field_name' => [
'active' => true,
'required' => true,
'title' => 'custom_field_title',
],
];
$config['custom_fields'] = $custom_field;
$di = $this->getDi();
$di['mod_config'] = $di->protect(fn ($modName): array => $config);
$data = [];
$service = new \Box\Mod\Client\Service();
$service->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Field custom_field_title cannot be empty');
$service->checkCustomFields($data);
}
public function testCheckCustomFieldsNotRequired(): void
{
$custom_field = [
'custom_field_name' => [
'active' => true,
'required' => false,
'title' => 'custom_field_title',
],
];
$config['custom_fields'] = $custom_field;
$di = $this->getDi();
$di['mod_config'] = $di->protect(fn ($modName): array => $config);
$data = [];
$service = new \Box\Mod\Client\Service();
$service->setDi($di);
$result = $service->checkCustomFields($data);
$this->assertNull($result);
}
public function testGuestCreateClientStripsAdminOnlyFields(): void
{
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$clientModel->id = 1;
$data = [
'email' => 'test@example.com',
'first_name' => 'John',
'password' => 'StrongPassword123!',
'group_id' => '2',
'auth_type' => 'oauth',
'aid' => 'foreign-id',
'notes' => 'admin notes',
'created_at' => '2020-01-01 00:00:00',
'currency' => 'USD',
'lang' => 'en',
'custom_1' => 'value',
];
$requestMock = $this->createStub(\Symfony\Component\HttpFoundation\Request::class);
$requestMock->method('getClientIp')->willReturn('127.0.0.1');
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->once())
->method('dispense')
->willReturn($clientModel);
$capturedData = null;
$dbMock->expects($this->once())
->method('store')
->willReturnCallback(function ($client) use (&$capturedData) {
$capturedData = $client;
return 1;
});
$eventManagerMock = $this->createMock('\Box_EventManager');
$eventManagerMock->expects($this->exactly(2))
->method('fire');
$passwordMock = $this->createStub(\FOSSBilling\PasswordManager::class);
$passwordMock->method('hashIt')->willReturn('hashed');
$modMock = $this->createStub(\FOSSBilling\Module::class);
$modMock->method('getConfig')->willReturn([]);
$di = $this->getDi();
$di['db'] = $dbMock;
$di['events_manager'] = $eventManagerMock;
$di['logger'] = new \Box_Log();
$di['mod'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\Stub => $modMock);
$di['password'] = $passwordMock;
$di['request'] = $requestMock;
$service = new \Box\Mod\Client\Service();
$service->setDi($di);
$service->guestCreateClient($data);
$this->assertSame('test@example.com', $clientModel->email);
$this->assertSame('John', $clientModel->first_name);
$this->assertSame('value', $clientModel->custom_1);
$this->assertNull($clientModel->client_group_id);
$this->assertNull($clientModel->auth_type);
$this->assertNull($clientModel->aid);
$this->assertNull($clientModel->notes);
$this->assertNull($clientModel->currency);
$this->assertNull($clientModel->lang);
$this->assertSame('127.0.0.1', $clientModel->ip);
$this->assertSame(\Model_Client::ACTIVE, $clientModel->status);
$this->assertStringNotContainsString('2020', $clientModel->created_at);
}
}