Files
FOSSBilling/tests-legacy/modules/Servicedownloadable/Api/AdminTest.php
T
f27d2e3646 Fix downloadable products (#2822)
* Fixed Downloadable product issue where the product config was overwritten if the settings were saved.

* Ran phpstan & cs-fixer

* Added pdate function to fix products where possible

* Fixed uploading new files when the config is broken.

* Added fix to updater

* Removed superfluous line

* Bump Last Patch Level to Fix Live Tests

* Clean up file download response headers and fix `json_decode` usage

* Handle null config in saveProductConfig and sendProductFile

* Apply suggestion from @Copilot

Co-Authored-By: Copilot <175728472+Copilot@users.noreply.github.com>

* Fix file upload path and improve error handling

* Refactor file update logic in Service class

Replaces inline update of the servicedownloadable record with a call to updateProductFile, improving code organization and maintainability.

* Update `last_patch` setting to version 48

Incremented the 'last_patch' value in the settings table from 47 to 48 to reflect the latest patch version.

* Improve downloadable product filename recovery in patch

Enhanced patch 48 in UpdatePatcher to more robustly recover missing filenames for downloadable products by checking both order configs and service_downloadable records, updating all related data sources, and cleaning up orphaned records. Added detailed statistics logging for the recovery process. Also removed an obsolete PHPStan ignore rule for polyfilled ctype functions.

* Add required param validation to config_save method

Introduced explicit required parameter validation for the 'id' field in the config_save method of the Admin API. This ensures that a missing product ID is caught early with a clear error message.

* Refactor file handling and patch logic for downloadable products

Simplifies and streamlines the patch logic for recovering lost filenames in downloadable products, removing verbose statistics and redundant comments. Refactors file upload error handling in Servicedownloadable Service to use a dedicated error message method and eliminates unnecessary try/catch blocks. Updates Admin API to use attribute-based required parameter validation for product file downloads.

* Update src/modules/Servicedownloadable/Service.php

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

* Apply suggestions from code review

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

* Refactor Servicedownloadable Admin API param validation

Removed redundant manual required param validation in Admin API methods in favor of attribute-based validation. Updated related tests to reflect this change and added more comprehensive tests for file sending scenarios. Also updated HTML form element IDs for clarity.

* Remove redundant test for missing product ID in config save

Deleted the testConfigSaveMissingId test from AdminTest.php as it is no longer needed. This streamlines the test suite by removing duplicate or unnecessary validation checks.

---------

Co-authored-by: Chris Schläpfer <c.schlaepfer@datact.ch>
Co-authored-by: Adam Daley <admdly@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2025-12-30 04:05:30 +00:00

255 lines
8.4 KiB
PHP

<?php
declare(strict_types=1);
namespace Box\Mod\Servicedownloadable\Api;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\Group;
#[Group('Core')]
final class AdminTest extends \BBTestCase
{
protected ?Admin $api;
public function setUp(): void
{
$this->api = new Admin();
}
public function testGetDi(): void
{
$di = $this->getDi();
$this->api->setDi($di);
$getDi = $this->api->getDi();
$this->assertEquals($di, $getDi);
}
public function testUpdateOrderNotActivated(): void
{
$data['order_id'] = 1;
$model = new \Model_ClientOrder();
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('getExistingModelById')
->willReturn($model);
$orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class);
$orderServiceMock->expects($this->atLeastOnce())
->method('getOrderService');
$validatorMock = $this->getMockBuilder(\FOSSBilling\Validate::class)->disableOriginalConstructor()->getMock();
$di = $this->getDi();
$di['db'] = $dbMock;
$di['mod_service'] = $di->protect(fn () => $orderServiceMock);
$di['validator'] = $validatorMock;
$this->api->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Order is not activated');
$this->api->update($data);
}
public function testUpdate(): void
{
$data['order_id'] = 1;
$model = new \Model_ClientOrder();
$modelDownloadableModel = new \Model_ServiceDownloadable();
$serviceMock = $this->createMock(\Box\Mod\Servicedownloadable\Service::class);
$serviceMock->expects($this->atLeastOnce())
->method('updateProductFile')
->willReturn(true);
$orderServiceMock = $this->createMock(\Box\Mod\Order\Service::class);
$orderServiceMock->expects($this->atLeastOnce())
->method('getOrderService')
->willReturn($modelDownloadableModel);
$dbMock = $this->createMock('\Box_Database');
$dbMock->expects($this->atLeastOnce())
->method('getExistingModelById')
->willReturn($model);
$di = $this->getDi();
$di['db'] = $dbMock;
$di['mod_service'] = $di->protect(fn () => $orderServiceMock);
$this->api->setDi($di);
$this->api->setService($serviceMock);
$result = $this->api->update($data);
$this->assertIsBool($result);
$this->assertTrue($result);
}
public function testConfigSave(): void
{
$data = [
'id' => 1,
'update_orders' => true,
];
$productModel = new \Model_Product();
$productModel->loadBean(new \DummyBean());
$productModel->config = '{"filename": "test.txt"}';
$serviceMock = $this->getMockBuilder('\\' . \Box\Mod\Servicedownloadable\Service::class)->getMock();
$serviceMock->expects($this->atLeastOnce())
->method('saveProductConfig')
->with($productModel, $data)
->willReturn(true);
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('getExistingModelById')
->with('Product', $data['id'], 'Product not found')
->willReturn($productModel);
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$this->api->setDi($di);
$this->api->setService($serviceMock);
$result = $this->api->config_save($data);
$this->assertIsBool($result);
$this->assertTrue($result);
}
public function testConfigSaveProductNotFound(): void
{
$data = [
'id' => 999,
'update_orders' => true,
];
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('getExistingModelById')
->with('Product', $data['id'], 'Product not found')
->willThrowException(new \FOSSBilling\Exception('Product not found'));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$this->api->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Product not found');
$this->api->config_save($data);
}
public function testSendFileProductNotFound(): void
{
$data = ['id' => 999];
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('getExistingModelById')
->with('Product', $data['id'], 'Product not found')
->willThrowException(new \FOSSBilling\Exception('Product not found'));
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$this->api->setDi($di);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('Product not found');
$this->api->send_file($data);
}
public function testSendFileNoFileConfigured(): void
{
$data = ['id' => 1];
$productModel = new \Model_Product();
$productModel->loadBean(new \DummyBean());
$productModel->config = '{}';
$serviceMock = $this->createMock(\Box\Mod\Servicedownloadable\Service::class);
$serviceMock->expects($this->atLeastOnce())
->method('sendProductFile')
->with($productModel)
->willThrowException(new \FOSSBilling\Exception('No file associated with this product.', null, 404));
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('getExistingModelById')
->with('Product', $data['id'], 'Product not found')
->willReturn($productModel);
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$this->api->setDi($di);
$this->api->setService($serviceMock);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('No file associated with this product.');
$this->api->send_file($data);
}
public function testSendFileFileNotFound(): void
{
$data = ['id' => 1];
$productModel = new \Model_Product();
$productModel->loadBean(new \DummyBean());
$productModel->config = '{"filename": "test.txt"}';
$serviceMock = $this->createMock(\Box\Mod\Servicedownloadable\Service::class);
$serviceMock->expects($this->atLeastOnce())
->method('sendProductFile')
->with($productModel)
->willThrowException(new \FOSSBilling\Exception('File cannot be downloaded at the moment. Please contact support.', null, 404));
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('getExistingModelById')
->with('Product', $data['id'], 'Product not found')
->willReturn($productModel);
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$this->api->setDi($di);
$this->api->setService($serviceMock);
$this->expectException(\FOSSBilling\Exception::class);
$this->expectExceptionMessage('File cannot be downloaded at the moment. Please contact support.');
$this->api->send_file($data);
}
public function testSendFile(): void
{
$data = ['id' => 1];
$productModel = new \Model_Product();
$productModel->loadBean(new \DummyBean());
$productModel->config = '{"filename": "test.txt"}';
$serviceMock = $this->createMock(\Box\Mod\Servicedownloadable\Service::class);
$serviceMock->expects($this->atLeastOnce())
->method('sendProductFile')
->with($productModel)
->willReturn(true);
$dbMock = $this->getMockBuilder('\Box_Database')->getMock();
$dbMock->expects($this->atLeastOnce())
->method('getExistingModelById')
->with('Product', $data['id'], 'Product not found')
->willReturn($productModel);
$di = new \Pimple\Container();
$di['db'] = $dbMock;
$this->api->setDi($di);
$this->api->setService($serviceMock);
$result = $this->api->send_file($data);
$this->assertIsBool($result);
$this->assertTrue($result);
}
}