Files
FOSSBilling/tests-legacy/modules/Invoice/ServiceSubscriptionTest.php
T
b2f4cb8ffa Enhance API rate limiting, validation, and subscription features (#3459)
* Validate redirect paths and targets

* Sanitize guest client data and misc cleanups

* Implement API rate limiting and pruning

* Bump `last_patch` setting to 54

* Add tests for `shouldUseTokenLogin` logic

Add tests to cover logic added in #3451.

* Add order/renewal line config for domain pricing

Resolves #2793.

* Add PayPalEmail adapter subscription test

* Add width/height styling for textarea fields

Resolves #2448.

* Send invoice-created email on approve

Resolves #3012.

* Enforce cumulative cart stock and stockSale

Resolves #3130.

* Centralize subscription period retrieval

Resolves #1035.

* Support wildcard staff permissions (default.all)

Resolves #2376.

* Enable quantity selection and enforce stock max

Resolves #2573.

* Extend order expiry on first paid invoice

Resolves #2864.

* Rate-limit request prefix; invoice & promo fixes

* Update src/modules/Redirect/templates/admin/mod_redirect_settings.html.twig

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

* Update src/modules/Api/Service.php

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

* Update src/modules/Client/Controller/Client.php

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

* Update src/modules/Cron/Service.php

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

* Fix quantity not passed to getOrderLineConfig in createOrder

Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/669f845c-50c4-4f7e-95cd-f44de162ad72

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

* Simplify quantity injection into getOrderLineConfig call

Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/669f845c-50c4-4f7e-95cd-f44de162ad72

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

* Fix rate limit prefix isolation, whitelist headers, and staff controller prefix

Agent-Logs-Url: https://github.com/FOSSBilling/FOSSBilling/sessions/11f3fd58-bb44-46a5-a6cf-0992b038f4a7

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

* Enforce promo usage limit and add test

---------

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: admdly <2807620+admdly@users.noreply.github.com>
2026-04-28 22:11:39 +01:00

343 lines
10 KiB
PHP

<?php
declare(strict_types=1);
namespace Box\Mod\Invoice;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
#[Group('Core')]
final class ServiceSubscriptionTest extends \BBTestCase
{
protected ?ServiceSubscription $service;
public function setUp(): void
{
$this->service = new ServiceSubscription();
}
public function testGetDi(): void
{
$di = $this->getDi();
$this->service->setDi($di);
$getDi = $this->service->getDi();
$this->assertEquals($di, $getDi);
}
public function testCreate(): void
{
$subscriptionModel = new \Model_Subscription();
$subscriptionModel->loadBean(new \DummyBean());
$newId = 10;
$dbMock = $this->getMockBuilder('\Box_Database')
->getMock();
$dbMock->expects($this->atLeastOnce())
->method('dispense')
->willReturn($subscriptionModel);
$dbMock->expects($this->atLeastOnce())
->method('store')
->willReturn($newId);
$eventsMock = $this->getMockBuilder('\Box_EventManager')
->getMock();
$eventsMock->expects($this->atLeastOnce())
->method('fire');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['logger'] = new \Box_Log();
$di['events_manager'] = $eventsMock;
$this->service->setDi($di);
$data = [
'client_id' => 1,
'gateway_id' => 2,
];
$result = $this->service->create(new \Model_Client(), new \Model_PayGateway(), $data);
$this->assertIsInt($result);
$this->assertEquals($newId, $result);
}
public function testUpdate(): void
{
$subscriptionModel = new \Model_Subscription();
$subscriptionModel->loadBean(new \DummyBean());
$data = [
'status' => '',
'sid' => '',
'period' => '',
'amount' => '',
'currency' => '',
];
$dbMock = $this->getMockBuilder('\Box_Database')
->getMock();
$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($subscriptionModel, $data);
$this->assertTrue($result);
}
public function testToApiArray(): void
{
$subscriptionModel = new \Model_Subscription();
$subscriptionModel->loadBean(new \DummyBean());
$clientModel = new \Model_Client();
$clientModel->loadBean(new \DummyBean());
$gatewayModel = new \Model_PayGateway();
$gatewayModel->loadBean(new \DummyBean());
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('load')
->willReturnOnConsecutiveCalls($clientModel, $gatewayModel);
$clientServiceMock = $this->getMockBuilder(\Box\Mod\Client\Service::class)
->getMock();
$clientServiceMock->expects($this->atLeastOnce())
->method('toApiArray')
->willReturn([]);
$payGatewayService = $this->getMockBuilder(ServicePayGateway::class)
->getMock();
$payGatewayService->expects($this->atLeastOnce())
->method('toApiArray')
->willReturn([]);
$di = $this->getDi();
$di['mod_service'] = $di->protect(function ($serviceName, $sub = '') use ($clientServiceMock, $payGatewayService) {
if ($serviceName == 'Client') {
return $clientServiceMock;
}
if ($sub == 'PayGateway') {
return $payGatewayService;
}
});
$di['db'] = $dbMock;
$this->service->setDi($di);
$expected = [
'id' => '',
'sid' => '',
'period' => '',
'amount' => '',
'currency' => '',
'status' => '',
'created_at' => '',
'updated_at' => '',
'client' => [],
'gateway' => [],
];
$result = $this->service->toApiArray($subscriptionModel);
$this->assertIsArray($result);
$this->assertIsArray($result['client']);
$this->assertIsArray($result['gateway']);
$this->assertEquals($expected, $result);
}
public function testDelete(): void
{
$subscriptionModel = new \Model_Subscription();
$subscriptionModel->loadBean(new \DummyBean());
$dbMock = $this->getMockBuilder('\Box_Database')
->getMock();
$dbMock->expects($this->atLeastOnce())
->method('trash');
$eventsMock = $this->getMockBuilder('\Box_EventManager')
->getMock();
$eventsMock->expects($this->atLeastOnce())
->method('fire');
$di = $this->getDi();
$di['db'] = $dbMock;
$di['logger'] = new \Box_Log();
$di['events_manager'] = $eventsMock;
$this->service->setDi($di);
$result = $this->service->delete($subscriptionModel);
$this->assertTrue($result);
}
public static function searchQueryData(): array
{
return [
[
[], 'FROM subscription', [],
],
[
['status' => 'active'], 'AND status = :status', [':status' => 'active'],
],
[
['invoice_id' => '1'], 'AND invoice_id = :invoice_id', ['invoice_id' => '1'],
],
[
['gateway_id' => '2'], 'AND gateway_id = :gateway_id', [':gateway_id' => '2'],
],
[
['client_id' => '3'], 'AND client_id = :client_id', [':client_id' => '3'],
],
[
['currency' => 'EUR'], 'AND currency = :currency', [':currency' => 'EUR'],
],
[
['date_from' => '1234567'], 'AND UNIX_TIMESTAMP(created_at) >= :date_from', [':date_from' => '1234567'],
],
[
['date_to' => '1234567'], 'AND UNIX_TIMESTAMP(created_at) <= :date_to', [':date_to' => '1234567'],
],
[
['id' => '10'], 'AND id = :id', [':id' => '10'],
],
[
['sid' => '10'], 'AND sid = :sid', [':sid' => '10'],
],
];
}
#[DataProvider('searchQueryData')]
public function testGetSearchQuery(array $data, string $expectedSqlPart, array $expectedParams): void
{
$di = $this->getDi();
$this->service->setDi($di);
$result = $this->service->getSearchQuery($data);
$this->assertIsArray($result);
$this->assertIsString($result[0]);
$this->assertIsArray($result[1]);
$this->assertEquals($expectedParams, $result[1]);
$this->assertTrue(str_contains($result[0], $expectedSqlPart));
}
public function testIsSubscribableisNotSusbcribable(): void
{
$dbMock = $this->getMockBuilder('\Box_Database')
->getMock();
$dbMock->expects($this->atLeastOnce())
->method('getAll')
->willReturn([
['period' => '', 'price' => 10, 'quantity' => 1],
]);
$di = $this->getDi();
$di['db'] = $dbMock;
$this->service->setDi($di);
$invoice_id = 2;
$result = $this->service->isSubscribable($invoice_id);
$this->assertIsBool($result);
$this->assertFalse($result);
}
public function testIsSubscribable(): void
{
$dbMock = $this->getMockBuilder('\Box_Database')
->getMock();
$dbMock->expects($this->atLeastOnce())
->method('getAll')
->willReturn([
['period' => '1W', 'price' => 10, 'quantity' => 1],
]);
$di = $this->getDi();
$di['db'] = $dbMock;
$this->service->setDi($di);
$invoice_id = 2;
$result = $this->service->isSubscribable($invoice_id);
$this->assertIsBool($result);
$this->assertTrue($result);
}
public function testIsSubscribableWithRecurringAddonLines(): void
{
$dbMock = $this->getMockBuilder('\Box_Database')
->getMock();
$dbMock->expects($this->once())
->method('getAll')
->willReturn([
['period' => '1M', 'price' => 10, 'quantity' => 1],
['period' => '1M', 'price' => 2, 'quantity' => 1],
]);
$di = $this->getDi();
$di['db'] = $dbMock;
$this->service->setDi($di);
$this->assertTrue($this->service->isSubscribable(2));
}
public function testIsSubscribableRejectsMixedRecurringAndOneTimeLines(): void
{
$dbMock = $this->getMockBuilder('\Box_Database')
->getMock();
$dbMock->expects($this->once())
->method('getAll')
->willReturn([
['period' => '1M', 'price' => 10, 'quantity' => 1],
['period' => '', 'price' => 5, 'quantity' => 1],
]);
$di = $this->getDi();
$di['db'] = $dbMock;
$this->service->setDi($di);
$this->assertFalse($this->service->isSubscribable(2));
}
public function testGetSubscriptionPeriod(): void
{
$period = '1W';
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('getAll')
->willReturn([
['period' => $period, 'price' => 10, 'quantity' => 1],
['period' => '', 'price' => -2, 'quantity' => 1],
]);
$di = $this->getDi();
$di['db'] = $dbMock;
$this->service->setDi($di);
$invoiceModel = new \Model_Invoice();
$invoiceModel->loadBean(new \DummyBean());
$result = $this->service->getSubscriptionPeriod($invoiceModel);
$this->assertIsString($result);
$this->assertEquals($period, $result);
}
public function testUnsubscribe(): void
{
$subscribtionModel = new \Model_Subscription();
$subscribtionModel->loadBean(new \DummyBean());
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('store');
$di = $this->getDi();
$di['db'] = $dbMock;
$this->service->setDi($di);
$this->service->unsubscribe($subscribtionModel);
}
}