Files
FOSSBilling/tests-legacy/modules/Servicehosting/ServiceTest.php
T
CopilotandAdam Daley d1a5f23e4d Improve license validation form accessibility/line-ending consistency and correct legacy test intent (#3673)
* Initial plan

* chore: outline plan for requested fixes

* fix license form accessibility and legacy test cleanup

---------

Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: Adam Daley <admdly@users.noreply.github.com>
2026-05-30 13:45:23 +01:00

1368 lines
47 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 validateOrderDataProvider(): 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('validateOrderDataProvider')]
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 testPrependOrderConfigSupportsFreeSubdomain(): void
{
$product = new \Model_Product();
$product->loadBean(new \DummyBean());
$product->config = json_encode([
'server_id' => 1,
'hosting_plan_id' => 2,
'allow_subdomain' => true,
'subdomain_base_domain' => 'example.com',
]);
$this->service->setDi($this->getDi());
$result = $this->service->prependOrderConfig($product, [
'domain' => [
'action' => 'subdomain',
'subdomain_sld' => 'customer',
],
]);
$this->assertSame('customer', $result['sld']);
$this->assertSame('.example.com', $result['tld']);
}
public function testPrependOrderConfigUsesConfiguredFreeSubdomainBaseDomain(): void
{
$product = new \Model_Product();
$product->loadBean(new \DummyBean());
$product->config = json_encode([
'server_id' => 1,
'hosting_plan_id' => 2,
'allow_subdomain' => true,
'subdomain_base_domain' => 'example.com',
]);
$this->service->setDi($this->getDi());
$result = $this->service->prependOrderConfig($product, [
'subdomain_base_domain' => 'attacker.example',
'domain' => [
'action' => 'subdomain',
'subdomain_sld' => 'customer',
],
]);
$this->assertSame('example.com', $result['subdomain_base_domain']);
$this->assertSame('customer', $result['sld']);
$this->assertSame('.example.com', $result['tld']);
}
public function testPrependOrderConfigRequiresFreeSubdomainName(): void
{
$product = new \Model_Product();
$product->loadBean(new \DummyBean());
$product->config = json_encode([
'allow_subdomain' => true,
'subdomain_base_domain' => 'example.com',
]);
$this->service->setDi($this->getDi());
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Subdomain name is required.');
$this->service->prependOrderConfig($product, [
'domain' => [
'action' => 'subdomain',
],
]);
}
public function testPrependOrderConfigRejectsInvalidFreeSubdomainBaseDomain(): void
{
$product = new \Model_Product();
$product->loadBean(new \DummyBean());
$product->config = json_encode([
'allow_subdomain' => true,
'subdomain_base_domain' => 'foo-.example.com',
]);
$this->service->setDi($this->getDi());
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Subdomain base domain is invalid.');
$this->service->prependOrderConfig($product, [
'domain' => [
'action' => 'subdomain',
'subdomain_sld' => 'customer',
],
]);
}
// 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->getHpSearchQuery([]);
$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);
}
}