Files
FOSSBilling/tests-legacy/modules/Servicehosting/ServiceTest.php
T
Adam Daley 53eb4f2fca Refactor Twig environments and extensions (#2752)
* Replace Custom Twig components

Replace custom Twig components with native Twig components, remove unused custom Twig components, and move functionality to macros where appropriate.

m

Update partial_extensions.html.twig

* Update Twig components and migrate to FOSSBilling namespace

* Update Currency module...

* Refactor Twig environment config and loading

* Switch CSRF to cookie-based double-submit

* Add Api Twig extension and restore widget/svg helpers

* Cast language flags; improve JSON parsing

* Add markdown DI service and use in Admin API

* Refactor Twig extensions and markdown, simplify APIs

* Add sandboxed email Twig env, update render APIs

* Fix pagination URL usage and menu class

* Fix HTML markup in email templates

* Use macro for avatars and status badges

* Fix Twig spacing and pagination URL filter order

* Replace Custom Twig components

Replace custom Twig components with native Twig components, remove unused custom Twig components, and move functionality to macros where appropriate.

m

Update partial_extensions.html.twig

* Update Twig components and migrate to FOSSBilling namespace

* Refactor Twig environment config and loading

* Add Api Twig extension and restore widget/svg helpers

* Cast language flags; improve JSON parsing

* Add markdown DI service and use in Admin API

* Refactor Twig extensions and markdown, simplify APIs

* Fix pagination URL usage and menu class

* Fix Twig spacing and pagination URL filter order

* Use `current_theme` global and refactor `period_title`

* Allow markdown and date in email Twig sandbox

* Update email template examples

* Fix Twig URLs, quoting, and logo alt text

* Fix Twig templates: URLs, i18n, search params

* Use `templates/*` directories for module templates

* Tighten Twig sandbox and add tests

* Add Twig environment and template docs to `AGENTS.md`

* Use Symfony Filesystem/Finder and assorted fixes

* Fix ticket reply link fragment concatenation

* Close missing divs in dashboard template

* Use detailed `toApiArray` for client

* Fix module name resolution for email templates

* Regenerate `composer.lock`

* Use `markdown_to_html` filter in admin macro

* Handle null price/quantity and rename test method

* Use `Tools::isHTTPS()` for HTTPS checks

* Add 'area' param to `url` filter and update templates

* Use client area URLs in admin templates

* Use client area and guest API URLs in templates

* Remove client-side invoice delete feature

* Create Twig env via `TwigFactory` for PDF rendering

* Add embedded orderbutton JS and update render

* Add 'srclink' to cspell allowlist

* Use `renderEmailTplString` for email rendering

* Bump twig/markdown-extra to v3.24

* Use `markdown_to_html` for privacy policy

* Escape API JSON, handle null IP, client URLs

* Remove initial admin creation flow

* Handle HTTP failures and make tests robust

* Use `api_url` helper in admin templates

* Use `url` filter for footer link 5

* Add patch53 to remove legacy html templates

* Bump setting last_patch to 53

* Add patch54 to restore legacy email templates

* Use renderEmailTplString for email templating

* Remove redundant else/endif in order template

* Add file-hash cache buster for asset URLs

* Remove raw Twig filter and handle missing modules dir

* Return '-' for future timestamps

* Register and reuse TwigFactory and add CSRF cookie

* Adjust API URL helper and add Twig error handling

* Update composer.lock and package-lock.json

* Add twig_factory to ServiceTest DI

* Use url filter for link_page in template

* Composer - update deps and add markdown-extra

* Remove docblocks and inline comments in `TwigFactory`

* Add patches 53-55 and update `last_patch`

* Add `has_permission` Twig function; exclude `href`

* Remove duplicate network interface form

* Guard removal of legacy dirs; add network input

* Handle null category descriptions in template

* Add Twig adapter env and sandbox policy

* Add 'striptags' to cspell word list

* Update composer.lock (deps updated/removed)

* Sanitize custom payment adapter templates

* Remove redundant legacy API tests

* Bump `last_patch` setting to 56

* Coalesce null before `markdown_to_html`

* Move update patcher to system CLI command

* Remove stray `endif` from recaptcha macro

* Update composer.lock

* Fix extra closing brace in Api ServiceTest

* Fix test errors from main merge

- Remove testGetRequestCount tests (method moved to RateLimiter)
- Remove testGetDi from Staff and System GuestTest (protected method)
- Fix IP error message in testLoginCheckIpException
- Add rate_limiter mock to Staff GuestTest tests that need it

* Fix remaining Staff GuestTest issues

- Add extension service mock for captcha check
- Fix error message from 'User not found' to 'Admin not found'

* Fix Staff GuestTest: update query and add logger mock

* Fix Staff GuestTest: set admin status to active for password reset flow

* Fix stale API calls and inverted required condition

- Notification: replace removed system_string_render with direct output
- Antispam settings: replace |alink/|link with |url/|api_url filters
- build_form macro: fix inverted required condition for non-textarea inputs

* Sanitize JS/data URIs and disable buttons by permission

* Use client login/profile links in support emails

* Encode fb-api JSON and tighten URL sanitization

* Prefer session auth for API key methods

* Remove stray endif and fix template formatting

* Add CSRF token to CSV exports; fix template

* Remove stray endif in currency template

* Make invoice PDF generation return Response

* Fix Twig filters, templates, and form handling

* Remove readonly from anonymous class in test

* Inline DebugBar CSS/JS in renderHead

* Add Bootstrap Tab to global bootstrap

* Capitalize template UI strings

* Use CKEditor API for reply quotes

* Remove WYSIWYG module and move CKEditor to themes

* Bump `last_patch` in install SQL to 59

* Remove Spamchecker module

* Run domain management script only for active orders

* Standardize UI text capitalization & minor fixes

* Remove Spamchecker legacy tests; fix period test

* Rename Spamchecker tests to Antispam

* Restore antispam config in tests; remove fixture

* Refactor Antispam Guest tests for config checks

* Update theme build docs; remove Wysiwyg module

* Remove spamchecker from cspell and tweak comment

* Add spamchecker to cspell word list

* Capitalize 'Update Graphs' button label
2026-05-06 21:02:16 +01:00

1275 lines
44 KiB
PHP

<?php
declare(strict_types=1);
namespace Box\Mod\Servicehosting;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
#[Group('Core')]
final class ServiceTest extends \BBTestCase
{
protected ?Service $service;
public function setUp(): void
{
$this->service = new Service();
}
public static function validateOrdertDataProvider(): array
{
return [
['server_id', 'Hosting product is not configured completely. Configure server for hosting product.', 701],
['hosting_plan_id', 'Hosting product is not configured completely. Configure hosting plan for hosting product.', 702],
['sld', 'Domain name is invalid.', 703],
['tld', 'Domain extension is invalid.', 704],
];
}
#[DataProvider('validateOrdertDataProvider')]
public function testValidateOrderData(string $field, string $exceptionMessage, int $excCode): void
{
$data = [
'server_id' => 1,
'hosting_plan_id' => 2,
'sld' => 'great',
'tld' => 'com',
];
unset($data[$field]);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage($exceptionMessage);
$this->service->validateOrderData($data);
}
public function testActionCreate(): void
{
$orderModel = new \Model_ClientOrder();
$orderModel->loadBean(new \DummyBean());
$confArr = [
'server_id' => 1,
'hosting_plan_id' => 2,
'sld' => 'great',
'tld' => 'com',
];
$orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class);
$orderServiceMock->expects($this->atLeastOnce())
->method('getConfig')
->willReturn($confArr);
$hostingServerModel = new \Model_ServiceHostingServer();
$hostingServerModel->loadBean(new \DummyBean());
$hostingPlansModel = new \Model_ServiceHostingHp();
$hostingPlansModel->loadBean(new \DummyBean());
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('getExistingModelById')
->willReturnOnConsecutiveCalls($hostingServerModel, $hostingPlansModel);
$servhostingModel = new \Model_ServiceHosting();
$servhostingModel->loadBean(new \DummyBean());
$dbMock->expects($this->atLeastOnce())
->method('dispense')
->willReturn($servhostingModel);
$newserviceHostingId = 4;
$dbMock->expects($this->atLeastOnce())
->method('store')
->willReturn($newserviceHostingId);
$di = $this->getDi();
$di['db'] = $dbMock;
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $orderServiceMock);
$this->service->setDi($di);
$this->service->action_create($orderModel);
}
// public function testAction_activate()
// {
// $orderModel = new \Model_ClientOrder();
// $orderModel->loadBean(new \DummyBean());
//
// $confArr = array(
// 'server_id' => 1,
// 'hosting_plan_id' => 2,
// 'sld' => 'great',
// 'tld' => 'com',
// 'username' => 'username',
// 'password' => 'password'
// );
//
// $orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class);
// $orderServiceMock->expects($this->atLeastOnce())
// ->method('getConfig')
// ->will($this->returnValue($confArr));
//
// $servhostingModel = new \Model_ServiceHosting();
// $servhostingModel->loadBean(new \DummyBean());
// $orderServiceMock->expects($this->atLeastOnce())
// ->method('getOrderService')
// ->will($this->returnValue($servhostingModel));
//
//
// $toolsMock = $this->createMock(\FOSSBilling\Tools::class);
// $toolsMock->expects($this->atLeastOnce())
// ->method('generatePassword')
// ->will($this->returnValue('generatePassword'));
//
// $dbMock = $this->createMock('\Box_Database');
// $dbMock->expects($this->atLeastOnce())
// ->method('store');
//
// $serviceMock = $this->getMockBuilder(\Box\Mod\Servicehosting\Service::class)
// ->onlyMethods(array('_getAM'))
// ->getMock();
//
// $serverManagerMock = $this->getMockBuilder('\Server_Manager_Custom')->disableOriginalConstructor()->getMock();
// $serverManagerMock->expects($this->atLeastOnce())
// ->method('createAccount');
//
// $AMresultArray = array($serverManagerMock, new \Server_Account());
// $serviceMock->expects($this->atLeastOnce())
// ->method('_getAM')
// ->will($this->returnValue($AMresultArray));
//
// $di = $this->getDi();
// $di['db'] = $dbMock;
// $di['tools'] = $toolsMock;
// $di['mod_service'] = $di->protect(fn() => $orderServiceMock);
//
// $serviceMock->setDi($di);
// $orderModel->config = $confArr;
// $result = $serviceMock->action_activate($orderModel);
// $this->assertIsArray($result);
// $this->assertNotEmpty($result['username']);
// $this->assertNotEmpty($result['password']);
// }
public function testActionRenew(): void
{
$orderModel = new \Model_ClientOrder();
$orderModel->loadBean(new \DummyBean());
$model = new \Model_ServiceHostingHp();
$model->loadBean(new \DummyBean());
$orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class);
$orderServiceMock->expects($this->atLeastOnce())
->method('getOrderService')
->willReturn($model);
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('store');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $orderServiceMock);
$this->service->setDi($di);
$result = $this->service->action_renew($orderModel);
$this->assertTrue($result);
}
public function testActionRenewOrderWithoutActiveService(): void
{
$orderModel = new \Model_ClientOrder();
$orderModel->loadBean(new \DummyBean());
$orderModel->id = 1;
$orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class);
$orderServiceMock->expects($this->atLeastOnce())
->method('getOrderService');
$di = $this->getDi();
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $orderServiceMock);
$this->service->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage(sprintf('Order %d has no active service', $orderModel->id));
$this->service->action_renew($orderModel);
}
public function testActionSuspend(): void
{
$orderModel = new \Model_ClientOrder();
$orderModel->loadBean(new \DummyBean());
$model = new \Model_ServiceHosting();
$model->loadBean(new \DummyBean());
$orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class);
$orderServiceMock->expects($this->atLeastOnce())
->method('getOrderService')
->willReturn($model);
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('store');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $orderServiceMock);
$serviceMock = $this->getMockBuilder(Service::class)
->onlyMethods(['_getAM'])
->getMock();
$serverManagerMock = $this->getMockBuilder('\Server_Manager_Custom')->disableOriginalConstructor()->getMock();
$serverManagerMock->expects($this->atLeastOnce())
->method('suspendAccount');
$AMresultArray = [$serverManagerMock, new \Server_Account()];
$serviceMock->expects($this->atLeastOnce())
->method('_getAM')
->willReturn($AMresultArray);
$serviceMock->setDi($di);
$result = $serviceMock->action_suspend($orderModel);
$this->assertTrue($result);
}
public function testActionSuspendOrderWithoutActiveService(): void
{
$orderModel = new \Model_ClientOrder();
$orderModel->loadBean(new \DummyBean());
$orderModel->id = 1;
$orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class);
$orderServiceMock->expects($this->atLeastOnce())
->method('getOrderService');
$di = $this->getDi();
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $orderServiceMock);
$this->service->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage(sprintf('Order %d has no active service', $orderModel->id));
$this->service->action_suspend($orderModel);
}
public function testActionUnsuspend(): void
{
$orderModel = new \Model_ClientOrder();
$orderModel->loadBean(new \DummyBean());
$model = new \Model_ServiceHosting();
$model->loadBean(new \DummyBean());
$orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class);
$orderServiceMock->expects($this->atLeastOnce())
->method('getOrderService')
->willReturn($model);
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('store');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $orderServiceMock);
$serviceMock = $this->getMockBuilder(Service::class)
->onlyMethods(['_getAM'])
->getMock();
$serverManagerMock = $this->getMockBuilder('\Server_Manager_Custom')->disableOriginalConstructor()->getMock();
$serverManagerMock->expects($this->atLeastOnce())
->method('unsuspendAccount');
$AMresultArray = [$serverManagerMock, new \Server_Account()];
$serviceMock->expects($this->atLeastOnce())
->method('_getAM')
->willReturn($AMresultArray);
$serviceMock->setDi($di);
$result = $serviceMock->action_unsuspend($orderModel);
$this->assertTrue($result);
}
public function testActionUnsuspendOrderWithoutActiveService(): void
{
$orderModel = new \Model_ClientOrder();
$orderModel->loadBean(new \DummyBean());
$orderModel->id = 1;
$orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class);
$orderServiceMock->expects($this->atLeastOnce())
->method('getOrderService');
$di = $this->getDi();
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $orderServiceMock);
$this->service->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage(sprintf('Order %d has no active service', $orderModel->id));
$this->service->action_unsuspend($orderModel);
}
public function testActionCancel(): void
{
$orderModel = new \Model_ClientOrder();
$orderModel->loadBean(new \DummyBean());
$model = new \Model_ServiceHosting();
$model->loadBean(new \DummyBean());
$orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class);
$orderServiceMock->expects($this->atLeastOnce())
->method('getOrderService')
->willReturn($model);
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('store');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $orderServiceMock);
$serviceMock = $this->getMockBuilder(Service::class)
->onlyMethods(['_getAM'])
->getMock();
$serverManagerMock = $this->getMockBuilder('\Server_Manager_Custom')->disableOriginalConstructor()->getMock();
$serverManagerMock->expects($this->atLeastOnce())
->method('cancelAccount');
$AMresultArray = [$serverManagerMock, new \Server_Account()];
$serviceMock->expects($this->atLeastOnce())
->method('_getAM')
->willReturn($AMresultArray);
$serviceMock->setDi($di);
$result = $serviceMock->action_cancel($orderModel);
$this->assertTrue($result);
}
public function testActionCancelOrderWithoutActiveService(): void
{
$orderModel = new \Model_ClientOrder();
$orderModel->loadBean(new \DummyBean());
$orderModel->id = 1;
$orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class);
$orderServiceMock->expects($this->atLeastOnce())
->method('getOrderService');
$di = $this->getDi();
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $orderServiceMock);
$this->service->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage(sprintf('Order %d has no active service', $orderModel->id));
$this->service->action_cancel($orderModel);
}
// public function testAction_uncancel()
// {
// $orderModel = new \Model_ClientOrder();
// $orderModel->loadBean(new \DummyBean());
// $confArr = array(
// 'server_id' => 1,
// 'hosting_plan_id' => 2,
// 'sld' => 'great',
// 'tld' => 'com'
// );
// $orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class);
// $orderServiceMock->expects($this->atLeastOnce())
// ->method('getConfig')
// ->will($this->returnValue($confArr));
//
// $model = new \Model_ServiceHosting();
// $model->loadBean(new \DummyBean());
// $orderServiceMock->expects($this->atLeastOnce())
// ->method('getOrderService')
// ->will($this->returnValue($model));
//
// $hostingServerModel = new \Model_ServiceHostingServer();
// $hostingServerModel->loadBean(new \DummyBean());
// $hostingPlansModel = new \Model_ServiceHostingHp();
// $hostingPlansModel->loadBean(new \DummyBean());
// $dbMock = $this->createMock('\Box_Database');
// $dbMock->expects($this->atLeastOnce())
// ->method('getExistingModelById')
// ->willReturn($hostingServerModel, $hostingPlansModel);
//
// $servhostingModel = new \Model_ServiceHosting();
// $servhostingModel->loadBean(new \DummyBean());
// $dbMock->expects($this->atLeastOnce())
// ->method('dispense')
// ->will($this->returnValue($servhostingModel));
//
// $newserviceHostingId = 4;
// $dbMock->expects($this->atLeastOnce())
// ->method('store')
// ->will($this->returnValue($newserviceHostingId));
//
// $di = $this->getDi();
// $di['db'] = $dbMock;
// $di['mod_service'] = $di->protect(fn() => $orderServiceMock);
//
//
//
// $serviceMock = $this->getMockBuilder(\Box\Mod\Servicehosting\Service::class)
// ->onlyMethods(array('_getAM'))
// ->getMock();
//
// $serverManagerMock = $this->getMockBuilder('\Server_Manager_Custom')->disableOriginalConstructor()->getMock();
// $serverManagerMock->expects($this->atLeastOnce())
// ->method('createAccount');
// $AMresultArray = array($serverManagerMock, new \Server_Account());
// $serviceMock->expects($this->atLeastOnce())
// ->method('_getAM')
// ->will($this->returnValue($AMresultArray));
//
//
// $serviceMock->setDi($di);
// $serviceMock->action_uncancel($orderModel);
// }
public function testActionDelete(): void
{
$orderModel = new \Model_ClientOrder();
$orderModel->loadBean(new \DummyBean());
$orderModel->status = 'active';
$model = new \Model_ServiceHosting();
$model->loadBean(new \DummyBean());
$orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class);
$orderServiceMock->expects($this->atLeastOnce())
->method('getOrderService')
->willReturn($model);
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('trash');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $orderServiceMock);
$serviceMock = $this->getMockBuilder(Service::class)
->onlyMethods(['action_cancel'])
->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('action_cancel');
$serviceMock->setDi($di);
$serviceMock->action_delete($orderModel);
}
public function testChangeAccountPlan(): void
{
$orderModel = new \Model_ClientOrder();
$orderModel->loadBean(new \DummyBean());
$model = new \Model_ServiceHosting();
$model->loadBean(new \DummyBean());
$modelHp = new \Model_ServiceHostingHp();
$modelHp->loadBean(new \DummyBean());
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('store');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['logger'] = new \Box_Log();
$serviceMock = $this->getMockBuilder(Service::class)
->onlyMethods(['_getAM', 'getServerPackage'])
->getMock();
$serverManagerMock = $this->getMockBuilder('\Server_Manager_Custom')->disableOriginalConstructor()->getMock();
$serverManagerMock->expects($this->atLeastOnce())
->method('changeAccountPackage');
$AMresultArray = [$serverManagerMock, new \Server_Account()];
$serviceMock->expects($this->atLeastOnce())
->method('_getAM')
->willReturn($AMresultArray);
$serviceMock->expects($this->atLeastOnce())
->method('getServerPackage')
->willReturn(new \Server_Package());
$serviceMock->setDi($di);
$result = $serviceMock->changeAccountPlan($orderModel, $model, $modelHp);
$this->assertTrue($result);
}
public function testChangeAccountUsername(): void
{
$data = [
'username' => 'u123456',
];
$orderModel = new \Model_ClientOrder();
$orderModel->loadBean(new \DummyBean());
$model = new \Model_ServiceHosting();
$model->loadBean(new \DummyBean());
$serviceMock = $this->getMockBuilder(Service::class)
->onlyMethods(['_getAM'])
->getMock();
$serverManagerMock = $this->getMockBuilder('\Server_Manager_Custom')->disableOriginalConstructor()->getMock();
$serverManagerMock->expects($this->atLeastOnce())
->method('changeAccountUsername');
$AMresultArray = [$serverManagerMock, new \Server_Account()];
$serviceMock->expects($this->atLeastOnce())
->method('_getAM')
->willReturn($AMresultArray);
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('store');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['logger'] = new \Box_Log();
$serviceMock->setDi($di);
$result = $serviceMock->changeAccountUsername($orderModel, $model, $data);
$this->assertTrue($result);
}
public function testChangeAccountUsernameMissingUsername(): void
{
$orderModel = new \Model_ClientOrder();
$orderModel->loadBean(new \DummyBean());
$model = new \Model_ServiceHosting();
$model->loadBean(new \DummyBean());
$data = [];
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Account username is missing or is invalid');
$this->service->changeAccountUsername($orderModel, $model, $data);
}
public function testChangeAccountIp(): void
{
$data = [
'ip' => '1.1.1.1',
];
$orderModel = new \Model_ClientOrder();
$orderModel->loadBean(new \DummyBean());
$model = new \Model_ServiceHosting();
$model->loadBean(new \DummyBean());
$serviceMock = $this->getMockBuilder(Service::class)
->onlyMethods(['_getAM'])
->getMock();
$serverManagerMock = $this->getMockBuilder('\Server_Manager_Custom')->disableOriginalConstructor()->getMock();
$serverManagerMock->expects($this->atLeastOnce())
->method('changeAccountIp');
$AMresultArray = [$serverManagerMock, new \Server_Account()];
$serviceMock->expects($this->atLeastOnce())
->method('_getAM')
->willReturn($AMresultArray);
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('store');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['logger'] = new \Box_Log();
$serviceMock->setDi($di);
$result = $serviceMock->changeAccountIp($orderModel, $model, $data);
$this->assertTrue($result);
}
public function testChangeAccountIpMissingIp(): void
{
$data = [];
$orderModel = new \Model_ClientOrder();
$orderModel->loadBean(new \DummyBean());
$model = new \Model_ServiceHosting();
$model->loadBean(new \DummyBean());
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Account IP address is missing or is invalid');
$this->service->changeAccountIp($orderModel, $model, $data);
}
public function testChangeAccountDomain(): void
{
$data = [
'tld' => 'com',
'sld' => 'testingSld',
];
$orderModel = new \Model_ClientOrder();
$orderModel->loadBean(new \DummyBean());
$model = new \Model_ServiceHosting();
$model->loadBean(new \DummyBean());
$serviceMock = $this->getMockBuilder(Service::class)
->onlyMethods(['_getAM'])
->getMock();
$serverManagerMock = $this->getMockBuilder('\Server_Manager_Custom')->disableOriginalConstructor()->getMock();
$serverManagerMock->expects($this->atLeastOnce())
->method('changeAccountDomain');
$AMresultArray = [$serverManagerMock, new \Server_Account()];
$serviceMock->expects($this->atLeastOnce())
->method('_getAM')
->willReturn($AMresultArray);
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('store');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['logger'] = new \Box_Log();
$serviceMock->setDi($di);
$result = $serviceMock->changeAccountDomain($orderModel, $model, $data);
$this->assertTrue($result);
}
public function testChangeAccountDomainMissingParams(): void
{
$data = [];
$orderModel = new \Model_ClientOrder();
$orderModel->loadBean(new \DummyBean());
$model = new \Model_ServiceHosting();
$model->loadBean(new \DummyBean());
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Domain SLD or TLD is missing');
$this->service->changeAccountDomain($orderModel, $model, $data);
}
public function testChangeAccountPassword(): void
{
$data = [
'password' => 'topsecret',
'password_confirm' => 'topsecret',
];
$orderModel = new \Model_ClientOrder();
$orderModel->loadBean(new \DummyBean());
$model = new \Model_ServiceHosting();
$model->loadBean(new \DummyBean());
$serviceMock = $this->getMockBuilder(Service::class)
->onlyMethods(['_getAM'])
->getMock();
$serverManagerMock = $this->getMockBuilder('\Server_Manager_Custom')->disableOriginalConstructor()->getMock();
$serverManagerMock->expects($this->atLeastOnce())
->method('changeAccountPassword');
$AMresultArray = [$serverManagerMock, new \Server_Account()];
$serviceMock->expects($this->atLeastOnce())
->method('_getAM')
->willReturn($AMresultArray);
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('store');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['logger'] = new \Box_Log();
$serviceMock->setDi($di);
$result = $serviceMock->changeAccountPassword($orderModel, $model, $data);
$this->assertTrue($result);
}
public function testChangeAccountPasswordMissingParams(): void
{
$data = [];
$orderModel = new \Model_ClientOrder();
$orderModel->loadBean(new \DummyBean());
$model = new \Model_ServiceHosting();
$model->loadBean(new \DummyBean());
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Account password is missing or is invalid');
$this->service->changeAccountPassword($orderModel, $model, $data);
}
public function testSync(): void
{
$data = [
'password' => 'topsecret',
'password_confirm' => 'topsecret',
];
$orderModel = new \Model_ClientOrder();
$orderModel->loadBean(new \DummyBean());
$model = new \Model_ServiceHosting();
$model->loadBean(new \DummyBean());
$serviceMock = $this->getMockBuilder(Service::class)
->onlyMethods(['_getAM'])
->getMock();
$accountObj = new \Server_Account();
$accountObj->setUsername('testUser1');
$accountObj->setIp('1.1.1.1');
$accountObj2 = new \Server_Account();
$accountObj2->setUsername('testUser2');
$accountObj2->setIp('2.2.2.2');
$serverManagerMock = $this->getMockBuilder('\Server_Manager_Custom')->disableOriginalConstructor()->getMock();
$serverManagerMock->expects($this->atLeastOnce())
->method('synchronizeAccount')
->willReturn($accountObj2);
$AMresultArray = [$serverManagerMock, $accountObj];
$serviceMock->expects($this->atLeastOnce())
->method('_getAM')
->willReturn($AMresultArray);
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('store');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['logger'] = new \Box_Log();
$serviceMock->setDi($di);
$result = $serviceMock->sync($orderModel, $model, $data);
$this->assertTrue($result);
}
public function testToApiArray(): void
{
$model = new \Model_ServiceHosting();
$model->loadBean(new \DummyBean());
$hostingServer = new \Model_ServiceHostingServer();
$hostingServer->loadBean(new \DummyBean());
$hostingServer->manager = 'Custom';
$hostingHp = new \Model_ServiceHostingHp();
$hostingHp->loadBean(new \DummyBean());
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('load')
->willReturnOnConsecutiveCalls($hostingServer, $hostingHp);
$orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class);
$orderServiceMock->expects($this->atLeastOnce())
->method('getServiceOrder');
$serverManagerCustomMock = $this->getMockBuilder('\Server_Manager_Custom')->disableOriginalConstructor()->getMock();
$di = $this->getDi();
$di['db'] = $dbMock;
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $orderServiceMock);
$di['server_manager'] = $di->protect(fn ($manager, $config): \PHPUnit\Framework\MockObject\MockObject => $serverManagerCustomMock);
$this->service->setDi($di);
$result = $this->service->toApiArray($model, false, new \Model_Admin());
$this->assertIsArray($result);
}
public function testUpdate(): void
{
$data = [
'username' => 'testUser',
'ip' => '1.1.1.1',
];
$model = new \Model_ServiceHosting();
$model->loadBean(new \DummyBean());
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('store');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['logger'] = new \Box_Log();
$this->service->setDi($di);
$result = $this->service->update($model, $data);
$this->assertTrue($result);
}
public function testGetServerManagers(): void
{
$result = $this->service->getServerManagers();
$this->assertIsArray($result);
}
public function testGetServerManagerConfig(): void
{
$manager = 'Custom';
$expected = [
'label' => 'Custom Server Manager',
];
$result = $this->service->getServerManagerConfig($manager);
$this->assertIsArray($result);
$this->assertEquals($expected, $result);
}
public function testGetServerPairs(): void
{
$expected = [
'1' => 'name',
'2' => 'ding',
];
$queryResult = [
[
'id' => 1,
'name' => 'name',
], [
'id' => 2,
'name' => 'ding',
],
];
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('getAll')
->willReturn($queryResult);
$di = $this->getDi();
$di['db'] = $dbMock;
$this->service->setDi($di);
$result = $this->service->getServerPairs();
$this->assertIsArray($result);
$this->assertEquals($expected, $result);
}
public function testGetServerSearchQuery(): void
{
$result = $this->service->getServersSearchQuery([]);
$this->assertIsString($result[0]);
$this->assertIsArray($result[1]);
$this->assertSame([], $result[1]);
}
public function testCreateServer(): void
{
$dbMock = $this->createMock('\Box_Database');
$hostingServerModel = new \Model_ServiceHostingServer();
$hostingServerModel->loadBean(new \DummyBean());
$dbMock->expects($this->atLeastOnce())
->method('dispense')
->willReturn($hostingServerModel);
$newId = 1;
$dbMock->expects($this->atLeastOnce())
->method('store')
->willReturn($newId);
$di = $this->getDi();
$di['db'] = $dbMock;
$di['logger'] = new \Box_Log();
$this->service->setDi($di);
$name = 'newSuperFastServer';
$ip = '1.1.1.1';
$manager = 'Custom';
$data = [];
$result = $this->service->createServer($name, $ip, $manager, $data);
$this->assertIsInt($result);
$this->assertEquals($newId, $result);
}
public function testDeleteServer(): void
{
$hostingServerModel = new \Model_ServiceHostingServer();
$hostingServerModel->loadBean(new \DummyBean());
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('trash');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['logger'] = new \Box_Log();
$this->service->setDi($di);
$result = $this->service->deleteServer($hostingServerModel);
$this->assertTrue($result);
}
public function testUpdateServer(): void
{
$data = [
'name' => 'newName',
'ip' => '1.1.1.1',
'hostname' => 'unknownStar',
'active' => 1,
'status_url' => 'na',
'ns1' => 'ns1.testserver.eu',
'ns2' => 'ns2.testserver.eu',
'ns3' => 'ns3.testserver.eu',
'ns4' => 'ns4.testserver.eu',
'manager' => 'Custom',
'username' => 'testingJohn',
'password' => 'hardToGuess',
'accesshash' => 'secret',
'port' => '23',
'secure' => 0,
];
$hostingServerModel = new \Model_ServiceHostingServer();
$hostingServerModel->loadBean(new \DummyBean());
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('store');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['logger'] = new \Box_Log();
$this->service->setDi($di);
$result = $this->service->updateServer($hostingServerModel, $data);
$this->assertTrue($result);
}
public function testGetServerManager(): void
{
$hostingServerModel = new \Model_ServiceHostingServer();
$hostingServerModel->loadBean(new \DummyBean());
$hostingServerModel->manager = 'Custom';
$serverManagerCustom = $this->getMockBuilder('\Server_Manager_Custom')->disableOriginalConstructor()->getMock();
$di = $this->getDi();
$di['server_manager'] = $di->protect(fn ($manager, $config): \PHPUnit\Framework\MockObject\MockObject => $serverManagerCustom);
$this->service->setDi($di);
$result = $this->service->getServerManager($hostingServerModel);
$this->assertInstanceOf('\Server_Manager_Custom', $result);
}
public function testGetServerManagerManagerNotDefined(): void
{
$hostingServerModel = new \Model_ServiceHostingServer();
$hostingServerModel->loadBean(new \DummyBean());
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionCode(654);
$this->expectExceptionMessage('Invalid server manager. Server was not configured properly');
$this->service->getServerManager($hostingServerModel);
}
public function testGetServerManagerServerManagerInvalid(): void
{
$hostingServerModel = new \Model_ServiceHostingServer();
$hostingServerModel->loadBean(new \DummyBean());
$hostingServerModel->manager = 'Custom';
$di = $this->getDi();
$di['server_manager'] = $di->protect(fn ($manager, $config): null => null);
$this->service->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage("Server manager {$hostingServerModel->manager} is invalid.");
$this->service->getServerManager($hostingServerModel);
}
public function testTestConnection(): void
{
$serverManagerMock = $this->getMockBuilder('\Server_Manager_Custom')->disableOriginalConstructor()->getMock();
$serverManagerMock->expects($this->atLeastOnce())
->method('testConnection')
->willReturn(true);
$serviceMock = $this->getMockBuilder(Service::class)
->onlyMethods(['getServerManager'])
->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getServerManager')
->willReturn($serverManagerMock);
$hostingServerModel = new \Model_ServiceHostingServer();
$result = $serviceMock->testConnection($hostingServerModel);
$this->assertIsBool($result);
$this->assertTrue($result);
}
public function testGetHpPairs(): void
{
$expected = [
'1' => 'free',
'2' => 'paid',
];
$queryResult = [
[
'id' => 1,
'name' => 'free',
], [
'id' => 2,
'name' => 'paid',
],
];
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('getAll')
->willReturn($queryResult);
$di = $this->getDi();
$di['db'] = $dbMock;
$this->service->setDi($di);
$result = $this->service->getHpPairs();
$this->assertIsArray($result);
$this->assertEquals($expected, $result);
}
public function testGetHpSearchQuery(): void
{
$result = $this->service->getServersSearchQuery([]);
$this->assertIsString($result[0]);
$this->assertIsArray($result[1]);
$this->assertSame([], $result[1]);
}
public function testDeleteHp(): void
{
$model = new \Model_ServiceHostingHp();
$model->loadBean(new \DummyBean());
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('trash');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['logger'] = new \Box_Log();
$this->service->setDi($di);
$result = $this->service->deleteHp($model);
$this->assertTrue($result);
}
public function testToHostingHpApiArray(): void
{
$model = new \Model_ServiceHostingHp();
$model->loadBean(new \DummyBean());
$result = $this->service->toHostingHpApiArray($model);
$this->assertIsArray($result);
}
public function testUpdateHp(): void
{
$data = [
'name' => 'firstPlan',
'bandwidth' => '100000',
'quota' => '1000',
'max_addon' => '0',
'max_ft' => '1',
'max_sql' => '2',
'max_pop' => '1',
'max_sub' => '2',
'max_park' => '1',
];
$model = new \Model_ServiceHostingHp();
$model->loadBean(new \DummyBean());
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('store');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['logger'] = new \Box_Log();
$this->service->setDi($di);
$result = $this->service->updateHp($model, $data);
$this->assertTrue($result);
}
public function testCreateHp(): void
{
$model = new \Model_ServiceHostingHp();
$model->loadBean(new \DummyBean());
$newId = 1;
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('dispense')->willReturn($model);
$dbMock->expects($this->atLeastOnce())
->method('store')->willReturn($newId);
$di = $this->getDi();
$di['db'] = $dbMock;
$di['logger'] = new \Box_Log();
$this->service->setDi($di);
$result = $this->service->createHp('Free Plan', []);
$this->assertIsInt($result);
$this->assertEquals($newId, $result);
}
public function testGetServerPackage(): void
{
$model = new \Model_ServiceHostingHp();
$model->loadBean(new \DummyBean());
$model->config = '{}';
$di = $this->getDi();
$this->service->setDi($di);
$result = $this->service->getServerPackage($model);
$this->assertInstanceOf('\Server_Package', $result);
}
public function testGetServerManagerWithLog(): void
{
$hostingServerModel = new \Model_ServiceHostingServer();
$hostingServerModel->loadBean(new \DummyBean());
$hostingServerModel->manager = 'Custom';
$clientOrderModel = new \Model_ClientOrder();
$clientOrderModel->loadBean(new \DummyBean());
$serverManagerMock = $this->getMockBuilder('\Server_Manager_Custom')->disableOriginalConstructor()->getMock();
$serviceMock = $this->getMockBuilder(Service::class)
->onlyMethods(['getServerManager'])
->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getServerManager')
->willReturn($serverManagerMock);
$orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class);
$orderServiceMock->expects($this->atLeastOnce())
->method('getLogger')
->willReturn(new \Box_Log());
$di = $this->getDi();
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $orderServiceMock);
$serviceMock->setDi($di);
$result = $serviceMock->getServerManagerWithLog($hostingServerModel, $clientOrderModel);
$this->assertInstanceOf('\Server_Manager_Custom', $result);
}
public function testGetManagerUrls(): void
{
$hostingServerModel = new \Model_ServiceHostingServer();
$hostingServerModel->loadBean(new \DummyBean());
$hostingServerModel->manager = 'Custom';
$serverManagerMock = $this->getMockBuilder('\Server_Manager_Custom')->disableOriginalConstructor()->getMock();
$serverManagerMock->expects($this->atLeastOnce())
->method('getLoginUrl')
->willReturn('/login');
$serverManagerMock->expects($this->atLeastOnce())
->method('getResellerLoginUrl')
->willReturn('/admin/login');
$serviceMock = $this->getMockBuilder(Service::class)
->onlyMethods(['getServerManager'])
->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getServerManager')
->willReturn($serverManagerMock);
$result = $serviceMock->getManagerUrls($hostingServerModel);
$this->assertIsArray($result);
$this->assertIsString($result[0]);
$this->assertIsString($result[1]);
}
public function testGetManagerUrlsException(): void
{
$hostingServerModel = new \Model_ServiceHostingServer();
$hostingServerModel->loadBean(new \DummyBean());
$hostingServerModel->manager = 'Custom';
$serviceMock = $this->getMockBuilder(Service::class)
->onlyMethods(['getServerManager'])
->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('getServerManager')
->will($this->throwException(new \Exception('Controlled unit test exception')));
$result = $serviceMock->getManagerUrls($hostingServerModel);
$this->assertIsArray($result);
$this->assertFalse($result[0]);
$this->assertFalse($result[1]);
}
public function testGetFreeTldsFreeTldsAreNotSet(): void
{
$di = $this->getDi();
$tldArray = ['tld' => '.com'];
$serviceDomainServiceMock = $this->createMock(\Box\Mod\Servicedomain\Service::class);
$serviceDomainServiceMock->expects($this->atLeastOnce())
->method('tldToApiArray')
->willReturn($tldArray);
$di['mod_service'] = $di->protect(fn (): \PHPUnit\Framework\MockObject\MockObject => $serviceDomainServiceMock);
$tldModel = new \Model_Tld();
$tldModel->loadBean(new \DummyBean());
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('find')
->willReturn([$tldModel]);
$di['db'] = $dbMock;
$this->service->setDi($di);
$model = new \Model_Product();
$model->loadBean(new \DummyBean());
$result = $this->service->getFreeTlds($model);
$this->assertIsArray($result);
}
public function testGetFreeTlds(): void
{
$config = [
'free_tlds' => ['.com'],
];
$di = $this->getDi();
$this->service->setDi($di);
$model = new \Model_Product();
$model->loadBean(new \DummyBean());
$model->config = json_encode($config);
$result = $this->service->getFreeTlds($model);
$this->assertIsArray($result);
$this->assertNotEmpty($result);
}
}